All files / extension/src/app/features/stacks-transaction-request stacks-transaction-signer.tsx

0% Statements 0/22
0% Branches 0/23
0% Functions 0/5
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                                                                                                                                                                                                                                                           
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
 
import { StacksTransaction } from '@stacks/transactions';
import { Formik, FormikHelpers } from 'formik';
import { Flex } from 'leather-styles/jsx';
import * as yup from 'yup';
 
import { HIGH_FEE_WARNING_LEARN_MORE_URL_STX } from '@leather.io/constants';
import { FeeTypes } from '@leather.io/models';
import {
  useCalculateStacksTxFees,
  useNextNonce,
  useStxCryptoAssetBalance,
} from '@leather.io/query';
import { Link } from '@leather.io/ui';
import { stxToMicroStx } from '@leather.io/utils';
 
import { StacksTransactionFormValues } from '@shared/models/form.model';
import { RouteUrls } from '@shared/route-urls';
import { analytics } from '@shared/utils/analytics';
 
import { useOnMount } from '@app/common/hooks/use-on-mount';
import { stxFeeValidator } from '@app/common/validation/forms/fee-validators';
import { nonceValidator } from '@app/common/validation/nonce-validators';
import { NonceSetter } from '@app/components/nonce-setter';
import { RequestingTabClosedWarningMessage } from '@app/features/errors/requesting-tab-closed-error-msg';
import { ContractCallDetails } from '@app/features/stacks-transaction-request/contract-call-details/contract-call-details';
import { ContractDeployDetails } from '@app/features/stacks-transaction-request/contract-deploy-details/contract-deploy-details';
import { PageTop } from '@app/features/stacks-transaction-request/page-top';
import { PostConditionModeWarning } from '@app/features/stacks-transaction-request/post-condition-mode-warning';
import { PostConditions } from '@app/features/stacks-transaction-request/post-conditions/post-conditions';
import { StxTransferDetails } from '@app/features/stacks-transaction-request/stx-transfer-details/stx-transfer-details';
import { TransactionError } from '@app/features/stacks-transaction-request/transaction-error/transaction-error';
import { useCurrentStacksAccountAddress } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { useTransactionRequestState } from '@app/store/transactions/requests.hooks';
 
import { HighFeeSheet } from '../stacks-high-fee-warning/stacks-high-fee-dialog';
import { FeeForm } from './fee-form';
import { MinimalErrorMessage } from './minimal-error-message';
import { StacksTxSubmitAction } from './submit-action';
 
interface StacksTransactionSignerProps {
  stacksTransaction: StacksTransaction;
  disableFeeSelection?: boolean;
  disableNonceSelection?: boolean;
  isMultisig: boolean;
  onCancel(): void;
  onSignStacksTransaction(fee: number, nonce: number): Promise<void>;
}
export function StacksTransactionSigner({
  stacksTransaction,
  disableFeeSelection,
  disableNonceSelection,
  onSignStacksTransaction,
  isMultisig,
}: StacksTransactionSignerProps) {
  const transactionRequest = useTransactionRequestState();
  const { data: stxFees } = useCalculateStacksTxFees(stacksTransaction);
 
  const stxAddress = useCurrentStacksAccountAddress();
  const { filteredBalanceQuery } = useStxCryptoAssetBalance(stxAddress);
  const availableUnlockedBalance = filteredBalanceQuery.data?.availableUnlockedBalance;
  const navigate = useNavigate();
  const { data: nextNonce, status: nonceQueryStatus } = useNextNonce(stxAddress);
  const canSubmit = filteredBalanceQuery.status === 'success' && nonceQueryStatus === 'success';
 
  const { search } = useLocation();
 
  useOnMount(() => {
    void analytics.track('view_transaction_signing'), [];
  });
 
  async function onSubmit(
    values: StacksTransactionFormValues,
    formikHelpers: FormikHelpers<StacksTransactionFormValues>
  ) {
    formikHelpers.setSubmitting(true);
    await onSignStacksTransaction(stxToMicroStx(values.fee).toNumber(), Number(values.nonce));
    formikHelpers.setSubmitting(false);
  }
 
  if (!transactionRequest) return null;
 
  const validationSchema =
    !transactionRequest.sponsored && !disableFeeSelection && !isMultisig
      ? yup.object({
          fee: stxFeeValidator(availableUnlockedBalance),
          nonce: nonceValidator,
        })
      : yup.object({
          nonce: nonceValidator,
        });
 
  const isNonceAlreadySet = !Number.isNaN(transactionRequest.nonce);
 
  const initialValues: StacksTransactionFormValues = {
    fee: '',
    feeCurrency: 'STX',
    feeType: FeeTypes[FeeTypes.Middle],
    nonce: isNonceAlreadySet ? transactionRequest.nonce : nextNonce?.nonce,
  };
 
  return (
    <Flex alignItems="center" flexDirection="column" p="space.05" width="100%">
      <Formik
        initialValues={initialValues}
        onSubmit={onSubmit}
        validateOnChange={false}
        validateOnBlur={false}
        validateOnMount={false}
        validationSchema={validationSchema}
      >
        {() => (
          <>
            <PageTop />
            <RequestingTabClosedWarningMessage />
            <PostConditionModeWarning />
            <TransactionError />
            <PostConditions />
            {transactionRequest.txType === 'contract_call' && <ContractCallDetails />}
            {transactionRequest.txType === 'token_transfer' && <StxTransferDetails />}
            {transactionRequest.txType === 'smart_contract' && <ContractDeployDetails />}
 
            {!isNonceAlreadySet && <NonceSetter />}
            <FeeForm
              fees={stxFees}
              sbtcSponsorshipEligibility={{ isEligible: false }}
              defaultFeeValue={Number(transactionRequest?.fee || 0)}
              disableFeeSelection={disableFeeSelection}
            />
            {!disableNonceSelection && (
              <Link
                alignSelf="flex-end"
                my="space.04"
                onClick={() => navigate(RouteUrls.EditNonce + search)}
              >
                Edit nonce
              </Link>
            )}
            <MinimalErrorMessage />
            <StacksTxSubmitAction canSubmit={canSubmit} />
            <HighFeeSheet learnMoreUrl={HIGH_FEE_WARNING_LEARN_MORE_URL_STX} />
            <Outlet />
          </>
        )}
      </Formik>
    </Flex>
  );
}