Swap
Swap tokens on any chain or bridge across chains with sendSwap. Users authenticate once with their passkey, and the swap executes automatically.
Using sendSwap
The sendSwap method handles token swaps and cross-chain bridges with built-in slippage protection.
Install the SDK
npm install @rhinestone/1authCreate client
client.ts
import { OneAuthClient } from '@rhinestone/1auth'
export const client = new OneAuthClient({
providerUrl: 'https://passkey.1auth.box',
})Swap tokens
const result = await client.sendSwap({
username: 'alice',
targetChain: 8453, // Base
fromToken: 'ETH',
toToken: 'USDC',
amount: '0.1', // Human-readable amount of toToken
})
if (result.success) {
console.log('Swap executed:', result.intentId)
console.log('Quote:', result.quote)
}Done
You have successfully swapped tokens!
Cross-chain bridge
sendSwap automatically handles cross-chain bridging. The user's funds are sourced from any chain where they have balance:
// Bridge ETH from any chain to USDC on Base
const result = await client.sendSwap({
username: 'alice',
targetChain: 8453, // Base
fromToken: 'ETH',
toToken: 'USDC',
amount: '100', // Get 100 USDC on Base
})The orchestrator finds the best route, whether that's a same-chain swap or a cross-chain bridge.
Slippage control
Set maximum slippage in basis points (1 bp = 0.01%):
const result = await client.sendSwap({
username: 'alice',
targetChain: 8453,
fromToken: 'ETH',
toToken: 'USDC',
amount: '100',
slippageBps: 100, // 1% max slippage (default: 50 = 0.5%)
})Supported tokens
You can use token symbols or addresses:
// Using symbols (ETH, USDC, USDT, WETH, etc.)
await client.sendSwap({
username: 'alice',
targetChain: 8453,
fromToken: 'ETH',
toToken: 'USDC',
amount: '100',
})
// Using addresses
await client.sendSwap({
username: 'alice',
targetChain: 8453,
fromToken: '0x0000000000000000000000000000000000000000', // ETH
toToken: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC on Base
amount: '100',
})Constraining source assets
By default, sendSwap uses the fromToken as the source. You can constrain which tokens the orchestrator uses:
const result = await client.sendSwap({
username: 'alice',
targetChain: 8453,
fromToken: 'ETH',
toToken: 'USDC',
amount: '100',
sourceAssets: ['ETH', 'WETH'], // Only use ETH or WETH as input
})Swap quote
The result includes quote information:
const result = await client.sendSwap({
username: 'alice',
targetChain: 8453,
fromToken: 'ETH',
toToken: 'USDC',
amount: '100',
})
if (result.success && result.quote) {
console.log('From:', result.quote.fromToken)
console.log('To:', result.quote.toToken)
console.log('Amount in:', result.quote.amountIn)
console.log('Amount out:', result.quote.amountOut)
console.log('Rate:', result.quote.rate)
console.log('Price impact:', result.quote.priceImpact)
}SendSwapOptions
| Property | Type | Required | Description |
|---|---|---|---|
username | string | Yes | Username of the signer |
targetChain | number | Yes | Chain ID where swap executes |
fromToken | string | Yes | Token to swap from (address or symbol like "ETH", "USDC") |
toToken | string | Yes | Token to swap to (address or symbol) |
amount | string | Yes | Amount in human-readable format (e.g., "0.1") |
slippageBps | number | No | Max slippage in basis points. Default: 50 (0.5%) |
sourceAssets | string[] | No | Override source assets for the swap |
closeOn | CloseOnStatus | No | When to close dialog. Default: "preconfirmed" |
waitForHash | boolean | No | Wait for transaction hash before resolving |
hashTimeoutMs | number | No | Max time to wait for hash in ms |
hashIntervalMs | number | No | Poll interval for hash in ms |
SendSwapResult
| Property | Type | Description |
|---|---|---|
success | boolean | Whether the swap was successful |
intentId | string | Intent ID for status tracking |
transactionHash | string | Transaction hash (if waitForHash: true) |
quote | SwapQuote | Quote information for the executed swap |
error | { code: string; message: string } | Error details if failed |
Next steps
- Token Requests - Request specific token outputs
- Batch Transactions - Queue multiple operations