Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Claiming Limit Orders

Learn how to claim filled amounts from your limit orders while keeping them active on the order book.

What is Claiming?

Claiming allows you to collect amounts that have been filled (received) from your limit order without removing the order from the book. This is useful when:

  • Your order is at a good price and you want to keep it active
  • You want to periodically collect profits while maintaining your position
  • The order is partially filled and you want the rest to remain available

Basic Claim

Setup

import { Token, Market, VifRouter, execute, rawOffer, Offer } from "vifdk";
import { createWalletClient, http, publicActions } from "viem";
import { mainnet } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
 
// Define tokens
const WETH = Token.from(WETH_ADDRESS, 18, "WETH", 1n);
const USDC = Token.from(USDC_ADDRESS, 6, "USDC", 1n);
 
// Create market
const market = Market.create({
  base: WETH,
  quote: USDC,
  tickSpacing: 1n,
});
 
// Initialize router
const router = new VifRouter(VIF_ROUTER_ADDRESS, VIF_CORE_ADDRESS, chainId);
 
// Create client
const client = createWalletClient({
  chain: mainnet,
  transport: http(),
  account: privateKeyToAccount(privateKey),
}).extend(publicActions);
 
// Get your offer ID
const offerId = 42;

Claim Filled Amounts

// Build claim action
const actions = router
  .createTypedActions()
  .claim({
    market: market.asks,
    offerId: offerId,
  })
  .build({
    addRecommendedActions: true, // Automatically adds takeAll(USDC)
    receiver: client.account.address,
  });
 
const { commands, args } = actions.txData();
 
// Simulate first
const { result, request } = await client.simulateContract({
  address: VIF_ROUTER_ADDRESS,
  ...execute(commands, args),
  account: client.account,
});
 
// Check what will be claimed
const [{ data: simResult }] = actions.parseSimulationResult(result);
 
console.log("Will claim:", simResult.inbound.amountString, USDC.symbol);
 
// Execute
const receipt = await client.writeContractSync(request);
 
// Parse receipt
const [{ data: claimResult }] = actions.parseLogs(receipt.logs);
console.log("Claimed:", claimResult?.inbound.amountString, USDC.symbol);

Check Before Claiming

Check if there are filled amounts to claim:

// Check if there are filled amounts to claim
const owner = client.account.address;
const packedOffer = await client.readContract({
  address: VIF_CORE_ADDRESS,
  ...rawOffer(market.asks, offerId),
});
 
const offer = Offer.fromPacked(market.asks, packedOffer, offerId, owner);
 
console.log("Available to claim:", offer.data.received.amountString);

Batch Claims

Claim from multiple orders in one transaction:

// Claim from multiple orders
const offerIds = [42, 43, 44];
 
const batchActions = router
  .createTypedActions()
  .claim({
    market: market.asks,
    offerId: offerIds[0],
  })
  .claim({
    market: market.asks,
    offerId: offerIds[1],
  })
  .claim({
    market: market.asks,
    offerId: offerIds[2],
  })
  .build({
    addRecommendedActions: true, // Automatically adds takeAll(USDC)
    receiver: client.account.address,
  });
 
// Claims all three offers in one transaction
const { commands: batchCommands, args: batchArgs } = batchActions.txData();
const receiptBatch = await client.writeContractSync({
  address: VIF_ROUTER_ADDRESS,
  ...execute(batchCommands, batchArgs),
});

Claim vs Cancel

OperationOrder StatusReturnsWhen to Use
ClaimActiveOnly filled amountsKeep providing liquidity at current price
CancelRemovedFilled + unfilled + provisionExit position or change price

Next Steps