1

I am trying to use web3.py to interact with a smart contract deployed on Ropsten. The Contract lives on the address 0xb80206789De27D14b3f458Bed9072d4a0129b41e and has two functions, one to save a string and one for an integer. I need to be able to use a python script to set the string and the integer on the contract and read from it. I have managed reading, but writing is where I find problems. the problem lies on how to build a transaction containing a new integer I want to set- (note: through remix.eethereum.org, the contract works and interacts fine)

solidity contract code:

pragma solidity ^0.6.2;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract dbim_basic is Ownable {
    string public IPFSHash = "I pretend to be a hash";
    uint public myUint;
    function setIPFSHash(string memory _IPFSHash) public  {
    IPFSHash = _IPFSHash;
    }
    function setMyUint(uint _myUint) public  {
        myUint = _myUint;
    }   
}

the python code for the transaction is this:

contract_abi = "contract abi goes here"
contract_address = "0xb80206789De27D14b3f458Bed9072d4a0129b41e"

myContract = web3.eth.contract(address=contract_address, abi=contract_abi)

nonce = web3.eth.getTransactionCount(account)

tx = myContract.functions.setMyUint(8888).buildTransaction({
    'chainId' : 3,
    'gas' : 210000,
    'gasPrice' : web3.toWei('50', 'gwei'),
    'nonce' : nonce,
})

signed_tx = web3.eth.account.sign_transaction(tx, key)
result = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
tx_receipt = web3.eth.getTransactionReceipt(result)

and web3.py reports back this error:

Warning: TransactionNotFound : Transaction with hash: 0xc5106c1438ad654c990cc749d5ce7b028413f3f97841ecbdcdcb2a98f4cb94b1 not found. ['  File "<string>", line 28, in <module>\n', '  File "C:\\Users\\arlav\\anaconda3\\envs\\dynamo383\\lib\\site-packages\\web3\\module.py", line 58, in caller\n    result = w3.manager.request_blocking(method_str, params, error_formatters)\n', '  File "C:\\Users\\arlav\\anaconda3\\envs\\dynamo383\\lib\\site-packages\\web3\\manager.py", line 160, in request_blocking\n    apply_error_formatters(error_formatters, response, params)\n', '  File "C:\\Users\\arlav\\anaconda3\\envs\\dynamo383\\lib\\site-packages\\web3\\manager.py", line 65, in apply_error_formatters\n    formatted_response = pipe(params, error_formatters)\n', '  File "cytoolz/functoolz.pyx", line 667, in cytoolz.f

It appears that web3.py formulates the whole transaction but is not able to send it because it does not find it on the account? Any advice on how to formulate transactions so that they are able to use a writing function on the smart contract code?

2
  • 1
    well the reason i can think of is because when u send the transaction it take times for the transaction to be verify and confirm but the code run all the way to the end without waiting for the transaction to verify – haxerl Dec 12 '20 at 11:52
  • Use async await? – Dendi Suhubdy Jan 2 at 12:57
0

We managed to solve the issue:

contract_abi = "contract abi goes here"
contract_address = "0xb80206789De27D14b3f458Bed9072d4a0129b41e"

myContract = web3.eth.contract(address=contract_address, abi=contract_abi)

receipts = []
nonce = w3.eth.getTransactionCount(wallet_address)
tx_dict = smartContract.functions.setIPFSHash(message).buildTransaction({
    'chainId' : 3,
    'gas' : 2100000,
    'gasPrice' : w3.toWei('50', 'gwei'),
    'nonce' : nonce,
})

signed_tx = w3.eth.account.sign_transaction(tx_dict, wallet_private_key)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=120, poll_latency=0.1)
receipts.append(tx_receipt)

nonce = w3.eth.getTransactionCount(wallet_address)
tx_dict = smartContract.functions.setMyUint(number).buildTransaction({
    'chainId' : 3,
    'gas' : 2100000,
    'gasPrice' : w3.toWei('50', 'gwei'),
    'nonce' : nonce,
})
signed_tx = w3.eth.account.sign_transaction(tx_dict, wallet_private_key)
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=120, poll_latency=0.1)

receipts.append(tx_receipt)

for each of the functions called, one needs to build a specific transaction formatted as above, then signed, then send to the network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.