Skip to content

Commit

Permalink
refactor(ordinals): remove use of recursive look up of inscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Nov 6, 2024
1 parent 2b5a954 commit 5a77d53
Show file tree
Hide file tree
Showing 14 changed files with 1,528 additions and 1,479 deletions.
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,15 @@
"@coinbase/cbpay-js": "2.1.0",
"@fungible-systems/zone-file": "2.0.0",
"@hirosystems/token-metadata-api-client": "1.2.0",
"@hookform/resolvers": "3.9.0",
"@leather.io/bitcoin": "0.14.2",
"@leather.io/constants": "0.12.5",
"@leather.io/crypto": "1.6.6",
"@leather.io/models": "0.18.2",
"@leather.io/query": "2.17.0",
"@leather.io/stacks": "1.2.3",
"@leather.io/bitcoin": "0.15.0",
"@leather.io/constants": "0.13.0",
"@leather.io/crypto": "1.6.7",
"@leather.io/models": "0.18.3",
"@leather.io/query": "2.18.0",
"@leather.io/stacks": "1.2.4",
"@leather.io/tokens": "0.9.1",
"@leather.io/ui": "1.30.0",
"@leather.io/utils": "0.16.6",
"@leather.io/ui": "1.31.1",
"@leather.io/utils": "0.16.7",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.5.0",
"@noble/secp256k1": "2.1.0",
Expand Down Expand Up @@ -182,10 +181,10 @@
"@stitches/react": "1.2.8",
"@storybook/addon-styling-webpack": "1.0.0",
"@styled-system/theme-get": "5.1.2",
"@tanstack/query-async-storage-persister": "5.51.21",
"@tanstack/react-query": "5.51.23",
"@tanstack/react-query-devtools": "5.51.23",
"@tanstack/react-query-persist-client": "5.51.23",
"@tanstack/query-async-storage-persister": "5.59.16",
"@tanstack/react-query": "5.59.16",
"@tanstack/react-query-devtools": "5.59.16",
"@tanstack/react-query-persist-client": "5.59.16",
"@types/lodash.uniqby": "4.7.7",
"@typescript-eslint/eslint-plugin": "7.5.0",
"@zondax/ledger-stacks": "1.0.4",
Expand Down Expand Up @@ -265,7 +264,7 @@
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.4.1",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.1.13",
"@leather.io/rpc": "2.1.14",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand Down
2,701 changes: 1,377 additions & 1,324 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions src/app/features/collectibles/collectibles.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

import { useQueryClient } from '@tanstack/react-query';
Expand All @@ -24,7 +23,6 @@ export function Collectibles() {
const isNftMetadataEnabled = useConfigNftMetadataEnabled();
const queryClient = useQueryClient();
const isFetching = useIsFetchingCollectiblesRelatedQuery();
const [isLoadingMore, setIsLoadingMore] = useState(false);

return (
<CollectiblesLayout
Expand All @@ -44,7 +42,6 @@ export function Collectibles() {
ledger: null,
})}
isLoading={isFetching}
isLoadingMore={isLoadingMore}
onRefresh={() => void queryClient.refetchQueries({ type: 'active' })}
>
<CurrentBitcoinSignerLoader>{() => <AddCollectible />}</CurrentBitcoinSignerLoader>
Expand All @@ -59,7 +56,7 @@ export function Collectibles() {
{() => (
<>
<Stamps />
<Ordinals setIsLoadingMore={setIsLoadingMore} />
<Ordinals />
</>
)}
</CurrentBitcoinSignerLoader>
Expand Down
62 changes: 13 additions & 49 deletions src/app/features/collectibles/components/bitcoin/ordinals.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,33 @@
import { useEffect } from 'react';
import { useInView } from 'react-intersection-observer';

import { Box } from 'leather-styles/jsx';

import { useInscriptions } from '@leather.io/query';

import { analytics } from '@shared/utils/analytics';

import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentTaprootAccount } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
import { useInscriptions } from '@app/query/bitcoin/ordinals/inscriptions/inscriptions.query';
import { useCurrentBitcoinAccountXpubs } from '@app/store/accounts/blockchain/bitcoin/bitcoin.hooks';

import { Inscription } from './inscription';

interface OrdinalsProps {
setIsLoadingMore(isLoading: boolean): void;
}
export function Ordinals({ setIsLoadingMore }: OrdinalsProps) {
const account = useCurrentTaprootAccount();
const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSigner();

const result = useInscriptions({
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
});

const { ref: intersectionSentinel, inView } = useInView({
rootMargin: '0% 0% 20% 0%',
});

useEffect(() => {
async function fetchNextPage() {
if (!result.hasNextPage || result.isLoading || result.isFetchingNextPage) return;
try {
setIsLoadingMore(true);
await result.fetchNextPage();
} catch (e) {
// TO-DO: handle error
// console.log(e);
} finally {
setIsLoadingMore(false);
}
}
if (inView) {
void fetchNextPage();
}
}, [inView, result, setIsLoadingMore]);
export function Ordinals() {
const xpubs = useCurrentBitcoinAccountXpubs();
const results = useInscriptions({ xpubs });

useEffect(() => {
const inscriptionsLength = result.inscriptions.length || 0;
if (!results.inscriptions) return;
const inscriptionsLength = results.inscriptions.length;
if (inscriptionsLength > 0) {
void analytics.track('view_collectibles', {
ordinals_count: inscriptionsLength,
});
void analytics.identify({ ordinals_count: inscriptionsLength });
}
}, [result.inscriptions.length]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [results.inscriptions?.length]);

if (!result.inscriptions) return null;
if (results.isLoading) return null;

return (
<>
{result.inscriptions.map(inscription => (
<Inscription inscription={inscription} key={inscription.id} />
))}
<Box ref={intersectionSentinel} />
</>
);
return results.inscriptions.map(inscription => (
<Inscription inscription={inscription} key={inscription.id} />
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { token } from 'leather-styles/tokens';

import { ArrowRotateRightLeftIcon, Spinner } from '@leather.io/ui';

import { LoadingSpinner } from '@app/components/loading-spinner';

interface CollectiblesLayoutProps {
title: string;
isLoading: boolean;
Expand All @@ -18,7 +16,7 @@ export function CollectiblesLayout({
isLoading,
onRefresh,
subHeader,
isLoadingMore,

children,
}: CollectiblesLayoutProps) {
return (
Expand Down Expand Up @@ -48,7 +46,6 @@ export function CollectiblesLayout({
>
{children}
</Grid>
{isLoadingMore && <LoadingSpinner />}
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { useCurrentTaprootAccountBalance } from '@leather.io/query';
import { Link } from '@leather.io/ui';
import { formatMoney } from '@leather.io/utils';

import { useCurrentTaprootAccountBalance } from '@app/query/bitcoin/ordinals/inscriptions/inscriptions.query';
import { useRecoverUninscribedTaprootUtxosFeatureEnabled } from '@app/query/common/remote-config/remote-config.query';
import { useCurrentAccountIndex } from '@app/store/accounts/account';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentTaprootAccount } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';

const taprootSpendNotSupportedYetMsg = `
Expand All @@ -17,15 +14,7 @@ interface TaprootBalanceDisplayerProps {
onSelectRetrieveBalance(): void;
}
export function TaprootBalanceDisplayer({ onSelectRetrieveBalance }: TaprootBalanceDisplayerProps) {
const currentAccountIndex = useCurrentAccountIndex();
const account = useCurrentTaprootAccount();
const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSigner();

const balance = useCurrentTaprootAccountBalance({
currentAccountIndex,
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
});
const balance = useCurrentTaprootAccountBalance();
const isRecoverFeatureEnabled = useRecoverUninscribedTaprootUtxosFeatureEnabled();
if (!isRecoverFeatureEnabled) return null;
if (balance.amount.isLessThanOrEqualTo(0)) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { useNavigate } from 'react-router-dom';

import { Stack } from 'leather-styles/jsx';

import {
useBitcoinBroadcastTransaction,
useCurrentTaprootAccountBalance,
useCurrentTaprootAccountUninscribedUtxos,
} from '@leather.io/query';
import { useBitcoinBroadcastTransaction } from '@leather.io/query';
import { Link } from '@leather.io/ui';
import { delay, formatMoneyPadded, truncateMiddle } from '@leather.io/utils';

Expand All @@ -16,12 +12,11 @@ import { analytics } from '@shared/utils/analytics';
import { FormAddressDisplayer } from '@app/components/address-displayer/form-address-displayer';
import { InfoCardRow, InfoCardSeparator } from '@app/components/info-card/info-card';
import { useToast } from '@app/features/toasts/use-toast';
import { useCurrentAccountIndex } from '@app/store/accounts/account';
import {
useCurrentAccountNativeSegwitAddressIndexZero,
useCurrentAccountNativeSegwitIndexZeroSigner,
} from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentTaprootAccount } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
useCurrentTaprootAccountBalance,
useCurrentTaprootAccountUninscribedUtxos,
} from '@app/query/bitcoin/ordinals/inscriptions/inscriptions.query';
import { useCurrentAccountNativeSegwitAddressIndexZero } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';

import { RetrieveTaprootToNativeSegwitLayout } from './components/retrieve-taproot-to-native-segwit.layout';
import { useGenerateRetrieveTaprootFundsTx } from './use-generate-retrieve-taproot-funds-tx';
Expand All @@ -30,20 +25,9 @@ export function RetrieveTaprootToNativeSegwit() {
const toast = useToast();
const navigate = useNavigate();

const currentAccountIndex = useCurrentAccountIndex();
const account = useCurrentTaprootAccount();
const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSigner();
const balance = useCurrentTaprootAccountBalance({
currentAccountIndex,
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
});
const balance = useCurrentTaprootAccountBalance();
const recipient = useCurrentAccountNativeSegwitAddressIndexZero();
const uninscribedUtxos = useCurrentTaprootAccountUninscribedUtxos({
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
currentAccountIndex,
});
const uninscribedUtxos = useCurrentTaprootAccountUninscribedUtxos();

const { generateRetrieveTaprootFundsTx, fee } = useGenerateRetrieveTaprootFundsTx();
const { broadcastTx, isBroadcasting } = useBitcoinBroadcastTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,25 @@ import * as btc from '@scure/btc-signer';

import { extractAddressIndexFromPath } from '@leather.io/crypto';
import type { Money } from '@leather.io/models';
import {
useAverageBitcoinFeeRates,
useCurrentTaprootAccountUninscribedUtxos,
useNumberOfInscriptionsOnUtxo,
} from '@leather.io/query';
import { useAverageBitcoinFeeRates } from '@leather.io/query';
import { createMoney, sumNumbers } from '@leather.io/utils';

import { BtcSizeFeeEstimator } from '@app/common/transactions/bitcoin/fees/btc-size-fee-estimator';
import { useCurrentAccountIndex } from '@app/store/accounts/account';
import { useBitcoinScureLibNetworkConfig } from '@app/store/accounts/blockchain/bitcoin/bitcoin-keychain';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import {
useCurrentAccountTaprootSigner,
useCurrentTaprootAccount,
} from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
useCurrentTaprootAccountUninscribedUtxos,
useNumberOfInscriptionsOnUtxo,
} from '@app/query/bitcoin/ordinals/inscriptions/inscriptions.query';
import { useBitcoinScureLibNetworkConfig } from '@app/store/accounts/blockchain/bitcoin/bitcoin-keychain';
import { useCurrentAccountTaprootSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';

export function useGenerateRetrieveTaprootFundsTx() {
const networkMode = useBitcoinScureLibNetworkConfig();

const currentAccountIndex = useCurrentAccountIndex();
const account = useCurrentTaprootAccount();
const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSigner();

const uninscribedUtxos = useCurrentTaprootAccountUninscribedUtxos({
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
currentAccountIndex,
});
const uninscribedUtxos = useCurrentTaprootAccountUninscribedUtxos();

const createSigner = useCurrentAccountTaprootSigner();
const { data: feeRates } = useAverageBitcoinFeeRates();
const getNumberOfInscriptionOnUtxo = useNumberOfInscriptionsOnUtxo({
taprootKeychain: account?.keychain,
nativeSegwitAddress: nativeSegwitSigner.address,
});
const getNumberOfInscriptionOnUtxo = useNumberOfInscriptionsOnUtxo();

const fee = useMemo(() => {
if (!feeRates) return createMoney(0, 'BTC');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNativeSegwitAddressIndexDerivationPath } from '@leather.io/bitcoin';
import { makeNativeSegwitAddressIndexDerivationPath } from '@leather.io/bitcoin';
import type { BitcoinNetworkModes, Inscription } from '@leather.io/models';
import type { UtxoWithDerivationPath } from '@leather.io/query';

Expand All @@ -25,7 +25,7 @@ export function createUtxoFromInscription({
block_time: genesisTimestamp,
},
value: Number(value),
derivationPath: getNativeSegwitAddressIndexDerivationPath(
derivationPath: makeNativeSegwitAddressIndexDerivationPath(
network,
accountIndex,
inscriptionAddressIdx
Expand Down
Loading

0 comments on commit 5a77d53

Please sign in to comment.