Editing Limit Orders
Learn how to update existing limit orders on the Vif order book.
What is Editing?
Editing a limit order allows you to:
- Change the price (tick) of an existing order
- Increase or decrease the amount offered
- Update the expiration time
- Automatically claim any filled amounts in the same transaction
Basic Edit
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 (from creation or storage)
const offerId = 42;Edit Order
// Build edit action
const actions = router
.createTypedActions()
.limitSingle({
market: market.asks,
initialOfferId: offerId, // Edit existing offer
gives: WETH.amount("2"), // Update to 2 WETH
tick: market.asks.price(3600), // Update price to 3600
})
.build({
addRecommendedActions: true, // Automatically adds settleAll(WETH) and takeAll(USDC)
receiver: client.account.address,
});
const { commands, args } = actions.txData();
// Simulate
const { result, request } = await client.simulateContract({
address: VIF_ROUTER_ADDRESS,
...execute(commands, args),
account: client.account,
});
// Check claimed amount
const [{ data: simResult }] = actions.parseSimulationResult(result);
console.log("Will claim:", simResult.claimedReceived.amountString);
// Execute
const receipt = await client.writeContractSync(request);
// Get results
const [{ data: editResult }] = actions.parseLogs(receipt.logs);
console.log("Claimed:", editResult?.claimedReceived.amountString, "USDC");What You Can Edit
Change Price:
// Change price
router.createTypedActions().limitSingle({
market: market.asks,
initialOfferId: offerId,
gives: WETH.amount("1"),
tick: market.asks.price(3700), // New price
});Increase Size:
// Increase size
router.createTypedActions().limitSingle({
market: market.asks,
initialOfferId: offerId,
gives: WETH.amount("3"), // Increase from 1 to 3
tick: market.asks.price(3500),
});Add Expiration:
// Add expiration
router.createTypedActions().limitSingle({
market: market.asks,
initialOfferId: offerId,
gives: WETH.amount("1"),
tick: market.asks.price(3500),
expiry: new Date(Date.now() + 86400000),
provision: Token.PROVISION_TOKEN.amount("0.001"),
});Batch Edits
Edit multiple orders in one transaction:
// Edit multiple orders
const offerId1 = 42;
const offerId2 = 43;
const offerId3 = 44;
const batchActions = router
.createTypedActions()
.limitSingle({
market: market.asks,
initialOfferId: offerId1,
gives: WETH.amount("1"),
tick: market.asks.price(3500),
})
.limitSingle({
market: market.asks,
initialOfferId: offerId2,
gives: WETH.amount("1"),
tick: market.asks.price(3600),
})
.limitSingle({
market: market.asks,
initialOfferId: offerId3,
gives: WETH.amount("1"),
tick: market.asks.price(3700),
})
.build({
addRecommendedActions: true, // Automatically adds settleAll(WETH) and takeAll(USDC) for all edits
receiver: client.account.address,
});Best Practices
- Always simulate first - Catch errors and see claimed amounts before executing
- Check ownership - Verify you own the offer before editing
- Handle claims - Claims are automatic when editing
- Monitor approvals - Increasing size requires additional approval
Important Notes
Next Steps
- Cancel Limit Orders - Remove orders completely
- Claim Limit Orders - Claim without editing
- Create Limit Orders - Create new orders
- Router Actions - All available actions