【问题标题】:Python equivalent to Solidity u256Python相当于Solidity u256
【发布时间】:2021-10-13 10:22:57
【问题描述】:

我正在研究 Solidity 智能合约。这个想法是使用 Python 自动化一些任务。所以我有这个代码:

idx = 1
event_id = cDF.iloc[idx]["A"].astype(int)
event_date = cDF.iloc[idx]["B"].astype(int)
x = cDF.iloc[idx]["C"].astype(int)
y = cDF.iloc[idx]["D"].astype(int)

a = 69295
print(type(a))
print(type(event_id))

txcreation_txn = contract.functions.publishEvent(event_id, event_date, x, y).buildTransaction({
    'from': <some_testing_wallet>, 
    'value': 0,
    'gas': 3000000000000,
    'gasPrice': w3.toWei('1', 'gwei'),
    'nonce': nonce_var})
signed_txn = w3.eth.account.sign_transaction(txcreation_txn, private_key=private_key)
result = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"result # {idx} - {result.hex()}")

我收到以下错误:

Could not identify the intended function with name `publishEvent`, positional argument(s) of type 
`(<class 'numpy.int64'>, <class 'numpy.int64'>, <class 'numpy.int64'>, <class 'numpy.int64'>)` and keyword 
argument(s) of type `{}`.
Found 1 function(s) with the name `publishEvent`: ['publishEvent(uint256,uint256,uint256,uint256)']
Function invocation failed due to no matching argument types.

但是,当我手动运行它时,它可以工作并将 tx 发送到区块链:

betcreation_txn = contract.functions.publishEvent(69295,1628516242331,24,28)
.buildTransaction({'from': '0xDaf36E4570e2f0A587331b8E1E3645Ce8861B6A5', 'value': 0,
'gas': 3000000,'gasPrice': w3.toWei('1', 'gwei'),'nonce': nonce_var}) 

Solidity 中的函数签名:

  function publishEvent(uint256 _event_id, uint256 _event_date, uint256 _x, uint256 _y) payable public

所以我想问题是我从 Python 发送的数据与 Solidity u256 不兼容。有没有办法解决这个问题?

【问题讨论】:

  • 你在使用 web3.py 吗?
  • from web3 import Web3
  • 你确实是:)
  • betcreation_txn = contract.functions.publishEvent(69295,1628516242331,24,28).buildTransaction({'from': &lt;wallet&gt;, 'value': 0,'gas': 3000000,'gasPrice': w3.toWei('1', 'gwei'),'nonce': nonce_var}) signed_txn = w3.eth.account.sign_transaction(betcreation_txn, private_key=pk) result = w3.eth.send_raw_transaction(signed_txn.rawTransaction) 这是代码...这适用于 'publishEvent(69295,1628516242331,24,28)'

标签: python web3


【解决方案1】:

我会将您的 xy 转换为这个,如下所示:

x = cDF.iloc[idx]["C"].astype(int).item()
y = cDF.iloc[idx]["D"].astype(int).item()

编辑:我已重构此答案以使用 numpy item() method 将 numpy int64 转换为 Python int

【讨论】:

  • 非常感谢您的指点...我现在收到此错误...TypeError: Unsupported type: '&lt;class 'numpy.int64'&gt;'. Must be one of: bool, str, bytes, bytearrayor int.
  • @MartinRasumoff 刚刚对其进行了编辑,让我知道它是如何工作的。
  • 非常感谢先生......不,它没有......它现在将其转换为字符串:ValidationError: Could not identify the intended function with name `publishEvent`, positional argument(s) of type `(&lt;class 'str'&gt;, &lt;class 'str'&gt;, &lt;class 'str'&gt;, &lt;class 'str'&gt;)` and keyword argument(s) of type `{}`. Found 1 function(s) with the name `publishEvent`: ['publishEvent(uint256,uint256,uint256,uint256)'] Function invocation failed due to no matching argument types. 已经为此工作了 2 天 :( 感谢任何指针...
  • @MartinRasumoff 尝试给出答案,去掉 toHex 行,只使用xy
  • 是的!!!!现在我得到一个气体错误,但至少不是数据类型!!!!非常感谢先生!!!!!!
猜你喜欢
  • 2010-10-29
  • 2011-06-25
  • 2019-01-08
  • 2014-03-06
  • 2012-12-03
  • 2016-06-23
  • 2011-05-29
  • 2011-12-12
  • 2016-05-08
相关资源
最近更新 更多