|
| 1 | +--- |
| 2 | +"thirdweb": minor |
| 3 | +--- |
| 4 | + |
| 5 | +Adds a new `Bridge` module to the thirdweb SDK to access the Universal Bridge. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### Buy & Sell Operations |
| 10 | + |
| 11 | +The Bridge module makes it easy to buy and sell tokens across chains: |
| 12 | + |
| 13 | +- `Bridge.Buy` - For specifying the destination amount you want to receive |
| 14 | +- `Bridge.Sell` - For specifying the origin amount you want to send |
| 15 | + |
| 16 | +Each operation provides two functions: |
| 17 | +1. `quote` - Get an estimate without connecting a wallet |
| 18 | +2. `prepare` - Get a finalized quote with transaction data |
| 19 | + |
| 20 | +#### Buy Example |
| 21 | + |
| 22 | +```typescript |
| 23 | +import { Bridge, toWei, NATIVE_TOKEN_ADDRESS } from "thirdweb"; |
| 24 | + |
| 25 | +// First, get a quote to see approximately how much you'll pay |
| 26 | +const buyQuote = await Bridge.Buy.quote({ |
| 27 | + originChainId: 1, // Ethereum |
| 28 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 29 | + destinationChainId: 10, // Optimism |
| 30 | + destinationTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 31 | + buyAmountWei: toWei("0.01"), // I want to receive 0.01 ETH on Optimism |
| 32 | + client: thirdwebClient, |
| 33 | +}); |
| 34 | + |
| 35 | +console.log(`To get ${buyQuote.destinationAmount} wei on destination chain, you need to pay ${buyQuote.originAmount} wei`); |
| 36 | + |
| 37 | +// When ready to execute, prepare the transaction |
| 38 | +const preparedBuy = await Bridge.Buy.prepare({ |
| 39 | + originChainId: 1, |
| 40 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 41 | + destinationChainId: 10, |
| 42 | + destinationTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 43 | + buyAmountWei: toWei("0.01"), |
| 44 | + sender: "0x...", // Your wallet address |
| 45 | + receiver: "0x...", // Recipient address (can be the same as sender) |
| 46 | + client: thirdwebClient, |
| 47 | +}); |
| 48 | + |
| 49 | +// The prepared quote contains the transactions you need to execute |
| 50 | +console.log(`Transactions to execute: ${preparedBuy.transactions.length}`); |
| 51 | +``` |
| 52 | + |
| 53 | +#### Sell Example |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { Bridge, toWei } from "thirdweb"; |
| 57 | + |
| 58 | +// First, get a quote to see approximately how much you'll receive |
| 59 | +const sellQuote = await Bridge.Sell.quote({ |
| 60 | + originChainId: 1, // Ethereum |
| 61 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 62 | + destinationChainId: 10, // Optimism |
| 63 | + destinationTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 64 | + sellAmountWei: toWei("0.01"), // I want to sell 0.01 ETH from Ethereum |
| 65 | + client: thirdwebClient, |
| 66 | +}); |
| 67 | + |
| 68 | +console.log(`If you send ${sellQuote.originAmount} wei, you'll receive approximately ${sellQuote.destinationAmount} wei`); |
| 69 | + |
| 70 | +// When ready to execute, prepare the transaction |
| 71 | +const preparedSell = await Bridge.Sell.prepare({ |
| 72 | + originChainId: 1, |
| 73 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 74 | + destinationChainId: 10, |
| 75 | + destinationTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 76 | + sellAmountWei: toWei("0.01"), |
| 77 | + sender: "0x...", // Your wallet address |
| 78 | + receiver: "0x...", // Recipient address (can be the same as sender) |
| 79 | + client: thirdwebClient, |
| 80 | +}); |
| 81 | + |
| 82 | +// Execute the transactions in sequence |
| 83 | +for (const tx of preparedSell.transactions) { |
| 84 | + // Send the transaction using your wallet |
| 85 | + // Wait for it to be mined |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Bridge Routes |
| 90 | + |
| 91 | +You can discover available bridge routes using the `routes` function: |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { Bridge, NATIVE_TOKEN_ADDRESS } from "thirdweb"; |
| 95 | + |
| 96 | +// Get all available routes |
| 97 | +const allRoutes = await Bridge.routes({ |
| 98 | + client: thirdwebClient, |
| 99 | +}); |
| 100 | + |
| 101 | +// Filter routes for a specific token or chain |
| 102 | +const filteredRoutes = await Bridge.routes({ |
| 103 | + originChainId: 1, // From Ethereum |
| 104 | + originTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 105 | + destinationChainId: 10, // To Optimism |
| 106 | + client: thirdwebClient, |
| 107 | +}); |
| 108 | + |
| 109 | +// Paginate through routes |
| 110 | +const paginatedRoutes = await Bridge.routes({ |
| 111 | + limit: 10, |
| 112 | + offset: 0, |
| 113 | + client: thirdwebClient, |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +### Bridge Transaction Status |
| 118 | + |
| 119 | +After executing bridge transactions, you can check their status: |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { Bridge } from "thirdweb"; |
| 123 | + |
| 124 | +// Check the status of a bridge transaction |
| 125 | +const bridgeStatus = await Bridge.status({ |
| 126 | + transactionHash: "0xe199ef82a0b6215221536e18ec512813c1aa10b4f5ed0d4dfdfcd703578da56d", |
| 127 | + chainId: 8453, // The chain ID where the transaction was initiated |
| 128 | + client: thirdwebClient, |
| 129 | +}); |
| 130 | + |
| 131 | +// The status will be one of: "COMPLETED", "PENDING", "FAILED", or "NOT_FOUND" |
| 132 | +if (bridgeStatus.status === "completed") { |
| 133 | + console.log(` |
| 134 | + Bridge completed! |
| 135 | + Sent: ${bridgeStatus.originAmount} wei on chain ${bridgeStatus.originChainId} |
| 136 | + Received: ${bridgeStatus.destinationAmount} wei on chain ${bridgeStatus.destinationChainId} |
| 137 | + `); |
| 138 | +} else if (bridgeStatus.status === "pending") { |
| 139 | + console.log("Bridge transaction is still pending..."); |
| 140 | +} else { |
| 141 | + console.log("Bridge transaction failed"); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Error Handling |
| 146 | + |
| 147 | +The Bridge module provides consistent error handling with descriptive error messages: |
| 148 | + |
| 149 | +```typescript |
| 150 | +try { |
| 151 | + await Bridge.Buy.quote({ |
| 152 | + // ...params |
| 153 | + }); |
| 154 | +} catch (error) { |
| 155 | + // Errors will have the format: "ErrorCode | Error message details" |
| 156 | + console.error(error.message); // e.g. "AmountTooHigh | The provided amount is too high for the requested route." |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## Types |
| 161 | + |
| 162 | +The Bridge module exports the following TypeScript types: |
| 163 | + |
| 164 | +- `Route` - Describes a bridge route between chains and tokens |
| 165 | +- `Status` - Represents the status of a bridge transaction |
| 166 | +- `Quote` - Contains quote information for a bridge transaction |
| 167 | +- `PreparedQuote` - Extends Quote with transaction data |
| 168 | + |
| 169 | +## Integration |
| 170 | + |
| 171 | +The Bridge module is accessible as a top-level export: |
| 172 | + |
| 173 | +```typescript |
| 174 | +import { Bridge } from "thirdweb"; |
| 175 | +``` |
| 176 | + |
| 177 | +Use `Bridge.Buy`, `Bridge.Sell`, `Bridge.routes`, and `Bridge.status` to access the corresponding functionality. |
0 commit comments