Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/yellow-cameras-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Also expose waitUntil param in settlePayment()
5 changes: 5 additions & 0 deletions packages/thirdweb/src/exports/x402.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ export {
facilitator,
type ThirdwebX402Facilitator,
type ThirdwebX402FacilitatorConfig,
type WaitUntil,
} from "../x402/facilitator.js";
export { wrapFetchWithPayment } from "../x402/fetchWithPayment.js";
export { settlePayment } from "../x402/settle-payment.js";
export type {
ERC20TokenAmount,
PaymentArgs,
PaymentRequiredResult,
SettlePaymentArgs,
SettlePaymentResult,
SupportedSignatureType,
VerifyPaymentResult,
} from "../x402/types.js";
export { verifyPayment } from "../x402/verify-payment.js";
24 changes: 22 additions & 2 deletions packages/thirdweb/src/x402/facilitator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import type {
RequestedPaymentRequirements,
} from "./schemas.js";

export type WaitUntil = "simulated" | "submitted" | "confirmed";

export type ThirdwebX402FacilitatorConfig = {
client: ThirdwebClient;
serverWalletAddress: string;
waitUtil?: "simulated" | "submitted" | "confirmed";
waitUtil?: WaitUntil;
vaultAccessToken?: string;
baseUrl?: string;
};
Expand All @@ -37,6 +39,7 @@ export type ThirdwebX402Facilitator = {
settle: (
payload: RequestedPaymentPayload,
paymentRequirements: RequestedPaymentRequirements,
waitUtil?: WaitUntil,
) => Promise<FacilitatorSettleResponse>;
supported: (filters?: {
chainId: number;
Expand Down Expand Up @@ -81,6 +84,21 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
* },
* thirdwebX402Facilitator,
* );
* ```
*
* #### Configuration Options
*
* ```ts
* const thirdwebX402Facilitator = facilitator({
* client: client,
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
* // Optional: Wait behavior for settlements
* // - "simulated": Only simulate the transaction (fastest)
* // - "submitted": Wait until transaction is submitted
* // - "confirmed": Wait for full on-chain confirmation (slowest, default)
* waitUntil: "confirmed",
* });

* ```
*
* @bridge x402
Expand Down Expand Up @@ -167,12 +185,14 @@ export function facilitator(
async settle(
payload: RequestedPaymentPayload,
paymentRequirements: RequestedPaymentRequirements,
waitUtil?: WaitUntil,
): Promise<FacilitatorSettleResponse> {
const url = config.baseUrl ?? DEFAULT_BASE_URL;

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

const res = await fetch(`${url}/settle`, {
method: "POST",
Expand All @@ -181,7 +201,7 @@ export function facilitator(
x402Version: payload.x402Version,
paymentPayload: payload,
paymentRequirements: paymentRequirements,
...(config.waitUtil ? { waitUtil: config.waitUtil } : {}),
...(waitUtilParam ? { waitUtil: waitUtilParam } : {}),
}),
});

Expand Down
6 changes: 4 additions & 2 deletions packages/thirdweb/src/x402/settle-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { stringify } from "../utils/json.js";
import { decodePaymentRequest } from "./common.js";
import { safeBase64Encode } from "./encode.js";
import {
type PaymentArgs,
type SettlePaymentArgs,
type SettlePaymentResult,
x402Version,
} from "./types.js";
Expand Down Expand Up @@ -97,6 +97,7 @@ import {
* payTo: "0x1234567890123456789012345678901234567890",
* network: arbitrumSepolia, // or any other chain
* price: "$0.05",
* waitUntil: "submitted",
* facilitator: thirdwebFacilitator,
* });
*
Expand Down Expand Up @@ -124,7 +125,7 @@ import {
* @bridge x402
*/
export async function settlePayment(
args: PaymentArgs,
args: SettlePaymentArgs,
): Promise<SettlePaymentResult> {
const { routeConfig = {}, facilitator } = args;
const { errorMessages } = routeConfig;
Expand All @@ -142,6 +143,7 @@ export async function settlePayment(
const settlement = await facilitator.settle(
decodedPayment,
selectedPaymentRequirements,
args.waitUntil,
);

if (settlement.success) {
Expand Down
6 changes: 5 additions & 1 deletion packages/thirdweb/src/x402/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type z from "zod";
import type { Chain } from "../chains/types.js";
import type { Address } from "../utils/address.js";
import type { Prettify } from "../utils/type-utils.js";
import type { ThirdwebX402Facilitator } from "./facilitator.js";
import type { ThirdwebX402Facilitator, WaitUntil } from "./facilitator.js";
import type {
FacilitatorNetwork,
FacilitatorSettleResponse,
Expand Down Expand Up @@ -39,6 +39,10 @@ export type PaymentArgs = {
routeConfig?: PaymentMiddlewareConfig;
};

export type SettlePaymentArgs = PaymentArgs & {
waitUntil?: WaitUntil;
};

export type PaymentRequiredResult = {
/** HTTP 402 - Payment Required, verification or processing failed or payment missing */
status: 402;
Expand Down
Loading