Skip to content

🪛 👷‍♂️UA Utils Update #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion packages/protocol-utils-evm/src/endpoint/sdk.ts
Original file line number Diff line number Diff line change
@@ -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 { CONFIG_TYPE_EXECUTOR, CONFIG_TYPE_ULN, Timeout } from '@layerzerolabs/protocol-utils'
import { defaultAbiCoder } from '@ethersproject/abi'

export class Endpoint extends OmniSDK implements IEndpoint {
constructor(
Expand Down Expand Up @@ -71,6 +80,97 @@ export class Endpoint extends OmniSDK implements IEndpoint {
}
}

async getDefaultReceiveLibraryTimeout(eid: EndpointId): Promise<Timeout> {
return await this.contract.contract.defaultReceiveLibraryTimeout(eid)
}

async setSendLibrary(eid: EndpointId, newLib: Address | null | undefined): Promise<OmniTransaction> {
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<OmniTransaction> {
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<OmniTransaction> {
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<Timeout> {
return await this.contract.contract.receiveLibraryTimeout(receiver, srcEid)
}

async setConfig(lib: Address, params: SetConfigParam[]): Promise<OmniTransaction> {
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<Uln302ExecutorConfig | Uln302UlnConfig> {
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, , , optionalDVNThreshold, requiredDVNs, optionalDVNs] = defaultAbiCoder.decode(
['tuple(uint64,uint8,uint8,uint8,address[],address[])'],
encodedUlnBytes
)
return { confirmations, optionalDVNThreshold, requiredDVNs, optionalDVNs }
}
}

isRegisteredLibrary(lib: Address): Promise<boolean> {
return this.contract.contract.isRegisteredLibrary(lib)
}
Expand Down
28 changes: 27 additions & 1 deletion packages/protocol-utils/src/endpoint/types.ts
Original file line number Diff line number Diff line change
@@ -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<IUln302>
Expand All @@ -23,6 +23,32 @@ export interface IEndpoint extends IOmniSDK {
receiver: Address,
srcEid: EndpointId
): Promise<[address: Bytes32 | undefined, isDefault: boolean]>

getDefaultReceiveLibraryTimeout(eid: EndpointId): Promise<Timeout>
getReceiveLibraryTimeout(receiver: Address, srcEid: EndpointId): Promise<Timeout>

setSendLibrary(eid: EndpointId, newLib: Address): Promise<OmniTransaction>
setReceiveLibrary(eid: EndpointId, newLib: Address, gracePeriod: number): Promise<OmniTransaction>
setReceiveLibraryTimeout(eid: EndpointId, newLib: Address, expiry: number): Promise<OmniTransaction>

setConfig(lib: Address, params: SetConfigParam[]): Promise<OmniTransaction>
getConfig(
oapp: Address,
lib: Address,
eid: EndpointId,
configType: number
): Promise<Uln302ExecutorConfig | Uln302UlnConfig>
}

export interface SetConfigParam {
eid: EndpointId
configType: number
config: string
}

export interface Timeout {
lib: string
expiry: number
}

export interface EndpointEdgeConfig {
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol-utils/src/uln302/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface IUln302 extends IOmniSDK {
setDefaultUlnConfig(eid: EndpointId, config: Uln302UlnConfig): Promise<OmniTransaction>
}

export const CONFIG_TYPE_EXECUTOR = 1

export const CONFIG_TYPE_ULN = 2

export interface Uln302ExecutorConfig {
maxMessageSize: number
executor: string
Expand All @@ -18,6 +22,8 @@ export interface Uln302UlnConfig {
optionalDVNThreshold: number
requiredDVNs: string[]
optionalDVNs: string[]
requiredDVNCount?: number
optionalDVNCount?: number
}

export interface Uln302NodeConfig {
Expand Down
13 changes: 10 additions & 3 deletions packages/ua-utils-evm-hardhat/src/utils/taskHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from '@layerzerolabs/utils'
import { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'
import { Timeout, Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'
import { createConnectedContractFactory, getEidForNetworkName } from '@layerzerolabs/utils-evm-hardhat'
import { createEndpointFactory } from '@layerzerolabs/protocol-utils-evm'

Expand Down Expand Up @@ -34,7 +34,7 @@ export async function getReceiveConfig(
localNetworkName: string,
remoteNetworkName: string,
address?: Address
): Promise<[Address, Uln302UlnConfig]> {
): Promise<[Address, Uln302UlnConfig, Timeout]> {
const localEid = getEidForNetworkName(localNetworkName)
const remoteEid = getEidForNetworkName(remoteNetworkName)
const contractFactory = createConnectedContractFactory()
Expand All @@ -50,8 +50,15 @@ export async function getReceiveConfig(
receiveLibrary = await localEndpointSDK.getDefaultReceiveLibrary(remoteEid)
}

let receiveLibraryTimeout: Timeout
if (address) {
receiveLibraryTimeout = await localEndpointSDK.getReceiveLibraryTimeout(address, remoteEid)
} else {
receiveLibraryTimeout = await localEndpointSDK.getDefaultReceiveLibraryTimeout(remoteEid)
}

const localReceiveUlnSDK = await localEndpointSDK.getUln302SDK(receiveLibrary)

const receiveUlnConfig = await localReceiveUlnSDK.getUlnConfig(remoteEid)
return [receiveLibrary, receiveUlnConfig]
return [receiveLibrary, receiveUlnConfig, receiveLibraryTimeout]
}
5 changes: 5 additions & 0 deletions packages/ua-utils-evm/src/oapp/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class OApp extends OmniSDK implements IOApp {
super(contract)
}

async callEndpoint(callData: string): Promise<OmniTransaction> {
const data = this.contract.contract.interface.encodeFunctionData('callEndpoint', [callData])
return this.createTransaction(data)
}

async getEndpointSDK(): Promise<IEndpoint> {
let address: string

Expand Down
Loading