Cancelling Limit Orders
Learn how to cancel (delete) limit orders from the Vif order book.
What is Cancelling?
Cancelling a limit order removes it from the order book and returns:
- Any unfilled amount (remaining
gives) - Any filled amount that hasn't been claimed (accumulated
received) - The provision (if the order had an expiration)
All returned amounts become credits in flash accounting and must be taken in the same transaction.
Basic Cancel
Setup
import { Token, Market, VifRouter, execute } 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;Cancel Order
// Build cancel action
const actions = router
.createTypedActions()
.cancel({
market: market.asks,
offerId: offerId,
})
.build({
addRecommendedActions: true, // Automatically adds takeAll(WETH) and 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 returned
const [{ data: simResult }] = actions.parseSimulationResult(result);
console.log("Will receive back:");
console.log("- Outbound:", simResult.outbound.amountString, WETH.symbol);
console.log("- Inbound:", simResult.inbound.amountString, USDC.symbol);
console.log("- Provision:", simResult.provision.amountString, "ETH");
// Execute
const receipt = await client.writeContractSync(request);
// Parse receipt
const [{ data: cancelResult }] = actions.parseLogs(receipt.logs);
console.log("Received:", cancelResult);Cancel with Provision
If your order had an expiration, you'll get the provision back:
// Cancel order with provision
const actionsWithProvision = router
.createTypedActions()
.cancel({
market: market.asks,
offerId: offerId,
})
.build({
addRecommendedActions: true, // Automatically adds takeAll(WETH), takeAll(USDC), and takeAll(NATIVE_TOKEN)
receiver: client.account.address,
});
const {
commands: provisionCommands,
args: provisionArgs,
} = actionsWithProvision.txData();
// Execute
const receiptWithProvision = await client.writeContractSync({
address: VIF_ROUTER_ADDRESS,
...execute(provisionCommands, provisionArgs),
});Batch Cancellations
Cancel multiple orders in one transaction:
// Cancel multiple orders
const offerIds = [42, 43, 44];
const batchActions = router
.createTypedActions()
.cancel({
market: market.asks,
offerId: offerIds[0],
})
.cancel({
market: market.asks,
offerId: offerIds[1],
})
.cancel({
market: market.asks,
offerId: offerIds[2],
})
.build({
addRecommendedActions: true, // Automatically adds takeAll for WETH, USDC, and NATIVE_TOKEN
receiver: client.account.address,
});
// Cancels all three orders in one transaction
const { commands: batchCommands, args: batchArgs } = batchActions.txData();
const receiptBatch = await client.writeContractSync({
address: VIF_ROUTER_ADDRESS,
...execute(batchCommands, batchArgs),
});What Cancel Returns
Cancelling returns:
- Unfilled amount - The remaining
givesthat hasn't been matched - Filled amount - Any accumulated
receivedfrom partial fills - Provision - If the order had an expiration
Cancel vs Claim
| Operation | Returns | When to Use |
|---|---|---|
| Cancel | Unfilled + filled + provision | Exit position, change price significantly |
| Claim | Only filled amounts | Keep order active, collect fills periodically |
Next Steps
- Claim Limit Orders - Collect fills without cancelling
- Create Limit Orders - Create new orders
- Edit Limit Orders - Update existing orders
- Router Actions - All available actions