Skip to content

Commit e114b2c

Browse files
authored
[SDK] Fix: Skip payment method selection when balance is sufficient (#7813)
1 parent d812cc3 commit e114b2c

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
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/bridge/Token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ThirdwebClient } from "../client/client.js";
2-
import { getThirdwebBaseUrl } from "../utils/domains.js";
2+
import { getThirdwebDomains } from "../utils/domains.js";
33
import { getClientFetch } from "../utils/fetch.js";
44
import { ApiError } from "./types/Errors.js";
55
import type { Token } from "./types/Token.js";
@@ -133,7 +133,7 @@ export async function tokens(options: tokens.Options): Promise<tokens.Result> {
133133
options;
134134

135135
const clientFetch = getClientFetch(client);
136-
const url = new URL(`${getThirdwebBaseUrl("bridge")}/v1/tokens`);
136+
const url = new URL(`${getThirdwebDomains().bridge}/v1/tokens`);
137137

138138
if (chainId !== null && chainId !== undefined) {
139139
url.searchParams.set("chainId", chainId.toString());
@@ -229,7 +229,7 @@ export async function add(options: add.Options): Promise<add.Result> {
229229
const { client, chainId, tokenAddress } = options;
230230

231231
const clientFetch = getClientFetch(client);
232-
const url = `${getThirdwebBaseUrl("bridge")}/v1/tokens`;
232+
const url = `${getThirdwebDomains().bridge}/v1/tokens`;
233233

234234
const requestBody = {
235235
chainId,

packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export function useTransactionDetails({
6464
return useQuery({
6565
enabled: !!transaction.to && !!chainMetadata.data,
6666
queryFn: async (): Promise<TransactionDetails> => {
67-
// Create contract instance for metadata fetching
6867
const contract = getContract({
6968
address: transaction.to as string,
7069
chain: transaction.chain,
@@ -84,7 +83,7 @@ export function useTransactionDetails({
8483
client,
8584
erc20Value?.tokenAddress || NATIVE_TOKEN_ADDRESS,
8685
transaction.chain.id,
87-
).catch(() => null),
86+
),
8887
hasSponsoredTransactions
8988
? 0n
9089
: getTransactionGasCost(transaction).catch(() => null),

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

Lines changed: 3 additions & 1 deletion
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 =
7474
| {
7575
type: "DESTINATION_CONFIRMED";
7676
destinationToken: Token;
@@ -230,6 +230,8 @@ export function usePaymentMachine(
230230
if (event.type === "DESTINATION_CONFIRMED")
231231
return "methodSelection";
232232
if (event.type === "ERROR_OCCURRED") return "error";
233+
if (event.type === "CONTINUE_TO_TRANSACTION")
234+
return "post-buy-transaction";
233235
break;
234236

235237
case "methodSelection":

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useChainMetadata } from "../../../core/hooks/others/useChainQuery.js";
2020
import { useTransactionDetails } from "../../../core/hooks/useTransactionDetails.js";
2121
import { useActiveAccount } from "../../../core/hooks/wallets/useActiveAccount.js";
2222
import { useActiveWallet } from "../../../core/hooks/wallets/useActiveWallet.js";
23+
import type { PaymentMachineEvent } from "../../../core/machines/paymentMachine.js";
2324
import { ConnectButton } from "../ConnectWallet/ConnectButton.js";
2425
import { PoweredByThirdweb } from "../ConnectWallet/PoweredByTW.js";
2526
import { Container, Line } from "../components/basic.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,18 @@ 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+
type: "CONTINUE_TO_TRANSACTION",
396+
});
397+
return;
398+
}
399+
381400
// otherwise, use the full transaction cost
382401
onContinue(
383402
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

packages/thirdweb/src/stories/Bridge/TransactionPayment.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const TransactionPaymentWithTheme = (
2929
const meta = {
3030
args: {
3131
client: storyClient,
32+
sendEvent: (_event) => {},
3233
onContinue: (_amount, _token, _receiverAddress) => {},
3334
theme: "dark",
3435
uiOptions: TRANSACTION_UI_OPTIONS.ethTransfer,

0 commit comments

Comments
 (0)