creation_sourcecode: {{
    "language": "Solidity",
    "sources": {
      "contracts/DestraRegistryRX01.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport \"@openzeppelin/contracts/utils/ReentrancyGuard.sol\";\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\nimport \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\n\ncontract DestraRegistryRX01 is Ownable, ReentrancyGuard {\n  uint256 public minYears;\n  uint256 public maxYears;\n  uint256 public discountThreshold;\n  uint256 public discountPercentage;\n  uint256 private currentId;\n  uint256 public basePrice;\n\n  struct DomainReservation {\n    uint256 id;\n    address owner;\n    uint256 expiryTime;\n  }\n\n  mapping(uint256 => DomainReservation) public reservationsById;\n  mapping(address => uint256[]) public domainReservationsByOwner;\n\n  address public proofOfSyncFactory;\n\n  event BasePriceUpdate(uint256 newPrice);\n  event ProofOfSyncFactorySet(address indexed proofOfSyncFactory);\n  event Withdrawal(address indexed to, uint256 amount);\n  event Reservation(\n    uint256 indexed reservationId,\n    address indexed owner,\n    uint256 expiryTime\n  );\n  event DomainRenewed(uint256 indexed reservationId, uint256 newExpiryTime);\n  event DomainTransferred(\n    uint256 indexed reservationId,\n    address indexed fromOwner,\n    address toOwner\n  );\n  event YearsLimitsUpdated(uint256 minYears, uint256 maxYears);\n  event DiscountUpdated(uint256 threshold, uint256 percentage);\n  event TransferForeignToken(address token, uint256 amount);\n\n  constructor(\n    uint256 _basePrice,\n    uint256 _minYear,\n    uint256 _maxYear,\n    uint256 _discountThreshold,\n    uint256 _discountPercentage,\n    uint256 _startId\n  ) Ownable(msg.sender) {\n    basePrice = _basePrice;\n    minYears = _minYear;\n    maxYears = _maxYear;\n    discountThreshold = _discountThreshold;\n    discountPercentage = _discountPercentage;\n    currentId = _startId;\n  }\n\n  function reserve(uint256 _years) public payable {\n    require(\n      _years >= minYears && _years <= maxYears,\n      \"Invalid reservation duration\"\n    );\n    uint256 cost = calculateCost(_years);\n    require(msg.value >= cost, \"Insufficient payment\");\n\n    DomainReservation memory newReservation = DomainReservation({\n      id: currentId,\n      owner: msg.sender,\n      expiryTime: block.timestamp + _years * 365 days\n    });\n    reservationsById[currentId] = newReservation;\n    domainReservationsByOwner[msg.sender].push(currentId);\n\n    currentId++;\n\n    emit Reservation(\n      currentId - 1,\n      msg.sender,\n      reservationsById[currentId - 1].expiryTime\n    );\n  }\n\n  function renewReservation(\n    uint256 reservationId,\n    uint256 _years\n  ) public payable {\n    require(\n      _years >= minYears && _years <= maxYears,\n      \"Invalid renewal duration\"\n    );\n    DomainReservation storage reservation = reservationsById[reservationId];\n    require(reservation.owner == msg.sender, \"Not the reservation owner\");\n    require(reservation.expiryTime > 0, \"No reservation to renew\");\n\n    uint256 cost = calculateCost(_years);\n    require(msg.value >= cost, \"Insufficient payment\");\n\n    reservation.expiryTime += _years * 365 days;\n\n    emit DomainRenewed(\n      reservationId,\n      reservationsById[reservationId].expiryTime\n    );\n  }\n\n  function transferReservation(uint256 reservationId, address _to) public {\n    require(_to != address(0), \"Invalid address\");\n    DomainReservation storage reservation = reservationsById[reservationId];\n    require(reservation.owner == msg.sender, \"Not the reservation owner\");\n    require(reservation.expiryTime > 0, \"No reservation to transfer\");\n\n    uint256[] storage senderReservations = domainReservationsByOwner[\n      msg.sender\n    ];\n    for (uint256 i = 0; i < senderReservations.length; i++) {\n      if (senderReservations[i] == reservationId) {\n        senderReservations[i] = senderReservations[\n          senderReservations.length - 1\n        ];\n        senderReservations.pop();\n        break;\n      }\n    }\n\n    reservation.owner = _to;\n    domainReservationsByOwner[_to].push(reservationId);\n\n    emit DomainTransferred(reservationId, msg.sender, _to);\n  }\n\n  function calculateCost(uint256 _years) public view returns (uint256) {\n    uint256 baseCost = basePrice * _years; // Example cost: 1 ETH per year\n    if (discountPercentage > 0 && _years >= discountThreshold) {\n      uint256 discount = (baseCost * discountPercentage) / 100;\n      return baseCost - discount;\n    } else {\n      return baseCost;\n    }\n  }\n\n  function setYearsLimits(\n    uint256 _minYears,\n    uint256 _maxYears\n  ) public onlyOwner {\n    require(_minYears <= _maxYears, \"Invalid range\");\n    minYears = _minYears;\n    maxYears = _maxYears;\n\n    emit YearsLimitsUpdated(_minYears, _maxYears);\n  }\n\n  function setDiscount(\n    uint256 _threshold,\n    uint256 _percentage\n  ) public onlyOwner {\n    discountThreshold = _threshold;\n    discountPercentage = _percentage;\n\n    emit DiscountUpdated(_threshold, _percentage);\n  }\n\n  function getExpiriesByUser(\n    address _owner\n  ) public view returns (uint256[] memory expiries) {\n    uint256[] memory reservationIds = domainReservationsByOwner[_owner];\n    expiries = new uint256[](reservationIds.length);\n\n    for (uint256 i = 0; i < reservationIds.length; i++) {\n      expiries[i] = reservationsById[reservationIds[i]].expiryTime;\n    }\n\n    return expiries;\n  }\n\n  function getExpiryById(uint256 reservationId) public view returns (uint256) {\n    require(\n      reservationsById[reservationId].expiryTime > 0,\n      \"Reservation does not exist\"\n    );\n    return reservationsById[reservationId].expiryTime;\n  }\n\n  function updateReservationPrice(uint256 newPrice) external onlyOwner {\n    basePrice = newPrice;\n    emit BasePriceUpdate(newPrice);\n  }\n\n  function setDestraFactory(address _proofOfSyncFactory) external onlyOwner {\n    require(proofOfSyncFactory == address(0), \"Withdrawal rights transferred\");\n\n    proofOfSyncFactory = _proofOfSyncFactory;\n    emit ProofOfSyncFactorySet(proofOfSyncFactory);\n  }\n\n  function claimBalance() external {\n    require(\n      msg.sender == proofOfSyncFactory,\n      \"Caller is not the Destra factory\"\n    );\n\n    uint256 amount = address(this).balance;\n    require(amount > 0, \"No funds available\");\n\n    (bool success, ) = proofOfSyncFactory.call{value: amount}(\"\");\n    require(success, \"Failed to send ETH\");\n    emit Withdrawal(proofOfSyncFactory, amount);\n  }\n\n  function getUserReservations(\n    address user\n  ) public view returns (uint256[] memory, address[] memory, uint256[] memory) {\n    uint256[] memory ids = domainReservationsByOwner[user];\n    uint256[] memory expirations = new uint256[](ids.length);\n    address[] memory owners = new address[](ids.length);\n\n    for (uint256 i = 0; i < ids.length; i++) {\n      DomainReservation storage reservation = reservationsById[ids[i]];\n      expirations[i] = reservation.expiryTime;\n      owners[i] = reservation.owner;\n    }\n\n    return (ids, owners, expirations);\n  }\n\n  function transferForeignToken(\n    address _token,\n    address _to\n  ) external onlyOwner returns (bool _sent) {\n    require(_token != address(0), \"_token address cannot be 0\");\n    require(_token != address(this), \"Can't withdraw native tokens\");\n    uint256 _contractBalance = IERC20(_token).balanceOf(address(this));\n    _sent = IERC20(_token).transfer(_to, _contractBalance);\n    emit TransferForeignToken(_token, _contractBalance);\n  }\n\n  function withdrawBalance(\n    address payable recipient,\n    uint256 amount\n  ) external onlyOwner nonReentrant {\n    require(proofOfSyncFactory == address(0), \"Withdrawal rights transferred\");\n    require(address(this).balance >= amount, \"Insufficient balance\");\n\n    (bool success, ) = recipient.call{value: amount}(\"\");\n    require(success, \"Failed to send ETH\");\n    emit Withdrawal(recipient, amount);\n  }\n}\n"
      },
      "@openzeppelin/contracts/token/ERC20/IERC20.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Interface of the ERC20 standard as defined in the EIP.\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}\n"
      },
      "@openzeppelin/contracts/access/Ownable.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)\n\npragma solidity ^0.8.20;\n\nimport {Context} from \"../utils/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}\n"
      },
      "@openzeppelin/contracts/utils/ReentrancyGuard.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)\n\npragma solidity ^0.8.20;\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}\n"
      },
      "@openzeppelin/contracts/utils/Context.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)\n\npragma solidity ^0.8.20;\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}\n"
      }
    },
    "settings": {
      "optimizer": {
        "enabled": true,
        "runs": 200
      },
      "outputSelection": {
        "*": {
          "*": [
            "evm.bytecode",
            "evm.deployedBytecode",
            "devdoc",
            "userdoc",
            "metadata",
            "abi"
          ]
        }
      }
    }
  }}