|
| 1 | +import { |
| 2 | + createMetadata, |
| 3 | + Callout, |
| 4 | + DocImage, |
| 5 | + InstallTabs, |
| 6 | + Steps, |
| 7 | + Step, |
| 8 | +} from "@doc"; |
| 9 | +import OnrampStepOne from "../../assets/avax-to-usd.png"; |
| 10 | + |
| 11 | +export const metadata = createMetadata({ |
| 12 | + image: { |
| 13 | + title: "thirdweb Universal Bridge - Cross-Chain Swapping", |
| 14 | + icon: "thirdweb", |
| 15 | + }, |
| 16 | + title: "thirdweb Universal Bridge - Cross-Chain Swapping | thirdweb", |
| 17 | + description: |
| 18 | + "Learn how to build a custom cross-chain swapping experience with thirdweb Universal Bridge.", |
| 19 | +}); |
| 20 | + |
| 21 | +# Leverage Cross-Chain Swaps with Universal Bridge |
| 22 | + |
| 23 | +Learn how to enable your users to swap from any asset to any other with thirdweb's Universal Bridge. |
| 24 | + |
| 25 | +In this guide, we'll show you how to purchase 10 USDC on Optimism in Typescript. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +<Steps> |
| 30 | +<Step title='Install the Connect SDK'> |
| 31 | +<InstallTabs |
| 32 | + npm="npm i thirdweb" |
| 33 | + yarn="yarn add thirdweb" |
| 34 | + pnpm="pnpm i thirdweb" |
| 35 | +/> |
| 36 | +<Step title='Get Your Client ID'> |
| 37 | + |
| 38 | +Log in to the [thirdweb dashboard](https://thirdweb.com/team). Click on Create New > Project to get your **Client ID**. You'll need your Client ID to interact with the Connect SDK. |
| 39 | + |
| 40 | +</Step> |
| 41 | +</Step> |
| 42 | +<Step title='Find available routes'> |
| 43 | +Before your user can select which tokens they'd like to swap, they'll need to find available routes. |
| 44 | + |
| 45 | +You can do this using the `routes` function in our `Bridge` namespace. You or your users can filter by origin and destination chains and/or tokens. Pagination is also built-in to the function. |
| 46 | + |
| 47 | +```tsx |
| 48 | +import { Bridge, NATIVE_TOKEN_ADDRESS } from "thirdweb"; |
| 49 | + |
| 50 | +// Get all available routes |
| 51 | +const allRoutes = await Bridge.routes({ |
| 52 | + client: thirdwebClient, |
| 53 | +}); |
| 54 | + |
| 55 | +// Filter routes for a specific token or chain |
| 56 | +const filteredRoutes = await Bridge.routes({ |
| 57 | + originChainId: 1, // From Ethereum |
| 58 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 59 | + destinationChainId: 10, // To Optimism |
| 60 | + client: thirdwebClient, |
| 61 | +}); |
| 62 | + |
| 63 | +// Paginate through routes |
| 64 | +const paginatedRoutes = await Bridge.routes({ |
| 65 | + limit: 10, |
| 66 | + offset: 0, |
| 67 | + client: thirdwebClient, |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +This will return an array of `Route` objects, which will include information such as `symbol`, `address`, and `chainId` for both the origin and destination tokens. |
| 72 | + |
| 73 | +</Step> |
| 74 | +<Step title='Get a quote'> |
| 75 | +Once you know which routes are available, you can retrieve a quote to show the user how much they can expect to pay for a given swap. |
| 76 | + |
| 77 | +In this example, we'll use the `Buy.quote` function to get a quote for buying 10 USDC on Optimism for Base ETH. |
| 78 | + |
| 79 | +<Callout variant="info"> |
| 80 | + The `Buy` namespace is purpose-built for when you want to obtain a specific amount of the output token. |
| 81 | + If you have a specific input amount and are flexible on the output amount, you can use the `Sell` namespace. |
| 82 | + Learn more about sells [here](https://portal.thirdweb.com/references/typescript/v5/sell/prepare). |
| 83 | +</Callout> |
| 84 | + |
| 85 | +Quote allows us to get an expected amount before the user has connected their wallet. This quote won't come with executable transactions, and won't be a guaranteed price. |
| 86 | + |
| 87 | + |
| 88 | +```tsx |
| 89 | +import { Bridge, NATIVE_TOKEN_ADDRESS } from "thirdweb"; |
| 90 | + |
| 91 | +const buyQuote = await Bridge.Buy.quote({ |
| 92 | + originChainId: 8453, // Base |
| 93 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 94 | + destinationChainId: 10, // Optimism |
| 95 | + destinationTokenAddress: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85", // Optimism USDC |
| 96 | + buyAmountWei: 10000000n, // 10 USDC |
| 97 | + client: thirdwebClient, |
| 98 | +}); |
| 99 | + |
| 100 | +console.log( |
| 101 | + `To get ${buyQuote.destinationAmount} wei on destination chain, you need to pay ${buyQuote.originAmount} wei`, |
| 102 | +); |
| 103 | +``` |
| 104 | + |
| 105 | +This will return a `Quote` object, which will include the `originAmount` and `destinationAmount` in wei, along with some more useful information about the predicted quote. |
| 106 | +</Step> |
| 107 | +<Step title='Get the prepared transaction'> |
| 108 | +Now that we know how much the user can expect to pay, we can have them connect their wallet and execute the swap. |
| 109 | + |
| 110 | +To get a prepared quote, we'll use the `Buy.prepare` function. The key difference with this function is it requires a `sender` and `receiver` to be specified. |
| 111 | + |
| 112 | +```tsx |
| 113 | +import { Bridge, NATIVE_TOKEN_ADDRESS } from "thirdweb"; |
| 114 | + |
| 115 | +const preparedBuy = await Bridge.Buy.prepare({ |
| 116 | + originChainId: 8453, // Base |
| 117 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 118 | + destinationChainId: 10, // Optimism |
| 119 | + destinationTokenAddress: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85", // Optimism USDC |
| 120 | + buyAmountWei: 10000000n, // 10 USDC |
| 121 | + sender: "0x...", // Your user's wallet address |
| 122 | + receiver: "0x...", // Recipient address (can be the same as sender) |
| 123 | + client: thirdwebClient, |
| 124 | +}); |
| 125 | + |
| 126 | +// The prepared quote contains the transactions you need to execute |
| 127 | +console.log(`Transactions to execute: ${preparedBuy.transactions.length}`); |
| 128 | +``` |
| 129 | + |
| 130 | +This will return a `PreparedQuote` object. It will look very similar to the `Quote` you received in the previous step, but it will include a `transactions` array. This array will contain the transactions you need to execute to complete the swap. |
| 131 | +</Step> |
| 132 | +<Step title='Execute the swap'> |
| 133 | +To execute the swap, we'll need to send all transactions in the `transactions` array one after the other. |
| 134 | + |
| 135 | +<Callout variant="warning"> |
| 136 | + Currently, the `transactions` array does not include approvals. You'll need to execute any necessary approvals prior to executing the swap transactions. |
| 137 | +</Callout> |
| 138 | + |
| 139 | +```tsx |
| 140 | +import { sendTransaction, waitForReceipt } from "thirdweb"; |
| 141 | + |
| 142 | +const preparedBuy = await Bridge.Buy.prepare({ |
| 143 | + originChainId: 8453, // Base |
| 144 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 145 | + destinationChainId: 10, // Optimism |
| 146 | + destinationTokenAddress: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85", // Optimism USDC |
| 147 | + buyAmountWei: 10000000n, // 10 USDC |
| 148 | + sender: "0x...", // Your user's wallet address |
| 149 | + receiver: "0x...", // Recipient address (can be the same as sender) |
| 150 | + client: thirdwebClient, |
| 151 | +}); |
| 152 | + |
| 153 | +}; |
| 154 | + |
| 155 | +for (const transaction of preparedBuy.transactions) { |
| 156 | + const tx = prepareTransaction({ |
| 157 | + to: transaction.to as string, |
| 158 | + value: BigInt(transaction.value ?? 0n), |
| 159 | + data: transaction.data, |
| 160 | + chain: defineChain(transaction.chainId), |
| 161 | + client |
| 162 | + }); |
| 163 | + |
| 164 | + const result = await sendAndConfirmTransaction({ transaction: tx, account }); |
| 165 | + let swapStatus; |
| 166 | + do { |
| 167 | + swapStatus = await Bridge.status({ |
| 168 | + transactionHash: result.transactionHash, |
| 169 | + client, |
| 170 | + }); |
| 171 | + } while (swapStatus.status !== "COMPLETED"); |
| 172 | +}; |
| 173 | +``` |
| 174 | + |
| 175 | +<Callout variant="info"> |
| 176 | + The returned transactions follow the [Ox](https://oxlib.sh/) standard [TransactionEnvelopeEip1559](https://oxlib.sh/guides/transaction-envelopes) |
| 177 | + format. This is a simple transaction with `data`, `to`, `value`, and `chainId` properties. **Gas is not included in the transaction, and should |
| 178 | + be estimated separately if necessary.** Normally, the thirdweb SDK will handle this for you. |
| 179 | +</Callout> |
| 180 | + |
| 181 | +</Step> |
| 182 | +<Step title='Getting the swap status'> |
| 183 | +You'll notice in the previous step we call `Bridge.status` to get the status of the swap. We can get the status of any past swap using just the transaction hash and chain ID of its origin transaction. |
| 184 | + |
| 185 | +When sending the transactions in a prepared quote, you **must** use `Bridge.status` to get the `COMPLETED` status before moving on to the next transaction. This is because `status` waits for both the origin |
| 186 | +and destination transactions to complete. Waiting for the origin transaction receipt is not sufficient since the funds might not have arrived on the destination chain yet. |
| 187 | + |
| 188 | +The `status` will also return all transactions (origin and destination) involved in the swap. |
| 189 | +```tsx |
| 190 | +import { Bridge } from "thirdweb"; |
| 191 | + |
| 192 | +// Check the status of a bridge transaction |
| 193 | +const bridgeStatus = await Bridge.status({ |
| 194 | + transactionHash: |
| 195 | + "0xe199ef82a0b6215221536e18ec512813c1aa10b4f5ed0d4dfdfcd703578da56d", |
| 196 | + chainId: 8453, // The chain ID where the transaction was initiated |
| 197 | + client: thirdwebClient, |
| 198 | +}); |
| 199 | + |
| 200 | +// The status will be one of: "COMPLETED", "PENDING", "FAILED", or "NOT_FOUND" |
| 201 | +if (bridgeStatus.status === "completed") { |
| 202 | + console.log(` |
| 203 | + Bridge completed! |
| 204 | + Sent: ${bridgeStatus.originAmount} wei on chain ${bridgeStatus.originChainId} |
| 205 | + Received: ${bridgeStatus.destinationAmount} wei on chain ${bridgeStatus.destinationChainId} |
| 206 | + `); |
| 207 | +} else if (bridgeStatus.status === "pending") { |
| 208 | + console.log("Bridge transaction is still pending..."); |
| 209 | +} else { |
| 210 | + console.log("Bridge transaction failed"); |
| 211 | +} |
| 212 | +``` |
| 213 | +</Step> |
| 214 | +</Steps> |
0 commit comments