// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../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.
 *
 * By default, the owner account will be the one that deploys the contract. 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;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @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 {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing 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 {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _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);
    }
}

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



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

pragma solidity ^0.8.0;

/**
 * @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 amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

    /**
     * @dev Moves `amount` 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 amount) 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 `amount` 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 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` 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 amount
    ) external returns (bool);
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

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

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IApi3Pool.sol



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

import "./ITimelockUtils.sol";

interface IApi3Pool is ITimelockUtils {
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IClaimUtils.sol



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

import "./IStakeUtils.sol";

interface IClaimUtils is IStakeUtils {
    event PaidOutClaim(
        address indexed recipient,
        uint256 amount,
        uint256 totalStake
        );

    function payOutClaim(
        address recipient,
        uint256 amount
        )
        external;
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IDelegationUtils.sol



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

import "./IRewardUtils.sol";

interface IDelegationUtils is IRewardUtils {
    event Delegated(
        address indexed user,
        address indexed delegate,
        uint256 shares,
        uint256 totalDelegatedTo
        );

    event Undelegated(
        address indexed user,
        address indexed delegate,
        uint256 shares,
        uint256 totalDelegatedTo
        );

    event UpdatedDelegation(
        address indexed user,
        address indexed delegate,
        bool delta,
        uint256 shares,
        uint256 totalDelegatedTo
        );

    function delegateVotingPower(address delegate) 
        external;

    function undelegateVotingPower()
        external;

    
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IGetterUtils.sol



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

import "./IStateUtils.sol";

interface IGetterUtils is IStateUtils {
    function userVotingPowerAt(
        address userAddress,
        uint256 _block
        )
        external
        view
        returns (uint256);

    function userVotingPower(address userAddress)
        external
        view
        returns (uint256);

    function totalSharesAt(uint256 _block)
        external
        view
        returns (uint256);

    function totalShares()
        external
        view
        returns (uint256);

    function userSharesAt(
        address userAddress,
        uint256 _block
        )
        external
        view
        returns (uint256);

    function userShares(address userAddress)
        external
        view
        returns (uint256);

    function userStake(address userAddress)
        external
        view
        returns (uint256);

    function delegatedToUserAt(
        address userAddress,
        uint256 _block
        )
        external
        view
        returns (uint256);

    function delegatedToUser(address userAddress)
        external
        view
        returns (uint256);

    function userDelegateAt(
        address userAddress,
        uint256 _block
        )
        external
        view
        returns (address);

    function userDelegate(address userAddress)
        external
        view
        returns (address);

    function userLocked(address userAddress)
        external
        view
        returns (uint256);

    function getUser(address userAddress)
        external
        view
        returns (
            uint256 unstaked,
            uint256 vesting,
            uint256 unstakeShares,
            uint256 unstakeAmount,
            uint256 unstakeScheduledFor,
            uint256 lastDelegationUpdateTimestamp,
            uint256 lastProposalTimestamp
            );
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IRewardUtils.sol



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

import "./IGetterUtils.sol";

interface IRewardUtils is IGetterUtils {
    event MintedReward(
        uint256 indexed epochIndex,
        uint256 amount,
        uint256 newApr,
        uint256 totalStake
        );

    function mintReward()
        external;
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IStakeUtils.sol



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

import "./ITransferUtils.sol";

interface IStakeUtils is ITransferUtils{
    event Staked(
        address indexed user,
        uint256 amount,
        uint256 mintedShares,
        uint256 userUnstaked,
        uint256 userShares,
        uint256 totalShares,
        uint256 totalStake
        );

    event ScheduledUnstake(
        address indexed user,
        uint256 amount,
        uint256 shares,
        uint256 scheduledFor,
        uint256 userShares
        );

    event Unstaked(
        address indexed user,
        uint256 amount,
        uint256 userUnstaked,
        uint256 totalShares,
        uint256 totalStake
        );

    function stake(uint256 amount)
        external;

    function depositAndStake(uint256 amount)
        external;

    function scheduleUnstake(uint256 amount)
        external;

    function unstake(address userAddress)
        external
        returns (uint256);

    function unstakeAndWithdraw()
        external;
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/IStateUtils.sol



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

interface IStateUtils {
    event SetDaoApps(
        address agentAppPrimary,
        address agentAppSecondary,
        address votingAppPrimary,
        address votingAppSecondary
        );

    event SetClaimsManagerStatus(
        address indexed claimsManager,
        bool indexed status
        );

    event SetStakeTarget(uint256 stakeTarget);

    event SetMaxApr(uint256 maxApr);

    event SetMinApr(uint256 minApr);

    event SetUnstakeWaitPeriod(uint256 unstakeWaitPeriod);

    event SetAprUpdateStep(uint256 aprUpdateStep);

    event SetProposalVotingPowerThreshold(uint256 proposalVotingPowerThreshold);

    event UpdatedLastProposalTimestamp(
        address indexed user,
        uint256 lastProposalTimestamp,
        address votingApp
        );

    function setDaoApps(
        address _agentAppPrimary,
        address _agentAppSecondary,
        address _votingAppPrimary,
        address _votingAppSecondary
        )
        external;

    function setClaimsManagerStatus(
        address claimsManager,
        bool status
        )
        external;

    function setStakeTarget(uint256 _stakeTarget)
        external;

    function setMaxApr(uint256 _maxApr)
        external;

    function setMinApr(uint256 _minApr)
        external;

    function setUnstakeWaitPeriod(uint256 _unstakeWaitPeriod)
        external;

    function setAprUpdateStep(uint256 _aprUpdateStep)
        external;

    function setProposalVotingPowerThreshold(uint256 _proposalVotingPowerThreshold)
        external;

    function updateLastProposalTimestamp(address userAddress)
        external;

    function isGenesisEpoch()
        external
        view
        returns (bool);
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/ITimelockUtils.sol



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

import "./IClaimUtils.sol";

interface ITimelockUtils is IClaimUtils {
    event DepositedByTimelockManager(
        address indexed user,
        uint256 amount,
        uint256 userUnstaked
        );

    event DepositedVesting(
        address indexed user,
        uint256 amount,
        uint256 start,
        uint256 end,
        uint256 userUnstaked,
        uint256 userVesting
        );

    event VestedTimelock(
        address indexed user,
        uint256 amount,
        uint256 userVesting
        );

    function deposit(
        address source,
        uint256 amount,
        address userAddress
        )
        external;

    function depositWithVesting(
        address source,
        uint256 amount,
        address userAddress,
        uint256 releaseStart,
        uint256 releaseEnd
        )
        external;

    function updateTimelockStatus(address userAddress)
        external;
}

// File: api3-dao/packages/pool/contracts/interfaces/v0.8/ITransferUtils.sol



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

import "./IDelegationUtils.sol";

interface ITransferUtils is IDelegationUtils{
    event Deposited(
        address indexed user,
        uint256 amount,
        uint256 userUnstaked
        );

    event Withdrawn(
        address indexed user,
        uint256 amount,
        uint256 userUnstaked
        );

    event CalculatingUserLocked(
        address indexed user,
        uint256 nextIndEpoch,
        uint256 oldestLockedEpoch
        );

    event CalculatedUserLocked(
        address indexed user,
        uint256 amount
        );

    function depositRegular(uint256 amount)
        external;

    function withdrawRegular(uint256 amount)
        external;

    function precalculateUserLocked(
        address userAddress,
        uint256 noEpochsPerIteration
        )
        external
        returns (bool finished);

    function withdrawPrecalculated(uint256 amount)
        external;
}

// File: contracts/interfaces/IStakeableVesting.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

interface IStakeableVesting {
    event SetBeneficiary(address beneficiary);

    event WithdrawnAsBeneficiary(uint256 amount);

    event WithdrawnAsOwner(uint256 amount);

    function initialize(
        address _owner,
        address _beneficiary,
        uint32 startTimestamp,
        uint32 endTimestamp,
        uint192 amount
    ) external;

    function setBeneficiary(address _beneficiary) external;

    function withdrawAsOwner() external;

    function withdrawAsBeneficiary() external;

    function depositAtPool(uint256 amount) external;

    function withdrawAtPool(uint256 amount) external;

    function withdrawPrecalculatedAtPool(uint256 amount) external;

    function stakeAtPool(uint256 amount) external;

    function scheduleUnstakeAtPool(uint256 amount) external;

    function unstakeAtPool() external;

    function delegateAtPool(address delegate) external;

    function undelegateAtPool() external;

    function stateAtPool()
        external
        view
        returns (
            uint256 unstaked,
            uint256 staked,
            uint256 unstaking,
            uint256 unstakeScheduledFor,
            uint256 lockedStakingRewards,
            address delegate,
            uint256 lastDelegationUpdateTimestamp
        );

    function unvestedAmount() external view returns (uint256);

    function api3Token() external returns (address);

    function beneficiary() external returns (address);

    function vesting()
        external
        returns (uint32 startTimestamp, uint32 endTimestamp, uint192 amount);
}

// File: contracts/StakeableVesting.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IStakeableVesting.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "api3-dao/packages/pool/contracts/interfaces/v0.8/IApi3Pool.sol";

/// @title Contract that implements a stakeable vesting of API3 tokens
/// allocated to a beneficiary, which is revocable by the owner of this
/// contract
/// @notice This contract is an implementation that is required to be cloned by
/// a StakeableVestingFactory contract. The beneficiary of the vesting is
/// expected to interact with this contract through a generic, ABI-based UI
/// such as Etherscan's. See the repo's README for instructions.
/// @dev The contract implements the Api3Pool interface explicitly instead of
/// acting as a general call forwarder (with only Api3Token interactions being
/// restricted) because the user will not be provided with a trusted frontend
/// that will encode the calls. This implementation allows general purpose
/// contract interaction frontends to be used.
contract StakeableVesting is Ownable, IStakeableVesting {
    struct Vesting {
        uint32 startTimestamp;
        uint32 endTimestamp;
        uint192 amount;
    }

    /// @notice Api3Token address
    address public immutable override api3Token;

    /// @notice Api3Pool address
    address public immutable api3Pool;

    /// @notice Beneficiary of the vesting
    address public override beneficiary;

    /// @notice Vesting parameters, including the schedule and the amount
    Vesting public override vesting;

    /// @dev Prevents tokens from being locked by setting an unreasonably late
    /// vesting end timestamp. The vesting periods are expected to be 4 years,
    /// and we have 1 year of buffer here in case the vesting is required to
    /// start in the future.
    uint256
        private constant MAXIMUM_TIME_BETWEEN_INITIALIZATION_AND_VESTING_END =
        5 * 365 days;

    /// @dev Reverts if the sender is not the beneficiary
    modifier onlyBeneficiary() {
        require(msg.sender == beneficiary, "Sender not beneficiary");
        _;
    }

    /// @dev This contract is means to be an implementation for
    /// StakeableVestingFactory to clone. To prevent the implementaion from
    /// being used, the contract is rendered uninitializable and the ownership
    /// is renounced.
    /// @param _api3Token Api3Token address
    /// @param _api3Pool Api3Pool address
    constructor(address _api3Token, address _api3Pool) {
        require(_api3Token != address(0), "Api3Token address zero");
        api3Token = _api3Token;
        require(_api3Pool != address(0), "Api3Pool address zero");
        api3Pool = _api3Pool;
        beneficiary = 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF;
        renounceOwnership();
    }

    /// @notice Initializes a newly cloned StakeableVesting
    /// @dev Since beneficiary is required to be zero address, only clones of
    /// this contract can be initialized.
    /// Anyone can initialize a StakeableVesting clone. The user is required to
    /// prevent others from initializing their clones, for example, by
    /// initializing the clone in the same transaction as it is deployed in.
    /// The StakeableVesting needs to have exactly `_amount` API3 tokens.
    /// @param _owner Owner of this StakeableVesting clone, i.e., the account
    /// that can revoke the vesting
    /// @param _beneficiary Beneficiary of the vesting
    /// @param _startTimestamp Starting timestamp of the vesting
    /// @param _endTimestamp Ending timestamp of the vesting
    /// @param _amount Amount of tokens to be vested over the period
    function initialize(
        address _owner,
        address _beneficiary,
        uint32 _startTimestamp,
        uint32 _endTimestamp,
        uint192 _amount
    ) external override {
        require(beneficiary == address(0), "Already initialized");
        require(_owner != address(0), "Owner address zero");
        require(_beneficiary != address(0), "Beneficiary address zero");
        require(_startTimestamp != 0, "Start timestamp zero");
        require(_endTimestamp > _startTimestamp, "End not later than start");
        require(
            _endTimestamp <=
                block.timestamp +
                    MAXIMUM_TIME_BETWEEN_INITIALIZATION_AND_VESTING_END,
            "End is too far in the future"
        );
        require(_amount != 0, "Amount zero");
        require(
            IERC20(api3Token).balanceOf(address(this)) == _amount,
            "Balance is not vesting amount"
        );
        _transferOwnership(_owner);
        beneficiary = _beneficiary;
        vesting = Vesting({
            startTimestamp: _startTimestamp,
            endTimestamp: _endTimestamp,
            amount: _amount
        });
    }

    /// @notice Called by the owner to set the beneficiary
    /// @dev This can be used to revoke the vesting by setting the beneficiary
    /// to be the owner, or to update the beneficiary address, e.g., because
    /// the previous beneficiary account was compromised
    /// @param _beneficiary Beneficiary of the vesting
    function setBeneficiary(address _beneficiary) external override onlyOwner {
        require(_beneficiary != address(0), "Beneficiary address zero");
        beneficiary = _beneficiary;
        emit SetBeneficiary(_beneficiary);
    }

    /// @notice Called by the owner to withdraw all API3 tokens
    /// @dev This function does not modify the state on purpose, so that the
    /// vesting can easily be reinstituted by returning the withdrawn amount
    function withdrawAsOwner() external override onlyOwner {
        uint256 withdrawalAmount = IERC20(api3Token).balanceOf(address(this));
        require(withdrawalAmount != 0, "No balance to withdraw");
        IERC20(api3Token).transfer(msg.sender, withdrawalAmount);
        emit WithdrawnAsOwner(withdrawalAmount);
    }

    /// @notice Called by the beneficiary as many tokens the vesting schedule
    /// allows
    function withdrawAsBeneficiary() external override onlyBeneficiary {
        uint256 balance = IERC20(api3Token).balanceOf(address(this));
        require(balance != 0, "Balance zero");
        uint256 totalBalance = balance + poolBalance();
        uint256 unvestedAmountInTotalBalance = unvestedAmount();
        require(
            totalBalance > unvestedAmountInTotalBalance,
            "Tokens in balance not vested yet"
        );
        uint256 vestedAmountInTotalBalance = totalBalance -
            unvestedAmountInTotalBalance;
        uint256 withdrawalAmount = vestedAmountInTotalBalance > balance
            ? balance
            : vestedAmountInTotalBalance;
        IERC20(api3Token).transfer(msg.sender, withdrawalAmount);
        emit WithdrawnAsBeneficiary(withdrawalAmount);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting deposit
    /// tokens at the pool
    /// @param amount Amount of tokens
    function depositAtPool(uint256 amount) external override onlyBeneficiary {
        IERC20(api3Token).approve(api3Pool, amount);
        IApi3Pool(api3Pool).depositRegular(amount);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting withdraw
    /// tokens from the pool
    /// @param amount Amount of tokens
    function withdrawAtPool(uint256 amount) external override onlyBeneficiary {
        IApi3Pool(api3Pool).withdrawRegular(amount);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting withdraw
    /// tokens from the pool based on the precalculated amount of locked
    /// staking rewards
    /// @dev This is only needed if the gas cost of calculating the amount of
    /// locked staking rewards exceeds the block gas limit. See the Api3Pool
    /// code for more details.
    /// `precalculateUserLocked()` at Api3Pool needs to be called before using
    /// this function. Since anyone can call it for any user address, it is not
    /// included in this contract.
    /// @param amount Amount of tokens
    function withdrawPrecalculatedAtPool(
        uint256 amount
    ) external override onlyBeneficiary {
        IApi3Pool(api3Pool).withdrawPrecalculated(amount);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting stake
    /// tokens at the pool
    /// @param amount Amount of tokens
    function stakeAtPool(uint256 amount) external override onlyBeneficiary {
        IApi3Pool(api3Pool).stake(amount);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting schedule
    /// an unstaking of tokens at the pool
    /// @param amount Amount of tokens
    function scheduleUnstakeAtPool(
        uint256 amount
    ) external override onlyBeneficiary {
        IApi3Pool(api3Pool).scheduleUnstake(amount);
    }

    /// @notice Called by the beneficiary to have the unstaking that the
    /// StakeableVesting has scheduled to be executed
    /// @dev Anyone can call this function at Api3Pool with the
    /// StakeableVesting address. This function is implemented for the
    /// convenience of the user.
    function unstakeAtPool() external override {
        IApi3Pool(api3Pool).unstake(address(this));
    }

    /// @notice Called by the beneficiary to have the StakeableVesting delegate
    /// its voting power at the pool
    /// @param delegate Address of the account that the voting power will be
    /// delegated to
    function delegateAtPool(
        address delegate
    ) external override onlyBeneficiary {
        IApi3Pool(api3Pool).delegateVotingPower(delegate);
    }

    /// @notice Called by the beneficiary to have the StakeableVesting
    /// undelegate its voting power at the pool
    function undelegateAtPool() external override onlyBeneficiary {
        IApi3Pool(api3Pool).undelegateVotingPower();
    }

    function stateAtPool()
        external
        view
        override
        returns (
            uint256 unstaked,
            uint256 staked,
            uint256 unstaking,
            uint256 unstakeScheduledFor,
            uint256 lockedStakingRewards,
            address delegate,
            uint256 lastDelegationUpdateTimestamp
        )
    {
        delegate = IApi3Pool(api3Pool).userDelegate(address(this));
        lockedStakingRewards = IApi3Pool(api3Pool).userLocked(address(this));
        staked = IApi3Pool(api3Pool).userStake(address(this));
        (
            unstaked,
            ,
            unstaking,
            ,
            unstakeScheduledFor,
            lastDelegationUpdateTimestamp,

        ) = IApi3Pool(api3Pool).getUser(address(this));
    }

    /// @notice Returns the amount of tokens that are yet to be vested based on
    /// the schedule
    /// @return Amount of unvested tokens
    function unvestedAmount() public view override returns (uint256) {
        (uint32 startTimestamp, uint32 endTimestamp, uint192 amount) = (
            vesting.startTimestamp,
            vesting.endTimestamp,
            vesting.amount
        );
        if (block.timestamp <= startTimestamp) {
            return amount;
        } else if (block.timestamp >= endTimestamp) {
            return 0;
        } else {
            uint256 passedTime = block.timestamp - startTimestamp;
            uint256 totalTime = endTimestamp - startTimestamp;
            return amount - (amount * passedTime) / totalTime;
        }
    }

    /// @notice Returns the total balance of StakeableVesting at the pool
    /// @dev Even though it is not certain that the beneficiary will be able to
    /// unstake the funds that are currently staked or being unstaked without
    /// getting slashed, the contract still counts them towards their total
    /// balance in favor of the beneficiary
    /// @return Pool balance
    function poolBalance() private view returns (uint256) {
        uint256 staked = IApi3Pool(api3Pool).userStake(address(this));
        (uint256 unstaked, , uint256 unstaking, , , , ) = IApi3Pool(api3Pool)
            .getUser(address(this));
        return staked + unstaked + unstaking;
    }
}