UNPKG

@coral-xyz/anchor

Version:
112 lines • 4.37 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isCompositeAccounts = isCompositeAccounts; exports.idlAddress = idlAddress; exports.seed = seed; exports.decodeIdlAccount = decodeIdlAccount; exports.encodeIdlAccount = encodeIdlAccount; exports.convertIdlToCamelCase = convertIdlToCamelCase; exports.handleDefinedFields = handleDefinedFields; const camelcase_1 = __importDefault(require("camelcase")); const buffer_1 = require("buffer"); const web3_js_1 = require("@solana/web3.js"); const borsh = __importStar(require("@coral-xyz/borsh")); function isCompositeAccounts(accountItem) { return "accounts" in accountItem; } // Deterministic IDL address as a function of the program id. async function idlAddress(programId) { const base = (await web3_js_1.PublicKey.findProgramAddress([], programId))[0]; return await web3_js_1.PublicKey.createWithSeed(base, seed(), programId); } // Seed for generating the idlAddress. function seed() { return "anchor:idl"; } const IDL_ACCOUNT_LAYOUT = borsh.struct([ borsh.publicKey("authority"), borsh.vecU8("data"), ]); function decodeIdlAccount(data) { return IDL_ACCOUNT_LAYOUT.decode(data); } function encodeIdlAccount(acc) { const buffer = buffer_1.Buffer.alloc(1000); // TODO: use a tighter buffer. const len = IDL_ACCOUNT_LAYOUT.encode(acc, buffer); return buffer.slice(0, len); } /** * Convert the given IDL to camelCase. * * The IDL is generated from Rust which has different conventions compared to * JS/TS, e.g. instruction names in Rust are snake_case. * * The conversion happens automatically for programs, however, if you are using * internals such as `BorshInstructionCoder` and you only have the original * (not camelCase) IDL, you might need to use this function. * * @param idl IDL to convert to camelCase * @returns camelCase version of the IDL */ function convertIdlToCamelCase(idl) { const KEYS_TO_CONVERT = ["name", "path", "account", "relations", "generic"]; // `my_account.field` is getting converted to `myAccountField` but we // need `myAccount.field`. const toCamelCase = (s) => s .split(".") .map((part) => (0, camelcase_1.default)(part, { locale: false })) .join("."); const recursivelyConvertNamesToCamelCase = (obj) => { for (const key in obj) { const val = obj[key]; if (KEYS_TO_CONVERT.includes(key)) { obj[key] = Array.isArray(val) ? val.map(toCamelCase) : toCamelCase(val); } else if (typeof val === "object") { recursivelyConvertNamesToCamelCase(val); } } }; const camelCasedIdl = structuredClone(idl); recursivelyConvertNamesToCamelCase(camelCasedIdl); return camelCasedIdl; } /** Conveniently handle all defined field kinds with proper type support. */ function handleDefinedFields(fields, unitCb, namedCb, tupleCb) { // Unit if (!(fields === null || fields === void 0 ? void 0 : fields.length)) return unitCb(); // Named if (fields[0].name) { return namedCb(fields); } // Tuple return tupleCb(fields); } //# sourceMappingURL=idl.js.map