Production-quality TypeScript examples demonstrating intent-based swaps (Fusion mode) on EVM chains and Solana using the 1inch Fusion SDKs.
Intent-based swaps allow you to:
- Swap without paying gas - Resolvers cover execution costs
- Get MEV protection - No front-running or sandwich attacks
- Achieve optimal rates - Dutch auction ensures competitive pricing
The EVM examples demonstrate that the Fusion SDK is truly library-agnostic. The architecture separates concerns:
┌─────────────────────┐ ┌─────────────────────┐
│ Provider (ethers) │ │ Provider (viem) │
│ - getTokenBalance │ │ - getTokenBalance │
│ - approveToken │ │ - approveToken │
│ - web3Like adapter │ │ - web3Like adapter │
└──────────┬──────────┘ └──────────┬──────────┘
│ │
└─────────┬─────────────────┘
│
▼
┌───────────────────────┐
│ createFusionSDK() │ ◀── Library-agnostic!
│ (shared/types.ts) │ Uses provider.web3Like
└───────────────────────┘
To switch libraries, just change one import:
// Use ethers.js:
import { createProvider } from "./providers/ethers.js";
// OR use viem:
import { createProvider } from "./providers/viem.js";
// SDK creation is the same regardless of provider!
const provider = await createProvider(config);
const sdk = createFusionSDK(config, provider.web3Like);| Script | Description | Command |
|---|---|---|
src/evm/quick-start.ts |
Minimal quick start | npm run quick-start |
src/evm/swap-erc20.ts |
Swap ERC-20 to ERC-20 | npm run evm:swap-erc20 |
src/evm/swap-native.ts |
Swap native ETH to ERC-20 | npm run evm:swap-native |
src/evm/swap-with-permit2.ts |
Gasless approvals with Permit2 | npm run evm:swap-permit2 |
| Script | Description | Command |
|---|---|---|
src/solana/quick-start.ts |
Minimal quick start | npm run solana:quick-start |
src/solana/swap-sol-to-jup.ts |
Swap native SOL to JUP | npm run solana:swap |
src/solana/cancel-order.ts |
Cancel an active order | npm run solana:cancel |
For the simplest possible integration, run a quick-start example:
npm install
cp .env.example .env # Add your API_KEY and private keys
# EVM (Ethereum)
npm run quick-start
# Solana
npm run solana:quick-startThese single-file examples demonstrate:
src/evm/quick-start.ts— USDC → 1INCH swap on Ethereum (~65 lines)src/solana/quick-start.ts— SOL → JUP swap on Solana (~95 lines)
npm installcp .env.example .envEdit .env with your values:
# Required: Your 1inch API key from https://business.1inch.com/portal
API_KEY=your-api-key
# EVM Configuration
EVM_PRIVATE_KEY=0x... # Your wallet private key (with 0x prefix)
EVM_RPC_URL= # Optional: defaults to 1inch Web3
EVM_NETWORK_ID=1 # 1=Ethereum, 8453=Base, etc.
# Solana Configuration
SOLANA_PRIVATE_KEY=... # Base58 or base64-encoded secret key
SOLANA_RPC_URL= # Optional: defaults to 1inch Web3# EVM Examples
npm run evm:swap-erc20 # Swap 1INCH → WETH
npm run evm:swap-native # Swap ETH → USDC
npm run evm:swap-permit2 # Swap USDC → WETH with Permit2
# Solana Examples
npm run solana:swap # Swap SOL → JUP
npm run solana:cancel # Cancel an ordersrc/evm/
├── providers/
│ ├── interface.ts # EVMProvider interface definition
│ ├── ethers.ts # ethers.js implementation (~95 lines)
│ └── viem.ts # viem implementation (~105 lines)
├── shared/
│ └── types.ts # Shared constants, utilities, createFusionSDK()
├── swap-erc20.ts # ERC-20 to ERC-20 swap example
├── swap-native.ts # Native ETH to ERC-20 swap example
└── swap-with-permit2.ts # ERC-20 swap with Permit2 example
src/solana/
├── utils/
│ └── connection.ts # Solana connection utilities
├── swap-sol-to-jup.ts
└── cancel-order.ts
Each example file has a clearly marked provider selection section at the top:
// ╔═══════════════════════════════════════════════════════════════════════════╗
// ║ PROVIDER SELECTION - Change this import to switch libraries ║
// ╚═══════════════════════════════════════════════════════════════════════════╝
import { createProvider } from "./providers/ethers.js";
// import { createProvider } from "./providers/viem.js";The SDK initialization is identical for both providers:
const config = loadConfig();
const provider = await createProvider(config); // Library-specific
const sdk = createFusionSDK(config, provider.web3Like); // Library-agnostic!Providers implement a minimal interface for blockchain operations:
interface EVMProvider {
address: string; // Wallet address
web3Like: Web3Like; // Adapter for Fusion SDK
// Read operations
getNativeBalance(address: string): Promise<bigint>;
getTokenBalance(tokenAddress: string, ownerAddress: string): Promise<bigint>;
getTokenAllowance(tokenAddress: string, ownerAddress: string, spenderAddress: string): Promise<bigint>;
// Write operations
approveToken(tokenAddress: string, spenderAddress: string, amount: bigint): Promise<string>;
waitForTransaction(txHash: string): Promise<void>;
}| Network | Chain ID | NetworkEnum |
|---|---|---|
| Ethereum | 1 | ETHEREUM |
| Base | 8453 | COINBASE |
| BNB Chain | 56 | BINANCE |
| Polygon | 137 | POLYGON |
| Arbitrum | 42161 | ARBITRUM |
| Optimism | 10 | OPTIMISM |
| Avalanche | 43114 | AVALANCHE |
| Gnosis | 100 | GNOSIS |
| Fantom | 250 | FANTOM |
| Token | Address |
|---|---|
| 1INCH | 0x111111111117dC0aa78b770fA6A738034120C302 |
| WETH | 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 |
| USDC | 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 |
| Native ETH | 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE |
| Contract | Address |
|---|---|
| Aggregation Router V6 | 0x111111125421ca6dc452d289314280a0f8842a65 |
| Permit2 | 0x000000000022D473030F116dDEE9F6B43aC78BA3 |
| Token | Mint Address | Decimals |
|---|---|---|
| SOL (Native) | Use Address.NATIVE |
9 |
| JUP | JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN |
6 |
| USDC | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v |
6 |
| Aspect | EVM | Solana |
|---|---|---|
| Order Submission | Off-chain to relayer | On-chain escrow |
| Token Approval | Required (ERC-20 approve) | Not needed |
| Finality | ~12 seconds | ~400ms |
| Private Key Format | Hex with 0x prefix | Base58 or base64 |
| Error | Solution |
|---|---|
| "Insufficient allowance" | Run approval transaction first |
| "Quote expired" | Get a fresh quote before creating order |
| Order expires | Try larger amounts or "fast" preset |
| Error | Solution |
|---|---|
| "Invalid PRIVATE_KEY format" | Ensure base58 or base64-encoded 64-byte key |
| "Insufficient funds" | Add SOL for swap amount + fees |
| "Blockhash not found" | Transaction expired, retry |
- 1inch Developer Portal
- Fusion SDK GitHub
- Solana Fusion SDK GitHub
- Intent Swap API Docs
- ethers.js Documentation
- viem Documentation
MIT