Cross Chain Swaps from Solana

View as Markdown

The 0x Cross-Chain API is in private beta. Request access to start building.

Quick link with examples: 0x-examples

Steps to Cross Chain Swap Tokens from Solana

This guide will walk you through using the /quotes and /status endpoints on 0x’s Cross Chain API to:

  1. Fetch a quote
  2. Sign and send a transaction
  3. Monitor the status of the cross chain swap

In our example, we will be swapping WSOL on Solana to USDC on Base. For understanding how to deal with the native SOL, please refer to Native Tokens Handling.

0. Prerequisites

Make sure you have:

  • A funded Solana wallet
  • 0x API key
  • An RPC Connection (see details below)

Solana provides a default public RPC endpoint, but for production use, it’s strongly recommended to run your own or use a third-party provider like Helius.

1import { Connection } from "@solana/web3.js";
2
3// You can replace this with your own or a third-party RPC URL
4const connection = new Connection("https://api.mainnet-beta.solana.com");

1. Fetch a Quote

Start by sending a GET request to the 0x /quotes endpoint to get a quotes for a specific tokens and chains pair with selected amount. You may use 'solana' or '999999999991' as the originChain.

1const quotesParams = new URLSearchParams({
2 originChain: 'solana', // Solana mainnet
3 destinationChain: '8453', // Base mainnet
4 sellToken: 'So11111111111111111111111111111111111111112', // WSOL on Solana
5 buyToken: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', // USDC on Base
6 sellAmount: '10000000000000000000', // Amount of sellToken in base units
7 originAddress: '$USER_TAKER_ADDRESS', // Solana Pubkey that will make the trade
8 destinationAddress: '$USER_RECEIVER_ADDRESS', // Base address that will receive the output
9 sortQuotesBy: 'price', // Prefer the quote that will result in the best price / output
10 maxNumQuotes: 1 // only the best quote
11});
12
13const headers = {
14 '0x-api-key': '[api-key]', // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
15};
16
17const quoteResponse = await fetch('https://api.0x.org/cross-chain/quotes?' + quotesParams.toString(), { headers });
18
19console.log(await quoteResponse.json());
1{
2 "liquidityAvailable": true,
3 "originChainId": 999999999991,
4 "originChain": "solana",
5 "destinationChainId": 8453,
6 "destinationChain": "base",
7 "sellToken": "So11111111111111111111111111111111111111112",
8 "buyToken": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
9 "issues": {
10 "allowance": null,
11 "balance": null,
12 "simulationIncomplete": false,
13 "invalidSwapSourcesPassed": [],
14 "invalidBridgesPassed": []
15 },
16 "zid": "0xbacab00bea6e849844e1be8b",
17 "routes": [
18 {
19 "sellAmount": "10000000000",
20 "buyAmount": "1946182873",
21 "minBuyAmount": "1926913735",
22 "fees": {
23 "integratorFee": null,
24 "zeroExFee": null,
25 "bridgeNativeFee": null
26 },
27 "gasCosts": {
28 "chainType": "svm",
29 "base": "5000",
30 "priority": "0",
31 "total": "5000"
32 },
33 "steps": [
34 {
35 "type": "bridge",
36 "originChainId": 999999999991,
37 "destinationChainId": 8453,
38 "sellToken": "So11111111111111111111111111111111111111112",
39 "buyToken": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
40 "sellAmount": "10000000000",
41 "buyAmount": "1946182873",
42 "minBuyAmount": "1926913735",
43 "provider": "relay",
44 "estimatedTimeSeconds": 3
45 }
46 ],
47 "transaction": {
48 "chainType": "svm",
49 "details": {
50 "serializedTransaction": "AQAA...JeZQ=="
51 }
52 },
53 "estimatedTimeSeconds": 3,
54 "issues": {
55 "allowance": null,
56 "balance": null,
57 "simulationIncomplete": false
58 }
59 }
60 ]
61}

2. Sign and submit transaction

In the next step, you need to build VersionedTransaction based on returned transaction data, sign it with your wallet, and send it to the Solana network, and wait for transaction confirmation.

1const serializedTx = quotesResponse.quotes[0].transaction.details.serializedTransaction;
2const transactionBuffer = Buffer.from(serializedTx, "base64");
3const transaction = VersionedTransaction.deserialize(transactionBuffer);
4
5transaction.sign([keypair]);
6
7const signature = await connection.sendTransaction(transaction, {
8 skipPreflight: false, // Let Solana do final preflight
9 preflightCommitment: "confirmed",
10});
11
12const confirmation = await connection.confirmTransaction(
13 {
14 signature,
15 ...(await connection.getLatestBlockhash()),
16 },
17 "finalized",
18);

3. Monitor the cross chain execution

The last step is to monitor the execution of the cross chain transaction, including the fill on the destination chain. For that, we will use the /status endpoint.

1const statusParams = new URLSearchParams({
2 originChain: "solana", // origin chain
3 originTxHash: signature, // transaction hash of the origin chain transaction, submitted in previous step
4});
5
6const headers = {
7 '0x-api-key': '[api-key]', // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
8};
9
10const statusResponse = await fetch('https://api.0x.org/cross-chain/status?' + statusParams.toString(), { headers });
11
12console.log(await statusResponse.json());

In your application, you might need to monitor the status repeatedly, as bridging operation might take several seconds or minutes to complete.

1{
2 "status": "bridge_filled",
3 "bridge": "relay",
4 "transactions": [
5 {
6 "chainId": 999999999991,
7 "chain": "solana",
8 "txHash": "5UnA6U8LBcV7ZrBcq9ZVJrAmp86f9m5yQyRtBF3nFYYFmQAjknBNiYGvj41CAUf9ZqAUfqYVME571KskA7ERX2ep",
9 "timestamp": 1755200803
10 },
11 {
12 "chainId": 8453,
13 "chain": "base",
14 "txHash": "0xbc52c14a42fe35cdf6f2eef0676bfa9af51870a72f19e9f510ea1e31619a6b7d",
15 "timestamp": 1755200808
16 }
17 ],
18 "zid": "0x9562c9b7dd462114505f5dcd"
19}

Extra - using alternative gas payer

One thing that is specific to cross chain swaps originating from Solana is the ability to specify an alternative wallet as a gas payer. When requesting a /quote, you need to pass an extra gasPayer parameter, equal to base58-encoded pubkey of the gas payer. Then, when processing the response, you need to additionally sign the transaction with the gas payer wallet.

1const serializedTx = quote.transaction.details.serializedTransaction;
2const transactionBuffer = Buffer.from(serializedTx, "base64");
3const transaction = VersionedTransaction.deserialize(transactionBuffer);
4// Sign transaction with both keypairs
5console.log("Signing transaction with gas payer and user keypairs...");
6
7transaction.sign([gasPayerKeypair, keypair]);

In the examples repo, you can find a dedicated end-to-end example utilising a gas payer functionality.