|
| 1 | +import { ethers } from "ethers"; |
| 2 | +import { ERC1967Proxy__factory } from "@certusone/wormhole-sdk/lib/cjs/ethers-contracts"; |
| 3 | +import { ChainId } from "@certusone/wormhole-sdk"; |
| 4 | +import { runOnEvms, ChainInfo, getChainConfig, LoggerFn, getDependencyAddress, writeDeployedContract, getContractAddress } from "../../helpers"; |
| 5 | +import { MatchingEngineConfiguration, TokenRouterConfiguration } from "../../config/config-types"; |
| 6 | + |
| 7 | +import { MatchingEngine__factory, TokenRouter__factory } from "../../contract-bindings"; |
| 8 | +import { TokenRouter } from "../../../evm/ts/src/types"; |
| 9 | + |
| 10 | +runOnEvms("deploy-matching-engine", async (chain: ChainInfo, signer: ethers.Signer, log: LoggerFn) => { |
| 11 | + const config = await getTokenRouterConfiguration(chain); |
| 12 | + const implementation = await deployImplementation(signer, config, log); |
| 13 | + const proxy = await deployProxy(signer, config, implementation, log); |
| 14 | + |
| 15 | +}); |
| 16 | + |
| 17 | +function getTokenRouterConfiguration (chain: ChainInfo): Promise<TokenRouterConfiguration> { |
| 18 | + return getChainConfig<TokenRouterConfiguration>("token-router", chain.chainId); |
| 19 | +} |
| 20 | + |
| 21 | +async function deployImplementation (signer: ethers.Signer, config: TokenRouterConfiguration, log: LoggerFn) { |
| 22 | + const factory = new TokenRouter__factory(signer); |
| 23 | + |
| 24 | + const token = getDependencyAddress("Token", config.chainId); |
| 25 | + const wormhole = getDependencyAddress("Wormhole", config.chainId); |
| 26 | + const tokenMessenger = getDependencyAddress("TokenMessenger", config.chainId); |
| 27 | + |
| 28 | + // TODO: ensure that this is a 32-byte address? |
| 29 | + const matchingEngineAddress = await getContractAddress( |
| 30 | + "MatchingEngineProxies", |
| 31 | + Number(config.matchingEngineChain) as ChainId |
| 32 | + ); |
| 33 | + |
| 34 | + const deployment = await factory.deploy( |
| 35 | + token, |
| 36 | + wormhole, |
| 37 | + tokenMessenger, |
| 38 | + config.matchingEngineChain, |
| 39 | + matchingEngineAddress, |
| 40 | + config.matchingEngineMintRecipient, |
| 41 | + config.matchingEngineDomain, |
| 42 | + {} // overrides |
| 43 | + ); |
| 44 | + |
| 45 | + await deployment.deployed(); |
| 46 | + |
| 47 | + log(`TokenRouter deployed at ${deployment.address}`); |
| 48 | + |
| 49 | + writeDeployedContract(config.chainId, "TokenRouterImplementations", deployment.address); |
| 50 | + |
| 51 | + return deployment; |
| 52 | +} |
| 53 | + |
| 54 | +async function deployProxy (signer: ethers.Signer, config: TokenRouterConfiguration, implementation: ethers.Contract, log: LoggerFn) { |
| 55 | + const factory = new ERC1967Proxy__factory(signer); |
| 56 | + |
| 57 | + const abi = ["function initialize(address)"]; |
| 58 | + const iface = new ethers.utils.Interface(abi); |
| 59 | + const encodedCall = iface.encodeFunctionData("initialize", [config.ownerAssistant]); |
| 60 | + |
| 61 | + const deployment = await factory.deploy( |
| 62 | + implementation.address, |
| 63 | + encodedCall, |
| 64 | + ); |
| 65 | + |
| 66 | + await deployment.deployed(); |
| 67 | + |
| 68 | + log(`TokenRouterProxy deployed at ${deployment.address}`); |
| 69 | + |
| 70 | + writeDeployedContract(config.chainId, "TokenRouterProxies", deployment.address); |
| 71 | + |
| 72 | + return deployment; |
| 73 | +} |
0 commit comments