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 check inscription send utxos #4947

Merged
merged 1 commit into from
Feb 16, 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
24 changes: 21 additions & 3 deletions src/app/pages/send/ordinal-inscription/send-inscription-review.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bytesToHex } from '@noble/hashes/utils';
import { Box, Stack } from 'leather-styles/jsx';
import get from 'lodash.get';

import { decodeBitcoinTx } from '@shared/crypto/bitcoin/bitcoin.utils';
import { RouteUrls } from '@shared/route-urls';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
Expand All @@ -12,6 +13,10 @@ import { BaseDrawer } from '@app/components/drawer/base-drawer';
import { InfoCard, InfoCardRow, InfoCardSeparator } from '@app/components/info-card/info-card';
import { InscriptionPreview } from '@app/components/inscription-preview-card/components/inscription-preview';
import { useCurrentNativeSegwitUtxos } from '@app/query/bitcoin/address/utxos-by-address.hooks';
import {
filterOutIntentionalInscriptionsSpend,
useCheckInscribedUtxos,
} from '@app/query/bitcoin/transaction/use-check-utxos';
import { useAppDispatch } from '@app/store';
import { inscriptionSent } from '@app/store/ordinals/ordinals.slice';
import { Button } from '@app/ui/components/button/button';
Expand Down Expand Up @@ -40,8 +45,17 @@ export function SendInscriptionReview() {
const { refetch } = useCurrentNativeSegwitUtxos();
const { broadcastTx, isBroadcasting } = useBitcoinBroadcastTransaction();

const { checkIfUtxosListIncludesInscribed, isLoading } = useCheckInscribedUtxos({
// Filer out inscription txid from the check list
inputs: filterOutIntentionalInscriptionsSpend({
inputs: decodeBitcoinTx(bytesToHex(signedTx)).inputs,
inscriptions: [inscription],
}),
});

async function sendInscription() {
await broadcastTx({
checkForInscribedUtxos: checkIfUtxosListIncludesInscribed,
tx: bytesToHex(signedTx),
async onSuccess(txid: string) {
void analytics.track('broadcast_ordinal_transaction');
Expand All @@ -58,8 +72,12 @@ export function SendInscriptionReview() {
},
});
},
onError() {
navigate(`/${RouteUrls.SendOrdinalInscription}/${RouteUrls.SendOrdinalInscriptionError}`);
onError(e) {
navigate(`/${RouteUrls.SendOrdinalInscription}/${RouteUrls.SendOrdinalInscriptionError}`, {
state: {
error: e,
},
});
},
});
}
Expand All @@ -82,7 +100,7 @@ export function SendInscriptionReview() {
<InfoCardRow title="Fee" value={feeRowValue} />
</Stack>

<Button aria-busy={isBroadcasting} width="100%" onClick={sendInscription}>
<Button aria-busy={isLoading || isBroadcasting} width="100%" onClick={sendInscription}>
Confirm and send transaction
</Button>
</InfoCard>
Expand Down
19 changes: 19 additions & 0 deletions src/app/query/bitcoin/transaction/use-check-utxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useState } from 'react';
import * as btc from '@scure/btc-signer';
import { bytesToHex } from '@stacks/common';

import type { SupportedInscription } from '@shared/models/inscription.model';
import { isUndefined } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
Expand All @@ -23,6 +24,24 @@ interface UseCheckInscribedUtxosArgs {
blockTxAction?(): void;
}

interface FilterOutIntentionalInscriptionsSpendArgs {
inputs: btc.TransactionInput[];
inscriptions: SupportedInscription[];
}
export function filterOutIntentionalInscriptionsSpend({
inputs,
inscriptions,
}: FilterOutIntentionalInscriptionsSpendArgs) {
return inputs.filter(input => {
if (!input.txid) throw new Error('Transaction ID is missing in the input');
const inputTxid = bytesToHex(input.txid);

return inscriptions.every(inscription => {
return inscription.tx_id !== inputTxid;
});
});
}

export function useCheckInscribedUtxos({ inputs, blockTxAction }: UseCheckInscribedUtxosArgs) {
const client = useBitcoinClient();
const analytics = useAnalytics();
Expand Down
Loading