-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat: Add getBlockByTag to Chain package
- Loading branch information
William Cory
authored and
William Cory
committed
Jul 14, 2024
1 parent
de2a2ab
commit 14d89e5
Showing
5 changed files
with
56 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { hexToBytes, hexToNumber, isHex } from '@tevm/utils' | ||
import { getBlock } from './getBlock.js' | ||
import { UnknownBlockError } from '@tevm/errors' | ||
import { getBlockFromRpc } from '../utils/getBlockFromRpc.js' | ||
import { putBlock } from './putBlock.js' | ||
|
||
/** | ||
* @param {import('../BaseChain.js').BaseChain} baseChain | ||
* @returns {import('../Chain.js').Chain['getBlockByTag']} | ||
*/ | ||
export const getBlockByTag = (baseChain) => async (blockId) => { | ||
const _getBlock = getBlock(baseChain) | ||
if (isHex(blockId)) { | ||
return blockId.length === 66 ? _getBlock(hexToBytes(blockId)) : _getBlock(hexToNumber(blockId)) | ||
} | ||
if (typeof blockId === 'number' || typeof blockId === 'bigint' || blockId instanceof Uint8Array) { | ||
return _getBlock(blockId) | ||
} | ||
const block = baseChain.blocksByTag.get(blockId) | ||
if (!block && baseChain.options.fork?.transport) { | ||
const block = await getBlockFromRpc( | ||
{ | ||
transport: baseChain.options.fork.transport, | ||
blockTag: blockId, | ||
}, | ||
baseChain.common, | ||
) | ||
await putBlock(baseChain)(block) | ||
return block | ||
} | ||
if (!block) { | ||
throw new UnknownBlockError(blockId) | ||
} | ||
return block | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.