@@ -12,34 +12,18 @@ contract Chainweb is CommonBase {
1212 uint256 private _chainIdOffset;
1313 uint24 private _chainwebChainIdOffset;
1414 uint256 [] private _chainForks;
15+ string private _hostUrl;
1516
1617 constructor (
1718 uint24 chainNumbers ,
1819 uint256 chainIdOffset ,
19- uint24 chainwebChainIdOffset
20+ uint24 chainwebChainIdOffset ,
21+ string memory hostUrl
2022 ) {
2123 _chains = chainNumbers;
2224 _chainIdOffset = chainIdOffset;
2325 _chainwebChainIdOffset = chainwebChainIdOffset;
24- }
25-
26- function uintToString (uint256 value ) internal pure returns (string memory ) {
27- if (value == 0 ) {
28- return "0 " ;
29- }
30- uint256 temp = value;
31- uint256 digits;
32- while (temp != 0 ) {
33- digits++ ;
34- temp /= 10 ;
35- }
36- bytes memory buffer = new bytes (digits);
37- while (value != 0 ) {
38- digits -= 1 ;
39- buffer[digits] = bytes1 (uint8 (48 + uint256 (value % 10 )));
40- value /= 10 ;
41- }
42- return string (buffer);
26+ _hostUrl = hostUrl;
4327 }
4428
4529 function getNodePath () private returns (string memory ) {
@@ -67,15 +51,28 @@ contract Chainweb is CommonBase {
6751 function setupChainsForScript () public {
6852 console.log ("Setting main RPC node for " , _chains, "chains " );
6953 for (uint256 i = 0 ; i < _chains; i++ ) {
70- string memory url = rpcUrl (uintToString (i + _chainIdOffset));
71- console.log ("Forking " , url);
72- _chainForks.push (vm.createFork (url));
54+ if (bytes (_hostUrl).length > 0 ) {
55+ string memory url = string (
56+ abi.encodePacked (
57+ _hostUrl,
58+ "/chain/ " ,
59+ vm.toString (i + _chainwebChainIdOffset),
60+ "/evm/rpc "
61+ )
62+ );
63+ console.log ("Using custom RPC URL: " , url);
64+ _chainForks.push (vm.createFork (url));
65+ } else {
66+ string memory url = rpcUrl (vm.toString (i + _chainIdOffset));
67+ console.log ("Forking " , url);
68+ _chainForks.push (vm.createFork (url));
69+ }
7370 }
7471 }
7572
7673 function setupChainsForTest () public {
7774 console.log ("Setting main RPC node for " , _chains, "chains " );
78- string memory url = rpcUrl (uintToString (_chainIdOffset));
75+ string memory url = rpcUrl (vm. toString (_chainIdOffset));
7976 console.log ("Forking " , url);
8077 for (uint24 i = 0 ; i < _chains; i++ ) {
8178 _chainForks.push (vm.createFork (url));
@@ -94,9 +91,17 @@ contract Chainweb is CommonBase {
9491 require (chainIndex < _chains, "Invalid chain ID " );
9592 uint256 forkId = _chainForks[chainIndex];
9693 vm.selectFork (forkId);
97- vm.chainId (chainId + _chainIdOffset); // Example offset for chain ID
94+ vm.chainId (chainIndex + _chainIdOffset); // Example offset for chain ID
9895 console.log ("Switched to chain: " , chainId);
9996 }
97+
98+ function getChainIds () public view returns (uint256 [] memory ) {
99+ uint256 [] memory chainIds = new uint256 [](_chains);
100+ for (uint24 i = 0 ; i < _chains; i++ ) {
101+ chainIds[i] = i + _chainwebChainIdOffset;
102+ }
103+ return chainIds;
104+ }
100105}
101106
102107struct ChainwebConfig {
@@ -113,7 +118,8 @@ contract ChainwebTest is Test {
113118 chainweb = new Chainweb (
114119 chainNumbers,
115120 block .chainid ,
116- chainwebChainIdOffset
121+ chainwebChainIdOffset,
122+ ""
117123 );
118124 chainweb.setupChainsForTest ();
119125 }
@@ -123,10 +129,15 @@ contract ChainwebScript is Script {
123129 Chainweb public chainweb;
124130
125131 constructor (uint24 chainNumbers , uint24 chainwebChainIdOffset ) {
132+ string memory nodeUrl = vm.envExists ("CHAINWEB_HOST " )
133+ ? vm.envString ("CHAINWEB_HOST " )
134+ : "" ;
135+ console.log ("Using node URL: " , nodeUrl);
126136 chainweb = new Chainweb (
127137 chainNumbers,
128138 block .chainid ,
129- chainwebChainIdOffset
139+ chainwebChainIdOffset,
140+ nodeUrl
130141 );
131142 chainweb.setupChainsForScript ();
132143 }
0 commit comments