Skip to main content

Universal Router Contract

The Universal Router executes swaps across V2 and V3 pools via an encoded command interface. In practice, you do not need to encode commands yourself — the Swap API generates the calldata for you. For the concept overview, see Universal Router Concepts.

Key Functions

execute

Executes a sequence of commands in order.
function execute(
    bytes calldata commands,
    bytes[] calldata inputs,
    uint256 deadline
) external payable;
ParameterTypeDescription
commandsbytesSequence of command codes (1 byte each)
inputsbytes[]ABI-encoded parameters for each command
deadlineuint256Unix timestamp after which the transaction reverts
You typically do not call execute() directly. Use the Swap API to generate the { to, data, value } object, then send it as a transaction.

Command Types

CommandCodeDescription
V2_SWAP_EXACT_IN0x00V2 swap with exact input amount
V2_SWAP_EXACT_OUT0x01V2 swap with exact output amount
V3_SWAP_EXACT_IN0x02V3 swap with exact input amount
V3_SWAP_EXACT_OUT0x03V3 swap with exact output amount
WRAP_ETH0x0bWrap native KAS to WKAS
UNWRAP_WETH0x0cUnwrap WKAS to native KAS
PERMIT2_PERMIT0x0aExecute a Permit2 signature-based permit
TRANSFER0x04Transfer tokens

Usage Pattern

The recommended pattern is to use the Swap API to generate calldata:
// 1. Get calldata from Swap API
const response = await fetch('/swap-api/api/v1/swap', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    tokenIn: '0x...',
    tokenOut: '0x...',
    amountIn: '1000000000000000000',
    tradeType: 0,
    slippage: 0.5,
    recipient: userAddress,
    deadline: 1200
  })
});

const swapData = await response.json();

// 2. Send the transaction directly
const tx = await signer.sendTransaction({
  to: swapData.to,      // Universal Router address
  data: swapData.data,  // Encoded execute() calldata
  value: swapData.value // Native KAS value (0 for ERC-20 swaps)
});

Error Handling

Common revert reasons:
ErrorCauseSolution
EXPIREDTransaction deadline exceededUse a longer deadline or submit faster
V3_INVALID_AMOUNT_OUTOutput below minAmountOutIncrease slippage tolerance
TRANSFER_FROM_FAILEDInsufficient approval or balanceCheck Permit2 approval and token balance
INSUFFICIENT_ETHNot enough native KAS sentEnsure value matches the required amount

ABI

[
  "function execute(bytes calldata commands, bytes[] calldata inputs, uint256 deadline) external payable"
]