Getting Transaction Receipt

Retrieving Transaction Receipt by User Operation Hash

The sdk.getTrxReceiptByUserOpHash(userOpHash, chainId) function is a crucial method in our SDK that allows you to retrieve the transaction receipt for a user operation hash. This receipt contains important information about the status and details of a transaction on the blockchain. You can use this function to confirm the successful execution of a transaction before proceeding with subsequent actions.

Parameters

  • userOpHash (string): The user operation hash for which you want to retrieve the transaction receipt. You will get this as an response of sendUserOp or sendUserOpBatch function.

  • chainId (number): The chain ID of the blockchain network where the transaction was executed.

Returns

  • If the transaction with the provided userOpHash exists on the blockchain and has been successfully executed, the function returns the transaction receipt object.

  • If the transaction has not been executed or does not exist, the function returns an error or a suitable indication of the transaction's absence.

Here's an example of how to achieve this using JavaScript's async/await and a simple retry loop:

const userOpHash = 'your-user-operation-hash';
const chainId = 1; // Replace with the appropriate chain ID for your blockchain network.
const maxRetryAttempts = 5; // Define the maximum number of retry attempts.
const retryIntervalMs = 5000; // Define the retry interval in milliseconds (e.g., 5 seconds).

async function waitForTransactionReceipt(userOpHash, chainId, maxAttempts, retryInterval) {
  let attempts = 0;
  let transactionReceipt = null;

  while (attempts < maxAttempts) {
    try {
      transactionReceipt = await sdk.getTrxReceiptByUserOpHash(userOpHash, chainId);
      if (transactionReceipt) {
        // Transaction was executed successfully.
        // You can access details such as block number, gas used, status, etc.
        console.log('Transaction Receipt:', transactionReceipt);
        return transactionReceipt; // Return the receipt and exit the loop.
      } else {
        // Transaction not found or not yet executed. Retry after a delay.
        console.log('Transaction not found or not executed. Retrying in', retryInterval / 1000, 'seconds...');
        await new Promise(resolve => setTimeout(resolve, retryInterval));
        attempts++;
      }
    } catch (error) {
      // Handle any errors that may occur during the retrieval process.
      console.error('Error:', error.message);
      // Retry after a delay.
      console.log('Retrying in', retryInterval / 1000, 'seconds...');
      await new Promise(resolve => setTimeout(resolve, retryInterval));
      attempts++;
    }
  }

  // If the maximum retry attempts are reached without success, handle the error.
  console.error('Maximum retry attempts reached. Transaction not confirmed.');
  return null; // Return null to indicate failure.
}

// Call the function to wait for the transaction receipt.
waitForTransactionReceipt(userOpHash, chainId, maxRetryAttempts, retryIntervalMs)
  .then(result => {
    if (result) {
      // Transaction was confirmed.
      // You can proceed with further actions here.
    } else {
      // Handle the case where the transaction was not confirmed.
      // Consider notifying the user or taking appropriate action.
    }
  });

Last updated