UNPKG

@gear-js/api

Version:

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

200 lines (197 loc) 6.66 kB
import { ApiPromise, WsProvider } from '@polkadot/api'; import { GearBalance } from './api/Balance.js'; import { GearBlock } from './api/Blocks.js'; import { GearBuiltin } from './api/Builtin.js'; import { GearCode } from './api/Code.js'; import { GearEthBridge } from './api/eth-bridge/index.js'; import './consts.js'; import '@polkadot/util'; import '@polkadot/types'; import { RUNTIME_METHODS, STAKING_REWARDS_METHODS, GEAR_ETH_BRIDGE_RPC_METHODS, GEAR_BUILTIN_RPC_METHODS, GEAR_RPC_METHODS } from './default/rpc.js'; import { GEAR_TYPES } from './default/types.js'; import 'node:assert'; import './metadata/programMetadata.js'; import './utils/generate.js'; import './utils/reply-code.js'; import '@polkadot/util-crypto'; 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'; import { GearEvents } from './events/Events.js'; class GearApi extends ApiPromise { program; /** @deprecated */ programState; programStorage; message; balance; gearEvents; blocks; mailbox; code; waitlist; voucher; ethBridge; builtin; provider; _rpcMethods; constructor(options = {}) { const { types = {}, derives = {}, providerAddress, noInitWarn, ...restOptions } = options; const provider = restOptions?.provider || new WsProvider(providerAddress ?? 'ws://127.0.0.1:9944'); super({ provider, derives, types: { ...types, ...GEAR_TYPES, }, rpc: { gear: GEAR_RPC_METHODS, gearBuiltin: GEAR_BUILTIN_RPC_METHODS, gearEthBridge: GEAR_ETH_BRIDGE_RPC_METHODS, stakingRewards: STAKING_REWARDS_METHODS, runtime: 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 GearProgram(this); this.voucher = new GearVoucher(this); this.message = new GearMessage(this); this.balance = new GearBalance(this); this.gearEvents = new GearEvents(this); this.programState = new GearProgramState(this); this.blocks = new GearBlock(this); this.programStorage = new GearProgramStorage(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); 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; } } export { GearApi };