1- import { useProjectAccessKey } from '@0xsequence/connect'
2- import {
3- compareAddress ,
4- ContractVerificationStatus ,
5- TRANSACTION_CONFIRMATIONS_DEFAULT ,
6- formatDisplay ,
7- sendTransactions
8- } from '@0xsequence/connect'
9- import {
10- useIndexerClient ,
11- useConfig ,
12- useClearCachedBalances ,
13- useGetTokenBalancesSummary ,
14- useGetSwapPrices ,
15- useGetContractInfo ,
16- useGetSwapQuote
17- } from '@0xsequence/hooks'
18- import { TransactionStatus } from '@0xsequence/indexer'
1+ import { compareAddress , ContractVerificationStatus , formatDisplay , sendTransactions } from '@0xsequence/connect'
2+ import { useIndexerClient , useGetTokenBalancesSummary , useGetSwapPrices , useGetSwapQuote } from '@0xsequence/hooks'
193import { ContractInfo , TokenMetadata } from '@0xsequence/metadata'
204import { findSupportedNetwork } from '@0xsequence/network'
21- import pako from 'pako'
22- import React , { useEffect , useState } from 'react'
5+ import { useState } from 'react'
236import { Hex , encodeFunctionData , formatUnits , zeroAddress } from 'viem'
247import { usePublicClient , useAccount , useReadContract , useWalletClient } from 'wagmi'
258
@@ -65,41 +48,31 @@ export interface UseCryptoPaymentReturn {
6548interface CryptoOptions {
6649 chainId : number
6750 currencyAddress : string
51+ currencyName : string
6852 totalPriceRaw : string
6953 symbol : string
7054 decimals : number
7155 totalPriceDisplay : string
7256 currrencyLogoUrl ?: string
73- isBalanceSufficient : boolean
57+ isInsufficientFunds : boolean
58+ isSelected : boolean
7459}
7560
7661export const useCryptoPayment = ( {
7762 chain,
7863 currencyAddress,
7964 totalPriceRaw,
80- collectible,
81- collectionAddress,
82- recipientAddress,
8365 targetContractAddress,
8466 txData,
8567 transactionConfirmations,
8668 onSuccess,
8769 onError,
8870 currencyInfo,
89- tokenMetadatas,
90- dataCollectionInfo,
91- isLoadingCollectionInfo,
92- errorCollectionInfo,
93- isLoadingTokenMetadatas,
94- errorTokenMetadata,
9571 isLoadingCurrencyInfo,
9672 errorCurrencyInfo
9773} : UseCryptoPaymentArgs ) : UseCryptoPaymentReturn => {
98- const [ actionInProgress , setActionInProgress ] = useState ( false )
99- const [ actionError , setActionError ] = useState < Error | null > ( null )
10074 const [ selectedCurrencyAddress , setSelectedCurrencyAddress ] = useState < string | undefined > ( undefined )
10175 const { address : userAddress , connector } = useAccount ( )
102- const { clearCachedBalances } = useClearCachedBalances ( )
10376 const network = findSupportedNetwork ( chain )
10477 const chainId = network ?. chainId || 137
10578 const isNativeCurrency = compareAddress ( currencyAddress , zeroAddress )
@@ -141,7 +114,11 @@ export const useCryptoPayment = ({
141114 const buyCurrencyAddress = currencyAddress
142115 const sellCurrencyAddress = selectedCurrencyAddress || ''
143116
144- const { data : swapPrices = [ ] , isLoading : swapPricesIsLoading } = useGetSwapPrices ( {
117+ const {
118+ data : swapPrices = [ ] ,
119+ isLoading : swapPricesIsLoading ,
120+ error : swapPricesError
121+ } = useGetSwapPrices ( {
145122 userAddress : userAddress ?? '' ,
146123 buyCurrencyAddress,
147124 chainId : chainId ,
@@ -176,12 +153,14 @@ export const useCryptoPayment = ({
176153 const mainCurrencyOption = {
177154 chainId,
178155 currencyAddress,
156+ currencyName : currencyInfo ?. name || 'unknown' ,
179157 totalPriceRaw : totalPriceRaw ,
180158 decimals : currencyDecimals || 18 ,
181159 totalPriceDisplay : priceDisplay ,
182160 currrencyLogoUrl : currencyInfo ?. logoURI ,
183161 symbol : currencySymbol || '' ,
184- isBalanceSufficient : Number ( mainCurrencyBalance ) > Number ( totalPriceRaw )
162+ isInsufficientFunds : Number ( mainCurrencyBalance ) < Number ( totalPriceRaw ) ,
163+ isSelected : compareAddress ( currencyAddress , selectedCurrencyAddress || '' )
185164 }
186165
187166 const swapOptions = swapPrices . map ( swapPrice => {
@@ -197,13 +176,15 @@ export const useCryptoPayment = ({
197176 return {
198177 chainId,
199178 currencyAddress : swapQuoteAddress ,
179+ currencyName : swapPrice . info ?. name || 'unknown' ,
200180 totalPriceRaw : swapPrice . price . price ,
201181 totalPriceDisplay : swapQuotePriceDisplay ,
202182 currrencyLogoUrl : swapPrice . info ?. logoURI ,
203183 symbol : swapPrice . info ?. symbol || '' ,
204184 decimals : swapPrice . info ?. decimals || 18 ,
205185 // The balance check is done at the API level
206- isBalanceSufficient : false
186+ isInsufficientFunds : false ,
187+ isSelected : compareAddress ( swapQuoteAddress , selectedCurrencyAddress || '' )
207188 }
208189 } )
209190
@@ -370,8 +351,8 @@ export const useCryptoPayment = ({
370351 return {
371352 cryptoOptions : {
372353 data : [ mainCurrencyOption , ...swapOptions ] ,
373- isLoading : isLoadingCurrencyInfo || isLoadingTokenMetadatas || isLoadingCollectionInfo ,
374- error : errorCurrencyInfo || errorTokenMetadata || errorCollectionInfo
354+ isLoading : isLoadingCurrencyInfo || swapPricesIsLoading ,
355+ error : errorCurrencyInfo || swapPricesError
375356 } ,
376357 purchaseAction : {
377358 action : purchaseAction ,
0 commit comments