Skip to content

Commit ec2f6e9

Browse files
committed
feat: tool-4249 Include purchaseData in status response
Closes: tool-4249
1 parent 657b219 commit ec2f6e9

File tree

6 files changed

+105
-69
lines changed

6 files changed

+105
-69
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Buy.prepare", () => {
4545
sender: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
4646
receiver: "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709",
4747
client: TEST_CLIENT,
48+
purchaseData: {
49+
foo: "bar",
50+
},
4851
});
4952

5053
expect(quote).toBeDefined();

packages/thirdweb/src/bridge/Buy.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ export declare namespace quote {
255255
* @param options.amount - The amount of the destination token to receive.
256256
* @param options.sender - The address of the sender.
257257
* @param options.receiver - The address of the recipient.
258+
* @param options.purchaseData - Arbitrary data to be passed to the purchase function and included with any webhooks or status calls.
258259
* @param options.client - Your thirdweb client.
259260
*
260261
* @returns A promise that resolves to a finalized quote and transactions for the requested buy.
@@ -275,19 +276,28 @@ export async function prepare(
275276
receiver,
276277
client,
277278
amount,
279+
purchaseData,
278280
} = options;
279281

280282
const clientFetch = getClientFetch(client);
281283
const url = new URL(`${UNIVERSAL_BRIDGE_URL}/buy/prepare`);
282-
url.searchParams.set("originChainId", originChainId.toString());
283-
url.searchParams.set("originTokenAddress", originTokenAddress);
284-
url.searchParams.set("destinationChainId", destinationChainId.toString());
285-
url.searchParams.set("destinationTokenAddress", destinationTokenAddress);
286-
url.searchParams.set("buyAmountWei", amount.toString());
287-
url.searchParams.set("sender", sender);
288-
url.searchParams.set("receiver", receiver);
289284

290-
const response = await clientFetch(url.toString());
285+
const response = await clientFetch(url.toString(), {
286+
method: "POST",
287+
headers: {
288+
"Content-Type": "application/json",
289+
},
290+
body: JSON.stringify({
291+
buyAmountWei: amount.toString(),
292+
originChainId: originChainId.toString(),
293+
originTokenAddress,
294+
destinationChainId: destinationChainId.toString(),
295+
destinationTokenAddress,
296+
sender,
297+
receiver,
298+
purchaseData,
299+
}),
300+
});
291301
if (!response.ok) {
292302
const errorJson = await response.json();
293303
throw new Error(
@@ -331,6 +341,7 @@ export declare namespace prepare {
331341
receiver: ox__Address.Address;
332342
amount: bigint;
333343
client: ThirdwebClient;
344+
purchaseData?: unknown;
334345
};
335346

336347
type Result = PreparedQuote & {
@@ -340,6 +351,7 @@ export declare namespace prepare {
340351
destinationChainId: number;
341352
destinationTokenAddress: ox__Address.Address;
342353
amount: bigint;
354+
purchaseData?: unknown;
343355
};
344356
};
345357
}

packages/thirdweb/src/bridge/Sell.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export declare namespace quote {
246246
* @param options.amount - The amount of the origin token to sell.
247247
* @param options.sender - The address of the sender.
248248
* @param options.receiver - The address of the recipient.
249+
* @param options.purchaseData - Arbitrary data to be passed to the purchase function and included with any webhooks or status calls.
249250
* @param options.client - Your thirdweb client.
250251
*
251252
* @returns A promise that resolves to a finalized quote and transactions for the requested sell.
@@ -266,19 +267,28 @@ export async function prepare(
266267
sender,
267268
receiver,
268269
client,
270+
purchaseData,
269271
} = options;
270272

271273
const clientFetch = getClientFetch(client);
272274
const url = new URL(`${UNIVERSAL_BRIDGE_URL}/sell/prepare`);
273-
url.searchParams.set("originChainId", originChainId.toString());
274-
url.searchParams.set("originTokenAddress", originTokenAddress);
275-
url.searchParams.set("destinationChainId", destinationChainId.toString());
276-
url.searchParams.set("destinationTokenAddress", destinationTokenAddress);
277-
url.searchParams.set("sellAmountWei", amount.toString());
278-
url.searchParams.set("sender", sender);
279-
url.searchParams.set("receiver", receiver);
280275

281-
const response = await clientFetch(url.toString());
276+
const response = await clientFetch(url.toString(), {
277+
method: "POST",
278+
headers: {
279+
"Content-Type": "application/json",
280+
},
281+
body: JSON.stringify({
282+
sellAmountWei: amount.toString(),
283+
originChainId: originChainId.toString(),
284+
originTokenAddress,
285+
destinationChainId: destinationChainId.toString(),
286+
destinationTokenAddress,
287+
sender,
288+
receiver,
289+
purchaseData,
290+
}),
291+
});
282292
if (!response.ok) {
283293
const errorJson = await response.json();
284294
throw new Error(
@@ -309,6 +319,7 @@ export async function prepare(
309319
destinationChainId,
310320
destinationTokenAddress,
311321
amount,
322+
purchaseData,
312323
},
313324
};
314325
}
@@ -323,6 +334,7 @@ export declare namespace prepare {
323334
sender: ox__Address.Address;
324335
receiver: ox__Address.Address;
325336
client: ThirdwebClient;
337+
purchaseData?: unknown;
326338
};
327339

328340
type Result = PreparedQuote & {
@@ -332,6 +344,7 @@ export declare namespace prepare {
332344
destinationChainId: number;
333345
destinationTokenAddress: ox__Address.Address;
334346
amount: bigint;
347+
purchaseData?: unknown;
335348
};
336349
};
337350
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,33 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.status", () => {
77
it("should handle successful status", async () => {
88
const result = await status({
99
transactionHash:
10-
"0x7bedc4693e899fe81a22dac11301e77a12a6e772834bba5b698baf3ebcf86f7a",
11-
chainId: 8453,
10+
"0x5959b9321ec581640db531b80bac53cbd968f3d34fc6cb1d5f4ea75f26df2ad7",
11+
chainId: 137,
1212
client: TEST_CLIENT,
1313
});
1414

1515
expect(result).toBeDefined();
1616
expect(result.status).toBe("COMPLETED");
1717
expect(result).toMatchInlineSnapshot(`
1818
{
19-
"destinationAmount": 500000n,
20-
"destinationChainId": 466,
21-
"destinationTokenAddress": "0x675C3ce7F43b00045a4Dab954AF36160fb57cB45",
22-
"originAmount": 524750n,
23-
"originChainId": 8453,
24-
"originTokenAddress": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
19+
"destinationAmount": 502590n,
20+
"destinationChainId": 8453,
21+
"destinationTokenAddress": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
22+
"originAmount": 507688n,
23+
"originChainId": 137,
24+
"originTokenAddress": "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
25+
"purchaseData": {
26+
"name": "Greg",
27+
},
2528
"status": "COMPLETED",
2629
"transactions": [
2730
{
28-
"chainId": 8453,
29-
"transactionHash": "0x7bedc4693e899fe81a22dac11301e77a12a6e772834bba5b698baf3ebcf86f7a",
31+
"chainId": 137,
32+
"transactionHash": "0x5959b9321ec581640db531b80bac53cbd968f3d34fc6cb1d5f4ea75f26df2ad7",
3033
},
3134
{
32-
"chainId": 466,
33-
"transactionHash": "0xb0de713fbe44b7939b3c9cfa02c0233ea659d1163cc4462462e12eef57bc17f1",
35+
"chainId": 8453,
36+
"transactionHash": "0xa3fa708d9f8e3bf4f97bb2bc04d4f6f7d27b13eb82fa29fc8596e433ed16295d",
3437
},
3538
],
3639
}

packages/thirdweb/src/bridge/Status.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ import type { Status } from "./types/Status.js";
4040
* chainId: 2741,
4141
* transactionHash: '0xa70a82f42330f54be95a542e1fcfe6ed2dd9f07fb8c82ae67afb4342319f7433'
4242
* }
43-
* ]
43+
* ],
44+
* purchaseData: {
45+
* foo: "bar"
46+
* }
4447
* }
4548
* ```
4649
*
@@ -153,21 +156,22 @@ export async function status(options: status.Options): Promise<status.Result> {
153156
originTokenAddress: data.originTokenAddress,
154157
destinationTokenAddress: data.destinationTokenAddress,
155158
transactions: data.transactions,
159+
purchaseData: data.purchaseData,
156160
};
157161
}
158162

159163
export declare namespace status {
160164
type Options =
161165
| {
162-
transactionHash: ox__Hex.Hex;
163-
chainId: number;
164-
client: ThirdwebClient;
165-
}
166+
transactionHash: ox__Hex.Hex;
167+
chainId: number;
168+
client: ThirdwebClient;
169+
}
166170
| {
167-
transactionHash: ox__Hex.Hex;
168-
chain: Chain;
169-
client: ThirdwebClient;
170-
};
171+
transactionHash: ox__Hex.Hex;
172+
chain: Chain;
173+
client: ThirdwebClient;
174+
};
171175

172176
type Result = Status;
173177
}

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,39 @@ import type { Address as ox__Address, Hex as ox__Hex } from "ox";
22

33
export type Status =
44
| {
5-
status: "COMPLETED";
6-
originAmount: bigint;
7-
destinationAmount: bigint;
8-
originChainId: number;
9-
destinationChainId: number;
10-
originTokenAddress: ox__Address.Address;
11-
destinationTokenAddress: ox__Address.Address;
12-
transactions: Array<{
13-
chainId: number;
14-
transactionHash: ox__Hex.Hex;
15-
}>;
16-
}
5+
status: "COMPLETED";
6+
originAmount: bigint;
7+
destinationAmount: bigint;
8+
originChainId: number;
9+
destinationChainId: number;
10+
originTokenAddress: ox__Address.Address;
11+
destinationTokenAddress: ox__Address.Address;
12+
transactions: Array<{
13+
chainId: number;
14+
transactionHash: ox__Hex.Hex;
15+
}>;
16+
purchaseData?: unknown;
17+
}
1718
| {
18-
status: "PENDING";
19-
originAmount: bigint;
20-
originChainId: number;
21-
destinationChainId: number;
22-
originTokenAddress: ox__Address.Address;
23-
destinationTokenAddress: ox__Address.Address;
24-
transactions: Array<{
25-
chainId: number;
26-
transactionHash: ox__Hex.Hex;
27-
}>;
28-
}
19+
status: "PENDING";
20+
originAmount: bigint;
21+
originChainId: number;
22+
destinationChainId: number;
23+
originTokenAddress: ox__Address.Address;
24+
destinationTokenAddress: ox__Address.Address;
25+
transactions: Array<{
26+
chainId: number;
27+
transactionHash: ox__Hex.Hex;
28+
}>;
29+
}
2930
| {
30-
status: "FAILED";
31-
transactions: Array<{
32-
chainId: number;
33-
transactionHash: ox__Hex.Hex;
34-
}>;
35-
}
31+
status: "FAILED";
32+
transactions: Array<{
33+
chainId: number;
34+
transactionHash: ox__Hex.Hex;
35+
}>;
36+
}
3637
| {
37-
status: "NOT_FOUND";
38-
transactions: [];
39-
};
38+
status: "NOT_FOUND";
39+
transactions: [];
40+
};

0 commit comments

Comments
 (0)