Skip to content

Commit af9a73d

Browse files
[SDK] Fix waitUntil facilitator parameter not being respected (#8184)
1 parent 34c5d24 commit af9a73d

File tree

5 files changed

+114
-95
lines changed

5 files changed

+114
-95
lines changed

.changeset/ten-donuts-fail.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+
Fix waitUntil facilitator param not being respected

.github/workflows/auto-assign.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Auto Author Assign
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, ready_for_review]
6+
7+
permissions:
8+
pull-requests: write
9+
10+
jobs:
11+
assign-author:
12+
runs-on: ubuntu-latest
13+
if: |
14+
github.event.pull_request.author_association == 'MEMBER' ||
15+
github.event.pull_request.author_association == 'OWNER' ||
16+
github.event.pull_request.author_association == 'COLLABORATOR' ||
17+
github.event.pull_request.author_association == 'CONTRIBUTOR'
18+
steps:
19+
- uses: toshimaru/auto-author-assign@16f0022cf3d7970c106d8d1105f75a1165edb516 # v2.1.1
Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,90 @@
1-
import { NextResponse } from "next/server";
1+
import { type NextRequest, NextResponse } from "next/server";
2+
import { createThirdwebClient, defineChain } from "thirdweb";
3+
import { toUnits } from "thirdweb/utils";
4+
import { facilitator, settlePayment } from "thirdweb/x402";
5+
import { token } from "../../payments/x402/components/constants";
6+
27
// Allow streaming responses up to 5 minutes
38
export const maxDuration = 300;
49

5-
export async function GET(_req: Request) {
6-
return NextResponse.json({
7-
success: true,
8-
message: "Payment successful. You have accessed the protected route.",
10+
export async function GET(request: NextRequest) {
11+
const client = createThirdwebClient({
12+
secretKey: process.env.THIRDWEB_SECRET_KEY as string,
13+
});
14+
15+
const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string;
16+
// const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_SMART_WALLET as string;
17+
const ENGINE_VAULT_ACCESS_TOKEN = process.env
18+
.ENGINE_VAULT_ACCESS_TOKEN as string;
19+
const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
20+
21+
const twFacilitator = facilitator({
22+
baseUrl: `${API_URL}/v1/payments/x402`,
23+
client,
24+
serverWalletAddress: BACKEND_WALLET_ADDRESS,
25+
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
26+
});
27+
28+
const paymentData = request.headers.get("X-PAYMENT");
29+
const queryParams = request.nextUrl.searchParams;
30+
31+
const chainId = queryParams.get("chainId");
32+
33+
if (!chainId) {
34+
return NextResponse.json(
35+
{ error: "Missing required parameters" },
36+
{ status: 400 },
37+
);
38+
}
39+
40+
const amount = queryParams.get("amount") || "0.01";
41+
const tokenAddress = queryParams.get("tokenAddress") || token.address;
42+
const decimals = queryParams.get("decimals") || token.decimals.toString();
43+
const waitUntil =
44+
(queryParams.get("waitUntil") as "simulated" | "submitted" | "confirmed") ||
45+
"simulated";
46+
47+
const result = await settlePayment({
48+
resourceUrl: "https://playground-web.thirdweb.com/api/paywall",
49+
method: "GET",
50+
paymentData,
51+
network: defineChain(Number(chainId)),
52+
price: {
53+
amount: toUnits(amount, parseInt(decimals)).toString(),
54+
asset: {
55+
address: tokenAddress as `0x${string}`,
56+
decimals: decimals ? parseInt(decimals) : token.decimals,
57+
},
58+
},
59+
routeConfig: {
60+
description: "Access to paid content",
61+
},
62+
waitUntil,
63+
facilitator: twFacilitator,
64+
});
65+
66+
if (result.status === 200) {
67+
// payment successful, execute the request
68+
return NextResponse.json(
69+
{
70+
success: true,
71+
message: "Payment successful. You have accessed the protected route.",
72+
payment: {
73+
amount,
74+
tokenAddress,
75+
},
76+
receipt: result.paymentReceipt,
77+
},
78+
{
79+
status: 200,
80+
headers: result.responseHeaders,
81+
},
82+
);
83+
}
84+
85+
// otherwise, request payment
86+
return NextResponse.json(result.responseBody, {
87+
status: result.status,
88+
headers: result.responseHeaders,
989
});
1090
}

apps/playground-web/src/middleware.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

packages/thirdweb/src/x402/facilitator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type WaitUntil = "simulated" | "submitted" | "confirmed";
1515
export type ThirdwebX402FacilitatorConfig = {
1616
client: ThirdwebClient;
1717
serverWalletAddress: string;
18-
waitUtil?: WaitUntil;
18+
waitUntil?: WaitUntil;
1919
vaultAccessToken?: string;
2020
baseUrl?: string;
2121
};
@@ -40,7 +40,7 @@ export type ThirdwebX402Facilitator = {
4040
settle: (
4141
payload: RequestedPaymentPayload,
4242
paymentRequirements: RequestedPaymentRequirements,
43-
waitUtil?: WaitUntil,
43+
waitUntil?: WaitUntil,
4444
) => Promise<FacilitatorSettleResponse>;
4545
supported: (filters?: {
4646
chainId: number;
@@ -185,14 +185,14 @@ export function facilitator(
185185
async settle(
186186
payload: RequestedPaymentPayload,
187187
paymentRequirements: RequestedPaymentRequirements,
188-
waitUtil?: WaitUntil,
188+
waitUntil?: WaitUntil,
189189
): Promise<FacilitatorSettleResponse> {
190190
const url = config.baseUrl ?? DEFAULT_BASE_URL;
191191

192192
let headers = { "Content-Type": "application/json" };
193193
const authHeaders = await facilitator.createAuthHeaders();
194194
headers = { ...headers, ...authHeaders.settle };
195-
const waitUtilParam = waitUtil || config.waitUtil;
195+
const waitUntilParam = waitUntil || config.waitUntil;
196196

197197
const res = await fetch(`${url}/settle`, {
198198
method: "POST",
@@ -201,7 +201,7 @@ export function facilitator(
201201
x402Version: payload.x402Version,
202202
paymentPayload: payload,
203203
paymentRequirements: paymentRequirements,
204-
...(waitUtilParam ? { waitUtil: waitUtilParam } : {}),
204+
...(waitUntilParam ? { waitUntil: waitUntilParam } : {}),
205205
}),
206206
});
207207

0 commit comments

Comments
 (0)