Skip to content

Commit

Permalink
feat(api) add support for builtin pallet (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zewasik authored Oct 29, 2024
1 parent d62cace commit bd77968
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/src/Builtin.ts
Original file line number Diff line number Diff line change
@@ -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<HexString> {
try {
const result = await this._api.rpc.gearBuiltin.queryId(builtinId);
return result.toHex();
} catch (err) {
throw new BuiltinQueryIdError(err.message);
}
}
}
3 changes: 3 additions & 0 deletions api/src/GearApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {}) {
Expand Down Expand Up @@ -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<GearApi> {
Expand Down
9 changes: 9 additions & 0 deletions api/src/errors/builtin.errors.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
}
1 change: 1 addition & 0 deletions api/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './state.errors';
export * from './claim.errors';
export * from './blocks.errors';
export * from './validation.errors';
export * from './builtin.errors';
5 changes: 4 additions & 1 deletion api/src/types/augment/augment-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -163,5 +163,8 @@ declare module '@polkadot/rpc-core/types/jsonrpc' {
stakingRewards: {
inflationInfo: AugmentedRpc<(at?: BlockHash | string | Uint8Array) => Observable<InflationInfo>>;
};
gearBuiltin: {
queryId: AugmentedRpc<(builtinId: u64) => Observable<H256>>;
};
}
}

0 comments on commit bd77968

Please sign in to comment.