1

I have deployed a smart contract dthat contains a payable function fundit(address _to). Now I am trying to use a Contract object to send ether to the function:

tx_hash = instance.fundit.sendTransaction(instance2.address, {"from": w3.eth.accounts[0], "value": 1})

However, I am receiving the following error:

'Contract' object has no attribute 'fundit'

How can I solve this?

  • Welcome to Stack Overflow! What are instance and instance2 in your example code? Which version of web3py are you using? – iamdefinitelyahuman Jan 29 at 12:24
  • @goodvibration web3py version 5.4.0. – DebaSen Jan 29 at 12:30
  • @iamdefinitelyahuman Thank you. Instance is the Smart Contract deployed having the function fundit(address payable to) public payable {}. Instance2 is another deployed contract where I want the donation to be sent. – DebaSen Jan 29 at 12:33
0

Assuming instance is a Contract object, classes for calling contract functions are found inside the Contract.function member. The correct syntax for what you wish to do is:

contract_function = instance.functions.fundit(instance2.address)
tx_hash = contract_function.transact({"from": w3.eth.accounts[0], "value": 1})
  1. In the first line, you invoke the ContractFunction with the inputs you wish to use in the transaction.
  2. In the second line, you call the ContractFunction.transact method to perform the transaction. The only argument should be a transaction dictionary.

You can of course merge these two lines into one, I only separated them to make the example easier to read.

| improve this answer | |
  • Thank you so much, this worked perfectly!! I have another question, how can you do this for ConciseContract? – DebaSen Jan 29 at 13:05
  • concise_instance.fundit(instance2.address, transact={"from": w3.eth.accounts[0], "value": 1}) should do it. – iamdefinitelyahuman Jan 29 at 13:09
  • Shameless self-promotion: you might also consider Brownie, which has a significantly less verbose API for contract interaction - eth-brownie.readthedocs.io/en/stable/… – iamdefinitelyahuman Jan 29 at 13:10
  • Thank you so much! I am quite new to this but I will surely take a look. :) – DebaSen Jan 29 at 13:39

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.