0xtrails 0.6.6 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aave.d.ts +10 -2
- package/dist/aave.d.ts.map +1 -1
- package/dist/analytics.d.ts +26 -0
- package/dist/analytics.d.ts.map +1 -1
- package/dist/{ccip-CbJrlK-L.js → ccip-fConRNoG.js} +21 -21
- package/dist/chains.d.ts +23 -8
- package/dist/chains.d.ts.map +1 -1
- package/dist/constants.d.ts +5 -5
- package/dist/constants.d.ts.map +1 -1
- package/dist/customTokens.d.ts +12 -0
- package/dist/customTokens.d.ts.map +1 -0
- package/dist/decoders.d.ts +2 -2
- package/dist/decoders.d.ts.map +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/fees.d.ts +37 -2
- package/dist/fees.d.ts.map +1 -1
- package/dist/gasless.d.ts +15 -36
- package/dist/gasless.d.ts.map +1 -1
- package/dist/{index-w7_dK4c5.js → index-BbajxCG_.js} +59269 -77146
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +828 -359
- package/dist/indexerClient.d.ts.map +1 -1
- package/dist/intentReceiptMonitor.d.ts +1 -1
- package/dist/intentReceiptMonitor.d.ts.map +1 -1
- package/dist/intentReceiptPoller.d.ts +1 -1
- package/dist/intentReceiptPoller.d.ts.map +1 -1
- package/dist/intents.d.ts +3 -2
- package/dist/intents.d.ts.map +1 -1
- package/dist/mode.d.ts +1 -1
- package/dist/mode.d.ts.map +1 -1
- package/dist/mutations.d.ts +2 -2
- package/dist/mutations.d.ts.map +1 -1
- package/dist/prepareSend.d.ts +2 -2
- package/dist/prepareSend.d.ts.map +1 -1
- package/dist/prices.d.ts +1 -1
- package/dist/prices.d.ts.map +1 -1
- package/dist/sequenceWallet.d.ts +2 -2
- package/dist/sequenceWallet.d.ts.map +1 -1
- package/dist/time.d.ts +6 -0
- package/dist/time.d.ts.map +1 -1
- package/dist/tokenBalances.d.ts +40 -25
- package/dist/tokenBalances.d.ts.map +1 -1
- package/dist/tokens.d.ts +54 -14
- package/dist/tokens.d.ts.map +1 -1
- package/dist/trailsClient.d.ts +1 -1
- package/dist/trailsClient.d.ts.map +1 -1
- package/dist/trailsRouter.d.ts +2 -1
- package/dist/trailsRouter.d.ts.map +1 -1
- package/dist/transactionIntent/deposits/depositOrchestrator.d.ts +3 -2
- package/dist/transactionIntent/deposits/depositOrchestrator.d.ts.map +1 -1
- package/dist/transactionIntent/deposits/gaslessDeposit.d.ts +2 -1
- package/dist/transactionIntent/deposits/gaslessDeposit.d.ts.map +1 -1
- package/dist/transactionIntent/execution/transactionState.d.ts +1 -1
- package/dist/transactionIntent/execution/transactionState.d.ts.map +1 -1
- package/dist/transactionIntent/handlers/crossChain.d.ts +5 -3
- package/dist/transactionIntent/handlers/crossChain.d.ts.map +1 -1
- package/dist/transactionIntent/handlers/sameChainSameToken.d.ts +5 -3
- package/dist/transactionIntent/handlers/sameChainSameToken.d.ts.map +1 -1
- package/dist/transactionIntent/quote/feeExtractors.d.ts +1 -6
- package/dist/transactionIntent/quote/feeExtractors.d.ts.map +1 -1
- package/dist/transactionIntent/quote/normalizeQuote.d.ts +4 -2
- package/dist/transactionIntent/quote/normalizeQuote.d.ts.map +1 -1
- package/dist/transactionIntent/quote/quoteHelpers.d.ts +1 -1
- package/dist/transactionIntent/quote/quoteHelpers.d.ts.map +1 -1
- package/dist/transactionIntent/types.d.ts +28 -5
- package/dist/transactionIntent/types.d.ts.map +1 -1
- package/dist/transactionIntent/utils/testnetHelpers.d.ts +0 -1
- package/dist/transactionIntent/utils/testnetHelpers.d.ts.map +1 -1
- package/dist/transactionIntent/validators.d.ts +0 -2
- package/dist/transactionIntent/validators.d.ts.map +1 -1
- package/dist/transactions.d.ts +2 -2
- package/dist/transactions.d.ts.map +1 -1
- package/dist/utils.d.ts +7 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/wallets.d.ts +1 -0
- package/dist/wallets.d.ts.map +1 -1
- package/dist/widget/components/AccountIntentTransactionHistory.d.ts.map +1 -1
- package/dist/widget/components/AddressWalletIcon.d.ts +6 -0
- package/dist/widget/components/AddressWalletIcon.d.ts.map +1 -0
- package/dist/widget/components/ChainFilterDropdown.d.ts +2 -6
- package/dist/widget/components/ChainFilterDropdown.d.ts.map +1 -1
- package/dist/widget/components/ChainImage.d.ts.map +1 -1
- package/dist/widget/components/ChainList.d.ts +0 -5
- package/dist/widget/components/ChainList.d.ts.map +1 -1
- package/dist/widget/components/ClassicSwap.d.ts +6 -6
- package/dist/widget/components/ClassicSwap.d.ts.map +1 -1
- package/dist/widget/components/ConnectWallet.d.ts.map +1 -1
- package/dist/widget/components/ConnectedWallets.d.ts.map +1 -1
- package/dist/widget/components/DebugMenu.d.ts +1 -1
- package/dist/widget/components/DebugMenu.d.ts.map +1 -1
- package/dist/widget/components/DebugScreensList.d.ts.map +1 -1
- package/dist/widget/components/DepositTracker.d.ts.map +1 -1
- package/dist/widget/components/Earn.d.ts +5 -5
- package/dist/widget/components/Earn.d.ts.map +1 -1
- package/dist/widget/components/FeeOption.d.ts +1 -1
- package/dist/widget/components/FeeOption.d.ts.map +1 -1
- package/dist/widget/components/FeeOptions.d.ts +2 -2
- package/dist/widget/components/FeeOptions.d.ts.map +1 -1
- package/dist/widget/components/Footer.d.ts +1 -1
- package/dist/widget/components/Footer.d.ts.map +1 -1
- package/dist/widget/components/Fund.d.ts +5 -5
- package/dist/widget/components/Fund.d.ts.map +1 -1
- package/dist/widget/components/FundMethods.d.ts +0 -1
- package/dist/widget/components/FundMethods.d.ts.map +1 -1
- package/dist/widget/components/FundSwap.d.ts +6 -6
- package/dist/widget/components/FundSwap.d.ts.map +1 -1
- package/dist/widget/components/HookModalContent.d.ts +8 -0
- package/dist/widget/components/HookModalContent.d.ts.map +1 -0
- package/dist/widget/components/OriginSelectionAmount.d.ts +11 -0
- package/dist/widget/components/OriginSelectionAmount.d.ts.map +1 -0
- package/dist/widget/components/Pay.d.ts +5 -5
- package/dist/widget/components/Pay.d.ts.map +1 -1
- package/dist/widget/components/PoolDeposit.d.ts +5 -5
- package/dist/widget/components/PoolDeposit.d.ts.map +1 -1
- package/dist/widget/components/PoolWithdraw.d.ts +3 -3
- package/dist/widget/components/PoolWithdraw.d.ts.map +1 -1
- package/dist/widget/components/QuoteDetails.d.ts.map +1 -1
- package/dist/widget/components/Receipt.d.ts +2 -1
- package/dist/widget/components/Receipt.d.ts.map +1 -1
- package/dist/widget/components/RecentTokens.d.ts +4 -4
- package/dist/widget/components/RecentTokens.d.ts.map +1 -1
- package/dist/widget/components/RecipientSelectorButton.d.ts.map +1 -1
- package/dist/widget/components/ShadowPortal.d.ts +6 -0
- package/dist/widget/components/ShadowPortal.d.ts.map +1 -0
- package/dist/widget/components/Swap.d.ts +6 -6
- package/dist/widget/components/Swap.d.ts.map +1 -1
- package/dist/widget/components/ThemeProvider.d.ts +1 -1
- package/dist/widget/components/ThemeProvider.d.ts.map +1 -1
- package/dist/widget/components/TokenList.d.ts +3 -4
- package/dist/widget/components/TokenList.d.ts.map +1 -1
- package/dist/widget/components/TokenSelector.d.ts +3 -4
- package/dist/widget/components/TokenSelector.d.ts.map +1 -1
- package/dist/widget/components/Tooltip.d.ts +6 -1
- package/dist/widget/components/Tooltip.d.ts.map +1 -1
- package/dist/widget/components/TrailsHookModal.d.ts +10 -0
- package/dist/widget/components/TrailsHookModal.d.ts.map +1 -0
- package/dist/widget/components/WaasFeeOptions.d.ts +3 -0
- package/dist/widget/components/WaasFeeOptions.d.ts.map +1 -1
- package/dist/widget/components/WalletConfirmation.d.ts.map +1 -1
- package/dist/widget/components/WalletConnect.d.ts.map +1 -1
- package/dist/widget/components/WidgetProviders.d.ts +14 -0
- package/dist/widget/components/WidgetProviders.d.ts.map +1 -0
- package/dist/widget/css/compiled.css +1 -1
- package/dist/widget/hooks/useAddressWalletIcon.d.ts +10 -0
- package/dist/widget/hooks/useAddressWalletIcon.d.ts.map +1 -0
- package/dist/widget/hooks/useBalanceVisible.d.ts +1 -1
- package/dist/widget/hooks/useBalanceVisible.d.ts.map +1 -1
- package/dist/widget/hooks/useChainFilter.d.ts +1 -1
- package/dist/widget/hooks/useChainFilter.d.ts.map +1 -1
- package/dist/widget/hooks/useCheckout.d.ts +13 -1
- package/dist/widget/hooks/useCheckout.d.ts.map +1 -1
- package/dist/widget/hooks/useConnector.d.ts +4 -0
- package/dist/widget/hooks/useConnector.d.ts.map +1 -0
- package/dist/widget/hooks/useCurrentScreen.d.ts +1 -1
- package/dist/widget/hooks/useCurrentScreen.d.ts.map +1 -1
- package/dist/widget/hooks/useCustomTokenFetch.d.ts +19 -0
- package/dist/widget/hooks/useCustomTokenFetch.d.ts.map +1 -0
- package/dist/widget/hooks/useCustomTokenSearch.d.ts +20 -0
- package/dist/widget/hooks/useCustomTokenSearch.d.ts.map +1 -0
- package/dist/widget/hooks/useDebounce.d.ts +10 -0
- package/dist/widget/hooks/useDebounce.d.ts.map +1 -0
- package/dist/widget/hooks/useDebugScreens.d.ts +7 -2
- package/dist/widget/hooks/useDebugScreens.d.ts.map +1 -1
- package/dist/widget/hooks/useDefaultTokenSelection.d.ts +3 -19
- package/dist/widget/hooks/useDefaultTokenSelection.d.ts.map +1 -1
- package/dist/widget/hooks/useDestinationSelectedToken.d.ts +1 -14
- package/dist/widget/hooks/useDestinationSelectedToken.d.ts.map +1 -1
- package/dist/widget/hooks/useEarnPool.d.ts +1 -1
- package/dist/widget/hooks/useEarnPool.d.ts.map +1 -1
- package/dist/widget/hooks/useIntentTransactionHistory.d.ts.map +1 -1
- package/dist/widget/hooks/useIsMobile.d.ts +5 -0
- package/dist/widget/hooks/useIsMobile.d.ts.map +1 -0
- package/dist/widget/hooks/useMode.d.ts +2 -2
- package/dist/widget/hooks/useMode.d.ts.map +1 -1
- package/dist/widget/hooks/useOriginSelectedToken.d.ts +2 -15
- package/dist/widget/hooks/useOriginSelectedToken.d.ts.map +1 -1
- package/dist/widget/hooks/usePayMessage.d.ts.map +1 -1
- package/dist/widget/hooks/usePriceImpactWarning.d.ts +1 -1
- package/dist/widget/hooks/usePriceImpactWarning.d.ts.map +1 -1
- package/dist/widget/hooks/useQuote.d.ts +173 -4
- package/dist/widget/hooks/useQuote.d.ts.map +1 -1
- package/dist/widget/hooks/useRecentTokens.d.ts +3 -3
- package/dist/widget/hooks/useRecentTokens.d.ts.map +1 -1
- package/dist/widget/hooks/useRecipients.d.ts +1 -1
- package/dist/widget/hooks/useRecipients.d.ts.map +1 -1
- package/dist/widget/hooks/useSelectedFeeOption.d.ts +2 -2
- package/dist/widget/hooks/useSelectedFeeOption.d.ts.map +1 -1
- package/dist/widget/hooks/useSelectedFundMethod.d.ts +1 -1
- package/dist/widget/hooks/useSelectedFundMethod.d.ts.map +1 -1
- package/dist/widget/hooks/useSelectedRecipient.d.ts +1 -1
- package/dist/widget/hooks/useSelectedRecipient.d.ts.map +1 -1
- package/dist/widget/hooks/useSendForm.d.ts +9 -31
- package/dist/widget/hooks/useSendForm.d.ts.map +1 -1
- package/dist/widget/hooks/useSwapAmount.d.ts +1 -1
- package/dist/widget/hooks/useSwapAmount.d.ts.map +1 -1
- package/dist/widget/hooks/useTheme.d.ts +1 -1
- package/dist/widget/hooks/useTheme.d.ts.map +1 -1
- package/dist/widget/hooks/useTokenList.d.ts +7 -31
- package/dist/widget/hooks/useTokenList.d.ts.map +1 -1
- package/dist/widget/hooks/useTrailsSendTransaction.d.ts +83 -0
- package/dist/widget/hooks/useTrailsSendTransaction.d.ts.map +1 -0
- package/dist/widget/hooks/useWalletConnectUri.d.ts +11 -0
- package/dist/widget/hooks/useWalletConnectUri.d.ts.map +1 -0
- package/dist/widget/hooks/useWidgetProps.d.ts +5 -0
- package/dist/widget/hooks/useWidgetProps.d.ts.map +1 -1
- package/dist/widget/index.d.ts +2 -0
- package/dist/widget/index.d.ts.map +1 -1
- package/dist/widget/index.js +8 -5
- package/dist/widget/providers/TrailsModalProvider.d.ts +65 -0
- package/dist/widget/providers/TrailsModalProvider.d.ts.map +1 -0
- package/dist/widget/providers/TrailsProvider.d.ts +1 -1
- package/dist/widget/providers/TrailsProvider.d.ts.map +1 -1
- package/dist/widget/types.d.ts +11 -18
- package/dist/widget/types.d.ts.map +1 -1
- package/dist/widget/widget.d.ts +20 -11
- package/dist/widget/widget.d.ts.map +1 -1
- package/package.json +45 -49
- package/src/aave.ts +387 -138
- package/src/analytics.ts +208 -2
- package/src/chains.ts +65 -64
- package/src/constants.ts +18 -14
- package/src/customTokens.ts +151 -0
- package/src/decoders.ts +4 -7
- package/src/error.ts +7 -3
- package/src/fees.ts +239 -125
- package/src/gasless.ts +54 -108
- package/src/index.ts +29 -9
- package/src/indexerClient.ts +2 -0
- package/src/intentReceiptMonitor.ts +1 -4
- package/src/intentReceiptPoller.ts +2 -2
- package/src/intents.ts +16 -5
- package/src/mode.ts +1 -1
- package/src/mutations.ts +7 -3
- package/src/prepareSend.ts +19 -14
- package/src/prices.ts +1 -1
- package/src/sequenceWallet.ts +2 -2
- package/src/time.ts +28 -0
- package/src/tokenBalances.ts +348 -153
- package/src/tokens.ts +393 -142
- package/src/trailsClient.ts +1 -1
- package/src/trailsRouter.ts +4 -5
- package/src/transactionIntent/deposits/depositOrchestrator.ts +6 -2
- package/src/transactionIntent/deposits/gaslessDeposit.ts +13 -7
- package/src/transactionIntent/deposits/standardDeposit.ts +1 -1
- package/src/transactionIntent/execution/transactionState.ts +1 -1
- package/src/transactionIntent/handlers/crossChain.ts +75 -37
- package/src/transactionIntent/handlers/sameChainSameToken.ts +66 -20
- package/src/transactionIntent/quote/feeExtractors.ts +1 -29
- package/src/transactionIntent/quote/normalizeQuote.ts +99 -7
- package/src/transactionIntent/quote/quoteHelpers.ts +1 -1
- package/src/transactionIntent/types.ts +31 -6
- package/src/transactionIntent/utils/testnetHelpers.ts +0 -5
- package/src/transactionIntent/validators.ts +0 -30
- package/src/transactions.ts +3 -3
- package/src/utils.ts +18 -0
- package/src/wallets.ts +32 -10
- package/src/widget/compiled.css +1 -1
- package/src/widget/components/AccountIntentTransactionHistory.tsx +2 -1
- package/src/widget/components/AccountIntentTransactionHistoryButton.tsx +2 -2
- package/src/widget/components/AddressWalletIcon.tsx +29 -0
- package/src/widget/components/ChainFilterDropdown.tsx +2 -8
- package/src/widget/components/ChainImage.tsx +8 -5
- package/src/widget/components/ChainList.tsx +6 -8
- package/src/widget/components/ClassicSwap.tsx +93 -85
- package/src/widget/components/ConnectWallet.tsx +1 -2
- package/src/widget/components/ConnectedWallets.tsx +17 -4
- package/src/widget/components/DebugMenu.tsx +2 -2
- package/src/widget/components/DebugScreensList.tsx +0 -1
- package/src/widget/components/DepositTracker.tsx +20 -34
- package/src/widget/components/Earn.tsx +7 -6
- package/src/widget/components/FeeOption.tsx +4 -4
- package/src/widget/components/FeeOptions.tsx +19 -39
- package/src/widget/components/Footer.tsx +1 -1
- package/src/widget/components/Fund.tsx +23 -119
- package/src/widget/components/FundMethods.tsx +9 -6
- package/src/widget/components/FundSwap.tsx +6 -5
- package/src/widget/components/FundingMethodSelectorButton.tsx +2 -2
- package/src/widget/components/HookModalContent.tsx +306 -0
- package/src/widget/components/Modal.tsx +1 -1
- package/src/widget/components/OriginSelectionAmount.tsx +135 -0
- package/src/widget/components/Pay.tsx +66 -124
- package/src/widget/components/PoolDeposit.tsx +11 -55
- package/src/widget/components/PoolWithdraw.tsx +3 -3
- package/src/widget/components/QuoteDetails.tsx +473 -728
- package/src/widget/components/Receipt.tsx +74 -7
- package/src/widget/components/RecentTokens.tsx +8 -8
- package/src/widget/components/RecipientSelectorButton.tsx +4 -2
- package/src/widget/components/ScreenHeader.tsx +2 -2
- package/src/widget/components/SearchInputField.tsx +1 -1
- package/src/widget/components/ShadowPortal.tsx +58 -0
- package/src/widget/components/Swap.tsx +6 -5
- package/src/widget/components/ThemeProvider.tsx +1 -1
- package/src/widget/components/TokenList.tsx +3 -4
- package/src/widget/components/TokenSelector.tsx +211 -80
- package/src/widget/components/Tooltip.tsx +18 -7
- package/src/widget/components/TrailsHookModal.tsx +118 -0
- package/src/widget/components/WaasFeeOptions.tsx +333 -138
- package/src/widget/components/WalletConfirmation.tsx +7 -2
- package/src/widget/components/WalletConnect.tsx +197 -235
- package/src/widget/components/WidgetProviders.tsx +75 -0
- package/src/widget/hooks/useAddressWalletIcon.ts +53 -0
- package/src/widget/hooks/useBalanceVisible.tsx +1 -1
- package/src/widget/hooks/useChainFilter.tsx +1 -1
- package/src/widget/hooks/useCheckout.ts +13 -1
- package/src/widget/hooks/useConnector.tsx +18 -0
- package/src/widget/hooks/useCurrentScreen.tsx +3 -3
- package/src/widget/hooks/useCustomTokenFetch.tsx +72 -0
- package/src/widget/hooks/useCustomTokenSearch.tsx +402 -0
- package/src/widget/hooks/useDebounce.ts +25 -0
- package/src/widget/hooks/useDebugScreens.ts +26 -10
- package/src/widget/hooks/useDefaultTokenSelection.tsx +99 -143
- package/src/widget/hooks/useDestinationSelectedToken.tsx +1 -14
- package/src/widget/hooks/useEarnPool.tsx +1 -1
- package/src/widget/hooks/useIntentTransactionHistory.ts +20 -11
- package/src/widget/hooks/useIsMobile.tsx +50 -0
- package/src/widget/hooks/useMode.ts +2 -3
- package/src/widget/hooks/useOriginSelectedToken.tsx +2 -15
- package/src/widget/hooks/usePayMessage.tsx +31 -11
- package/src/widget/hooks/usePriceImpactWarning.ts +1 -1
- package/src/widget/hooks/useQuote.ts +189 -6
- package/src/widget/hooks/useRecentTokens.ts +6 -6
- package/src/widget/hooks/useRecipients.ts +1 -1
- package/src/widget/hooks/useSelectedFeeOption.tsx +2 -2
- package/src/widget/hooks/useSelectedFundMethod.tsx +1 -1
- package/src/widget/hooks/useSelectedRecipient.tsx +1 -1
- package/src/widget/hooks/useSendForm.ts +328 -152
- package/src/widget/hooks/useSwapAmount.tsx +1 -1
- package/src/widget/hooks/useTheme.tsx +1 -1
- package/src/widget/hooks/useTokenList.ts +672 -400
- package/src/widget/hooks/useTrailsSendTransaction.ts +949 -0
- package/src/widget/hooks/useWalletConnectUri.tsx +228 -0
- package/src/widget/hooks/useWidgetProps.tsx +3 -1
- package/src/widget/index.tsx +12 -0
- package/src/widget/providers/TrailsModalProvider.tsx +195 -0
- package/src/widget/providers/TrailsProvider.tsx +9 -3
- package/src/widget/types.ts +12 -20
- package/src/widget/widget.tsx +598 -385
- package/dist/cctp.d.ts +0 -3
- package/dist/cctp.d.ts.map +0 -1
- package/dist/lifi.d.ts +0 -4
- package/dist/lifi.d.ts.map +0 -1
- package/dist/meshconnect.d.ts +0 -171
- package/dist/meshconnect.d.ts.map +0 -1
- package/dist/relaySdk.d.ts +0 -87
- package/dist/relaySdk.d.ts.map +0 -1
- package/dist/widget/components/MeshConnectExchanges.d.ts +0 -7
- package/dist/widget/components/MeshConnectExchanges.d.ts.map +0 -1
- package/dist/widget/components/MeshConnectFlow.d.ts +0 -13
- package/dist/widget/components/MeshConnectFlow.d.ts.map +0 -1
- package/dist/widget/components/MeshConnectIframe.d.ts +0 -15
- package/dist/widget/components/MeshConnectIframe.d.ts.map +0 -1
- package/dist/widget/components/Receive.d.ts +0 -12
- package/dist/widget/components/Receive.d.ts.map +0 -1
- package/dist/widget/hooks/useSelectedMeshExchange.d.ts +0 -14
- package/dist/widget/hooks/useSelectedMeshExchange.d.ts.map +0 -1
- package/src/cctp.ts +0 -54
- package/src/lifi.ts +0 -108
- package/src/meshconnect.ts +0 -531
- package/src/relaySdk.ts +0 -703
- package/src/widget/components/MeshConnectExchanges.tsx +0 -290
- package/src/widget/components/MeshConnectFlow.tsx +0 -90
- package/src/widget/components/MeshConnectIframe.tsx +0 -500
- package/src/widget/components/Receive.tsx +0 -175
- package/src/widget/hooks/useSelectedMeshExchange.tsx +0 -46
package/src/tokenBalances.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
} from "@0xsequence/indexer"
|
|
9
9
|
import { ContractVerificationStatus } from "@0xsequence/indexer"
|
|
10
10
|
import type { Page } from "@0xsequence/indexer"
|
|
11
|
-
import type { TokenPrice
|
|
11
|
+
import type { TokenPrice } from "@0xtrails/api"
|
|
12
12
|
import { QueryClient, useQuery } from "@tanstack/react-query"
|
|
13
13
|
import type { Address } from "ox"
|
|
14
14
|
import { useEffect, useState } from "react"
|
|
@@ -17,7 +17,7 @@ import { useIndexerGatewayClient } from "./indexerClient.js"
|
|
|
17
17
|
import { getTokenPrices, useTokenPrices } from "./prices.js"
|
|
18
18
|
import { logger } from "./logger.js"
|
|
19
19
|
import { getChainInfo } from "./chains.js"
|
|
20
|
-
import {
|
|
20
|
+
import { getTokenImageUrl, type Token } from "./tokens.js"
|
|
21
21
|
import type { TrailsClient } from "./trailsClient.js"
|
|
22
22
|
import { useTrailsClient } from "./trailsClient.js"
|
|
23
23
|
import { getFullErrorMessage } from "./error.js"
|
|
@@ -29,6 +29,139 @@ export type Price = {
|
|
|
29
29
|
currency: string
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// Conversion Helpers for TokenBalance -> Token
|
|
34
|
+
// ============================================================================
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Converts TokenBalance to Token
|
|
38
|
+
*/
|
|
39
|
+
function convertTokenBalanceToToken(
|
|
40
|
+
token: TokenBalance,
|
|
41
|
+
priceData?: TokenPrice,
|
|
42
|
+
chainInfo?: ReturnType<typeof getChainInfo>,
|
|
43
|
+
): Token {
|
|
44
|
+
const chainId = token.contractInfo?.chainId || 0
|
|
45
|
+
const chain = chainInfo || getChainInfo(chainId)
|
|
46
|
+
const contractAddress = token.contractAddress || zeroAddress
|
|
47
|
+
|
|
48
|
+
const result: Token = {
|
|
49
|
+
symbol: token.contractInfo?.symbol || "???",
|
|
50
|
+
name: token.contractInfo?.name || "Unknown Token",
|
|
51
|
+
decimals: token.contractInfo?.decimals || 18,
|
|
52
|
+
contractAddress,
|
|
53
|
+
chainId,
|
|
54
|
+
chainName: chain?.name || `Chain ${chainId}`,
|
|
55
|
+
imageUrl: getTokenImageUrl({
|
|
56
|
+
chainId,
|
|
57
|
+
contractAddress,
|
|
58
|
+
symbol: token.contractInfo?.symbol || "???",
|
|
59
|
+
fallbackImageUrl: token.contractInfo?.logoURI,
|
|
60
|
+
}),
|
|
61
|
+
isNativeToken: false,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Format balance if available
|
|
65
|
+
if (token.balance) {
|
|
66
|
+
const decimals = result.decimals
|
|
67
|
+
const balanceFields = formatBalanceFields(token.balance, decimals)
|
|
68
|
+
result.balance = balanceFields.balance
|
|
69
|
+
result.balanceFormatted = balanceFields.balanceFormatted
|
|
70
|
+
result.balanceDisplay = balanceFields.balanceDisplay
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Add price and USD balance if price data is available
|
|
74
|
+
if (priceData?.priceUsd !== undefined) {
|
|
75
|
+
const priceFields = formatPriceFields(priceData.priceUsd)
|
|
76
|
+
result.priceUsd = priceFields.priceUsd
|
|
77
|
+
result.priceUsdFormatted = priceFields.priceUsdFormatted
|
|
78
|
+
result.priceUsdDisplay = priceFields.priceUsdDisplay
|
|
79
|
+
|
|
80
|
+
if (token.balance) {
|
|
81
|
+
const price: Price = {
|
|
82
|
+
value: priceData.priceUsd,
|
|
83
|
+
currency: "USD",
|
|
84
|
+
}
|
|
85
|
+
const balanceUsd = getTokenBalanceUsd(token, price)
|
|
86
|
+
if (balanceUsd !== undefined) {
|
|
87
|
+
const usdFields = formatUsdFields(balanceUsd)
|
|
88
|
+
result.balanceUsd = usdFields.balanceUsd
|
|
89
|
+
result.balanceUsdFormatted = usdFields.balanceUsdFormatted
|
|
90
|
+
result.balanceUsdDisplay = usdFields.balanceUsdDisplay
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Default isSufficientBalance to true
|
|
96
|
+
result.isSufficientBalance = true
|
|
97
|
+
|
|
98
|
+
return result
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Converts NativeTokenBalance to Token
|
|
103
|
+
*/
|
|
104
|
+
function convertNativeTokenBalanceToToken(
|
|
105
|
+
token: NativeTokenBalance,
|
|
106
|
+
priceData?: TokenPrice,
|
|
107
|
+
chainInfo?: ReturnType<typeof getChainInfo>,
|
|
108
|
+
): Token {
|
|
109
|
+
const chainId = token.chainId
|
|
110
|
+
const chain = chainInfo || getChainInfo(chainId)
|
|
111
|
+
const symbol = chain?.nativeCurrency.symbol || "ETH"
|
|
112
|
+
|
|
113
|
+
const result: Token = {
|
|
114
|
+
symbol,
|
|
115
|
+
name: chain?.nativeCurrency.name || "Native Token",
|
|
116
|
+
decimals: chain?.nativeCurrency.decimals || 18,
|
|
117
|
+
contractAddress: zeroAddress,
|
|
118
|
+
chainId,
|
|
119
|
+
chainName: chain?.name || `Chain ${chainId}`,
|
|
120
|
+
imageUrl: getTokenImageUrl({
|
|
121
|
+
chainId,
|
|
122
|
+
contractAddress: zeroAddress,
|
|
123
|
+
symbol,
|
|
124
|
+
}),
|
|
125
|
+
isNativeToken: true,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Format balance if available
|
|
129
|
+
if (token.balance) {
|
|
130
|
+
const decimals = result.decimals
|
|
131
|
+
const balanceFields = formatBalanceFields(token.balance, decimals)
|
|
132
|
+
result.balance = balanceFields.balance
|
|
133
|
+
result.balanceFormatted = balanceFields.balanceFormatted
|
|
134
|
+
result.balanceDisplay = balanceFields.balanceDisplay
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Add price and USD balance if price data is available
|
|
138
|
+
if (priceData?.priceUsd !== undefined) {
|
|
139
|
+
const priceFields = formatPriceFields(priceData.priceUsd)
|
|
140
|
+
result.priceUsd = priceFields.priceUsd
|
|
141
|
+
result.priceUsdFormatted = priceFields.priceUsdFormatted
|
|
142
|
+
result.priceUsdDisplay = priceFields.priceUsdDisplay
|
|
143
|
+
|
|
144
|
+
if (token.balance) {
|
|
145
|
+
const price: Price = {
|
|
146
|
+
value: priceData.priceUsd,
|
|
147
|
+
currency: "USD",
|
|
148
|
+
}
|
|
149
|
+
const balanceUsd = getTokenBalanceUsd(token, price)
|
|
150
|
+
if (balanceUsd !== undefined) {
|
|
151
|
+
const usdFields = formatUsdFields(balanceUsd)
|
|
152
|
+
result.balanceUsd = usdFields.balanceUsd
|
|
153
|
+
result.balanceUsdFormatted = usdFields.balanceUsdFormatted
|
|
154
|
+
result.balanceUsdDisplay = usdFields.balanceUsdDisplay
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Default isSufficientBalance to true
|
|
160
|
+
result.isSufficientBalance = true
|
|
161
|
+
|
|
162
|
+
return result
|
|
163
|
+
}
|
|
164
|
+
|
|
32
165
|
const REFRESH_INTERVAL = 10000 // 10 seconds
|
|
33
166
|
|
|
34
167
|
// Initialize query client for token balances
|
|
@@ -62,36 +195,10 @@ export function isNativeToken(
|
|
|
62
195
|
return false
|
|
63
196
|
}
|
|
64
197
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
chainName?: string
|
|
70
|
-
imageUrl?: string
|
|
71
|
-
symbol?: string
|
|
72
|
-
name?: string
|
|
73
|
-
decimals?: number
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface NativeTokenBalanceWithPrice extends NativeTokenBalance {
|
|
77
|
-
price?: Price
|
|
78
|
-
balanceUsd?: number
|
|
79
|
-
balanceUsdFormatted?: string
|
|
80
|
-
symbol?: string
|
|
81
|
-
chainName?: string
|
|
82
|
-
imageUrl?: string
|
|
83
|
-
name?: string
|
|
84
|
-
decimals?: number
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export type TokenBalanceExtended =
|
|
88
|
-
| TokenBalanceWithPrice
|
|
89
|
-
| NativeTokenBalanceWithPrice
|
|
90
|
-
|
|
91
|
-
export function sortTokensByPriority(
|
|
92
|
-
a: TokenBalanceExtended,
|
|
93
|
-
b: TokenBalanceExtended,
|
|
94
|
-
): number {
|
|
198
|
+
/**
|
|
199
|
+
* Sort function for Token by priority
|
|
200
|
+
*/
|
|
201
|
+
export function sortTokensByPriority(a: Token, b: Token): number {
|
|
95
202
|
// First sort by USD balance if available
|
|
96
203
|
const aUsdBalance = a.balanceUsd ?? 0
|
|
97
204
|
const bUsdBalance = b.balanceUsd ?? 0
|
|
@@ -105,48 +212,29 @@ export function sortTokensByPriority(
|
|
|
105
212
|
if (aUsdBalance > 0 && bUsdBalance === 0) return -1
|
|
106
213
|
if (aUsdBalance === 0 && bUsdBalance > 0) return 1
|
|
107
214
|
|
|
108
|
-
// If both have zero balance, sort by volume
|
|
215
|
+
// If both have zero balance, sort by price volume (if available)
|
|
109
216
|
if (aUsdBalance === 0 && bUsdBalance === 0) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (aVolume !== bVolume) {
|
|
113
|
-
return bVolume - aVolume // Higher volume first for zero balance tokens
|
|
114
|
-
}
|
|
217
|
+
// Note: price24hVol is not in Token, so we skip volume sorting
|
|
218
|
+
// This can be added later if needed
|
|
115
219
|
}
|
|
116
220
|
|
|
117
221
|
// Then sort by native token status
|
|
118
|
-
if (isNativeToken
|
|
119
|
-
if (!isNativeToken
|
|
222
|
+
if (a.isNativeToken && !b.isNativeToken) return -1
|
|
223
|
+
if (!a.isNativeToken && b.isNativeToken) return 1
|
|
120
224
|
|
|
121
225
|
// Then sort by token balance
|
|
122
226
|
try {
|
|
123
|
-
const balanceA = BigInt(a.balance)
|
|
124
|
-
const balanceB = BigInt(b.balance)
|
|
227
|
+
const balanceA = BigInt(a.balance || "0")
|
|
228
|
+
const balanceB = BigInt(b.balance || "0")
|
|
125
229
|
if (balanceA > balanceB) return -1
|
|
126
230
|
if (balanceA < balanceB) return 1
|
|
127
231
|
} catch {
|
|
128
|
-
// If balance comparison fails, continue
|
|
232
|
+
// If balance comparison fails, continue
|
|
129
233
|
}
|
|
130
234
|
|
|
131
235
|
return 0
|
|
132
236
|
}
|
|
133
237
|
|
|
134
|
-
// Helper function to extract 24h volume from price data
|
|
135
|
-
function getTokenVolume24h(price?: any): number {
|
|
136
|
-
// Try different possible volume field names from the API
|
|
137
|
-
if (!price) return 0
|
|
138
|
-
|
|
139
|
-
// Primary: use price24hVol if available, otherwise try other common field names
|
|
140
|
-
return Number(
|
|
141
|
-
price.price24hVol ||
|
|
142
|
-
price.volume24h ||
|
|
143
|
-
price.volume_24h ||
|
|
144
|
-
price.vol24h ||
|
|
145
|
-
price.dailyVolume ||
|
|
146
|
-
0,
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
238
|
export interface GetTokenBalancesWithPrice {
|
|
151
239
|
page: Page
|
|
152
240
|
nativeBalances: Array<
|
|
@@ -178,12 +266,11 @@ export function useTokenBalances(
|
|
|
178
266
|
isLoadingPrices: boolean
|
|
179
267
|
isLoadingSortedTokens: boolean
|
|
180
268
|
balanceError: Error | null
|
|
181
|
-
sortedTokens:
|
|
269
|
+
sortedTokens: Token[]
|
|
182
270
|
} {
|
|
183
271
|
// Always call hooks unconditionally to fix React rules violation
|
|
184
272
|
const hookIndexerClient = useIndexerGatewayClient()
|
|
185
273
|
const hookTrailsClient = useTrailsClient()
|
|
186
|
-
const { getTokenImageUrl } = useGetTokenImageUrl()
|
|
187
274
|
|
|
188
275
|
// Use passed parameters if available, otherwise use hook results
|
|
189
276
|
const indexerClient = indexerGatewayClient ?? hookIndexerClient
|
|
@@ -294,7 +381,7 @@ export function useTokenBalances(
|
|
|
294
381
|
)
|
|
295
382
|
|
|
296
383
|
const { data: sortedTokens = [], isLoading: isLoadingSortedTokens } =
|
|
297
|
-
useQuery<
|
|
384
|
+
useQuery<Token[]>({
|
|
298
385
|
queryKey: [
|
|
299
386
|
"tokenBalances",
|
|
300
387
|
"sorted",
|
|
@@ -307,76 +394,36 @@ export function useTokenBalances(
|
|
|
307
394
|
return []
|
|
308
395
|
}
|
|
309
396
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
decimals:
|
|
318
|
-
(token as any).decimals ??
|
|
319
|
-
(token as any).contractInfo?.decimals ??
|
|
320
|
-
18, // Use token decimals or default to 18
|
|
321
|
-
imageUrl: getTokenImageUrl({
|
|
322
|
-
chainId: token.chainId,
|
|
323
|
-
contractAddress: zeroAddress,
|
|
324
|
-
symbol: token.symbol ?? (token as any).contractInfo?.symbol,
|
|
325
|
-
fallbackImageUrl: (token as any).contractInfo?.logoURI,
|
|
326
|
-
}),
|
|
327
|
-
})),
|
|
328
|
-
...tokenBalancesData.balances.map((token) => ({
|
|
329
|
-
...token,
|
|
330
|
-
contractAddress: token.contractAddress,
|
|
331
|
-
chainName: getChainInfo(token.contractInfo?.chainId)?.name || "",
|
|
332
|
-
symbol: token.contractInfo?.symbol,
|
|
333
|
-
name: token.contractInfo?.name,
|
|
334
|
-
decimals: token.contractInfo?.decimals,
|
|
335
|
-
imageUrl: getTokenImageUrl({
|
|
336
|
-
chainId: token.contractInfo?.chainId || 0,
|
|
337
|
-
contractAddress: token.contractAddress,
|
|
338
|
-
symbol: token.contractInfo?.symbol,
|
|
339
|
-
fallbackImageUrl: token.contractInfo?.logoURI,
|
|
340
|
-
}),
|
|
341
|
-
})),
|
|
342
|
-
].filter((token) => {
|
|
343
|
-
try {
|
|
344
|
-
return BigInt(token.balance) > 0n
|
|
345
|
-
} catch {
|
|
346
|
-
return false
|
|
347
|
-
}
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
// First pass: add prices to all tokens
|
|
351
|
-
const tokensWithPrices = balances.map((token) => {
|
|
352
|
-
const isNative = isNativeToken(token)
|
|
353
|
-
const priceData: TrailsTokenPrice | undefined = tokenPrices.find(
|
|
354
|
-
(p) =>
|
|
355
|
-
p.token.tokenAddress ===
|
|
356
|
-
(isNative ? zeroAddress : token.contractAddress) &&
|
|
357
|
-
p.token.chainId ===
|
|
358
|
-
(isNative ? token.chainId : token.contractInfo?.chainId),
|
|
359
|
-
)
|
|
360
|
-
|
|
361
|
-
if (priceData?.priceUsd !== undefined) {
|
|
362
|
-
// Create a Price object compatible with the indexer API
|
|
363
|
-
const price: Price = {
|
|
364
|
-
value: priceData.priceUsd,
|
|
365
|
-
currency: "USD",
|
|
366
|
-
}
|
|
367
|
-
const tokenWithPrice = { ...token, price }
|
|
368
|
-
tokenWithPrice.balanceUsd = getTokenBalanceUsd(token, price)
|
|
369
|
-
tokenWithPrice.balanceUsdFormatted = getTokenBalanceUsdFormatted(
|
|
370
|
-
token,
|
|
371
|
-
price,
|
|
397
|
+
// Build Token directly from balances
|
|
398
|
+
const trailsTokens: Token[] = [
|
|
399
|
+
...tokenBalancesData.nativeBalances.map((token) => {
|
|
400
|
+
const priceData = tokenPrices.find(
|
|
401
|
+
(p) =>
|
|
402
|
+
p.token.tokenAddress === zeroAddress &&
|
|
403
|
+
p.token.chainId === token.chainId,
|
|
372
404
|
)
|
|
373
|
-
return
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
405
|
+
return convertNativeTokenBalanceToToken(token, priceData)
|
|
406
|
+
}),
|
|
407
|
+
...tokenBalancesData.balances.map((token) => {
|
|
408
|
+
const priceData = tokenPrices.find(
|
|
409
|
+
(p) =>
|
|
410
|
+
p.token.tokenAddress?.toLowerCase() ===
|
|
411
|
+
token.contractAddress.toLowerCase() &&
|
|
412
|
+
p.token.chainId === token.contractInfo?.chainId,
|
|
413
|
+
)
|
|
414
|
+
return convertTokenBalanceToToken(token, priceData)
|
|
415
|
+
}),
|
|
416
|
+
]
|
|
417
|
+
.filter((token) => {
|
|
418
|
+
try {
|
|
419
|
+
return token.balance && BigInt(token.balance) > 0n
|
|
420
|
+
} catch {
|
|
421
|
+
return false
|
|
422
|
+
}
|
|
423
|
+
})
|
|
424
|
+
.sort(sortTokensByPriority)
|
|
378
425
|
|
|
379
|
-
return
|
|
426
|
+
return trailsTokens
|
|
380
427
|
},
|
|
381
428
|
enabled:
|
|
382
429
|
!isLoadingBalances &&
|
|
@@ -593,6 +640,160 @@ export function getTokenBalanceUsdFormatted(
|
|
|
593
640
|
return formatUsdAmountDisplay(balanceUsd)
|
|
594
641
|
}
|
|
595
642
|
|
|
643
|
+
/**
|
|
644
|
+
* Formats USD value without currency symbol (for formatted fields)
|
|
645
|
+
* Similar to formatUsdAmountDisplay but without the $ sign
|
|
646
|
+
*/
|
|
647
|
+
export function formatUsdAmountFormatted(value: number | string = 0): string {
|
|
648
|
+
if (!value) {
|
|
649
|
+
value = 0
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const numValue = Number(value)
|
|
653
|
+
|
|
654
|
+
// Validate input
|
|
655
|
+
if (!Number.isFinite(numValue) || Number.isNaN(numValue)) {
|
|
656
|
+
return "0.00"
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// Handle negative values explicitly
|
|
660
|
+
const isNegative = numValue < 0
|
|
661
|
+
const absValue = Math.abs(numValue)
|
|
662
|
+
|
|
663
|
+
// Cap at reasonable max to prevent display issues (100 trillion)
|
|
664
|
+
const MAX_DISPLAY_VALUE = 100_000_000_000_000
|
|
665
|
+
const cappedValue = Math.min(absValue, MAX_DISPLAY_VALUE)
|
|
666
|
+
|
|
667
|
+
// For very small amounts (less than 0.01), show 3 decimal places
|
|
668
|
+
if (cappedValue > 0 && cappedValue < 0.01) {
|
|
669
|
+
const formatted = Number(cappedValue).toLocaleString(undefined, {
|
|
670
|
+
maximumFractionDigits: 3,
|
|
671
|
+
minimumFractionDigits: 3,
|
|
672
|
+
useGrouping: false,
|
|
673
|
+
})
|
|
674
|
+
return isNegative ? `-${formatted}` : formatted
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// For amounts >= 0.01, use standard 2 decimal places
|
|
678
|
+
const formatted = Number(cappedValue).toLocaleString(undefined, {
|
|
679
|
+
maximumFractionDigits: 2,
|
|
680
|
+
minimumFractionDigits: 2,
|
|
681
|
+
useGrouping: false,
|
|
682
|
+
})
|
|
683
|
+
return isNegative ? `-${formatted}` : formatted
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Formats balance fields (raw → formatted → display)
|
|
688
|
+
* Returns all three versions for Token
|
|
689
|
+
*/
|
|
690
|
+
export function formatBalanceFields(
|
|
691
|
+
balance: string | bigint,
|
|
692
|
+
decimals: number,
|
|
693
|
+
): {
|
|
694
|
+
balance: string // Raw value as string (e.g., "1000000000000000000")
|
|
695
|
+
balanceFormatted: string // Formatted with decimals, no commas (e.g., "1.0")
|
|
696
|
+
balanceDisplay: string // Display with commas (e.g., "1,000.00")
|
|
697
|
+
} {
|
|
698
|
+
if (!balance) {
|
|
699
|
+
return {
|
|
700
|
+
balance: "0",
|
|
701
|
+
balanceFormatted: "0",
|
|
702
|
+
balanceDisplay: "0",
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
try {
|
|
707
|
+
// Validate decimals
|
|
708
|
+
if (!Number.isInteger(decimals) || decimals < 1 || decimals > 18) {
|
|
709
|
+
throw new Error("Invalid decimals")
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const balanceBigInt = BigInt(balance)
|
|
713
|
+
const balanceString = balanceBigInt.toString()
|
|
714
|
+
|
|
715
|
+
// Check for negative balance
|
|
716
|
+
if (balanceBigInt < 0n) {
|
|
717
|
+
throw new Error("Negative balance")
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// Format using formatUnits
|
|
721
|
+
const formatted = formatUnits(balanceBigInt, decimals)
|
|
722
|
+
|
|
723
|
+
// balanceFormatted: no commas (uses formatAmount with useGrouping: false)
|
|
724
|
+
const balanceFormatted = formatAmount(formatted)
|
|
725
|
+
|
|
726
|
+
// balanceDisplay: with commas (uses formatAmountDisplay with useGrouping: true)
|
|
727
|
+
const balanceDisplay = formatAmountDisplay(formatted)
|
|
728
|
+
|
|
729
|
+
return {
|
|
730
|
+
balance: balanceString,
|
|
731
|
+
balanceFormatted,
|
|
732
|
+
balanceDisplay,
|
|
733
|
+
}
|
|
734
|
+
} catch (err) {
|
|
735
|
+
logger.console.error("[trails-sdk] Error formatting balance fields:", {
|
|
736
|
+
balance,
|
|
737
|
+
decimals,
|
|
738
|
+
err,
|
|
739
|
+
})
|
|
740
|
+
// Return safe defaults on error
|
|
741
|
+
return {
|
|
742
|
+
balance: typeof balance === "string" ? balance : balance.toString(),
|
|
743
|
+
balanceFormatted: "0",
|
|
744
|
+
balanceDisplay: "0",
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Formats USD value fields (raw → formatted → display)
|
|
751
|
+
* Returns all three versions for Token
|
|
752
|
+
*/
|
|
753
|
+
export function formatUsdFields(usdValue: number | string | null | undefined): {
|
|
754
|
+
balanceUsd?: number // Raw number (e.g., 1000.50)
|
|
755
|
+
balanceUsdFormatted?: string // Formatted, no commas or $ (e.g., "1000.50")
|
|
756
|
+
balanceUsdDisplay?: string // Display with commas and $ (e.g., "$1,000.50")
|
|
757
|
+
} {
|
|
758
|
+
if (usdValue === null || usdValue === undefined) {
|
|
759
|
+
return {}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const numValue = Number(usdValue)
|
|
763
|
+
|
|
764
|
+
// Validate input
|
|
765
|
+
if (!Number.isFinite(numValue) || Number.isNaN(numValue)) {
|
|
766
|
+
return {
|
|
767
|
+
balanceUsd: 0,
|
|
768
|
+
balanceUsdFormatted: "0.00",
|
|
769
|
+
balanceUsdDisplay: "$0.00",
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
return {
|
|
774
|
+
balanceUsd: numValue,
|
|
775
|
+
balanceUsdFormatted: formatUsdAmountFormatted(numValue),
|
|
776
|
+
balanceUsdDisplay: formatUsdAmountDisplay(numValue),
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Formats price fields (raw → formatted → display)
|
|
782
|
+
* Returns all three versions for Token
|
|
783
|
+
*/
|
|
784
|
+
export function formatPriceFields(price: number | string | null | undefined): {
|
|
785
|
+
priceUsd?: number // Raw number (e.g., 1.50)
|
|
786
|
+
priceUsdFormatted?: string // Formatted, no commas or $ (e.g., "1.50")
|
|
787
|
+
priceUsdDisplay?: string // Display with commas and $ (e.g., "$1.50")
|
|
788
|
+
} {
|
|
789
|
+
const usdFields = formatUsdFields(price)
|
|
790
|
+
return {
|
|
791
|
+
priceUsd: usdFields.balanceUsd,
|
|
792
|
+
priceUsdFormatted: usdFields.balanceUsdFormatted,
|
|
793
|
+
priceUsdDisplay: usdFields.balanceUsdDisplay,
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
596
797
|
export function useTokenBalanceUsdFormat(
|
|
597
798
|
token: TokenBalance | NativeTokenBalance,
|
|
598
799
|
tokenPrice: Price,
|
|
@@ -772,7 +973,7 @@ export type GetTokenBalancesWithPricesParams = {
|
|
|
772
973
|
}
|
|
773
974
|
|
|
774
975
|
export type GetTokenBalancesWithPriceReturn = {
|
|
775
|
-
balances:
|
|
976
|
+
balances: Token[]
|
|
776
977
|
}
|
|
777
978
|
|
|
778
979
|
export async function getTokenBalancesWithPrices({
|
|
@@ -792,9 +993,10 @@ export async function getTokenBalancesWithPrices({
|
|
|
792
993
|
tokenSymbol: t.contractInfo?.symbol || "",
|
|
793
994
|
})),
|
|
794
995
|
)
|
|
795
|
-
const balancesWithPrices = tokens.map((b) => {
|
|
796
|
-
const priceData:
|
|
797
|
-
const
|
|
996
|
+
const balancesWithPrices: Token[] = tokens.map((b) => {
|
|
997
|
+
const priceData: TokenPrice | undefined = tokenPrices.find((p) => {
|
|
998
|
+
const tokenChainId = b.chainId ?? (b as any).contractInfo?.chainId
|
|
999
|
+
const isSameChain = p.token.chainId === tokenChainId
|
|
798
1000
|
let isSameToken = p.token.tokenAddress === b.contractAddress
|
|
799
1001
|
if (!b.contractAddress) {
|
|
800
1002
|
isSameToken =
|
|
@@ -803,20 +1005,14 @@ export async function getTokenBalancesWithPrices({
|
|
|
803
1005
|
return isSameChain && isSameToken
|
|
804
1006
|
})
|
|
805
1007
|
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
return {
|
|
813
|
-
...b,
|
|
814
|
-
price,
|
|
815
|
-
balanceUsd: price ? getTokenBalanceUsd(b, price) : undefined,
|
|
816
|
-
balanceUsdFormatted: price
|
|
817
|
-
? getTokenBalanceUsdFormatted(b, price)
|
|
818
|
-
: undefined,
|
|
1008
|
+
const isNative = isNativeToken(b)
|
|
1009
|
+
if (isNative) {
|
|
1010
|
+
return convertNativeTokenBalanceToToken(
|
|
1011
|
+
b as NativeTokenBalance,
|
|
1012
|
+
priceData,
|
|
1013
|
+
)
|
|
819
1014
|
}
|
|
1015
|
+
return convertTokenBalanceToToken(b as TokenBalance, priceData)
|
|
820
1016
|
})
|
|
821
1017
|
|
|
822
1018
|
return {
|
|
@@ -858,7 +1054,7 @@ export function useAccountTokenBalance({
|
|
|
858
1054
|
})
|
|
859
1055
|
const tokenBalance = balances.find(
|
|
860
1056
|
(b) =>
|
|
861
|
-
b.chainId === chainId &&
|
|
1057
|
+
(b.chainId ?? 0) === chainId &&
|
|
862
1058
|
(b.contractAddress?.toLowerCase() === token.toLowerCase() ||
|
|
863
1059
|
(!b.contractAddress && token === zeroAddress)),
|
|
864
1060
|
)
|
|
@@ -895,21 +1091,20 @@ export async function getHasSufficientBalanceToken({
|
|
|
895
1091
|
indexerGatewayClient,
|
|
896
1092
|
trailsClient,
|
|
897
1093
|
})
|
|
898
|
-
const tokenBalance
|
|
1094
|
+
const tokenBalance = balances.find(
|
|
899
1095
|
(b) =>
|
|
900
|
-
b.chainId === chainId &&
|
|
1096
|
+
(b.chainId ?? 0) === chainId &&
|
|
901
1097
|
(b.contractAddress?.toLowerCase() === token.toLowerCase() ||
|
|
902
1098
|
(!b.contractAddress && token === zeroAddress)),
|
|
903
1099
|
)
|
|
904
1100
|
if (!tokenBalance) {
|
|
905
1101
|
return false
|
|
906
1102
|
}
|
|
907
|
-
const
|
|
908
|
-
const decimals = isNative ? 18 : tokenBalance?.contractInfo?.decimals
|
|
1103
|
+
const decimals = tokenBalance.decimals
|
|
909
1104
|
if (!decimals) {
|
|
910
1105
|
throw new Error("Decimals not found")
|
|
911
1106
|
}
|
|
912
|
-
return tokenBalance
|
|
1107
|
+
return tokenBalance.balance
|
|
913
1108
|
? BigInt(tokenBalance.balance) >= parseUnits(amount, decimals)
|
|
914
1109
|
: false
|
|
915
1110
|
}
|