Skip to content

Cross-chain swaps

Swap tokens from one chain to another

If your dapp or wallet requires users to hold a specific token, you can use Glide to let users deposit that token using any other token they hold, regardless of the chain. You maintain full control over the user experience and the user never has to leave your app.

Setup

This guide is an extension to the Instant bridge guide. Make sure you are able to bridge tokens from one chain to another before enabling cross-chain swaps.

Create session

A cross-chain swap is just like any other Glide transaction. However, in this case, the user is making a "transfer" transaction on the destination chain. This transfer transaction is for the token they want to receive. The user pays for this transaction using the token they want to swap on the origin chain.

Below are the sample calls to the createSession 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 { createSession, currencies, chains } from "@paywithglide/glide-js";
import { erc20Abi } from "viem";
 
const session = await createSession(glideConfig, {
  // The user's wallet address
  account: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
 
  // The *origin* chain where the tokens will be bridged from
  // In this case, the user will bridge ETH from Optimism
  paymentCurrency: currencies.eth.on(chains.optimism),
 
  // The amount of ETH to bridge
  paymentAmount: 0.01,
 
  // The *destination* chain where the tokens will be bridged to
  chainId: chains.base.id,
 
  // The ERC-20 token contract address on the destination chain
  address: currencies.usdc.contractAddressOn(chains.base),
 
  // Bridge transaction details
  abi: erc20Abi,
  functionName: "transfer",
  args: [
    // The recipient wallet address. Generally, this is the
    // same as the user's wallet address
    "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
 
    // Enter a nominal amount of USDC to transfer.
    // This is just a placeholder and will be updated depending
    // on the exchange rate between ETH and USDC.
    parseUnits("1", currencies.usdc.decimals),
  ],
});

Swap ERC-20 token for ETH

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

import { createSession, currencies, chains } from "@paywithglide/glide-js";
import { erc20Abi } from "viem";
 
const session = await createSession(glideConfig, {
  // The user's wallet address
  account: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
 
  // The *origin* chain where the tokens will be bridged from
  // In this case, the user will bridge USDC from Base
  paymentCurrency: currencies.usdc.on(chains.base),
 
  // The amount of USDC to bridge
  paymentAmount: 1,
 
  // The *destination* chain where the tokens will be bridged to
  chainId: chains.optimism.id,
 
  // The recipient wallet address. Generally, this is the
  // same as the user's wallet address
  address: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
});

Swap ERC-20 tokens

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

import { createSession, currencies, chains } from "@paywithglide/glide-js";
import { erc20Abi } from "viem";
 
const session = await createSession(glideConfig, {
  // The user's wallet address
  account: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
 
  // The *origin* chain where the tokens will be bridged from
  // In this case, the user will bridge USDC from Optimism
  paymentCurrency: currencies.usdc.on(chains.optimism),
 
  // The amount of USDC to bridge
  paymentAmount: 1,
 
  // The *destination* chain where the tokens will be bridged to
  chainId: chains.base.id,
 
  // The ERC-20 token contract address on the destination chain
  address: currencies.moxie.contractAddressOn(chains.base),
 
  // Bridge transaction details
  abi: erc20Abi,
  functionName: "transfer",
  args: [
    // The recipient wallet address. Generally, this is the
    // same as the user's wallet address
    "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
 
    // Enter a nominal amount of MOXIE to transfer.
    // This is just a placeholder and will be updated depending
    // on the exchange rate between USDC and MOXIE.
    parseUnits("1000", currencies.moxie.decimals),
  ],
});

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 provide the amount that the user will pay on the origin chain. The returned session object contains the amount that the user will receive on the destination chain.

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.