Skip to content

Commit

Permalink
feat: add sip10s to account total balance
Browse files Browse the repository at this point in the history
  • Loading branch information
alexp3y authored and kyranjamie committed Dec 23, 2024
1 parent 94fa068 commit 5fbcb4b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/wallet-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
62 changes: 62 additions & 0 deletions src/app/common/hooks/balance/use-sip10-balance.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 10 additions & 1 deletion src/app/common/hooks/balance/use-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -73,5 +81,6 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
isLoadingAdditionalDataStxBalance,
stxAddress,
btcAddress,
sip10BalanceUsd,
]);
}

0 comments on commit 5fbcb4b

Please sign in to comment.