From 068f828ac8a5588f5053eaa612982a641dddc607 Mon Sep 17 00:00:00 2001 From: "T.K. Kwon" Date: Mon, 16 Oct 2023 04:40:43 +0900 Subject: [PATCH] add reward calculation function for liquidity mining --- contracts/callpaths/LiquidityMiningPath.sol | 9 ++++ contracts/mixins/LiquidityMining.sol | 48 ++++++++++++++------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/contracts/callpaths/LiquidityMiningPath.sol b/contracts/callpaths/LiquidityMiningPath.sol index 9b11c9a7..99637b24 100644 --- a/contracts/callpaths/LiquidityMiningPath.sol +++ b/contracts/callpaths/LiquidityMiningPath.sol @@ -56,6 +56,15 @@ contract LiquidityMiningPath is LiquidityMining { claimConcentratedRewards(payable(msg.sender), poolIdx, lowerTick, upperTick, weeksToClaim); } + function calculateConcentratedRewards( + bytes32 poolIdx, + int24 lowerTick, + int24 upperTick, + uint32[] memory weeksToClaim + ) public view returns (uint256) { + return calculateConcentratedRewards(poolIdx, lowerTick, upperTick, weeksToClaim); + } + function setConcRewards(bytes32 poolIdx, uint32 weekFrom, uint32 weekTo, uint64 weeklyReward) public payable { // require(msg.sender == governance_, "Only callable by governance"); require(weekFrom % WEEK == 0 && weekTo % WEEK == 0, "Invalid weeks"); diff --git a/contracts/mixins/LiquidityMining.sol b/contracts/mixins/LiquidityMining.sol index c0a7c5f5..5899e319 100644 --- a/contracts/mixins/LiquidityMining.sol +++ b/contracts/mixins/LiquidityMining.sol @@ -186,6 +186,26 @@ contract LiquidityMining is PositionRegistrar { int24 upperTick, uint32[] memory weeksToClaim ) internal { + uint256 rewardsToSend = calculateConcentratedRewards( + owner, + poolIdx, + lowerTick, + upperTick, + weeksToClaim + ); + if (rewardsToSend > 0) { + (bool sent, ) = owner.call{value: rewardsToSend}(""); + require(sent, "Sending rewards failed"); + } + } + + function calculateConcentratedRewards( + address payable owner, + bytes32 poolIdx, + int24 lowerTick, + int24 upperTick, + uint32[] memory weeksToClaim + ) internal returns (uint256) { accrueConcentratedPositionTimeWeightedLiquidity( owner, poolIdx, @@ -201,24 +221,20 @@ contract LiquidityMining is PositionRegistrar { for (uint256 i; i < weeksToClaim.length; ++i) { uint32 week = weeksToClaim[i]; require(week + WEEK < block.timestamp, "Week not over yet"); - require( - !concLiquidityRewardsClaimed_[poolIdx][posKey][week], - "Already claimed" - ); - uint256 overallInRangeLiquidity = timeWeightedWeeklyGlobalConcLiquidity_[poolIdx][week]; - if (overallInRangeLiquidity > 0) { - uint256 inRangeLiquidityOfPosition; - for (int24 j = lowerTick + 100; j <= upperTick - 100; ++j) { - inRangeLiquidityOfPosition += timeWeightedWeeklyPositionInRangeConcLiquidity_[poolIdx][posKey][week][j]; + // only claim if not already claimed + if (!concLiquidityRewardsClaimed_[poolIdx][posKey][week]) { + uint256 overallInRangeLiquidity = timeWeightedWeeklyGlobalConcLiquidity_[poolIdx][week]; + if (overallInRangeLiquidity > 0) { + uint256 inRangeLiquidityOfPosition; + for (int24 j = lowerTick + 100; j <= upperTick - 100; ++j) { + inRangeLiquidityOfPosition += timeWeightedWeeklyPositionInRangeConcLiquidity_[poolIdx][posKey][week][j]; + } + // Percentage of this weeks overall in range liquidity that was provided by the user times the overall weekly rewards + rewardsToSend += inRangeLiquidityOfPosition * concRewardPerWeek_[poolIdx][week] / overallInRangeLiquidity; } - // Percentage of this weeks overall in range liquidity that was provided by the user times the overall weekly rewards - rewardsToSend += inRangeLiquidityOfPosition * concRewardPerWeek_[poolIdx][week] / overallInRangeLiquidity; + concLiquidityRewardsClaimed_[poolIdx][posKey][week] = true; } - concLiquidityRewardsClaimed_[poolIdx][posKey][week] = true; - } - if (rewardsToSend > 0) { - (bool sent, ) = owner.call{value: rewardsToSend}(""); - require(sent, "Sending rewards failed"); } + return rewardsToSend; } }