TransactionsSwap tokens

Same-chain swaps (Alpha)

Swaps let you convert any token to any other token onchain. They’re built natively into Smart Wallets and you can integrate in minutes.

Smart Wallets also allow you to add actions after the swap completes. For example, you can deposit your newly swapped tokens into a DeFi protocol.

Swaps work just like any other Smart Wallet transaction, so you can sponsor gas to do gasless swaps, or pay for gas in an ERC-20 token.

Cross-chain swaps are live! Send your first cross-chain swap now!

Swaps are in alpha. Note that there may be changes in the future to simplify the endpoint/sdk. We will let you know if/when that happens.

The Swap flow

Flow

  1. Request a swap quote
  2. Sign the prepared swap calls (including any post swap action)
  3. Send prepared calls
  4. Wait for onchain confirmation

Swap options

When requesting a swap quote, you can specify either an amount in, or a minimum amount out.

1// Mode 1: Swap exact input amount
2{
3 fromAmount: "0x2710";
4} // Swap exactly 0.01 USDC (10000 in hex, 6 decimals)
5
6// Mode 2: Get minimum output amount
7{
8 minimumToAmount: "0x5AF3107A4000";
9} // Get at least 0.0001 ETH (18 decimals). We calculate how much USDC you need to spend to get at least your desired ETH amount.

Prerequisites

Before you begin, ensure you have:

  • An Alchemy API Key
  • If you’re sponsoring gas, then a Gas Manager policy
  • A small amount of USDC for testing (~$1 worth is enough!)
    • Important: You’ll need to send these tokens to your smart wallet address to be able to swap!
  • A signer to own the account and sign messages
Required SDK version: ^v4.65.0

You’ll need the following env variables:

1import { swapActions } from "@account-kit/wallet-client/experimental";
2import { client } from "./client";
3
4const account = await client.requestAccount();
5
6// Add the swap actions to the client
7const swapClient = client.extend(swapActions);
8
9// Request the swap quote
10const { quote, ...calls } = await swapClient.requestQuoteV0({
11 from: account.address,
12 fromToken: "0x...",
13 toToken: "0x...",
14 minimumToAmount: "0x...",
15});
16
17// Display the swap quote, including the minimum amount to receive and the expiry
18console.log(quote);
19
20// Assert that the calls are not raw calls.
21// This will always be the case when requestQuoteV0 is used without the `returnRawCalls` option,
22// the assertion is just needed for Typescript to recognize the result type.
23if (calls.rawCalls) {
24 throw new Error("Expected user operation calls");
25}
26
27// Sign the quote, getting back prepared and signed calls
28const signedCalls = await swapClient.signPreparedCalls(calls);
29
30// Send the prepared calls
31const { preparedCallIds } = await swapClient.sendPreparedCalls(signedCalls);
32
33// Wait for the call to resolve
34const callStatusResult = await swapClient.waitForCallsStatus({
35 id: preparedCallIds[0]!,
36});
37
38// Filter through success or failure cases
39if (
40 callStatusResult.status !== "success" ||
41 !callStatusResult.receipts ||
42 !callStatusResult.receipts[0]
43) {
44 throw new Error(
45 `Transaction failed with status ${callStatusResult.status}, full receipt:\n ${JSON.stringify(callStatusResult, null, 2)}`,
46 );
47}
48
49console.log("Swap confirmed!");
50console.log(
51 `Transaction hash: ${callStatusResult.receipts[0].transactionHash}`,
52);

FAQs

What chains are supported?

Chains supported (for now) are: Ethereum, Arbitrum, Base, Berachain, BSC/BNB, Ink, Monad, Optimism, Polygon, Unichain and World Chain mainnets.

Does the Swap API support cross-chain swaps?

Currently, the Swap API supports only single-chain swaps. Cross-chain swaps are coming soon!

How do you encode values?

Values are simply passed as hexadecimal strings. The Swap API doesn’t add complexity to consider decimals, so 0x01 is always the smallest amount of a given asset. 1 ETH, or DAI (18 decimals) is 0xDE0B6B3A7640000 1 USDC (6 decimals) is 0xF4240 This removes any ambiguity— if it’s numerical, it’s a hex.

What is the expiry?

The expiry is an informational indicator of when you can expect to be able to process the swap request. If you’re at/near the expiry, it might be a good time to request a new quote.