Skip to content

Commit fb539fa

Browse files
Add ChainNameResolver (#131)
1 parent a2a0c02 commit fb539fa

10 files changed

Lines changed: 56 additions & 10 deletions

ccip/cct/foundry/script/AcceptAdminRole.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {TokenAdminRegistry} from "@chainlink/contracts-ccip/contracts/tokenAdminRegistry/TokenAdminRegistry.sol";
89

910
contract AcceptAdminRole is Script {
1011
function run() external {
12+
ChainNameResolver resolver = new ChainNameResolver();
1113
// Get the chain name based on the current chain ID
12-
string memory chainName = getChain(block.chainid).chainAlias;
14+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1315

1416
// Construct the path to the deployed token JSON file
1517
string memory root = vm.projectRoot();

ccip/cct/foundry/script/ApplyChainUpdates.s.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {TokenPool} from "@chainlink/contracts-ccip/contracts/pools/TokenPool.sol";
89
import {RateLimiter} from "@chainlink/contracts-ccip/contracts/libraries/RateLimiter.sol";
910

1011
contract ApplyChainUpdates is Script {
1112
function run() external {
13+
ChainNameResolver resolver = new ChainNameResolver();
1214
// Get the current chain name based on the chain ID
13-
string memory chainName = getChain(block.chainid).chainAlias;
15+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1416

1517
// Construct paths to the configuration and local pool JSON files
1618
string memory root = vm.projectRoot();
@@ -23,7 +25,7 @@ contract ApplyChainUpdates is Script {
2325
);
2426

2527
// Get the remote chain name based on the remoteChainId
26-
string memory remoteChainName = getChain(remoteChainId).chainAlias;
28+
string memory remoteChainName = resolver.getChainNameSafe(remoteChainId);
2729
string memory remotePoolPath =
2830
string.concat(root, "/script/output/deployedTokenPool_", remoteChainName, ".json");
2931
string memory remoteTokenPath = string.concat(root, "/script/output/deployedToken_", remoteChainName, ".json");

ccip/cct/foundry/script/ClaimAdmin.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {
89
RegistryModuleOwnerCustom
910
} from "@chainlink/contracts-ccip/contracts/tokenAdminRegistry/RegistryModuleOwnerCustom.sol";
1011
import {BurnMintERC20} from "@chainlink/contracts/src/v0.8/shared/token/ERC20/BurnMintERC20.sol";
1112

1213
contract ClaimAdmin is Script {
1314
function run() external {
15+
ChainNameResolver resolver = new ChainNameResolver();
1416
// Get the chain name based on the current chain ID
15-
string memory chainName = getChain(block.chainid).chainAlias;
17+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1618

1719
// Define paths to the necessary JSON files
1820
string memory root = vm.projectRoot();

ccip/cct/foundry/script/DeployBurnMintTokenPool.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {BurnMintTokenPool} from "@chainlink/contracts-ccip/contracts/pools/BurnMintTokenPool.sol";
89
import {BurnMintERC20} from "@chainlink/contracts/src/v0.8/shared/token/ERC20/BurnMintERC20.sol";
910
import {IBurnMintERC20} from "@chainlink/contracts/src/v0.8/shared/token/ERC20/IBurnMintERC20.sol";
1011

1112
contract DeployBurnMintTokenPool is Script {
1213
function run() external {
14+
ChainNameResolver resolver = new ChainNameResolver();
1315
// Get the chain name based on the current chain ID
14-
string memory chainName = getChain(block.chainid).chainAlias;
16+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1517

1618
// Construct the path to the deployed token JSON file
1719
string memory root = vm.projectRoot();

ccip/cct/foundry/script/DeployLockReleaseTokenPool.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {LockReleaseTokenPool} from "@chainlink/contracts-ccip/contracts/pools/LockReleaseTokenPool.sol";
89
import {IERC20} from "@openzeppelin/contracts@4.8.3/token/ERC20/IERC20.sol";
910

1011
contract DeployLockReleaseTokenPool is Script {
1112
function run() external {
13+
ChainNameResolver resolver = new ChainNameResolver();
1214
// Get the chain name based on the current chain ID
13-
string memory chainName = getChain(block.chainid).chainAlias;
15+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1416

1517
// Construct the path to the deployed token JSON file
1618
string memory root = vm.projectRoot();

ccip/cct/foundry/script/DeployToken.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ pragma solidity 0.8.24;
33

44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
6+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
67
import {BurnMintERC20} from "@chainlink/contracts/src/v0.8/shared/token/ERC20/BurnMintERC20.sol";
78

89
contract DeployToken is Script {
910
function run() external {
11+
ChainNameResolver resolver = new ChainNameResolver();
1012
// Get the chain name based on the current chain ID
11-
string memory chainName = getChain(block.chainid).chainAlias;
13+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1214

1315
// Define the path to the config.json file
1416
string memory root = vm.projectRoot();

ccip/cct/foundry/script/MintTokens.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ pragma solidity 0.8.24;
33

44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
6+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
67
import {BurnMintERC20} from "@chainlink/contracts/src/v0.8/shared/token/ERC20/BurnMintERC20.sol";
78

89
contract MintTokens is Script {
910
function run() external {
11+
ChainNameResolver resolver = new ChainNameResolver();
1012
// Get the current chain name based on the chain ID
11-
string memory chainName = getChain(block.chainid).chainAlias;
13+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1214

1315
// Construct paths to the configuration and token JSON files
1416
string memory root = vm.projectRoot();

ccip/cct/foundry/script/SetPool.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {TokenAdminRegistry} from "@chainlink/contracts-ccip/contracts/tokenAdminRegistry/TokenAdminRegistry.sol";
89

910
// Script contract to set the token pool in the TokenAdminRegistry
1011
contract SetPool is Script {
1112
function run() external {
13+
ChainNameResolver resolver = new ChainNameResolver();
1214
// Get the chain name based on the current chain ID
13-
string memory chainName = getChain(block.chainid).chainAlias;
15+
string memory chainName = resolver.getChainNameSafe(block.chainid);
1416

1517
// Construct paths to the JSON files containing deployed token and pool addresses
1618
string memory root = vm.projectRoot();

ccip/cct/foundry/script/TransferTokens.s.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity 0.8.24;
44
import {Script, console} from "forge-std/Script.sol";
55
import {HelperUtils} from "./utils/HelperUtils.s.sol"; // Utility functions for JSON parsing and chain info
66
import {HelperConfig} from "./HelperConfig.s.sol"; // Network configuration helper
7+
import {ChainNameResolver} from "./utils/ChainNameResolver.s.sol"; // Chain name resolution utility
78
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
89
import {IRouterClient} from "@chainlink/contracts-ccip/contracts/interfaces/IRouterClient.sol";
910
import {Client} from "@chainlink/contracts-ccip/contracts/libraries/Client.sol";
@@ -15,8 +16,9 @@ contract TransferTokens is Script {
1516
}
1617

1718
function run() external {
19+
ChainNameResolver resolver = new ChainNameResolver();
1820
// Get the chain name based on the current chain ID
19-
string memory chainName = getChain(block.chainid).chainAlias;
21+
string memory chainName = resolver.getChainNameSafe(block.chainid);
2022

2123
// Construct paths to the configuration and token JSON files
2224
string memory root = vm.projectRoot();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.24;
3+
4+
import {Script} from "forge-std/Script.sol";
5+
6+
/**
7+
* @title ChainNameResolver
8+
* @notice Provides safe access to StdChains functionality without constructor dependencies
9+
* @dev This contract acts as a resolver,
10+
* and can be used independently to get chain information for any chain,
11+
* with graceful fallback.
12+
*/
13+
contract ChainNameResolver is Script {
14+
/// @dev External function to call getChain (needed for try/catch)
15+
function getChainAlias(uint256 chainId) external returns (string memory) {
16+
return getChain(chainId).chainAlias;
17+
}
18+
19+
/// @dev Safely get chain alias with fallback to "custom_network" if not found in StdChains
20+
function getChainNameSafe(uint256 chainId) public returns (string memory) {
21+
try this.getChainAlias(chainId) returns (string memory chainAlias) {
22+
return chainAlias;
23+
} catch {
24+
// Fallback to "custom_network" for chains not in StdChains
25+
return "custom_network";
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)