Skip to content

Commit

Permalink
fix: ledger bitcoin query
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Jun 26, 2024
1 parent d3595f4 commit a11f6a6
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"@leather.io/constants": "0.8.0",
"@leather.io/crypto": "1.0.2",
"@leather.io/models": "0.10.0",
"@leather.io/query": "0.9.0",
"@leather.io/query": "0.9.1",
"@leather.io/tokens": "0.6.0",
"@leather.io/ui": "1.5.0",
"@leather.io/utils": "0.9.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions src/app/common/hooks/balance/use-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
// get stx balance
const {
data: balance,
isFetching: isFetchingStacksBalance,
isLoading,
isPending,
isFetching: isFetchingStxBalance,
isLoading: isLoadingStxBalance,
isPending: isPendingStxBalance,
} = useStxCryptoAssetBalance(stxAddress);
const stxBalance = balance ? balance.totalBalance : createMoney(0, 'STX');

// get btc balance
const { balance: btcBalance, query: btcQueryResult } =
useBtcCryptoAssetBalanceNativeSegwit(btcAddress);
const {
balance: btcBalance,
isFetching: isFetchingBtcBalance,
isLoading: isLoadingBtcBalance,
isPending: isPendingBtcBalance,
} = useBtcCryptoAssetBalanceNativeSegwit(btcAddress);

return useMemo(() => {
// calculate total balance
Expand All @@ -37,24 +41,24 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)

const totalBalance = { ...stxUsdAmount, amount: stxUsdAmount.amount.plus(btcUsdAmount.amount) };
return {
isFetching: isFetchingStxBalance || isFetchingBtcBalance,
isLoading: isLoadingStxBalance || isLoadingBtcBalance,
isPending: isPendingStxBalance || isPendingBtcBalance,
totalBalance,
totalUsdBalance: i18nFormatCurrency(
totalBalance,
totalBalance.amount.isGreaterThanOrEqualTo(100_000) ? 0 : 2
),
isLoading: isLoading || btcQueryResult.isLoading,
isPending: isPending || btcQueryResult.isPending,
isFetching: isFetchingStacksBalance || btcQueryResult.isFetching,
};
}, [
btcBalance.availableBalance,
btcMarketData,
btcQueryResult.isFetching,
btcQueryResult.isLoading,
btcQueryResult.isPending,
isFetchingStacksBalance,
isLoading,
isPending,
isFetchingBtcBalance,
isFetchingStxBalance,
isLoadingBtcBalance,
isLoadingStxBalance,
isPendingBtcBalance,
isPendingStxBalance,
stxBalance,
stxMarketData,
]);
Expand Down
20 changes: 14 additions & 6 deletions src/app/components/loaders/btc-balance-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { BtcCryptoAssetBalance } from '@leather.io/models';
import { isFetchedWithSuccess, isInitializingData } from '@leather.io/query';
import { BtcAvatarIcon } from '@leather.io/ui';

import { useBtcCryptoAssetBalanceNativeSegwit } from '@app/query/bitcoin/balance/btc-balance-native-segwit.hooks';
Expand All @@ -9,12 +8,21 @@ import { CryptoAssetItemPlaceholder } from '../crypto-asset-item/crypto-asset-it

interface BtcBalanceLoaderProps {
address: string;
children(balance: BtcCryptoAssetBalance, isInitialLoading: boolean): React.ReactNode;
children(balance: BtcCryptoAssetBalance): React.ReactNode;
}
export function BtcBalanceLoader({ address, children }: BtcBalanceLoaderProps) {
const { balance, query: result } = useBtcCryptoAssetBalanceNativeSegwit(address);
if (isInitializingData(result)) return <CryptoAssetItemPlaceholder />;
if (!isFetchedWithSuccess(result))
const { balance } = useBtcCryptoAssetBalanceNativeSegwit(address);
return children(balance);
}

interface BtcAssetItemBalanceLoaderProps {
address: string;
children(balance: BtcCryptoAssetBalance, isLoading: boolean): React.ReactNode;
}
export function BtcAssetItemBalanceLoader({ address, children }: BtcAssetItemBalanceLoaderProps) {
const { balance, isError, isLoading } = useBtcCryptoAssetBalanceNativeSegwit(address);
if (isLoading) return <CryptoAssetItemPlaceholder />;
if (isError)
return <CryptoAssetItemError caption="BTC" icon={<BtcAvatarIcon />} title="Bitcoin" />;
return children(balance, result.isInitialLoading);
return children(balance, isLoading);
}
4 changes: 2 additions & 2 deletions src/app/components/loaders/sip10-tokens-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ interface Sip10TokensLoaderProps {
children(isLoading: boolean, tokens: Sip10TokenAssetDetails[]): React.ReactNode;
}
export function Sip10TokensLoader({ address, filter, children }: Sip10TokensLoaderProps) {
const { isInitialLoading, tokens = [] } = useFilteredSip10Tokens({ address, filter });
return children(isInitialLoading, tokens);
const { isLoading, tokens = [] } = useFilteredSip10Tokens({ address, filter });
return children(isLoading, tokens);
}
2 changes: 1 addition & 1 deletion src/app/components/loaders/stacks-account-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function CurrentStacksAccountLoader({
fallback,
}: CurrentStacksAccountLoaderProps) {
const currentAccount = useCurrentStacksAccount();
if (!currentAccount) return fallback ? fallback : null;
if (!currentAccount) return fallback;
return children(currentAccount);
}

Expand Down
10 changes: 5 additions & 5 deletions src/app/components/loaders/stx-balance-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { StxAvatarIcon } from '@leather.io/ui';
import { CryptoAssetItemError } from '../crypto-asset-item/crypto-asset-item-error';
import { CryptoAssetItemPlaceholder } from '../crypto-asset-item/crypto-asset-item-placeholder';

interface StxBalanceLoaderProps {
interface StxAssetItemBalanceLoaderProps {
address: string;
children(balance: StxCryptoAssetBalance, isInitialLoading: boolean): React.ReactNode;
children(balance: StxCryptoAssetBalance, isLoading: boolean): React.ReactNode;
}
export function StxBalanceLoader({ address, children }: StxBalanceLoaderProps) {
export function StxAssetItemBalanceLoader({ address, children }: StxAssetItemBalanceLoaderProps) {
const result = useStxCryptoAssetBalance(address);
if (isInitializingData(result)) return <CryptoAssetItemPlaceholder />;
if (isErrorTooManyRequests(result))
Expand All @@ -28,6 +28,6 @@ export function StxBalanceLoader({ address, children }: StxBalanceLoaderProps) {
);
if (!isFetchedWithSuccess(result))
return <CryptoAssetItemError caption="STX" icon={<StxAvatarIcon />} title="Stacks" />;
const { data: balance, isInitialLoading } = result;
return children(balance, isInitialLoading);
const { data: balance, isLoading } = result;
return children(balance, isLoading);
}
24 changes: 11 additions & 13 deletions src/app/features/activity-list/activity-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export function ActivityList() {
const updateSubmittedTxs = useUpdateSubmittedTransactions();

const [
{ isInitialLoading: isInitialLoadingNsBitcoinTransactions, data: nsBitcoinTransactions = [] },
{ isInitialLoading: isInitialLoadingTrBitcoinTransactions, data: trBitcoinTransactions = [] },
{ isLoading: isLoadingNsBitcoinTransactions, data: nsBitcoinTransactions = [] },
{ isLoading: isLoadingTrBitcoinTransactions, data: trBitcoinTransactions = [] },
] = useGetBitcoinTransactionsByAddressesQuery([nsBitcoinAddress, trBitcoinAddress]);

const [{ data: nsPendingTxs = [] }, { data: trPendingTxs = [] }] = useBitcoinPendingTransactions([
Expand All @@ -63,12 +63,10 @@ export function ActivityList() {
[nsPendingTxs, trPendingTxs]
);

const { isLoading: isLoadingStacksTransactions, data: stacksTransactionsWithTransfers } =
useGetAccountTransactionsWithTransfersQuery(stxAddress);
const {
isInitialLoading: isInitialLoadingStacksTransactions,
data: stacksTransactionsWithTransfers,
} = useGetAccountTransactionsWithTransfersQuery(stxAddress);
const {
query: { isInitialLoading: isInitialLoadingStacksPendingTransactions },
query: { isLoading: isLoadingStacksPendingTransactions },
transactions: stacksPendingTransactions,
} = useStacksPendingTransactions(stxAddress);
const submittedTransactions = useSubmittedTransactions();
Expand All @@ -78,11 +76,11 @@ export function ActivityList() {
updateSubmittedTxs(stacksPendingTransactions);
}, [stacksPendingTransactions, updateSubmittedTxs]);

const isInitialLoading =
isInitialLoadingNsBitcoinTransactions ||
isInitialLoadingTrBitcoinTransactions ||
isInitialLoadingStacksTransactions ||
isInitialLoadingStacksPendingTransactions;
const isLoading =
isLoadingNsBitcoinTransactions ||
isLoadingTrBitcoinTransactions ||
isLoadingStacksTransactions ||
isLoadingStacksPendingTransactions;

const transactionListBitcoinTxs = useMemo(() => {
return convertBitcoinTxsToListType(
Expand All @@ -107,7 +105,7 @@ export function ActivityList() {

const hasTxs = hasSubmittedTransactions || hasPendingTransactions || hasTransactions;

if (isInitialLoading)
if (isLoading)
return (
<ActivityListTabWrapper padContent>
<LoadingSpinner />
Expand Down
28 changes: 14 additions & 14 deletions src/app/features/asset-list/asset-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
BitcoinTaprootAccountLoader,
} from '@app/components/loaders/bitcoin-account-loader';
import { Brc20TokensLoader } from '@app/components/loaders/brc20-tokens-loader';
import { BtcBalanceLoader } from '@app/components/loaders/btc-balance-loader';
import { BtcAssetItemBalanceLoader } from '@app/components/loaders/btc-balance-loader';
import { RunesLoader } from '@app/components/loaders/runes-loader';
import { Sip10TokensLoader } from '@app/components/loaders/sip10-tokens-loader';
import { Src20TokensLoader } from '@app/components/loaders/src20-tokens-loader';
import { CurrentStacksAccountLoader } from '@app/components/loaders/stacks-account-loader';
import { Stx20TokensLoader } from '@app/components/loaders/stx20-tokens-loader';
import { StxBalanceLoader } from '@app/components/loaders/stx-balance-loader';
import { StxAssetItemBalanceLoader } from '@app/components/loaders/stx-balance-loader';
import { Brc20TokenAssetList } from '@app/features/asset-list/bitcoin/brc20-token-asset-list/brc20-token-asset-list';
import { RunesAssetList } from '@app/features/asset-list/bitcoin/runes-asset-list/runes-asset-list';
import { Src20TokenAssetList } from '@app/features/asset-list/bitcoin/src20-token-asset-list/src20-token-asset-list';
Expand Down Expand Up @@ -54,15 +54,15 @@ export function AssetList({ onSelectAsset, variant = 'read-only' }: AssetListPro
}
>
{nativeSegwitAccount => (
<BtcBalanceLoader address={nativeSegwitAccount.address}>
{(balance, isInitialLoading) => (
<BtcAssetItemBalanceLoader address={nativeSegwitAccount.address}>
{(balance, isLoading) => (
<BtcCryptoAssetItem
balance={balance}
isLoading={isInitialLoading}
isLoading={isLoading}
onSelectAsset={onSelectAsset}
/>
)}
</BtcBalanceLoader>
</BtcAssetItemBalanceLoader>
)}
</BitcoinNativeSegwitAccountLoader>

Expand All @@ -86,22 +86,22 @@ export function AssetList({ onSelectAsset, variant = 'read-only' }: AssetListPro
>
{account => (
<>
<StxBalanceLoader address={account.address}>
{(balance, isInitialLoading) => (
<StxAssetItemBalanceLoader address={account.address}>
{(balance, isLoading) => (
<StxCryptoAssetItem
balance={balance}
isLoading={isInitialLoading}
isLoading={isLoading}
onSelectAsset={onSelectAsset}
/>
)}
</StxBalanceLoader>
</StxAssetItemBalanceLoader>
<Sip10TokensLoader
address={account.address}
filter={variant === 'interactive' ? 'all' : 'supported'}
>
{(isInitialLoading, tokens) => (
{(isLoading, tokens) => (
<Sip10TokenAssetList
isLoading={isInitialLoading}
isLoading={isLoading}
tokens={tokens}
onSelectAsset={onSelectAsset}
/>
Expand Down Expand Up @@ -149,8 +149,8 @@ export function AssetList({ onSelectAsset, variant = 'read-only' }: AssetListPro
<CurrentStacksAccountLoader>
{account => (
<Sip10TokensLoader address={account.address} filter="unsupported">
{(isInitialLoading, tokens) => (
<Sip10TokenAssetListUnsupported isLoading={isInitialLoading} tokens={tokens} />
{(isLoading, tokens) => (
<Sip10TokenAssetListUnsupported isLoading={isLoading} tokens={tokens} />
)}
</Sip10TokensLoader>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Brc20TokenAssetListProps {
}
export function Brc20TokenAssetList({ tokens, variant }: Brc20TokenAssetListProps) {
const navigate = useNavigate();
const { balance, query: result } = useCurrentBtcCryptoAssetBalanceNativeSegwit();
const { balance, isLoading } = useCurrentBtcCryptoAssetBalanceNativeSegwit();

const hasPositiveBtcBalanceForFees =
variant === 'interactive' && balance.availableBalance.amount.isGreaterThan(0);
Expand All @@ -49,7 +49,7 @@ export function Brc20TokenAssetList({ tokens, variant }: Brc20TokenAssetListProp
availableBalance={token.balance.availableBalance}
captionLeft={token.info.name.toUpperCase()}
icon={<Brc20AvatarIcon />}
isLoading={result.isInitialLoading}
isLoading={isLoading}
key={token.info.symbol}
onSelectAsset={
hasPositiveBtcBalanceForFees ? () => navigateToBrc20SendForm(token) : undefined
Expand Down
20 changes: 10 additions & 10 deletions src/app/pages/fund/choose-asset-to-fund/choose-asset-to-fund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Stack, styled } from 'leather-styles/jsx';
import { RouteUrls } from '@shared/route-urls';

import { BitcoinNativeSegwitAccountLoader } from '@app/components/loaders/bitcoin-account-loader';
import { BtcBalanceLoader } from '@app/components/loaders/btc-balance-loader';
import { BtcAssetItemBalanceLoader } from '@app/components/loaders/btc-balance-loader';
import { CurrentStacksAccountLoader } from '@app/components/loaders/stacks-account-loader';
import { StxBalanceLoader } from '@app/components/loaders/stx-balance-loader';
import { StxAssetItemBalanceLoader } from '@app/components/loaders/stx-balance-loader';
import { BtcCryptoAssetItem } from '@app/features/asset-list/bitcoin/btc-crypto-asset-item/btc-crypto-asset-item';
import { StxCryptoAssetItem } from '@app/features/asset-list/stacks/stx-crypo-asset-item/stx-crypto-asset-item';
import { Card } from '@app/ui/layout/card/card';
Expand All @@ -35,29 +35,29 @@ export function ChooseCryptoAssetToFund() {
<Stack pb="space.04" px="space.05">
<BitcoinNativeSegwitAccountLoader current>
{signer => (
<BtcBalanceLoader address={signer.address}>
{(balance, isInitialLoading) => (
<BtcAssetItemBalanceLoader address={signer.address}>
{(balance, isLoading) => (
<BtcCryptoAssetItem
balance={balance}
isLoading={isInitialLoading}
isLoading={isLoading}
onSelectAsset={() => navigateToFund('BTC')}
/>
)}
</BtcBalanceLoader>
</BtcAssetItemBalanceLoader>
)}
</BitcoinNativeSegwitAccountLoader>

<CurrentStacksAccountLoader>
{account => (
<StxBalanceLoader address={account.address}>
{(balance, isInitialLoading) => (
<StxAssetItemBalanceLoader address={account.address}>
{(balance, isLoading) => (
<StxCryptoAssetItem
balance={balance}
isLoading={isInitialLoading}
isLoading={isLoading}
onSelectAsset={() => navigateToFund('STX')}
/>
)}
</StxBalanceLoader>
</StxAssetItemBalanceLoader>
)}
</CurrentStacksAccountLoader>
</Stack>
Expand Down
4 changes: 1 addition & 3 deletions src/app/pages/home/components/send-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ import { RouteUrls } from '@shared/route-urls';
import { useWalletType } from '@app/common/use-wallet-type';
import { whenPageMode } from '@app/common/utils';
import { openIndexPageInNewTab } from '@app/common/utils/open-in-new-tab';
import { useBtcCryptoAssetBalanceNativeSegwit } from '@app/query/bitcoin/balance/btc-balance-native-segwit.hooks';
import { useCurrentAccountNativeSegwitIndexZeroSignerNullable } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentStacksAccountAddress } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';

function SendButtonSuspense() {
const navigate = useNavigate();
const { whenWallet } = useWalletType();
const btcAddress = useCurrentAccountNativeSegwitIndexZeroSignerNullable()?.address;
const { balance: btcBalance } = useBtcCryptoAssetBalanceNativeSegwit(btcAddress ?? '');
const stxAddress = useCurrentStacksAccountAddress();
const { data: stxBalance } = useStxCryptoAssetBalance(stxAddress);
const stacksFtAssets = useTransferableSip10Tokens(stxAddress);

const isDisabled = !btcBalance && !stxBalance && stacksFtAssets?.length === 0;
const isDisabled = !btcAddress && !stxBalance && stacksFtAssets?.length === 0;

return (
<IconButton
Expand Down
Loading

0 comments on commit a11f6a6

Please sign in to comment.