Skip to content

Commit

Permalink
fix: stx available balance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Jan 17, 2025
1 parent 6a94dbe commit ec6d4e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface StxCryptoAssetItemProps {
isPrivate?: boolean;
onSelectAsset?(symbol: string): void;
}

export function StxCryptoAssetItem({
balance,
isLoading,
Expand All @@ -25,26 +26,24 @@ export function StxCryptoAssetItem({
}: StxCryptoAssetItemProps) {
const marketData = useCryptoCurrencyMarketDataMeanAverage('STX');

const { availableBalance, lockedBalance } = balance;
const { lockedBalance, totalBalance } = balance;
const showLockedBalance = lockedBalance.amount.isGreaterThan(0) && !isPrivate;

const fiatLockedBalance = i18nFormatCurrency(
baseCurrencyAmountInQuote(lockedBalance, marketData)
);
const fiatAvailableBalance = i18nFormatCurrency(
baseCurrencyAmountInQuote(availableBalance, marketData)
);
const fiatTotalBalance = i18nFormatCurrency(baseCurrencyAmountInQuote(totalBalance, marketData));
const titleRightBulletInfo = (
<styled.span>{formatMoneyWithoutSymbol(lockedBalance)} locked</styled.span>
);
const captionRightBulletInfo = <Caption>{fiatLockedBalance} locked</Caption>;

return (
<CryptoAssetItemLayout
availableBalance={balance.availableBalance}
availableBalance={totalBalance}
captionLeft="STX"
captionRightBulletInfo={showLockedBalance && captionRightBulletInfo}
fiatBalance={fiatAvailableBalance}
fiatBalance={fiatTotalBalance}
icon={<StxAvatarIcon />}
isLoading={isLoading}
isPrivate={isPrivate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const symbol = 'STX' satisfies CryptoCurrency;
export function StxSendForm() {
const stxMarketData = useCryptoCurrencyMarketDataMeanAverage(symbol);
const {
availableUnlockedBalance,
availableBalance,
initialValues,
previewTransaction,
sendMaxBalance,
Expand All @@ -27,13 +27,10 @@ export function StxSendForm() {

const amountField = (
<AmountField
balance={availableUnlockedBalance}
balance={availableBalance}
switchableAmount={<SendFiatValue marketData={stxMarketData} assetSymbol={symbol} />}
bottomInputOverlay={
<SendMaxButton
balance={availableUnlockedBalance}
sendMaxBalance={sendMaxBalance.toString()}
/>
<SendMaxButton balance={availableBalance} sendMaxBalance={sendMaxBalance.toString()} />
}
/>
);
Expand All @@ -53,7 +50,7 @@ export function StxSendForm() {
amountField={amountField}
selectedAssetField={selectedAssetField}
fees={fees}
availableTokenBalance={availableUnlockedBalance}
availableTokenBalance={availableBalance}
/>
</Content>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { STX_DECIMALS } from '@leather.io/constants';
import {
useCalculateStacksTxFees,
useStacksValidateFeeByNonce,
useStxAvailableUnlockedBalance,
useStxCryptoAssetBalance,
} from '@leather.io/query';
import { convertAmountToBaseUnit } from '@leather.io/utils';
import { convertAmountToBaseUnit, createMoney } from '@leather.io/utils';

import { logger } from '@shared/logger';
import { StacksSendFormValues } from '@shared/models/form.model';
Expand Down Expand Up @@ -37,34 +37,38 @@ export function useStxSendForm() {
const sendFormNavigate = useSendFormNavigate();
const address = useCurrentStacksAccountAddress();
const { changeFeeByNonce } = useStacksValidateFeeByNonce(address);
const availableUnlockedBalance = useStxAvailableUnlockedBalance(address);

// get stx balance
const { filteredBalanceQuery } = useStxCryptoAssetBalance(address);
const { data: balance } = filteredBalanceQuery;
const availableBalance = balance?.availableUnlockedBalance ?? createMoney(0, 'STX');

const sendMaxBalance = useMemo(
() =>
convertAmountToBaseUnit(
availableUnlockedBalance.amount.minus(stxFees?.estimates[1].fee.amount || 0),
availableBalance.amount.minus(stxFees?.estimates[1].fee.amount || 0),
STX_DECIMALS
),
[availableUnlockedBalance.amount, stxFees?.estimates]
[availableBalance.amount, stxFees?.estimates]
);

const { initialValues, checkFormValidation, recipient, memo, nonce } = useStacksCommonSendForm({
symbol: 'STX',
availableTokenBalance: availableUnlockedBalance,
availableTokenBalance: availableBalance,
});

return {
availableUnlockedBalance,
availableBalance,
initialValues,
onFormStateChange,
sendMaxBalance,
stxFees,

validationSchema: yup.object({
amount: stxAmountValidator(availableUnlockedBalance).concat(
stxAvailableBalanceValidator(availableUnlockedBalance)
amount: stxAmountValidator(availableBalance).concat(
stxAvailableBalanceValidator(availableBalance)
),
fee: stxFeeValidator(availableUnlockedBalance),
fee: stxFeeValidator(availableBalance),
recipient,
memo,
nonce,
Expand Down

0 comments on commit ec6d4e3

Please sign in to comment.