Router Swaps
Router Swaps
Section titled “Router Swaps”The VoidAI Router enables token swaps within the same chain or across compatible networks using the routeSwap method. Like other router operations, routeSwap returns a signed transaction payload for the user’s wallet.
1. Estimate Swap Fees
Section titled “1. Estimate Swap Fees”Always estimate fees before executing a swap to show the user the expected output and total cost.
const estimate = await sdk.bridge.getRouterSwapFeeEstimate({ originAssetId: 1, // ID of the source asset destinationAssetId: 2, // ID of the destination asset amount: 100, operationType: 'SWAP', toAddress: '0xRecipient...',});
console.log(`Estimated Output: ${estimate.swapAmount}`);console.log(`Total Fee: ${estimate.totalFee}`);console.log(`Platform Fee: ${estimate.platformFee}`);console.log(`Tenant Fee: ${estimate.tenantFee}`);Fee Estimate Response
Section titled “Fee Estimate Response”| Property | Type | Description |
|---|---|---|
swapAmount | string | Estimated output amount the recipient will receive. |
totalFee | string | Total combined fee. |
platformFee | string | VoidAI platform fee component. |
ccipFee | string | CCIP fee component (typically 0 for pure swaps). |
tenantFee | string | Fee collected by the tenant/integrator. |
2. Execute a Swap
Section titled “2. Execute a Swap”- Call
routeSwapto receive the transaction payload. - Sign and send the transaction using your wallet provider.
// Step 1 — Request swap payloadconst response = await sdk.bridge.routeSwap({ operationType: 'SWAP', originAssetId: 1, // Source asset ID destinationAssetId: 2, // Destination asset ID fromAddress: '0xSender...', toAddress: '0xRecipient...', amount: 100,});
// Step 2 — Send transaction via wallet providerif (response.success && response.data) { const { to, data, value, chainId } = response.data;
await window.ethereum.request({ method: 'eth_sendTransaction', params: [ { from: '0xSender...', to, data, value, chainId: `0x${chainId.toString(16)}`, // chainId as hex }, ], });}Request: RouteSwapRequest
Section titled “Request: RouteSwapRequest”| Property | Type | Required | Description |
|---|---|---|---|
operationType | 'SWAP' | ✅ | Must be the literal string 'SWAP'. |
originAssetId | number | ✅ | ID of the source asset. |
destinationAssetId | number | ✅ | ID of the destination asset. |
fromAddress | string | ✅ | Sender wallet address. |
toAddress | string | ✅ | Recipient wallet address. |
amount | number | ✅ | Amount to swap. |
Response: RouteTransactionResponse
Section titled “Response: RouteTransactionResponse”| Property | Type | Description |
|---|---|---|
success | boolean | Whether the routing request was successful. |
identifier | string | Unique transaction identifier for tracking. |
data.to | string | Router contract address. |
data.data | string | Encoded calldata for the swap. |
data.value | string | Native token value to send with the transaction. |
data.chainId | number | Chain ID of the source network. |
Cancelling a Swap
Section titled “Cancelling a Swap”If a swap has not yet been finalized on-chain, it can be cancelled:
const result = await sdk.bridge.cancelRouterSwap({ uuid: 'transaction-uuid',});console.log('Cancelled:', result.success);