UNPKG

@ar.io/sdk

Version:

[![codecov](https://codecov.io/gh/ar-io/ar-io-sdk/graph/badge.svg?token=7dXKcT7dJy)](https://codecov.io/gh/ar-io/ar-io-sdk)

94 lines (93 loc) 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ANTVersionsWritable = exports.ANTVersionsReadable = exports.ANTVersions = void 0; /** * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const constants_js_1 = require("../constants.js"); const io_js_1 = require("../types/io.js"); const ao_js_1 = require("../utils/ao.js"); const arweave_js_1 = require("../utils/arweave.js"); const ao_process_js_1 = require("./contracts/ao-process.js"); const error_js_1 = require("./error.js"); class ANTVersions { static init(config) { if (config !== undefined && 'signer' in config) { return new ANTVersionsWritable(config); } return new ANTVersionsReadable(config); } } exports.ANTVersions = ANTVersions; class ANTVersionsReadable { process; constructor(config) { if (config === undefined || Object.keys(config).length === 0) { this.process = new ao_process_js_1.AOProcess({ processId: constants_js_1.ANT_REGISTRY_ID, }); } else if ((0, io_js_1.isProcessConfiguration)(config)) { this.process = config.process; } else if ((0, io_js_1.isProcessIdConfiguration)(config)) { this.process = new ao_process_js_1.AOProcess({ processId: config.processId, }); } else { throw new error_js_1.InvalidContractConfigurationError(); } } async getANTVersions() { const res = await this.process.read({ tags: [{ name: 'Action', value: 'Get-Versions' }], }); return Object.fromEntries(Object.entries(res).sort(([a], [b]) => a.localeCompare(b))); } async getLatestANTVersion() { const versions = await this.getANTVersions(); const lastestVersion = Object.entries(versions).at(-1); if (!lastestVersion) throw new Error('No version found'); return { version: lastestVersion[0], ...lastestVersion[1], }; } } exports.ANTVersionsReadable = ANTVersionsReadable; class ANTVersionsWritable extends ANTVersionsReadable { signer; constructor({ signer, ...config }) { super(config); this.signer = (0, ao_js_1.createAoSigner)(signer); } async addVersion(params, options) { const { version, moduleId, luaSourceId, notes } = params; return this.process.send({ tags: (0, arweave_js_1.pruneTags)([ { name: 'Action', value: 'Add-Version' }, { name: 'Version', value: version }, { name: 'Module-Id', value: moduleId }, { name: 'Lua-Source-Id', value: luaSourceId }, { name: 'Notes', value: notes }, ...(options?.tags ?? []), ]), signer: this.signer, }); } } exports.ANTVersionsWritable = ANTVersionsWritable;