Re-entrancy occurs when a contract makes a call back into the contract that called it, e.g. Contract A calls Contract B but then Contract B makes a call back into Contract A.
To mitigate security concerns there are two approaches that are commonly used:
- Implement a guard: detect when a re-entrancy occurs
- Defensive programming: perform calls after all state changes have been made
Sway provides a stateless re-entrancy guard, which reverts at run-time when re-entrancy is detected.
To use the guard we must import it.
{{#include ../../code/operations/re_entrency/src/main.sw:import}}Then call it in a contract function.
{{#include ../../code/operations/re_entrency/src/main.sw:guard}}The pattern states that all state (storage) changes should be made before a call is made.
{{#include ../../code/operations/re_entrency/src/main.sw:check}}