from __future__ import unicode_literals

import json
from time import sleep, time # 中文编码
def encode_str(_str):
try:
return bytearray(_str, "utf-8").hex()
except Exception as e:
print(e) # 中文解码
def decode_str(_str):
try:
return bytearray.fromhex(_str).decode("utf-8")
except Exception as e:
print(e) MATH_BYTECODE = "6060604052341561000f57600080fd5b5b60c08061001e6000396000f300606060405263ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663696c75c68114603c575b600080fd5b3415604657600080fd5b60526004356024356054565b005b817f3ea5d667934df8dbb4978ddd30e65375fd1bf06f8ed39d033379c5cd3efb102c428360405191825260208201526040908101905180910390a25b50505600a165627a7a723058208d6649bbcad0893ba1e52ea3904adaf2ff4b9ed80f71f5ac5c6b982ce5bac3a20029"
# 6060604052341561000f57600080fd5b5b6105078061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c90d8448114610048578063d10fa94614610111575b600080fd5b341561005357600080fd5b61009960046024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506101e395505050505050565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100d65780820151818401525b6020016100bd565b50505050905090810190601f1680156101035780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561011c57600080fd5b61016260046024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506102fa95505050505050565b604051821515815260406020820181815290820183818151815260200191508051906020019080838360005b838110156101a75780820151818401525b60200161018e565b50505050905090810190601f1680156101d45780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6101eb610429565b600080836040518082805190602001908083835b6020831061021f57805182525b601f1990920191602091820191016101ff565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390209050806001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102ec5780601f106102c1576101008083540402835291602001916102ec565b820191906000526020600020905b8154815290600101906020018083116102cf57829003601f168201915b505050505091505b50919050565b6000610304610429565b60408051908101604052808481526020016040805190810160405280600981526020017fe4b88de5ad98e59ca800000000000000000000000000000000000000000000008152508152506000846040518082805190602001908083835b6020831061038157805182525b601f199092019160209182019101610361565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040519081900390208151819080516103c992916020019061043b565b506020820151816001019080516103e492916020019061043b565b50905050600060408051908101604052600681527f6f6b6f6b31310000000000000000000000000000000000000000000000000000602082015290925090505b915091565b60206040519081016040526000815290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061047c57805160ff19168380011785556104a9565b828001600101855582156104a9579182015b828111156104a957825182559160200191906001019061048e565b5b506104b69291506104ba565b5090565b6104d891905b808211156104b657600081556001016104c0565b5090565b905600a165627a7a72305820faf711785ac7369219291016aa91535aa22004c60da82a8fcd956777c3b7a7000029
MATH_ABI = json.loads("""
[
{
"constant": false,
"inputs": [
{
"name": "_type",
"type": "bytes32"
},
{
"name": "_metadata",
"type": "bytes32"
}
],
"name": "publish",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_type",
"type": "bytes32"
},
{
"indexed": false,
"name": "_created",
"type": "uint256"
},
{
"indexed": false,
"name": "_metadata",
"type": "bytes32"
}
],
"name": "ArticleOperationLog",
"type": "event"
}
]
""") import web3.personal as wp
from web3 import Web3, HTTPProvider web3 = Web3(HTTPProvider('http://localhost:8545'))
web3.eth.defaultAccount = b'0x3364aa6342c8c18' print("unlockAccount")
p = wp.Personal(web3)
print(p.unlockAccount("0x336394f4b93aa6342c8c18", "123456"))
print("unlockAccount ok") MathContract = web3.eth.contract(
abi=MATH_ABI,
# bytecode=MATH_BYTECODE,
) # deploy_txn = MathContract.deploy()
# transaction={
# 'from': b'0x336495440ce8dd55029bd394f4b93aa6342c8c18',
# 'value': 12345,
# },
# args=('DGD', 18) print("isConnected:", web3.isConnected()) # print(deploy_txn)
# deploy_receipt = web3.eth.getTransactionReceipt("0x71ce48069f2e17079c2cf016f40bd47d995f4c77aab74d12ae89a9cf13cfeb77")
# print(deploy_receipt, "\n\n") # print(deploy_txn)
# deploy_receipt = web3.eth.getTransactionReceipt(b"0xb2e4aacfbd147ddd6f807c0f4d47cc77c6a21ce7e2c9cbd17b7ba3d410473bd4")
# print(deploy_receipt)
# assert deploy_receipt is not None # deploy_receipt["contractAddress"]
# math_contract = MathContract(address=deploy_receipt["contractAddress"])
import re def my_callback(a):
# print(a)
args = a.get("args")
print(decode_str(re.split(r"\W+", args.get("_type"))[0]))
print(str(args.get("_created")))
print(decode_str(re.split(r"\W+", args.get("_metadata"))[0]))
print("\n\n") def my_callback1(a):
print("\n\n\n\npastEvents")
# print(a)
args = a.get("args")
print(decode_str(re.split(r"\W+", args.get("_type"))[0]))
print(str(args.get("_created")))
print(decode_str(re.split(r"\W+", args.get("_metadata"))[0])) math_contract = MathContract(address="0x8851accb27fc4b149bda886d6d579f2fb5c1a492")
# math_contract.on("ArticleOperationLog",{'filter': {'_type': 'hello'}}).watch(my_callback) # math_contract.on("ArticleOperationLog").watch(my_callback)
# math_contract.on("ArticleOperationLog", {"filter": {"_type": encode_str("hello")}}).watch(my_callback) # 历史日志
math_contract.pastEvents("ArticleOperationLog", {"filter": {"_type": encode_str("hello1")}}).watch(my_callback1) # 历史日志
# math_contract.pastEvents("ArticleOperationLog").watch(my_callback1) while 1:
# math_contract.transact().publish(encode_str("hello1"), encode_str("hello1上海"))
print(time())
sleep(2) # _a = "1234562" # print(math_contract.encodeABI("addDNA", args=(_a,_a,_a))) # 发送一个tx到区块链上
# print(math_contract.transact().addDNA(_a, _a, _a)) # 发送一个tx到本地执行,不会写入到区块链上
# print(math_contract.call().addDNA(_a, _a, _a))
# print(math_contract.call().getDNA(_a))

python 智能合约日志操作的更多相关文章

  1. python中的日志操作和发送邮件

    1.python中的日志操作 安装log模块:pip install nnlog 参数:my_log = nnlog.Logger('server_log.log',level='debug',bac ...

  2. [Python之路] 日志操作

    使用logging模块来写日志 日志直接输出到准备输出 import logging logging.basicConfig(level=logging.WARNING, format="% ...

  3. EOS开发基础之六:使用cleos命令行客户端操作EOS——智能合约之eosio.msig和eosio.system

    上一节我们搞了老半天,也没整明白Exchange这个合约到底干啥用的.就它官方提供的说法,是用于货币的创造和交易.我没整明白,所以去看了下代码,发现代码头文件里面有下面这段话: This contra ...

  4. EOS开发基础之四:使用cleos命令行客户端操作EOS——智能合约之eosio.bios和eosio.token

    现实世界中的合约,简单地说,是一个参与活动的所有人都需要遵循的协议.合约可以是正式的法律合同(例如,金融交易),或者是简单的游戏规则.典型的活动可以是诸如资金转移(在金融合约的情况下)或游戏动作(在游 ...

  5. 从零开始的Python学习Episode 14——日志操作

    日志操作 一.logging模块 %(message)s 日志信息 %(levelno)s 日志级别 datefmt 设置时间格式 filename 设置日志保存的路径 level 设置日志记录的级别 ...

  6. Python之日志操作(logging)

    import logging   1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...

  7. Hyperledger Fabric Chaincode for Operators——实操智能合约

    什么是Chaincode(智能合约)? chaincode是一个程序,它是使用Go语言编写的,最终在Java等其他编程语言中实现了指定的接口.chaincode运行在一个被背书peer进程独立出来的安 ...

  8. 【精解】EOS智能合约演练

    EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...

  9. 以太坊智能合约虚拟机(EVM)原理与实现

    以太坊 EVM原理与实现 以太坊底层通过EVM模块支持合约的执行与调用,调用时根据合约地址获取到代码,生成环境后载入到EVM中运行.通常智能合约的开发流程是用solidlity编写逻辑代码,再通过编译 ...

随机推荐

  1. Oracle中常用的语句

    1.查询锁表 SELECT a.object_name,b.session_id,c.serial#,c.program,c.username,c.command,c.machine,c.lockwa ...

  2. var let const的一些区别

    var let const 都是来定义变量的. var let 作用域有些区别. const 类似于java中的常量的概念.即:只能给一个变量赋值一次,即指定一个引用. 举例来说: function ...

  3. Redis服务端和客户端的命令

    服务器端 服务器端的命令为redis-server 可以使⽤help查看帮助⽂档 redis-server --help 个人习惯 ps aux | grep redis 查看redis服务器进程su ...

  4. Comparable和Compartor的区别

    1.List对象实现Comparable接口,使对象具备可比性 package tao; import java.util.ArrayList; import java.util.Collection ...

  5. 常用关于时间的一些设置。获取当前时间后30天;判断时间段一年内;Date转String,String转Date

        //获取当前时间后30天(之前也可),天数不限可修改1 var data = new Date(); var date1 = newe Date(date); date2 = date1.se ...

  6. 在tornado中使用异步mysql操作

    在使用tornado框架进行开发的过程中,发现tornado的mysql数据库操作并不是一步的,造成了所有用户行为的堵塞.tornado本身是一个异步的框架,要求所有的操作都应该是异步的,但是数据库这 ...

  7. HTML-CSS的几种布局

    第一种  两栏式布局 <body> <!-- 两栏式布局 --> <!-- 想要的效果是左边图片右边文字 拉伸盒子文字的高度宽度自动改变 --> <div c ...

  8. python学习笔记:第13天 内置函数(一)

    详细文件查看点击这里:详细地址

  9. PAT A1127 ZigZagging on a Tree (30 分)

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  10. ubuntu 防止软件包自动更新

    阻止软件包升级 有两种方法阻止软件包升级,使用dpkg,或者在Woody中使用APT. 使用dpkg,首先导出软件包选择列表: dpkg --get-selections \* > select ...