Instant bridge
If your dapp or wallet requires users to hold tokens on a certain chain, you can use Glide to let users bridge tokens from one chain to another. You maintain full control over the user experience and the user never has to leave your app.
Setup
Before you start, make sure you setup the Glide client using the Installation guide.
Bridge ETH
1. Create a session
When the user is ready to bridge, create a Glide session.
import { createSession, currencies, chains } from "@paywithglide/glide-js";
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 on Optimism
paymentCurrency: currencies.eth.on(chains.optimism),
// The *destination* chain where the tokens will be bridged to
chainId: chains.base.id,
// The recipient wallet address. Generally, this is the
// same as the user's wallet address
address: "0xc6FfEB1298Eb33Da430d14e5Eb789256ec344625",
// Bridge 0.01 ETH
value: parseEther("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 { executeSession } from "@paywithglide/glide-js";
const { sponsoredTransactionHash } = await executeSession(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 executeSession
action orchestrates the process required to complete the Glide session. More specifically, it:
- Switches the user's wallet to the correct chain.
- Triggers the payment transaction on the origin chain using the user's wallet.
- Update Glide with the payment transaction hash.
- Waits for the Glide session to be completed.
- Returns a complete session object.
3. Handle errors
The executeSession
can throw errors if the user rejects the transaction or if the transaction fails. Here are Glide-specific errors that you should handle:
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.
The transaction has failed. Glide will automatically refund the user.
4. That's it!
The executeSession
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 ERC-20 tokens
You can extend the same process to bridge other tokens like USDC, DAI, etc. All you have to do is update the createSession
call.
Here's how you can bridge USDC:
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 *destination* chain where the tokens will be bridged to
chainId: chains.base.id,
// The 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",
// Bridge 1 USDC
parseUnits("1", currencies.usdc.decimals),
],
});
Cross-chain swaps
You can extend the instant bridge process to enable cross-chain swaps — where the user pays in one token on a chain and receives another token on a different chain.
Follow the guide for cross-chain swaps here.