All files / app/api watch-contract-execution.ts

0% Statements 0/20
0% Branches 0/14
0% Functions 0/4
0% Lines 0/18

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                                                                         
import { Api } from '@api/api';
import { safeAwait } from '@utils/safe-await';
 
const defaultErrorMsg = 'Stacking contract failed to execute';
 
interface WatchContractExecutionArgs {
  nodeUrl: string;
  txId: string;
}
export function watchContractExecution(args: WatchContractExecutionArgs) {
  const { nodeUrl, txId } = args;
  const pollingInterval = 1000;
  return new Promise((resolve, reject) => {
    const fetchTx = async (timeoutInterval: number) => {
      const [error, txResponse] = await safeAwait(new Api(nodeUrl).getTxDetails(txId));
 
      Iif (!txResponse || txResponse.data.tx_status === 'pending') return;
      const tx = txResponse.data;
 
      Iif (
        error ||
        tx.tx_status === 'abort_by_response' ||
        tx.tx_status === 'abort_by_post_condition'
      ) {
        clearInterval(timeoutInterval);
        return reject(error?.message || defaultErrorMsg);
      }
 
      Iif (tx.tx_status === 'success') {
        clearInterval(timeoutInterval);
        return resolve(true);
      }
    };
    const interval: number = window.setInterval(() => void fetchTx(interval), pollingInterval);
  });
}