Skip to content

Commit fbecb94

Browse files
[SDK] Expose waitUntil parameter in settlePayment()
1 parent ce9220e commit fbecb94

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

.changeset/yellow-cameras-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Also expose waitUntil param in settlePayment()

packages/thirdweb/src/exports/x402.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ export {
33
facilitator,
44
type ThirdwebX402Facilitator,
55
type ThirdwebX402FacilitatorConfig,
6+
type WaitUntil,
67
} from "../x402/facilitator.js";
78
export { wrapFetchWithPayment } from "../x402/fetchWithPayment.js";
89
export { settlePayment } from "../x402/settle-payment.js";
910
export type {
11+
ERC20TokenAmount,
1012
PaymentArgs,
13+
PaymentRequiredResult,
14+
SettlePaymentArgs,
1115
SettlePaymentResult,
16+
SupportedSignatureType,
1217
VerifyPaymentResult,
1318
} from "../x402/types.js";
1419
export { verifyPayment } from "../x402/verify-payment.js";

packages/thirdweb/src/x402/facilitator.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import type {
99
RequestedPaymentRequirements,
1010
} from "./schemas.js";
1111

12+
export type WaitUntil = "simulated" | "submitted" | "confirmed";
13+
1214
export type ThirdwebX402FacilitatorConfig = {
1315
client: ThirdwebClient;
1416
serverWalletAddress: string;
15-
waitUtil?: "simulated" | "submitted" | "confirmed";
17+
waitUtil?: WaitUntil;
1618
vaultAccessToken?: string;
1719
baseUrl?: string;
1820
};
@@ -37,6 +39,7 @@ export type ThirdwebX402Facilitator = {
3739
settle: (
3840
payload: RequestedPaymentPayload,
3941
paymentRequirements: RequestedPaymentRequirements,
42+
waitUtil?: WaitUntil,
4043
) => Promise<FacilitatorSettleResponse>;
4144
supported: (filters?: {
4245
chainId: number;
@@ -81,6 +84,21 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
8184
* },
8285
* thirdwebX402Facilitator,
8386
* );
87+
* ```
88+
*
89+
* #### Configuration Options
90+
*
91+
* ```ts
92+
* const thirdwebX402Facilitator = facilitator({
93+
* client: client,
94+
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
95+
* // Optional: Wait behavior for settlements
96+
* // - "simulated": Only simulate the transaction (fastest)
97+
* // - "submitted": Wait until transaction is submitted
98+
* // - "confirmed": Wait for full on-chain confirmation (slowest, default)
99+
* waitUntil: "confirmed",
100+
* });
101+
84102
* ```
85103
*
86104
* @bridge x402
@@ -167,12 +185,14 @@ export function facilitator(
167185
async settle(
168186
payload: RequestedPaymentPayload,
169187
paymentRequirements: RequestedPaymentRequirements,
188+
waitUtil?: WaitUntil,
170189
): Promise<FacilitatorSettleResponse> {
171190
const url = config.baseUrl ?? DEFAULT_BASE_URL;
172191

173192
let headers = { "Content-Type": "application/json" };
174193
const authHeaders = await facilitator.createAuthHeaders();
175194
headers = { ...headers, ...authHeaders.settle };
195+
const waitUtilParam = waitUtil || config.waitUtil;
176196

177197
const res = await fetch(`${url}/settle`, {
178198
method: "POST",
@@ -181,7 +201,7 @@ export function facilitator(
181201
x402Version: payload.x402Version,
182202
paymentPayload: payload,
183203
paymentRequirements: paymentRequirements,
184-
...(config.waitUtil ? { waitUtil: config.waitUtil } : {}),
204+
...(waitUtilParam ? { waitUtil: waitUtilParam } : {}),
185205
}),
186206
});
187207

packages/thirdweb/src/x402/settle-payment.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { stringify } from "../utils/json.js";
22
import { decodePaymentRequest } from "./common.js";
33
import { safeBase64Encode } from "./encode.js";
44
import {
5-
type PaymentArgs,
5+
type SettlePaymentArgs,
66
type SettlePaymentResult,
77
x402Version,
88
} from "./types.js";
@@ -97,6 +97,7 @@ import {
9797
* payTo: "0x1234567890123456789012345678901234567890",
9898
* network: arbitrumSepolia, // or any other chain
9999
* price: "$0.05",
100+
* waitUntil: "submitted",
100101
* facilitator: thirdwebFacilitator,
101102
* });
102103
*
@@ -124,7 +125,7 @@ import {
124125
* @bridge x402
125126
*/
126127
export async function settlePayment(
127-
args: PaymentArgs,
128+
args: SettlePaymentArgs,
128129
): Promise<SettlePaymentResult> {
129130
const { routeConfig = {}, facilitator } = args;
130131
const { errorMessages } = routeConfig;
@@ -142,6 +143,7 @@ export async function settlePayment(
142143
const settlement = await facilitator.settle(
143144
decodedPayment,
144145
selectedPaymentRequirements,
146+
args.waitUntil,
145147
);
146148

147149
if (settlement.success) {

packages/thirdweb/src/x402/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type z from "zod";
33
import type { Chain } from "../chains/types.js";
44
import type { Address } from "../utils/address.js";
55
import type { Prettify } from "../utils/type-utils.js";
6-
import type { ThirdwebX402Facilitator } from "./facilitator.js";
6+
import type { ThirdwebX402Facilitator, WaitUntil } from "./facilitator.js";
77
import type {
88
FacilitatorNetwork,
99
FacilitatorSettleResponse,
@@ -39,6 +39,10 @@ export type PaymentArgs = {
3939
routeConfig?: PaymentMiddlewareConfig;
4040
};
4141

42+
export type SettlePaymentArgs = PaymentArgs & {
43+
waitUntil?: WaitUntil;
44+
};
45+
4246
export type PaymentRequiredResult = {
4347
/** HTTP 402 - Payment Required, verification or processing failed or payment missing */
4448
status: 402;

0 commit comments

Comments
 (0)