Skip to content

Commit

Permalink
Refactoring setConfig into setUlnConfig and setExecutorConfig. Refact…
Browse files Browse the repository at this point in the history
…ored getExecutorConfig and getUlnConfig. Added encodeExecutorConfig and encodeUlnConfig into Uln302 sdk.
  • Loading branch information
sirarthurmoney committed Dec 13, 2023
1 parent 5306f5d commit 2138b02
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
66 changes: 56 additions & 10 deletions packages/protocol-utils-evm/src/endpoint/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert'
import type {
IEndpoint,
IUln302,
SetConfigParam,
Uln302ExecutorConfig,
Uln302Factory,
Uln302UlnConfig,
Expand All @@ -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

Expand Down Expand Up @@ -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<OmniTransaction> {
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<OmniTransaction> {
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<Uln302ExecutorConfig> {
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<Uln302UlnConfig> {
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<boolean> {
Expand Down
13 changes: 13 additions & 0 deletions packages/protocol-utils-evm/src/uln302/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uln302UlnConfig> {
Expand Down Expand Up @@ -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<OmniTransaction> {
const serializedConfig = Uln302UlnConfigInputSchema.parse(config)
const data = this.contract.contract.interface.encodeFunctionData('setDefaultUlnConfigs', [
Expand Down

0 comments on commit 2138b02

Please sign in to comment.