All files / app/api watch-tx-to-appear-in-api.ts

0% Statements 0/21
0% Branches 0/10
0% Functions 0/3
0% Lines 0/20

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                                                                           
import { Api } from '@api/api';
import { safeAwait } from '@stacks/ui';
import { delay } from '@utils/delay';
 
interface WatchForNewTxAppearArgs {
  nodeUrl: string;
  txId: string;
}
 
export function watchForNewTxToAppear({ txId, nodeUrl }: WatchForNewTxAppearArgs) {
  let retryCount = 0;
  return new Promise((resolve, reject) => {
    const fetchTx = async (): Promise<void> => {
      await delay(2000);
 
      Iif (retryCount > 5) {
        // In this instance, the API has failed to find the tx, but it could still
        // be pending this absolute delay provides a last ditch effort for it to update
        return reject(false);
      }
      retryCount += 1;
 
      const [error, txResponse] = await safeAwait(new Api(nodeUrl).getTxDetails(txId));
      Iif (error || !txResponse) return fetchTx();
 
      const tx = txResponse.data;
      Iif (tx.tx_status === 'abort_by_response' || tx.tx_status === 'abort_by_post_condition') {
        return reject(false);
      }
      Iif (tx.tx_status === 'pending' || tx.tx_status === 'success') {
        return resolve(tx);
      }
      return fetchTx();
    };
    void fetchTx();
  });
}