creation_sourcecode: {"Context.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)\r\n\r\npragma solidity ^0.8.20;\r\n\r\n/**\r\n * @dev Provides information about the current execution context, including the\r\n * sender of the transaction and its data. While these are generally available\r\n * via msg.sender and msg.data, they should not be accessed in such a direct\r\n * manner, since when dealing with meta-transactions the account sending and\r\n * paying for execution may not be the actual sender (as far as an application\r\n * is concerned).\r\n *\r\n * This contract is only required for intermediate, library-like contracts.\r\n */\r\nabstract contract Context {\r\n    function _msgSender() internal view virtual returns (address) {\r\n        return msg.sender;\r\n    }\r\n\r\n    function _msgData() internal view virtual returns (bytes calldata) {\r\n        return msg.data;\r\n    }\r\n\r\n    function _contextSuffixLength() internal view virtual returns (uint256) {\r\n        return 0;\r\n    }\r\n}\r\n"},"EnumerableSet.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/EnumerableSet.sol)\r\n// This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.\r\n\r\npragma solidity ^0.8.20;\r\n\r\n/**\r\n * @dev Library for managing\r\n * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive\r\n * types.\r\n *\r\n * Sets have the following properties:\r\n *\r\n * - Elements are added, removed, and checked for existence in constant time\r\n * (O(1)).\r\n * - Elements are enumerated in O(n). No guarantees are made on the ordering.\r\n *\r\n * ```solidity\r\n * contract Example {\r\n *     // Add the library methods\r\n *     using EnumerableSet for EnumerableSet.AddressSet;\r\n *\r\n *     // Declare a set state variable\r\n *     EnumerableSet.AddressSet private mySet;\r\n * }\r\n * ```\r\n *\r\n * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)\r\n * and `uint256` (`UintSet`) are supported.\r\n *\r\n * [WARNING]\r\n * ====\r\n * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure\r\n * unusable.\r\n * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.\r\n *\r\n * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an\r\n * array of EnumerableSet.\r\n * ====\r\n */\r\nlibrary EnumerableSet {\r\n    // To implement this library for multiple types with as little code\r\n    // repetition as possible, we write it in terms of a generic Set type with\r\n    // bytes32 values.\r\n    // The Set implementation uses private functions, and user-facing\r\n    // implementations (such as AddressSet) are just wrappers around the\r\n    // underlying Set.\r\n    // This means that we can only create new EnumerableSets for types that fit\r\n    // in bytes32.\r\n\r\n    struct Set {\r\n        // Storage of set values\r\n        bytes32[] _values;\r\n        // Position is the index of the value in the `values` array plus 1.\r\n        // Position 0 is used to mean a value is not in the set.\r\n        mapping(bytes32 value =\u003e uint256) _positions;\r\n    }\r\n\r\n    /**\r\n     * @dev Add a value to a set. O(1).\r\n     *\r\n     * Returns true if the value was added to the set, that is if it was not\r\n     * already present.\r\n     */\r\n    function _add(Set storage set, bytes32 value) private returns (bool) {\r\n        if (!_contains(set, value)) {\r\n            set._values.push(value);\r\n            // The value is stored at length-1, but we add 1 to all indexes\r\n            // and use 0 as a sentinel value\r\n            set._positions[value] = set._values.length;\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @dev Removes a value from a set. O(1).\r\n     *\r\n     * Returns true if the value was removed from the set, that is if it was\r\n     * present.\r\n     */\r\n    function _remove(Set storage set, bytes32 value) private returns (bool) {\r\n        // We cache the value\u0027s position to prevent multiple reads from the same storage slot\r\n        uint256 position = set._positions[value];\r\n\r\n        if (position != 0) {\r\n            // Equivalent to contains(set, value)\r\n            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in\r\n            // the array, and then remove the last element (sometimes called as \u0027swap and pop\u0027).\r\n            // This modifies the order of the array, as noted in {at}.\r\n\r\n            uint256 valueIndex = position - 1;\r\n            uint256 lastIndex = set._values.length - 1;\r\n\r\n            if (valueIndex != lastIndex) {\r\n                bytes32 lastValue = set._values[lastIndex];\r\n\r\n                // Move the lastValue to the index where the value to delete is\r\n                set._values[valueIndex] = lastValue;\r\n                // Update the tracked position of the lastValue (that was just moved)\r\n                set._positions[lastValue] = position;\r\n            }\r\n\r\n            // Delete the slot where the moved value was stored\r\n            set._values.pop();\r\n\r\n            // Delete the tracked position for the deleted slot\r\n            delete set._positions[value];\r\n\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the value is in the set. O(1).\r\n     */\r\n    function _contains(Set storage set, bytes32 value) private view returns (bool) {\r\n        return set._positions[value] != 0;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the number of values on the set. O(1).\r\n     */\r\n    function _length(Set storage set) private view returns (uint256) {\r\n        return set._values.length;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the value stored at position `index` in the set. O(1).\r\n     *\r\n     * Note that there are no guarantees on the ordering of values inside the\r\n     * array, and it may change when more values are added or removed.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `index` must be strictly less than {length}.\r\n     */\r\n    function _at(Set storage set, uint256 index) private view returns (bytes32) {\r\n        return set._values[index];\r\n    }\r\n\r\n    /**\r\n     * @dev Return the entire set in an array\r\n     *\r\n     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\r\n     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\r\n     * this function has an unbounded cost, and using it as part of a state-changing function may render the function\r\n     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\r\n     */\r\n    function _values(Set storage set) private view returns (bytes32[] memory) {\r\n        return set._values;\r\n    }\r\n\r\n    // Bytes32Set\r\n\r\n    struct Bytes32Set {\r\n        Set _inner;\r\n    }\r\n\r\n    /**\r\n     * @dev Add a value to a set. O(1).\r\n     *\r\n     * Returns true if the value was added to the set, that is if it was not\r\n     * already present.\r\n     */\r\n    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {\r\n        return _add(set._inner, value);\r\n    }\r\n\r\n    /**\r\n     * @dev Removes a value from a set. O(1).\r\n     *\r\n     * Returns true if the value was removed from the set, that is if it was\r\n     * present.\r\n     */\r\n    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {\r\n        return _remove(set._inner, value);\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the value is in the set. O(1).\r\n     */\r\n    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {\r\n        return _contains(set._inner, value);\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the number of values in the set. O(1).\r\n     */\r\n    function length(Bytes32Set storage set) internal view returns (uint256) {\r\n        return _length(set._inner);\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the value stored at position `index` in the set. O(1).\r\n     *\r\n     * Note that there are no guarantees on the ordering of values inside the\r\n     * array, and it may change when more values are added or removed.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `index` must be strictly less than {length}.\r\n     */\r\n    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {\r\n        return _at(set._inner, index);\r\n    }\r\n\r\n    /**\r\n     * @dev Return the entire set in an array\r\n     *\r\n     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\r\n     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\r\n     * this function has an unbounded cost, and using it as part of a state-changing function may render the function\r\n     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\r\n     */\r\n    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {\r\n        bytes32[] memory store = _values(set._inner);\r\n        bytes32[] memory result;\r\n\r\n        /// @solidity memory-safe-assembly\r\n        assembly {\r\n            result := store\r\n        }\r\n\r\n        return result;\r\n    }\r\n\r\n    // AddressSet\r\n\r\n    struct AddressSet {\r\n        Set _inner;\r\n    }\r\n\r\n    /**\r\n     * @dev Add a value to a set. O(1).\r\n     *\r\n     * Returns true if the value was added to the set, that is if it was not\r\n     * already present.\r\n     */\r\n    function add(AddressSet storage set, address value) internal returns (bool) {\r\n        return _add(set._inner, bytes32(uint256(uint160(value))));\r\n    }\r\n\r\n    /**\r\n     * @dev Removes a value from a set. O(1).\r\n     *\r\n     * Returns true if the value was removed from the set, that is if it was\r\n     * present.\r\n     */\r\n    function remove(AddressSet storage set, address value) internal returns (bool) {\r\n        return _remove(set._inner, bytes32(uint256(uint160(value))));\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the value is in the set. O(1).\r\n     */\r\n    function contains(AddressSet storage set, address value) internal view returns (bool) {\r\n        return _contains(set._inner, bytes32(uint256(uint160(value))));\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the number of values in the set. O(1).\r\n     */\r\n    function length(AddressSet storage set) internal view returns (uint256) {\r\n        return _length(set._inner);\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the value stored at position `index` in the set. O(1).\r\n     *\r\n     * Note that there are no guarantees on the ordering of values inside the\r\n     * array, and it may change when more values are added or removed.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `index` must be strictly less than {length}.\r\n     */\r\n    function at(AddressSet storage set, uint256 index) internal view returns (address) {\r\n        return address(uint160(uint256(_at(set._inner, index))));\r\n    }\r\n\r\n    /**\r\n     * @dev Return the entire set in an array\r\n     *\r\n     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\r\n     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\r\n     * this function has an unbounded cost, and using it as part of a state-changing function may render the function\r\n     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\r\n     */\r\n    function values(AddressSet storage set) internal view returns (address[] memory) {\r\n        bytes32[] memory store = _values(set._inner);\r\n        address[] memory result;\r\n\r\n        /// @solidity memory-safe-assembly\r\n        assembly {\r\n            result := store\r\n        }\r\n\r\n        return result;\r\n    }\r\n\r\n    // UintSet\r\n\r\n    struct UintSet {\r\n        Set _inner;\r\n    }\r\n\r\n    /**\r\n     * @dev Add a value to a set. O(1).\r\n     *\r\n     * Returns true if the value was added to the set, that is if it was not\r\n     * already present.\r\n     */\r\n    function add(UintSet storage set, uint256 value) internal returns (bool) {\r\n        return _add(set._inner, bytes32(value));\r\n    }\r\n\r\n    /**\r\n     * @dev Removes a value from a set. O(1).\r\n     *\r\n     * Returns true if the value was removed from the set, that is if it was\r\n     * present.\r\n     */\r\n    function remove(UintSet storage set, uint256 value) internal returns (bool) {\r\n        return _remove(set._inner, bytes32(value));\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the value is in the set. O(1).\r\n     */\r\n    function contains(UintSet storage set, uint256 value) internal view returns (bool) {\r\n        return _contains(set._inner, bytes32(value));\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the number of values in the set. O(1).\r\n     */\r\n    function length(UintSet storage set) internal view returns (uint256) {\r\n        return _length(set._inner);\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the value stored at position `index` in the set. O(1).\r\n     *\r\n     * Note that there are no guarantees on the ordering of values inside the\r\n     * array, and it may change when more values are added or removed.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `index` must be strictly less than {length}.\r\n     */\r\n    function at(UintSet storage set, uint256 index) internal view returns (uint256) {\r\n        return uint256(_at(set._inner, index));\r\n    }\r\n\r\n    /**\r\n     * @dev Return the entire set in an array\r\n     *\r\n     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\r\n     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\r\n     * this function has an unbounded cost, and using it as part of a state-changing function may render the function\r\n     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\r\n     */\r\n    function values(UintSet storage set) internal view returns (uint256[] memory) {\r\n        bytes32[] memory store = _values(set._inner);\r\n        uint256[] memory result;\r\n\r\n        /// @solidity memory-safe-assembly\r\n        assembly {\r\n            result := store\r\n        }\r\n\r\n        return result;\r\n    }\r\n}\r\n"},"IERC20.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)\r\n\r\npragma solidity ^0.8.20;\r\n\r\n/**\r\n * @dev Interface of the ERC20 standard as defined in the EIP.\r\n */\r\ninterface IERC20 {\r\n    /**\r\n     * @dev Emitted when `value` tokens are moved from one account (`from`) to\r\n     * another (`to`).\r\n     *\r\n     * Note that `value` may be zero.\r\n     */\r\n    event Transfer(address indexed from, address indexed to, uint256 value);\r\n\r\n    /**\r\n     * @dev Emitted when the allowance of a `spender` for an `owner` is set by\r\n     * a call to {approve}. `value` is the new allowance.\r\n     */\r\n    event Approval(address indexed owner, address indexed spender, uint256 value);\r\n\r\n    /**\r\n     * @dev Returns the value of tokens in existence.\r\n     */\r\n    function totalSupply() external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Returns the value of tokens owned by `account`.\r\n     */\r\n    function balanceOf(address account) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Moves a `value` amount of tokens from the caller\u0027s account to `to`.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * Emits a {Transfer} event.\r\n     */\r\n    function transfer(address to, uint256 value) external returns (bool);\r\n\r\n    /**\r\n     * @dev Returns the remaining number of tokens that `spender` will be\r\n     * allowed to spend on behalf of `owner` through {transferFrom}. This is\r\n     * zero by default.\r\n     *\r\n     * This value changes when {approve} or {transferFrom} are called.\r\n     */\r\n    function allowance(address owner, address spender) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the\r\n     * caller\u0027s tokens.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * IMPORTANT: Beware that changing an allowance with this method brings the risk\r\n     * that someone may use both the old and the new allowance by unfortunate\r\n     * transaction ordering. One possible solution to mitigate this race\r\n     * condition is to first reduce the spender\u0027s allowance to 0 and set the\r\n     * desired value afterwards:\r\n     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\r\n     *\r\n     * Emits an {Approval} event.\r\n     */\r\n    function approve(address spender, uint256 value) external returns (bool);\r\n\r\n    /**\r\n     * @dev Moves a `value` amount of tokens from `from` to `to` using the\r\n     * allowance mechanism. `value` is then deducted from the caller\u0027s\r\n     * allowance.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * Emits a {Transfer} event.\r\n     */\r\n    function transferFrom(address from, address to, uint256 value) external returns (bool);\r\n}\r\n"},"Ownable.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)\r\n\r\npragma solidity ^0.8.20;\r\n\r\nimport {Context} from \"./Context.sol\";\r\n\r\n/**\r\n * @dev Contract module which provides a basic access control mechanism, where\r\n * there is an account (an owner) that can be granted exclusive access to\r\n * specific functions.\r\n *\r\n * The initial owner is set to the address provided by the deployer. This can\r\n * later be changed with {transferOwnership}.\r\n *\r\n * This module is used through inheritance. It will make available the modifier\r\n * `onlyOwner`, which can be applied to your functions to restrict their use to\r\n * the owner.\r\n */\r\nabstract contract Ownable is Context {\r\n    address private _owner;\r\n\r\n    /**\r\n     * @dev The caller account is not authorized to perform an operation.\r\n     */\r\n    error OwnableUnauthorizedAccount(address account);\r\n\r\n    /**\r\n     * @dev The owner is not a valid owner account. (eg. `address(0)`)\r\n     */\r\n    error OwnableInvalidOwner(address owner);\r\n\r\n    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\r\n\r\n    /**\r\n     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.\r\n     */\r\n    constructor(address initialOwner) {\r\n        if (initialOwner == address(0)) {\r\n            revert OwnableInvalidOwner(address(0));\r\n        }\r\n        _transferOwnership(initialOwner);\r\n    }\r\n\r\n    /**\r\n     * @dev Throws if called by any account other than the owner.\r\n     */\r\n    modifier onlyOwner() {\r\n        _checkOwner();\r\n        _;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the address of the current owner.\r\n     */\r\n    function owner() public view virtual returns (address) {\r\n        return _owner;\r\n    }\r\n\r\n    /**\r\n     * @dev Throws if the sender is not the owner.\r\n     */\r\n    function _checkOwner() internal view virtual {\r\n        if (owner() != _msgSender()) {\r\n            revert OwnableUnauthorizedAccount(_msgSender());\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @dev Leaves the contract without owner. It will not be possible to call\r\n     * `onlyOwner` functions. Can only be called by the current owner.\r\n     *\r\n     * NOTE: Renouncing ownership will leave the contract without an owner,\r\n     * thereby disabling any functionality that is only available to the owner.\r\n     */\r\n    function renounceOwnership() public virtual onlyOwner {\r\n        _transferOwnership(address(0));\r\n    }\r\n\r\n    /**\r\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\r\n     * Can only be called by the current owner.\r\n     */\r\n    function transferOwnership(address newOwner) public virtual onlyOwner {\r\n        if (newOwner == address(0)) {\r\n            revert OwnableInvalidOwner(address(0));\r\n        }\r\n        _transferOwnership(newOwner);\r\n    }\r\n\r\n    /**\r\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\r\n     * Internal function without access restriction.\r\n     */\r\n    function _transferOwnership(address newOwner) internal virtual {\r\n        address oldOwner = _owner;\r\n        _owner = newOwner;\r\n        emit OwnershipTransferred(oldOwner, newOwner);\r\n    }\r\n}\r\n"},"Pausable.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)\r\n\r\npragma solidity ^0.8.20;\r\n\r\nimport \"./Context.sol\";\r\n\r\n/**\r\n * @dev Contract module which allows children to implement an emergency stop\r\n * mechanism that can be triggered by an authorized account.\r\n *\r\n * This module is used through inheritance. It will make available the\r\n * modifiers `whenNotPaused` and `whenPaused`, which can be applied to\r\n * the functions of your contract. Note that they will not be pausable by\r\n * simply including this module, only once the modifiers are put in place.\r\n */\r\nabstract contract Pausable is Context {\r\n    /**\r\n     * @dev Emitted when the pause is triggered by `account`.\r\n     */\r\n    event Paused(address account);\r\n\r\n    /**\r\n     * @dev Emitted when the pause is lifted by `account`.\r\n     */\r\n    event Unpaused(address account);\r\n\r\n    bool private _paused;\r\n\r\n    /**\r\n     * @dev Initializes the contract in unpaused state.\r\n     */\r\n    constructor() {\r\n        _paused = false;\r\n    }\r\n\r\n    /**\r\n     * @dev Modifier to make a function callable only when the contract is not paused.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - The contract must not be paused.\r\n     */\r\n    modifier whenNotPaused() {\r\n        _requireNotPaused();\r\n        _;\r\n    }\r\n\r\n    /**\r\n     * @dev Modifier to make a function callable only when the contract is paused.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - The contract must be paused.\r\n     */\r\n    modifier whenPaused() {\r\n        _requirePaused();\r\n        _;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the contract is paused, and false otherwise.\r\n     */\r\n    function paused() public view virtual returns (bool) {\r\n        return _paused;\r\n    }\r\n\r\n    /**\r\n     * @dev Throws if the contract is paused.\r\n     */\r\n    function _requireNotPaused() internal view virtual {\r\n        require(!paused(), \"Pausable: paused\");\r\n    }\r\n\r\n    /**\r\n     * @dev Throws if the contract is not paused.\r\n     */\r\n    function _requirePaused() internal view virtual {\r\n        require(paused(), \"Pausable: not paused\");\r\n    }\r\n\r\n    /**\r\n     * @dev Triggers stopped state.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - The contract must not be paused.\r\n     */\r\n    function _pause() internal virtual whenNotPaused {\r\n        _paused = true;\r\n        emit Paused(_msgSender());\r\n    }\r\n\r\n    /**\r\n     * @dev Returns to normal state.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - The contract must be paused.\r\n     */\r\n    function _unpause() internal virtual whenPaused {\r\n        _paused = false;\r\n        emit Unpaused(_msgSender());\r\n    }\r\n}\r\n"},"ReentrancyGuard.sol":{"content":"// SPDX-License-Identifier: MIT\r\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)\r\n\r\npragma solidity ^0.8.20;\r\n\r\n/**\r\n * @dev Contract module that helps prevent reentrant calls to a function.\r\n *\r\n * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier\r\n * available, which can be applied to functions to make sure there are no nested\r\n * (reentrant) calls to them.\r\n *\r\n * Note that because there is a single `nonReentrant` guard, functions marked as\r\n * `nonReentrant` may not call one another. This can be worked around by making\r\n * those functions `private`, and then adding `external` `nonReentrant` entry\r\n * points to them.\r\n *\r\n * TIP: If EIP-1153 (transient storage) is available on the chain you\u0027re deploying at,\r\n * consider using {ReentrancyGuardTransient} instead.\r\n *\r\n * TIP: If you would like to learn more about reentrancy and alternative ways\r\n * to protect against it, check out our blog post\r\n * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].\r\n */\r\nabstract contract ReentrancyGuard {\r\n    // Booleans are more expensive than uint256 or any type that takes up a full\r\n    // word because each write operation emits an extra SLOAD to first read the\r\n    // slot\u0027s contents, replace the bits taken up by the boolean, and then write\r\n    // back. This is the compiler\u0027s defense against contract upgrades and\r\n    // pointer aliasing, and it cannot be disabled.\r\n\r\n    // The values being non-zero value makes deployment a bit more expensive,\r\n    // but in exchange the refund on every call to nonReentrant will be lower in\r\n    // amount. Since refunds are capped to a percentage of the total\r\n    // transaction\u0027s gas, it is best to keep them low in cases like this one, to\r\n    // increase the likelihood of the full refund coming into effect.\r\n    uint256 private constant NOT_ENTERED = 1;\r\n    uint256 private constant ENTERED = 2;\r\n\r\n    uint256 private _status;\r\n\r\n    /**\r\n     * @dev Unauthorized reentrant call.\r\n     */\r\n    error ReentrancyGuardReentrantCall();\r\n\r\n    constructor() {\r\n        _status = NOT_ENTERED;\r\n    }\r\n\r\n    /**\r\n     * @dev Prevents a contract from calling itself, directly or indirectly.\r\n     * Calling a `nonReentrant` function from another `nonReentrant`\r\n     * function is not supported. It is possible to prevent this from happening\r\n     * by making the `nonReentrant` function external, and making it call a\r\n     * `private` function that does the actual work.\r\n     */\r\n    modifier nonReentrant() {\r\n        _nonReentrantBefore();\r\n        _;\r\n        _nonReentrantAfter();\r\n    }\r\n\r\n    function _nonReentrantBefore() private {\r\n        // On the first call to nonReentrant, _status will be NOT_ENTERED\r\n        if (_status == ENTERED) {\r\n            revert ReentrancyGuardReentrantCall();\r\n        }\r\n\r\n        // Any calls to nonReentrant after this point will fail\r\n        _status = ENTERED;\r\n    }\r\n\r\n    function _nonReentrantAfter() private {\r\n        // By storing the original value once again, a refund is triggered (see\r\n        // https://eips.ethereum.org/EIPS/eip-2200)\r\n        _status = NOT_ENTERED;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns true if the reentrancy guard is currently set to \"entered\", which indicates there is a\r\n     * `nonReentrant` function in the call stack.\r\n     */\r\n    function _reentrancyGuardEntered() internal view returns (bool) {\r\n        return _status == ENTERED;\r\n    }\r\n}"},"TokenStaking.sol":{"content":"// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.20;\r\n\r\nimport \"./IERC20.sol\";\r\nimport \"./EnumerableSet.sol\";\r\nimport \"./Ownable.sol\";\r\nimport \"./Pausable.sol\";\r\nimport \"./ReentrancyGuard.sol\";\r\n\r\ncontract TokenStaking is Ownable, Pausable, ReentrancyGuard {\r\n    using EnumerableSet for EnumerableSet.AddressSet;\r\n\r\n    IERC20 private stakingToken;\r\n    EnumerableSet.AddressSet private participants;\r\n\r\n    struct Stake {\r\n        uint256 amountStaked;\r\n        uint256 sharesOwned;\r\n        uint256 accumulatedRewards;\r\n    }\r\n\r\n    uint256 private tokenDecimals;\r\n    uint256 private totalStakedTokens;\r\n    uint256 private totalShares;\r\n    uint256 private totalAccumulatedRewards;\r\n    bool private isInitialRatioSet;\r\n\r\n    mapping(address =\u003e Stake) private stakes;\r\n    mapping(address =\u003e bool) private admins;\r\n\r\n    event StakeCreated(\r\n        address indexed user,\r\n        uint256 amount,\r\n        uint256 shares,\r\n        uint256 timestamp\r\n    );\r\n    event StakeWithdrawn(\r\n        address indexed user,\r\n        uint256 amount,\r\n        uint256 shares,\r\n        uint256 timestamp\r\n    );\r\n    event RewardsWithdrawn(\r\n        address indexed user,\r\n        uint256 amount,\r\n        uint256 timestamp\r\n    );\r\n    event RewardsReinvested(\r\n        address indexed user,\r\n        uint256 amount,\r\n        uint256 shares,\r\n        uint256 timestamp\r\n    );\r\n    event AdminAdded(address indexed admin);\r\n    event AdminRemoved(address indexed admin);\r\n\r\n    modifier onlyAdmin() {\r\n        require(\r\n            admins[msg.sender] || owner() == msg.sender,\r\n            \"Caller is not an admin\"\r\n        );\r\n        _;\r\n    }\r\n\r\n    modifier initialRatioNotSet() {\r\n        require(!isInitialRatioSet, \"Initial ratio already set\");\r\n        _;\r\n    }\r\n\r\n    modifier initialRatioSet() {\r\n        require(isInitialRatioSet, \"Initial ratio not set\");\r\n        _;\r\n    }\r\n\r\n    constructor(\r\n        address admin,\r\n        address token,\r\n        uint256 decimals\r\n    ) Ownable(msg.sender) {\r\n        stakingToken = IERC20(token);\r\n        tokenDecimals = decimals;\r\n        addAdmin(admin);\r\n        addAdmin(owner());\r\n    }\r\n\r\n    function pause() external onlyAdmin {\r\n        _pause();\r\n    }\r\n\r\n    function unpause() external onlyAdmin {\r\n        _unpause();\r\n    }\r\n\r\n    function setInitialStake(\r\n        uint256 amount\r\n    ) external onlyAdmin initialRatioNotSet {\r\n        require(\r\n            totalShares == 0 \u0026\u0026 stakingToken.balanceOf(address(this)) == 0,\r\n            \"Already initialized\"\r\n        );\r\n\r\n        participants.add(msg.sender);\r\n        stakes[msg.sender] = Stake({\r\n            amountStaked: amount,\r\n            sharesOwned: amount,\r\n            accumulatedRewards: 0\r\n        });\r\n        totalStakedTokens = amount;\r\n        totalShares = amount;\r\n        isInitialRatioSet = true;\r\n\r\n        require(\r\n            stakingToken.transferFrom(msg.sender, address(this), amount),\r\n            \"Transfer failed\"\r\n        );\r\n\r\n        emit StakeCreated(msg.sender, amount, amount, block.timestamp);\r\n    }\r\n\r\n    function stake(\r\n        uint256 amount\r\n    ) external whenNotPaused initialRatioSet nonReentrant {\r\n        // Actualizamos las recompensas acumuladas para todos los participantes existentes\r\n        updateAccumulatedRewards();\r\n\r\n        // Calculamos las nuevas acciones basadas solo en los tokens iniciales\r\n        uint256 totalInitialTokens = totalStakedTokens -\r\n            totalAccumulatedRewards;\r\n        uint256 shares = (amount * totalShares) / totalInitialTokens;\r\n        require(\r\n            stakingToken.transferFrom(msg.sender, address(this), amount),\r\n            \"Transfer failed\"\r\n        );\r\n\r\n        participants.add(msg.sender);\r\n        stakes[msg.sender].amountStaked += amount;\r\n        stakes[msg.sender].sharesOwned += shares;\r\n        totalStakedTokens += amount;\r\n        totalShares += shares;\r\n\r\n        emit StakeCreated(msg.sender, amount, shares, block.timestamp);\r\n    }\r\n\r\n    function withdrawStake(uint256 amount) external nonReentrant {\r\n        updateAccumulatedRewards();\r\n\r\n        Stake storage userStake = stakes[msg.sender];\r\n        require(userStake.amountStaked \u003e= amount, \"Insufficient staked amount\");\r\n\r\n        uint256 sharesToWithdraw = (amount * userStake.sharesOwned) /\r\n            userStake.amountStaked;\r\n\r\n        userStake.sharesOwned -= sharesToWithdraw;\r\n        userStake.amountStaked -= amount;\r\n        totalStakedTokens -= amount;\r\n        totalShares -= sharesToWithdraw;\r\n\r\n        require(stakingToken.transfer(msg.sender, amount), \"Transfer failed\");\r\n\r\n        if (userStake.amountStaked == 0 \u0026\u0026 userStake.accumulatedRewards == 0) {\r\n            participants.remove(msg.sender);\r\n        }\r\n\r\n        emit StakeWithdrawn(\r\n            msg.sender,\r\n            amount,\r\n            sharesToWithdraw,\r\n            block.timestamp\r\n        );\r\n    }\r\n\r\n    function withdrawRewards(uint256 amount) external nonReentrant {\r\n        updateAccumulatedRewards();\r\n\r\n        Stake storage userStake = stakes[msg.sender];\r\n        uint256 rewards = userStake.accumulatedRewards;\r\n        require(amount \u003c= rewards, \"Insufficient rewards to withdraw\");\r\n\r\n        userStake.accumulatedRewards -= amount;\r\n        totalAccumulatedRewards -= amount;\r\n\r\n        require(stakingToken.transfer(msg.sender, amount), \"Transfer failed\");\r\n\r\n        if (userStake.amountStaked == 0 \u0026\u0026 userStake.accumulatedRewards == 0) {\r\n            participants.remove(msg.sender);\r\n        }\r\n\r\n        emit RewardsWithdrawn(msg.sender, amount, block.timestamp);\r\n    }\r\n\r\n    function reinvestRewards(uint256 amount) external nonReentrant {\r\n        updateAccumulatedRewards();\r\n\r\n        Stake storage userStake = stakes[msg.sender];\r\n        uint256 rewards = userStake.accumulatedRewards;\r\n        require(amount \u003c= rewards, \"Insufficient rewards to reinvest\");\r\n\r\n        userStake.accumulatedRewards -= amount;\r\n        totalAccumulatedRewards -= amount;\r\n\r\n        uint256 totalInitialTokens = totalStakedTokens;\r\n        uint256 shares = (amount * totalShares) / totalInitialTokens;\r\n\r\n        userStake.amountStaked += amount;\r\n        userStake.sharesOwned += shares;\r\n        totalStakedTokens += amount;\r\n        totalShares += shares;\r\n\r\n        emit RewardsReinvested(msg.sender, amount, shares, block.timestamp);\r\n    }\r\n\r\n    function updateAccumulatedRewards() internal {\r\n        uint256 currentRewards = stakingToken.balanceOf(address(this)) -\r\n            totalStakedTokens -\r\n            totalAccumulatedRewards;\r\n        if (currentRewards \u003e 0) {\r\n            for (uint256 i = 0; i \u003c participants.length(); i++) {\r\n                address participant = participants.at(i);\r\n                Stake storage userStake = stakes[participant];\r\n                uint256 userReward = (userStake.sharesOwned * currentRewards) /\r\n                    totalShares;\r\n                userStake.accumulatedRewards += userReward;\r\n            }\r\n            totalAccumulatedRewards += currentRewards;\r\n        }\r\n    }\r\n\r\n    function getTokenPerShare() external view returns (uint256) {\r\n        return\r\n            ((stakingToken.balanceOf(address(this)) - totalAccumulatedRewards) *\r\n                tokenDecimals) / totalShares;\r\n    }\r\n\r\n    function getUserStake(address user) external view returns (uint256) {\r\n        return stakes[user].amountStaked;\r\n    }\r\n\r\n    function getUserShares(address user) external view returns (uint256) {\r\n        return stakes[user].sharesOwned;\r\n    }\r\n\r\n    function getUserReward(address user) external view returns (uint256) {\r\n        Stake memory userStake = stakes[user];\r\n        if (userStake.sharesOwned == 0) {\r\n            return 0;\r\n        }\r\n\r\n        uint256 stakedRatio = (userStake.amountStaked * tokenDecimals) /\r\n            userStake.sharesOwned;\r\n        uint256 currentRatio = ((stakingToken.balanceOf(address(this)) -\r\n            totalAccumulatedRewards) * tokenDecimals) / totalShares;\r\n\r\n        if (currentRatio \u003c= stakedRatio) {\r\n            return userStake.accumulatedRewards;\r\n        }\r\n\r\n        return\r\n            (userStake.sharesOwned * (currentRatio - stakedRatio)) /\r\n            tokenDecimals +\r\n            userStake.accumulatedRewards;\r\n    }\r\n\r\n    function calculateRewardForTokens(\r\n        address user,\r\n        uint256 amount\r\n    ) external view returns (uint256) {\r\n        Stake memory userStake = stakes[user];\r\n        require(userStake.amountStaked \u003e= amount, \"Insufficient staked amount\");\r\n\r\n        uint256 stakedRatio = (userStake.amountStaked * tokenDecimals) /\r\n            userStake.sharesOwned;\r\n        uint256 currentRatio = ((stakingToken.balanceOf(address(this)) -\r\n            totalAccumulatedRewards) * tokenDecimals) / totalShares;\r\n        uint256 sharesToWithdraw = (amount * userStake.sharesOwned) /\r\n            userStake.amountStaked;\r\n\r\n        if (currentRatio \u003c= stakedRatio) {\r\n            return 0;\r\n        }\r\n\r\n        return\r\n            (sharesToWithdraw * (currentRatio - stakedRatio)) / tokenDecimals;\r\n    }\r\n\r\n    function getTotalStakedTokens() external view returns (uint256) {\r\n        return totalStakedTokens;\r\n    }\r\n\r\n    function getTotalShares() external view returns (uint256) {\r\n        return totalShares;\r\n    }\r\n\r\n    function getCurrentRewards() external view returns (uint256) {\r\n        return\r\n            stakingToken.balanceOf(address(this)) -\r\n            totalStakedTokens -\r\n            totalAccumulatedRewards;\r\n    }\r\n\r\n    function getNumberOfParticipants() external view returns (uint256) {\r\n        return participants.length();\r\n    }\r\n\r\n    function refundStakes(\r\n        uint256 start,\r\n        uint256 end\r\n    ) external onlyAdmin nonReentrant {\r\n        require(end \u003c= participants.length(), \"Invalid range\");\r\n\r\n        for (uint256 i = start; i \u003c end; i++) {\r\n            address participant = participants.at(i);\r\n            uint256 amount = stakes[participant].amountStaked;\r\n\r\n            // Actualizamos el estado antes de la transferencia\r\n            totalStakedTokens -= amount;\r\n            require(\r\n                stakingToken.transfer(participant, amount),\r\n                \"Transfer failed\"\r\n            );\r\n\r\n            stakes[participant].amountStaked = 0;\r\n        }\r\n    }\r\n\r\n    function removeUnusedRewards() external onlyAdmin nonReentrant {\r\n        require(totalStakedTokens == 0, \"Stakes still exist\");\r\n\r\n        uint256 balance = stakingToken.balanceOf(address(this));\r\n        require(stakingToken.transfer(msg.sender, balance), \"Transfer failed\");\r\n    }\r\n\r\n    function addAdmin(address admin) public onlyOwner {\r\n        admins[admin] = true;\r\n        emit AdminAdded(admin);\r\n    }\r\n\r\n    function removeAdmin(address admin) public onlyOwner {\r\n        admins[admin] = false;\r\n        emit AdminRemoved(admin);\r\n    }\r\n\r\n    function isAdmin(address admin) public view returns (bool) {\r\n        return admins[admin];\r\n    }\r\n}\r\n"}}