EVM → Solana swap
Bridge USDC on Base into SOL on Solana end to end — quote, simulate, approve, send with viem, report the hash and track the order.
To swap USDC on Base into SOL on Solana with LayerHop, request a quote with srcChain=base and dstChain=solana, build the transaction for the best route, have the user's EVM wallet approve and send it, report the transaction hash, then track the order until SOL arrives. Relay, deBridge and LI.FI all serve EVM → Solana routes, and LayerHop picks the one that gives the user the most.
What do you need before you start?#
| You need | Why |
|---|---|
The TypeScript SDK (@layerhop/sdk on npm) or plain HTTP |
Every step below is one REST call; the SDK wraps them. |
| An EVM wallet on the source chain | It pays the input token and the source-chain gas. |
| The user's Solana address | It is the recipient. An EVM address here is rejected or lost. |
| Optional: an API key | Higher rate limits, webhooks and order attribution. |
The example moves 100 USDC (6 decimals, so amount: "100000000") from Base to SOL.
1. Get a quote#
import { LayerHop, type Quote } from "@layerhop/sdk";
const lh = new LayerHop({ apiKey: process.env.LAYERHOP_API_KEY }); // baseUrl defaults to https://api.layerhop.com
const res = await lh.quote({
srcChain: "base",
srcToken: "USDC",
amount: "100000000", // 100 USDC in base units
dstChain: "solana",
dstToken: "SOL",
sender: evmAddress, // the connected EVM wallet
recipient: solanaAddress, // a Solana address, not an EVM one
slippageBps: 50,
});
const quote: Quote = res.best;Show the user quote.dstAmount and quote.dstAmountMin (SOL has 9 decimals), quote.estimatedSeconds, quote.provider and the fee in quote.fees. The other routes are in res.quotes, and providers that could not quote are listed in res.errors.
Warning
recipientmust be a valid address on the destination chain. Validate it before you quote. A wrong-family address is rejected by providers or, worse, delivers to an address the user does not control.
2. Build the transaction#
const tx = await lh.buildTx(quote.quoteId);
if (tx.vm !== "evm") throw new Error("expected an EVM transaction for an EVM source");The source is EVM, so you get an EvmTx on tx.chainId (8453 for Base). Selling an ERC-20 such as USDC usually adds an approval; see Token approvals. Build only after the user confirms the price: quotes expire within about a minute.
3. Simulate (optional)#
POST /v1/simulate dry-runs the same transaction against the source chain without recording an order. Use it to catch reverts before the wallet prompt.
const sim = await lh.simulate(quote.quoteId);
if (!sim.ok) console.warn(sim.error?.code, sim.error?.message);
if (sim.needsApproval) console.log("Approval required first");See Simulate before signing for what a result means.
4. Approve and send with viem#
import { createPublicClient, createWalletClient, custom, erc20Abi, http, type Address, type Hex } from "viem";
import { base } from "viem/chains";
const wallet = createWalletClient({ chain: base, transport: custom(window.ethereum!) });
const pub = createPublicClient({ chain: base, transport: http() });
const [account] = await wallet.requestAddresses();
if ((await wallet.getChainId()) !== tx.chainId) await wallet.switchChain({ id: tx.chainId });
if (tx.approval) {
const spender = tx.approval.spender as Address;
const token = tx.approval.token as Address;
const need = BigInt(tx.approval.amount);
const have = await pub.readContract({ address: token, abi: erc20Abi, functionName: "allowance", args: [account!, spender] });
if (have < need) {
const h = await wallet.writeContract({ account: account!, address: token, abi: erc20Abi, functionName: "approve", args: [spender, need] });
await pub.waitForTransactionReceipt({ hash: h });
}
}
const srcTxHash = await wallet.sendTransaction({
account: account!,
to: tx.to as Address,
data: tx.data as Hex,
value: BigInt(tx.value), // non-zero for native-coin input or providers with a fixed native fee
});5. Report the transaction hash#
The wallet broadcast the transaction, so tell LayerHop its hash. This moves the order from built to submitted, starts server-side tracking and triggers your webhooks.
await lh.reportSubmitted(quote.quoteId, srcTxHash); // POST /v1/orders/{quoteId}/submitted, idempotent6. Track the order#
for await (const s of lh.watch(quote.provider, srcTxHash)) {
if (s.state === "filled") console.log("Delivered:", `https://solscan.io/tx/${s.dstTxHash}`);
if (s.state === "refunded") console.log("Refunded to the sender on Base");
}Cross-chain providers are tracked by the source transaction hash. Expect unknown or pending first. With an API key you can skip polling and listen for order.filled instead. See Order tracking.
What can go wrong at each step?#
| Step | Error | What to do |
|---|---|---|
| Quote | NO_ROUTE |
Retry shortly, or change the amount or token. |
| Quote | UNSUPPORTED_TOKEN |
Pass the token address instead of a symbol. |
| Build | QUOTE_EXPIRED, QUOTE_NOT_FOUND |
Re-quote, show the new price, ask the user to confirm again. |
| Approve / send | Wallet rejection | Nothing was spent. Let the user retry. |
| Report | CONFLICT |
A different hash was already reported for this quote. Do not resend. |
| Track | PROVIDER_*, RATE_LIMITED |
Keep polling with backoff. The order is on chain regardless. |
More in Error handling & retries.
Last updated