@solana-developers/helpers
Version:
Solana helper functions
46 lines • 2.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getErrorFromRPCResponse = exports.getLogs = void 0;
const transaction_1 = require("./transaction");
const getLogs = async (connection, tx) => {
await (0, transaction_1.confirmTransaction)(connection, tx);
const txDetails = await connection.getTransaction(tx, {
maxSupportedTransactionVersion: 0,
commitment: "confirmed",
});
return txDetails?.meta?.logMessages || [];
};
exports.getLogs = getLogs;
const getErrorFromRPCResponse = (rpcResponse) => {
// Note: `confirmTransaction` does not throw an error if the confirmation does not succeed,
// but rather a `TransactionError` object. so we handle that here
// See https://solana-labs.github.io/solana-web3.js/v1.x/classes/Connection.html#confirmTransaction.confirmTransaction-1
const error = rpcResponse.value.err;
if (error) {
// Can be a string or an object (literally just {}, no further typing is provided by the library)
// https://github.com/solana-labs/solana-web3.js/blob/4436ba5189548fc3444a9f6efb51098272926945/packages/library-legacy/src/connection.ts#L2930
// TODO: if still occurs in web3.js 2 (unlikely), fix it.
if (typeof error === "object") {
const errorKeys = Object.keys(error);
if (errorKeys.length === 1) {
if (errorKeys[0] !== "InstructionError") {
throw new Error(`Unknown RPC error: ${error}`);
}
// @ts-ignore due to missing typing information mentioned above.
const instructionError = error["InstructionError"];
// An instruction error is a custom program error and looks like:
// [
// 1,
// {
// "Custom": 1
// }
// ]
// See also https://solana.stackexchange.com/a/931/294
throw new Error(`Error in transaction: instruction index ${instructionError[0]}, custom program error ${instructionError[1]["Custom"]}`);
}
}
throw Error(error.toString());
}
};
exports.getErrorFromRPCResponse = getErrorFromRPCResponse;
//# sourceMappingURL=logs.js.map
;