Ethers.js: How to call fallback function of a contract?

Created on 20 May 2018  路  8Comments  路  Source: ethers-io/ethers.js

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
discussion

Most helpful comment

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);

All 8 comments

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.

Whats the status on this?

@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.

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! :)

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

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);

Marvelous. Thank You @zemse

Welcome :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thegostep picture thegostep  路  3Comments

ricmoo picture ricmoo  路  3Comments

jochenonline picture jochenonline  路  3Comments

pcowgill picture pcowgill  路  3Comments

crazyrabbitLTC picture crazyrabbitLTC  路  3Comments