Skip to content

Commit

Permalink
Added a contract that can prevent re-entrancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Sep 4, 2023
1 parent 9d3afe9 commit cd1da16
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions contracts/interfaces/INoReEntrancy.sol
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();
}
46 changes: 46 additions & 0 deletions contracts/utils/NoReEntrancy.sol
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)
}
}
}

0 comments on commit cd1da16

Please sign in to comment.