UNPKG

@kamino-finance/klend-sdk

Version:

Typescript SDK for interacting with the Kamino Lending (klend) protocol

91 lines 3.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProgramAccounts = getProgramAccounts; exports.getAccountOwner = getAccountOwner; const web3_js_1 = require("@solana/web3.js"); const buffer_1 = require("buffer"); const axios_1 = __importDefault(require("axios")); const zstd_wasm_1 = require("@bokuweb/zstd-wasm"); const uuid_1 = require("uuid"); (async () => { await (0, zstd_wasm_1.init)(); })(); /** * Uses zstd compression when fetching all accounts owned by a program for a smaller response size * Uses axios instead of node-fetch to work around a bug in node-fetch that causes subsequent requests with different encoding to fail * @param connection * @param programId * @param configOrCommitment */ async function getProgramAccounts(connection, programId, configOrCommitment) { const programIdStr = programId.toBase58(); const { commitment, config } = extractCommitmentFromConfig(configOrCommitment); const { encoding: _encoding, ...configWithoutEncoding } = config || {}; // Use axios here to work around a bug in node fetch that causes subsequent requests with different encoding to fail // https://github.com/node-fetch/node-fetch/issues/1767 const response = await axios_1.default.post(connection.rpcEndpoint, { method: 'getProgramAccounts', jsonrpc: '2.0', params: [ programIdStr, { encoding: 'base64+zstd', commitment, ...configWithoutEncoding, }, ], id: (0, uuid_1.v4)(), }, { headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'no-cache', }, }); const unsafeRes = response.data; if ('error' in unsafeRes) { throw new web3_js_1.SolanaJSONRPCError(unsafeRes.error, `Failed to get accounts owned by program: ${[programIdStr]}`); } const res = unsafeRes; const deser = res.result.map(async (account) => ({ account: await deserializeAccountInfo(account.account), pubkey: new web3_js_1.PublicKey(account.pubkey), })); const x = await Promise.all(deser); return x; } async function getAccountOwner(connection, address) { const acc = await connection.getAccountInfo(address); if (acc == null) { throw Error(`Could not fetch mint ${address.toString()}`); } return acc.owner; } async function deserializeAccountInfo(accountInfo) { const data = (0, zstd_wasm_1.decompress)(buffer_1.Buffer.from(accountInfo.data[0], 'base64')); return { owner: accountInfo.owner, lamports: accountInfo.lamports, executable: accountInfo.executable, rentEpoch: accountInfo.rentEpoch, data: buffer_1.Buffer.from(data), }; } function extractCommitmentFromConfig(commitmentOrConfig) { let commitment; let config; if (typeof commitmentOrConfig === 'string') { commitment = commitmentOrConfig; } else if (commitmentOrConfig) { const { commitment: specifiedCommitment, ...specifiedConfig } = commitmentOrConfig; commitment = specifiedCommitment; config = specifiedConfig; } return { commitment, config }; } //# sourceMappingURL=rpc.js.map