Closed ayeteng closed 5 years ago
Your python code looks broken.
# this line would not return a transaction hash, but rather a `ContractFunction` object.
tx_hash = contract_instance.functions.setDocData(IDX, name)
# this would be the correct line
tx_hash = contract_instance.functions.setDocData(IDX, name).transact(<transaction_params>)
Yes @pipermerriam. I forget to copy the transact function as you mentioned. But still, I cannot get correct returned args after revise the code. Can you help please? Is this related with solidity 0.5.1? py-solc doesn't support 0.5 yet and perhaps the issue comes with the unsupported ABI structure?
solved my bug. The issue is in the smart contract. Sorry to bother you. The complete code as below:
import json
import web3
from web3 import Web3, HTTPProvider
from solc import compile_source
from web3.middleware import geth_poa_middleware
# Solidity source code
contract_source_code = '''
pragma solidity ^0.5.2;
contract StoreVar {
uint8 public _myVar = 5;
uint8 public _count = 0;
event MyEvent(uint _myVar, uint _count);
function setVar(uint8 _var) public {
_myVar = _var;
_count += 1;
emit MyEvent(_myVar, _count);
}
function getVar() public view returns (uint8) {
return _myVar;
}
function getVarAndCount() public view returns(uint8, uint8) {
return (_myVar, _count);
}
}
'''
compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:StoreVar']
# web3.py instance
web3_server = 'http://localhost:8545'
w3 = Web3(HTTPProvider(web3_server, request_kwargs={'timeout': 240}))
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
# set pre-funded account as sender
w3.eth.defaultAccount = w3.eth.accounts[0]
# Instantiate and deploy contract
StoreVar = w3.eth.contract(abi=contract_interface['abi'],
bytecode=contract_interface['bin'])
# Submit the transaction that deploys the contract
tx_hash = StoreVar.constructor().transact()
# Wait for the transaction to be mined, and get the transaction receipt
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
# Create the contract instance with the newly-deployed address
MyVar = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=contract_interface['abi'],
)
# Display the default var from the contract
print('Default var = %d' % (MyVar.functions.getVar().call()))
print('Setting the var to 10...')
tx_hash = MyVar.functions.setVar(10).transact()
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=600)
logs = MyVar.events.MyEvent().processReceipt(tx_receipt)
print(logs[0]['args'])
# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)
# Display the new value
value = MyVar.functions.getVarAndCount().call()
print('Var = %d , Count = %d' % (value[0], value[1]))
pip freeze
outputWhat was wrong?
I cannot get correct args which returned by event
Please include any of the following that are applicable:
My solidity code:
My python code:
result: AttributeDict({'': 'AA'})
expect: 1, 1, 'AA'
How can it be fixed?
Fill this section in if you know how this could or should be fixed.