All files / app/modals/revoke-delegation revoke-delegation-modal.tsx

0% Statements 0/42
0% Branches 0/2
0% Functions 0/9
0% Lines 0/37

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                                                                                                                                               
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { watchForNewTxToAppear } from '@api/watch-tx-to-appear-in-api';
import { REVOKE_DELEGATION_TX_SIZE_BYTES } from '@constants/index';
import { useApi } from '@hooks/use-api';
import { useBroadcastTx } from '@hooks/use-broadcast-tx';
import { useCalculateFee } from '@hooks/use-calculate-fee';
import { useMempool } from '@hooks/use-mempool';
import { useStackingClient } from '@hooks/use-stacking-client';
import { TxSigningModal } from '@modals/tx-signing-modal/tx-signing-modal';
import { PostCoreNodeTransactionsError } from '@stacks/stacks-blockchain-api-types';
import { ContractCallOptions, StacksTransaction } from '@stacks/transactions';
import { homeActions } from '@store/home';
import { selectPoxInfo } from '@store/stacking';
import { safeAwait } from '@utils/safe-await';
import React, { FC, useState, useCallback, useMemo } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { useSelector, useDispatch } from 'react-redux';
 
export const RevokeDelegationModal: FC = () => {
  const dispatch = useDispatch();
 
  useHotkeys('esc', () => void dispatch(homeActions.closeRevokeDelegationModal()));
  const closeModal = () => dispatch(homeActions.closeRevokeDelegationModal());
 
  const api = useApi();
  const { stackingClient } = useStackingClient();
  const { broadcastTx, isBroadcasting } = useBroadcastTx();
  const { refetch: refetchMempool } = useMempool();
  const calcFee = useCalculateFee();
  const poxInfo = useSelector(selectPoxInfo);
 
  const [nodeResponseError, setNodeResponseError] = useState<PostCoreNodeTransactionsError | null>(
    null
  );
 
  const revocationTxOptions = useMemo((): ContractCallOptions => {
    Iif (!poxInfo) throw new Error('`poxInfo` undefined');
    return {
      ...stackingClient.getRevokeDelegateStxOptions(poxInfo.contract_id),
      fee: calcFee(REVOKE_DELEGATION_TX_SIZE_BYTES).toString(),
    };
  }, [calcFee, poxInfo, stackingClient]);
 
  const revokeDelegation = useCallback(
    (signedTx: StacksTransaction) =>
      broadcastTx({
        onSuccess: async txId => {
          await safeAwait(watchForNewTxToAppear({ txId, nodeUrl: api.baseUrl }));
          await refetchMempool();
          dispatch(homeActions.closeRevokeDelegationModal());
        },
        onFail: (error?: any) => {
          Iif (error) setNodeResponseError(error);
        },
        tx: signedTx,
      }),
    [api.baseUrl, broadcastTx, dispatch, refetchMempool]
  );
 
  return (
    <TxSigningModal
      action="revoke delegation"
      txDetails={revocationTxOptions}
      isBroadcasting={isBroadcasting}
      error={nodeResponseError}
      onTransactionSigned={tx => revokeDelegation(tx)}
      onTryAgain={() => setNodeResponseError(null)}
      onClose={closeModal}
    />
  );
};