Withdraw Reward

To withdraw the earned reward for a position, the withdrawReward function is used. It requires, as input:

function withdrawReward(uint256 positionId) external byPositionOwner(positionId) {     _withdrawReward(positionId, 0); }
  • positionId -> id corresponding to the position to be transferred

withdrawReward can be called at any time to withdraw the amount of reward tokens accumulated thus far by a position. It internally calls the _withdrawReward method, the helper function used to calculate and send the reward to the position owner.‌

If there is a claimable reward amount, the function transfers the amount to the position owner's address:

reward = calculateFreeFarmingReward(positionId, false);
_safeTransfer(_rewardTokenAddress, farmingPosition.uniqueOwner, reward);

The withdrawn reward amount is added to the rewardPaid of the position; rewardPaid represents the total amount of reward already claimed for the position:

_rewardPaid[farmingPosition.setupIndex] += reward;

Aside from being called directly in this function, withdrawReward will also be called internally when the withdrawLiquidity function is used.‌

When a farmer claims his reward, as explained in the NFT Management section, he also withdraws all of the trading fees earned by his particular NFT. _withdrawReward carries this out by calling the _retrieveGen2LiquidityAndFees method.

uint256 lastRedeemeableBlock = block.number > chosenSetup.endBlock ? chosenSetup.endBlock : block.number;uint256 percentageOfFeesOwnedByPosition = reward * ONE_HUNDRED / (((chosenSetup.rewardPerBlock * (lastRedeemeableBlock - chosenSetup.startBlock)) - _rewardPaid[farmingPosition.setupIndex]));

Then, the feeAmount0 and feeAmount1 amounts are withdrawn from the NFT position using the _collect method, which internally calls the collect method of INonfungiblePositionManager:

address token0;
address token1;
uint256 collectedAmount0;
uint256 collectedAmount1;
(token0, token1, collectedAmount0, collectedAmount0) = _collect(positionId, tokenId);

At this stage, if a fee for the Covenants DFO is to be taken, it is calculated, subtracted from the fee amounts and sent to the Covenants DFO wallet. The rest is sent to the address of the position owner:

if (exitFeePercentage > 0) {
    dfoFee0 = ((collectedAmount0 - decreasedAmount0) * ((exitFeePercentage * 1e18) / ONE_HUNDRED)) / 1e18;
    dfoFee1 = ((collectedAmount1 - decreasedAmount1) * ((exitFeePercentage * 1e18) / ONE_HUNDRED)) / 1e18;
    _safeTransfer(token0, exitFeeWallet, dfoFee0);
    _safeTransfer(token1, exitFeeWallet, dfoFee1);
}
_safeTransfer(token0, recipient, collectedAmount0 - dfoFee0);
_safeTransfer(token1, recipient, collectedAmount1 - dfoFee1);

Otherwise, if the fees are 0, the whole share of the position fees is sent directly to the user.‌

(This differs from the Covenant Farming Generation 1 setup system, in which the fee for the Covenants DFO is taken as a percentage of the user's LP tokens at the moment of liquidity withdrawal).

Last updated