@@ -108,35 +108,78 @@ struct ChainwebConfig {
108108 uint256 numberOfChains;
109109 uint256 chainIdOffset;
110110 uint256 chainwebChainIdOffset;
111+ string extenalHostUrl;
111112}
112113
113- contract ChainwebTest is Test {
114+ contract ChainwebConfigReader is CommonBase {
115+ function readOptionalJsonUint (string memory json , string memory key , uint256 defaultValue ) internal returns (uint256 ) {
116+ try vm.parseJsonUint (json, key) returns (uint256 value ) {
117+ return value;
118+ } catch {
119+ return defaultValue;
120+ }
121+ }
122+
123+ function readOptionalJsonString (string memory json , string memory key , string memory defaultValue ) internal returns (string memory ) {
124+ try vm.parseJsonString (json, key) returns (string memory value ) {
125+ return value;
126+ } catch {
127+ return defaultValue;
128+ }
129+ }
130+
131+ function readChainwebConfig (string memory environment ) internal returns (ChainwebConfig memory ) {
132+ string memory root = vm.projectRoot ();
133+ string memory path = string (abi.encodePacked (root, "/chainweb.config.json " ));
134+ string memory json = vm.readFile (path);
135+
136+ string memory envPath = string (abi.encodePacked (". " , environment));
137+
138+ // Parse all fields from config
139+ uint256 numberOfChains = readOptionalJsonUint (json, string (abi.encodePacked (envPath, ".numberOfChains " )), 1 );
140+ uint256 chainIdOffset = readOptionalJsonUint (json, string (abi.encodePacked (envPath, ".chainIdOffset " )), 31337 );
141+ uint256 chainwebChainIdOffset = readOptionalJsonUint (json, string (abi.encodePacked (envPath, ".chainwebChainIdOffset " )), 0 );
142+ string memory extenalHostUrl = readOptionalJsonString (json, string (abi.encodePacked (envPath, ".extenalHostUrl " )), "" );
143+
144+ return ChainwebConfig ({
145+ numberOfChains: numberOfChains,
146+ chainIdOffset: chainIdOffset,
147+ chainwebChainIdOffset: chainwebChainIdOffset,
148+ extenalHostUrl: extenalHostUrl
149+ });
150+ }
151+ }
152+
153+ contract ChainwebTest is Test , ChainwebConfigReader {
114154 Chainweb public chainweb;
115155
116- constructor (uint24 chainNumbers , uint24 chainwebChainIdOffset ) {
117- // TODO: READ chainweb.json and initialize Chainweb
156+ constructor (string memory environment ) {
157+ ChainwebConfig memory config = readChainwebConfig (environment);
118158 chainweb = new Chainweb (
119- chainNumbers ,
159+ uint24 (config.numberOfChains) ,
120160 block .chainid ,
121- chainwebChainIdOffset,
122- ""
161+ uint24 (config. chainwebChainIdOffset) ,
162+ config.extenalHostUrl
123163 );
124164 chainweb.setupChainsForTest ();
125165 }
126166}
127167
128- contract ChainwebScript is Script {
168+ contract ChainwebScript is Script , ChainwebConfigReader {
129169 Chainweb public chainweb;
130170
131- constructor (uint24 chainNumbers , uint24 chainwebChainIdOffset ) {
171+ constructor (string memory environment ) {
172+ ChainwebConfig memory config = readChainwebConfig (environment);
173+
132174 string memory nodeUrl = vm.envExists ("CHAINWEB_HOST " )
133175 ? vm.envString ("CHAINWEB_HOST " )
134- : "" ;
176+ : config.extenalHostUrl ;
135177 console.log ("Using node URL: " , nodeUrl);
178+
136179 chainweb = new Chainweb (
137- chainNumbers ,
180+ uint24 (config.numberOfChains) ,
138181 block .chainid ,
139- chainwebChainIdOffset,
182+ uint24 (config. chainwebChainIdOffset) ,
140183 nodeUrl
141184 );
142185 chainweb.setupChainsForScript ();
0 commit comments