UNPKG

@massalabs/massa-web3

Version:
235 lines 9.21 kB
"use strict"; /* eslint-disable @typescript-eslint/naming-convention */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PublicAPI = void 0; const tslib_1 = require("tslib"); const basicElements_1 = require("../basicElements"); const _1 = require("."); const smartContracts_1 = require("../smartContracts"); const operation_1 = require("../operation"); const lodash_isequal_1 = tslib_1.__importDefault(require("lodash.isequal")); const connector_1 = require("./connector"); class PublicAPI { url; options; connector; lastStatus; // eslint-disable-next-line max-params constructor(url, options = {}) { this.url = url; this.options = options; this.connector = new connector_1.Connector(url, this.options); } async executeReadOnlyBytecode(readOnlyBytecodeExecution) { return this.connector .execute_read_only_bytecode([readOnlyBytecodeExecution]) .then((r) => r[0]); } async executeMultipleReadOnlyBytecode(readOnlyBytecodeExecutions) { return this.connector.execute_read_only_bytecode(readOnlyBytecodeExecutions); } async executeReadOnlyCall(params) { const [res] = await this.connector.execute_read_only_call([ { max_gas: Number(params.maxGas ?? smartContracts_1.MAX_GAS_CALL), target_address: params.target, target_function: params.func, parameter: Array.from(params.parameter), caller_address: params.caller, coins: params.coins ? basicElements_1.Mas.toString(params.coins) : null, fee: params.fee ? basicElements_1.Mas.toString(params.fee) : null, }, ]); if (!res) { throw new Error('No result returned from execute_read_only_call'); } return (0, _1.formatReadOnlyCallResponse)(res); } async executeMultipleReadOnlyCall(readOnlyCalls) { const params = readOnlyCalls.map((call) => ({ max_gas: Number(call.maxGas ?? smartContracts_1.MAX_GAS_CALL), target_address: call.target, target_function: call.func, parameter: Array.from(call.parameter), caller_address: call.caller, coins: call.coins ? basicElements_1.Mas.toString(call.coins) : null, fee: call.fee ? basicElements_1.Mas.toString(call.fee) : null, })); const res = await this.connector.execute_read_only_call(params); return res.map((r) => (0, _1.formatReadOnlyCallResponse)(r)); } async getAddressInfo(address) { return this.getMultipleAddressInfo([address]).then((r) => r[0]); } async getBalance(address, final = true) { return this.getAddressInfo(address).then((r) => { return basicElements_1.Mas.fromString(final ? r.final_balance : r.candidate_balance); }); } async getMultipleAddressInfo(addresses) { return this.connector.get_addresses(addresses); } async getAddressesBytecode(addressFilter) { return this.connector .get_addresses_bytecode([addressFilter]) .then((r) => Uint8Array.from(r[0])); } async executeMultipleGetAddressesBytecode(addressFilters) { const bytecodes = await this.connector.get_addresses_bytecode(addressFilters); return bytecodes.map((bytecode) => Uint8Array.from(bytecode)); } async getBlock(blockId) { return this.connector.get_blocks([blockId]).then((r) => r[0]); } async getMultipleBlocks(blockIds) { return this.connector.get_blocks(blockIds); } async getBlockcliqueBlock(slot) { return this.connector.get_blockclique_block_by_slot(slot); } async getCliques() { return this.connector.get_cliques(); } async getDataStoreKeys(contract, filter = new Uint8Array(), final = true) { const addrInfo = await this.getAddressInfo(contract); const keys = final ? addrInfo.final_datastore_keys : addrInfo.candidate_datastore_keys; return keys .filter((key) => !filter.length || (0, lodash_isequal_1.default)(Uint8Array.from(key.slice(0, filter.length)), filter)) .map((key) => Uint8Array.from(key)); } async getDatastoreEntries(inputs, final = true) { const entriesQuery = inputs.map((entry) => { const byteKey = typeof entry.key === 'string' ? (0, basicElements_1.strToBytes)(entry.key) : entry.key; return { key: Array.from(byteKey), address: entry.address, }; }); const res = await this.connector.get_datastore_entries(entriesQuery); return res.map((r) => { const val = final ? r.final_value : r.candidate_value; return val ? Uint8Array.from(val) : null; }); } async getDatastoreEntry(key, address, final = true) { return this.getDatastoreEntries([{ key, address }], final).then((r) => r[0]); } async getSlotTransfers(slot) { return this.connector.get_slots_transfers([slot]).then((r) => r[0]); } async getMultipleSlotTransfers(slots) { return this.connector.get_slots_transfers(slots); } async getEndorsement(endorsementId) { return this.getMultipleEndorsements([endorsementId]).then((r) => r[0]); } async getMultipleEndorsements(endorsementIds) { return this.connector.get_endorsements(endorsementIds); } async getEvents(filter) { const formattedFilter = { start: filter.start, end: filter.end, emitter_address: filter.smartContractAddress, original_caller_address: filter.callerAddress, original_operation_id: filter.operationId, is_final: filter.isFinal, is_error: filter.isError, }; return this.connector.get_filtered_sc_output_event(formattedFilter); } async getGraphInterval(start, end) { return this.connector.get_graph_interval({ start, end }); } async getOperations(operationIds) { return this.connector.get_operations(operationIds); } async getOperation(operationId) { return this.getOperations([operationId]).then((r) => r[0]); } async getOperationStatus(operationId) { const op = await this.getOperation(operationId); if (!op) { return operation_1.OperationStatus.NotFound; } if (op.op_exec_status === null) { if (op.is_operation_final === null) { return operation_1.OperationStatus.NotFound; } throw new Error('unexpected status'); } if (op.in_pool) { return operation_1.OperationStatus.PendingInclusion; } if (!op.is_operation_final) { return op.op_exec_status ? operation_1.OperationStatus.SpeculativeSuccess : operation_1.OperationStatus.SpeculativeError; } return op.op_exec_status ? operation_1.OperationStatus.Success : operation_1.OperationStatus.Error; } // todo rename PageRequest pagination async getStakers(pagination) { return this.connector.get_stakers(pagination); } async status() { this.lastStatus = await this.connector.get_status(); return this.lastStatus; } async getMinimalFee() { if (!this.lastStatus) { await this.status(); } if (!this.lastStatus.minimal_fees) { throw new Error('minimal fees: not available'); } return basicElements_1.Mas.fromString(this.lastStatus.minimal_fees); } async getChainId() { if (!this.lastStatus) { await this.status(); } return BigInt(this.lastStatus.chain_id); } async fetchPeriod() { const status = await this.status(); if (!status.last_slot) { throw new Error('last slot: not available'); } return status.last_slot.period; } async getCurrentSlot() { const { last_slot } = await this.status(); return last_slot; } static convertOperationInput(data) { return { serialized_content: Array.from(data.data), creator_public_key: data.publicKey, signature: data.signature, }; } async sendOperation(data) { return this.sendOperations([data]).then((r) => r[0]); } async sendOperations(data) { return this.sendMultipleOperations(data.map((e) => PublicAPI.convertOperationInput(e))); } async sendMultipleOperations(data) { return this.connector.send_operations(data); } async deferredCallQuote(quoteRequests) { return this.connector.get_deferred_call_quote(quoteRequests); } async deferredCallsInfo(deferredCallsIds) { return this.connector.get_deferred_call_info(deferredCallsIds); } async deferredCallsBySlot(slots) { return this.connector.get_deferred_call_ids_by_slot(slots); } } exports.PublicAPI = PublicAPI; //# sourceMappingURL=publicAPI.js.map