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

Commit 7e849dc

Browse files
committed
suppurt chainweb host
1 parent e157f41 commit 7e849dc

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,16 @@ contract CounterScript is ChainwebScript(5, 20) {
8585
Counter public counter;
8686
8787
function run() public {
88-
chainweb.switchChain(20);
89-
console.log("Running script on chain:", block.chainid);
90-
vm.startBroadcast();
91-
Counter counter1 = new Counter();
92-
counter1.setNumber(1);
93-
vm.stopBroadcast();
94-
95-
chainweb.switchChain(21);
96-
console.log("Running script on chain 2:", block.chainid);
97-
vm.startBroadcast();
98-
Counter counter2 = new Counter();
99-
counter2.setNumber(2);
100-
vm.stopBroadcast();
88+
uint256[] memory chainIds = chainweb.getChainIds();
89+
// loop over chains and deploy the contract
90+
for (uint256 i = 0; i < chainIds.length; i++) {
91+
chainweb.switchChain(chainIds[i]);
92+
console.log("Running script on chain:", block.chainid);
93+
vm.startBroadcast();
94+
Counter counter1 = new Counter();
95+
counter1.setNumber(1);
96+
vm.stopBroadcast();
97+
}
10198
}
10299
}
103100
```
@@ -110,12 +107,22 @@ forge script --multi src/example/script/Counter.s.sol:CounterScript \
110107
--broadcast
111108
```
112109

110+
run script against testnet
111+
112+
```bash
113+
CHAINWEB_HOST=https://evm-testnet.chainweb.com/chainweb/0.0/evm-testnet forge script --multi src/example/script/Counter.s.sol:CounterScript \
114+
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
115+
--broadcast
116+
113117
### ChainID
114118

115119
Since Chainweb is a network of multiple chains, the `chainid` in tests and scripts is computed as:
116120

117121
```
122+
118123
chainid = chain_id + index
124+
119125
```
120126
121127
You can configure the `chain_id` in your `foundry.toml` file. Each test or script chain will increment from this base value. The default value is `31337` in Foundry.
128+
```

src/Chainweb.sol

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

102107
struct 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
}

src/example/script/Counter.s.sol

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@ 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(2, 0) {
8+
contract CounterScript is ChainwebScript(5, 20) {
99
Counter public counter;
1010

1111
function run() public {
12-
chainweb.switchChain(0);
13-
console.log("Running script on chain:", block.chainid);
14-
vm.startBroadcast();
15-
Counter counter1 = new Counter();
16-
counter1.setNumber(1);
17-
vm.stopBroadcast();
18-
19-
chainweb.switchChain(1);
20-
console.log("Running script on chain 2:", block.chainid);
21-
vm.startBroadcast();
22-
Counter counter2 = new Counter();
23-
counter2.setNumber(2);
24-
vm.stopBroadcast();
12+
uint256[] memory chainIds = chainweb.getChainIds();
13+
for (uint256 i = 0; i < chainIds.length; i++) {
14+
chainweb.switchChain(chainIds[i]);
15+
console.log("Running script on chain:", block.chainid);
16+
vm.startBroadcast();
17+
Counter counter1 = new Counter();
18+
counter1.setNumber(1);
19+
vm.stopBroadcast();
20+
}
2521
}
2622
}

0 commit comments

Comments
 (0)