vdAMM - Dependencies
ReentrancyGuard.sol
This file contains the ReentrancyGuard contract which helps prevent reentrant calls to a function. Reentrant calls can cause issues in the logic of a contract, and this contract provides a modifier, nonReentrant, that can be used to prevent such calls. The contract also provides an explanation of why booleans are more expensive in Solidity and how to work around this issue.
ReentrancyGuard
The ReentrancyGuard contract helps to prevent reentrant calls to a function. It provides a nonReentrant modifier that can be applied to functions to ensure there are no nested (reentrant) calls to them.
_NOT_ENTERED
A private constant variable used to indicate that a function is not being entered.
_ENTERED
A private constant variable used to indicate that a function is being entered.
_status
A private variable used to keep track of the current function call status.
ReentrancyGuard
constructor()
The constructor for the ReentrancyGuard contract. It sets the initial _status to _NOT_ENTERED.
nonReentrant
modifier nonReentrant()
A modifier to prevent a contract from calling itself, directly or indirectly. It prevents nested (reentrant) calls to functions.
Errors
- ReentrancyGuard: reentrant call: Error thrown if a reentrant call is attempted.
Examples
Using the nonReentrant modifier
This example demonstrates how to use the nonReentrant modifier in a function of a contract that inherits from ReentrancyGuard.
// Assume we have a contract that inherits from ReentrancyGuard
contract MyContract is ReentrancyGuard {
// Define a state variable
uint256 private _counter;
// Define a function that uses the nonReentrant modifier
function increment() public nonReentrant {
_counter++;
}
}