I have deployed a contract on Ropsten, this contract has a function name testFunc() and I want to execute testFunc() from a python script.
This is how I'm connecting to the contract from python:
from web3 import Web3
def connect_to_contract(self):
try:
self.web3 = Web3(
Web3.HTTPProvider(
"https://" +
self.network +
".infura.io/v3/" +
os.getenv("WEB3_INFURA_PROJECT_ID")))
self.contract = self.web3.eth.contract(
address=self.contract_address, abi=self.contract_abi)
except Exception as e:
raise e
Then, this is how I first tried to execute the contract function but nothing happend:
self.contract.functions.testFunc().call()
I've read on different topics that I've to sign the transaction in order to make it work. So that's how I'm trying to do know:
txn = self.contract.functions.testFunc().buildTransaction({
'gas': 70000,
'gasPrice': self.web3.toWei('1', 'gwei'),
'from': self.contract_address,
'nonce': nonce
})
private_key = "xxxxxxxxxx"
signed_txn = self.web3.eth.account.signTransaction(txn, private_key=private_key)
self.web3.eth.sendRawTransaction(signed_txn.rawTransaction)
But that's don't work neither and I have the error message:
TypeError: from field must match key's 0x25c77Axxxxxxxxxxxxxxxxxxxxxxxx, but it was 0x7d8e0ea7axxxxxxxxxxxxxxxxxxxx
The 0x25c77Axxxxxxxxxxxxxxxxxxxxxxxx address mentionned here is the public key I used to deploy the contract, the second one 0x7d8e0ea7axxxxxxxxxxxxxxxxxxxx is the contract which have the testFunc() function.
So my question is, how to delegate the call from the python script or how can I get the private key for the deployed contract ? How should I do ?