Skip to content

Commit 53b6fcf

Browse files
committed
fix: skip payment method selection when balance is sufficient
1 parent 7883127 commit 53b6fcf

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

.changeset/fluffy-bobcats-walk.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+
Skips payment selection in the TransactionWidget if the user's balance is sufficient to complete the transaction.

packages/thirdweb/src/react/core/machines/paymentMachine.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface PaymentMachineContext {
7070
/**
7171
* Events that can be sent to the payment machine
7272
*/
73-
type PaymentMachineEvent =
73+
export type PaymentMachineEvent = { state?: PaymentMachineState } & ( // Current state override to get out of overly strict switch statement hell
7474
| {
7575
type: "DESTINATION_CONFIRMED";
7676
destinationToken: Token;
@@ -89,7 +89,8 @@ type PaymentMachineEvent =
8989
| { type: "CONTINUE_TO_TRANSACTION" }
9090
| { type: "RETRY" }
9191
| { type: "RESET" }
92-
| { type: "BACK" };
92+
| { type: "BACK" }
93+
);
9394

9495
type PaymentMachineState =
9596
| "init"
@@ -116,7 +117,8 @@ export function usePaymentMachine(
116117

117118
const send = useCallback(
118119
(event: PaymentMachineEvent) => {
119-
setCurrentState((state) => {
120+
setCurrentState((currentState) => {
121+
const state = event.state ?? currentState;
120122
setContext((ctx) => {
121123
switch (state) {
122124
case "init":

packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export function BridgeOrchestrator({
288288
client={client}
289289
connectOptions={modifiedConnectOptions}
290290
onContinue={handleRequirementsResolved}
291+
sendEvent={send}
291292
showThirdwebBranding={showThirdwebBranding}
292293
uiOptions={uiOptions}
293294
/>

packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22
import { useQuery } from "@tanstack/react-query";
3+
import type { PaymentMachineEvent } from "src/react/core/machines/paymentMachine.js";
34
import type { Token } from "../../../../bridge/index.js";
45
import type { ThirdwebClient } from "../../../../client/client.js";
56
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
@@ -48,6 +49,11 @@ export interface TransactionPaymentProps {
4849
*/
4950
onContinue: (amount: string, token: Token, receiverAddress: Address) => void;
5051

52+
/**
53+
* Send arbitrary payment events for UI flow control
54+
*/
55+
sendEvent: (event: PaymentMachineEvent) => void;
56+
5157
/**
5258
* Connect options for wallet connection
5359
*/
@@ -64,6 +70,7 @@ export function TransactionPayment({
6470
uiOptions,
6571
client,
6672
onContinue,
73+
sendEvent,
6774
connectOptions,
6875
showThirdwebBranding = true,
6976
}: TransactionPaymentProps) {
@@ -378,6 +385,19 @@ export function TransactionPayment({
378385
return;
379386
}
380387

388+
// If the user has enough to pay, skip the payment step altogether
389+
if (
390+
userBalance &&
391+
Number(userBalance) >
392+
Number(transactionDataQuery.data.totalCost)
393+
) {
394+
sendEvent({
395+
state: "success",
396+
type: "CONTINUE_TO_TRANSACTION",
397+
});
398+
return;
399+
}
400+
381401
// otherwise, use the full transaction cost
382402
onContinue(
383403
transactionDataQuery.data.totalCost,

packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ type UIOptionsResult =
218218
* @example
219219
* ### Default configuration
220220
*
221-
* By default, the `TransactionWidget` component will allows users to fund their wallets with crypto or fiat on any of the supported chains..
221+
* By default, the `TransactionWidget` component allows users to fund their wallets with crypto or fiat on any of the supported chains.
222222
*
223223
* ```tsx
224224
* <TransactionWidget

0 commit comments

Comments
 (0)