Skip to content

Commit

Permalink
feat!: support for NStorageMaps (#1053)
Browse files Browse the repository at this point in the history
* feat!: support for NStorageMaps

* update docs

* fix merge conflict
  • Loading branch information
TarikGul authored Sep 28, 2022
1 parent 1e09ae7 commit a00a6b0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

30 changes: 12 additions & 18 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1067,19 +1067,17 @@ paths:
required: false
schema:
type: boolean
- name: key1
- name: keys
in: query
description: Key for a map, or first key for a double map. Required for querying
a map.
description: Set of N keys used for querying a storage map. It should be queried using the
following format - ?keys[]=key1&keys[]=key2. Order matters, as it will determine the
order the keys are passed into the storage calls.
required: false
schema:
type: string
- name: key2
in: query
description: Second key for a double map. Required for querying a double map.
required: false
schema:
type: string
type: array
items:
type: string
description: An array of storage keys.
- name: at
in: query
description: Block at which to query the storage item at.
Expand Down Expand Up @@ -2210,14 +2208,10 @@ components:
type: string
description: Name of the storage item.
example: "referendumInfoOf"
key1:
type: string
description: Key1 query param. Will not show up in response unless it was passed as part of the URI.
example: "2"
key2:
type: string
description: Key2 query param. Will not show up in response if not defined in URI.
example: ""
keys:
type: array
description: N Storage keys passed in as the `keys` query param.
example: ["0x00", "0x01"]
value:
type: object
description: Value returned by this storage query.
Expand Down
21 changes: 14 additions & 7 deletions src/controllers/pallets/PalletsStorageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { RequestHandler } from 'express-serve-static-core';
import { validateBoolean } from '../..//middleware';
import { Log } from '../../logging/Log';
import { PalletsStorageService } from '../../services';
import {
IPalletsStorageParam,
IPalletsStorageQueryParam,
} from '../../types/requests';
import AbstractController from '../AbstractController';

/**
Expand Down Expand Up @@ -49,20 +53,24 @@ export default class PalletsStorageController extends AbstractController<Pallets
protected initRoutes(): void {
this.router.use(this.path, validateBoolean(['adjustMetadataV13']));
this.safeMountAsyncGetHandlers([
['/:storageItemId', this.getStorageItem],
['/:storageItemId', this.getStorageItem as RequestHandler],
['/', this.getStorage],
]);
}

private getStorageItem: RequestHandler = async (
private getStorageItem: RequestHandler<
IPalletsStorageParam,
unknown,
unknown,
IPalletsStorageQueryParam
> = async (
{
query: { at, key1, key2, metadata, adjustMetadataV13 },
query: { at, keys, metadata, adjustMetadataV13 },
params: { palletId, storageItemId },
},
res
): Promise<void> => {
const key1Arg = typeof key1 === 'string' ? key1 : undefined;
const key2Arg = typeof key2 === 'string' ? key2 : undefined;
const parsedKeys = Array.isArray(keys) ? keys : [];
const metadataArg = metadata === 'true';
const adjustMetadataV13Arg = adjustMetadataV13 === 'true';

Expand All @@ -78,8 +86,7 @@ export default class PalletsStorageController extends AbstractController<Pallets
// stringCamelCase ensures we don't have snake case or kebab case
palletId: stringCamelCase(palletId),
storageItemId: stringCamelCase(storageItemId),
key1: key1Arg,
key2: key2Arg,
keys: parsedKeys,
metadata: metadataArg,
adjustMetadataV13Arg,
})
Expand Down
9 changes: 3 additions & 6 deletions src/services/pallets/PalletsStorageService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: 'democracy',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
keys: ['0'],
metadata: false,
adjustMetadataV13Arg: true,
})
Expand All @@ -75,8 +74,7 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: '15',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
keys: ['0'],
metadata: false,
adjustMetadataV13Arg: true,
})
Expand All @@ -91,8 +89,7 @@ describe('PalletStorageService', () => {
hash: blockHash789629,
palletId: 'democracy',
storageItemId: 'referendumInfoOf',
key1: '0',
key2: undefined,
keys: ['0'],
metadata: true,
adjustMetadataV13Arg: true,
})
Expand Down
11 changes: 4 additions & 7 deletions src/services/pallets/PalletsStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ interface IFetchPalletArgs {

interface IFetchStorageItemArgs extends IFetchPalletArgs {
storageItemId: string;
key1?: string;
key2?: string;
keys: string[];
metadata: boolean;
adjustMetadataV13Arg: boolean;
}
Expand All @@ -66,8 +65,7 @@ export class PalletsStorageService extends AbstractService {
hash,
palletId,
storageItemId,
key1,
key2,
keys,
metadata,
adjustMetadataV13Arg,
}: IFetchStorageItemArgs
Expand Down Expand Up @@ -95,7 +93,7 @@ export class PalletsStorageService extends AbstractService {
}

const [value, { number }] = await Promise.all([
historicApi.query[palletName][storageItemId](key1, key2),
historicApi.query[palletName][storageItemId](...keys),
this.api.rpc.chain.getHeader(hash),
]);

Expand All @@ -107,8 +105,7 @@ export class PalletsStorageService extends AbstractService {
pallet: palletName,
palletIndex: palletMetaIdx,
storageItem: storageItemId,
key1,
key2,
keys,
value,
metadata: normalizedStorageItemMeta,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"pallet": "democracy",
"palletIndex": "15",
"storageItem": "referendumInfoOf",
"key1": "0"
"keys": ["0"]
}
11 changes: 11 additions & 0 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ export interface IRangeQueryParam extends Query {
range: string;
}

export interface IPalletsStorageParam extends ParamsDictionary {
palletId: string;
storageItemId: string;
}

export interface IPalletsStorageQueryParam extends Query {
keys: string[];
metadata: string;
adjustMetadataV13: string;
}

export interface IConvertQueryParams extends Query {
scheme: string;
prefix: string;
Expand Down
3 changes: 1 addition & 2 deletions src/types/responses/PalletStorageItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { IPallet, ISanitizedStorageItemMetadata } from '.';

export interface IPalletStorageItem extends IPallet {
storageItem: string;
key1: string | undefined;
key2: string | undefined;
keys: string[];
value: Codec;
metadata: ISanitizedStorageItemMetadata | undefined;
}

0 comments on commit a00a6b0

Please sign in to comment.