UNPKG

@gear-js/api

Version:

A JavaScript library that provides functionality to connect GEAR Component APIs.

202 lines (198 loc) 6.62 kB
'use strict'; var api = require('@polkadot/api'); var Balance = require('./api/Balance.js'); var Blocks = require('./api/Blocks.js'); var Builtin = require('./api/Builtin.js'); var Code = require('./api/Code.js'); var index = require('./api/eth-bridge/index.js'); require('./consts.js'); require('@polkadot/util'); require('@polkadot/types'); var rpc = require('./default/rpc.js'); var types = require('./default/types.js'); require('node:assert'); require('./metadata/programMetadata.js'); require('./utils/generate.js'); require('./utils/reply-code.js'); require('@polkadot/util-crypto'); var Mailbox = require('./api/Mailbox.js'); var Message = require('./api/Message.js'); var Program = require('./api/Program.js'); var State = require('./api/State.js'); var Storage = require('./api/Storage.js'); var Voucher = require('./api/Voucher.js'); var Waitlist = require('./api/Waitlist.js'); var Events = require('./events/Events.js'); class GearApi extends api.ApiPromise { program; /** @deprecated */ programState; programStorage; message; balance; gearEvents; blocks; mailbox; code; waitlist; voucher; ethBridge; builtin; provider; _rpcMethods; constructor(options = {}) { const { types: types$1 = {}, derives = {}, providerAddress, noInitWarn, ...restOptions } = options; const provider = restOptions?.provider || new api.WsProvider(providerAddress ?? 'ws://127.0.0.1:9944'); super({ provider, derives, types: { ...types$1, ...types.GEAR_TYPES, }, rpc: { gear: rpc.GEAR_RPC_METHODS, gearBuiltin: rpc.GEAR_BUILTIN_RPC_METHODS, gearEthBridge: rpc.GEAR_ETH_BRIDGE_RPC_METHODS, stakingRewards: rpc.STAKING_REWARDS_METHODS, runtime: rpc.RUNTIME_METHODS, }, runtime: { GearApi: [ { methods: {}, version: 2, }, ], Vara: [ { methods: {}, version: 1, }, { methods: {}, version: 1030, }, ], GearBuiltinApi: [{ methods: {}, version: 1 }], }, signedExtensions: { StakingBlackList: { extrinsic: {}, payload: {} }, }, noInitWarn: noInitWarn ?? true, ...restOptions, }); this.provider = provider; this.program = new Program.GearProgram(this); this.voucher = new Voucher.GearVoucher(this); this.message = new Message.GearMessage(this); this.balance = new Balance.GearBalance(this); this.gearEvents = new Events.GearEvents(this); this.programState = new State.GearProgramState(this); this.blocks = new Blocks.GearBlock(this); this.programStorage = new Storage.GearProgramStorage(this); this.mailbox = new Mailbox.GearMailbox(this); this.code = new Code.GearCode(this); this.waitlist = new Waitlist.GearWaitlist(this); this.ethBridge = new index.GearEthBridge(this); this.builtin = new Builtin.GearBuiltin(this); this.isReady .then(() => this.rpc.rpc.methods()) .then((rpc) => { this._rpcMethods = rpc.methods.toArray().map((item) => item.toString()); }); } static async create(options) { const api = new GearApi(options); await api.isReadyOrError; return api; } async totalIssuance() { return (await this.query.balances.totalIssuance()).toHuman(); } async chain() { return (await this.rpc.system.chain()).toHuman(); } async nodeName() { return (await this.rpc.system.name()).toHuman(); } async nodeVersion() { return (await this.rpc.system.version()).toHuman(); } get specVersion() { return this.runtimeVersion.specVersion.toNumber(); } get specName() { return this.runtimeVersion.specName.toString(); } get existentialDeposit() { return this.consts.balances.existentialDeposit; } get blockGasLimit() { return this.consts.gearGas.blockGasLimit; } get mailboxTreshold() { return this.consts.gear.mailboxThreshold; } get waitlistCost() { return this.consts.gearScheduler.waitlistCost; } get valuePerGas() { const gasMultiplier = this.consts.gearBank.gasMultiplier; if (gasMultiplier.isValuePerGas) { return gasMultiplier.asValuePerGas; } throw new Error('gasMultiplier.isValuePerGas is not set'); } async inflationInfo() { const info = await this.rpc.stakingRewards.inflationInfo(); return this.createType('InflationInfo', info); } async wasmBlobVersion() { // biome-ignore lint/complexity/useLiteralKeys: `rpc` parameter is generated dynamically based on the metadata, so we need to use string index to access it const result = await this.rpc['runtime'].wasmBlobVersion(); return result.toString(); } /** * Method provides opportunity to get informations about error occurs in ExtrinsicFailed event * @param event * @returns */ getExtrinsicFailedError(event) { const error = event.data[0]; if (error.isModule) { const data = this.registry.findMetaError(error.asModule); return { docs: data?.docs.join(' '), method: data?.method, name: data?.name, }; } if (error.isCannotLookup) { return { docs: null, method: 'CannotLookup', name: 'CannotLookup', }; } if (error.isBadOrigin) { return { docs: null, method: 'BadOrigin', name: 'BadOrigin', }; } return { docs: null, method: 'Unknown error', name: 'Unknown error', }; } get rpcMethods() { if (!this._rpcMethods) { throw new Error('RPC methods not available. Ensure API is ready by awaiting api.isReady before accessing rpcMethods.'); } return this._rpcMethods; } } exports.GearApi = GearApi;