Skip to content

Commit

Permalink
refactor: stx available balance
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Dec 27, 2024
1 parent 7d28486 commit 7f838c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 37 deletions.
25 changes: 18 additions & 7 deletions src/app/common/hooks/balance/use-balances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export function useBalances({ btcAddress, stxAddress }: UseBalanceArgs) {
isPending: isPendingStxBalance,
} = filteredBalanceQuery;

const stxBalance = balance ? balance.totalBalance : createMoney(0, 'STX');
const totalStxBalance = balance ? balance.totalBalance : createMoney(0, 'STX');
const availableUnlockedStxBalance = balance
? balance.availableUnlockedBalance
: createMoney(0, 'STX');

// get btc balance
const {
Expand All @@ -47,7 +50,12 @@ export function useBalances({ btcAddress, stxAddress }: UseBalanceArgs) {

return useMemo(() => {
// calculate total balance
const stxUsdAmount = baseCurrencyAmountInQuote(stxBalance, stxMarketData);
const totalStxUsdAmount = baseCurrencyAmountInQuote(totalStxBalance, stxMarketData);
// calculate available unlocked balance
const availableUnlockedStxUsdAmount = baseCurrencyAmountInQuote(
availableUnlockedStxBalance,
stxMarketData
);

const availableBtcUsdAmount = baseCurrencyAmountInQuote(
btcBalance.availableBalance,
Expand All @@ -57,13 +65,15 @@ export function useBalances({ btcAddress, stxAddress }: UseBalanceArgs) {
const totalBtcUsdAmount = baseCurrencyAmountInQuote(btcBalance.totalBalance, btcMarketData);

const totalBalance = {
...stxUsdAmount,
amount: stxUsdAmount.amount.plus(totalBtcUsdAmount.amount).plus(sip10BalanceUsd.amount),
...totalStxUsdAmount,
amount: totalStxUsdAmount.amount.plus(totalBtcUsdAmount.amount).plus(sip10BalanceUsd.amount),
};

const availableBalance = {
...stxUsdAmount,
amount: stxUsdAmount.amount.plus(availableBtcUsdAmount.amount).plus(sip10BalanceUsd.amount),
...availableUnlockedStxUsdAmount,
amount: availableUnlockedStxUsdAmount.amount
.plus(availableBtcUsdAmount.amount)
.plus(sip10BalanceUsd.amount),
};

return {
Expand All @@ -86,8 +96,9 @@ export function useBalances({ btcAddress, stxAddress }: UseBalanceArgs) {
isLoadingAdditionalDataStxBalance || isLoadingAdditionalDataBtcBalance,
};
}, [
stxBalance,
totalStxBalance,
stxMarketData,
availableUnlockedStxBalance,
btcBalance.availableBalance,
btcBalance.totalBalance,
btcMarketData,
Expand Down
32 changes: 4 additions & 28 deletions src/app/pages/home/components/account-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useLocation, useNavigate } from 'react-router-dom';

import { ChainID } from '@stacks/transactions';
import { HomePageSelectors } from '@tests/selectors/home.selectors';
import { Box, Flex } from 'leather-styles/jsx';

import { whenStacksChainId } from '@leather.io/stacks';
import { ArrowsRepeatLeftRightIcon, CreditCardIcon, IconButton, InboxIcon } from '@leather.io/ui';

import { RouteUrls } from '@shared/route-urls';
Expand All @@ -16,7 +14,6 @@ import {
import { useCurrentAccountNativeSegwitIndexZeroSignerNullable } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { useCurrentNetworkState } from '@app/store/networks/networks.hooks';
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';

import { SendButton } from './send-button';
Expand All @@ -29,7 +26,6 @@ export function AccountActions() {
const stacksAccount = useCurrentStacksAccount();
const currentBtcSigner = useCurrentAccountNativeSegwitIndexZeroSignerNullable();
const btcAccount = currentBtcSigner?.address;
const currentNetwork = useCurrentNetwork();
const { isTestnet } = useCurrentNetworkState();

const swapsEnabled = useConfigSwapsEnabled();
Expand Down Expand Up @@ -57,37 +53,17 @@ export function AccountActions() {
onClick={() => navigate(RouteUrls.FundChooseCurrency)}
/>
)}
{whenStacksChainId(currentNetwork.chain.stacks.chainId)({
[ChainID.Mainnet]: (
<BasicTooltip
label={swapsEnabled ? '' : <SwapsDisabledTooltipLabel />}
side="left"
asChild
>
<Box display="flex">
<IconButton
data-testid={HomePageSelectors.SwapBtn}
disabled={swapsBtnDisabled}
icon={<ArrowsRepeatLeftRightIcon />}
label="Swap"
onClick={() =>
navigate(RouteUrls.Swap.replace(':base', 'STX').replace(':quote', ''))
}
/>
</Box>
</BasicTooltip>
),
// Temporary for sBTC testing
[ChainID.Testnet]: (
<BasicTooltip label={swapsEnabled ? '' : <SwapsDisabledTooltipLabel />} side="left" asChild>
<Box display="flex">
<IconButton
data-testid={HomePageSelectors.SwapBtn}
disabled={swapsBtnDisabled}
icon={<ArrowsRepeatLeftRightIcon />}
label="Swap"
onClick={() => navigate(RouteUrls.Swap.replace(':base', 'STX').replace(':quote', ''))}
/>
),
})}
</Box>
</BasicTooltip>
</Flex>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export function StacksCommonSendForm({
amountField,
selectedAssetField,
fees,

availableTokenBalance,
}: StacksCommonSendFormProps) {
const navigate = useNavigate();
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/components/account/account.card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PrivateTextLayout } from '@app/components/privacy/private-text.layout';
import { BasicTooltip } from '../tooltip/basic-tooltip';

const availableBalanceTooltipLabel =
'Total balance minus locked amounts, outbound transfers, protected collectibles and uneconomical UTXOs';
'Total balance minus locked amounts, outbound transfers, protected collectibles and uneconomical UTXOs.';

interface AccountCardProps {
name: string;
Expand Down

0 comments on commit 7f838c2

Please sign in to comment.