diff --git a/packages/protocol-utils-evm/src/endpoint/sdk.ts b/packages/protocol-utils-evm/src/endpoint/sdk.ts index 6f9b58f35..82e68057a 100644 --- a/packages/protocol-utils-evm/src/endpoint/sdk.ts +++ b/packages/protocol-utils-evm/src/endpoint/sdk.ts @@ -1,8 +1,17 @@ import assert from 'assert' -import type { IEndpoint, IUln302, Uln302Factory } from '@layerzerolabs/protocol-utils' +import type { + IEndpoint, + IUln302, + SetConfigParam, + Uln302ExecutorConfig, + Uln302Factory, + Uln302UlnConfig, +} from '@layerzerolabs/protocol-utils' import { formatEid, type Address, type OmniTransaction, formatOmniPoint } from '@layerzerolabs/utils' import type { EndpointId } from '@layerzerolabs/lz-definitions' import { ignoreZero, isZero, makeZeroAddress, type OmniContract, OmniSDK } from '@layerzerolabs/utils-evm' +import { Timeout, CONFIG_TYPE_EXECUTOR, CONFIG_TYPE_ULN } from '@layerzerolabs/protocol-utils' +import { defaultAbiCoder } from '@ethersproject/abi' export class Endpoint extends OmniSDK implements IEndpoint { constructor( @@ -71,6 +80,101 @@ export class Endpoint extends OmniSDK implements IEndpoint { } } + async getDefaultReceiveLibraryTimeout(eid: EndpointId): Promise { + return await this.contract.contract.defaultReceiveLibraryTimeout(eid) + } + + async setSendLibrary(eid: EndpointId, newLib: Address | null | undefined): Promise { + const data = this.contract.contract.interface.encodeFunctionData('setSendLibrary', [eid, newLib]) + + return { + ...this.createTransaction(data), + description: `Setting send library for ${formatEid(eid)} to ${newLib}`, + } + } + + async setReceiveLibrary( + eid: EndpointId, + newLib: Address | null | undefined, + gracePeriod: number + ): Promise { + const data = this.contract.contract.interface.encodeFunctionData('setReceiveLibrary', [ + eid, + newLib, + gracePeriod, + ]) + + return { + ...this.createTransaction(data), + description: `Setting receive library for ${formatEid(eid)} to ${newLib} with grace period ${gracePeriod}`, + } + } + + async setReceiveLibraryTimeout( + eid: EndpointId, + lib: Address | null | undefined, + expiry: number + ): Promise { + const data = this.contract.contract.interface.encodeFunctionData('setReceiveLibraryTimeout', [eid, lib, expiry]) + + return { + ...this.createTransaction(data), + description: `Setting receive library timeout for ${formatEid( + eid + )} to ${lib} with expiration period ${expiry}`, + } + } + + async getReceiveLibraryTimeout(receiver: Address, srcEid: EndpointId): Promise { + return await this.contract.contract.receiveLibraryTimeout(receiver, srcEid) + } + + async setConfig(lib: Address, params: SetConfigParam[]): Promise { + const data = this.contract.contract.interface.encodeFunctionData('setConfig', [lib, params]) + + console.log({ params }) + + let description: string = '' + for (const param of params) { + description += `Setting ${ + param.configType === CONFIG_TYPE_EXECUTOR ? 'executor' : 'uln' + } config for endpoint ${formatEid(param.eid)}. ` + } + + return { + ...this.createTransaction(data), + description: description, + } + } + + async getConfig( + oapp: Address, + lib: Address, + eid: EndpointId, + configType: number + ): Promise { + assert( + configType === CONFIG_TYPE_EXECUTOR || configType === CONFIG_TYPE_ULN, + `configType invalid ${configType}` + ) + if (configType === CONFIG_TYPE_EXECUTOR) { + const encodedExecutorBytes = await this.contract.contract.getConfig(oapp, lib, eid, configType) + const [maxMessageSize, executor] = defaultAbiCoder.decode(['uint32', 'address'], encodedExecutorBytes) + return { maxMessageSize, executor } + } else { + const encodedUlnBytes = await this.contract.contract.getConfig(oapp, lib, eid, configType) + const [ + confirmations, + requiredDVNCount, + optionalDVNCount, + optionalDVNThreshold, + requiredDVNs, + optionalDVNs, + ] = defaultAbiCoder.decode(['tuple(uint64,uint8,uint8,uint8,address[],address[])'], encodedUlnBytes) + return { confirmations, optionalDVNThreshold, requiredDVNs, optionalDVNs } + } + } + isRegisteredLibrary(lib: Address): Promise { return this.contract.contract.isRegisteredLibrary(lib) } diff --git a/packages/protocol-utils/src/endpoint/types.ts b/packages/protocol-utils/src/endpoint/types.ts index c307e573e..c82efcf04 100644 --- a/packages/protocol-utils/src/endpoint/types.ts +++ b/packages/protocol-utils/src/endpoint/types.ts @@ -1,6 +1,6 @@ import type { Address, Factory, OmniGraph, OmniPoint, OmniTransaction, IOmniSDK, Bytes32 } from '@layerzerolabs/utils' import type { EndpointId } from '@layerzerolabs/lz-definitions' -import type { IUln302 } from '@/uln302/types' +import type { IUln302, Uln302ExecutorConfig, Uln302UlnConfig } from '@/uln302/types' export interface IEndpoint extends IOmniSDK { getUln302SDK(address: Address): Promise @@ -23,6 +23,41 @@ export interface IEndpoint extends IOmniSDK { receiver: Address, srcEid: EndpointId ): Promise<[address: Bytes32 | undefined, isDefault: boolean]> + + getDefaultReceiveLibraryTimeout(eid: EndpointId): Promise + getReceiveLibraryTimeout(receiver: Address, srcEid: EndpointId): Promise + + setSendLibrary(eid: EndpointId, newLib: Address): Promise + setReceiveLibrary(eid: EndpointId, newLib: Address, gracePeriod: number): Promise + setReceiveLibraryTimeout(eid: EndpointId, newLib: Address, expiry: number): Promise + + setConfig(lib: Address, params: SetConfigParam[]): Promise + getConfig( + oapp: Address, + lib: Address, + eid: EndpointId, + configType: number + ): Promise +} + +export const CONFIG_TYPE_EXECUTOR = 1 + +export const CONFIG_TYPE_ULN = 2 + +export interface SetConfigParam { + eid: EndpointId + configType: number + config: string +} + +export interface ReceiveLibraryConfig { + receiveLibrary: string + gracePeriod: number +} + +export interface Timeout { + lib: string + expiry: number } export interface EndpointEdgeConfig { diff --git a/packages/protocol-utils/src/uln302/types.ts b/packages/protocol-utils/src/uln302/types.ts index a19aeeb93..edc99201b 100644 --- a/packages/protocol-utils/src/uln302/types.ts +++ b/packages/protocol-utils/src/uln302/types.ts @@ -18,6 +18,8 @@ export interface Uln302UlnConfig { optionalDVNThreshold: number requiredDVNs: string[] optionalDVNs: string[] + requiredDVNCount?: number + optionalDVNCount?: number } export interface Uln302NodeConfig {