-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
2 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
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,66 @@ | ||
// SPDX-License-Identifier: MIT | ||
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol) | ||
|
||
// Vendored from OpenZeppelin Contracts v4.7.3, see: | ||
// <https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/v4.7.3/contracts/security/ReentrancyGuard.sol> | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Contract module that helps prevent reentrant calls to a function. | ||
* | ||
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier | ||
* available, which can be applied to functions to make sure there are no nested | ||
* (reentrant) calls to them. | ||
* | ||
* Note that because there is a single `nonReentrant` guard, functions marked as | ||
* `nonReentrant` may not call one another. This can be worked around by making | ||
* those functions `private`, and then adding `external` `nonReentrant` entry | ||
* points to them. | ||
* | ||
* TIP: If you would like to learn more about reentrancy and alternative ways | ||
* to protect against it, check out our blog post | ||
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. | ||
*/ | ||
abstract contract ReentrancyGuard { | ||
// Booleans are more expensive than uint256 or any type that takes up a full | ||
// word because each write operation emits an extra SLOAD to first read the | ||
// slot's contents, replace the bits taken up by the boolean, and then write | ||
// back. This is the compiler's defense against contract upgrades and | ||
// pointer aliasing, and it cannot be disabled. | ||
|
||
// The values being non-zero value makes deployment a bit more expensive, | ||
// but in exchange the refund on every call to nonReentrant will be lower in | ||
// amount. Since refunds are capped to a percentage of the total | ||
// transaction's gas, it is best to keep them low in cases like this one, to | ||
// increase the likelihood of the full refund coming into effect. | ||
uint256 private constant _NOT_ENTERED = 1; | ||
uint256 private constant _ENTERED = 2; | ||
|
||
uint256 private _status; | ||
|
||
constructor() { | ||
_status = _NOT_ENTERED; | ||
} | ||
|
||
/** | ||
* @dev Prevents a contract from calling itself, directly or indirectly. | ||
* Calling a `nonReentrant` function from another `nonReentrant` | ||
* function is not supported. It is possible to prevent this from happening | ||
* by making the `nonReentrant` function external, and making it call a | ||
* `private` function that does the actual work. | ||
*/ | ||
modifier nonReentrant() { | ||
// On the first call to nonReentrant, _notEntered will be true | ||
require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); | ||
|
||
// Any calls to nonReentrant after this point will fail | ||
_status = _ENTERED; | ||
|
||
_; | ||
|
||
// By storing the original value once again, a refund is triggered (see | ||
// https://eips.ethereum.org/EIPS/eip-2200) | ||
_status = _NOT_ENTERED; | ||
} | ||
} |
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,50 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
pragma solidity ^0.8; | ||
|
||
// Used to test reentrancy when receiving ETH | ||
contract CallOnReceive { | ||
address payable public to; | ||
uint256 public value; | ||
bytes public data; | ||
|
||
// Store call result for later retrieval | ||
bool public lastFallbackCallSuccess; | ||
bytes public lastFallbackCallReturnData; | ||
|
||
receive() external payable { | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(lastFallbackCallSuccess, lastFallbackCallReturnData) = to.call{ | ||
value: value | ||
}(data); | ||
} | ||
|
||
function execCall( | ||
address payable _to, | ||
uint256 _value, | ||
bytes memory _data | ||
) public returns (bytes memory) { | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, bytes memory result) = _to.call{value: _value}(_data); | ||
if (success == false) { | ||
// Forward revert error | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
let ptr := mload(0x40) | ||
let size := returndatasize() | ||
returndatacopy(ptr, 0, size) | ||
revert(ptr, size) | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function setCallOnReceive( | ||
address payable _to, | ||
uint256 _value, | ||
bytes calldata _data | ||
) external { | ||
to = _to; | ||
value = _value; | ||
data = _data; | ||
} | ||
} |
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