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))