Skip to content

Commit

Permalink
Merge pull request #31 from ronin-chain/feature/statistic
Browse files Browse the repository at this point in the history
feat: return collected fees of a position & auto burn when the liquidity is empty and no tokens owed
  • Loading branch information
thaixuandang committed Aug 26, 2024
2 parents 1697ba1 + a5a1003 commit 921a1b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/periphery/NonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ contract NonfungiblePositionManager is
uint128 tokensOwed1;
}

struct CollectedFees {
uint256 token0;
uint256 token1;
}

/// @dev IDs of pools assigned by this contract
mapping(address => uint80) private _poolIds;

Expand All @@ -60,6 +65,9 @@ contract NonfungiblePositionManager is
/// @dev The token ID position data
mapping(uint256 => Position) private _positions;

/// @dev How many tokens are collected by the position, as of the last colection
mapping(uint256 => CollectedFees) private _collectedFees;

/// @dev The ID of the next token that will be minted. Skips 0
uint176 private _nextId = 1;
/// @dev The ID of the next pool that is used for the first time. Skips 0
Expand Down Expand Up @@ -114,6 +122,11 @@ contract NonfungiblePositionManager is
);
}

function collectedFees(uint256 tokenId) external view override returns (uint256 token0, uint256 token1) {
CollectedFees memory fees = _collectedFees[tokenId];
return (fees.token0, fees.token1);
}

/// @dev Caches a pool key
function cachePoolKey(address pool, PoolAddress.PoolKey memory poolKey) private returns (uint80 poolId) {
poolId = _poolIds[pool];
Expand Down Expand Up @@ -333,10 +346,20 @@ contract NonfungiblePositionManager is
// the actual amounts collected are returned
(amount0, amount1) = pool.collect(recipient, position.tickLower, position.tickUpper, amount0Collect, amount1Collect);

CollectedFees storage fees = _collectedFees[params.tokenId];
fees.token0 += amount0;
fees.token1 += amount1;

// sometimes there will be a few less wei than expected due to rounding down in core, but we just subtract the full amount expected
// instead of the actual amount so we can burn the token
(position.tokensOwed0, position.tokensOwed1) = (tokensOwed0 - amount0Collect, tokensOwed1 - amount1Collect);

// if there's no liquidity and no tokens owed, burn the position
if (position.liquidity == 0 && tokensOwed0 == amount0Collect && tokensOwed1 == amount1Collect) {
delete _positions[params.tokenId];
_burn(params.tokenId);
}

emit Collect(params.tokenId, recipient, amount0Collect, amount1Collect);
}

Expand Down
6 changes: 6 additions & 0 deletions src/periphery/interfaces/INonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ interface INonfungiblePositionManager is
uint128 tokensOwed1
);

/// @notice Returns the collected fees for a given token ID
/// @param tokenId The ID of the token for which fees are collected
/// @return token0 The amount of token0 collected
/// @return token1 The amount of token1 collected
function collectedFees(uint256 tokenId) external view returns (uint256 token0, uint256 token1);

struct MintParams {
address token0;
address token1;
Expand Down

0 comments on commit 921a1b6

Please sign in to comment.