Created
December 25, 2023 22:55
-
-
Save alfonso-paella/1b62b7b5e4a09606d80a8fbea9f68e8e to your computer and use it in GitHub Desktop.
[Solana] Get compute units spent for tx
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 solanaWeb3 = require("@solana/web3.js"); | |
| async function getComputeUnits(txId, clusterName) { | |
| // Use a public RPC endpoint | |
| const connection = new solanaWeb3.Connection( | |
| solanaWeb3.clusterApiUrl(clusterName), | |
| "confirmed", | |
| ); | |
| try { | |
| // Fetch transaction details | |
| const tx = await connection.getTransaction(txId); | |
| if (tx && tx.meta) { | |
| console.log(`Compute Units Consumed: ${tx.meta.computeUnitsConsumed}`); | |
| return tx.meta.computeUnitsConsumed; | |
| } else { | |
| console.log("Transaction not found or does not have metadata."); | |
| } | |
| } catch (error) { | |
| console.error("Error fetching transaction:", error); | |
| } | |
| return -1; | |
| } | |
| // Example | |
| getComputeUnits("5iqyDvPgio8pzQygC7v7Ho8L1tD8AfoBaKfBwqxENox4rEV1Mq8RPSFRAqBN6upG1UuhbQTDKzvJ4r88hkak2gQR", "mainnet-beta"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment