Skip to main content
This guide demonstrates how to integrate Lidian’s cross-chain swap functionality using pure TypeScript and viem. We’ll walk through the complete flow: requesting quotes, accepting a quote, approving tokens, signing the order, and monitoring execution.

Prerequisites

Before you begin, ensure you have:
  • A Web3-enabled wallet (e.g., MetaMask installed in browser)
  • Access to testnet tokens on Base Sepolia and Polygon Amoy
  • Basic understanding of EIP-712 signatures
  • Node.js 18+ and your preferred package manager

Installation

npm
yarn
pnpm

Configuration

Set up your chain and token configurations:

Setup Clients

Create viem clients for interacting with the blockchain:

Step 1: Request a Quote

Create a quote request to get competitive rates from liquidity providers:

Step 2: Poll for Quotes

After requesting a quote, poll the API to retrieve competitive quotes from liquidity providers:

Step 3: Select the Best Quote

Step 4: Check and Approve Token Allowance

Before executing the swap, ensure the Lidian contract has sufficient allowance to spend your tokens:

Step 5: Sign the Order (EIP-712)

Sign the order using EIP-712 typed data:

Step 6: Submit the Swap

Submit the signed order to execute the swap:

Step 7: Monitor Order Status

Poll the order status to track execution:

Complete End-to-End Script

Here’s how to put it all together in a single executable script:

Usage Examples

Basic Swap

Same-Chain Swap

With Custom Status Handling

Order Lifecycle

Understanding the order status flow:
Status Descriptions:
  1. created - Order created in system, awaiting source chain deposit
  2. received - Source chain deposit transaction confirmed
  3. fulfilled - Destination chain tokens delivered to recipient
  4. completed - Settlement finalized on-chain
Terminal Failure States:
  • failed - Order execution failed at any stage
  • cancelled - Order was cancelled by user or system

Helper Utilities

Display Quote Comparison

Calculate Exchange Rate

Estimate Fees

Error Handling

Implement comprehensive error handling:

Best Practices

1. Always Validate Inputs

2. Check User Balance

3. Handle Network Switching

Troubleshooting

No Quotes Received

Transaction Failures

Next Steps

Support