Skip to content

[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 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions .changeset/clear-olives-know.md
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({
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.
82 changes: 25 additions & 57 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,67 +128,35 @@
"import": "./dist/esm/exports/ai.js",
"default": "./dist/cjs/exports/ai.js"
},
"./bridge": {
"types": "./dist/types/exports/bridge.d.ts",
"import": "./dist/esm/exports/bridge.js",
"default": "./dist/cjs/exports/bridge.js"
},
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"adapters/*": [
"./dist/types/exports/adapters/*.d.ts"
],
"auth": [
"./dist/types/exports/auth.d.ts"
],
"chains": [
"./dist/types/exports/chains.d.ts"
],
"contract": [
"./dist/types/exports/contract.d.ts"
],
"deploys": [
"./dist/types/exports/deploys.d.ts"
],
"event": [
"./dist/types/exports/event.d.ts"
],
"extensions/*": [
"./dist/types/exports/extensions/*.d.ts"
],
"pay": [
"./dist/types/exports/pay.d.ts"
],
"react": [
"./dist/types/exports/react.d.ts"
],
"react-native": [
"./dist/types/exports/react-native.d.ts"
],
"rpc": [
"./dist/types/exports/rpc.d.ts"
],
"storage": [
"./dist/types/exports/storage.d.ts"
],
"transaction": [
"./dist/types/exports/transaction.d.ts"
],
"utils": [
"./dist/types/exports/utils.d.ts"
],
"wallets": [
"./dist/types/exports/wallets.d.ts"
],
"wallets/*": [
"./dist/types/exports/wallets/*.d.ts"
],
"modules": [
"./dist/types/exports/modules.d.ts"
],
"social": [
"./dist/types/exports/social.d.ts"
],
"ai": [
"./dist/types/exports/ai.d.ts"
]
"adapters/*": ["./dist/types/exports/adapters/*.d.ts"],
"auth": ["./dist/types/exports/auth.d.ts"],
"chains": ["./dist/types/exports/chains.d.ts"],
"contract": ["./dist/types/exports/contract.d.ts"],
"deploys": ["./dist/types/exports/deploys.d.ts"],
"event": ["./dist/types/exports/event.d.ts"],
"extensions/*": ["./dist/types/exports/extensions/*.d.ts"],
"pay": ["./dist/types/exports/pay.d.ts"],
"react": ["./dist/types/exports/react.d.ts"],
"react-native": ["./dist/types/exports/react-native.d.ts"],
"rpc": ["./dist/types/exports/rpc.d.ts"],
"storage": ["./dist/types/exports/storage.d.ts"],
"transaction": ["./dist/types/exports/transaction.d.ts"],
"utils": ["./dist/types/exports/utils.d.ts"],
"wallets": ["./dist/types/exports/wallets.d.ts"],
"wallets/*": ["./dist/types/exports/wallets/*.d.ts"],
"modules": ["./dist/types/exports/modules.d.ts"],
"social": ["./dist/types/exports/social.d.ts"],
"ai": ["./dist/types/exports/ai.d.ts"],
"bridge": ["./dist/types/exports/bridge.d.ts"]
}
},
"browser": {
Expand Down
74 changes: 74 additions & 0 deletions packages/thirdweb/src/bridge/Buy.test.ts
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.]`,
);
});
});
Loading
Loading