@solana-developers/helpers
Version:
Solana helper functions
50 lines • 2.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExplorerLink = exports.getCustomErrorMessage = void 0;
const getCustomErrorMessage = (possibleProgramErrors, errorMessage) => {
const customErrorExpression = /.*custom program error: 0x(?<errorNumber>[0-9abcdef]+)/;
let match = customErrorExpression.exec(errorMessage);
const errorNumberFound = match?.groups?.errorNumber;
if (!errorNumberFound) {
return null;
}
// errorNumberFound is a base16 string
const errorNumber = parseInt(errorNumberFound, 16);
return possibleProgramErrors[errorNumber] || null;
};
exports.getCustomErrorMessage = getCustomErrorMessage;
const encodeURL = (baseUrl, searchParams) => {
// This was a little new to me, but it's the
// recommended way to build URLs with query params
// (and also means you don't have to do any encoding)
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
const url = new URL(baseUrl);
url.search = new URLSearchParams(searchParams).toString();
return url.toString();
};
const getExplorerLink = (linkType, id, cluster = "mainnet-beta") => {
const searchParams = {};
if (cluster !== "mainnet-beta") {
if (cluster === "localnet") {
// localnet technically isn't a cluster, so requires special handling
searchParams["cluster"] = "custom";
searchParams["customUrl"] = "http://localhost:8899";
}
else {
searchParams["cluster"] = cluster;
}
}
let baseUrl = "";
if (linkType === "address") {
baseUrl = `https://explorer.solana.com/address/${id}`;
}
if (linkType === "transaction" || linkType === "tx") {
baseUrl = `https://explorer.solana.com/tx/${id}`;
}
if (linkType === "block") {
baseUrl = `https://explorer.solana.com/block/${id}`;
}
return encodeURL(baseUrl, searchParams);
};
exports.getExplorerLink = getExplorerLink;
//# sourceMappingURL=explorer.js.map
;