英文:
Trying to tokenise Ethereum TX sender "from" to crosscheck values
问题
这段代码尝试从一个Web3项目中的交易中获取精确的数值(from,to,value),然后通过Python来验证这些值是否符合预期。下面是已经正常工作的部分:
txtest = web3.eth.get_transaction('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4')
finaltx = txtest.to
print('This is the raw tx result', txtest)
print('This is the sender address:', finaltx)
这段代码成功获取并打印了交易的详细信息,包括发送者地址。
但是,如果你尝试从AttributeDict中获取"from"字段,你会遇到SyntaxError:
finaltx = txtest.from
这是因为 "from" 是Python中的保留关键字,不能作为变量名使用。要获取发送者地址,你可以使用以下代码:
addressfrom = txtest['from']
这样就可以获取发送者地址而不会遇到语法错误。希望这对你有帮助!
英文:
Trying to get back to python with a web3 project. I am trying to extrapolate precise values from a tx (from, to, value) to crosscheck via python if those values are the expected one.
txtest = web3.eth.get_transaction('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4')
finaltx = txtest.to
print('This is the raw tx result ', txtest)
print('This is the sender address: ', finaltx)
perfectly works:
This is the raw tx result AttributeDict({'blockHash': HexBytes('0x3ec30c2f44d15e55e3caee66ae238819ae2809d81a0df6b0ca58fa722a78ae91'), 'blockNumber': 16751195, 'from': '0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5', 'gas': 21000, 'gasPrice': 22256602635, 'maxFeePerGas': 22256602635, 'maxPriorityFeePerGas': 0, 'hash': HexBytes('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4'), 'input': '0x', 'nonce': 252468, 'to': '0xe688b84b23f322a994A53dbF8E15FA82CDB71127', 'transactionIndex': 166, 'value': 274458557610409937, 'type': '0x2', 'accessList': [], 'chainId': '0x1', 'v': 0, 'r': HexBytes('0xe2a1cf9bf7ec838468a03929c535a536462e6e4cde2978e4a1e39826a738f534'), 's': HexBytes('0x08a9073666cb98adcd7166ffc216f1b23b09b9463a703440427047cae6d95217')})
This is the sender address: 0xe688b84b23f322a994A53dbF8E15FA82CDB71127
but if i try to fetch the "from" in the AttributeDict with finaltx = txtest.from
then i can't run the code:
txtest = web3.eth.get_transaction('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4')
finaltx = txtest.to
print('This is the raw tx result ', txtest)
print('This is the sender address: ', finaltx)
addressfrom = txtest.from
errors with
finaltx = txtest.from
^
SyntaxError: invalid syntax
Thank you in advance!
答案1
得分: 0
Found the solution.
Might sound dumb from me but as from
is reserved for code, in order to call addressfrom = txtest.from
in a readable state for python, it has to be passed with getattr()
, so the solution is:
addressfrom = getattr(txtest, 'from')
print(addressfrom)
which outputs the right address ^^
英文:
Found the solution.
Might sound dumb from me but as from
is reserved for code, in order to call addressfrom = txtest.from
in a readable state for python, it has to be passed with getattr()
, so the solution is:
addressfrom = getattr(txtest, 'from')
print(addressfrom)
which outputs the right address ^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论