Skip to content

Commit 1b718f0

Browse files
lint
1 parent 9253e16 commit 1b718f0

File tree

7 files changed

+64
-13
lines changed

7 files changed

+64
-13
lines changed

apps/playground-web/src/app/payments/x402/components/X402LeftSection.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { defineChain } from "thirdweb/chains";
66
import { BridgeNetworkSelector } from "@/components/blocks/NetworkSelectors";
77
import { Input } from "@/components/ui/input";
88
import { Label } from "@/components/ui/label";
9+
import {
10+
Select,
11+
SelectContent,
12+
SelectItem,
13+
SelectTrigger,
14+
SelectValue,
15+
} from "@/components/ui/select";
916
import { TokenSelector } from "@/components/ui/TokenSelector";
1017
import { THIRDWEB_CLIENT } from "@/lib/client";
1118
import type { TokenMetadata } from "@/lib/types";
@@ -38,6 +45,7 @@ export function X402LeftSection(props: {
3845
const tokenId = useId();
3946
const amountId = useId();
4047
const payToId = useId();
48+
const waitUntilId = useId();
4149

4250
const handleChainChange = (chainId: number) => {
4351
setSelectedChain(chainId);
@@ -81,6 +89,15 @@ export function X402LeftSection(props: {
8189
}));
8290
};
8391

92+
const handleWaitUntilChange = (
93+
value: "simulated" | "submitted" | "confirmed",
94+
) => {
95+
setOptions((v) => ({
96+
...v,
97+
waitUntil: value,
98+
}));
99+
};
100+
84101
return (
85102
<div className="space-y-6">
86103
<div>
@@ -146,6 +163,28 @@ export function X402LeftSection(props: {
146163
The wallet address that will receive the payment
147164
</p>
148165
</div>
166+
167+
{/* Wait Until selection */}
168+
<div className="flex flex-col gap-2">
169+
<Label htmlFor={waitUntilId}>Wait Until</Label>
170+
<Select
171+
value={options.waitUntil}
172+
onValueChange={handleWaitUntilChange}
173+
>
174+
<SelectTrigger className="bg-card">
175+
<SelectValue placeholder="Select wait condition" />
176+
</SelectTrigger>
177+
<SelectContent>
178+
<SelectItem value="simulated">Simulated</SelectItem>
179+
<SelectItem value="submitted">Submitted</SelectItem>
180+
<SelectItem value="confirmed">Confirmed</SelectItem>
181+
</SelectContent>
182+
</Select>
183+
<p className="text-sm text-muted-foreground">
184+
When to consider the payment settled: simulated (fastest),
185+
submitted (medium), or confirmed (most secure)
186+
</p>
187+
</div>
149188
</div>
150189
</div>
151190
</div>

apps/playground-web/src/app/payments/x402/components/X402Playground.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const defaultOptions: X402PlaygroundOptions = {
1414
tokenDecimals: token.decimals,
1515
amount: "0.01",
1616
payTo: "0x0000000000000000000000000000000000000000",
17+
waitUntil: "simulated",
1718
};
1819

1920
export function X402Playground() {

apps/playground-web/src/app/payments/x402/components/X402RightSection.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function X402RightSection(props: { options: X402PlaygroundOptions }) {
5151
searchParams.set("amount", props.options.amount);
5252
searchParams.set("tokenAddress", props.options.tokenAddress);
5353
searchParams.set("decimals", props.options.tokenDecimals.toString());
54+
searchParams.set("waitUntil", props.options.waitUntil);
5455

5556
const url =
5657
"/api/paywall" +
@@ -95,6 +96,7 @@ const client = createThirdwebClient({
9596
const thirdwebFacilitator = facilitator({
9697
client,
9798
serverWalletAddress: "0xYourServerWalletAddress",
99+
waitUtil: "${props.options.waitUntil}",
98100
});
99101
100102
export async function POST(request: Request) {
@@ -250,7 +252,7 @@ export async function POST(request: Request) {
250252
<CodeClient
251253
className="h-full rounded-none border-none"
252254
code={serverCode}
253-
lang="tsx"
255+
lang="typescript"
254256
/>
255257
</div>
256258
)}

apps/playground-web/src/app/payments/x402/components/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// const chain = arbitrumSepolia;
2-
31
import { base } from "thirdweb/chains";
42

53
export const chain = base;

apps/playground-web/src/app/payments/x402/components/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export type X402PlaygroundOptions = {
88
tokenDecimals: number;
99
amount: string;
1010
payTo: Address;
11+
waitUntil: "simulated" | "submitted" | "confirmed";
1112
};

apps/playground-web/src/middleware.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ const BACKEND_WALLET_ADDRESS = process.env.ENGINE_BACKEND_WALLET as string;
1313
const ENGINE_VAULT_ACCESS_TOKEN = process.env
1414
.ENGINE_VAULT_ACCESS_TOKEN as string;
1515
const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
16-
const twFacilitator = facilitator({
17-
baseUrl: `${API_URL}/v1/payments/x402`,
18-
client,
19-
serverWalletAddress: BACKEND_WALLET_ADDRESS,
20-
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
21-
waitUtil: "simulated",
22-
});
16+
function createFacilitator(
17+
waitUntil: "simulated" | "submitted" | "confirmed" = "simulated",
18+
) {
19+
return facilitator({
20+
baseUrl: `${API_URL}/v1/payments/x402`,
21+
client,
22+
serverWalletAddress: BACKEND_WALLET_ADDRESS,
23+
vaultAccessToken: ENGINE_VAULT_ACCESS_TOKEN,
24+
waitUtil: waitUntil,
25+
});
26+
}
2327

2428
export async function middleware(request: NextRequest) {
2529
const pathname = request.nextUrl.pathname;
@@ -41,6 +45,9 @@ export async function middleware(request: NextRequest) {
4145
const amount = queryParams.get("amount") || "0.01";
4246
const tokenAddress = queryParams.get("tokenAddress") || token.address;
4347
const decimals = queryParams.get("decimals") || token.decimals.toString();
48+
const waitUntil =
49+
(queryParams.get("waitUntil") as "simulated" | "submitted" | "confirmed") ||
50+
"simulated";
4451

4552
const result = await settlePayment({
4653
resourceUrl,
@@ -58,7 +65,7 @@ export async function middleware(request: NextRequest) {
5865
routeConfig: {
5966
description: "Access to paid content",
6067
},
61-
facilitator: twFacilitator,
68+
facilitator: createFacilitator(waitUntil),
6269
});
6370

6471
if (result.status === 200) {

packages/thirdweb/src/x402/common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getContract } from "../contract/contract.js";
88
import { isPermitSupported } from "../extensions/erc20/__generated__/IERC20Permit/write/permit.js";
99
import { isTransferWithAuthorizationSupported } from "../extensions/erc20/__generated__/USDC/write/transferWithAuthorization.js";
1010
import { getAddress } from "../utils/address.js";
11+
import { toUnits } from "../utils/units.js";
1112
import { decodePayment } from "./encode.js";
1213
import type { ThirdwebX402Facilitator } from "./facilitator.js";
1314
import {
@@ -23,7 +24,6 @@ import {
2324
type SupportedSignatureType,
2425
x402Version,
2526
} from "./types.js";
26-
import { toUnits } from "../utils/units.js";
2727

2828
type GetPaymentRequirementsResult = {
2929
status: 200;
@@ -223,7 +223,10 @@ async function processPriceToAtomicAmount(
223223
};
224224
}
225225
asset = defaultAsset;
226-
maxAmountRequired = toUnits(parsedUsdAmount.toString(), defaultAsset.decimals).toString();
226+
maxAmountRequired = toUnits(
227+
parsedUsdAmount.toString(),
228+
defaultAsset.decimals,
229+
).toString();
227230
} else {
228231
// Token amount in atomic units
229232
maxAmountRequired = price.amount;

0 commit comments

Comments
 (0)