-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
26 changed files
with
274 additions
and
203 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
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
89 changes: 51 additions & 38 deletions
89
packages/network/src/provider/utils/const/blocks/blocks.ts
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,46 +1,59 @@ | ||
import { Hex } from '@vechain/sdk-core'; | ||
import { type BlockQuantityInputRPC } from '../../rpc-mapper/types'; | ||
import { HexUInt, Revision } from '@vechain/sdk-core'; | ||
import { JSONRPCInvalidDefaultBlock } from '@vechain/sdk-errors'; | ||
|
||
type DefaultBlock = | ||
| `0x${string}` | ||
| 'latest' | ||
| 'earliest' | ||
| 'pending' | ||
| 'safe' | ||
| 'finalized'; | ||
const defaultBlockTags: DefaultBlock[] = [ | ||
'latest', | ||
'earliest', | ||
'pending', | ||
'safe', | ||
'finalized' | ||
]; | ||
|
||
/** | ||
* Get the correct block number for the given block number. | ||
* | ||
* @param block - The block tag to get. | ||
* 'latest' or 'earliest' or 'pending' or 'safe' or 'finalized' | ||
* or an object: { blockNumber: number } or { blockHash: string } | ||
* Maps the Ethereum "default block" type to VeChainThor Revision type. | ||
* Ethereum "default block" can be: | ||
* - 'latest' or 'earliest' or 'pending' or 'safe' or 'finalized' | ||
* - a hexadecimal block number | ||
* VeChainThor revision type can be: | ||
* - 'best', 'next', 'justified', 'finalized' | ||
* - a hexadecimal block Id | ||
* - a integer block number | ||
* | ||
* @note | ||
* * Currently VeChainThor supports 'earliest', 'latest' and 'finalized' as block tags. | ||
* So 'pending' and 'safe' are converted to 'best' which is the alias for 'latest' and 'finalized' in VeChainThor. | ||
* @param defaultBlock - The Ethereum default block type to convert | ||
* @returns The VeChainThor revision type | ||
*/ | ||
const getCorrectBlockNumberRPCToVeChain = ( | ||
block: BlockQuantityInputRPC | ||
): string => { | ||
// Tag block number | ||
if (typeof block === 'string') { | ||
// Latest, Finalized, Safe blocks | ||
if ( | ||
block === 'latest' || | ||
block === 'finalized' || | ||
block === 'safe' || | ||
block === 'pending' | ||
) | ||
// 'best' is the alias for 'latest', 'finalized' and 'safe' in VeChainThor | ||
return 'best'; | ||
|
||
// Earliest block | ||
if (block === 'earliest') return Hex.of(0).toString(); | ||
|
||
// Hex number of block | ||
return block; | ||
const DefaultBlockToRevision = (defaultBlock: DefaultBlock): Revision => { | ||
// if valid hex then return integer block number | ||
if (HexUInt.isValid(defaultBlock)) { | ||
return Revision.of(HexUInt.of(defaultBlock).n.toString()); | ||
} | ||
|
||
// Object with block number | ||
if (block.blockNumber !== undefined) { | ||
return Hex.of(block.blockNumber).toString(); | ||
// check if default block is a valid block tag | ||
if (!defaultBlockTags.includes(defaultBlock)) { | ||
const defaultBlockValue = defaultBlock.toString(); | ||
throw new JSONRPCInvalidDefaultBlock( | ||
'DefaultBlockToRevision', | ||
`Invalid default block: ${defaultBlockValue}`, | ||
defaultBlockValue, | ||
null | ||
); | ||
} | ||
// map block tag to VeChainThor revision | ||
if (defaultBlock === 'earliest') { | ||
return Revision.of(HexUInt.of(0)); | ||
} else if (defaultBlock === 'safe') { | ||
return Revision.of('justified'); | ||
} else if (defaultBlock === 'finalized') { | ||
return Revision.of('finalized'); | ||
} else { | ||
return Revision.of('best'); | ||
} | ||
|
||
// Object with block hash - Default case | ||
return block.blockHash; | ||
}; | ||
|
||
export { getCorrectBlockNumberRPCToVeChain }; | ||
export { type DefaultBlock, DefaultBlockToRevision }; |
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,3 +1,2 @@ | ||
export * from './methods'; | ||
export * from './rpc-mapper'; | ||
export type * from './types.d'; |
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
Oops, something went wrong.
5121de3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Coverage
Summary