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

refactor: updated ordinals tx functions #552

Closed
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
38 changes: 22 additions & 16 deletions src/app/components/confirmBtcTransactionComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Recipient,
SignedBtcTx,
signNonOrdinalBtcSendTransaction,
signOrdinalSendTransaction,
signOrdinalTransaction,
} from '@secretkeylabs/xverse-core/transactions/btc';
import { StoreState } from '@stores/index';
import { useMutation } from '@tanstack/react-query';
Expand All @@ -30,6 +30,7 @@ import { NumericFormat } from 'react-number-format';
import { useSelector } from 'react-redux';
import styled from 'styled-components';
import TransactionDetailComponent from '../transactionDetailComponent';
import { Inscription } from '@secretkeylabs/xverse-core/types';

const OuterContainer = styled.div`
display: flex;
Expand Down Expand Up @@ -123,6 +124,8 @@ interface Props {
isRestoreFundFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
isBtcSendBrowserTx?: boolean;
ordinal?: Inscription;
isRestoreOrdinalFlow?: boolean;
currentFeeRate: BigNumber;
setCurrentFee: (feeRate: BigNumber) => void;
setCurrentFeeRate: (feeRate: BigNumber) => void;
Expand All @@ -143,6 +146,8 @@ function ConfirmBtcTransactionComponent({
isRestoreFundFlow,
nonOrdinalUtxos,
isBtcSendBrowserTx,
ordinal,
isRestoreOrdinalFlow = false,
currentFeeRate,
setCurrentFee,
setCurrentFeeRate,
Expand All @@ -153,9 +158,8 @@ function ConfirmBtcTransactionComponent({
const { t } = useTranslation('translation');
const isGalleryOpen: boolean = document.documentElement.clientWidth > 360;
const [loading, setLoading] = useState(false);
const { btcAddress, selectedAccount, seedPhrase, network, btcFiatRate } = useSelector(
(state: StoreState) => state.walletState,
);
const { btcAddress, selectedAccount, seedPhrase, network, btcFiatRate, ordinalsAddress } =
useSelector((state: StoreState) => state.walletState);
const [showFeeSettings, setShowFeeSettings] = useState(false);
const [error, setError] = useState('');
const [signedTx, setSignedTx] = useState(signedTxHex);
Expand Down Expand Up @@ -210,21 +214,21 @@ function ConfirmBtcTransactionComponent({
data: ordinalData,
error: ordinalError,
mutate: ordinalMutate,
} = useMutation<SignedBtcTx, ResponseError, string>({
} = useMutation<SignedBtcTx | null, ResponseError, string>({
mutationFn: async (txFee) => {
const ordinalsUtxos = ordinals!.map((ord) => ord.utxo);

const newSignedTx = await signOrdinalSendTransaction(
recipients[0]?.address,
ordinalTxUtxo!,
if (!ordinal) return null;
const signedTx = await signOrdinalTransaction({
recipientAddress: recipients[0]?.address,
btcAddress,
Number(selectedAccount?.id),
ordinalsAddress,
accountIndex: Number(selectedAccount?.id),
seedPhrase,
network.type,
ordinalsUtxos,
new BigNumber(txFee),
);
return newSignedTx;
network: network.type,
ordinal,
fee: new BigNumber(txFee),
isRecover: isRestoreOrdinalFlow,
});
return signedTx;
},
});

Expand Down Expand Up @@ -408,6 +412,8 @@ function ConfirmBtcTransactionComponent({
isRestoreFlow={isRestoreFundFlow}
showFeeSettings={showFeeSettings}
setShowFeeSettings={setShowFeeSettings}
ordinal={ordinal}
isRestoreOrdinalFlow={isRestoreOrdinalFlow}
/>
</Container>
<ErrorContainer>
Expand Down
49 changes: 30 additions & 19 deletions src/app/components/transactionSetting/editFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { NumericFormat } from 'react-number-format';
import {
getBtcFees,
getBtcFeesForNonOrdinalBtcSend,
getBtcFeesForOrdinalSend,
getBtcFeesForOrdinalTransaction,
Recipient,
} from '@secretkeylabs/xverse-core/transactions/btc';
import { BtcUtxoDataResponse, ErrorCodes, UTXO } from '@secretkeylabs/xverse-core';
import useDebounce from '@hooks/useDebounce';
import useOrdinalsByAddress from '@hooks/useOrdinalsByAddress';
import { Inscription } from '@secretkeylabs/xverse-core/types';

const Container = styled.div((props) => ({
display: 'flex',
Expand Down Expand Up @@ -151,6 +152,9 @@ interface Props {
ordinalTxUtxo?: UTXO;
isRestoreFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
ordinal?: Inscription;
isRecover?: boolean;
isRestoreOrdinalFlow?: boolean;
feeMode: string;
error: string;
setIsLoading: () => void;
Expand All @@ -168,6 +172,9 @@ function EditFee({
ordinalTxUtxo,
isRestoreFlow,
nonOrdinalUtxos,
ordinal,
isRecover,
isRestoreOrdinalFlow = false,
feeMode,
error,
setIsLoading,
Expand Down Expand Up @@ -248,15 +255,17 @@ function EditFee({
setFeeRateInput(selectedFeeRate?.toString() || '');
setTotalFee(modifiedFee.toString());
}
} else if (type === 'Ordinals' && btcRecipients && ordinalTxUtxo) {
const { fee: modifiedFee, selectedFeeRate } = await getBtcFeesForOrdinalSend(
btcRecipients[0].address,
ordinalTxUtxo,
} else if (type === 'Ordinals' && btcRecipients && ordinal) {
const { fee: modifiedFee, selectedFeeRate } = await getBtcFeesForOrdinalTransaction({
recipientAddress: btcRecipients[0].address,
btcAddress,
network.type,
ordinalsUtxos || [],
mode,
);
ordinalsAddress,
network: network.type,
ordinal,
isRecover: isRestoreOrdinalFlow,
feeMode: mode,
});

setFeeRateInput(selectedFeeRate?.toString() || '');
setTotalFee(modifiedFee.toString());
}
Expand Down Expand Up @@ -296,7 +305,7 @@ function EditFee({
feeMode,
feeRateInput,
);
setFeeRateInput(selectedFeeRate?.toString());
setFeeRateInput(selectedFeeRate?.toString() || '');
setTotalFee(modifiedFee.toString());
} else if (btcRecipients && selectedAccount) {
const { fee: modifiedFee, selectedFeeRate } = await getBtcFees(
Expand All @@ -306,7 +315,7 @@ function EditFee({
feeMode,
feeRateInput,
);
setFeeRateInput(selectedFeeRate?.toString());
setFeeRateInput(selectedFeeRate?.toString() || '');
setTotalFee(modifiedFee.toString());
}
} catch (err: any) {
Expand All @@ -318,21 +327,23 @@ function EditFee({
} finally {
setIsNotLoading();
}
} else if (type === 'Ordinals' && btcRecipients && ordinalTxUtxo) {
} else if (type === 'Ordinals' && btcRecipients && ordinal) {
try {
setIsLoading();
setError('');

const { fee: modifiedFee, selectedFeeRate } = await getBtcFeesForOrdinalSend(
btcRecipients[0].address,
ordinalTxUtxo,
const { fee: modifiedFee, selectedFeeRate } = await getBtcFeesForOrdinalTransaction({
recipientAddress: btcRecipients[0].address,
btcAddress,
network.type,
ordinalsUtxos || [],
ordinalsAddress,
network: network.type,
ordinal,
isRecover: isRestoreOrdinalFlow,
feeMode,
feeRateInput,
);
setFeeRateInput(selectedFeeRate?.toString());
});

setFeeRateInput(selectedFeeRate?.toString() || '');
setTotalFee(modifiedFee.toString());
} catch (err: any) {
if (Number(err) === ErrorCodes.InSufficientBalance) {
Expand Down
7 changes: 7 additions & 0 deletions src/app/components/transactionSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BtcUtxoDataResponse, UTXO } from '@secretkeylabs/xverse-core';
import useWalletSelector from '@hooks/useWalletSelector';
import EditNonce from './editNonce';
import EditFee from './editFee';
import { Inscription } from '@secretkeylabs/xverse-core/types';

const ButtonContainer = styled.div((props) => ({
display: 'flex',
Expand Down Expand Up @@ -65,6 +66,8 @@ interface Props {
ordinalTxUtxo?: UTXO;
isRestoreFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
ordinal?: Inscription;
isRestoreOrdinalFlow?: boolean;
showFeeSettings: boolean;
setShowFeeSettings: (value: boolean) => void;
}
Expand All @@ -82,6 +85,8 @@ function TransactionSettingAlert({
ordinalTxUtxo,
isRestoreFlow,
nonOrdinalUtxos,
ordinal,
isRestoreOrdinalFlow = false,
showFeeSettings,
setShowFeeSettings,
}: Props) {
Expand Down Expand Up @@ -186,6 +191,8 @@ function TransactionSettingAlert({
ordinalTxUtxo={ordinalTxUtxo}
isRestoreFlow={isRestoreFlow}
nonOrdinalUtxos={nonOrdinalUtxos}
ordinal={ordinal}
isRestoreOrdinalFlow={isRestoreOrdinalFlow}
/>
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/app/screens/confirmOrdinalTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function ConfirmOrdinalTransaction() {
const btcClient = useBtcClient();
const [recipientAddress, setRecipientAddress] = useState('');
const location = useLocation();
const { fee, feePerVByte, signedTxHex, ordinalUtxo } = location.state;
const { fee, feePerVByte, signedTxHex, ordinalUtxo, ordinal, isRestoreOrdinalFlow } =
location.state;

const {
isLoading,
Expand Down Expand Up @@ -196,6 +197,8 @@ function ConfirmOrdinalTransaction() {
setCurrentFee={setCurrentFee}
currentFeeRate={currentFeeRate}
setCurrentFeeRate={setCurrentFeeRate}
ordinal={ordinal}
isRestoreOrdinalFlow={isRestoreOrdinalFlow}
>
<Container>
<NftContainer>
Expand Down
80 changes: 41 additions & 39 deletions src/app/screens/restoreFunds/restoreOrdinals/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import ActionButton from '@components/button';
import BottomTabBar from '@components/tabBar';
import TopRow from '@components/topRow';
import useOrdinalDataReducer from '@hooks/stores/useOrdinalReducer';
import useOrdinalsByAddress from '@hooks/useOrdinalsByAddress';
import useWalletSelector from '@hooks/useWalletSelector';
import { getBtcFiatEquivalent } from '@secretkeylabs/xverse-core/currency';
import {
SignedBtcTx,
signOrdinalSendTransaction,
} from '@secretkeylabs/xverse-core/transactions/btc';
import { BtcOrdinal, ErrorCodes, Inscription } from '@secretkeylabs/xverse-core/types';
import { useMutation } from '@tanstack/react-query';
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation, useNavigate } from 'react-router-dom';
import { BtcOrdinal, ErrorCodes, Inscription } from '@secretkeylabs/xverse-core/types';
import { getBtcFiatEquivalent } from '@secretkeylabs/xverse-core/currency';
import { SignedBtcTx, signOrdinalTransaction } from '@secretkeylabs/xverse-core/transactions/btc';
import useWalletSelector from '@hooks/useWalletSelector';
import useOrdinalsByAddress from '@hooks/useOrdinalsByAddress';
import useOrdinalDataReducer from '@hooks/stores/useOrdinalReducer';
import TopRow from '@components/topRow';
import styled from 'styled-components';
import ActionButton from '@components/button';
import BottomTabBar from '@components/tabBar';
import OrdinalRow from './ordinalRow';
import useOrdinalsApi from '@hooks/useOrdinalsApi';

const RestoreFundTitle = styled.h1((props) => ({
...props.theme.body_l,
Expand Down Expand Up @@ -57,29 +55,30 @@ function RestoreOrdinals() {
useWalletSelector();
const { setSelectedOrdinalDetails } = useOrdinalDataReducer();
const navigate = useNavigate();

const { ordinals } = useOrdinalsByAddress(btcAddress);
const [error, setError] = useState('');
const [transferringOrdinalId, setTransferringOrdinalId] = useState<string | null>(null);
const location = useLocation();
const isRestoreFundFlow = location.state?.isRestoreFundFlow;

const ordinalsUtxos = useMemo(() => ordinals?.map((ord) => ord.utxo), [ordinals]);
const ordinalsApi = useOrdinalsApi();

const {
isLoading,
error: transactionError,
mutateAsync,
} = useMutation<SignedBtcTx, string, BtcOrdinal>({
} = useMutation<SignedBtcTx, string, Inscription>({
mutationFn: async (ordinal) => {
const tx = await signOrdinalSendTransaction(
ordinalsAddress,
ordinal.utxo,
const tx = await signOrdinalTransaction({
recipientAddress: ordinalsAddress,
btcAddress,
Number(selectedAccount?.id),
ordinalsAddress,
accountIndex: Number(selectedAccount?.id),
seedPhrase,
network.type,
ordinalsUtxos!,
);
network: network.type,
ordinal,
isRecover: true,
});
return tx;
},
});
Expand All @@ -103,22 +102,25 @@ function RestoreOrdinals() {
};

const onClickTransfer = async (selectedOrdinal: BtcOrdinal, ordinalData: Inscription) => {
setTransferringOrdinalId(selectedOrdinal.id);

const signedTx = await mutateAsync(selectedOrdinal);
setSelectedOrdinalDetails(ordinalData);
navigate(`/confirm-ordinal-tx/${selectedOrdinal.id}`, {
state: {
signedTxHex: signedTx.signedTx,
recipientAddress: ordinalsAddress,
fee: signedTx.fee,
feePerVByte: signedTx.feePerVByte,
fiatFee: getBtcFiatEquivalent(signedTx.fee, btcFiatRate),
total: signedTx.total,
fiatTotal: getBtcFiatEquivalent(signedTx.total, btcFiatRate),
ordinalUtxo: selectedOrdinal.utxo,
},
});
if (selectedOrdinal) {
const selectedInscription: Inscription = await ordinalsApi.getInscription(selectedOrdinal.id);
const signedTx = await mutateAsync(selectedInscription);
setSelectedOrdinalDetails(ordinalData);
navigate(`/confirm-ordinal-tx/${selectedOrdinal.id}`, {
state: {
signedTxHex: signedTx.signedTx,
recipientAddress: ordinalsAddress,
fee: signedTx.fee,
feePerVByte: signedTx.feePerVByte,
fiatFee: getBtcFiatEquivalent(signedTx.fee, btcFiatRate),
total: signedTx.total,
fiatTotal: getBtcFiatEquivalent(signedTx.total, btcFiatRate),
ordinalUtxo: selectedOrdinal.utxo,
ordinal: selectedInscription,
isRestoreOrdinalFlow: true,
},
});
}
};

return (
Expand Down
Loading