Last active
August 26, 2025 11:17
-
-
Save Teepheh-Git/95c85cc640b8233f7d2661b3006b97ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require("axios"); | |
| const BASE_URL = "https://testnet.mirrornode.hedera.com/api/v1"; // Use mainnet for production | |
| async function getTransactionReceipt(transactionHashOrId) { | |
| try { | |
| const transactionUrl = `${BASE_URL}/transactions/${transactionHashOrId}`; | |
| const response = await axios.get(transactionUrl); | |
| if (response.status !== 200) { | |
| throw new Error(`Failed to fetch transaction: ${response.statusText}`); | |
| } | |
| const transactions = response.data.transactions; | |
| if (!transactions || transactions.length === 0) { | |
| throw new Error("No transaction found for the provided hash or ID."); | |
| } | |
| const transaction = transactions[0]; // Usually, you take the first one | |
| let taskState; | |
| switch (transaction.result) { | |
| case "SUCCESS": | |
| taskState = "ExecSuccess"; | |
| break; | |
| case "INVALID_TRANSACTION": | |
| case "INSUFFICIENT_PAYER_BALANCE": | |
| case "DUPLICATE_TRANSACTION": | |
| case "CONTRACT_REVERT_EXECUTED": | |
| taskState = "ExecReverted"; | |
| break; | |
| default: | |
| taskState = "WaitingForConfirmation"; | |
| } | |
| return { | |
| taskId: { | |
| taskState, | |
| accountId: transaction.entity_id || null, | |
| contractId: transaction.name === "CONTRACTCREATEINSTANCE" ? transaction.entity_id : null, | |
| tokenId: transaction.name === "TOKENCREATION" ? transaction.entity_id : null, | |
| topicId: transaction.name === "CONSENSUSCREATETOPIC" ? transaction.entity_id : null, | |
| scheduleId: transaction.name === "SCHEDULECREATE" ? transaction.entity_id : null, | |
| scheduledTransactionId: transaction.scheduled_transaction_id || null, | |
| serials: [], // Not included in basic response | |
| exchangeRate: null, // Not included in mirror node response | |
| topicSequenceNumber: null, | |
| topicRunningHash: null, | |
| totalSupply: 0, | |
| }, | |
| }; | |
| } catch (error) { | |
| console.error("Error fetching transaction receipt:", error.message); | |
| throw error; | |
| } | |
| } | |
| class CheckStatusWithHash { | |
| constructor() { | |
| this.loading = false; | |
| this.error = null; | |
| this.result = null; | |
| } | |
| async checkStatus(transactionHashOrId) { | |
| this.loading = true; | |
| this.error = null; | |
| this.result = null; | |
| try { | |
| const receipt = await getTransactionReceipt(transactionHashOrId); | |
| this.result = receipt; | |
| return receipt; | |
| } catch (err) { | |
| this.error = err.message || "Unknown error occurred"; | |
| throw err; | |
| } finally { | |
| this.loading = false; | |
| } | |
| } | |
| reset() { | |
| this.loading = false; | |
| this.error = null; | |
| this.result = null; | |
| } | |
| } | |
| module.exports = { | |
| getTransactionReceipt, | |
| CheckStatusWithHash, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment