Skip to content

Commit 657b219

Browse files
committed
feat: tool-4249 Include steps in bridge function response
BREAKING CHANGE: Bridge functions take amount instead of former names Closes: tool-4249
1 parent 525e603 commit 657b219

File tree

7 files changed

+306
-112
lines changed

7 files changed

+306
-112
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Buy.quote", () => {
1010
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
1111
destinationChainId: 10,
1212
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
13-
buyAmountWei: toWei("0.01"),
13+
amount: toWei("0.01"),
1414
client: TEST_CLIENT,
1515
});
1616

1717
expect(quote).toBeDefined();
1818
expect(quote.destinationAmount).toEqual(toWei("0.01"));
1919
expect(quote.intent).toBeDefined();
20+
expect(quote.steps.length).toBeGreaterThan(0);
2021
});
2122

2223
it("should surface any errors", async () => {
@@ -26,7 +27,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Buy.quote", () => {
2627
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
2728
destinationChainId: 444,
2829
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
29-
buyAmountWei: toWei("1000000000"),
30+
amount: toWei("1000000000"),
3031
client: TEST_CLIENT,
3132
}),
3233
).rejects.toThrowError();
@@ -40,16 +41,17 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Buy.prepare", () => {
4041
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
4142
destinationChainId: 10,
4243
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
43-
buyAmountWei: toWei("0.01"),
44+
amount: toWei("0.01"),
4445
sender: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
4546
receiver: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
4647
client: TEST_CLIENT,
4748
});
4849

4950
expect(quote).toBeDefined();
5051
expect(quote.destinationAmount).toEqual(toWei("0.01"));
51-
expect(quote.transactions).toBeDefined();
52-
expect(quote.transactions.length).toBeGreaterThan(0);
52+
for (const step of quote.steps) {
53+
expect(step.transactions.length).toBeGreaterThan(0);
54+
}
5355
expect(quote.intent).toBeDefined();
5456
});
5557

@@ -60,7 +62,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Buy.prepare", () => {
6062
originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
6163
destinationChainId: 444,
6264
destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
63-
buyAmountWei: toWei("1000000000"),
65+
amount: toWei("1000000000"),
6466
sender: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
6567
receiver: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
6668
client: TEST_CLIENT,

packages/thirdweb/src/bridge/Buy.ts

Lines changed: 102 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { PreparedQuote, Quote } from "./types/Quote.js";
1717
* originTokenAddress: NATIVE_TOKEN_ADDRESS,
1818
* destinationChainId: 10,
1919
* destinationTokenAddress: NATIVE_TOKEN_ADDRESS,
20-
* buyAmountWei: toWei("0.01"),
20+
* amount: toWei("0.01"),
2121
* client: thirdwebClient,
2222
* });
2323
* ```
@@ -30,12 +30,37 @@ import type { PreparedQuote, Quote } from "./types/Quote.js";
3030
* blockNumber: 22026509n,
3131
* timestamp: 1741730936680,
3232
* estimatedExecutionTimeMs: 1000
33+
* steps: [
34+
* {
35+
* originToken: {
36+
* chainId: 1,
37+
* address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
38+
* symbol: "ETH",
39+
* name: "Ethereum",
40+
* decimals: 18,
41+
* priceUsd: 0.0025,
42+
* iconUri: "https://..."
43+
* },
44+
* destinationToken: {
45+
* chainId: 10,
46+
* address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
47+
* symbol: "ETH",
48+
* name: "Ethereum",
49+
* decimals: 18,
50+
* priceUsd: 0.0025,
51+
* iconUri: "https://..."
52+
* },
53+
* originAmount: 10000026098875381n,
54+
* destinationAmount: 1000000000000000000n,
55+
* estimatedExecutionTimeMs: 1000
56+
* }
57+
* ],
3358
* intent: {
3459
* originChainId: 1,
3560
* originTokenAddress: NATIVE_TOKEN_ADDRESS,
3661
* destinationChainId: 10,
3762
* destinationTokenAddress: NATIVE_TOKEN_ADDRESS,
38-
* buyAmountWei: 1000000000000000000n
63+
* amount: 1000000000000000000n
3964
* }
4065
* }
4166
* ```
@@ -50,7 +75,7 @@ import type { PreparedQuote, Quote } from "./types/Quote.js";
5075
* @param options.originTokenAddress - The address of the origin token.
5176
* @param options.destinationChainId - The chain ID of the destination token.
5277
* @param options.destinationTokenAddress - The address of the destination token.
53-
* @param options.buyAmountWei - The amount of the origin token to buy.
78+
* @param options.amount - The amount of the destination token to receive.
5479
* @param options.client - Your thirdweb client.
5580
*
5681
* @returns A promise that resolves to a non-finalized quote for the requested buy.
@@ -65,17 +90,18 @@ export async function quote(options: quote.Options): Promise<quote.Result> {
6590
originTokenAddress,
6691
destinationChainId,
6792
destinationTokenAddress,
68-
buyAmountWei,
6993
client,
7094
} = options;
95+
const amount =
96+
"buyAmountWei" in options ? options.buyAmountWei : options.amount;
7197

7298
const clientFetch = getClientFetch(client);
7399
const url = new URL(`${UNIVERSAL_BRIDGE_URL}/buy/quote`);
74100
url.searchParams.set("originChainId", originChainId.toString());
75101
url.searchParams.set("originTokenAddress", originTokenAddress);
76102
url.searchParams.set("destinationChainId", destinationChainId.toString());
77103
url.searchParams.set("destinationTokenAddress", destinationTokenAddress);
78-
url.searchParams.set("buyAmountWei", buyAmountWei.toString());
104+
url.searchParams.set("buyAmountWei", amount.toString());
79105

80106
const response = await clientFetch(url.toString());
81107
if (!response.ok) {
@@ -92,12 +118,14 @@ export async function quote(options: quote.Options): Promise<quote.Result> {
92118
blockNumber: data.blockNumber ? BigInt(data.blockNumber) : undefined,
93119
timestamp: data.timestamp,
94120
estimatedExecutionTimeMs: data.estimatedExecutionTimeMs,
121+
steps: data.steps,
95122
intent: {
96123
originChainId,
97124
originTokenAddress,
98125
destinationChainId,
99126
destinationTokenAddress,
100-
buyAmountWei,
127+
buyAmountWei: amount,
128+
amount,
101129
},
102130
};
103131
}
@@ -108,9 +136,15 @@ export declare namespace quote {
108136
originTokenAddress: ox__Address.Address;
109137
destinationChainId: number;
110138
destinationTokenAddress: ox__Address.Address;
111-
buyAmountWei: bigint;
112139
client: ThirdwebClient;
113-
};
140+
} & (
141+
| {
142+
buyAmountWei: bigint;
143+
}
144+
| {
145+
amount: bigint;
146+
}
147+
);
114148

115149
type Result = Quote & {
116150
intent: {
@@ -119,6 +153,7 @@ export declare namespace quote {
119153
destinationChainId: number;
120154
destinationTokenAddress: ox__Address.Address;
121155
buyAmountWei: bigint;
156+
amount: bigint;
122157
};
123158
};
124159
}
@@ -135,7 +170,7 @@ export declare namespace quote {
135170
* originTokenAddress: NATIVE_TOKEN_ADDRESS,
136171
* destinationChainId: 10,
137172
* destinationTokenAddress: NATIVE_TOKEN_ADDRESS,
138-
* buyAmountWei: toWei("0.01"),
173+
* amount: toWei("0.01"),
139174
* client: thirdwebClient,
140175
* });
141176
* ```
@@ -148,31 +183,56 @@ export declare namespace quote {
148183
* blockNumber: 22026509n,
149184
* timestamp: 1741730936680,
150185
* estimatedExecutionTimeMs: 1000
151-
* transactions: [
186+
* steps: [
152187
* {
153-
* action: "approval",
154-
* id: "0x",
155-
* to: "0x...",
156-
* data: "0x...",
157-
* chainId: 10,
158-
* type: "eip1559"
159-
* },
160-
* {
161-
* action: "buy",
162-
* to: "0x...",
163-
* value: 10000026098875381n,
164-
* data: "0x...",
165-
* chainId: 10,
166-
* type: "eip1559"
188+
* originToken: {
189+
* chainId: 1,
190+
* address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
191+
* symbol: "ETH",
192+
* name: "Ethereum",
193+
* decimals: 18,
194+
* priceUsd: 2000,
195+
* iconUri: "https://..."
196+
* },
197+
* destinationToken: {
198+
* chainId: 10,
199+
* address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
200+
* symbol: "ETH",
201+
* name: "Ethereum",
202+
* decimals: 18,
203+
* priceUsd: 2000,
204+
* iconUri: "https://..."
205+
* },
206+
* originAmount: 10000026098875381n,
207+
* destinationAmount: 1000000000000000000n,
208+
* estimatedExecutionTimeMs: 1000
209+
* transactions: [
210+
* {
211+
* action: "approval",
212+
* id: "0x",
213+
* to: "0x...",
214+
* data: "0x...",
215+
* chainId: 10,
216+
* type: "eip1559"
217+
* },
218+
* {
219+
* action: "buy",
220+
* to: "0x...",
221+
* value: 10000026098875381n,
222+
* data: "0x...",
223+
* chainId: 10,
224+
* type: "eip1559"
225+
* }
226+
* ]
167227
* }
168228
* ],
169229
* expiration: 1741730936680,
170230
* intent: {
171231
* originChainId: 1,
172-
* originTokenAddress: NATIVE_TOKEN_ADDRESS,
232+
* originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
173233
* destinationChainId: 10,
174-
* destinationTokenAddress: NATIVE_TOKEN_ADDRESS,
175-
* buyAmountWei: 1000000000000000000n
234+
* destinationTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
235+
* amount: 1000000000000000000n
176236
* }
177237
* }
178238
* ```
@@ -192,12 +252,12 @@ export declare namespace quote {
192252
* @param options.originTokenAddress - The address of the origin token.
193253
* @param options.destinationChainId - The chain ID of the destination token.
194254
* @param options.destinationTokenAddress - The address of the destination token.
195-
* @param options.buyAmountWei - The amount of the origin token to buy.
255+
* @param options.amount - The amount of the destination token to receive.
196256
* @param options.sender - The address of the sender.
197257
* @param options.receiver - The address of the recipient.
198258
* @param options.client - Your thirdweb client.
199259
*
200-
* @returns A promise that resolves to a non-finalized quote for the requested buy.
260+
* @returns A promise that resolves to a finalized quote and transactions for the requested buy.
201261
*
202262
* @throws Will throw an error if there is an issue fetching the quote.
203263
* @bridge Buy
@@ -211,10 +271,10 @@ export async function prepare(
211271
originTokenAddress,
212272
destinationChainId,
213273
destinationTokenAddress,
214-
buyAmountWei,
215274
sender,
216275
receiver,
217276
client,
277+
amount,
218278
} = options;
219279

220280
const clientFetch = getClientFetch(client);
@@ -223,7 +283,7 @@ export async function prepare(
223283
url.searchParams.set("originTokenAddress", originTokenAddress);
224284
url.searchParams.set("destinationChainId", destinationChainId.toString());
225285
url.searchParams.set("destinationTokenAddress", destinationTokenAddress);
226-
url.searchParams.set("buyAmountWei", buyAmountWei.toString());
286+
url.searchParams.set("buyAmountWei", amount.toString());
227287
url.searchParams.set("sender", sender);
228288
url.searchParams.set("receiver", receiver);
229289

@@ -242,19 +302,21 @@ export async function prepare(
242302
blockNumber: data.blockNumber ? BigInt(data.blockNumber) : undefined,
243303
timestamp: data.timestamp,
244304
estimatedExecutionTimeMs: data.estimatedExecutionTimeMs,
245-
transactions: data.transactions.map((transaction) => ({
246-
...transaction,
247-
value: transaction.value ? BigInt(transaction.value) : undefined,
248-
client,
249-
chain: defineChain(transaction.chainId),
305+
steps: data.steps.map((step) => ({
306+
...step,
307+
transactions: step.transactions.map((transaction) => ({
308+
...transaction,
309+
value: transaction.value ? BigInt(transaction.value) : undefined,
310+
client,
311+
chain: defineChain(transaction.chainId),
312+
})),
250313
})),
251-
expiration: data.expiration,
252314
intent: {
253315
originChainId,
254316
originTokenAddress,
255317
destinationChainId,
256318
destinationTokenAddress,
257-
buyAmountWei,
319+
amount,
258320
},
259321
};
260322
}
@@ -265,9 +327,9 @@ export declare namespace prepare {
265327
originTokenAddress: ox__Address.Address;
266328
destinationChainId: number;
267329
destinationTokenAddress: ox__Address.Address;
268-
buyAmountWei: bigint;
269330
sender: ox__Address.Address;
270331
receiver: ox__Address.Address;
332+
amount: bigint;
271333
client: ThirdwebClient;
272334
};
273335

@@ -277,7 +339,7 @@ export declare namespace prepare {
277339
originTokenAddress: ox__Address.Address;
278340
destinationChainId: number;
279341
destinationTokenAddress: ox__Address.Address;
280-
buyAmountWei: bigint;
342+
amount: bigint;
281343
};
282344
};
283345
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ describe.runIf(process.env.TW_SECRET_KEY)("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
});

packages/thirdweb/src/bridge/Routes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import type { Route } from "./types/Route.js";
9393
* @param options.destinationChainId - Filter by a specific destination chain ID.
9494
* @param options.destinationTokenAddress - Filter by a specific destination token address.
9595
* @param options.transactionHash - Filter by a specific transaction hash.
96+
* @param options.maxSteps - Limit the number of steps returned.
9697
* @param options.limit - Limit the number of routes returned.
9798
* @param options.offset - Offset the number of routes returned.
9899
*
@@ -109,6 +110,7 @@ export async function routes(options: routes.Options): Promise<routes.Result> {
109110
originTokenAddress,
110111
destinationChainId,
111112
destinationTokenAddress,
113+
maxSteps,
112114
limit,
113115
offset,
114116
} = options;
@@ -127,6 +129,9 @@ export async function routes(options: routes.Options): Promise<routes.Result> {
127129
if (destinationTokenAddress) {
128130
url.searchParams.set("destinationTokenAddress", destinationTokenAddress);
129131
}
132+
if (maxSteps) {
133+
url.searchParams.set("maxSteps", maxSteps.toString());
134+
}
130135
if (limit) {
131136
url.searchParams.set("limit", limit.toString());
132137
}
@@ -152,6 +157,7 @@ export declare namespace routes {
152157
destinationChainId?: number;
153158
destinationTokenAddress?: ox__Address.Address;
154159
transactionHash?: ox__Hex.Hex;
160+
maxSteps?: number;
155161
limit?: number;
156162
offset?: number;
157163
};

0 commit comments

Comments
 (0)