Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call fallback function of a contract? #182

Closed
Arul- opened this issue May 20, 2018 · 8 comments
Closed

How to call fallback function of a contract? #182

Arul- opened this issue May 20, 2018 · 8 comments
Assignees
Labels
discussion Questions, feedback and general information.

Comments

@Arul-
Copy link

Arul- commented May 20, 2018

Does Contract API support calling a fallback function? (both payable and non-payable)

I don't find any documentation on this! I tried the following which fails

contractInstance[''].apply(contractInstance); //giving it a blank string as the function name
@ricmoo ricmoo self-assigned this May 20, 2018
@ricmoo ricmoo added enhancement New feature or improvement. discussion Questions, feedback and general information. labels May 20, 2018
@ricmoo
Copy link
Member

ricmoo commented May 20, 2018

This is a good idea to add to the Interface object, for completion.

However, it is not strictly needed. Calling the default function can be achieved simply by sending a transaction to the contraxct:

var tx = {
    to: contractAddress,
    value: ethers.utils.parseEther("1.0")
};

wallet.sendTransaction(tx).then(function(tx) {
    console.log(tx);
});

You bring up an interesting thing I had not considered though... Can the default function returns? I will experiment with this and make sure. It should (according to EVM) not be anything special, so it should be supported, but I'm not sure if Solidity supports it. Regardless, I can make some samples assembly examples to test it.

@ricmoo ricmoo added v4.0 and removed enhancement New feature or improvement. labels Jun 29, 2018
@ricmoo ricmoo removed the v4.0 label Sep 30, 2018
@GregTheGreek
Copy link

Whats the status on this?

@ricmoo
Copy link
Member

ricmoo commented Feb 5, 2020

@GregTheGreek Still has not been added. I'm still mulling it over, especially insight of the new Solidity changes. There almost either needs to be zero functions to do this, or two separate options (of which, for any given situation, only one would be valid, I think?)...

Both seem correct, but zero seems cleaner. :p

I still need to catch up on the "received" Solidity feature.

@ricmoo
Copy link
Member

ricmoo commented Jun 3, 2020

For now, I think the best way to perform these operations is to rely on manually sending to the contract address. I may consider adding a function for receive() and () (for fallback), but for now I'm going to close this, since it is easy to manually do and once we move to a Proxy-based contract these become easier and safer.

But please re-open if you feel this is something that is important to be part of a Contract object.

Thanks! :)

@ricmoo ricmoo closed this as completed Jun 3, 2020
@psytron
Copy link

psytron commented Apr 25, 2021

So how can we get data returned from fallback() function of proxy contract?
sendTransaction(tx) doesn't seem to return the data?

@zemse
Copy link
Collaborator

zemse commented Apr 25, 2021

Hey @psytron, if your fallback function returns a value and you directly calling the contract using sendTransaction(tx), then you can't get the return value trivial since it's a transaction (though it could be extracted from an execution trace somehow). If you instead wanted to do a simple read call (to a maybe view or pure function), you can do following:

const result = await provider.call({
    to: contractAddress,
    data: '0x1234..' // encoded data
})

If your proxy contract is a minimal proxy contract like EIP-1167, then you can simply pass the proxy address to the Contract.

const myContract = new ethers.Contract(proxyAddr, implementationAbi, provider);

@psytron
Copy link

psytron commented Apr 26, 2021

Marvelous. Thank You @zemse

@zemse
Copy link
Collaborator

zemse commented Apr 26, 2021

Welcome :)

@ethers-io ethers-io locked and limited conversation to collaborators Apr 28, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

5 participants