Open lrettig opened 3 weeks ago
What's the motivation to have a fallback()
method? I mean, why not just fail a transaction if the template doesn't have a receive()
?
Same reason you can set up a fallback email address for a domain. It's just useful to have a "catch all" in case you receive something inbound you weren't expecting: a call to a nonexistent method/if there's no matching method signature (including, potentially, the wrong args), or if no data is supplied.
To be clear I consider this nice to have but quite low priority. We can wait and see if developers ask for it.
It's just useful to have a "catch all" in case you receive something inbound you weren't expecting: a call to a nonexistent method/if there's no matching method signature (including, potentially, the wrong args), or if no data is supplied.
I understand when it would be called. I wonder about the benefit of calling the fallback instead of immediately failing the TX.
We should do some homework to better understand how and why this is used in solidity and EVM, and whether other execution environments have something similar.
As in EVM, we want to allow a template to optionally implement a
fallback()
method. If a template without a fallback method receives an inbound tx or a CALL to nonexistent method, it should immediately fail. If it does have a fallback method, that method should receive the tx or CALL.IIRC, right now, we route a tx or a CALL without a method selector to the default ELF entrypoint -- which in the case of a template compiled with the
noentrypoint
feature and#![no_main]
would result in a panic. We should change this behavior.It should be able to receive encoded args as a single input bytearray. It would need to manually decode those args if it chooses to do so.
The
fallback
method, if declared payable, should also be called when coins are received and a payablereceive()
method doesn't exist, or if coins are received and there is calldata (sincereceive
cannot take calldata).We may need a new macro for this, or else we may be able to update the existing
#[template]
and#[callable]
proc macros to do the additional work here (if they notice that the method name isfallback
).See also: https://docs.soliditylang.org/en/latest/contracts.html#fallback-function