-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplyChain.s.sol
95 lines (76 loc) · 4.88 KB
/
ApplyChain.s.sol
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24 <0.9.0;
import {Script, console} from "lib/forge-std/src/Script.sol";
import {HelperUtils} from "../utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
import {HelperConfig} from "../utils/HelperConfig.s.sol"; // Network configuration helper
import {TokenPool} from "lib/ccip/contracts/src/v0.8/ccip/pools/TokenPool.sol";
import {RateLimiter} from "lib/ccip/contracts/src/v0.8/ccip/libraries/RateLimiter.sol";
contract ApplyChain is Script {
function run() external {
// Get the current chain name based on the chain ID
string memory chainName = HelperUtils.getChainName(block.chainid);
// Construct paths to the configuration and local pool JSON files
string memory root = vm.projectRoot();
string memory configPath = string.concat(root, "/script/config.json");
string memory localPoolPath = string.concat(root, "/script/output/deployedTokenPool_", chainName, ".json");
// Read the remoteChainId from config.json based on the current chain ID
uint256 remoteChainId = HelperUtils.getUintFromJson(
vm, configPath, string.concat(".remoteChains.", HelperUtils.uintToStr(block.chainid))
);
// Get the remote chain name based on the remoteChainId
string memory remoteChainName = HelperUtils.getChainName(remoteChainId);
string memory remotePoolPath =
string.concat(root, "/script/output/deployedTokenPool_", remoteChainName, ".json");
string memory remoteTokenPath = string.concat(root, "/script/output/deployedToken_", remoteChainName, ".json");
// Extract addresses from the JSON files
address poolAddress =
HelperUtils.getAddressFromJson(vm, localPoolPath, string.concat(".deployedTokenPool_", chainName));
address remotePoolAddress =
HelperUtils.getAddressFromJson(vm, remotePoolPath, string.concat(".deployedTokenPool_", remoteChainName));
address remoteTokenAddress =
HelperUtils.getAddressFromJson(vm, remoteTokenPath, string.concat(".deployedToken_", remoteChainName));
// For remotePoolAddresses, create an array with the remotePoolAddress
address[] memory remotePoolAddresses = new address[](1);
remotePoolAddresses[0] = remotePoolAddress;
// Fetch the remote network configuration to get the chain selector
HelperConfig helperConfig = new HelperConfig();
HelperConfig.NetworkConfig memory remoteNetworkConfig =
HelperUtils.getNetworkConfig(helperConfig, remoteChainId);
uint64 remoteChainSelector = remoteNetworkConfig.chainSelector;
require(poolAddress != address(0), "Invalid pool address");
require(remotePoolAddress != address(0), "Invalid remote pool address");
require(remoteTokenAddress != address(0), "Invalid remote token address");
require(remoteChainSelector != 0, "chainSelector is not defined for the remote chain");
vm.startBroadcast();
// Instantiate the local TokenPool contract
TokenPool poolContract = TokenPool(poolAddress);
// Prepare chain update data for configuring cross-chain transfers
TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
// Encode remote pool addresses
bytes[] memory remotePoolAddressesEncoded = new bytes[](remotePoolAddresses.length);
for (uint256 i = 0; i < remotePoolAddresses.length; i++) {
remotePoolAddressesEncoded[i] = abi.encode(remotePoolAddresses[i]);
}
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: remoteChainSelector, // Chain selector of the remote chain
remotePoolAddresses: remotePoolAddressesEncoded, // Array of encoded addresses of the remote pools
remoteTokenAddress: abi.encode(remoteTokenAddress), // Encoded address of the remote token
outboundRateLimiterConfig: RateLimiter.Config({
isEnabled: false, // Set to true to enable outbound rate limiting
capacity: 0, // Max tokens allowed in the outbound rate limiter
rate: 0 // Refill rate per second for the outbound rate limiter
}),
inboundRateLimiterConfig: RateLimiter.Config({
isEnabled: false, // Set to true to enable inbound rate limiting
capacity: 0, // Max tokens allowed in the inbound rate limiter
rate: 0 // Refill rate per second for the inbound rate limiter
})
});
// Create an empty array for chainSelectorRemovals
uint64[] memory chainSelectorRemovals = new uint64[](0);
// Apply the chain updates to configure the pool
poolContract.applyChainUpdates(chainSelectorRemovals, chainUpdates);
console.log("Chain update applied to pool at address:", poolAddress);
vm.stopBroadcast();
}
}