Refine Block/Log types when not using pending block tag
#846
Answered
by
jxom
frolic
asked this question in
Idea / Feature Request
-
|
I am fetching blocks/logs with block tags other than I am currently working around this by wrapping the types (below), but it would be nice if viem took into consideration the block tag or whether the returned block/log could be pending (similar to strict log parsing). Pending feels like a less common use case, so being able to opt-in might save some conditionals in user land. import type { Block } from "viem";
export type NonPendingBlock<TBlock extends Block> = TBlock & {
hash: NonNullable<TBlock["hash"]>;
logsBloom: NonNullable<TBlock["logsBloom"]>;
nonce: NonNullable<TBlock["nonce"]>;
number: NonNullable<TBlock["number"]>;
};
export function isNonPendingBlock<TBlock extends Block>(block: TBlock): block is NonPendingBlock<TBlock> {
return block.hash != null && block.logsBloom != null && block.nonce != null && block.number != null;
}import type { Log } from "viem";
export type NonPendingLog<TLog extends Log> = TLog & {
blockHash: NonNullable<TLog["blockHash"]>;
blockNumber: NonNullable<TLog["blockNumber"]>;
logIndex: NonNullable<TLog["logIndex"]>;
transactionHash: NonNullable<TLog["transactionHash"]>;
transactionIndex: NonNullable<TLog["transactionIndex"]>;
};
export function isNonPendingLog<TLog extends Log>(log: TLog): log is NonPendingLog<TLog> {
return (
log.blockHash != null &&
log.blockNumber != null &&
log.logIndex != null &&
log.transactionHash != null &&
log.transactionIndex != null
);
} |
Beta Was this translation helpful? Give feedback.
Answered by
jxom
Jul 5, 2023
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
frolic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#847