Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 11, 2024
1 parent 2e6cc58 commit 02e724d
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions packages/eth-providers/src/utils/queryStoarge.ts
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

Check warning on line 37 in packages/eth-providers/src/utils/queryStoarge.ts

View check run for this annotation

Codecov / codecov/patch

packages/eth-providers/src/utils/queryStoarge.ts#L37

Added line #L37 was not covered by tests
: 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;
};

0 comments on commit 02e724d

Please sign in to comment.