11import { useQuery } from "@tanstack/react-query" ;
22import type { Quote } from "../../../bridge/index.js" ;
33import { 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" ;
55import type { ThirdwebClient } from "../../../client/client.js" ;
66import { getThirdwebBaseUrl } from "../../../utils/domains.js" ;
77import { getClientFetch } from "../../../utils/fetch.js" ;
@@ -29,7 +29,7 @@ import { useActiveWallet } from "./wallets/useActiveWallet.js";
2929 * ```
3030 */
3131export 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" ,
0 commit comments