UNPKG

@gear-js/api

Version:

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

186 lines (183 loc) 5.88 kB
import { ApiPromise, WsProvider } from '@polkadot/api'; import { gearTypes, gearRpc } from './default/index.js'; import '@polkadot/types'; import { GearEvents } from './events/Events.js'; import { GearBalance } from './api/Balance.js'; import { GearBlock } from './api/Blocks.js'; import { GearBuiltin } from './api/Builtin.js'; import { GearClaimValue } from './api/Claim.js'; import { GearCode } from './api/Code.js'; import { GearEthBridge } from './api/eth-bridge/index.js'; import './consts.js'; import '@polkadot/util'; import './utils/generate.js'; import '@polkadot/util-crypto'; import 'assert'; import './metadata/programMetadata.js'; import { GearMailbox } from './api/Mailbox.js'; import { GearMessage } from './api/Message.js'; import { GearProgram } from './api/Program.js'; import { GearProgramState } from './api/State.js'; import { GearProgramStorage } from './api/Storage.js'; import { GearVoucher } from './api/Voucher.js'; import { GearWaitlist } from './api/Waitlist.js'; class GearApi extends ApiPromise { program; /** @deprecated */ programState; programStorage; message; balance; gearEvents; defaultTypes; blocks; mailbox; claimValueFromMailbox; code; waitlist; voucher; ethBridge; builtin; provider; constructor(options = {}) { const { types, providerAddress, noInitWarn, ...restOptions } = options; const provider = restOptions?.provider || new WsProvider(providerAddress ?? 'ws://127.0.0.1:9944'); const defaultTypes = types ? { ...types, ...gearTypes } : gearTypes; super({ provider, derives: {}, types: { ...defaultTypes, }, rpc: { ...gearRpc, }, runtime: { GearApi: [ { methods: {}, version: 2, }, ], Vara: [ { methods: {}, version: 1, }, { methods: {}, version: 1030, }, ], GearBuiltinApi: [{ methods: {}, version: 1 }], }, signedExtensions: { VoucherLegitimate: { extrinsic: {}, payload: {} }, StakingBlackList: { extrinsic: {}, payload: {} }, }, noInitWarn: noInitWarn ?? true, ...restOptions, }); this.provider = provider; this.program = new GearProgram(this); this.voucher = new GearVoucher(this); this.message = new GearMessage(this); this.balance = new GearBalance(this); this.gearEvents = new GearEvents(this); this.defaultTypes = defaultTypes; this.programState = new GearProgramState(this); this.blocks = new GearBlock(this); this.programStorage = new GearProgramStorage(this); this.claimValueFromMailbox = new GearClaimValue(this); this.mailbox = new GearMailbox(this); this.code = new GearCode(this); this.waitlist = new GearWaitlist(this); this.ethBridge = new GearEthBridge(this); this.builtin = new 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', }; } } export { GearApi };