From 2138b02f4539a865d2acc57da7e1b9a7327af9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lz=2Esir=CE=94rthurmoney=28=29?= Date: Wed, 13 Dec 2023 15:49:28 -0800 Subject: [PATCH] Refactoring setConfig into setUlnConfig and setExecutorConfig. Refactored getExecutorConfig and getUlnConfig. Added encodeExecutorConfig and encodeUlnConfig into Uln302 sdk. --- .../protocol-utils-evm/src/endpoint/sdk.ts | 66 ++++++++++++++++--- packages/protocol-utils-evm/src/uln302/sdk.ts | 13 ++++ 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/packages/protocol-utils-evm/src/endpoint/sdk.ts b/packages/protocol-utils-evm/src/endpoint/sdk.ts index 62ae37459..21941f3b0 100644 --- a/packages/protocol-utils-evm/src/endpoint/sdk.ts +++ b/packages/protocol-utils-evm/src/endpoint/sdk.ts @@ -2,6 +2,7 @@ import assert from 'assert' import type { IEndpoint, IUln302, + SetConfigParam, Uln302ExecutorConfig, Uln302Factory, Uln302UlnConfig, @@ -10,7 +11,7 @@ import { formatEid, type Address, type OmniTransaction, formatOmniPoint } from ' import type { EndpointId } from '@layerzerolabs/lz-definitions' import { ignoreZero, isZero, makeZeroAddress, type OmniContract, OmniSDK } from '@layerzerolabs/utils-evm' import { Timeout } from '@layerzerolabs/protocol-utils' -import { defaultAbiCoder } from '@ethersproject/abi' +import { Uln302 } from '@/uln302' export const CONFIG_TYPE_EXECUTOR = 1 @@ -132,19 +133,64 @@ export class Endpoint extends OmniSDK implements IEndpoint { return await this.contract.contract.receiveLibraryTimeout(receiver, srcEid) } + async setUlnConfig( + oapp: Address, + eidArray: EndpointId[], + ulnConfigArray: Uln302UlnConfig[] + ): Promise { + assert(eidArray.length === ulnConfigArray.length, `array length should match`) + const uln = (await this.getUln302SDK(oapp)) as Uln302 + const setConfigParamArray: SetConfigParam[] = [] + for (const [index, ulnConfig] of ulnConfigArray.entries()) { + const eid = eidArray[index] + assert(eid !== undefined, `eid must be defined`) + setConfigParamArray.push({ + eid: eid, + configType: CONFIG_TYPE_ULN, + config: uln.encodeUlnConfig(ulnConfig), + }) + } + + const data = this.contract.contract.interface.encodeFunctionData('setConfig', [oapp, setConfigParamArray]) + return { + ...this.createTransaction(data), + description: `Set Executor Config for oapp: ${oapp}`, + } + } + + async setExecutorConfig( + oapp: Address, + eidArray: EndpointId[], + executorConfigArray: Uln302ExecutorConfig[] + ): Promise { + assert(eidArray.length === executorConfigArray.length, `array length should match`) + const uln = (await this.getUln302SDK(oapp)) as Uln302 + const setConfigParamArray: SetConfigParam[] = [] + for (const [index, executorConfig] of executorConfigArray.entries()) { + const eid = eidArray[index] + assert(eid !== undefined, `eid must be defined`) + setConfigParamArray.push({ + eid: eid, + configType: CONFIG_TYPE_EXECUTOR, + config: uln.encodeExecutorConfig(executorConfig), + }) + } + + const data = this.contract.contract.interface.encodeFunctionData('setConfig', [oapp, setConfigParamArray]) + return { + ...this.createTransaction(data), + description: `Set Executor Config for oapp: ${oapp}`, + } + } + async getExecutorConfig(oapp: Address, lib: Address, eid: EndpointId): Promise { - const encodedExecutorBytes = await this.contract.contract.getConfig(oapp, lib, eid, CONFIG_TYPE_EXECUTOR) - const [maxMessageSize, executor] = defaultAbiCoder.decode(['uint32', 'address'], encodedExecutorBytes) - return { maxMessageSize, executor } + const uln = await this.getUln302SDK(lib) + return await uln.getExecutorConfig(eid, oapp) } async getUlnConfig(oapp: Address, lib: Address, eid: EndpointId): Promise { - const encodedUlnBytes = await this.contract.contract.getConfig(oapp, lib, eid, CONFIG_TYPE_ULN) - const [confirmations, , , optionalDVNThreshold, requiredDVNs, optionalDVNs] = defaultAbiCoder.decode( - ['tuple(uint64,uint8,uint8,uint8,address[],address[])'], - encodedUlnBytes - ) - return { confirmations, optionalDVNThreshold, requiredDVNs, optionalDVNs } + const uln = await this.getUln302SDK(lib) + return await uln.getUlnConfig(eid, oapp) } isRegisteredLibrary(lib: Address): Promise { diff --git a/packages/protocol-utils-evm/src/uln302/sdk.ts b/packages/protocol-utils-evm/src/uln302/sdk.ts index 24ab124df..57e7e1bc2 100644 --- a/packages/protocol-utils-evm/src/uln302/sdk.ts +++ b/packages/protocol-utils-evm/src/uln302/sdk.ts @@ -3,6 +3,7 @@ import type { IUln302, Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerol import { Address, formatEid, type OmniTransaction } from '@layerzerolabs/utils' import { makeZeroAddress, OmniSDK } from '@layerzerolabs/utils-evm' import { Uln302ExecutorConfigSchema, Uln302UlnConfigInputSchema, Uln302UlnConfigSchema } from './schema' +import assert from 'assert' export class Uln302 extends OmniSDK implements IUln302 { async getUlnConfig(eid: EndpointId, address?: Address | null | undefined): Promise { @@ -33,6 +34,18 @@ export class Uln302 extends OmniSDK implements IUln302 { return this.createTransaction(data) } + encodeExecutorConfig(config: Uln302ExecutorConfig): string { + const [encoded] = this.contract.contract.interface.encodeFunctionResult('getExecutorConfig', [config]) + + return assert(typeof encoded === 'string', 'Must be a string'), encoded + } + + encodeUlnConfig(config: Uln302UlnConfig): string { + const [encoded] = this.contract.contract.interface.encodeFunctionResult('getUlnConfig', [config]) + + return assert(typeof encoded === 'string', 'Must be a string'), encoded + } + async setDefaultUlnConfig(eid: EndpointId, config: Uln302UlnConfig): Promise { const serializedConfig = Uln302UlnConfigInputSchema.parse(config) const data = this.contract.contract.interface.encodeFunctionData('setDefaultUlnConfigs', [