Back to Sway

Re-entrancy

docs/reference/src/documentation/operations/reentrancy.md

0.71.01.1 KB
Original Source

Re-entrancy

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:

Re-entrancy Guard

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.

sway
{{#include ../../code/operations/re_entrency/src/main.sw:import}}

Then call it in a contract function.

sway
{{#include ../../code/operations/re_entrency/src/main.sw:guard}}

Checks-Effects-Interactions Pattern

The pattern states that all state (storage) changes should be made before a call is made.

sway
{{#include ../../code/operations/re_entrency/src/main.sw:check}}