-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e6cc58
commit 02e724d
Showing
1 changed file
with
33 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,53 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { Codec } from '@polkadot/types/types'; | ||
import { decorateStorage, unwrapStorageType } from '@polkadot/types'; | ||
import { isNull, u8aToU8a } from '@polkadot/util'; | ||
import { u8aToU8a } from '@polkadot/util'; | ||
|
||
export const queryStorage = async <T extends Codec = Codec>( | ||
api: ApiPromise, | ||
module: `${string}.${string}`, | ||
args: any[], | ||
blockHash: string, | ||
): Promise<T> => { | ||
const apiAt = await api.at(blockHash); | ||
const [section, method] = module.split('.'); | ||
const res = await apiAt.query[section][method](...args); | ||
return res as T; | ||
}; | ||
|
||
// export const queryStorage = async <T extends Codec = Codec>( | ||
// api: ApiPromise, | ||
// module: `${string}.${string}`, | ||
// args: any[], | ||
// blockHash: string, | ||
// ): Promise<T> => { | ||
// const registry = await api.getBlockRegistry(u8aToU8a(blockHash)); | ||
const registry = await api.getBlockRegistry(u8aToU8a(blockHash)); | ||
|
||
// const storage = decorateStorage( | ||
// registry.registry, | ||
// registry.metadata.asLatest, | ||
// registry.metadata.version, | ||
// ); | ||
const storage = decorateStorage( | ||
registry.registry, | ||
registry.metadata.asLatest, | ||
registry.metadata.version, | ||
); | ||
|
||
// const [section, method] = module.split('.'); | ||
const [section, method] = module.split('.'); | ||
|
||
// const entry = storage[section][method]; | ||
// const key = entry(...args); | ||
const entry = storage[section][method]; | ||
const key = entry(...args); | ||
|
||
// const outputType = unwrapStorageType( | ||
// registry.registry, | ||
// entry.meta.type, | ||
// entry.meta.modifier.isOptional, | ||
// ); | ||
const outputType = unwrapStorageType( | ||
registry.registry, | ||
entry.meta.type, | ||
entry.meta.modifier.isOptional, | ||
); | ||
|
||
|
||
// const value: any = await api.rpc.state.getStorage(key, blockHash); | ||
const value: any = await api.rpc.state.getStorage(key, blockHash); | ||
|
||
// // we convert to Uint8Array since it maps to the raw encoding, all | ||
// // data will be correctly encoded (incl. numbers, excl. :code) | ||
// const input = value === null | ||
// ? null | ||
// : u8aToU8a( | ||
// entry.meta.modifier.isOptional | ||
// ? value.toU8a() | ||
// : value.isSome | ||
// ? value.unwrap().toU8a() | ||
// : null, | ||
// ); | ||
// we convert to Uint8Array since it maps to the raw encoding, all | ||
// data will be correctly encoded (incl. numbers, excl. :code) | ||
const input = value === null | ||
? null | ||
: u8aToU8a( | ||
entry.meta.modifier.isOptional | ||
? value.toU8a() | ||
: value.isSome | ||
? value.unwrap().toU8a() | ||
: null, | ||
); | ||
|
||
|
||
// const result = registry.registry.createTypeUnsafe<T>(outputType, [input], { | ||
// blockHash, | ||
// isPedantic: !entry.meta.modifier.isOptional, | ||
// }); | ||
const result = registry.registry.createTypeUnsafe<T>(outputType, [input], { | ||
blockHash, | ||
isPedantic: !entry.meta.modifier.isOptional, | ||
}); | ||
|
||
// return result; | ||
// }; | ||
return result; | ||
}; |