You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Description
This PR closes#5566.
Contracts now can have a special function decorated with `#[fallback]`
which is called when the contract method selection fails.
This function for all intents and purposes works as a standard contract
method, so:
- it cannot call others contracts methods;
- it has the same limitations of inputs and outputs.
This function can return a value like a normal contract would, or it can
use the `__contract_ret` intrinsics to return any value.
## Checklist
- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Copy file name to clipboardexpand all lines: docs/book/src/blockchain-development/calling_contracts.md
+28
Original file line number
Diff line number
Diff line change
@@ -163,3 +163,31 @@ While the Fuel contract calling paradigm is similar to the EVM's (using an ABI,
163
163
1. [**Native assets**](./native_assets.md): FuelVM calls can forward any native asset not just base asset.
164
164
165
165
2. **No data serialization**: Contract calls in the FuelVM do not need to serialize data to pass it between contracts; instead they simply pass a pointer to the data. This is because the FuelVM has a shared global memory which all call frames can read from.
166
+
167
+
## Fallback
168
+
169
+
When a contract is compiled, a special section called "contract selection" is also generated. This section checks if the contract call method matches any of the available ABI methods. If this fails, one of two possible actions will happen:
170
+
171
+
1 - if no fallback function was specified, the contract will revert;
172
+
2 - otherwise, the fallback function will be called.
173
+
174
+
For all intents and purposes the fallback function is considered a contract method, which means that it has all the limitations that other contract methods have. As the fallback function signature, the function cannot have arguments, but they can return anything.
175
+
176
+
If for some reason the fallback function needs to returns different types, the intrinsic `__contract_ret` can be used.
Copy file name to clipboardexpand all lines: docs/book/src/reference/attributes.md
+4
Original file line number
Diff line number
Diff line change
@@ -59,3 +59,7 @@ More details in [Unit Testing](../testing/unit-testing.md).
59
59
The `#[deprecated]` attribute marks an item as deprecated and makes the compiler emit a warning for every usage of the deprecated item. This warning can be disabled using `#[allow(deprecated)]`.
60
60
61
61
It is possible to improve the warning message with `#[deprecated(note = "your message")]`
62
+
63
+
## Fallback
64
+
65
+
The `#[fallback]` attribute makes the compiler use the marked function as the contract call fallback function, which means that, when a contract is called, and the contract selection fails, the fallback function will be called instead.
0 commit comments