Skip to content

Commit 6ddb15c

Browse files
[SDK] Optimize payment methods query by removing quotes and improving balance checks (#8493)
1 parent 551ec68 commit 6ddb15c

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useQuery } from "@tanstack/react-query";
22
import type { Quote } from "../../../bridge/index.js";
33
import { ApiError } from "../../../bridge/types/Errors.js";
4-
import type { Token, TokenWithPrices } from "../../../bridge/types/Token.js";
4+
import type { TokenWithPrices } from "../../../bridge/types/Token.js";
55
import type { ThirdwebClient } from "../../../client/client.js";
66
import { getThirdwebBaseUrl } from "../../../utils/domains.js";
77
import { getClientFetch } from "../../../utils/fetch.js";
@@ -29,7 +29,7 @@ import { useActiveWallet } from "./wallets/useActiveWallet.js";
2929
* ```
3030
*/
3131
export function usePaymentMethods(options: {
32-
destinationToken: Token;
32+
destinationToken: TokenWithPrices;
3333
destinationAmount: string;
3434
client: ThirdwebClient;
3535
payerWallet?: Wallet;
@@ -65,6 +65,8 @@ export function usePaymentMethods(options: {
6565
"amount",
6666
toUnits(destinationAmount, destinationToken.decimals).toString(),
6767
);
68+
// dont include quotes to speed up the query
69+
url.searchParams.set("includeQuotes", "false");
6870

6971
const clientFetch = getClientFetch(client);
7072
const response = await clientFetch(url.toString());
@@ -80,8 +82,9 @@ export function usePaymentMethods(options: {
8082

8183
const {
8284
data: allValidOriginTokens,
83-
}: { data: { quote: Quote; balance: string; token: TokenWithPrices }[] } =
84-
await response.json();
85+
}: {
86+
data: { quote?: Quote; balance: string; token: TokenWithPrices }[];
87+
} = await response.json();
8588

8689
// Sort by enough balance to pay THEN gross balance
8790
const validTokenQuotes = allValidOriginTokens.map((s) => ({
@@ -92,7 +95,7 @@ export function usePaymentMethods(options: {
9295
quote: s.quote,
9396
}));
9497

95-
const sufficientBalanceQuotes = validTokenQuotes
98+
const sortedValidTokenQuotes = validTokenQuotes
9699
.filter((s) => !!s.originToken.prices.USD)
97100
.sort((a, b) => {
98101
return (
@@ -114,18 +117,29 @@ export function usePaymentMethods(options: {
114117
)
115118
: [];
116119
const finalQuotes = supportedTokens
117-
? sufficientBalanceQuotes.filter((q) =>
120+
? sortedValidTokenQuotes.filter((q) =>
118121
tokensToInclude.find(
119122
(t) =>
120123
t.chainId === q.originToken.chainId &&
121124
t.address.toLowerCase() === q.originToken.address.toLowerCase(),
122125
),
123126
)
124-
: sufficientBalanceQuotes;
125-
return finalQuotes.map((x) => ({
126-
...x,
127-
action: "buy",
128-
}));
127+
: sortedValidTokenQuotes;
128+
129+
const requiredUsdValue =
130+
(destinationToken.prices?.["USD"] ?? 0) * Number(destinationAmount);
131+
132+
return finalQuotes.map((x) => {
133+
const tokenUsdValue =
134+
(x.originToken.prices?.["USD"] ?? 0) *
135+
Number(toTokens(x.balance, x.originToken.decimals));
136+
const hasEnoughBalance = tokenUsdValue >= requiredUsdValue;
137+
return {
138+
...x,
139+
action: "buy",
140+
hasEnoughBalance,
141+
};
142+
});
129143
},
130144
queryKey: [
131145
"payment-methods",

packages/thirdweb/src/react/web/ui/Bridge/payment-selection/PaymentSelection.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22
import { useEffect, useState } from "react";
33
import { trackPayEvent } from "../../../../../analytics/track/pay.js";
4-
import type { Token } from "../../../../../bridge/types/Token.js";
4+
import type { TokenWithPrices } from "../../../../../bridge/types/Token.js";
55
import { defineChain } from "../../../../../chains/utils.js";
66
import type { ThirdwebClient } from "../../../../../client/client.js";
77
import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
@@ -26,7 +26,7 @@ type PaymentSelectionProps = {
2626
/**
2727
* The destination token to bridge to
2828
*/
29-
destinationToken: Token;
29+
destinationToken: TokenWithPrices;
3030

3131
/**
3232
* The destination amount to bridge

packages/thirdweb/src/react/web/ui/Bridge/payment-selection/TokenSelection.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ function PaymentMethodTokenRow({
4747
}: PaymentMethodTokenRowProps) {
4848
const theme = useCustomTheme();
4949

50-
const displayOriginAmount = paymentMethod.quote.originAmount;
51-
const hasEnoughBalance = displayOriginAmount
52-
? paymentMethod.balance >= displayOriginAmount
53-
: false;
50+
const hasEnoughBalance = paymentMethod.hasEnoughBalance;
5451
const currencyPrice = paymentMethod.originToken.prices[currency || "USD"];
5552

5653
return (

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/SwapWidget.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ function SwapWidgetContent(
451451
balance: screen.sellTokenBalance,
452452
originToken: screen.sellToken,
453453
action: screen.mode,
454+
hasEnoughBalance: true,
454455
}}
455456
preparedQuote={screen.preparedQuote}
456457
currency={props.currency}

packages/thirdweb/src/react/web/ui/Bridge/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export type PaymentMethod =
3737
payerWallet: Wallet;
3838
originToken: TokenWithPrices;
3939
balance: bigint;
40-
quote: Quote;
40+
quote?: Quote;
41+
hasEnoughBalance: boolean;
4142
}
4243
| {
4344
type: "fiat";

0 commit comments

Comments
 (0)