Skip to content
This repository was archived by the owner on Jan 9, 2026. It is now read-only.

Commit 82e1faf

Browse files
committed
Reading chainweb configuration from json
1 parent ae162e2 commit 82e1faf

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

chainweb.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"anvil": {
3-
"chains": 5,
3+
"numberOfChains": 5,
44
"chainwebChainIdOffset": 20,
55
"chainIdOffset": 62600
66
},
77
"sandbox": {
8-
"chains": 5,
8+
"numberOfChains": 5,
99
"chainwebChainIdOffset": 20,
1010
"chainIdOffset": 1789,
1111
"extenalHostUrl": "http://localhost:1848/chainweb/0.0/evm-development"

foundry.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ test = "src/example/test"
55
out = "src/example/out"
66
broadcast = "src/example/broadcast"
77
libs = ["lib"]
8+
fs_permissions = [{ access = "read", path = "./chainweb.config.json" }]
89

910
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

src/Chainweb.sol

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/example/script/Counter.s.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Script, console} from "forge-std/Script.sol";
55
import {Counter} from "../src/Counter.sol";
66
import {ChainwebScript} from "kadena-io/foundry-chainweb/Chainweb.sol";
77

8-
contract CounterScript is ChainwebScript(5, 20) {
8+
contract CounterScript is ChainwebScript("anvil") {
99
Counter public counter;
1010

1111
function run() public {

src/example/test/Counter.t.sol

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ import {Test, console} from "forge-std/Test.sol";
55
import {Counter} from "../src/Counter.sol";
66
import {ChainwebTest, ChainwebConfig} from "kadena-io/foundry-chainweb/Chainweb.sol";
77

8-
contract CounterTest is ChainwebTest(4, 0) {
8+
contract CounterTest is ChainwebTest("anvil") {
99
function test_MultiChains() public {
10-
chainweb.switchChain(0);
10+
uint256[] memory chainIds = chainweb.getChainIds();
11+
console.log("Available chain IDs:", chainIds.length);
12+
13+
chainweb.switchChain(chainIds[0]);
1114
Counter counter0 = new Counter();
1215
console.log("Running test on chain:", block.chainid);
1316
counter0.setNumber(10);
1417

15-
chainweb.switchChain(1);
18+
chainweb.switchChain(chainIds[1]);
1619
Counter counter1 = new Counter();
1720
counter1.setNumber(11);
1821

19-
chainweb.switchChain(0);
22+
chainweb.switchChain(chainIds[0]);
2023
counter0.increment();
21-
assertEq(block.chainid, 31337);
2224
assertEq(counter0.number(), 11);
2325

24-
chainweb.switchChain(1);
26+
chainweb.switchChain(chainIds[1]);
2527
counter1.increment();
26-
assertEq(block.chainid, 31338);
2728
assertEq(counter1.number(), 12);
2829
}
2930
}

0 commit comments

Comments
 (0)