Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Swap – 1auth
Skip to content

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/1auth

Create 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

PropertyTypeRequiredDescription
usernamestringYesUsername of the signer
targetChainnumberYesChain ID where swap executes
fromTokenstringYesToken to swap from (address or symbol like "ETH", "USDC")
toTokenstringYesToken to swap to (address or symbol)
amountstringYesAmount in human-readable format (e.g., "0.1")
slippageBpsnumberNoMax slippage in basis points. Default: 50 (0.5%)
sourceAssetsstring[]NoOverride source assets for the swap
closeOnCloseOnStatusNoWhen to close dialog. Default: "preconfirmed"
waitForHashbooleanNoWait for transaction hash before resolving
hashTimeoutMsnumberNoMax time to wait for hash in ms
hashIntervalMsnumberNoPoll interval for hash in ms

SendSwapResult

PropertyTypeDescription
successbooleanWhether the swap was successful
intentIdstringIntent ID for status tracking
transactionHashstringTransaction hash (if waitForHash: true)
quoteSwapQuoteQuote information for the executed swap
error{ code: string; message: string }Error details if failed

Next steps