Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sip10s to account total balance #6030

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]);
}
Loading