Getting Transaction Receipt
Retrieving Transaction Receipt by User Operation Hash
Parameters
Returns
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