Skip to content

Commit 9c39cd9

Browse files
authored
Add .multi support on api.at(...).<section>.<method> (#4478)
1 parent 43e2c44 commit 9c39cd9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Contributed:
99
Changes:
1010

1111
- Adjust `Registry` to augment based on used packages
12+
- Add `.multi` support on `api.at(...).<section>.<method>`
1213
- Add support for ink! metadata V3 with payable constructors
1314
- Cleanup ink! metadata parsing, allowing for easier extension
1415
- Fix storage metadata, aligning method with decorated name

packages/api/src/base/Decorate.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,15 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
613613
this._retrieveMapKeys(creator, blockHash, args)));
614614
}
615615

616+
if (this.supportMulti && creator.meta.type.isMap) {
617+
// When using double map storage function, user need to pass double map key as an array
618+
decorated.multi = decorateMethod((args: unknown[]): Observable<Codec[]> =>
619+
creator.meta.type.asMap.hashers.length === 1
620+
? this._retrieveMulti(args.map((a) => [creator, [a]]), blockHash)
621+
: this._retrieveMulti(args.map((a) => [creator, a as unknown[]]), blockHash)
622+
);
623+
}
624+
616625
/* eslint-enable @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
617626

618627
return this._decorateFunctionMeta(creator as unknown as MetaDecoration, decorated) as unknown as QueryableStorageEntry<ApiType>;
@@ -647,17 +656,21 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
647656
}
648657

649658
// retrieve a set of values for a specific set of keys - here we chunk the keys into PAGE_SIZE sizes
650-
private _retrieveMulti (keys: [StorageEntry, unknown[]][]): Observable<Codec[]> {
659+
private _retrieveMulti (keys: [StorageEntry, unknown[]][], blockHash?: Uint8Array): Observable<Codec[]> {
651660
if (!keys.length) {
652661
return of([]);
653662
}
654663

655-
const queryCall = this.hasSubscriptions
664+
const queryCall = this.hasSubscriptions && !blockHash
656665
? this._rpcCore.state.subscribeStorage
657666
: this._rpcCore.state.queryStorageAt;
658667

659668
return combineLatest(
660-
arrayChunk(keys, PAGE_SIZE_V).map((keys) => queryCall(keys))
669+
arrayChunk(keys, PAGE_SIZE_V).map((k) =>
670+
blockHash
671+
? queryCall(k, blockHash)
672+
: queryCall(k)
673+
)
661674
).pipe(
662675
map(arrayFlatten)
663676
);

0 commit comments

Comments
 (0)