hirosystems / stacks.js

JavaScript libraries for identity, auth, storage and transactions on the Stacks blockchain.
https://stacks.js.org
MIT License
949 stars 309 forks source link

Add a waitForTransactionFunction #1738

Open 0x2me opened 1 month ago

0x2me commented 1 month ago

Problem

When working with transactions in a blockchain context, there is often a need to wait for a transaction to be confirmed by the network before proceeding with further actions. Currently, stacks lacks a straightforward way to wait for a transaction to be mined and achieve a specified number of confirmations.

Solution

I propose adding a waitForTransaction(txHash, confirmations) function to the codebase. This function would operate similarly to ethers.js' waitForTransaction method, allowing us to wait for a transaction identified by txHash to be confirmed by the network a specified number of times (confirmations). The function should:

  1. Accept a txHash (transaction hash) as input.
  2. Optionally accept a confirmations parameter, defaulting to 1 if not provided.
  3. Poll the blockchain at regular intervals to check the transaction status.
  4. Resolve once the transaction has been confirmed the specified number of times.
  5. Reject if the transaction fails or is not confirmed within a reasonable timeframe.

This utility would simplify the handling of transaction confirmations and reduce the likelihood of errors in the transaction flow.

Additional context

For reference, ethers.js provides a similar utility method: provider.waitForTransaction(txHash, confirmations). Implementing a similar function would streamline the development process, especially in scenarios where multiple confirmations are required before proceeding with further logic.