Skip to content

Cross-chain swaps

Swap tokens from one chain to another

A cross-chain swap let's users swap their tokens for another tokens across different chains. For example, a user can swap their ETH on Base for SOL on Solana.

Setup

Before you start, make sure you setup the Glide client using the Installation guide.

1. Create session

Below are the sample calls to the createSwapSession action for different scenarios.

Swap ETH for ERC-20 token

In this scenario, we create a session to swap 0.01 ETH on Optimism for USDC on Base.

import { createSwapSession } from "@paywithglide/glide-js";
import { eth, usdc } from "@paywithglide/glide-js/currencies";
import { base, optimism } from "@paywithglide/glide-js/chains";
 
const session = await createSwapSession(glideConfig, {
  account: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
  paymentMethod: "wallet",
 
  sellChain: optimism,
  sellCurrency: eth,
  sellAmount: "0.01",
 
  buyChain: base,
  buyCurrency: usdc,
});

Swap ERC-20 token for ETH

In this scenario, we create a session to swap USDC on Base for 0.01 ETH on Optimism.

import { createSwapSession } from "@paywithglide/glide-js";
import { eth, usdc } from "@paywithglide/glide-js/currencies";
import { base, optimism } from "@paywithglide/glide-js/chains";
 
const session = await createSwapSession(glideConfig, {
  account: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
  paymentMethod: "wallet",
 
  sellChain: base,
  sellCurrency: usdc,
 
  buyChain: optimism,
  buyCurrency: eth,
  buyAmount: "0.01",
});

2. Execute the session

Once the user has reviewed the transaction details and is ready to pay on the origin chain, execute the session to complete the transaction.

import { executeEVMSession } from "@paywithglide/glide-js";
 
const { sponsoredTransactionHash } = await executeEVMSession(glideConfig, {
  // The session object returned in the previous step
  session,
 
  // The user's *current* chain.
  currentChainId: 1,
 
  switchChainAsync: async (chainId) => {
    // switch current chain to chainId on the user's wallet
  },
  sendTransactionAsync: async (tx) => {
    // send tx to the chain using the user's wallet
    // // return the transaction hash
  },
});

The executeEVMSession action orchestrates the process required to complete the Glide session. More specifically, it:

  1. Switches the user's wallet to the correct chain.
  2. Triggers the payment transaction on the origin chain using the user's wallet.
  3. Update Glide with the payment transaction hash.
  4. Waits for the Glide session to be completed.
  5. Returns a complete session object.

3. Handle errors

The executeEVMSession can throw errors if the user rejects the transaction or if the transaction fails. Here are Glide-specific errors that you should handle:

SessionExpiredError*
Error

The session has expired. You should create a new session and retry the transaction. If the user has already paid, Glide will automatically refund the user.

SponsoredTransactionFailedError*
Error

The transaction has failed. Glide will automatically refund the user.

4. That's it!

The executeEVMSession returns a completed session object. This session contains the sponsoredTransactionHash which is the hash of the transaction on the destination chain where the tokens are deposited in the user's wallet.

Bridge

If you want to send the same token from one chain to another, follow the Instant bridge guide.

Exchange rate

When creating a session for a cross-chain swap, you can either provide the "buy" amount or the "sell" amount. The returned session object contains details on the exchange rate for the session.

sponsoredTransactionAmount*
number

The amount of tokens the user will receive on the destination chain. This amount is calculated based on the exchange rate between the payment currency and the destination currency.

sponsoredTransactionAmountUSD*
number

The equivalent amount in USD of the tokens the user will receive on the destination chain.

See the full list of fields for the session object here, which includes the exchange rate fields and metadata fields useful for displaying the transaction to the user.