Skip to content

Commit 820c963

Browse files
committed
changeset and lint
1 parent 00c60c0 commit 820c963

File tree

10 files changed

+195
-15
lines changed

10 files changed

+195
-15
lines changed

.changeset/clear-olives-know.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 } 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: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
29+
destinationChainId: 10, // Optimism
30+
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
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: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
41+
destinationChainId: 10,
42+
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
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: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
62+
destinationChainId: 10, // Optimism
63+
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
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: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
74+
destinationChainId: 10,
75+
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
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 } 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: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
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", or "failed"
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.

packages/thirdweb/src/bridge/Routes.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { afterAll, beforeAll, afterEach, describe, expect, it } from "vitest";
2-
import { TEST_CLIENT } from "~test/test-clients.js";
3-
import { routes } from "./Routes.js";
41
import { http, passthrough } from "msw";
52
import { setupServer } from "msw/node";
3+
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
4+
import { TEST_CLIENT } from "~test/test-clients.js";
5+
import { routes } from "./Routes.js";
66

77
const server = setupServer(
88
http.get("https://bridge.thirdweb.com/v1/routes", () => {
@@ -102,9 +102,9 @@ describe("Bridge.routes", () => {
102102
route.originToken.chainId === 1 &&
103103
route.destinationToken.chainId === 10 &&
104104
route.originToken.address ===
105-
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" &&
105+
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" &&
106106
route.destinationToken.address ===
107-
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
107+
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
108108
),
109109
).toBe(true);
110110
});
@@ -154,6 +154,8 @@ describe("Bridge.routes", () => {
154154
limit: 1000,
155155
offset: 1000,
156156
}),
157-
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: InvalidRoutesRequest | The provided request is invalid.]`);
157+
).rejects.toThrowErrorMatchingInlineSnapshot(
158+
`[Error: InvalidRoutesRequest | The provided request is invalid.]`,
159+
);
158160
});
159161
});

packages/thirdweb/src/bridge/Status.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { status } from "./Status.js";
55
describe("Bridge.status", () => {
66
it("should handle invalid transaction hash without throwing", async () => {
77
const result = await status({
8-
transactionHash: "0xe199ef82a0b6215221536e18ec512813c1aa10b4f5ed0d4dfdfcd703578da56d",
8+
transactionHash:
9+
"0xe199ef82a0b6215221536e18ec512813c1aa10b4f5ed0d4dfdfcd703578da56d",
910
chainId: 1,
1011
client: TEST_CLIENT,
1112
});
@@ -14,4 +15,4 @@ describe("Bridge.status", () => {
1415
expect(result.status).toBe("failed");
1516
expect(result.transactions).toBeDefined();
1617
});
17-
});
18+
});

packages/thirdweb/src/bridge/Status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ export declare namespace status {
143143
};
144144

145145
type Result = Status;
146-
}
146+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const UNIVERSAL_BRIDGE_URL = "https://bridge.thirdweb.com/v1";
1+
export const UNIVERSAL_BRIDGE_URL = "https://bridge.thirdweb.com/v1";

packages/thirdweb/src/bridge/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export { routes } from "./Routes.js";
55

66
export type { Status } from "./types/Status.js";
77
export type { Route } from "./types/Route.js";
8-
export type { Quote, PreparedQuote } from "./types/Quote.js";
8+
export type { Quote, PreparedQuote } from "./types/Quote.js";

packages/thirdweb/src/bridge/types/Quote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ export type PreparedQuote = Quote & {
3939
"eip1559"
4040
>
4141
>;
42-
};
42+
};

packages/thirdweb/src/bridge/types/Route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export type Route = {
1717
name: string;
1818
iconUri?: string;
1919
};
20-
};
20+
};

packages/thirdweb/src/bridge/types/Status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ export type Status =
3232
chainId: number;
3333
transactionHash: ox__Hex.Hex;
3434
}>;
35-
};
35+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "../bridge/index.js";
1+
export * from "../bridge/index.js";

0 commit comments

Comments
 (0)