-
Notifications
You must be signed in to change notification settings - Fork 548
[SDK] Feature: Adds Universal Bridge #6464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Adds a new `Bridge` module to the thirdweb SDK to access the Universal Bridge. | ||
|
||
## Features | ||
|
||
### Buy & Sell Operations | ||
|
||
The Bridge module makes it easy to buy and sell tokens across chains: | ||
|
||
- `Bridge.Buy` - For specifying the destination amount you want to receive | ||
- `Bridge.Sell` - For specifying the origin amount you want to send | ||
|
||
Each operation provides two functions: | ||
1. `quote` - Get an estimate without connecting a wallet | ||
2. `prepare` - Get a finalized quote with transaction data | ||
|
||
#### Buy Example | ||
|
||
```typescript | ||
import { Bridge, toWei, NATIVE_TOKEN_ADDRESS } from "thirdweb"; | ||
|
||
// First, get a quote to see approximately how much you'll pay | ||
const buyQuote = await Bridge.Buy.quote({ | ||
originChainId: 1, // Ethereum | ||
originTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
destinationChainId: 10, // Optimism | ||
destinationTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
buyAmountWei: toWei("0.01"), // I want to receive 0.01 ETH on Optimism | ||
client: thirdwebClient, | ||
}); | ||
|
||
console.log(`To get ${buyQuote.destinationAmount} wei on destination chain, you need to pay ${buyQuote.originAmount} wei`); | ||
|
||
// When ready to execute, prepare the transaction | ||
const preparedBuy = await Bridge.Buy.prepare({ | ||
originChainId: 1, | ||
originTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
destinationChainId: 10, | ||
destinationTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
buyAmountWei: toWei("0.01"), | ||
sender: "0x...", // Your wallet address | ||
receiver: "0x...", // Recipient address (can be the same as sender) | ||
client: thirdwebClient, | ||
}); | ||
|
||
// The prepared quote contains the transactions you need to execute | ||
console.log(`Transactions to execute: ${preparedBuy.transactions.length}`); | ||
``` | ||
|
||
#### Sell Example | ||
|
||
```typescript | ||
import { Bridge, toWei } from "thirdweb"; | ||
|
||
// First, get a quote to see approximately how much you'll receive | ||
const sellQuote = await Bridge.Sell.quote({ | ||
originChainId: 1, // Ethereum | ||
originTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
destinationChainId: 10, // Optimism | ||
destinationTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
sellAmountWei: toWei("0.01"), // I want to sell 0.01 ETH from Ethereum | ||
client: thirdwebClient, | ||
}); | ||
|
||
console.log(`If you send ${sellQuote.originAmount} wei, you'll receive approximately ${sellQuote.destinationAmount} wei`); | ||
|
||
// When ready to execute, prepare the transaction | ||
const preparedSell = await Bridge.Sell.prepare({ | ||
originChainId: 1, | ||
originTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
destinationChainId: 10, | ||
destinationTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
sellAmountWei: toWei("0.01"), | ||
sender: "0x...", // Your wallet address | ||
receiver: "0x...", // Recipient address (can be the same as sender) | ||
client: thirdwebClient, | ||
}); | ||
|
||
// Execute the transactions in sequence | ||
for (const tx of preparedSell.transactions) { | ||
// Send the transaction using your wallet | ||
// Wait for it to be mined | ||
} | ||
``` | ||
|
||
### Bridge Routes | ||
|
||
You can discover available bridge routes using the `routes` function: | ||
|
||
```typescript | ||
import { Bridge, NATIVE_TOKEN_ADDRESS } from "thirdweb"; | ||
|
||
// Get all available routes | ||
const allRoutes = await Bridge.routes({ | ||
joaquim-verges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client: thirdwebClient, | ||
}); | ||
|
||
// Filter routes for a specific token or chain | ||
const filteredRoutes = await Bridge.routes({ | ||
originChainId: 1, // From Ethereum | ||
originTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
destinationChainId: 10, // To Optimism | ||
client: thirdwebClient, | ||
}); | ||
|
||
// Paginate through routes | ||
const paginatedRoutes = await Bridge.routes({ | ||
limit: 10, | ||
offset: 0, | ||
client: thirdwebClient, | ||
}); | ||
``` | ||
|
||
### Bridge Transaction Status | ||
|
||
After executing bridge transactions, you can check their status: | ||
|
||
```typescript | ||
import { Bridge } from "thirdweb"; | ||
|
||
// Check the status of a bridge transaction | ||
const bridgeStatus = await Bridge.status({ | ||
transactionHash: "0xe199ef82a0b6215221536e18ec512813c1aa10b4f5ed0d4dfdfcd703578da56d", | ||
chainId: 8453, // The chain ID where the transaction was initiated | ||
client: thirdwebClient, | ||
}); | ||
|
||
// The status will be one of: "COMPLETED", "PENDING", "FAILED", or "NOT_FOUND" | ||
if (bridgeStatus.status === "completed") { | ||
console.log(` | ||
Bridge completed! | ||
Sent: ${bridgeStatus.originAmount} wei on chain ${bridgeStatus.originChainId} | ||
Received: ${bridgeStatus.destinationAmount} wei on chain ${bridgeStatus.destinationChainId} | ||
`); | ||
} else if (bridgeStatus.status === "pending") { | ||
console.log("Bridge transaction is still pending..."); | ||
} else { | ||
console.log("Bridge transaction failed"); | ||
} | ||
``` | ||
|
||
## Error Handling | ||
|
||
The Bridge module provides consistent error handling with descriptive error messages: | ||
|
||
```typescript | ||
try { | ||
await Bridge.Buy.quote({ | ||
// ...params | ||
}); | ||
} catch (error) { | ||
// Errors will have the format: "ErrorCode | Error message details" | ||
console.error(error.message); // e.g. "AmountTooHigh | The provided amount is too high for the requested route." | ||
} | ||
``` | ||
|
||
## Types | ||
|
||
The Bridge module exports the following TypeScript types: | ||
|
||
- `Route` - Describes a bridge route between chains and tokens | ||
- `Status` - Represents the status of a bridge transaction | ||
- `Quote` - Contains quote information for a bridge transaction | ||
- `PreparedQuote` - Extends Quote with transaction data | ||
|
||
## Integration | ||
|
||
The Bridge module is accessible as a top-level export: | ||
|
||
```typescript | ||
import { Bridge } from "thirdweb"; | ||
``` | ||
|
||
Use `Bridge.Buy`, `Bridge.Sell`, `Bridge.routes`, and `Bridge.status` to access the corresponding functionality. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { toWei } from "src/utils/units.js"; | ||
import { describe, expect, it } from "vitest"; | ||
import { TEST_CLIENT } from "~test/test-clients.js"; | ||
import * as Buy from "./Buy.js"; | ||
|
||
describe("Bridge.Buy.quote", () => { | ||
it("should get a valid quote", async () => { | ||
const quote = await Buy.quote({ | ||
originChainId: 1, | ||
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
destinationChainId: 10, | ||
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
buyAmountWei: toWei("0.01"), | ||
client: TEST_CLIENT, | ||
}); | ||
|
||
expect(quote).toBeDefined(); | ||
expect(quote.destinationAmount).toEqual(toWei("0.01")); | ||
expect(quote.intent).toBeDefined(); | ||
}); | ||
|
||
it("should surface any errors", async () => { | ||
await expect( | ||
Buy.quote({ | ||
originChainId: 1, | ||
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
destinationChainId: 10, | ||
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
buyAmountWei: toWei("1000000000"), | ||
client: TEST_CLIENT, | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`[Error: AMOUNT_TOO_HIGH | The provided amount is too high for the requested route.]`, | ||
); | ||
}); | ||
}); | ||
|
||
describe("Bridge.Buy.prepare", () => { | ||
it("should get a valid prepared quote", async () => { | ||
const quote = await Buy.prepare({ | ||
originChainId: 1, | ||
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
destinationChainId: 10, | ||
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
buyAmountWei: toWei("0.01"), | ||
sender: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
receiver: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
client: TEST_CLIENT, | ||
}); | ||
|
||
expect(quote).toBeDefined(); | ||
expect(quote.destinationAmount).toEqual(toWei("0.01")); | ||
expect(quote.transactions).toBeDefined(); | ||
expect(quote.transactions.length).toBeGreaterThan(0); | ||
expect(quote.intent).toBeDefined(); | ||
}); | ||
|
||
it("should surface any errors", async () => { | ||
await expect( | ||
Buy.prepare({ | ||
originChainId: 1, | ||
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
destinationChainId: 10, | ||
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
buyAmountWei: toWei("1000000000"), | ||
sender: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
receiver: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", | ||
client: TEST_CLIENT, | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`[Error: AMOUNT_TOO_HIGH | The provided amount is too high for the requested route.]`, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.