Python equivalent to Solidity u256

Question:

I am working on a Solidity Smart Contract. The idea is to automate some of the tasks using Python. So I have this code:

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()}")

for which I get the following error:

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.

However, it works and send the tx to the blockchain when I ran this manually:

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

the function signature in Solidity:

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

So I guess the issue is that the data I am sending from Python is not compatible with Solidity u256. Is there any way to solve this?

Asked By: Martin Rasumoff

||

Answers:

I’d convert your x and y to this, like so:

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

EDIT: I have refactored this answer to convert the numpy int64‘s to Python int‘s using the numpy item() method.

Answered By: dir
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.