Verified contractExactGPL-3.0
BatchDepositorV6
Address0x802b822b20b9071ddc2fee4ae62d7a1410ab2ce3
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.25
- Same code
Only this contract
Details
- Compiler
- solc 0.8.25+commit.b61c2a91
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- GPL-3.0
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,154,249
- Creation transaction
- 0x625ab79ede…afdbc279fe
10 files
- contracts/contracts-BatchDepositV6/BatchDepositorV6.solmain8.96 KB
- contracts/contracts-BatchDepositV6/BatchDepositorV6Storage.sol851 B
- contracts/contracts-BatchDepositV6/interfaces/IBatchDepositorV6.sol1.25 KB
- contracts/contracts-BatchDepositV6/interfaces/IDepositContract.sol280 B
- contracts/contracts-BatchDepositV6/lib/DepositDataLib.sol2.86 KB
- contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/access/Ownable.sol3.1 KB
- contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/access/Ownable2Step.sol2.48 KB
- contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/Context.sol963 B
- contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/Pausable.sol2.7 KB
- contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/ReentrancyGuard.sol3.38 KB
contracts/contracts-BatchDepositV6/BatchDepositorV6.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
import "./lib/openzeppelin-contracts-5.0/access/Ownable2Step.sol";
import "./lib/openzeppelin-contracts-5.0/utils/ReentrancyGuard.sol";
import "./lib/openzeppelin-contracts-5.0/utils/Pausable.sol";
import "./BatchDepositorV6Storage.sol";
import "./lib/DepositDataLib.sol";
contract BatchDepositorV6 is BatchDepositorV6Storage, Ownable2Step, ReentrancyGuard, Pausable {
constructor(
address _depositContract,
address _defaultOwner,
uint256 _maxDepositsPerCall,
uint256 _maxPerWhitelistCount
) Ownable(_defaultOwner) ReentrancyGuard() payable {
require(_depositContract != address(0), "Invalid deposit contract");
require(_defaultOwner != address(0), "Invalid default owner");
require(_maxDepositsPerCall > 0, "Invalid max deposits per call");
require(_maxPerWhitelistCount > 0, "Invalid max per whitelist count");
depositContract = IDepositContract(_depositContract);
maxDepositsPerCall = _maxDepositsPerCall;
maxPerWhitelistCount = _maxPerWhitelistCount;
}
function updateMaxDepositsPerCall(uint256 _maxDepositsPerCall) external whenNotPaused onlyOwner {
require(_maxDepositsPerCall > 0, "Invalid max deposits per call");
require(_maxDepositsPerCall != maxDepositsPerCall, "Same max deposits per call");
maxDepositsPerCall = _maxDepositsPerCall;
emit MaxDepositsPerCallUpdated(_maxDepositsPerCall);
}
function setPause(bool _status) external onlyOwner {
if (_status) {
_pause();
} else {
_unpause();
}
}
function updateMaxPerWhitelistCount(uint256 _maxPerWhitelistCount) external whenNotPaused onlyOwner {
require(_maxPerWhitelistCount > 0, "Invalid max whitelist count");
require(_maxPerWhitelistCount != maxPerWhitelistCount, "Same max per whitelist count");
maxPerWhitelistCount = _maxPerWhitelistCount;
emit MaxPerWhitelistCountUpdated(_maxPerWhitelistCount);
}
function addWhitelist(address[] calldata _addresses) external whenNotPaused onlyOwner {
require(_addresses.length > 0, "Empty added addresses array");
require(_addresses.length <= maxPerWhitelistCount, "Over max whitelist count");
for (uint256 i = 0; i < _addresses.length; ) {
require(_addresses[i] != address(0), "Add zero address");
require(!whitelist[_addresses[i]], "Already in whitelist");
whitelist[_addresses[i]] = true;
unchecked {++i;}
}
emit WhitelistAdded(_addresses);
}
function removeWhitelist(address[] calldata _addresses) external onlyOwner {
require(_addresses.length > 0, "Empty removed addresses array");
require(_addresses.length <= maxPerWhitelistCount, "Over max whitelist count");
for (uint256 i = 0; i < _addresses.length; ) {
require(_addresses[i] != address(0), "Remove zero address");
require(whitelist[_addresses[i]], "Not in whitelist");
delete whitelist[_addresses[i]];
unchecked {++i;}
}
emit WhitelistRemoved(_addresses);
}
function addDepositData(DepositDataParams calldata _params) external whenNotPaused onlyWhitelisted(_params.addr) onlyOwner {
require(_params.addr != address(0), "Invalid address");
require(_params.quantity != 0, "Zero quantity");
require(_params.quantity <= MAX_PEER_ADD_KEYS, "Over max key amounts");
require(_params.pubKeys.length == _params.quantity, "Invalid pubKeys length");
require(_params.amounts.length == _params.quantity, "Invalid amounts length");
require(_params.withdrawalCredentials.length == _params.quantity, "Invalid withdrawal credentials length");
require(_params.signatures.length == _params.quantity, "Invalid signatures length");
require(_params.depositDataRoots.length == _params.quantity, "Invalid deposit data roots length");
for (uint256 i = 0; i < _params.quantity; ) {
require(_params.pubKeys[i].length == PUBKEY_LENGTH, "Invalid pubkey length");
require(_params.withdrawalCredentials[i].length == WITHDRAWAL_CREDENTIAL_LENGTH, "Invalid withdrawal_credential length");
require(_params.signatures[i].length == SIGNATURE_LENGTH, "Invalid signature length");
require(_params.amounts[i] >= 1 ether, "Amount below 1 ether");
require(_params.amounts[i] % 1 gwei == 0, "Amount not multiple of 1 gwei");
bytes32 expectedRoot =
DepositDataLib.computeActivationDepositDataRoot(_params.pubKeys[i], _params.amounts[i], _params.withdrawalCredentials[i], _params.signatures[i]);
require(expectedRoot == _params.depositDataRoots[i], "Invalid deposit data root");
require(!existingPubKeys[keccak256(_params.pubKeys[i])], "Pubkey exists");
existingPubKeys[keccak256(_params.pubKeys[i])] = true;
depositData[_params.addr].push(DepositDataInfo({
pubkey: _params.pubKeys[i],
amount: _params.amounts[i],
withdrawalCredential: _params.withdrawalCredentials[i],
signature: _params.signatures[i],
depositDataRoot: _params.depositDataRoots[i],
status: DepositDataStatus.Available
}));
unchecked {++i;}
}
emit DepositDataAdded(_params.addr, _params.quantity, _params.pubKeys);
}
function deprecateDepositData(address _addr, uint256[] calldata _indices) external onlyOwner {
require(_indices.length > 0, "Empty indices array");
for (uint256 i = 0; i < _indices.length; ) {
require(_indices[i] < getDepositDataLength(_addr), "Invalid index");
DepositDataInfo storage currentDepositData = depositData[_addr][_indices[i]];
require(currentDepositData.status == DepositDataStatus.Available, "depositData is not Available");
currentDepositData.status = DepositDataStatus.Deprecated;
unchecked {++i;}
}
emit DepositDataDeprecated(_addr, _indices);
}
function depositETH(uint256[] calldata _indices) external payable whenNotPaused onlyWhitelisted(msg.sender) nonReentrant() {
require(_indices.length > 0, "Empty indices array");
require(_indices.length <= maxDepositsPerCall, "Over max deposits per call");
uint256 totalAmount = 0;
for (uint256 i = 0; i < _indices.length; ) {
require(_indices[i] < getDepositDataLength(msg.sender), "Invalid index");
DepositDataInfo storage currentDepositData = depositData[msg.sender][_indices[i]];
require(currentDepositData.status == DepositDataStatus.Available, "depositData is not Available");
currentDepositData.status = DepositDataStatus.Deposited;
totalAmount += currentDepositData.amount;
depositContract.deposit{value: currentDepositData.amount}(
currentDepositData.pubkey,
currentDepositData.withdrawalCredential,
currentDepositData.signature,
currentDepositData.depositDataRoot
);
unchecked {++i;}
}
require(msg.value == totalAmount, "Provided ETH is mismatch with required ETH");
emit ETHDeposited(msg.sender, msg.value, _indices);
}
function getDepositDataInfo(
address _addr,
uint256 _startIndex,
uint256 _endIndex,
DepositDataStatus _status
) external view returns (DepositDataSummary[] memory) {
require(_startIndex < _endIndex, "Invalid start index");
require(_endIndex <= getDepositDataLength(_addr), "Invalid end index");
uint256 matchCount = 0;
for(uint256 i = _startIndex; i < _endIndex;) {
if(depositData[_addr][i].status == _status) {
unchecked{matchCount++;}
}
unchecked {++i;}
}
if (matchCount == 0) return new DepositDataSummary[](0);
DepositDataSummary[] memory result = new DepositDataSummary[](matchCount);
uint256 index = 0;
for(uint256 i = _startIndex; i < _endIndex;) {
if(depositData[_addr][i].status == _status) {
result[index] = DepositDataSummary({
index: i,
amount: depositData[_addr][i].amount,
pubKey: depositData[_addr][i].pubkey,
withdrawalCredential: depositData[_addr][i].withdrawalCredential
});
unchecked{index++;}
}
unchecked {++i;}
}
return result;
}
function getDepositDataLength(address _addr) public view returns (uint256) {
return depositData[_addr].length;
}
}contracts/contracts-BatchDepositV6/BatchDepositorV6Storage.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
import "./interfaces/IDepositContract.sol";
import "./interfaces/IBatchDepositorV6.sol";
abstract contract BatchDepositorV6Storage is IBatchDepositorV6 {
uint256 public constant PUBKEY_LENGTH = 48;
uint256 public constant WITHDRAWAL_CREDENTIAL_LENGTH = 32;
uint256 public constant SIGNATURE_LENGTH = 96;
uint256 public constant MAX_PEER_ADD_KEYS = 50;
uint256 public maxDepositsPerCall;
uint256 public maxPerWhitelistCount;
IDepositContract public immutable depositContract;
mapping(address => DepositDataInfo[]) public depositData;
mapping(bytes32 => bool) public existingPubKeys;
mapping(address => bool) public whitelist;
modifier onlyWhitelisted(address _addr) {
require(whitelist[_addr], "Not in whitelist");
_;
}
}contracts/contracts-BatchDepositV6/interfaces/IBatchDepositorV6.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
interface IBatchDepositorV6 {
enum DepositDataStatus {
Available, // 0
Deposited, // 1
Deprecated // 2
}
struct DepositDataInfo {
bytes pubkey;
uint256 amount;
bytes withdrawalCredential;
bytes signature;
bytes32 depositDataRoot;
DepositDataStatus status;
}
struct DepositDataParams {
address addr;
uint256 quantity;
bytes[] pubKeys;
uint256[] amounts;
bytes[] withdrawalCredentials;
bytes[] signatures;
bytes32[] depositDataRoots;
}
struct DepositDataSummary {
uint256 index;
uint256 amount;
bytes pubKey;
bytes withdrawalCredential;
}
event MaxDepositsPerCallUpdated(uint256 newMaxValue);
event MaxPerWhitelistCountUpdated(uint256 newMaxValue);
event WhitelistAdded(address[] addresses);
event WhitelistRemoved(address[] addresses);
event DepositDataAdded(address indexed addr, uint256 quantity, bytes[] pubKeys);
event DepositDataDeprecated(address indexed addr, uint256[] indices);
event ETHDeposited(address indexed addr, uint256 amount, uint256[] indices);
}contracts/contracts-BatchDepositV6/interfaces/IDepositContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
interface IDepositContract {
function deposit(
bytes calldata pubkey,
bytes calldata withdrawal_credentials,
bytes calldata signature,
bytes32 deposit_data_root
) external payable;
}contracts/contracts-BatchDepositV6/lib/DepositDataLib.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.25;
library DepositDataLib {
bytes32 private constant UPPER_128_MASK =
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000;
function computeActivationDepositDataRoot(
bytes memory _pubkey,
uint256 _amount,
bytes memory _wc,
bytes memory _signature
) internal view returns (bytes32 result) {
bytes32 pubkeyRoot = computePubkeyRoot(_pubkey);
bytes32 signatureRoot = computeSignatureRoot(_signature);
bytes32 amountRoot = toLittleEndian64(uint64(_amount / 1 gwei));
assembly ("memory-safe") {
let buf := mload(0x40)
mstore(buf, pubkeyRoot)
mstore(add(buf, 0x20), mload(add(_wc, 0x20)))
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
let left := mload(buf)
mstore(buf, amountRoot)
mstore(add(buf, 0x20), signatureRoot)
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
let right := mload(buf)
mstore(buf, left)
mstore(add(buf, 0x20), right)
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
result := mload(buf)
}
}
function computePubkeyRoot(bytes memory _pubkey) internal view returns (bytes32 result) {
assembly ("memory-safe") {
let buf := mload(0x40)
mstore(buf, mload(add(_pubkey, 0x20)))
mstore(add(buf, 0x20), and(mload(add(_pubkey, 0x40)), UPPER_128_MASK))
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
result := mload(buf)
}
}
function computeSignatureRoot(bytes memory _signature) internal view returns (bytes32 result) {
assembly ("memory-safe") {
let buf := mload(0x40)
let sigData := add(_signature, 0x20)
if iszero(staticcall(gas(), 0x02, sigData, 64, buf, 32)) { revert(0, 0) }
let hash1 := mload(buf)
mstore(buf, mload(add(sigData, 64)))
mstore(add(buf, 0x20), 0)
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
let hash2 := mload(buf)
mstore(buf, hash1)
mstore(add(buf, 0x20), hash2)
if iszero(staticcall(gas(), 0x02, buf, 64, buf, 32)) { revert(0, 0) }
result := mload(buf)
}
}
function toLittleEndian64(uint64 _value) internal pure returns (bytes32) {
_value = ((_value & 0xFF00FF00FF00FF00) >> 8) | ((_value & 0x00FF00FF00FF00FF) << 8);
_value = ((_value & 0xFFFF0000FFFF0000) >> 16) | ((_value & 0x0000FFFF0000FFFF) << 16);
_value = (_value >> 32) | (_value << 32);
return bytes32(uint256(_value) << 192);
}
}
contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/access/Ownable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/access/Ownable2Step.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (access/Ownable2Step.sol)
pragma solidity ^0.8.20;
import {Ownable} from "./Ownable.sol";
/**
* @dev Contract module which provides access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* This extension of the {Ownable} contract includes a two-step mechanism to transfer
* ownership, where the new owner must call {acceptOwnership} in order to replace the
* old one. This can help prevent common mistakes, such as transfers of ownership to
* incorrect accounts, or to contracts that are unable to interact with the
* permission system.
*
* The initial owner is specified at deployment time in the constructor for `Ownable`. This
* can later be changed with {transferOwnership} and {acceptOwnership}.
*
* This module is used through inheritance. It will make available all functions
* from parent (Ownable).
*/
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
/**
* @dev Returns the address of the pending owner.
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*
* Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
/**
* @dev The new owner accepts the ownership transfer.
*/
function acceptOwnership() public virtual {
address sender = _msgSender();
if (pendingOwner() != sender) {
revert OwnableUnauthorizedAccount(sender);
}
_transferOwnership(sender);
}
}
contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/Pausable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/Pausable.sol)
pragma solidity ^0.8.20;
import {Context} from "./Context.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract Pausable is Context {
bool private _paused;
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
/**
* @dev The operation failed because the contract is paused.
*/
error EnforcedPause();
/**
* @dev The operation failed because the contract is not paused.
*/
error ExpectedPause();
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
if (paused()) {
revert EnforcedPause();
}
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
if (!paused()) {
revert ExpectedPause();
}
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
contracts/contracts-BatchDepositV6/lib/openzeppelin-contracts-5.0/utils/ReentrancyGuard.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @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 EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* 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;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
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() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.