ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.96k stars 1.69k forks source link

Issue with event args returned #1190

Closed ayeteng closed 5 years ago

ayeteng commented 5 years ago
abci==0.6.0
adal==1.2.0
aiohttp==3.2.1
anyconfig==0.9.7
appnope==0.1.0
argh==0.26.2
asn1crypto==0.24.0
astroid==2.1.0
async-lru==0.1.0
async-timeout==3.0.1
atomicwrites==1.2.1
attrdict==2.0.0
attrs==18.2.0
autopep8==1.4.3
backcall==0.1.0
bcrypt==3.1.5
bitcoin==1.1.42
bleach==3.0.2
bumpversion==0.5.3
cachetools==3.0.0
certifi==2018.11.29
cffi==1.11.5
chardet==3.0.4
click==6.7
coincurve==7.1.0
colorlog==4.0.2
contextlib2==0.5.5
coverage==4.5.2
cryptography==2.4.2
cytoolz==0.9.0.1
dataclasses==0.6
decorator==4.3.0
defusedxml==0.5.0
distro==1.3.0
docopt==0.6.2
dpath==1.4.2
entrypoints==0.2.3
eth-abi==1.3.0
eth-account==0.2.3
eth-bloom==1.0.1
eth-hash==0.2.0
eth-keyfile==0.5.1
eth-keys==0.2.0b3
eth-rlp==0.1.2
eth-tester==0.1.0b26
eth-testrpc==1.3.5
eth-typing==2.0.0
eth-utils==1.4.1
ethereum==2.3.2
ethereum-input-decoder==0.2.2
ethpm==0.1.3
ExifRead==2.1.2
Flask==1.0.2
flex==6.13.2
future==0.17.1
GDAL==2.3.2
gevent==1.3.7
google-auth==1.6.2
gprof2dot==2017.9.19
gpxpy==1.3.4
greenlet==0.4.15
hexbytes==0.1.0
idna==2.6
idna-ssl==1.1.0
ifaddr==0.1.6
ipaddress==1.0.22
ipfsapi==0.4.3
IPTCInfo3==2.0.3
ipykernel==5.1.0
ipython==6.5.0
ipython-genutils==0.2.0
ipywidgets==7.4.2
isort==4.3.4
itsdangerous==1.1.0
jedi==0.13.2
Jinja2==2.10
json-rpc==1.11.1
json-spec==0.10.1
jsonpointer==1.14
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.2.4
jupyter-console==6.0.0
jupyter-core==4.4.0
keyring==17.1.1
kubernetes==8.0.0
lazy-object-proxy==1.3.1
lru-dict==1.1.6
lxml==4.2.5
mapnik==3.1.0
MarkupSafe==1.1.0
mccabe==0.6.1
mistune==0.8.4
more-itertools==4.3.0
multidict==4.5.2
mysql-connector-python==8.0.13
mysqlclient==1.3.14
nbconvert==5.4.0
nbformat==4.4.0
netdisco==2.2.0
netifaces==0.10.7
notebook==5.7.4
numpy==1.15.4
oauthlib==2.1.0
pandas==0.23.4
pandocfilters==1.4.2
parsimonious==0.8.1
parso==0.3.1
pathtools==0.1.2
pbkdf2==1.3
pexpect==4.6.0
pickleshare==0.7.5
Pillow==5.3.0
pipreqs==0.4.9
pluggy==0.8.0
plyvel==1.0.4
prometheus-client==0.5.0
prompt-toolkit==1.0.15
protobuf==3.6.1
ptyprocess==0.6.0
py==1.7.0
py-ecc==1.4.7
py-evm==0.2.0a18
py-geth==2.0.1
py-solc==2.2.0
pyasn1==0.4.4
pyasn1-modules==0.2.2
pycairo==1.18.0
pycodestyle==2.4.0
pycparser==2.19
pycryptodome==3.7.2
pyethash==0.1.27
Pygments==2.3.1
PyGObject==3.30.4
PyJWT==1.7.1
pylint==2.2.2
pylru==1.1.0
pymongo==3.7.2
PyMySQL==0.9.3
pysha3==1.0.2
pytest==4.0.2
pytest-cov==2.6.0
pytest-pythonpath==0.7.3
python-dateutil==2.7.5
python-magic==0.4.15
python-telegram-bot==11.1.0
pytz==2018.7
PyYAML==3.12
pyzmq==17.1.2
qtconsole==4.4.3
redis==3.0.1
repoze.lru==0.7
requests==2.18.4
requests-oauthlib==1.0.0
rfc3987==1.3.8
rlp==1.0.3
rope==0.11.0
rsa==4.0
scrypt==0.8.6
secp256k1==0.13.2
semantic-version==2.6.0
Send2Trash==1.5.0
simplegeneric==0.8.1
six==1.12.0
slackclient==1.3.0
strict-rfc3339==0.7
swagger-py-codegen==0.3.7
tabula-py==1.3.1
tastyworks==3.1.0
terminado==0.8.1
testpath==0.4.2
toolz==0.9.0
toposort==1.5
tornado==5.1.1
traitlets==4.3.2
trie==1.3.8
uPnPClient==0.0.8
urllib3==1.22
validate-email==1.3
watchdog==0.9.0
wcwidth==0.1.7
web3==4.8.2
webencodings==0.5.1
websocket-client==0.54.0
websockets==6.0
Werkzeug==0.14.1
widgetsnbextension==3.4.2
wrapt==1.10.11
yarg==0.1.9
yarl==1.3.0
zeroconf==0.21.3

What was wrong?

I cannot get correct args which returned by event

Please include any of the following that are applicable:

My solidity code:

pragma solidity ^0.5.1;

contract myproject {

    int private_counter = 0;

    //// Event
    event notifyDataSetComplete(int, int, string);

    //// Functions
    function setDocData(int IDX, string memory NAME) public payable
    {
       private_counter += 1;
       emit notifyDataSetComplete(private_counter, IDX, NAME);
    }
}

My python code:

contract_instance = web3.eth.contract(address=contract_addr, abi=json.loads(contract_abi))
IDX = 1
name = "AA"
tx_hash = contract_instance.functions.setDocData(IDX, name)
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash, timeout=600)
logs = contract_instance.events.notifyDataSetComplete().processReceipt(tx_receipt)
print(logs['args'])

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.

pipermerriam commented 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>)
ayeteng commented 5 years ago

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?

ayeteng commented 5 years ago

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