Infura – Connecting to Rinkeby and Calling State-Changing Function

infuraproof-of-authorityrinkebysolidityweb3.py

So i have trouble using a smart contract on Rinkeby through infura as it gives an "extra-data" error, which as far as i have read, is due to Rinkeby being a POA testnet. There are suggestions of using a middle-ware for this, but there's no good documentation to describe how it can be done …

Also, the extra-data error occurs when calling a state-changing function ( like assigning a new value to a storage variable ), not when calling a read only function ( like a "getter" method )

anyone has any idea how to fix this ?

Best Answer

easy solution RIGHT HERE;

Import your required libraries

from web3 import Web3, HTTPProvider

Intialize your infura node in a variable

w3 = Web3(Web3.HTTPProvider("https://rinkeby.infura.io/v3/YOUR_INFURA_KEY"))

aand.. the most SIMPLE part, which TOOK OVER THREE WEEKS to figure out because the documentation is absolutely horrenous ;

inject the damn middle-ware in the variable holding your infura node;

w3.middleware_stack.inject(geth_poa_middleware, layer=0)

just remember that you have to sign all transactions LOCALLY, as infura does not handle any keys from your wallet

transaction = contract.functions.function_Name(params)
signed_tx = w3.eth.account.signTransaction(transaction, private_key)

and then send;

txn_hash = w3.eth.sendRawTransaction(signed.rawTransaction) 
txn_receipt = w3.eth.waitForTransactionReceipt(txn_hash)

there you go, an all in one solution !

P.S : use print(txn_receipt) to get transaction id to view in etherscan

Related Topic