2

So Im pretty new to Solidity and I was looking into the receive and fallback functions and came across this very helpful post:

What is the receive keyword in solidity?

Then I read this:

The receive method is used as a fallback function in a contract and is called when ether is sent to a contract with no calldata.

So how does one call a function of a contract with no calldata (but with some value) such that the receive function is triggered in web3 and geth?

I know how to call functions like so:

await contract.methods.methodAB().call();
await contract.methods.methodXY(ARGUMENTS).send({from: MY_ADDRESS, value: 'SOME_VALUE_IN_GWEI'});

But how do I call a contract without calldata, or is it just as simple as doing

eth.sendTransaction({
    from: MY_ADDRESS, 
    to: TARGET_ADDRESS, 
    value: 'SOME_VALUE_IN_GWEI'
    }, function(err, value){
        console.log(err ? err : 'Transaction was sent! txhash: '+ value);
    }
);

Can anybody specify what is meant with calldata? Thanks in advance

DigitalJedi
  • 150
  • 4

1 Answers1

1

There are two ways of interacting with a contract. A call (read-only, doesn't change the state, and therefore doesn't require a transaction, and is free), and a transaction which does change the state.

When you 'transact' a function on a contract, you're really just sending an ordinary transaction to that contract, with some data. You could do this "by hand" with sendTransaction: just add a data field. That data tells the contract which of its functions you want to call (via the signature, which serves as a function selector), and which arguments to pass it.

A receive or fallback function is there to deal with cases where a transaction is sent to the contract but the function selector doesn't correspond to the signature of any function on the contract, including the case when no data is provided. A good example is the fallback function of the wrapped ether (WETH) contract, whereby if the contract receives a transaction with no data, the fallback function automatically calls the 'deposit' function.

So, if you want to trigger a fallback or receive function, just send some ether (or a 0 ether transaction) to that contract.

SimonR
  • 326
  • 1
  • 4