0

I am trying to call a function from an already-deployed contract on the Ropsten network:

from web3 import Web3


infura_url = "https://ropsten.infura.io/v3/XXXXXXXXXXXXXX"
w3 = Web3(Web3.HTTPProvider(infura_url))

ABI = '[{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]'

contract_address = '0xF12f96cbc638E01546d9Ba3a1847BE762ea6dd4b'
contract = w3.eth.contract(contract_address, abi=ABI)

print('Return message from function: {}'.format(contract.say()))

My Infura key works, just tried to get the last block and balance of some random account. But I cannot interact with the contract despite giving the correct ABI and contract address? I also tried with:

  • contract.functions().say()
  • contract.functions.say()

But I am always getting an AttributeError?

2
  • hmm I may also need the bytecode or compile the .sol before locally with truffle? According to this long tutorial: levelup.gitconnected.com/…
    – Robert Ggg
    Nov 2, 2019 at 16:47
  • In web3.py v4, try contract.transact({'from': ..., 'gas': ..., 'gasPrice': ...}).say(). Nov 2, 2019 at 16:51

1 Answer 1

1

You were close with your final attempt, you just need to call the call() method as well. The proper syntax is:

contract.functions.say().call()

See the related web3.py documentation for more information.

2
  • You may also need to specify the caller depending on the function. contract.functions.say().call({'from': ''0x......}). `
    – Molina
    Nov 2, 2019 at 17:08
  • ok thanks guys will try now but this may be a bigger issue - I have old web3 version (3.6.15) as well as "unholly" way to compile Python modules as I manually installed VC++ build tools...hope none of this messes with the processes. If it's just coding issue I'll try as Molina suggests and specify account source.
    – Robert Ggg
    Nov 2, 2019 at 18:24

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.