Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 3, 2024
1 parent f2dec75 commit d7fb75a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
11 changes: 8 additions & 3 deletions packages/eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import {
filterLogByTopics,
getAllReceiptsAtBlock,
getHealthResult,
getTimestamp,
getTransactionRequest,
hexlifyRpcResult,
isEvmExtrinsic,
Expand Down Expand Up @@ -609,6 +608,12 @@ export abstract class BaseProvider extends AbstractProvider {
: this.bestBlockNumber
);

getTimestamp = async (blockHash: string): Promise<number> => {
const apiAt = await this.getApiAt(blockHash);
const timestamp = await apiAt.query.timestamp.now();
return timestamp.toNumber();
};

getBlockData = async (_blockTag: BlockTag | Promise<BlockTag>, full?: boolean): Promise<BlockData> => {
const blockTag = await this._ensureSafeModeBlockTagFinalization(_blockTag);
const header = await this._getBlockHeader(blockTag);
Expand All @@ -629,7 +634,7 @@ export abstract class BaseProvider extends AbstractProvider {
const [block, headerExtended, timestamp, receiptsFromSubql] = await Promise.all([
this.api.rpc.chain.getBlock(blockHash),
this.api.derive.chain.getHeader(blockHash),
getTimestamp(this.api, blockHash),
this.getTimestamp(blockHash),
this.subql?.getAllReceiptsAtBlock(blockHash),
]);

Expand Down Expand Up @@ -1408,7 +1413,7 @@ export abstract class BaseProvider extends AbstractProvider {
result.blockNumber = startBlock;
result.blockHash = startBlockHash;

const timestamp = await getTimestamp(this.api, result.blockHash);
const timestamp = await this.getTimestamp(result.blockHash);
result.timestamp = Math.floor(timestamp / 1000);

result.wait = async (confirms?: number, timeoutMs?: number) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/eth-providers/src/utils/parseBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getAllReceiptsAtBlock = async (
blockHash: string,
targetTxHash?: string
): Promise<TransactionReceipt[]> => {
const apiAtTargetBlock = await api.at(blockHash);
const apiAtTargetBlock = await api.at(blockHash); // TODO: integrate cache for this

const [block, blockEvents] = await Promise.all([
api.rpc.chain.getBlock(blockHash),
Expand Down
6 changes: 0 additions & 6 deletions packages/eth-providers/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,3 @@ export const toBN = (bigNumberis: BigNumberish = 0): BN => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new BN(bigNumberis as any);
};

export const getTimestamp = async (api: ApiPromise, blockHash: string): Promise<number> => {
const apiAt = await api.at(blockHash);
const timestamp = await apiAt.query.timestamp.now();
return timestamp.toNumber();
};

0 comments on commit d7fb75a

Please sign in to comment.