After successful fill of our limit order we need to claim it to complete the process. Claiming of limit orders start from LimitOrderManager contract where we call the claimLimitOrder() function
/// @notice To Claim LimitOrder
/// @param tokenId limit order id
/// @param unwrapVault to user wallet/vault
function claimLimitOrder(uint256 tokenId, bool unwrapVault) public {
The function claimLimitOrder
appears to be a function for claiming a previously placed limit order in a concentrated liquidity pool. The limit order is identified by its tokenId
and the user calling the function must be the owner of the limit order. The unwrapVault
parameter indicates whether the user wants to claim the order directly to their wallet or to a vault.
address recipient = limitOrderToken.ownerOf(tokenId);
LimitOrder memory limitOrder = limitOrders[tokenId];
IConcentratedLiquidityPoolStruct.LimitOrderTickData memory limitTick = limitOrder.pool.limitOrderTicks(limitOrder.tick);
require(limitOrder.status != LimitOrderStatus.closed, "Limit Order: Inactive");
uint256 totalClaimableAmount = limitOrder.amountOut - limitOrder.claimedAmount;
The first line assigns the recipient
variable to the owner of the given tokenId
as determined by the limitOrderToken
contract.
The limitOrder
variable is assigned to the LimitOrder
struct stored in the limitOrders
mapping at the index of tokenId
.
The limitTick
variable is assigned to the LimitOrderTickData
struct in the limitOrderTicks
mapping of the IConcentratedLiquidityPoolStruct
contract at the index of limitOrder.tick
.
A require statement checks that the status
field of the limitOrder
struct is not equal to LimitOrderStatus.closed
. If it is, the function reverts with the error message "Limit Order: Inactive".
The totalClaimableAmount
variable is assigned the value of the amountOut
field of the limitOrder
struct minus the claimedAmount
field of the same struct. This represents the total amount that can still be claimed from this limit order.
uint256 amount;
if (limitOrder.zeroForOne) {
amount = limitTick.token1ClaimableGrowth > limitOrder.claimableGrowth1
? (limitTick.token1Claimable >= totalClaimableAmount ? totalClaimableAmount : limitTick.token1Claimable)
: 0;
} else {
amount = limitTick.token0ClaimableGrowth > limitOrder.claimableGrowth0
? (limitTick.token0Claimable >= totalClaimableAmount ? totalClaimableAmount : limitTick.token0Claimable)
: 0;
}
The amount
variable will be set to the lesser of the total claimable amount and the amount that is currently claimable. If the limit order is a "zero for one" order, the amount will be set to the lesser of totalClaimableAmount
and limitTick.token1Claimable
if limitTick.token1ClaimableGrowth
is greater than limitOrder.claimableGrowth1
. If limitTick.token1ClaimableGrowth
is not greater than limitOrder.claimableGrowth1
, amount
will be set to 0. If the limit order is not a "zero for one" order, the amount will be set to the lesser of totalClaimableAmount
and limitTick.token0Claimable
if limitTick.token0ClaimableGrowth
is greater than limitOrder.claimableGrowth0
. If limitTick.token0ClaimableGrowth
is not greater than limitOrder.claimableGrowth0
, amount
will be set to 0.
require(amount > 0, "cant claim zero");
if (amount == 0) return;
limitOrder.pool.claimLimitOrder(amount, limitOrder.tick, limitOrder.zeroForOne);
limitOrders[tokenId].claimedAmount += amount;
(, , , , , , address _token0, address _token1) = limitOrder.pool.getImmutables();
This code snippet determines the amount of the limit order that can be claimed and calls the claimLimitOrder
function on the pool contract to claim the amount. It then updates the claimedAmount
field in the limitOrders
mapping. It also gets the addresses of the tokens in the pool from the getImmutables
function of the pool contract.