Skip to content

Commit 11629cf

Browse files
[thirdweb] Fix waitUntil facilitator parameter not being respected
1 parent 34c5d24 commit 11629cf

File tree

5 files changed

+110
-95
lines changed

5 files changed

+110
-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: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,86 @@
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+
7+
const client = createThirdwebClient({
8+
secretKey: process.env.THIRDWEB_SECRET_KEY as string,
9+
});
10+
11+
const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string;
12+
// const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_SMART_WALLET as string;
13+
const ENGINE_VAULT_ACCESS_TOKEN = process.env
14+
.ENGINE_VAULT_ACCESS_TOKEN as string;
15+
const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
16+
17+
const twFacilitator = facilitator({
18+
baseUrl: `${API_URL}/v1/payments/x402`,
19+
client,
20+
serverWalletAddress: BACKEND_WALLET_ADDRESS,
21+
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
22+
});
223
// Allow streaming responses up to 5 minutes
324
export const maxDuration = 300;
425

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

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)