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 7 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
34 changes: 18 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,7 @@ interface Props {
isRestoreFundFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
isBtcSendBrowserTx?: boolean;
ordinal?: Inscription;
currentFeeRate: BigNumber;
setCurrentFee: (feeRate: BigNumber) => void;
setCurrentFeeRate: (feeRate: BigNumber) => void;
Expand All @@ -143,6 +145,7 @@ function ConfirmBtcTransactionComponent({
isRestoreFundFlow,
nonOrdinalUtxos,
isBtcSendBrowserTx,
ordinal,
currentFeeRate,
setCurrentFee,
setCurrentFeeRate,
Expand All @@ -153,9 +156,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 +212,20 @@ 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),
});
return signedTx;
},
});

Expand Down Expand Up @@ -408,6 +409,7 @@ function ConfirmBtcTransactionComponent({
isRestoreFlow={isRestoreFundFlow}
showFeeSettings={showFeeSettings}
setShowFeeSettings={setShowFeeSettings}
ordinal={ordinal}
/>
</Container>
<ErrorContainer>
Expand Down
43 changes: 26 additions & 17 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 @@ -150,6 +151,8 @@ interface Props {
ordinalTxUtxo?: UTXO;
isRestoreFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
ordinal?: Inscription;
isRecover?: boolean;
error: string;
setIsLoading: () => void;
setIsNotLoading: () => void;
Expand All @@ -166,6 +169,8 @@ function EditFee({
ordinalTxUtxo,
isRestoreFlow,
nonOrdinalUtxos,
ordinal,
isRecover,
error,
setIsLoading,
setIsNotLoading,
Expand Down Expand Up @@ -246,15 +251,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,
feeMode: mode,
});

setFeeRateInput(selectedFeeRate?.toString());
setTotalFee(modifiedFee.toString());
}
Expand Down Expand Up @@ -316,20 +323,22 @@ 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 || [],
selectedOption,
ordinalsAddress,
network: network.type,
ordinal,
isRecover,
feeMode: selectedOption,
feeRateInput,
);
});

setFeeRateInput(selectedFeeRate?.toString());
setTotalFee(modifiedFee.toString());
} catch (err: any) {
Expand Down
4 changes: 4 additions & 0 deletions src/app/components/transactionSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isCustomFeesAllowed, Recipient } from '@secretkeylabs/xverse-core/trans
import { BtcUtxoDataResponse, UTXO } from '@secretkeylabs/xverse-core';
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 @@ -64,6 +65,7 @@ interface Props {
ordinalTxUtxo?: UTXO;
isRestoreFlow?: boolean;
nonOrdinalUtxos?: BtcUtxoDataResponse[];
ordinal?: Inscription;
showFeeSettings: boolean;
setShowFeeSettings: (value: boolean) => void;
}
Expand All @@ -82,6 +84,7 @@ function TransactionSettingAlert({
ordinalTxUtxo,
isRestoreFlow,
nonOrdinalUtxos,
ordinal,
showFeeSettings,
setShowFeeSettings,
}: Props) {
Expand Down Expand Up @@ -173,6 +176,7 @@ function TransactionSettingAlert({
ordinalTxUtxo={ordinalTxUtxo}
isRestoreFlow={isRestoreFlow}
nonOrdinalUtxos={nonOrdinalUtxos}
ordinal={ordinal}
/>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/screens/confirmOrdinalTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ 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 } = location.state;

const {
isLoading,
Expand Down Expand Up @@ -197,6 +197,7 @@ function ConfirmOrdinalTransaction() {
setCurrentFee={setCurrentFee}
currentFeeRate={currentFeeRate}
setCurrentFeeRate={setCurrentFeeRate}
ordinal={ordinal}
>
<Container>
<NftContainer>
Expand Down
79 changes: 40 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,24 @@ 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,
},
});
}
};

return (
Expand Down
Loading