Skip to content
Verified contractExactMIT

urLegacy

Address0x222b2a6ac9e10d0090df08505cdd9ff75c0e4dfa
Balance

0 ETH

 

Transactions

15

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,619,145
Creation transaction
0x2df3bf022f…9089b5824c

contracts/urLegacy.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
import {AutomationCompatibleInterface} from "@chainlink/contracts/src/v0.8/automation/AutomationCompatible.sol";

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/interfaces/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; // Import EnumerableSet from OpenZeppelin

contract urLegacy is Ownable, ReentrancyGuard, AutomationCompatibleInterface {
    using EnumerableSet for EnumerableSet.AddressSet;
    using EnumerableSet for EnumerableSet.UintSet;
    // ========================
    // Events
    // ========================
    event SubscriptionActivated(
        address indexed user,
        uint256 indexed subscriptionId,
        uint256 startTime,
        uint256 subScriptionLimit,
        address heirAddress,
        bool isPaidinNative,
        address stableToken
    );

    event SubscriptionRenewed(
        address indexed user,
        uint256 indexed subscriptionId,
        uint256 renewalTime,
        uint256 subscriptionLimit,
        bool isPaidinNative,
        address stableToken
    );

    event EthGasFeeTransfered(
        address sender,
        uint256 amount,
        uint256 subscriptionID,
        uint256 timestamp
    );
    event ETHGasFeeTransferedForTokensDeposit(
        address sender,
        // address receiver,
        uint256 tokensLength,
        uint256 amount,
        uint256 subscriptionID,
        uint256 timestamp
    );
    event ERC20TokenGasFeeTransfered(
        address sender,
        address token,
        uint256 amount,
        uint256 subscpriptionID,
        uint256 timestamp
    );
    event FundsDepositedinETH(
        address indexed user,
        uint256 subscriptionId,
        uint256 ethAmount,
        uint256 time
    );
    event FundsDepositedinToken(
        address indexed user,
        uint256 subscriptionId,
        address token,
        uint256 amount,
        uint256 time
    );
    event FundsDeposited_Consolidated(
        address indexed user,
        uint256 subscriptionId,
        uint256 ethAmount,
        address[] tokens,
        uint256[] amounts,
        uint256 time
    );

    event FundsWithdrawninETH(
        address indexed user,
        uint256 subscriptionId,
        uint256 ethAmount,
        uint256 time
    );
    event FundsWithdrawninToken(
        address indexed user,
        uint256 subscriptionId,
        address token,
        uint256 amount,
        uint256 time
    );
    event FundsWithdrawn_Consolidated(
        address indexed user,
        uint256 subscriptionId,
        uint256 ethAmount,
        address[] tokens,
        uint256[] amounts,
        uint256 time
    );

    event HeirChanged(
        address indexed owner,
        uint256 subscriptionId,
        uint256 time,
        address newHeir
    );

    event SubscriptionDeactivated(
        uint256 subscriptionId,
        address user,
        address heir,
        uint256 time
    );

    // ========================
    // State Variables
    // ========================

    // Global Data
    // ========================
    address public feeReceiver1 = 0x3162210648CC77ba625bC091c834BDd730fDB9a6; // 43%
    address public feeReceiver2 = 0xD6A43D33dEbE97E86f10870c95BBE45453C0ff79; // 43%
    address public keeperFeeReceiver =
        0xA2bbB1C384b29bCEc84968735FEb4b09beAfbfE4; // 4%
    address public devAddress = 0x5F7B6950e5A173dDAc168b12c1Fa7fBAFb78e414; // 10%
    uint256 public gasFeeReceiverPercent = 43; // 43%
    uint256 public keeperFeeReceiverPercent = 4; // 4%
    uint256 public devAddressPercent = 10; // 10%

    // fee of activation
    uint public THRESHOLD_TIME_1 = 10; // For 1-10 days, user will pay 1 USD
    uint public feeForThresholdTime1 = 1; // 1 USD
    uint public THRESHOLD_TIME_2 = 20; // For 11-20 days, user will pay 2 USD
    uint public feeForThresholdTime2 = 2; // 2 USD
    uint public THRESHOLD_TIME_3 = 30; // For 21-30 days, user will pay 3 USD
    uint public feeForThresholdTime3 = 3; // 3 USD
    uint public MAX_THRESHOLD_TIME = 369; // maximum 369 days
    uint public dailyFeeRate = 369; // 0.369 USD per day (stored as 369 to avoid floating point)

    uint private feeInUSDPerTokenDeposit = 2; // on each token deposit 2 USD will be transfered to fund address used to sent funds to the heir

    EnumerableSet.UintSet private activeIdsOfSystem;
    // Unique subscription ID tracker
    uint public subscriptionCounter;
    // id => boolean
    mapping(uint256 => bool) public idStatus; // id is active or not active
    mapping(uint256 => address) public idActivatedBy; // id => activator address

    AggregatorV3Interface public priceFeed; // Chainlink ETH/USD Price Feed

    // Set to store user addresses
    EnumerableSet.AddressSet private users; // A set to store all unique users who enter the system

    // Mapping to store price feed addresses for different tokens
    mapping(address => address) public tokenPriceFeeds;

    // Set to store allowed tokens (both stablecoins and other tokens)
    EnumerableSet.AddressSet private allowedTokens;

    struct Subscription {
        uint id;
        string heirName;
        uint ethBalance; // eth amount deposited in the id
        uint activationTime; // time at which id is created
        uint subscriptionTimeLimit; // times in seconds, for which this id will be activated
        address heir;
        bool activationStatus; // activation is activated or not
        mapping(address => uint256) tokenBalance; // token-address => amount
        EnumerableSet.AddressSet depositedTokens; // deposited tokens in the id
    }

    // User Related Data
    // ========================
    // userAddress => id => Subscription data
    mapping(address => mapping(uint256 => Subscription))
        private subscriptionOfUserAgainstId;
    // userAddress => all ids
    mapping(address => EnumerableSet.UintSet) private allIdsOf;
    // userAddress => active ids
    mapping(address => EnumerableSet.UintSet) private activeIdsOf;
    // userAddress => inactive ids
    mapping(address => EnumerableSet.UintSet) private inactiveIdsOf;

    // Logic Implementation
    // ========================

    constructor(address _priceFeedAddress) Ownable(msg.sender) {
        priceFeed = AggregatorV3Interface(_priceFeedAddress); // Chainlink ETH/USD price feed
    }

    // ========================
    // State-Changing Functions
    // ========================

    // Activate subscription (pay in either ETH or stablecoin)
    function activateSubscription(
        address _heir,
        string memory _heirName,
        address _paymentToken, // payment token alternate to eth
        uint256 _subscriptionTimeLimit,
        // deposits funds details
        uint256 _ETHDeposited, // eth amount deposited in subscription
        address[] calldata _tokens, // tokens deposited in subscription
        uint256[] calldata _amounts // amounts respective to tokens deposited in subscription
    ) public payable nonReentrant {
        address msgSender = msg.sender;
        bool payInETH = _paymentToken == address(0);
        bool isDepositInETH = _ETHDeposited > 0;
        uint256 _ETHFeeForTokensDeposit;
        if (_tokens.length > 0) {
            _ETHFeeForTokensDeposit = getDepositTokensFeeInETH(_tokens.length);
        }

        require(_heir != address(0), "Invalid heir address");
        require(
            _tokens.length == _amounts.length,
            "Tokens and amounts length mismatch"
        );

        // Activate this id to the current user and initialize the subscription
        subscriptionCounter++;
        idStatus[subscriptionCounter] = true;
        activeIdsOf[msgSender].add(subscriptionCounter);
        allIdsOf[msgSender].add(subscriptionCounter);
        idActivatedBy[subscriptionCounter] = msgSender;

        Subscription storage newSubscription = subscriptionOfUserAgainstId[
            msgSender
        ][subscriptionCounter];
        newSubscription.id = subscriptionCounter;
        newSubscription.heirName = _heirName;
        newSubscription.activationTime = block.timestamp;
        newSubscription.subscriptionTimeLimit = _subscriptionTimeLimit;
        newSubscription.activationStatus = true;
        newSubscription.heir = payable(_heir);

        uint256 feeInUSD = getStableCoinFee(_subscriptionTimeLimit);
        uint requiredETHFee;
        if (payInETH) {
            // Payment in ETH
            requiredETHFee = calculateETHFee(feeInUSD);
            uint totalFee = requiredETHFee +
                _ETHDeposited +
                _ETHFeeForTokensDeposit;
            require(msg.value >= totalFee, "Incorrect ETH amount");

            // Return extra eth back to the user
            if (msg.value > totalFee) {
                payable(msgSender).transfer(msg.value - totalFee);
            }

            emit EthGasFeeTransfered(
                msgSender,
                requiredETHFee,
                subscriptionCounter,
                block.timestamp
            );
        } else {
            // Payment in token (modified logic)
            require(
                isAllowedToken(_paymentToken),
                "Token not allowed for payment"
            );

            // Calculate required token amount
            uint256 requiredTokenAmount = calculateTokenFee(
                feeInUSD,
                _paymentToken
            );

            // Transfer tokens to fee receivers
            uint256 feeReceiversAmount = (requiredTokenAmount *
                gasFeeReceiverPercent) / 100;
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    feeReceiver1,
                    feeReceiversAmount
                ),
                "Token transfer failed for gas fee"
            );
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    feeReceiver2,
                    feeReceiversAmount
                ),
                "Token transfer failed for gas fee"
            );

            uint256 keeperFeeAmount = (requiredTokenAmount *
                keeperFeeReceiverPercent) / 100;
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    keeperFeeReceiver,
                    keeperFeeAmount
                ),
                "Token transfer failed for gas fee"
            );

            uint256 remaining = requiredTokenAmount -
                (feeReceiversAmount * 2 + keeperFeeAmount);
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    devAddress,
                    remaining
                ),
                "Token transfer failed for gas fee"
            );

            emit ERC20TokenGasFeeTransfered(
                msgSender,
                _paymentToken,
                requiredTokenAmount,
                subscriptionCounter,
                block.timestamp
            );
        }

        // ---------- Deposits ------------- //
        // handle ETHs deposited
        if (isDepositInETH) {
            newSubscription.ethBalance += _ETHDeposited;
            emit FundsDepositedinETH(
                msgSender,
                subscriptionCounter,
                _ETHDeposited,
                block.timestamp
            );
        }
        if (_tokens.length > 0) {
            // transfer tokens deposits fee in ETH to fund address
            // -----------------------------
            // 43% to each feeReceiver1 & feeReceiver2
            // 4% to keeper Fee Transfer Address
            // 10% to dev Address
            // -----------------------------
            uint _totalETHFeeTransfered = requiredETHFee +
                _ETHFeeForTokensDeposit;
            uint _feeReceiversAmount = (_totalETHFeeTransfered *
                gasFeeReceiverPercent) / 100;
            payable(feeReceiver1).transfer(_feeReceiversAmount);
            payable(feeReceiver2).transfer(_feeReceiversAmount);
            uint _keeperFeeAmount = (_totalETHFeeTransfered *
                keeperFeeReceiverPercent) / 100;
            payable(keeperFeeReceiver).transfer(_keeperFeeAmount);
            uint _remaining = _totalETHFeeTransfered -
                (_feeReceiversAmount * 2 + _keeperFeeAmount); // remaining 10%
            payable(devAddress).transfer(_remaining);

            emit ETHGasFeeTransferedForTokensDeposit(
                msgSender,
                // gasFeeReceiver,
                _tokens.length,
                _ETHFeeForTokensDeposit,
                subscriptionCounter,
                block.timestamp
            );
            // loop through each token and amount
            for (uint i = 0; i < _tokens.length; i++) {
                address token = _tokens[i];
                uint amount = _amounts[i];

                require(amount > 0, "Invalid amount");

                // Add the amount to the user's balance
                require(
                    IERC20(token).transferFrom(
                        msgSender,
                        address(this),
                        amount
                    ),
                    "Token transfer failed"
                );

                newSubscription.depositedTokens.add(token); // it is first time, so there is no need to check for already added.
                newSubscription.tokenBalance[token] += amount;

                emit FundsDepositedinToken(
                    msgSender,
                    subscriptionCounter,
                    token,
                    amount,
                    block.timestamp
                );
            }
        } else {
            // transfer tokens deposits fee in ETH to fund address
            // -----------------------------
            // 43% to each feeReceiver1 & feeReceiver2
            // 4% to keeper Fee Transfer Address
            // 10% to dev Address
            // -----------------------------
            uint _feeReceiversAmount = (requiredETHFee *
                gasFeeReceiverPercent) / 100;
            payable(feeReceiver1).transfer(_feeReceiversAmount);
            payable(feeReceiver2).transfer(_feeReceiversAmount);
            uint _keeperFeeAmount = (requiredETHFee *
                keeperFeeReceiverPercent) / 100;
            payable(keeperFeeReceiver).transfer(_keeperFeeAmount);
            uint _remaining = requiredETHFee -
                (_feeReceiversAmount * 2 + _keeperFeeAmount); // remaining 10%
            payable(devAddress).transfer(_remaining);
        }
        emit FundsDeposited_Consolidated(
            msgSender,
            subscriptionCounter,
            _ETHDeposited,
            _tokens,
            _amounts,
            block.timestamp
        );

        // Add the user to the set of users if it's their first time activating a subscription
        if (!users.contains(msgSender)) {
            users.add(msgSender);
        }

        activeIdsOfSystem.add(subscriptionCounter);

        emit SubscriptionActivated(
            msgSender,
            subscriptionCounter,
            block.timestamp,
            _subscriptionTimeLimit,
            _heir,
            payInETH,
            _paymentToken
        );
    }

    // Deposit funds (user can deposit in ETH or stablecoin)
    function depositFunds(
        address[] calldata _tokens,
        uint256[] calldata _amounts,
        uint256 _ETHDeposited, // eth amount deposited in subscription
        uint256 _subscriptionId
    ) external payable nonReentrant {
        address msgSender = msg.sender;
        uint256 msgValue = msg.value;
        uint256 _ETHFeeForTokensDeposit = getDepositTokensFeeInETH(
            _tokens.length
        );

        require(
            msgValue >= _ETHDeposited + _ETHFeeForTokensDeposit,
            "Incorrect ETH amount"
        );
        require(_tokens.length > 0 || msgValue != 0, "No funds provided");
        require(
            _tokens.length == _amounts.length,
            "Tokens and amounts length mismatch"
        );
        require(idStatus[_subscriptionId], "Invalid ID");
        require(
            activeIdsOf[msgSender].contains(_subscriptionId),
            "Not activated by you"
        );
        require(getSubscriptionActiveStatus(_subscriptionId), "Time Elapsed");

        Subscription storage sub = subscriptionOfUserAgainstId[msgSender][
            _subscriptionId
        ];

        if (_ETHDeposited > 0) {
            sub.ethBalance += msgValue - _ETHFeeForTokensDeposit;
            emit FundsDepositedinETH(
                msgSender,
                _subscriptionId,
                msgValue - _ETHFeeForTokensDeposit,
                block.timestamp
            );
        }
        if (_tokens.length > 0) {
            // transfer tokens deposits fee in ETH to fund address
            // -----------------------------
            // 43% to each feeReceiver1 & feeReceiver2
            // 4% to keeper Fee Transfer Address
            // 10% to dev Address
            // -----------------------------
            uint _feeReceiversAmount = (_ETHFeeForTokensDeposit *
                gasFeeReceiverPercent) / 100;
            payable(feeReceiver1).transfer(_feeReceiversAmount);
            payable(feeReceiver2).transfer(_feeReceiversAmount);
            uint _keeperFeeAmount = (_ETHFeeForTokensDeposit *
                keeperFeeReceiverPercent) / 100;
            payable(keeperFeeReceiver).transfer(_keeperFeeAmount);
            uint _remaining = _ETHFeeForTokensDeposit -
                (_feeReceiversAmount * 2 + _keeperFeeAmount); // remaining 10%
            payable(devAddress).transfer(_remaining);
            emit ETHGasFeeTransferedForTokensDeposit(
                msgSender,
                _tokens.length,
                _ETHFeeForTokensDeposit,
                subscriptionCounter,
                block.timestamp
            );

            // loop through each token and amount
            for (uint i = 0; i < _tokens.length; i++) {
                address token = _tokens[i];
                uint amount = _amounts[i];

                require(amount > 0, "Invalid amount");

                // Add the amount to the user's balance
                require(
                    IERC20(token).transferFrom(
                        msgSender,
                        address(this),
                        amount
                    ),
                    "Token transfer failed"
                );

                if (!sub.depositedTokens.contains(token)) {
                    sub.depositedTokens.add(token);
                }

                sub.tokenBalance[token] += amount;

                emit FundsDepositedinToken(
                    msgSender,
                    _subscriptionId,
                    token,
                    amount,
                    block.timestamp
                );
            }
        }

        emit FundsDeposited_Consolidated(
            msgSender,
            _subscriptionId,
            msgValue,
            _tokens,
            _amounts,
            block.timestamp
        );
    }

    // Withdraw funds (user can withdraw in ETH or stablecoin)
    function withdrawFunds(
        uint _ethAmount,
        address[] calldata _tokens,
        uint256[] calldata _amounts,
        uint256 _subscriptionId
    ) public nonReentrant {
        address msgSender = msg.sender;
        bool withdrawInETH = _ethAmount != 0;

        require(
            _tokens.length == _amounts.length,
            "Tokens and amounts length mismatch"
        );
        require(idStatus[_subscriptionId], "Invalid ID");
        require(
            activeIdsOf[msgSender].contains(_subscriptionId),
            "Not activated by you"
        );
        require(getSubscriptionActiveStatus(_subscriptionId), "Time Elapsed");

        Subscription storage sub = subscriptionOfUserAgainstId[msgSender][
            _subscriptionId
        ];

        if (withdrawInETH) {
            require(sub.ethBalance >= _ethAmount, "Insufficient ETH balance"); // Check if user has enough ETH
            sub.ethBalance -= _ethAmount;
            payable(msg.sender).transfer(_ethAmount); // Withdraw in ETH

            emit FundsWithdrawninETH(
                msgSender,
                _subscriptionId,
                _ethAmount,
                block.timestamp
            );
        }
        if (_tokens.length > 0) {
            for (uint i = 0; i < _tokens.length; i++) {
                address token = _tokens[i];
                uint amount = _amounts[i];

                require(amount > 0, "Invalid amount");
                require(
                    sub.depositedTokens.contains(token),
                    "Token not deposited"
                );
                require(
                    sub.tokenBalance[token] >= amount,
                    "Insufficient token balance"
                );

                require(
                    IERC20(token).transfer(msgSender, amount),
                    "Token transfer failed"
                );

                sub.tokenBalance[token] -= amount;

                if (sub.tokenBalance[token] == 0) {
                    sub.depositedTokens.remove(token);
                }

                emit FundsWithdrawninToken(
                    msgSender,
                    _subscriptionId,
                    token,
                    amount,
                    block.timestamp
                );
            }
        }
        emit FundsWithdrawn_Consolidated(
            msgSender,
            _subscriptionId,
            _ethAmount,
            _tokens,
            _amounts,
            block.timestamp
        );
    }

    // Renew subscription (pay in either ETH or stablecoin)
    function renewSubscription(
        address _paymentToken, // Changed from _stableCoin to _paymentToken
        uint256 _subscriptionId,
        uint256 _subscriptionTimeLimit
    ) public payable nonReentrant {
        require(idStatus[_subscriptionId], "Invalid ID");
        address msgSender = msg.sender;
        bool payInETH = msg.value != 0;

        Subscription storage sub = subscriptionOfUserAgainstId[msgSender][
            _subscriptionId
        ];
        require(getSubscriptionActiveStatus(_subscriptionId), "Time Elapsed");

        uint256 feeInUSD = getStableCoinFee(_subscriptionTimeLimit);

        if (payInETH) {
            // Existing ETH payment logic
            uint256 requiredFee = calculateETHFee(feeInUSD);
            require(msg.value >= requiredFee, "Incorrect ETH subscription fee");

            if (msg.value > requiredFee) {
                payable(msgSender).transfer(msg.value - requiredFee);
            }

            uint _feeReceiversAmount = (requiredFee * gasFeeReceiverPercent) /
                100;
            payable(feeReceiver1).transfer(_feeReceiversAmount);
            payable(feeReceiver2).transfer(_feeReceiversAmount);
            uint _keeperFeeAmount = (requiredFee * keeperFeeReceiverPercent) /
                100;
            payable(keeperFeeReceiver).transfer(_keeperFeeAmount);
            uint _remaining = requiredFee -
                (_feeReceiversAmount * 2 + _keeperFeeAmount);
            payable(devAddress).transfer(_remaining);

            emit EthGasFeeTransfered(
                msgSender,
                requiredFee,
                subscriptionCounter,
                block.timestamp
            );
        } else {
            // New token payment logic
            require(
                isAllowedToken(_paymentToken),
                "Token not allowed for payment"
            );

            uint256 requiredTokenAmount = calculateTokenFee(
                feeInUSD,
                _paymentToken
            );

            uint256 feeReceiversAmount = (requiredTokenAmount *
                gasFeeReceiverPercent) / 100;
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    feeReceiver1,
                    feeReceiversAmount
                ),
                "Token transfer failed for gas fee"
            );
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    feeReceiver2,
                    feeReceiversAmount
                ),
                "Token transfer failed for gas fee"
            );

            uint256 keeperFeeAmount = (requiredTokenAmount *
                keeperFeeReceiverPercent) / 100;
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    keeperFeeReceiver,
                    keeperFeeAmount
                ),
                "Token transfer failed for gas fee"
            );

            uint256 remaining = requiredTokenAmount -
                (feeReceiversAmount * 2 + keeperFeeAmount);
            require(
                IERC20(_paymentToken).transferFrom(
                    msgSender,
                    devAddress,
                    remaining
                ),
                "Token transfer failed for gas fee"
            );

            emit ERC20TokenGasFeeTransfered(
                msgSender,
                _paymentToken,
                requiredTokenAmount,
                subscriptionCounter,
                block.timestamp
            );
        }

        sub.subscriptionTimeLimit =
            sub.subscriptionTimeLimit +
            _subscriptionTimeLimit;

        emit SubscriptionRenewed(
            msgSender,
            _subscriptionId,
            block.timestamp,
            _subscriptionTimeLimit,
            payInETH,
            _paymentToken
        );
    }

    // Change heir
    function changeHeir(
        uint256 _subscriptionId,
        address _newHeir,
        string memory _newHeirName
    ) external {
        address msgSender = msg.sender;

        require(_newHeir != address(0), "New heir address cannot be zero");

        require(idStatus[_subscriptionId], "Invalid ID"); // Verify the subscription ID is valid
        require( // Verify that the sender is the owner of the subscription
            activeIdsOf[msgSender].contains(_subscriptionId),
            "Only the owner of this subscription can change the heir"
        );
        require(getSubscriptionActiveStatus(_subscriptionId), "Time Elapsed"); // Ensure the change is made within the time limit

        // Access the subscription data
        Subscription storage sub = subscriptionOfUserAgainstId[msgSender][
            _subscriptionId
        ];

        // Change the heir
        sub.heir = _newHeir;
        sub.heirName = _newHeirName;

        // Emit event for the heir change
        emit HeirChanged(msgSender, _subscriptionId, block.timestamp, _newHeir);
    }

    // Add allowed token with its price feed
    function addAllowedToken(
        address _token,
        address _priceFeed
    ) public onlyOwner {
        require(_token != address(0), "Invalid token address");
        require(_priceFeed != address(0), "Invalid price feed address");

        // Check if token is already allowed
        require(!isAllowedToken(_token), "Token already allowed");

        // Add token to allowed tokens set
        allowedTokens.add(_token);

        // Set price feed for the token
        tokenPriceFeeds[_token] = _priceFeed;
    }

    // Remove allowed token
    function removeAllowedToken(address _token) public onlyOwner {
        require(isAllowedToken(_token), "Token not allowed");

        // Remove token from allowed tokens set
        allowedTokens.remove(_token);

        // Remove price feed mapping
        delete tokenPriceFeeds[_token];
    }

    // Add multiple allowed tokens with their price feeds
    function addAllowedTokens(
        address[] calldata _tokens,
        address[] calldata _priceFeeds
    ) public onlyOwner {
        require(_tokens.length == _priceFeeds.length, "Arrays length mismatch");

        for (uint256 i = 0; i < _tokens.length; i++) {
            require(_tokens[i] != address(0), "Invalid token address");
            require(_priceFeeds[i] != address(0), "Invalid price feed address");
            require(!isAllowedToken(_tokens[i]), "Token already allowed");

            // Add token to allowed tokens set
            allowedTokens.add(_tokens[i]);

            // Set price feed for the token
            tokenPriceFeeds[_tokens[i]] = _priceFeeds[i];
        }
    }

    // Remove multiple allowed tokens
    function removeAllowedTokens(address[] calldata _tokens) public onlyOwner {
        for (uint256 i = 0; i < _tokens.length; i++) {
            require(isAllowedToken(_tokens[i]), "Token not allowed");

            // Remove token from allowed tokens set
            allowedTokens.remove(_tokens[i]);

            // Remove price feed mapping
            delete tokenPriceFeeds[_tokens[i]];
        }
    }

    // Admin function change feeReceiver1 address
    function setFeeReceiver1(address _feeReceiver1) public onlyOwner {
        feeReceiver1 = _feeReceiver1;
    }

    // Admin function change feeReceiver2 address
    function setFeeReceiver2(address _feeReceiver2) public onlyOwner {
        feeReceiver2 = _feeReceiver2;
    }

    // Admin function change keeperFeeReceiver address
    function setKeeperFeeReceiver(address _keeperFeeReceiver) public onlyOwner {
        keeperFeeReceiver = _keeperFeeReceiver;
    }

    // Admin function to change FeeReceiver percentage
    function setFeeReceiverPercent(
        uint256 _feeReceiverPercent
    ) public onlyOwner {
        gasFeeReceiverPercent = _feeReceiverPercent;
    }

    // Admin function to change keeperFeeReceiver Percent
    function setKeeperFeeReceiverPercent(
        uint256 _keeperFeeReceiverPercent
    ) public onlyOwner {
        keeperFeeReceiverPercent = _keeperFeeReceiverPercent;
    }

    // Admin function to change devAddress Percent
    function setDevAddressPercent(uint256 _devAddressPercent) public onlyOwner {
        devAddressPercent = _devAddressPercent;
    }

    // Admin function change devAddress address
    function setDevAddress(address _devAddress) public onlyOwner {
        devAddress = _devAddress;
    }

    // Admin function to change THRESHOLD_TIME_1
    function setTHRESHOLD_TIME_1(uint _THRESHOLD_TIME_1) public onlyOwner {
        THRESHOLD_TIME_1 = _THRESHOLD_TIME_1;
    }

    // Admin function to change feeForThresholdTime1
    function setFeeForThresholdTime1(
        uint _feeForThresholdTime1
    ) public onlyOwner {
        feeForThresholdTime1 = _feeForThresholdTime1;
    }

    // Admin function to change THRESHOLD_TIME_2
    function setTHRESHOLD_TIME_2(uint _THRESHOLD_TIME_2) public onlyOwner {
        THRESHOLD_TIME_2 = _THRESHOLD_TIME_2;
    }

    // Admin function to change feeForThresholdTime2
    function setFeeForThresholdTime2(
        uint _feeForThresholdTime2
    ) public onlyOwner {
        feeForThresholdTime2 = _feeForThresholdTime2;
    }

    // Admin function to change THRESHOLD_TIME_3
    function setTHRESHOLD_TIME_3(uint _THRESHOLD_TIME_3) public onlyOwner {
        THRESHOLD_TIME_3 = _THRESHOLD_TIME_3;
    }

    // Admin function to change feeForThresholdTime3
    function setFeeForThresholdTime3(
        uint _feeForThresholdTime3
    ) public onlyOwner {
        feeForThresholdTime3 = _feeForThresholdTime3;
    }

    // Admin function to change feeForThresholdTime3
    function setMAX_THRESHOLD_TIME(uint _MAX_THRESHOLD_TIME) public onlyOwner {
        MAX_THRESHOLD_TIME = _MAX_THRESHOLD_TIME;
    }

    // Admin function to change fee per Token deposit
    function setFeeInUSDPerTokenDeposit(
        uint _feePerTokenDeposit
    ) public onlyOwner {
        feeInUSDPerTokenDeposit = _feePerTokenDeposit;
    }

    // Admin function to change daily fee rate
    function setDailyFeeRate(uint _dailyFeeRate) public onlyOwner {
        dailyFeeRate = _dailyFeeRate;
    }

    function performUpkeep(bytes calldata performData) external {
        // Decode the IDs that require upkeep
        uint256[] memory expiredIds = abi.decode(performData, (uint256[]));

        for (uint i = 0; i < expiredIds.length; i++) {
            uint256 _id = expiredIds[i];
            require(idStatus[_id], "Invalid ID"); // Verify the subscription ID is valid
            address _user = idActivatedBy[_id];

            Subscription storage sub = subscriptionOfUserAgainstId[_user][_id];

            // Transfer all assets (ETH and tokens) to the heir
            if (sub.ethBalance > 0) {
                uint256 ethAmount = sub.ethBalance;

                // Update state before the external call
                sub.ethBalance = 0;

                // Transfer ETH to the heir after updating state
                payable(sub.heir).transfer(ethAmount); // Transfer ETH to the heir
            }

            // Transfer all deposited tokens to the heir
            for (uint j = 0; j < sub.depositedTokens.length(); j++) {
                address token = sub.depositedTokens.at(j);
                uint256 tokenBalance = sub.tokenBalance[token];

                if (tokenBalance > 0) {
                    IERC20(token).transfer(sub.heir, tokenBalance); // Transfer tokens to the heir
                    sub.tokenBalance[token] = 0; // Reset token balance after transfer
                }
            }

            // Mark the subscription ID as inactive
            idStatus[_id] = false;

            // Mark the activation status as false
            sub.activationStatus = false;

            // Remove the subscription from the user's active IDs
            activeIdsOfSystem.remove(_id);
            activeIdsOf[_user].remove(_id);
            inactiveIdsOf[_user].add(_id);

            // Emit an event for the subscription deactivation and asset transfer
            emit SubscriptionDeactivated(_id, _user, sub.heir, block.timestamp);
        }
    }

    // ========================
    // Read-Only Functions
    // ========================

    // Check for all IDs that need upkeep (expired subscriptions)
    function checkUpkeep(
        bytes calldata /*checkData*/
    ) external view returns (bool upkeepNeeded, bytes memory performData) {
        uint256[] memory tempExpiredIds = new uint256[](
            activeIdsOfSystem.length()
        ); // Temporary array
        uint256 counter;

        for (uint i = 0; i < activeIdsOfSystem.length(); i++) {
            uint256 _id = activeIdsOfSystem.at(i);
            address _user = idActivatedBy[_id];

            Subscription storage sub = subscriptionOfUserAgainstId[_user][_id];

            // Check if the subscription has passed the subscription period limit
            if (
                (block.timestamp >=
                    sub.activationTime + sub.subscriptionTimeLimit) &&
                idStatus[_id]
            ) {
                tempExpiredIds[counter] = _id;
                counter++;
            }
        }

        if (counter > 0) {
            upkeepNeeded = true;
            // Copy to a smaller array
            uint256[] memory expiredIds = new uint256[](counter);
            for (uint i = 0; i < counter; i++) {
                expiredIds[i] = tempExpiredIds[i];
            }
            performData = abi.encode(expiredIds);
        }
    }

    // function that will take length of tokens and convert fee of all tokens in USD
    function getDepositTokensFeeInUSD(
        uint256 _tokens
    ) public view returns (uint) {
        return (_tokens * feeInUSDPerTokenDeposit * 1e18);
    }

    // function that will take length of tokens and convert fee of all tokens in ETH
    function getDepositTokensFeeInETH(
        uint256 _tokens
    ) public view returns (uint) {
        int ethPriceInUSD = getLatestPrice(); // Price of 1 ETH in USD (with 8 decimals)
        require(ethPriceInUSD > 0, "Invalid price from oracle");

        uint tokensFeeInUSD = getDepositTokensFeeInUSD(_tokens);

        uint ethFee = (tokensFeeInUSD * 1e8) / uint(ethPriceInUSD); // Conversion to ETH amount
        return ethFee;
    }

    // Function to get subscription status through ID
    // It will return false if more then grace period time limit has elapsed otherwise true
    function getSubscriptionActiveStatus(
        uint256 _subscriptionId
    ) public view returns (bool status) {
        address activator = idActivatedBy[_subscriptionId];
        Subscription storage sub = subscriptionOfUserAgainstId[activator][
            _subscriptionId
        ];

        if (block.timestamp <= sub.activationTime + sub.subscriptionTimeLimit) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @notice Calculates the subscription fee in stablecoin based on the subscription duration.
     * @param _subscriptionLimitInSeconds The subscription duration in seconds.
     * @return _feeInStableCoin The calculated fee in stablecoin (1 USD = 1e18).
     */
    function getStableCoinFee(
        uint256 _subscriptionLimitInSeconds
    ) public view returns (uint256 _feeInStableCoin) {
        uint256 subscriptionDays = _subscriptionLimitInSeconds / 86400; // Convert seconds to days

        // Determine the fee based on the duration
        if (subscriptionDays <= THRESHOLD_TIME_1) {
            _feeInStableCoin = feeForThresholdTime1 * 1e18; // 1-10 days: $1
        } else if (subscriptionDays <= THRESHOLD_TIME_2) {
            _feeInStableCoin = feeForThresholdTime2 * 1e18; // 11-20 days: $2
        } else if (subscriptionDays <= THRESHOLD_TIME_3) {
            _feeInStableCoin = feeForThresholdTime3 * 1e18; // 21-30 days: $3
        } else if (subscriptionDays <= MAX_THRESHOLD_TIME) {
            // After 30 days, charge dailyFeeRate per additional day i.e. 0.369$ per day
            uint256 additionalDays = subscriptionDays - THRESHOLD_TIME_3;
            _feeInStableCoin =
                feeForThresholdTime3 *
                1e18 +
                (((additionalDays * dailyFeeRate) * 1e18) / 1000); // dailyFeeRate per day
        } else {
            revert("Invalid Subscription days");
        }
    }

    // Get the latest price of ETH in USD (used to calculate equivalent ETH for stablecoin fee)
    function getLatestPrice() public view returns (int) {
        (, int price, , , ) = priceFeed.latestRoundData();
        return price;
    }

    // Calculate the equivalent ETH for the subscription fee based on the current price
    // This is done by scaling the stablecoin fee by the current ETH price
    // @param _feeInStableCoin is the fee in stablecoin in wei
    function calculateETHFee(
        uint256 _feeInStableCoin
    ) public view returns (uint) {
        int ethPriceInUSD = getLatestPrice(); // Price of 1 ETH in USD (with 8 decimals)
        require(ethPriceInUSD > 0, "Invalid price from oracle");

        uint ethFee = (_feeInStableCoin * 1e8) / uint(ethPriceInUSD); // Conversion to ETH amount
        return ethFee;
    }

    function convertEthToUSD(
        uint256 ethAmountInWei
    ) public view returns (uint256 usdtAmount) {
        int ethPriceInUsdt = getLatestPrice(); // The price of 1 ETH in USDT, scaled by 10^8
        require(ethPriceInUsdt > 0, "Invalid ETH price");

        // Convert wei to ETH
        uint256 ethAmountInEth = ethAmountInWei / 1e18;

        // Calculate the USDT amount (price feed has 8 decimals)
        uint256 usdtAmountInDecimals = ethAmountInEth * uint256(ethPriceInUsdt);

        // Normalize by 1e8 to adjust for price feed decimals
        return (usdtAmountInDecimals / 1e8) * 1e18;
    }

    // function to get all active ids of the system
    function getActiveIdsOfSystem()
        public
        view
        returns (uint256[] memory activeIds)
    {
        return activeIdsOfSystem.values();
    }

    // Function to get total number of unique users entered in the system
    function totalUsers() public view returns (uint) {
        return users.length();
    }

    // Get user address by index (for listing purposes)
    function getUserByIndex(uint index) public view returns (address) {
        require(index < users.length(), "Index out of bounds");
        return users.at(index);
    }

    // Get all users
    function getAllUsers() public view returns (address[] memory allUsers) {
        return users.values();
    }

    // Check if a user is in the system
    function isUser(address _user) public view returns (bool) {
        return users.contains(_user);
    }

    // Getter for all subscription ids of a user
    function getAllIdsOfUser(
        address user
    ) public view returns (uint256[] memory) {
        uint256 length = allIdsOf[user].length();
        uint256[] memory allIds = new uint256[](length);

        for (uint256 i = 0; i < length; i++) {
            allIds[i] = allIdsOf[user].at(i);
        }

        return allIds;
    }

    // Getter for active subscription ids of a user
    function getActiveIdsOfUser(
        address user
    ) public view returns (uint256[] memory) {
        uint256 length = activeIdsOf[user].length();
        uint256[] memory activeIds = new uint256[](length);

        for (uint256 i = 0; i < length; i++) {
            activeIds[i] = activeIdsOf[user].at(i);
        }

        return activeIds;
    }

    // Getter for inactive subscription ids of a user
    function getInActiveIdsOfUser(
        address user
    ) public view returns (uint256[] memory) {
        uint256 length = inactiveIdsOf[user].length();
        uint256[] memory inactiveIds = new uint256[](length);

        for (uint256 i = 0; i < length; i++) {
            inactiveIds[i] = inactiveIdsOf[user].at(i);
        }

        return inactiveIds;
    }

    // Getter for deposited tokens of a specific subscription
    function getDepositedTokensOfUserAgainstID(
        address user,
        uint256 id
    ) public view returns (address[] memory) {
        Subscription storage subscription = subscriptionOfUserAgainstId[user][
            id
        ];
        uint256 length = subscription.depositedTokens.length();
        address[] memory tokens = new address[](length);

        for (uint256 i = 0; i < length; i++) {
            tokens[i] = subscription.depositedTokens.at(i);
        }

        return tokens;
    }

    // Getter for token balance of a specific subscription
    function getTokenBalanceOfUserAgainstIDForToken(
        address user,
        uint256 id,
        address token
    ) public view returns (uint256) {
        Subscription storage subscription = subscriptionOfUserAgainstId[user][
            id
        ];
        return subscription.tokenBalance[token];
    }

    // Getter for a specific subscription for a user by id

    // which contains tokens address only.

    function getSubscriptionDetailsById_withTokens(
        address user,
        uint256 id
    )
        public
        view
        returns (
            uint256 subId,
            uint256 ethBalance,
            uint256 activationTime,
            uint256 expirationTime,
            bool isActive,
            address heir,
            string memory heirName,
            address[] memory depositedTokens
        )
    {
        Subscription storage subscription = subscriptionOfUserAgainstId[user][
            id
        ];

        address[]
            memory depositedTokensOfUserAgainstId = getDepositedTokensOfUserAgainstID(
                user,
                id
            );

        return (
            subscription.id,
            subscription.ethBalance,
            subscription.activationTime,
            subscription.activationTime + subscription.subscriptionTimeLimit,
            subscription.activationStatus,
            subscription.heir,
            subscription.heirName,
            depositedTokensOfUserAgainstId
        );
    }

    struct TokenBalance {
        address token;
        uint256 balance;
    }

    // Get subscription details of a specific subscription for a user by id
    // which contains all the tokens with their balances also
    function getSubscriptionDetailsById(
        address user,
        uint256 id
    )
        public
        view
        returns (
            uint256 subId,
            uint256 ethBalance,
            uint256 activationTime,
            uint256 expirationTime,
            bool isActive,
            address heir,
            string memory heirName,
            TokenBalance[] memory tokensWithBalances
        )
    {
        Subscription storage subscription = subscriptionOfUserAgainstId[user][
            id
        ];
        address[] memory depositedTokens = getDepositedTokensOfUserAgainstID(
            user,
            id
        );
        tokensWithBalances = new TokenBalance[](depositedTokens.length);

        for (uint256 i = 0; i < depositedTokens.length; i++) {
            uint tokenBalance = getTokenBalanceOfUserAgainstIDForToken(
                user,
                id,
                depositedTokens[i]
            );

            tokensWithBalances[i] = TokenBalance({
                token: depositedTokens[i],
                balance: tokenBalance
            });
        }

        return (
            subscription.id,
            subscription.ethBalance,
            subscription.activationTime,
            subscription.activationTime + subscription.subscriptionTimeLimit,
            subscription.activationStatus,
            subscription.heir,
            subscription.heirName,
            tokensWithBalances
        );
    }

    // Check if a token is allowed
    function isAllowedToken(address _token) public view returns (bool) {
        return allowedTokens.contains(_token);
    }

    // Get the price feed address for a token
    function getTokenPriceFeed(address _token) public view returns (address) {
        return tokenPriceFeeds[_token];
    }

    // Get all allowed tokens
    function getAllowedTokens() public view returns (address[] memory) {
        return allowedTokens.values();
    }

    // Calculate fee in a specific token
    function calculateTokenFee(
        uint256 _feeInUSD,
        address _token
    ) public view returns (uint256) {
        require(isAllowedToken(_token), "Token not allowed");

        // Get price feed for the token
        address priceFeedAddress = tokenPriceFeeds[_token];
        require(priceFeedAddress != address(0), "Price feed not set for token");

        // Get token price in USD
        AggregatorV3Interface _priceFeed = AggregatorV3Interface(
            priceFeedAddress
        );
        (, int price, , , ) = _priceFeed.latestRoundData();
        require(price > 0, "Invalid price from oracle");

        // Get token decimals
        uint8 decimals = IERC20Metadata(_token).decimals();

        // Calculate token amount needed
        // _feeInUSD is in 1e18 (USD with 18 decimals)
        // price is in 1e8 (USD with 8 decimals)
        // We need to adjust for both the price feed decimals and token decimals
        uint256 tokenAmount = (_feeInUSD * 1e8) / uint256(price);

        // Adjust for token decimals
        if (decimals < 18) {
            tokenAmount = tokenAmount / (10 ** (18 - decimals));
        } else if (decimals > 18) {
            tokenAmount = tokenAmount * (10 ** (decimals - 18));
        }

        return tokenAmount;
    }
}

@chainlink/contracts/src/v0.8/automation/AutomationBase.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract AutomationBase {
  error OnlySimulatedBackend();

  /**
   * @notice method that allows it to be simulated via eth_call by checking that
   * the sender is the zero address.
   */
  function _preventExecution() internal view {
    // solhint-disable-next-line avoid-tx-origin
    if (tx.origin != address(0) && tx.origin != address(0x1111111111111111111111111111111111111111)) {
      revert OnlySimulatedBackend();
    }
  }

  /**
   * @notice modifier that allows it to be simulated via eth_call by checking
   * that the sender is the zero address.
   */
  modifier cannotExecute() {
    _preventExecution();
    _;
  }
}

@chainlink/contracts/src/v0.8/automation/AutomationCompatible.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {AutomationBase} from "./AutomationBase.sol";
import {AutomationCompatibleInterface} from "./interfaces/AutomationCompatibleInterface.sol";

abstract contract AutomationCompatible is AutomationBase, AutomationCompatibleInterface {}

@chainlink/contracts/src/v0.8/automation/interfaces/AutomationCompatibleInterface.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// solhint-disable-next-line interface-starts-with-i
interface AutomationCompatibleInterface {
  /**
   * @notice method that is simulated by the keepers to see if any work actually
   * needs to be performed. This method does does not actually need to be
   * executable, and since it is only ever simulated it can consume lots of gas.
   * @dev To ensure that it is never called, you may want to add the
   * cannotExecute modifier from KeeperBase to your implementation of this
   * method.
   * @param checkData specified in the upkeep registration so it is always the
   * same for a registered upkeep. This can easily be broken down into specific
   * arguments using `abi.decode`, so multiple upkeeps can be registered on the
   * same contract and easily differentiated by the contract.
   * @return upkeepNeeded boolean to indicate whether the keeper should call
   * performUpkeep or not.
   * @return performData bytes that the keeper should call performUpkeep with, if
   * upkeep is needed. If you would like to encode data to decode later, try
   * `abi.encode`.
   */
  function checkUpkeep(bytes calldata checkData) external returns (bool upkeepNeeded, bytes memory performData);

  /**
   * @notice method that is actually executed by the keepers, via the registry.
   * The data returned by the checkUpkeep simulation will be passed into
   * this method to actually be executed.
   * @dev The input to this method should not be trusted, and the caller of the
   * method should not even be restricted to any single registry. Anyone should
   * be able call it, and the input should be validated, there is no guarantee
   * that the data passed in is the performData returned from checkUpkeep. This
   * could happen due to malicious keepers, racing keepers, or simply a state
   * change while the performUpkeep transaction is waiting for confirmation.
   * Always validate the data passed in.
   * @param performData is the data which was passed back from the checkData
   * simulation. If it is encoded, it can easily be decoded into other types by
   * calling `abi.decode`. This data should not be trusted, and should be
   * validated against the contract's current state.
   */
  function performUpkeep(bytes calldata performData) external;
}

@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// solhint-disable-next-line interface-starts-with-i
interface AggregatorV3Interface {
  function decimals() external view returns (uint8);

  function description() external view returns (string memory);

  function version() external view returns (uint256);

  function getRoundData(
    uint80 _roundId
  ) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

  function latestRoundData()
    external
    view
    returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

@openzeppelin/contracts/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);
    }
}

@openzeppelin/contracts/interfaces/IERC20Metadata.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC20Metadata.sol)

pragma solidity ^0.8.20;

import {IERC20Metadata} from "../token/ERC20/extensions/IERC20Metadata.sol";

@openzeppelin/contracts/token/ERC20/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.20;

import {IERC20} from "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

@openzeppelin/contracts/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;
    }
}

@openzeppelin/contracts/utils/ReentrancyGuard.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.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 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;
    }
}

@openzeppelin/contracts/utils/structs/EnumerableSet.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/EnumerableSet.sol)
// This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.

pragma solidity ^0.8.20;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```solidity
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 *
 * [WARNING]
 * ====
 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
 * unusable.
 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
 * array of EnumerableSet.
 * ====
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;
        // Position is the index of the value in the `values` array plus 1.
        // Position 0 is used to mean a value is not in the set.
        mapping(bytes32 value => uint256) _positions;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._positions[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We cache the value's position to prevent multiple reads from the same storage slot
        uint256 position = set._positions[value];

        if (position != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 valueIndex = position - 1;
            uint256 lastIndex = set._values.length - 1;

            if (valueIndex != lastIndex) {
                bytes32 lastValue = set._values[lastIndex];

                // Move the lastValue to the index where the value to delete is
                set._values[valueIndex] = lastValue;
                // Update the tracked position of the lastValue (that was just moved)
                set._positions[lastValue] = position;
            }

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the tracked position for the deleted slot
            delete set._positions[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._positions[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function _values(Set storage set) private view returns (bytes32[] memory) {
        return set._values;
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
        bytes32[] memory store = _values(set._inner);
        bytes32[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(AddressSet storage set) internal view returns (address[] memory) {
        bytes32[] memory store = _values(set._inner);
        address[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(UintSet storage set) internal view returns (uint256[] memory) {
        bytes32[] memory store = _values(set._inner);
        uint256[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.