-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a contract that can prevent re-entrancy
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title Pausable | ||
* @notice This contract provides a mechanism to halt the execution of specific functions | ||
* if a pause condition is activated. | ||
*/ | ||
interface INoReEntrancy { | ||
error ReEntrancy(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import { INoReEntrancy } from '../interfaces/INoReEntrancy.sol'; | ||
|
||
/** | ||
* @title Pausable | ||
* @notice This contract provides a mechanism to halt the execution of specific functions | ||
* if a pause condition is activated. | ||
*/ | ||
contract NoReEntrancy is INoReEntrancy { | ||
// uint256(keccak256('entered')) - 1 | ||
uint256 internal constant ENTERED_SLOT = 0x01f33dd720a8dea3c4220dc5074a2239fb442c4c775306a696f97a7c54f785fc; | ||
|
||
/** | ||
* @notice A modifier that throws a Paused custom error if the contract is paused | ||
* @dev This modifier should be used with functions that can be paused | ||
*/ | ||
modifier noReEntrancy() { | ||
if (_hasEntered()) revert ReEntrancy(); | ||
_setEntered(true); | ||
_; | ||
_setEntered(false); | ||
} | ||
|
||
/** | ||
* @notice Check if the contract is already executing. | ||
* @return entered A boolean representing the entered status. True if already executing, false otherwise. | ||
*/ | ||
function _hasEntered() internal view returns (bool entered) { | ||
assembly { | ||
entered := sload(ENTERED_SLOT) | ||
} | ||
} | ||
|
||
/** | ||
* @notice Sets the entered status of the contract | ||
* @param entered A boolean representing the entered status. True if already executing, false otherwise. | ||
*/ | ||
function _setEntered(bool entered) internal { | ||
assembly { | ||
sstore(ENTERED_SLOT, entered) | ||
} | ||
} | ||
} |