UNPKG

@gear-js/api

Version:

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

187 lines (183 loc) 5.86 kB
'use strict'; var api = require('@polkadot/api'); var index = require('./default/index.js'); require('@polkadot/types'); var Events = require('./events/Events.js'); var Balance = require('./api/Balance.js'); var Blocks = require('./api/Blocks.js'); var Builtin = require('./api/Builtin.js'); var Claim = require('./api/Claim.js'); var Code = require('./api/Code.js'); var EthBridge = require('./api/EthBridge.js'); require('./consts.js'); require('@polkadot/util'); require('./utils/generate.js'); require('@polkadot/util-crypto'); require('assert'); require('./metadata/programMetadata.js'); 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'); class GearApi extends api.ApiPromise { program; /** @deprecated */ programState; programStorage; message; balance; gearEvents; defaultTypes; blocks; mailbox; claimValueFromMailbox; code; waitlist; voucher; ethBridge; builtin; provider; constructor(options = {}) { const { types, providerAddress, ...restOptions } = options; const provider = restOptions?.provider || new api.WsProvider(providerAddress ?? 'ws://127.0.0.1:9944'); const defaultTypes = types ? { ...types, ...index.gearTypes } : index.gearTypes; super({ provider, derives: {}, types: { ...defaultTypes, }, rpc: { ...index.gearRpc, }, runtime: { GearApi: [ { methods: {}, version: 2, }, ], Vara: [ { methods: {}, version: 1, }, { methods: {}, version: 1030, }, ], GearBuiltinApi: [{ methods: {}, version: 1 }], }, signedExtensions: { VoucherLegitimate: { extrinsic: {}, payload: {} }, StakingBlackList: { extrinsic: {}, payload: {} }, }, ...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.defaultTypes = defaultTypes; this.programState = new State.GearProgramState(this); this.blocks = new Blocks.GearBlock(this); this.programStorage = new Storage.GearProgramStorage(this); this.claimValueFromMailbox = new Claim.GearClaimValue(this); this.mailbox = new Mailbox.GearMailbox(this); this.code = new Code.GearCode(this); this.waitlist = new Waitlist.GearWaitlist(this); this.ethBridge = new EthBridge.GearEthBridge(this); this.builtin = new Builtin.GearBuiltin(this); } 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; } } async inflationInfo() { const info = await this.rpc.stakingRewards.inflationInfo(); return this.createType('InflationInfo', info); } async wasmBlobVersion() { 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', }; } } exports.GearApi = GearApi;