UNPKG

banx-vaults-sdk

Version:

Fbonds SDK for interacting with protocol banx vaults

154 lines (153 loc) 6.82 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.enumToAnchorEnum = exports.nowInSeconds = exports.enumToAnchorEnumNew = exports.createDecodeEnum = exports.anchorRawBNsAndPubkeysToNumsAndStringsWithType = exports.anchorRawBNsAndPubkeysToNumsAndStrings = exports.returnAnchorProgram = exports.ParseType = void 0; const common_1 = require("../common"); const IDL = __importStar(require("./idl/banx_vaults.json")); const anchor_1 = require("@coral-xyz/anchor"); const anchor = __importStar(require("@coral-xyz/anchor")); const anchor_2 = require("@coral-xyz/anchor"); const bs58 = __importStar(require("bs58")); const lodash_1 = require("lodash"); var ParseType; (function (ParseType) { ParseType["Number"] = "number"; ParseType["String"] = "string"; })(ParseType = exports.ParseType || (exports.ParseType = {})); global.structuredClone = (val) => { return JSON.parse(JSON.stringify(val)); }; const returnAnchorProgram = (connection) => { const snakeCaseIdl = JSON.parse(JSON.stringify(IDL)); return new anchor_1.Program(snakeCaseIdl, new anchor_1.AnchorProvider(connection, (0, common_1.createFakeWallet)())); }; exports.returnAnchorProgram = returnAnchorProgram; const anchorRawBNsAndPubkeysToNumsAndStrings = (rawAccount) => { const copyRawAccount = Object.assign({}, rawAccount); const newAccount = parseRawAccount(rawAccount.account); return Object.assign(Object.assign({}, newAccount), { publicKey: new anchor.web3.PublicKey(copyRawAccount.publicKey) }); }; exports.anchorRawBNsAndPubkeysToNumsAndStrings = anchorRawBNsAndPubkeysToNumsAndStrings; const anchorRawBNsAndPubkeysToNumsAndStringsWithType = (rawAccount, parseType) => { const copyRawAccount = Object.assign({}, rawAccount); const newAccount = parseRawAccountWithType(rawAccount.account, parseType); return Object.assign(Object.assign({}, newAccount), { publicKey: copyRawAccount.publicKey.toString() }); }; exports.anchorRawBNsAndPubkeysToNumsAndStringsWithType = anchorRawBNsAndPubkeysToNumsAndStringsWithType; const parseRawAccount = (rawAccount) => { const copyRawAccount = Object.assign({}, rawAccount); for (const key in copyRawAccount) { if (copyRawAccount[key] === null || copyRawAccount[key] === undefined) continue; if (key === 'roundValue') { copyRawAccount[key] = copyRawAccount[key].toString(); continue; } if (key === 'creatorHash') { copyRawAccount[key] = bs58.encode(copyRawAccount[key]); continue; } if (copyRawAccount[key].toBase58) { copyRawAccount[key] = new anchor.web3.PublicKey(copyRawAccount[key].toBase58()); continue; } if (copyRawAccount[key].toNumber) { try { if (anchor_2.BN.isBN(copyRawAccount[key])) { copyRawAccount[key] = new anchor_2.BN(copyRawAccount[key]); continue; } else { copyRawAccount[key] = copyRawAccount[key].toNumber(); } } catch (err) { copyRawAccount[key] = 0; } } if (typeof copyRawAccount[key] === 'object' && Object.keys(copyRawAccount[key]).length === 1) { copyRawAccount[key] = Object.keys(copyRawAccount[key])[0]; } else if (typeof copyRawAccount[key] === 'object') { copyRawAccount[key] = parseRawAccount(copyRawAccount[key]); } } return copyRawAccount; }; const parseRawAccountWithType = (rawAccount, parseType) => { const copyRawAccount = Object.assign({}, rawAccount); for (const key in copyRawAccount) { if (copyRawAccount[key] === null || copyRawAccount[key] === undefined) continue; if (key === 'roundValue') { copyRawAccount[key] = copyRawAccount[key].toString(); continue; } if (key === 'creatorHash') { copyRawAccount[key] = bs58.encode(copyRawAccount[key]); continue; } if (parseType == ParseType.String && key != 'adventureSubscriptionState' && key != 'adventureState' && key != 'banxStakingSettingsState' && key != 'banxStakeState') { copyRawAccount[key] = copyRawAccount[key].toString(); } if (parseType == ParseType.Number) { if (copyRawAccount[key].toNumber) { try { copyRawAccount[key] = copyRawAccount[key].toNumber(); } catch (err) { copyRawAccount[key] = 0; } } } if (copyRawAccount[key].toBase58) { copyRawAccount[key] = copyRawAccount[key].toBase58(); } if (typeof copyRawAccount[key] === 'object' && Object.keys(copyRawAccount[key]).length === 1) { copyRawAccount[key] = Object.keys(copyRawAccount[key])[0]; } else if (typeof copyRawAccount[key] === 'object') { copyRawAccount[key] = parseRawAccount(copyRawAccount[key]); } } return copyRawAccount; }; // Универсальная функция для создания DecodeEnum const createDecodeEnum = (enumValue, decodedEnumType) => { return { [enumValue]: undefined }; }; exports.createDecodeEnum = createDecodeEnum; const enumToAnchorEnumNew = (enumValue, enumObject) => { return { [enumValue]: {} }; }; exports.enumToAnchorEnumNew = enumToAnchorEnumNew; const nowInSeconds = () => Math.floor((0, lodash_1.now)() / 1000); exports.nowInSeconds = nowInSeconds; const enumToAnchorEnum = (anyEnum) => ({ [anyEnum.toString()]: {} }); exports.enumToAnchorEnum = enumToAnchorEnum;