creation_sourcecode: {{
    "language": "Solidity",
    "sources": {
      "Context.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Provides information about the current execution context, including the\n * sender of the transaction and its data. While these are generally available\n * via msg.sender and msg.data, they should not be accessed in such a direct\n * manner, since when dealing with meta-transactions the account sending and\n * paying for execution may not be the actual sender (as far as an application\n * is concerned).\n *\n * This contract is only required for intermediate, library-like contracts.\n */\nabstract contract Context {\n    function _msgSender() internal view virtual returns (address) {\n        return msg.sender;\n    }\n\n    function _msgData() internal view virtual returns (bytes calldata) {\n        return msg.data;\n    }\n\n    function _contextSuffixLength() internal view virtual returns (uint256) {\n        return 0;\n    }\n}"
      },
      "IERC20.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Interface of the ERC-20 standard as defined in the ERC.\n */\ninterface IERC20 {\n    /**\n     * @dev Emitted when `value` tokens are moved from one account (`from`) to\n     * another (`to`).\n     *\n     * Note that `value` may be zero.\n     */\n    event Transfer(address indexed from, address indexed to, uint256 value);\n\n    /**\n     * @dev Emitted when the allowance of a `spender` for an `owner` is set by\n     * a call to {approve}. `value` is the new allowance.\n     */\n    event Approval(address indexed owner, address indexed spender, uint256 value);\n\n    /**\n     * @dev Returns the value of tokens in existence.\n     */\n    function totalSupply() external view returns (uint256);\n\n    /**\n     * @dev Returns the value of tokens owned by `account`.\n     */\n    function balanceOf(address account) external view returns (uint256);\n\n    /**\n     * @dev Moves a `value` amount of tokens from the caller's account to `to`.\n     *\n     * Returns a boolean value indicating whether the operation succeeded.\n     *\n     * Emits a {Transfer} event.\n     */\n    function transfer(address to, uint256 value) external returns (bool);\n\n    /**\n     * @dev Returns the remaining number of tokens that `spender` will be\n     * allowed to spend on behalf of `owner` through {transferFrom}. This is\n     * zero by default.\n     *\n     * This value changes when {approve} or {transferFrom} are called.\n     */\n    function allowance(address owner, address spender) external view returns (uint256);\n\n    /**\n     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the\n     * caller's tokens.\n     *\n     * Returns a boolean value indicating whether the operation succeeded.\n     *\n     * IMPORTANT: Beware that changing an allowance with this method brings the risk\n     * that someone may use both the old and the new allowance by unfortunate\n     * transaction ordering. One possible solution to mitigate this race\n     * condition is to first reduce the spender's allowance to 0 and set the\n     * desired value afterwards:\n     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n     *\n     * Emits an {Approval} event.\n     */\n    function approve(address spender, uint256 value) external returns (bool);\n\n    /**\n     * @dev Moves a `value` amount of tokens from `from` to `to` using the\n     * allowance mechanism. `value` is then deducted from the caller's\n     * allowance.\n     *\n     * Returns a boolean value indicating whether the operation succeeded.\n     *\n     * Emits a {Transfer} event.\n     */\n    function transferFrom(address from, address to, uint256 value) external returns (bool);\n}"
      },
      "Ownable.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)\n\npragma solidity ^0.8.0;\n\nimport {Context} from \"./Context.sol\";\n\n/**\n * @dev Contract module which provides a basic access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * The initial owner is set to the address provided by the deployer. This can\n * later be changed with {transferOwnership}.\n *\n * This module is used through inheritance. It will make available the modifier\n * `onlyOwner`, which can be applied to your functions to restrict their use to\n * the owner.\n */\nabstract contract Ownable is Context {\n    address private _owner;\n\n    /**\n     * @dev The caller account is not authorized to perform an operation.\n     */\n    error OwnableUnauthorizedAccount(address account);\n\n    /**\n     * @dev The owner is not a valid owner account. (eg. `address(0)`)\n     */\n    error OwnableInvalidOwner(address owner);\n\n    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n\n    /**\n     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.\n     */\n    constructor(address initialOwner) {\n        if (initialOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(initialOwner);\n    }\n\n    /**\n     * @dev Throws if called by any account other than the owner.\n     */\n    modifier onlyOwner() {\n        _checkOwner();\n        _;\n    }\n\n    /**\n     * @dev Returns the address of the current owner.\n     */\n    function owner() public view virtual returns (address) {\n        return _owner;\n    }\n\n    /**\n     * @dev Throws if the sender is not the owner.\n     */\n    function _checkOwner() internal view virtual {\n        if (owner() != _msgSender()) {\n            revert OwnableUnauthorizedAccount(_msgSender());\n        }\n    }\n\n    /**\n     * @dev Leaves the contract without owner. It will not be possible to call\n     * `onlyOwner` functions. Can only be called by the current owner.\n     *\n     * NOTE: Renouncing ownership will leave the contract without an owner,\n     * thereby disabling any functionality that is only available to the owner.\n     */\n    function renounceOwnership() public virtual onlyOwner {\n        _transferOwnership(address(0));\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Can only be called by the current owner.\n     */\n    function transferOwnership(address newOwner) public virtual onlyOwner {\n        if (newOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(newOwner);\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Internal function without access restriction.\n     */\n    function _transferOwnership(address newOwner) internal virtual {\n        address oldOwner = _owner;\n        _owner = newOwner;\n        emit OwnershipTransferred(oldOwner, newOwner);\n    }\n}"
      },
      "ReentrancyGuard.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Contract module that helps prevent reentrant calls to a function.\n *\n * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier\n * available, which can be applied to functions to make sure there are no nested\n * (reentrant) calls to them.\n *\n * Note that because there is a single `nonReentrant` guard, functions marked as\n * `nonReentrant` may not call one another. This can be worked around by making\n * those functions `private`, and then adding `external` `nonReentrant` entry\n * points to them.\n *\n * TIP: If you would like to learn more about reentrancy and alternative ways\n * to protect against it, check out our blog post\n * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].\n */\nabstract contract ReentrancyGuard {\n    // Booleans are more expensive than uint256 or any type that takes up a full\n    // word because each write operation emits an extra SLOAD to first read the\n    // slot's contents, replace the bits taken up by the boolean, and then write\n    // back. This is the compiler's defense against contract upgrades and\n    // pointer aliasing, and it cannot be disabled.\n\n    // The values being non-zero value makes deployment a bit more expensive,\n    // but in exchange the refund on every call to nonReentrant will be lower in\n    // amount. Since refunds are capped to a percentage of the total\n    // transaction's gas, it is best to keep them low in cases like this one, to\n    // increase the likelihood of the full refund coming into effect.\n    uint256 private constant NOT_ENTERED = 1;\n    uint256 private constant ENTERED = 2;\n\n    uint256 private _status;\n\n    /**\n     * @dev Unauthorized reentrant call.\n     */\n    error ReentrancyGuardReentrantCall();\n\n    constructor() {\n        _status = NOT_ENTERED;\n    }\n\n    /**\n     * @dev Prevents a contract from calling itself, directly or indirectly.\n     * Calling a `nonReentrant` function from another `nonReentrant`\n     * function is not supported. It is possible to prevent this from happening\n     * by making the `nonReentrant` function external, and making it call a\n     * `private` function that does the actual work.\n     */\n    modifier nonReentrant() {\n        _nonReentrantBefore();\n        _;\n        _nonReentrantAfter();\n    }\n\n    function _nonReentrantBefore() private {\n        // On the first call to nonReentrant, _status will be NOT_ENTERED\n        if (_status == ENTERED) {\n            revert ReentrancyGuardReentrantCall();\n        }\n\n        // Any calls to nonReentrant after this point will fail\n        _status = ENTERED;\n    }\n\n    function _nonReentrantAfter() private {\n        // By storing the original value once again, a refund is triggered (see\n        // https://eips.ethereum.org/EIPS/eip-2200)\n        _status = NOT_ENTERED;\n    }\n\n    /**\n     * @dev Returns true if the reentrancy guard is currently set to \"entered\", which indicates there is a\n     * `nonReentrant` function in the call stack.\n     */\n    function _reentrancyGuardEntered() internal view returns (bool) {\n        return _status == ENTERED;\n    }\n}"
      },
      "V2StakingArcx.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport \"./IERC20.sol\";\nimport \"./Ownable.sol\";\nimport \"./ReentrancyGuard.sol\";\n\ncontract StakingArchitexV2 is Ownable, ReentrancyGuard {\n    struct RewardsAdjustement {\n        uint256 timestamp;\n        uint256 totalStaked;\n        uint256 incentiveRate;\n        uint256 rewardRate;\n    }\n\n    struct UserInfo {\n        uint256 balanceStaked;\n        uint256 unlockTime;\n        uint256 lastClaimTimestamp;\n        uint256 lastClaimAdjustmentIndex;\n    }\n\n    IERC20 public stakedERC20;\n    IERC20 public incentiveERC20;\n    IERC20 public rewardERC20;\n    address public stakedAddr;\n    address public incentiveAddr;\n    address public rewardAddr;\n\n    uint256 public totalStaked;\n    uint256 public totalIncentiveRate;\n    uint256 public totalRewardRate;\n\n    mapping(address => UserInfo) public usersInfo;\n\n    RewardsAdjustement[] public adjustements;\n    uint256 public adjustementsLength;\n    uint256 public adjustementDuration = 3;\n\n    uint256 public lockDuration = 14;\n\n    bool public allowUnstaking;\n    bool public pauseStake;\n    bool public pauseClaim;\n    bool public pauseUnstake;\n\n    event Staked(address indexed user, uint256 amount);\n    event Unstaked(address indexed user, uint256 amount);\n    event RewardPaid(address indexed user, uint256 incentive, uint256 reward);\n\n    constructor(address _stakedAddr, address _incentiveAddr, address _rewardAddr) Ownable(msg.sender) {\n        stakedAddr = _stakedAddr;\n        incentiveAddr = _incentiveAddr;\n        rewardAddr = _rewardAddr;\n        \n        stakedERC20 = IERC20(stakedAddr);\n        incentiveERC20 = IERC20(incentiveAddr);\n        rewardERC20 = IERC20(rewardAddr);\n\n        adjustements.push(RewardsAdjustement(\n            block.timestamp, totalStaked, totalIncentiveRate, totalRewardRate\n        ));\n        adjustementsLength += 1;\n    }\n\n    function pause(bool _stakeValue, bool _claimValue, bool _unstakeValue) external onlyOwner {\n        pauseStake = _stakeValue;\n        pauseClaim = _claimValue;\n        pauseUnstake = _unstakeValue;\n    }\n\n    function setAllowUnstaking(bool _value) external onlyOwner {\n        allowUnstaking = _value;\n    }\n\n    function setAdjustementDuration(uint256 _duration) external onlyOwner {\n        adjustementDuration = _duration;\n    }\n\n    function editAdjustement(uint256 _index, uint256 _incentiveRate, uint256 _rewardRate) external onlyOwner {\n        adjustements[_index].incentiveRate = _incentiveRate;\n        adjustements[_index].rewardRate = _rewardRate;\n    }\n\n    function setLockDuration(uint256 _duration) external onlyOwner {\n        lockDuration = _duration;\n    }\n\n    function emergencyWithdraw(bool _all, bool _incentive, bool _reward) external onlyOwner {\n        if (_all) stakedERC20.transfer(msg.sender, stakedERC20.balanceOf(address(this)));\n        else if (_incentive) incentiveERC20.transfer(msg.sender, incentiveERC20.balanceOf(address(this)) - totalStaked);\n\n        if (_reward) rewardERC20.transfer(msg.sender, rewardERC20.balanceOf(address(this)));\n    }\n\n    function adjustRewards(uint256 _incentiveRate, uint256 _rewardRate) external onlyOwner {\n        totalIncentiveRate = _incentiveRate;\n        totalRewardRate = _rewardRate;\n\n        if (block.timestamp - (adjustementDuration * 1 days) >= adjustements[adjustementsLength - 1].timestamp) {\n            adjustements.push(RewardsAdjustement(\n                block.timestamp, totalStaked, totalIncentiveRate, totalRewardRate\n            ));\n            adjustementsLength += 1;\n        } else {\n            adjustements[adjustementsLength - 1].totalStaked = totalStaked;\n            adjustements[adjustementsLength - 1].incentiveRate = totalIncentiveRate;\n            adjustements[adjustementsLength - 1].rewardRate = totalRewardRate;\n        }\n    }\n\n    function stake(uint256 _amount) external nonReentrant {\n        require(!pauseStake, \"Stake paused\");\n        require(stakedERC20.balanceOf(msg.sender) >= _amount, \"Insufficient balance\");\n\n        UserInfo memory userInfo = usersInfo[msg.sender];\n\n        if (userInfo.balanceStaked > 0) {\n            _claim(msg.sender);\n        }\n\n        stakedERC20.transferFrom(msg.sender, address(this), _amount);\n\n        _manageAdjustment();\n\n        userInfo.lastClaimTimestamp = block.timestamp;\n        userInfo.lastClaimAdjustmentIndex = adjustementsLength > 0 ? adjustementsLength - 1 : 0;\n\n        userInfo.balanceStaked += _amount;\n        userInfo.unlockTime = block.timestamp + (lockDuration * 1 days);\n        usersInfo[msg.sender] = userInfo;\n\n        totalStaked += _amount;\n        adjustements[adjustementsLength - 1].totalStaked = totalStaked;\n\n        emit Staked(msg.sender, _amount);\n    }\n\n    function unstake(uint256 _amount) external nonReentrant {\n        require(!pauseUnstake, \"Unstake paused\");\n\n        UserInfo memory userInfo = usersInfo[msg.sender];\n\n        require(userInfo.balanceStaked >= _amount, \"Insufficient stake\");\n        require(allowUnstaking || block.timestamp >= userInfo.unlockTime, \"Lock has not expired\");\n\n        _claim(msg.sender);\n\n        _manageAdjustment();\n\n        userInfo.lastClaimTimestamp = block.timestamp;\n        userInfo.lastClaimAdjustmentIndex = adjustementsLength > 0 ? adjustementsLength - 1 : 0;\n        userInfo.balanceStaked -= _amount;\n        usersInfo[msg.sender] = userInfo;\n\n        stakedERC20.transfer(msg.sender, _amount);\n\n        totalStaked -= _amount;\n        adjustements[adjustementsLength - 1].totalStaked = totalStaked;\n\n        emit Unstaked(msg.sender, _amount);\n    }\n\n    function claim() public nonReentrant {\n        require(!pauseClaim, \"Claim paused\");\n\n        UserInfo memory userInfo = usersInfo[msg.sender];\n\n        require(userInfo.balanceStaked > 0, \"No stake\");\n\n        _claim(msg.sender);\n\n        userInfo.lastClaimTimestamp = block.timestamp;\n        userInfo.lastClaimAdjustmentIndex = adjustementsLength > 0 ? adjustementsLength - 1 : 0;\n\n        usersInfo[msg.sender] = userInfo;\n    }\n\n    function earned(address _user) public view returns (uint256, uint256) {\n        UserInfo memory userInfo = usersInfo[_user];\n\n        if (userInfo.balanceStaked == 0) {\n            return (0, 0);\n        }\n\n        RewardsAdjustement memory adjustement;\n        uint256 incentives;\n        uint256 rewards;\n\n        uint256 time = userInfo.lastClaimTimestamp;\n        uint256 elapsed;\n\n        for (uint256 i = userInfo.lastClaimAdjustmentIndex; i < adjustementsLength; i++) {\n            adjustement = adjustements[i];\n\n            if (i + 1 < adjustementsLength) {\n                elapsed = adjustements[i + 1].timestamp - time;\n                time = adjustements[i + 1].timestamp;\n            } else {\n                elapsed = block.timestamp - time;\n            }\n            \n            incentives += userInfo.balanceStaked * adjustement.incentiveRate * elapsed / adjustement.totalStaked;\n            rewards += userInfo.balanceStaked * adjustement.rewardRate * elapsed / adjustement.totalStaked;\n        }\n\n        return (incentives, rewards);\n    }\n\n    function _claim(address _user) internal {\n        if (pauseClaim) return;\n\n        (uint256 incentivesClaimable, uint256 rewardsClaimable) = earned(_user);\n\n        if (incentivesClaimable > 0) {\n            incentiveERC20.transfer(_user, incentivesClaimable);\n        }\n        if (rewardsClaimable > 0) {\n            rewardERC20.transfer(_user, rewardsClaimable);\n        }\n\n        emit RewardPaid(_user, incentivesClaimable, rewardsClaimable);\n    }\n\n    function _manageAdjustment() internal {\n        if (block.timestamp - (adjustementDuration * 1 days) >= adjustements[adjustementsLength - 1].timestamp) {\n            adjustements.push(RewardsAdjustement(\n                block.timestamp, totalStaked, totalIncentiveRate, totalRewardRate\n            ));\n            adjustementsLength += 1;\n        }\n    }\n}\n"
      }
    },
    "settings": {
      "optimizer": {
        "enabled": true,
        "runs": 800
      },
      "evmVersion": "cancun",
      "remappings": [],
      "outputSelection": {
        "*": {
          "*": [
            "evm.bytecode",
            "evm.deployedBytecode",
            "devdoc",
            "userdoc",
            "metadata",
            "abi"
          ]
        }
      }
    }
  }}