Skip to content
LogoLogo

Swap with Wallet

wallet.swap

Opens the connected Tempo wallet's swap flow, with optional pre-filled fields.

This action invokes the wallet_swap JSON-RPC method on the connected wallet, prompting the user to review and submit a swap. Any fields you omit are left for the user to fill in.

Usage

example.ts
import { ,  } from 'viem/tempo'
import 'viem/window'
 
const  = .({
  : (.!),
})
 
const {  } = await ..({
  : '1.5',
  : '0x20c0000000000000000000000000000000000001',
  : 'sell',
})
 
.('Transaction hash:', .)
Transaction hash: 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef

Recipes

Buy an Exact Invoice Amount

Set type: 'buy' when an invoice is priced in a token the customer does not hold: amount is the exact amount of token to receive, funded by selling pairToken.

example.ts
import { ,  } from 'viem/tempo'
import 'viem/window'
 
const  = .({
  : (.!),
})
 
const {  } = await ..({
  : '250',
  : '0x20c0000000000000000000000000000000000000',
  : '0x20c0000000000000000000000000000000000001',
  : 'buy',
})

Cap Slippage on a Treasury Rebalance

Pass slippage to bound how far from quote a large conversion may fill, here 0.5%.

example.ts
import { ,  } from 'viem/tempo'
import 'viem/window'
 
const  = .({
  : (.!),
})
 
const {  } = await ..({
  : '100000',
  : '0x20c0000000000000000000000000000000000000',
  : 0.005,
  : '0x20c0000000000000000000000000000000000001',
  : 'sell',
})

Return Value

type ReturnType = {
  /** Receipt of the submitted swap. */
  receipt: TransactionReceipt
}

The submitted swap receipt.

Parameters

amount

  • Type: string

Human-readable amount to pre-fill (for example, "1.5").

pairToken

  • Type: Address

Other side of the swap pair. For buys, this is the token to sell. For sells, this is the token to buy.

slippage

  • Type: number

Maximum allowed slippage as a decimal fraction (for example, 0.05).

token

  • Type: Address

Token to buy or sell. Omit to let the user choose.

type

  • Type: 'buy' | 'sell'

Whether the amount is an exact buy or sell amount.