-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path01b_ConfigureByChainName.s.sol
More file actions
296 lines (244 loc) · 11.7 KB
/
Copy path01b_ConfigureByChainName.s.sol
File metadata and controls
296 lines (244 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/console.sol";
import {LZAddressContext} from "../../src/helpers/LZAddressContext.sol";
import {ILayerZeroEndpointV2} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
import {SetConfigParam} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/IMessageLibManager.sol";
import {UlnConfig} from "@layerzerolabs/lz-evm-messagelib-v2/contracts/uln/UlnBase.sol";
import {ExecutorConfig} from "@layerzerolabs/lz-evm-messagelib-v2/contracts/SendLibBase.sol";
import {IOAppCore} from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppCore.sol";
import {IOAppOptionsType3, EnforcedOptionParam} from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppOptionsType3.sol";
import {OptionsBuilder} from "@layerzerolabs/oapp-evm/contracts/oapp/libs/OptionsBuilder.sol";
/// @title ConfigureByChainName
/// @notice Configure an OApp across multiple chains using human-readable chain names
/// @dev Run this script once per chain - it auto-detects via block.chainid:
///
/// forge script ConfigureByChainName --rpc-url ethereum --broadcast --account deployer
/// forge script ConfigureByChainName --rpc-url arbitrum --broadcast --account deployer
/// forge script ConfigureByChainName --rpc-url base --broadcast --account deployer
/// forge script ConfigureByChainName --rpc-url optimism --broadcast --account deployer
///
/// This variant uses chain names (e.g., "arbitrum-mainnet") instead of chain IDs.
/// See ConfigureByChainId.s.sol for the chain ID variant.
/// See ConfigureByEid.s.sol for the LayerZero EID variant.
contract ConfigureByChainName is Script {
using OptionsBuilder for bytes;
/*//////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////*/
/// @notice Configuration for a single chain
/// @param chainName Human-readable chain name (e.g., "arbitrum-mainnet", "base-mainnet")
/// @param oapp OApp address deployed on this chain
/// @param confirmations Block confirmations required when this chain is destination
/// @param sendOptions Enforced options for MSG_TYPE_SEND (1) - gas for lzReceive
/// @param sendAndCallOptions Enforced options for MSG_TYPE_SEND_AND_CALL (2) - empty if not using compose
struct ChainConfig {
string chainName;
address oapp;
uint64 confirmations;
bytes sendOptions;
bytes sendAndCallOptions;
}
/// @dev Cached addresses for the local chain
struct LocalContext {
address oapp;
address endpoint;
address sendLib;
address receiveLib;
address executor;
address[] dvns;
uint64 confirmations;
}
/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
uint8 internal constant REQUIRED_DVN_COUNT = 2;
uint32 internal constant MAX_MESSAGE_SIZE = 10000;
uint16 internal constant MSG_TYPE_SEND = 1;
uint16 internal constant MSG_TYPE_SEND_AND_CALL = 2;
string internal constant DVN_1 = "LayerZero Labs";
string internal constant DVN_2 = "Nethermind";
/*//////////////////////////////////////////////////////////////
ENTRY POINT
//////////////////////////////////////////////////////////////*/
function run() external {
// ============================================================
// STEP 1: Define all chain configurations (MODIFY FOR YOUR DEPLOYMENT)
// ============================================================
ChainConfig[] memory chains = new ChainConfig[](4);
// Ethereum Mainnet
chains[0] = ChainConfig({
chainName: "ethereum-mainnet",
oapp: 0x1111111111111111111111111111111111111111,
confirmations: 15, // Ethereum - slower finality
sendOptions: OptionsBuilder.newOptions().addExecutorLzReceiveOption(80_000, 0),
sendAndCallOptions: "" // No compose
});
// Arbitrum
chains[1] = ChainConfig({
chainName: "arbitrum-mainnet",
oapp: 0x2222222222222222222222222222222222222222,
confirmations: 1, // L2 - fast finality
sendOptions: OptionsBuilder.newOptions().addExecutorLzReceiveOption(80_000, 0),
sendAndCallOptions: ""
});
// Base
chains[2] = ChainConfig({
chainName: "base-mainnet",
oapp: 0x3333333333333333333333333333333333333333,
confirmations: 1, // L2 - fast finality
sendOptions: OptionsBuilder.newOptions().addExecutorLzReceiveOption(80_000, 0),
sendAndCallOptions: ""
});
// Optimism
chains[3] = ChainConfig({
chainName: "optimism-mainnet",
oapp: 0x4444444444444444444444444444444444444444,
confirmations: 1, // L2 - fast finality
sendOptions: OptionsBuilder.newOptions().addExecutorLzReceiveOption(80_000, 0),
sendAndCallOptions: ""
});
// ============================================================
// STEP 2: Find current chain and setup context
// ============================================================
(uint256 localIndex, LocalContext memory local, uint32[] memory eids) = _setup(chains);
if (local.endpoint == address(0)) {
console.log("Chain", block.chainid, "not in config. Skipping.");
return;
}
console.log("=== Configuring chain", block.chainid, "===");
console.log("OApp:", local.oapp);
console.log("");
// ============================================================
// STEP 3: Configure pathways to all remote chains
// ============================================================
vm.startBroadcast();
for (uint256 i = 0; i < chains.length; i++) {
if (i == localIndex) continue;
_configurePathway(local, eids[i], chains[i]);
console.log("Configured pathway to EID", eids[i]);
}
vm.stopBroadcast();
console.log("");
console.log("=== Done ===");
}
/*//////////////////////////////////////////////////////////////
SETUP
//////////////////////////////////////////////////////////////*/
function _setup(ChainConfig[] memory chains)
internal
returns (uint256 localIndex, LocalContext memory local, uint32[] memory eids)
{
LZAddressContext ctx = new LZAddressContext();
// Resolve all EIDs and find current chain by matching block.chainid
eids = new uint32[](chains.length);
localIndex = type(uint256).max;
for (uint256 i = 0; i < chains.length; i++) {
ctx.setChain(chains[i].chainName);
eids[i] = ctx.getCurrentEID();
// Match by chain ID derived from chain name
if (ctx.getCurrentChainId() == block.chainid) {
localIndex = i;
}
}
if (localIndex == type(uint256).max) {
return (0, local, eids);
}
// Setup context for local chain
ctx.setChain(chains[localIndex].chainName);
local = LocalContext({
oapp: chains[localIndex].oapp,
endpoint: ctx.getEndpointV2(),
sendLib: ctx.getSendUln302(),
receiveLib: ctx.getReceiveUln302(),
executor: ctx.getExecutor(),
dvns: ctx.getSortedDVNs(_getDvnNames()),
confirmations: chains[localIndex].confirmations
});
}
/*//////////////////////////////////////////////////////////////
CONFIGURATION
//////////////////////////////////////////////////////////////*/
function _configurePathway(LocalContext memory local, uint32 remoteEid, ChainConfig memory remote) internal {
ILayerZeroEndpointV2 endpoint = ILayerZeroEndpointV2(local.endpoint);
// 1. Set libraries for this pathway
endpoint.setSendLibrary(local.oapp, remoteEid, local.sendLib);
endpoint.setReceiveLibrary(local.oapp, remoteEid, local.receiveLib, 0);
// 2. Set send config (local -> remote): use remote's confirmation requirement
_setSendConfig(local, remoteEid, remote.confirmations);
// 3. Set receive config (remote -> local): use local's confirmation requirement
_setReceiveConfig(local, remoteEid);
// 4. Set enforced options (gas to deliver to remote chain)
_setEnforcedOptions(local.oapp, remoteEid, remote.sendOptions, remote.sendAndCallOptions);
// 5. Set peer to open the pathway
IOAppCore(local.oapp).setPeer(remoteEid, bytes32(uint256(uint160(remote.oapp))));
}
function _setSendConfig(LocalContext memory local, uint32 remoteEid, uint64 confirmations) internal {
SetConfigParam[] memory params = new SetConfigParam[](2);
params[0] = SetConfigParam({
eid: remoteEid,
configType: 1,
config: abi.encode(ExecutorConfig({maxMessageSize: MAX_MESSAGE_SIZE, executor: local.executor}))
});
params[1] = SetConfigParam({
eid: remoteEid,
configType: 2,
config: abi.encode(
UlnConfig({
confirmations: confirmations,
requiredDVNCount: REQUIRED_DVN_COUNT,
optionalDVNCount: 0,
optionalDVNThreshold: 0,
requiredDVNs: local.dvns,
optionalDVNs: new address[](0)
})
)
});
ILayerZeroEndpointV2(local.endpoint).setConfig(local.oapp, local.sendLib, params);
}
function _setReceiveConfig(LocalContext memory local, uint32 remoteEid) internal {
SetConfigParam[] memory params = new SetConfigParam[](1);
params[0] = SetConfigParam({
eid: remoteEid,
configType: 2,
config: abi.encode(
UlnConfig({
confirmations: local.confirmations,
requiredDVNCount: REQUIRED_DVN_COUNT,
optionalDVNCount: 0,
optionalDVNThreshold: 0,
requiredDVNs: local.dvns,
optionalDVNs: new address[](0)
})
)
});
ILayerZeroEndpointV2(local.endpoint).setConfig(local.oapp, local.receiveLib, params);
}
function _setEnforcedOptions(
address oapp,
uint32 remoteEid,
bytes memory sendOptions,
bytes memory sendAndCallOptions
) internal {
uint256 count = 1;
if (sendAndCallOptions.length > 0) count = 2;
EnforcedOptionParam[] memory enforcedOptions = new EnforcedOptionParam[](count);
// MSG_TYPE_SEND (1) - standard transfer
enforcedOptions[0] = EnforcedOptionParam({eid: remoteEid, msgType: MSG_TYPE_SEND, options: sendOptions});
// MSG_TYPE_SEND_AND_CALL (2) - transfer with compose
if (sendAndCallOptions.length > 0) {
enforcedOptions[1] =
EnforcedOptionParam({eid: remoteEid, msgType: MSG_TYPE_SEND_AND_CALL, options: sendAndCallOptions});
}
IOAppOptionsType3(oapp).setEnforcedOptions(enforcedOptions);
}
/*//////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////*/
function _getDvnNames() internal pure returns (string[] memory names) {
names = new string[](2);
names[0] = DVN_1;
names[1] = DVN_2;
}
}