From bd779680cfda41be325c7c46c090a98851ef960a Mon Sep 17 00:00:00 2001 From: Zewasik <89100777+Zewasik@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:53:48 +0200 Subject: [PATCH] feat(api) add support for `builtin` pallet (#1659) --- api/src/Builtin.ts | 17 +++++++++++++++++ api/src/GearApi.ts | 3 +++ api/src/errors/builtin.errors.ts | 9 +++++++++ api/src/errors/index.ts | 1 + api/src/types/augment/augment-api-rpc.ts | 5 ++++- 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 api/src/Builtin.ts create mode 100644 api/src/errors/builtin.errors.ts diff --git a/api/src/Builtin.ts b/api/src/Builtin.ts new file mode 100644 index 0000000000..79d0844787 --- /dev/null +++ b/api/src/Builtin.ts @@ -0,0 +1,17 @@ +import { HexString } from 'types'; +import { GearApi } from './GearApi'; +import { u64 } from '@polkadot/types-codec'; +import { BuiltinQueryIdError } from './errors'; + +export class GearBuiltin { + constructor(private _api: GearApi) {} + + async queryId(builtinId: u64): Promise { + try { + const result = await this._api.rpc.gearBuiltin.queryId(builtinId); + return result.toHex(); + } catch (err) { + throw new BuiltinQueryIdError(err.message); + } + } +} diff --git a/api/src/GearApi.ts b/api/src/GearApi.ts index ba43f2a4a5..5644589438 100644 --- a/api/src/GearApi.ts +++ b/api/src/GearApi.ts @@ -16,6 +16,7 @@ import { GearProgramState } from './State'; import { GearProgramStorage } from './Storage'; import { GearVoucher } from './Voucher'; import { GearWaitlist } from './Waitlist'; +import { GearBuiltin } from './Builtin'; export class GearApi extends ApiPromise { public program: GearProgram; @@ -31,6 +32,7 @@ export class GearApi extends ApiPromise { public code: GearCode; public waitlist: GearWaitlist; public voucher: GearVoucher; + public builtin: GearBuiltin; public provider: WsProvider; constructor(options: GearApiOptions = {}) { @@ -87,6 +89,7 @@ export class GearApi extends ApiPromise { this.mailbox = new GearMailbox(this); this.code = new GearCode(this); this.waitlist = new GearWaitlist(this); + this.builtin = new GearBuiltin(this); } static async create(options?: GearApiOptions): Promise { diff --git a/api/src/errors/builtin.errors.ts b/api/src/errors/builtin.errors.ts new file mode 100644 index 0000000000..343c1d3345 --- /dev/null +++ b/api/src/errors/builtin.errors.ts @@ -0,0 +1,9 @@ +export class BuiltinQueryIdError extends Error { + name = 'BuiltInQueryIdError'; + message = 'Unable to query builtin id'; + + constructor(message?: string) { + super(); + this.message = `${this.message}. ${message}`; + } +} diff --git a/api/src/errors/index.ts b/api/src/errors/index.ts index 7de4a001d5..6f0b6edc5b 100644 --- a/api/src/errors/index.ts +++ b/api/src/errors/index.ts @@ -5,3 +5,4 @@ export * from './state.errors'; export * from './claim.errors'; export * from './blocks.errors'; export * from './validation.errors'; +export * from './builtin.errors'; diff --git a/api/src/types/augment/augment-api-rpc.ts b/api/src/types/augment/augment-api-rpc.ts index edd9accd21..d82c8772d7 100644 --- a/api/src/types/augment/augment-api-rpc.ts +++ b/api/src/types/augment/augment-api-rpc.ts @@ -3,7 +3,7 @@ import '@polkadot/rpc-core/types/jsonrpc'; import type { AnyNumber, Codec } from '@polkadot/types-codec/types'; import type { AugmentedRpc } from '@polkadot/rpc-core/types'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; -import type { Bytes } from '@polkadot/types-codec'; +import type { Bytes, u64 } from '@polkadot/types-codec'; import type { H256 } from '@polkadot/types/interfaces/runtime'; import type { Observable } from '@polkadot/types/types'; @@ -163,5 +163,8 @@ declare module '@polkadot/rpc-core/types/jsonrpc' { stakingRewards: { inflationInfo: AugmentedRpc<(at?: BlockHash | string | Uint8Array) => Observable>; }; + gearBuiltin: { + queryId: AugmentedRpc<(builtinId: u64) => Observable>; + }; } }