|
| 1 | +import { toCapsSnakeCase } from "./utils"; |
| 2 | +import { |
| 3 | + platformToChains, |
| 4 | + contracts, |
| 5 | + rpc, |
| 6 | + circle, |
| 7 | +} from "@wormhole-foundation/sdk-base"; |
| 8 | +import { EvmAddress } from "@wormhole-foundation/sdk-evm"; |
| 9 | + |
| 10 | +const {coreBridge, tokenBridge, relayer, circleContracts} = contracts; |
| 11 | +const {usdcContract, circleChainId} = circle; |
| 12 | + |
| 13 | +console.log( |
| 14 | +`// SPDX-License-Identifier: Apache 2 |
| 15 | +pragma solidity ^0.8.0; |
| 16 | +
|
| 17 | +// ╭──────────────────────────────────────╮ |
| 18 | +// │ Auto-generated by gen/chainConsts.ts │ |
| 19 | +// ╰──────────────────────────────────────╯ |
| 20 | +
|
| 21 | +// This file contains 2 libraries and an associated set of free standing functions: |
| 22 | +// |
| 23 | +// The libraries: |
| 24 | +// 1. MainnetChainConstants |
| 25 | +// 2. TestnetChainConstants |
| 26 | +// |
| 27 | +// Both provide the same set of functions, which are also provided as free standing functions |
| 28 | +// with an additional network parameter, though library functions use an underscore prefix to |
| 29 | +// avoid a declaration shadowing bug that has been first reported in the solc Github in 2020... |
| 30 | +// see https://github.com/ethereum/solidity/issues/10155 |
| 31 | +// |
| 32 | +// Function │ Parameter │ Returns |
| 33 | +// ────────────────────────┼───────────┼──────────── |
| 34 | +// chainName │ chainId │ string |
| 35 | +// defaultRPC │ chainId │ string |
| 36 | +// coreBridge │ chainId │ address |
| 37 | +// tokenBridge │ chainId │ address |
| 38 | +// wormholeRelayer │ chainId │ address |
| 39 | +// cctpDomain │ chainId │ cctpDomain |
| 40 | +// usdc │ chainId │ address |
| 41 | +// cctpMessageTransmitter │ chainId │ address |
| 42 | +// cctpTokenMessenger │ chainId │ address |
| 43 | +// |
| 44 | +// Empty fields return invalid values (empty string, address(0), INVALID_CCTP_DOMAIN) |
| 45 | +
|
| 46 | +import "wormhole-sdk/constants/Chains.sol"; |
| 47 | +import "wormhole-sdk/constants/CctpDomains.sol"; |
| 48 | +
|
| 49 | +uint32 constant INVALID_CCTP_DOMAIN = type(uint32).max; |
| 50 | +error UnsupportedChainId(uint16 chainId); |
| 51 | +error UnsupportedCctpDomain(uint32 cctpDomain); |
| 52 | +` |
| 53 | +); |
| 54 | + |
| 55 | +const evmChains = platformToChains("Evm"); |
| 56 | +type EvmChain = typeof evmChains[number]; |
| 57 | +const networks = ["Mainnet", "Testnet"] as const; |
| 58 | + |
| 59 | +const networkChains = { |
| 60 | + Mainnet: evmChains.filter(chain => |
| 61 | + contracts.coreBridge.has("Mainnet", chain)), |
| 62 | + //remove testnets that were superseded by Sepolia testnets |
| 63 | + Testnet: evmChains.filter(chain => |
| 64 | + contracts.coreBridge.has("Testnet", chain) && |
| 65 | + chain !== "Ethereum" && |
| 66 | + !evmChains.includes(chain + "Sepolia" as EvmChain) |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +enum AliasType { |
| 71 | + String = "string", |
| 72 | + Address = "address", |
| 73 | + CctpDomain = "uint32", |
| 74 | +} |
| 75 | + |
| 76 | +const emptyValue = { |
| 77 | + [AliasType.String ]: "", |
| 78 | + [AliasType.Address ]: "address(0)", |
| 79 | + [AliasType.CctpDomain]: "INVALID_CCTP_DOMAIN", |
| 80 | +} |
| 81 | + |
| 82 | +const returnParam = (aliasType: AliasType) => |
| 83 | + aliasType === AliasType.String ? "string memory" : aliasType; |
| 84 | + |
| 85 | +const toReturnValue = (value: string, returnType: AliasType) => { |
| 86 | + if (returnType === AliasType.Address && value) |
| 87 | + return new EvmAddress(value).toString(); //ensures checksum format |
| 88 | + |
| 89 | + const ret = value || emptyValue[returnType]; |
| 90 | + return returnType === AliasType.String |
| 91 | + ? `\"${ret}\"` |
| 92 | + : ret; |
| 93 | +} |
| 94 | + |
| 95 | +const functions = [ |
| 96 | + ["chainName", AliasType.String ], |
| 97 | + ["defaultRPC", AliasType.String ], |
| 98 | + ["coreBridge", AliasType.Address ], |
| 99 | + ["tokenBridge", AliasType.Address ], |
| 100 | + ["wormholeRelayer", AliasType.Address ], |
| 101 | + ["cctpDomain", AliasType.CctpDomain], |
| 102 | + ["usdc", AliasType.Address ], |
| 103 | + ["cctpMessageTransmitter", AliasType.Address ], |
| 104 | + ["cctpTokenMessenger", AliasType.Address ], |
| 105 | +] as const; |
| 106 | + |
| 107 | +const indent = (lines: string[]) => lines.map(line => ` ${line}`); |
| 108 | + |
| 109 | +console.log( |
| 110 | + functions.map(([name, returnType]) => [ |
| 111 | + `function ${name}(bool mainnet, uint16 chainId)` + |
| 112 | + ` pure returns (${returnParam(returnType)}) {`, |
| 113 | + ...indent([ |
| 114 | + `return mainnet`, |
| 115 | + ` ? MainnetChainConstants._${name}(chainId)`, |
| 116 | + ` : TestnetChainConstants._${name}(chainId);`, |
| 117 | + ]), |
| 118 | + `}` |
| 119 | + ].join("\n") |
| 120 | + ).join("\n\n") |
| 121 | +); |
| 122 | + |
| 123 | +for (const network of networks) { |
| 124 | + const functionDefinition = ( |
| 125 | + name: string, |
| 126 | + returnType: AliasType, |
| 127 | + mapping: (chain: EvmChain) => string, |
| 128 | + ) => indent([ |
| 129 | + `function _${name}(uint16 chainId) internal pure returns (${returnParam(returnType)}) {`, |
| 130 | + ...indent([ |
| 131 | + ...networkChains[network].map(chain => [ |
| 132 | + `if (chainId == CHAIN_ID_${toCapsSnakeCase(chain)})`, |
| 133 | + ` return ${toReturnValue(mapping(chain), returnType)};` |
| 134 | + ]).flat(), |
| 135 | + `revert UnsupportedChainId(chainId);` |
| 136 | + ]), |
| 137 | + `}` |
| 138 | + ]).join("\n"); |
| 139 | + |
| 140 | + const cctpDomainConst = (chain: EvmChain) => |
| 141 | + circleChainId.has(network, chain) |
| 142 | + ? `CCTP_DOMAIN_${toCapsSnakeCase(chain)}` |
| 143 | + : "INVALID_CCTP_DOMAIN"; |
| 144 | + |
| 145 | + const mappings = { |
| 146 | + chainName: chain => chain, |
| 147 | + defaultRPC: chain => rpc.rpcAddress(network, chain), |
| 148 | + coreBridge: chain => coreBridge.get(network, chain), |
| 149 | + tokenBridge: chain => tokenBridge.get(network, chain), |
| 150 | + wormholeRelayer: chain => relayer.get(network, chain), |
| 151 | + cctpDomain: chain => cctpDomainConst(chain), |
| 152 | + usdc: chain => usdcContract.get(network, chain), |
| 153 | + cctpMessageTransmitter: chain => circleContracts.get(network, chain)?.messageTransmitter, |
| 154 | + cctpTokenMessenger: chain => circleContracts.get(network, chain)?.tokenMessenger, |
| 155 | + }; |
| 156 | + |
| 157 | + console.log([ |
| 158 | + ``, |
| 159 | + `library ${network}ChainConstants {`, |
| 160 | + functions |
| 161 | + .map(([name, returnType]) => functionDefinition(name, returnType, mappings[name])) |
| 162 | + .join("\n\n"), |
| 163 | + `}` |
| 164 | + ].join("\n") |
| 165 | + ); |
| 166 | +} |
0 commit comments