@gorbchain-xyz/chaindecode
Version:
GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.
77 lines (76 loc) • 3.43 kB
JavaScript
// Generic fetcher for any Solana account (program, mint, etc)
// Usage: await fetchProgramAccount(address)
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getGorbchainConfig } from './gorbchainConfig.js';
import { decodeMintAccount } from './decodeMintAccount.js';
export function fetchProgramAccount(address) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
// Use the public Solana RPC (or allow override via config)
const res = yield fetch('https://rpc.gorbchain.xyz', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getAccountInfo',
params: [address, { encoding: 'base64' }]
})
});
const data = yield res.json();
if (!((_a = data === null || data === void 0 ? void 0 : data.result) === null || _a === void 0 ? void 0 : _a.value))
return null;
const info = data.result.value;
return {
pubkey: address,
owner: info.owner,
lamports: info.lamports,
executable: info.executable,
rentEpoch: info.rentEpoch,
data: info.data[0], // base64
raw: Uint8Array.from(atob(info.data[0]), c => c.charCodeAt(0))
};
});
}
export function fetchMintAccountFromRpc(mint) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const acct = yield fetchProgramAccount(mint);
if (!(acct === null || acct === void 0 ? void 0 : acct.data))
return null;
// Use all known token program IDs from config
const config = getGorbchainConfig();
const TOKEN_PROGRAMS = [
(_a = config.programIds) === null || _a === void 0 ? void 0 : _a.token2022,
(_b = config.programIds) === null || _b === void 0 ? void 0 : _b.token,
(_c = config.programIds) === null || _c === void 0 ? void 0 : _c.splToken,
(_d = config.programIds) === null || _d === void 0 ? void 0 : _d.mainnetToken,
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' // fallback SPL Token
].filter(Boolean);
if (acct.raw && acct.raw.length < 82)
return null;
if (!TOKEN_PROGRAMS.includes(acct.owner))
return null;
try {
// Try to decode as base64
return decodeMintAccount(acct.data, { encoding: 'base64' });
}
catch (_e) {
// fallback: try as hex
try {
return decodeMintAccount(acct.data, { encoding: 'hex' });
}
catch (_e2) {
return null;
}
}
});
}