Python 相当于 Solidity u256

Python equivalent to Solidity u256

我正在研究 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 不兼容。有什么办法可以解决吗?

我会将你的 xy 转换成这样:

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

编辑:我重构了这个答案,使用 numpy item() method.

将 numpy int64 转换为 Python int