1
+ import { Hex } from '@vechain/sdk-core' ;
1
2
import { type ThorClient } from '../../../../../thor-client' ;
2
3
import {
3
4
JSONRPCInternalError ,
4
5
JSONRPCInvalidParams ,
5
6
stringifyData
6
7
} from '@vechain/sdk-errors' ;
7
- import { CHAIN_ID } from '../../../const' ;
8
- import { networkInfo } from '@vechain/sdk-core' ;
8
+
9
+ // In-memory cache
10
+ let cachedChainId : Hex | null = null ;
11
+ let cachedChainTag : Hex | null = null ;
9
12
10
13
/**
11
14
* RPC Method eth_chainId implementation
12
15
*
13
16
* @link [eth_chainId](https://ethereum.github.io/execution-apis/api-documentation/)
14
- * @link [Chain IDs](https://chainlist.org/?search=vechain&testnets=true)
15
17
*
16
18
* @param thorClient - ThorClient instance.
17
- * @returns The chain id
19
+ * @returns Returns the block id of the genesis block.
18
20
* @throws {JSONRPCInvalidParams, JSONRPCInternalError }
19
21
*/
20
22
const ethChainId = async ( thorClient : ThorClient ) : Promise < string > => {
21
23
try {
24
+ if ( cachedChainId !== null ) return cachedChainId . toString ( ) ;
22
25
const genesisBlock = await thorClient . blocks . getGenesisBlock ( ) ;
23
-
24
26
if ( genesisBlock ?. id === null || genesisBlock ?. id === undefined ) {
25
27
throw new JSONRPCInvalidParams (
26
28
'eth_chainId()' ,
@@ -30,13 +32,19 @@ const ethChainId = async (thorClient: ThorClient): Promise<string> => {
30
32
}
31
33
) ;
32
34
}
33
-
34
- // We are on Mainnet
35
- if ( genesisBlock . id === networkInfo . mainnet . genesisBlock . id )
36
- return CHAIN_ID . MAINNET ;
37
-
38
- // Testnet OR Solo OR some other network
39
- return CHAIN_ID . TESTNET ;
35
+ if ( ! Hex . isValid ( genesisBlock . id ) ) {
36
+ throw new JSONRPCInvalidParams (
37
+ 'eth_chainId()' ,
38
+ 'The genesis block id is invalid. Unable to get the chain id.' ,
39
+ {
40
+ url : thorClient . httpClient . baseURL
41
+ }
42
+ ) ;
43
+ }
44
+ const genesisBlockId = Hex . of ( genesisBlock . id ) ;
45
+ cachedChainId = genesisBlockId ;
46
+ cachedChainTag = Hex . of ( genesisBlockId . bytes . slice ( - 2 ) ) ;
47
+ return cachedChainId . toString ( ) ;
40
48
} catch ( e ) {
41
49
throw new JSONRPCInternalError (
42
50
'eth_chainId()' ,
@@ -49,4 +57,28 @@ const ethChainId = async (thorClient: ThorClient): Promise<string> => {
49
57
}
50
58
} ;
51
59
52
- export { ethChainId } ;
60
+ /*
61
+ * Get the chain id from the cached value or fetch it from the network.
62
+ *
63
+ * @param thorClient - ThorClient instance.
64
+ * @returns The chain id.
65
+ */
66
+ const getCachedChainId = async ( thorClient : ThorClient ) : Promise < string > => {
67
+ return cachedChainId !== null
68
+ ? cachedChainId . toString ( )
69
+ : await ethChainId ( thorClient ) ;
70
+ } ;
71
+
72
+ /*
73
+ * Get the chain tag from the cached value or fetch it from the network.
74
+ *
75
+ * @param thorClient - ThorClient instance.
76
+ * @returns The chain tag.
77
+ */
78
+ const getCachedChainTag = async ( thorClient : ThorClient ) : Promise < string > => {
79
+ return cachedChainTag !== null
80
+ ? cachedChainTag . toString ( )
81
+ : await ethChainId ( thorClient ) ;
82
+ } ;
83
+
84
+ export { ethChainId , getCachedChainId , getCachedChainTag } ;
0 commit comments