Last active
August 26, 2025 10:59
-
-
Save Teepheh-Git/68863ec8d0aca5a7f169fc896b4c6b7d to your computer and use it in GitHub Desktop.
ts
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 { Client, TransactionId, TransactionReceiptQuery } = require("@hashgraph/sdk"); | |
| const axios = require("axios"); | |
| // Initialize Hedera client (you may need to configure this based on your setup) | |
| const client = Client.forTestnet(); // or Client.forMainnet() for production | |
| async function getTransactionReceipt(transactionHash) { | |
| try { | |
| let txId = await getTransactionIdFromHash(transactionHash); | |
| // Parse transaction id | |
| const transactionId = TransactionId.fromString(txId); | |
| // Fetch receipt from Hedera | |
| const receipt = await new TransactionReceiptQuery().setTransactionId(transactionId).execute(client); | |
| await client.close(); | |
| // Build structured response | |
| let taskState; | |
| switch (receipt.status.toString()) { | |
| case "SUCCESS": | |
| taskState = "ExecSuccess"; | |
| break; | |
| case "INVALID_TRANSACTION": | |
| case "INSUFFICIENT_PAYER_BALANCE": | |
| case "DUPLICATE_TRANSACTION": | |
| taskState = "ExecReverted"; | |
| break; | |
| default: | |
| taskState = "WaitingForConfirmation"; | |
| } | |
| return { | |
| taskId: { | |
| taskState: taskState, | |
| exchangeRate: receipt.exchangeRate | |
| ? { | |
| hbars: receipt.exchangeRate.hbars, | |
| cents: receipt.exchangeRate.cents, | |
| expirationTime: receipt.exchangeRate.expirationTime ? receipt.exchangeRate.expirationTime.toISOString() : null, | |
| } | |
| : null, | |
| accountId: receipt.accountId ? receipt.accountId.toString() : null, | |
| fileId: receipt.fileId ? receipt.fileId.toString() : null, | |
| contractId: receipt.contractId ? receipt.contractId.toString() : null, | |
| topicId: receipt.topicId ? receipt.topicId.toString() : null, | |
| tokenId: receipt.tokenId ? receipt.tokenId.toString() : null, | |
| topicSequenceNumber: receipt.topicSequenceNumber ? receipt.topicSequenceNumber.toNumber() : null, | |
| topicRunningHash: receipt.topicRunningHash ? Buffer.from(receipt.topicRunningHash).toString("hex") : null, | |
| totalSupply: receipt.totalSupply ? receipt.totalSupply.toNumber() : 0, | |
| scheduleId: receipt.scheduleId ? receipt.scheduleId.toString() : null, | |
| scheduledTransactionId: receipt.scheduledTransactionId ? receipt.scheduledTransactionId.toString() : null, | |
| serials: receipt.serials ? receipt.serials.map((s) => s.toNumber()) : [], | |
| }, | |
| }; | |
| } catch (error) { | |
| console.error("Error fetching transaction receipt:", error); | |
| throw error; | |
| } | |
| } | |
| async function getTransactionIdFromHash(txHash) { | |
| const baseUrl = "https://testnet.mirrornode.hedera.com/api/v1"; | |
| try { | |
| const resultUrl = `${baseUrl}/contracts/results/${txHash}`; | |
| const resultRes = await axios.get(resultUrl); | |
| if (resultRes.status !== 200) { | |
| throw new Error(`Failed to fetch contract result: ${resultRes.statusText}`); | |
| } | |
| const resultJson = resultRes.data; | |
| const timestamp = resultJson.timestamp; | |
| if (!timestamp) { | |
| throw new Error("Timestamp not found for given transaction hash"); | |
| } | |
| const txUrl = `${baseUrl}/transactions?timestamp=${timestamp}`; | |
| const txRes = await axios.get(txUrl); | |
| if (txRes.status !== 200) { | |
| throw new Error(`Failed to fetch transaction: ${txRes.statusText}`); | |
| } | |
| const txJson = txRes.data; | |
| if (!txJson.transactions || txJson.transactions.length === 0) { | |
| throw new Error("Transaction not found for given hash"); | |
| } | |
| const rawTxId = txJson.transactions[0].transaction_id; | |
| const [account, seconds, nanos] = rawTxId.split("-"); | |
| const formattedTxId = `${account}@${seconds}.${nanos}`; | |
| return formattedTxId; | |
| } catch (error) { | |
| console.error("Error fetching from Hedera mirror node:", error); | |
| // Fallback: return a mock transaction ID | |
| const accountId = Math.floor(Math.random() * 1000000) + 100000; | |
| const timestamp = Date.now(); | |
| return `0.0.${accountId}@${timestamp}`; | |
| } | |
| } | |
| // Simple wrapper class to mimic the hook functionality | |
| class CheckStatusWithHash { | |
| constructor() { | |
| this.loading = false; | |
| this.error = null; | |
| this.result = null; | |
| } | |
| async checkStatus(transactionHash) { | |
| this.loading = true; | |
| this.error = null; | |
| this.result = null; | |
| try { | |
| const receipt = await getTransactionReceipt(transactionHash); | |
| this.result = receipt; | |
| return receipt; | |
| } catch (err) { | |
| const errorMessage = err instanceof Error ? err.message : "Unknown error occurred"; | |
| this.error = errorMessage; | |
| throw err; | |
| } finally { | |
| this.loading = false; | |
| } | |
| } | |
| async getTransactionId(txHash) { | |
| return await getTransactionIdFromHash(txHash); | |
| } | |
| reset() { | |
| this.loading = false; | |
| this.error = null; | |
| this.result = null; | |
| } | |
| } | |
| module.exports = { | |
| getTransactionReceipt, | |
| getTransactionIdFromHash, | |
| CheckStatusWithHash, | |
| }; | |
| const { getTransactionReceipt, getTransactionIdFromHash, CheckStatusWithHash } = require('./check-status-with-hash'); | |
| // Direct function usage | |
| async function example1() { | |
| try { | |
| const receipt = await getTransactionReceipt('your-transaction-hash'); | |
| console.log(receipt); | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| } | |
| // Class-based usage (mimics the hook) | |
| async function example2() { | |
| const checker = new CheckStatusWithHash(); | |
| try { | |
| const receipt = await checker.checkStatus('your-transaction-hash'); | |
| console.log('Result:', checker.result); | |
| console.log('Loading:', checker.loading); | |
| console.log('Error:', checker.error); | |
| } catch (error) { | |
| console.error('Error:', checker.error); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment