Skip to content

Commit

Permalink
WIP: refactoring wireAll
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Nov 15, 2023
1 parent ebac7be commit 29631ba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 204 deletions.
2 changes: 2 additions & 0 deletions packages/ua-utils/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const action: ActionType<any> = 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(
Expand All @@ -32,6 +33,7 @@ const action: ActionType<any> = 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)

Expand Down
117 changes: 15 additions & 102 deletions packages/ua-utils/src/utils/wireAllHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,6 @@
import { Transaction, getLayerZeroChainId, getContractInstance } from "./crossChainHelper"

export async function setUseCustomAdapterParams(
hre: any,
localNetwork: string,
localContractNameOrAddress: string,
useCustom: boolean
): Promise<Transaction[]> {
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<Transaction[]> {
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<Transaction[]> {
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
Expand All @@ -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)
}
145 changes: 43 additions & 102 deletions packages/ua-utils/src/wireAll.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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(`************************************************`)
Expand All @@ -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)))
}
})
)
Expand All @@ -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<Transaction[]> {
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<Transaction[]> => {
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<Transaction[]> {
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<Transaction[]> => {
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
}

0 comments on commit 29631ba

Please sign in to comment.