From 5fbcb4b4e78223f7d2584d3c9b663b0c9bcf9e5a Mon Sep 17 00:00:00 2001 From: Alex Perry Date: Thu, 19 Dec 2024 10:29:11 +0100 Subject: [PATCH] feat: add sip10s to account total balance --- config/wallet-config.json | 2 +- .../hooks/balance/use-sip10-balance.tsx | 62 +++++++++++++++++++ .../hooks/balance/use-total-balance.tsx | 11 +++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/app/common/hooks/balance/use-sip10-balance.tsx diff --git a/config/wallet-config.json b/config/wallet-config.json index 9e72752b87..8e18427e05 100644 --- a/config/wallet-config.json +++ b/config/wallet-config.json @@ -100,7 +100,7 @@ "sponsorshipsEnabled": true, "sponsorshipApiUrl": { "mainnet": "https://sponsor.leather.io", - "testnet": "http://testnet-13-60-14-218.nip.io" + "testnet": "https://sponsor-testnet.leather.io" }, "contracts": { "mainnet": { diff --git a/src/app/common/hooks/balance/use-sip10-balance.tsx b/src/app/common/hooks/balance/use-sip10-balance.tsx new file mode 100644 index 0000000000..49c364eb73 --- /dev/null +++ b/src/app/common/hooks/balance/use-sip10-balance.tsx @@ -0,0 +1,62 @@ +import { useMemo } from 'react'; + +import type { Money } from '@leather.io/models'; +import { + baseCurrencyAmountInQuote, + createMoney, + isDefined, + isMoneyGreaterThanZero, + sumMoney, +} from '@leather.io/utils'; + +import { useSip10FiatMarketData } from '../use-calculate-sip10-fiat-value'; +import { useCombinedFilteredSip10Tokens } from '../use-filtered-sip10-tokens'; +import { type AssetFilter, useManageTokens } from '../use-manage-tokens'; + +interface UseSip10ManagedTokensBalanceArgs { + stxAddress: string; + assetFilter?: AssetFilter; +} + +export function useSip10ManagedTokensBalance({ + stxAddress, + assetFilter = 'all', +}: UseSip10ManagedTokensBalanceArgs) { + const { getTokenMarketData } = useSip10FiatMarketData(); + const { allTokens = [], supportedTokens } = useCombinedFilteredSip10Tokens({ + address: stxAddress, + filter: 'all', + }); + const { filterTokens } = useManageTokens(); + const preEnabledTokensIds = supportedTokens.map(t => t.info.contractId); + const managedTokens = filterTokens({ + tokens: allTokens, + filter: assetFilter, + getTokenId: t => t.info.contractId, + preEnabledTokensIds, + }); + + const balance = useMemo(() => { + const baseBalance: Money = createMoney(0, 'USD'); + return sumMoney([ + baseBalance, + ...managedTokens + .map(token => { + const tokenMarketData = getTokenMarketData( + token.info.contractId, + token.balance.availableBalance.symbol + ); + if ( + !tokenMarketData || + !token.balance.availableBalance || + !isMoneyGreaterThanZero(tokenMarketData.price) + ) + return; + return baseCurrencyAmountInQuote(token.balance.availableBalance, tokenMarketData); + }) + .filter(isDefined), + ]); + }, [managedTokens, getTokenMarketData]); + + return balance; +} diff --git a/src/app/common/hooks/balance/use-total-balance.tsx b/src/app/common/hooks/balance/use-total-balance.tsx index e2e67b0814..7129f0846a 100644 --- a/src/app/common/hooks/balance/use-total-balance.tsx +++ b/src/app/common/hooks/balance/use-total-balance.tsx @@ -8,6 +8,8 @@ import { baseCurrencyAmountInQuote, createMoney, i18nFormatCurrency } from '@lea import { useBtcCryptoAssetBalanceNativeSegwit } from '@app/query/bitcoin/balance/btc-balance-native-segwit.hooks'; +import { useSip10ManagedTokensBalance } from './use-sip10-balance'; + interface UseTotalBalanceArgs { btcAddress: string; stxAddress: string; @@ -38,12 +40,18 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs) isLoadingAdditionalData: isLoadingAdditionalDataBtcBalance, } = useBtcCryptoAssetBalanceNativeSegwit(btcAddress); + // get sip10 balance + const sip10BalanceUsd = useSip10ManagedTokensBalance({ stxAddress, assetFilter: 'enabled' }); + return useMemo(() => { // calculate total balance const stxUsdAmount = baseCurrencyAmountInQuote(stxBalance, stxMarketData); const btcUsdAmount = baseCurrencyAmountInQuote(btcBalance.availableBalance, btcMarketData); - const totalBalance = { ...stxUsdAmount, amount: stxUsdAmount.amount.plus(btcUsdAmount.amount) }; + const totalBalance = { + ...stxUsdAmount, + amount: stxUsdAmount.amount.plus(btcUsdAmount.amount).plus(sip10BalanceUsd.amount), + }; return { isFetching: isFetchingStxBalance || isFetchingBtcBalance, isLoading: isLoadingStxBalance || isLoadingBtcBalance, @@ -73,5 +81,6 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs) isLoadingAdditionalDataStxBalance, stxAddress, btcAddress, + sip10BalanceUsd, ]); }