10 请问下uniswap V3 staker合约里的Stake数据结构里的secondsPerLiquidityInsideInitialX128字段含义和奖励计算原理

V3-staker链接https://github.com/Uniswap/v3-staker/blob/main/contracts/UniswapV3Staker.sol 数据结构

/// @notice Represents a staking incentive
    struct Incentive {
        uint256 totalRewardUnclaimed;
        uint160 totalSecondsClaimedX128;
        uint96 numberOfStakes;
    }

/// @notice Represents a staked liquidity NFT
    struct Stake {
        uint160 secondsPerLiquidityInsideInitialX128;
        uint96 liquidityNoOverflow;
        uint128 liquidityIfOverflow;
    }

Reward计算函数如下 出自https://github.com/Uniswap/v3-staker/blob/main/contracts/libraries/RewardMath.sol

/// @notice Compute the amount of rewards owed given parameters of the incentive and stake
    /// @param totalRewardUnclaimed The total amount of unclaimed rewards left for an incentive
    /// @param totalSecondsClaimedX128 How many full liquidity-seconds have been already claimed for the incentive
    /// @param startTime When the incentive rewards began in epoch seconds
    /// @param endTime When rewards are no longer being dripped out in epoch seconds
    /// @param liquidity The amount of liquidity, assumed to be constant over the period over which the snapshots are measured
    /// @param secondsPerLiquidityInsideInitialX128 The seconds per liquidity of the liquidity tick range as of the beginning of the period
    /// @param secondsPerLiquidityInsideX128 The seconds per liquidity of the liquidity tick range as of the current block timestamp
    /// @param currentTime The current block timestamp, which must be greater than or equal to the start time
    /// @return reward The amount of rewards owed
    /// @return secondsInsideX128 The total liquidity seconds inside the position's range for the duration of the stake
    function computeRewardAmount(
        uint256 totalRewardUnclaimed,
        uint160 totalSecondsClaimedX128,
        uint256 startTime,
        uint256 endTime,
        uint128 liquidity,
        uint160 secondsPerLiquidityInsideInitialX128,
        uint160 secondsPerLiquidityInsideX128,
        uint256 currentTime
    ) internal pure returns (uint256 reward, uint160 secondsInsideX128) {
        // this should never be called before the start time
        assert(currentTime >= startTime);

        // this operation is safe, as the difference cannot be greater than 1/stake.liquidity
        secondsInsideX128 = (secondsPerLiquidityInsideX128 - secondsPerLiquidityInsideInitialX128) * liquidity;

        uint256 totalSecondsUnclaimedX128 =
            ((Math.max(endTime, currentTime) - startTime) << 128) - totalSecondsClaimedX128;

        reward = FullMath.mulDiv(totalRewardUnclaimed, secondsInsideX128, totalSecondsUnclaimedX128);
    }
请先 登录 后评论

1 个回答

g
请先 登录 后评论
  • 0 关注
  • 0 收藏,2922 浏览
  • Ethereal 提出于 2022-01-30 16:34