UNPKG

@chorus-one/ton

Version:

All-in-one tooling for building staking dApps on TON

43 lines (42 loc) 1.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TonClient = void 0; const ton_1 = require("@ton/ton"); const axios_1 = __importDefault(require("axios")); const zod_1 = require("zod"); const configParamCodec = zod_1.z.object({ ok: zod_1.z.boolean(), result: zod_1.z.object({ '@type': zod_1.z.string(), config: zod_1.z.object({ '@type': zod_1.z.string(), bytes: zod_1.z.string() }), '@extra': zod_1.z.string() }) }); class TonClient extends ton_1.TonClient { async getConfigParam(config_id) { const url = new URL(this.parameters.endpoint); const base = url.pathname.split('/').slice(0, -1).join('/'); url.pathname = base + '/getConfigParam'; url.searchParams.set('config_id', config_id.toString()); const r = await axios_1.default.get(url.toString()); if (r.status !== 200) { throw Error('Unable to fetch config param, error: ' + r.status + ' ' + r.statusText); } const configParam = configParamCodec.safeParse(r.data); if (!configParam.success) { throw Error('Unable to parse config param, error: ' + JSON.stringify(configParam.error)); } const paramBytes = configParam.data?.result.config.bytes; if (paramBytes === undefined) { throw Error('Failed to get config param bytes'); } return ton_1.Cell.fromBoc(Buffer.from(paramBytes, 'base64'))[0]; } } exports.TonClient = TonClient;