From 29631ba1421b365ca88fec546fedff60b1e12940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lz=2Esir=CE=94rthurmoney=28=29?= Date: Wed, 15 Nov 2023 13:41:02 -0800 Subject: [PATCH] WIP: refactoring wireAll --- packages/ua-utils/src/getConfig.ts | 2 + packages/ua-utils/src/utils/wireAllHelpers.ts | 117 ++------------ packages/ua-utils/src/wireAll.ts | 145 ++++++------------ 3 files changed, 60 insertions(+), 204 deletions(-) diff --git a/packages/ua-utils/src/getConfig.ts b/packages/ua-utils/src/getConfig.ts index 08ff79831..bd5300766 100644 --- a/packages/ua-utils/src/getConfig.ts +++ b/packages/ua-utils/src/getConfig.ts @@ -23,6 +23,7 @@ const action: ActionType = async (taskArgs, hre) => { return } + // TODO fix this: 'deployments' does not exist on type 'HardhatRuntimeEnvironment'. const ulnConfigDeployment = await hre.deployments.get("UlnConfig") const remoteNetworks = taskArgs.remoteNetworks.split(",") const configByNetwork = await Promise.all( @@ -32,6 +33,7 @@ const action: ActionType = async (taskArgs, hre) => { const remoteEid = await remoteEndpointV2.eid() const localSendLibrary = await localEndpointV2.getSendLibrary(localContractAddress, remoteEid) + // TODO fix this: 'getContractAt' does not exist on type 'typeof import("/Users/kz-layerzero/Documents/layerzero/git/lz-utils/node_modules/ethers/lib/ethers")'. const localUlnConfig = await ethers.getContractAt(ulnConfigDeployment.abi, localSendLibrary) const [ulnConfigStruct, outboundConfigStruct] = await localUlnConfig.getUlnAndOutboundConfig(localContractAddress, remoteEid) diff --git a/packages/ua-utils/src/utils/wireAllHelpers.ts b/packages/ua-utils/src/utils/wireAllHelpers.ts index 7d81ce005..55f862846 100644 --- a/packages/ua-utils/src/utils/wireAllHelpers.ts +++ b/packages/ua-utils/src/utils/wireAllHelpers.ts @@ -1,106 +1,6 @@ import { Transaction, getLayerZeroChainId, getContractInstance } from "./crossChainHelper" - -export async function setUseCustomAdapterParams( - hre: any, - localNetwork: string, - localContractNameOrAddress: string, - useCustom: boolean -): Promise { - const localContract = await getContractInstance(hre, localNetwork, localContractNameOrAddress) - const cur = await localContract.useCustomAdapterParams() - const needChange = cur !== useCustom - - // function setUseCustomAdapterParams(bool _useCustomAdapterParams) - const functionName = "setUseCustomAdapterParams" - const params = ["bool"] - const args = [useCustom] - - const tx: any = { - needChange, - chainId: getLayerZeroChainId(localNetwork), - contractName: localContractNameOrAddress, - functionName: functionName, - args: args, - calldata: localContract.interface.encodeFunctionData(functionName, args), - } - if (tx.needChange) { - tx.diff = JSON.stringify({ useCustomAdapterParams: { oldValue: cur, newValue: useCustom } }) - } - return [tx] -} - -export async function setMinDstGas( - hre: any, - localNetwork: string, - localContractNameOrAddress: string, - minDstGasConfig: any, - remoteChainId: string -): Promise { - const txns: Transaction[] = [] - const localContract = await getContractInstance(hre, localNetwork, localContractNameOrAddress) - const packetTypes = Object.keys(minDstGasConfig) - for (const packet of packetTypes) { - const packetType = parseInt(packet.at(-1) as string) - const minGas = minDstGasConfig[packet] - const cur = (await localContract.minDstGasLookup(remoteChainId, packetType)).toNumber() - const needChange = cur !== minGas - - // function setMinDstGas(uint16 _dstChainId, uint16 _packetType, uint _minGas) - const functionName = "setMinDstGas" - const params = ["uint16", "uint16", "uint256"] - const args = [remoteChainId, packetType, minGas] - - const tx: any = { - needChange, - chainId: getLayerZeroChainId(localNetwork), - contractName: localContractNameOrAddress, - functionName, - args: args, - calldata: localContract.interface.encodeFunctionData(functionName, args), - } - if (tx.needChange) { - tx.diff = JSON.stringify({ oldValue: cur, newValue: minGas }) - } - txns.push(tx) - } - return txns -} - -export async function setTrustedRemote( - hre: any, - localNetwork: string, - localContractNameOrAddress: string, - remoteNetwork: string, - remoteContractNameOrAddress: string -): Promise { - const localContract = await getContractInstance(hre, localNetwork, localContractNameOrAddress) - const remoteContract = await getContractInstance(hre, remoteNetwork, remoteContractNameOrAddress) - - const remoteContractAddress = await remoteContract.address - const desiredTrustedRemote = hre.ethers.utils.solidityPack(["bytes"], [remoteContractAddress + localContract.address.substring(2)]) - - const remoteChainId = getLayerZeroChainId(remoteNetwork) - const cur = await localContract.trustedRemoteLookup(remoteChainId) - const needChange = cur != desiredTrustedRemote - - // function setTrustedRemote(uint16 _srcChainId, bytes calldata _path) - const functionName = "setTrustedRemote" - const params = ["uint16", "bytes"] - const args = [remoteChainId, desiredTrustedRemote] - - const tx: any = { - needChange, - chainId: getLayerZeroChainId(localNetwork), - contractName: localContractNameOrAddress, - functionName: functionName, - args: args, - calldata: localContract.interface.encodeFunctionData(functionName, args), - } - if (tx.needChange) { - tx.diff = JSON.stringify({ trustedRemote: { oldValue: cur, newValue: desiredTrustedRemote } }) - } - return [tx] -} +import OAPP_ARTIFACT from "@layerzerolabs/lz-evm-sdk-v2/artifacts/contracts/OApp.sol/OApp.json" +import { ethers } from "ethers" export function getContractNameOrAddress(chain: string, WIRE_UP_CONFIG: any) { let contractNameOrAddress @@ -122,3 +22,16 @@ export function getContractNameOrAddress(chain: string, WIRE_UP_CONFIG: any) { } return contractNameOrAddress } + +export function getAndReturnContract(contractNameOrAddress: string, environment: any) { + if (ethers.utils.isAddress(contractNameOrAddress)) { + const oappFactory = ethers.getContractFactory(OAPP_ARTIFACT.abi) + return oappFactory.attach(contractNameOrAddress).connect(environment.provider) + } else { + return await environment.getContract(contractNameOrAddress, environment.provider) + } +} + +export function getOAppContract(chain: string, environment: any, WIRE_UP_CONFIG: any) { + return getAndReturnContract(getContractNameOrAddress(chain, WIRE_UP_CONFIG), environment) +} diff --git a/packages/ua-utils/src/wireAll.ts b/packages/ua-utils/src/wireAll.ts index 895cf46ee..f2458b72c 100644 --- a/packages/ua-utils/src/wireAll.ts +++ b/packages/ua-utils/src/wireAll.ts @@ -1,5 +1,6 @@ import { Transaction, NetworkTransactions, getContractInstance, getLayerZeroChainId, executeTransactions } from "./utils/crossChainHelper" import { configExist, getConfig, logError, printTransactions } from "./utils/helpers" +import { configExist, getConfig, logError, printTransactions } from "./utils/helpers" import { setUseCustomAdapterParams, setMinDstGas, setTrustedRemote, getContractNameOrAddress } from "./utils/wireAllHelpers" export default async function (taskArgs: any, hre: any) { @@ -19,6 +20,9 @@ export default async function (taskArgs: any, hre: any) { const WIRE_UP_CONFIG = getConfig(taskArgs.configPath) const localNetworks = Object.keys(WIRE_UP_CONFIG?.chainConfig) + const localNetwork = hre.network.name + const getEnvironment = createGetNetworkEnvironment(hre) + console.log(`************************************************`) console.log(`Computing diff`) console.log(`************************************************`) @@ -29,65 +33,32 @@ export default async function (taskArgs: any, hre: any) { const transactions: Transaction[] = [] const remoteNetworks = Object.keys(WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig) - const localContractNameOrAddress = getContractNameOrAddress(localNetwork, WIRE_UP_CONFIG) + const localEnvironment = await getEnvironment(localNetwork) + const localContract = getOAppContract(localNetwork, localEnvironment, WIRE_UP_CONFIG) if (localContractNameOrAddress === undefined) { logError(`Invalid wire up config for localContractNameOrAddress.`) return } - // check if useCustomAdapterParams needs to be set - const useCustomAdapterParams = WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.useCustomAdapterParams - if (useCustomAdapterParams !== undefined) { - transactions.push(...(await setUseCustomAdapterParams(hre, localNetwork, localContractNameOrAddress, useCustomAdapterParams))) - } - - // check if defaultFeeBp needs to be set - const defaultFeeBp = WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.defaultFeeBp - if (defaultFeeBp !== undefined) { - transactions.push(...(await setDefaultFeeBp(hre, localNetwork, localContractNameOrAddress, defaultFeeBp))) - } - await Promise.all( remoteNetworks.map(async (remoteNetwork) => { // skip wiring itself if (localNetwork === remoteNetwork) return - const proxyChain = WIRE_UP_CONFIG?.proxyContractConfig?.chain - const remoteContractNameOrAddress = getContractNameOrAddress(remoteNetwork, WIRE_UP_CONFIG) + const remoteEnvironment = await getEnvironment(remoteNetwork) + const remoteContract = getOAppContract(remoteNetwork, remoteEnvironment, WIRE_UP_CONFIG) if (remoteContractNameOrAddress === undefined) { logError(`Invalid wire up config for remoteContractNameOrAddress.`) return } - // setTrustedRemote - transactions.push( - ...(await setTrustedRemote(hre, localNetwork, localContractNameOrAddress, remoteNetwork, remoteContractNameOrAddress)) - ) - - // setFeeBp - if (WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig?.[remoteNetwork]?.feeBpConfig !== undefined) { - transactions.push( - ...(await setFeeBp( - hre, - localNetwork, - localContractNameOrAddress, - WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig?.[remoteNetwork].feeBpConfig, - getLayerZeroChainId(remoteNetwork) - )) - ) - } + // setPeer + transactions.push(...(await setPeer(localContract, remoteContract))) - // setMinDstGas - if (WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig?.[remoteNetwork]?.minDstGasConfig !== undefined) { - transactions.push( - ...(await setMinDstGas( - hre, - localNetwork, - localContractNameOrAddress, - WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig?.[remoteNetwork].minDstGasConfig, - getLayerZeroChainId(remoteNetwork) - )) - ) + // setEnforcedOptions + const enforcedOptions = WIRE_UP_CONFIG?.chainConfig?.[localNetwork]?.remoteNetworkConfig?.[remoteNetwork].enforcedOptions + if (enforcedOptions !== undefined) { + transactions.push(...(await setEnforcedOptions(localContract, remoteContract, enforcedOptions))) } }) ) @@ -112,66 +83,36 @@ export default async function (taskArgs: any, hre: any) { await executeTransactions(hre, taskArgs, transactionByNetwork) } -async function setDefaultFeeBp( - hre: any, - localNetwork: string, - localContractNameOrAddress: string, - defaultFeeBp: number -): Promise { - const localContract = await getContractInstance(hre, localNetwork, localContractNameOrAddress) - const cur = await localContract.defaultFeeBp() - const needChange = cur !== defaultFeeBp - - // function setDefaultFeeBp(uint16 _feeBp) - const functionName = "setDefaultFeeBp" - const params = ["uint16"] - const args = [defaultFeeBp] - - const tx: any = { - needChange, - chainId: getLayerZeroChainId(localNetwork), - contractName: localContractNameOrAddress, - functionName: functionName, - args: args, - calldata: localContract.interface.encodeFunctionData(functionName, args), - } - if (tx.needChange) { - tx.diff = JSON.stringify({ defaultFeeBp: { oldValue: cur, newValue: defaultFeeBp } }) - } - return [tx] +const setPeer = async (localOApp: any, remoteOApp: any): Promise => { + const oldPeer = await localOApp.peers(await remoteOApp.endpoint.eid()) + const newPeer = await remoteOApp.address + const needChange = oldPeer !== newPeer + const contractAddress = await localOApp.address + const functionName = localOApp.setPeer.selector + const args = [newPeer] + const calldata = localOApp.interface.encodeFunctionData(functionName, args) + const diff = needChange ? { oldValue: oldPeer, newValue: newPeer } : undefined + return [{ needChange, chainId, contractAddress, functionName, args, calldata, diff }] } -async function setFeeBp( - hre: any, - localNetwork: string, - localContractNameOrAddress: string, - feeBpConfig: any, - remoteChainId: string -): Promise { - const localContract = await getContractInstance(hre, localNetwork, localContractNameOrAddress) - const feeConfig = await localContract.chainIdToFeeBps(remoteChainId) - const curFeeBp = feeConfig[0] - const curEnabled = feeConfig[1] - const needChange = curFeeBp !== feeBpConfig.feeBp || curEnabled !== feeBpConfig.enabled - - // function setFeeBp(uint16 _dstChainId, bool _enabled, uint16 _feeBp) - const functionName = "setFeeBp" - const params = ["uint16", "bool", "uint16"] - const args = [remoteChainId, feeBpConfig.enabled, feeBpConfig.feeBp] - const calldata = localContract.interface.encodeFunctionData(functionName, args) - - const tx: any = { - needChange, - chainId: getLayerZeroChainId(localNetwork), - contractName: localContractNameOrAddress, - functionName: functionName, - args: args, - calldata: localContract.interface.encodeFunctionData(functionName, args), - } - if (tx.needChange) { - tx.diff = JSON.stringify({ - feeBp: { oldFeeBpValue: curFeeBp, newFeeBpValue: feeBpConfig.feeBp, oldEnabledFee: curEnabled, newEnabledFee: feeBpConfig.enabled }, - }) +const setEnforcedOptions = async (localOApp: any, remoteOApp: any, enforcedOptions: any): Promise => { + const contractAddress = await localOApp.address + const endpointId = await localOApp.endpoint.eid() + const txns: Transaction[] = [] + const packetTypes = Object.keys(enforcedOptions) + for (const packet of packetTypes) { + const packetType = parseInt(packet.at(-1) as string) + const minGas = enforcedOptions[packet] + const remoteChainId = await remoteOApp.endpoint.eid() + const encodedOptions = await localContract.enforcedOptions(remoteChainId, packetType) + const [version, curGas] = hre.ethers.utils.defaultAbiCoder.decode(["uint16", "uint256"], encodedOptions) + const needChange = curGas !== minGas + const functionName = localOApp.setEnforcedOptions.selector + const options = hre.ethers.utils.solidityPack(["uint16", "uint256"], [3, minGas]) + const args = [remoteChainId, packetType, options] + const calldata = localOApp.interface.encodeFunctionData(functionName, args) + const diff = needChange ? { oldValue: cur, newValue: minGas } : undefined + txns.push({ needChange, endpointId, contractAddress, functionName, args, calldata, diff }) } - return [tx] + return txns }