Skip to content

Commit

Permalink
Updating oapp types in ua-utils. Updating oapp sdk in ua-utils-evm
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Dec 13, 2023
1 parent 59be4c5 commit 09ebe6a
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 9 deletions.
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
187 changes: 184 additions & 3 deletions packages/ua-utils/src/oapp/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type { OmniTransaction } from '@layerzerolabs/utils'
import { Address, flattenTransactions, type OmniTransaction } from '@layerzerolabs/utils'
import type { OAppFactory, OAppOmniGraph } from './types'
import { createModuleLogger, printBoolean } from '@layerzerolabs/io-utils'
import { formatOmniVector } from '@layerzerolabs/utils'
import {
CONFIG_TYPE_EXECUTOR,
CONFIG_TYPE_ULN,
Uln302ExecutorConfig,
Uln302UlnConfig,
} from '@layerzerolabs/protocol-utils'
import { SetConfigParam } from '@layerzerolabs/protocol-utils'
import { defaultAbiCoder } from '@ethersproject/abi'

export const configureOApp = async (graph: OAppOmniGraph, createSdk: OAppFactory): Promise<OmniTransaction[]> => {
const logger = createModuleLogger('OApp')
export type OAppConfigurator = (graph: OAppOmniGraph, createSdk: OAppFactory) => Promise<OmniTransaction[]>

export const configureOApp: OAppConfigurator = async (graph: OAppOmniGraph, createSdk: OAppFactory) =>
flattenTransactions([await configureOAppPeers(graph, createSdk), await configureOAppConfigs(graph, createSdk)])

export const configureOAppPeers: OAppConfigurator = async (graph, createSdk) => {
const logger = createModuleLogger('OApp')
const setPeers = await Promise.all(
graph.connections.map(async ({ vector: { from, to } }): Promise<OmniTransaction[]> => {
logger.verbose(`Checking connection ${formatOmniVector({ from, to })}`)
Expand All @@ -23,3 +35,172 @@ export const configureOApp = async (graph: OAppOmniGraph, createSdk: OAppFactory

return [...setPeers].flat()
}

export const configureOAppConfigs: OAppConfigurator = async (graph, createSdk) => {
const setSendLibrary = await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
if (!config?.sendLibrary) return []
const oappSdk = await createSdk(from)
const endpointSdk = await oappSdk.getEndpointSDK()
const currentSendLibrary = await endpointSdk.getSendLibrary(from.address, to.eid)

if (currentSendLibrary === config.sendLibrary) return []
return [await endpointSdk.setSendLibrary(to.eid, config.sendLibrary)]
})
)

const setReceiveLibrary = await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
if (!config?.receiveLibraryConfig) return []
const oappSdk = await createSdk(from)
const endpointSdk = await oappSdk.getEndpointSDK()
const [currentReceiveLibrary] = await endpointSdk.getReceiveLibrary(from.address, to.eid)

if (currentReceiveLibrary === config.receiveLibraryConfig.receiveLibrary) return []
return [
await endpointSdk.setReceiveLibrary(
to.eid,
config.receiveLibraryConfig.receiveLibrary,
config.receiveLibraryConfig.gracePeriod
),
]
})
)

const setReceiveLibraryTimeout = await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
if (!config?.receiveLibraryTimeoutConfig) return []
const oappSdk = await createSdk(from)
const endpointSdk = await oappSdk.getEndpointSDK()
const timeout = await endpointSdk.getReceiveLibraryTimeout(from.address, to.eid)

if (
timeout.lib === config.receiveLibraryTimeoutConfig.lib &&
timeout.expiry === config.receiveLibraryTimeoutConfig.expiry
)
return []
return [
await endpointSdk.setReceiveLibraryTimeout(
to.eid,
config.receiveLibraryTimeoutConfig.lib,
config.receiveLibraryTimeoutConfig.expiry
),
]
})
)

const setConfg = await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
const oappSdk = await createSdk(from)
const endpointSdk = await oappSdk.getEndpointSDK()
const sendConfigs: SetConfigParam[] = []
const receiveConfigs: SetConfigParam[] = []
const transactions: OmniTransaction[] = []

if (config?.sendConfig) {
const currentSendLibrary: Address = config.sendLibrary
? config.sendLibrary
: (await endpointSdk.getSendLibrary(from.address, to.eid)) ?? ''
const sendExecutorConfig: Uln302ExecutorConfig = <Uln302ExecutorConfig>(
await endpointSdk.getConfig(from.address, currentSendLibrary, to.eid, CONFIG_TYPE_EXECUTOR)
)

const setExecutorConfigParam: SetConfigParam = {
eid: to.eid,
configType: CONFIG_TYPE_EXECUTOR,
config: '',
}

// 1) executor
// - maxMessageSize
// - executor
if (
sendExecutorConfig.maxMessageSize !== config.sendConfig.executorConfig.maxMessageSize ||
sendExecutorConfig.executor !== config.sendConfig.executorConfig.executor
) {
setExecutorConfigParam.config = defaultAbiCoder.encode(
['uint32', 'address'],
[config.sendConfig.executorConfig.maxMessageSize, config.sendConfig.executorConfig.executor]
)
sendConfigs.push(setExecutorConfigParam)
}

const setUlnConfigParam: SetConfigParam = {
eid: to.eid,
configType: CONFIG_TYPE_ULN,
config: '',
}

// 2) uln
// - confirmations
// - optionalDVNThreshold
// - requiredDVNs
// - optionalDVNs
const sendUlnConfig = <Uln302UlnConfig>(
await endpointSdk.getConfig(from.address, currentSendLibrary, to.eid, CONFIG_TYPE_ULN)
)
if (
sendUlnConfig.confirmations !== config.sendConfig.ulnConfig.confirmations ||
sendUlnConfig.optionalDVNThreshold !== config.sendConfig.ulnConfig.optionalDVNThreshold ||
sendUlnConfig.requiredDVNs !== config.sendConfig.ulnConfig.requiredDVNs ||
sendUlnConfig.optionalDVNs !== config.sendConfig.ulnConfig.optionalDVNs
) {
setUlnConfigParam.config = defaultAbiCoder.encode(
['uint64', 'uint8', 'uint8', 'uint8', 'address[]', 'address[]'],
[
config.sendConfig.ulnConfig.confirmations,
config.sendConfig.ulnConfig.requiredDVNs.length,
config.sendConfig.ulnConfig.optionalDVNs.length,
config.sendConfig.ulnConfig.optionalDVNThreshold,
config.sendConfig.ulnConfig.requiredDVNs,
config.sendConfig.ulnConfig.optionalDVNs,
]
)
sendConfigs.push(setUlnConfigParam)
}

if (sendConfigs.length) transactions.push(await endpointSdk.setConfig(currentSendLibrary, sendConfigs))
}

if (config?.receiveConfig) {
const setUlnConfigParam: SetConfigParam = {
eid: to.eid,
configType: CONFIG_TYPE_ULN,
config: '',
}
const [currentReceiveLibrary] = config.receiveLibraryConfig?.receiveLibrary
? [config.receiveLibraryConfig?.receiveLibrary, false]
: await endpointSdk.getReceiveLibrary(from.address, to.eid)
const receiveUlnConfig: Uln302UlnConfig = <Uln302UlnConfig>(
await endpointSdk.getConfig(from.address, currentReceiveLibrary!, to.eid, CONFIG_TYPE_ULN)
)
if (
receiveUlnConfig.confirmations !== config.receiveConfig.ulnConfig.confirmations ||
receiveUlnConfig.optionalDVNThreshold !== config.receiveConfig.ulnConfig.optionalDVNThreshold ||
receiveUlnConfig.requiredDVNs !== config.receiveConfig.ulnConfig.requiredDVNs ||
receiveUlnConfig.optionalDVNs !== config.receiveConfig.ulnConfig.optionalDVNs
) {
setUlnConfigParam.config = defaultAbiCoder.encode(
['uint64', 'uint8', 'uint8', 'uint8', 'address[]', 'address[]'],
[
config.receiveConfig.ulnConfig.confirmations,
config.receiveConfig.ulnConfig.requiredDVNs.length,
config.receiveConfig.ulnConfig.optionalDVNs.length,
config.receiveConfig.ulnConfig.optionalDVNThreshold,
config.receiveConfig.ulnConfig.requiredDVNs,
config.receiveConfig.ulnConfig.optionalDVNs,
]
)
receiveConfigs.push(setUlnConfigParam)
}
if (receiveConfigs.length)
transactions.push(await endpointSdk.setConfig(currentReceiveLibrary!, receiveConfigs))
}

if (!sendConfigs.length && !receiveConfigs.length) return []
return [...transactions]
})
)

return [...setSendLibrary, ...setReceiveLibrary, ...setReceiveLibraryTimeout, ...setConfg].flat()
}
26 changes: 23 additions & 3 deletions packages/ua-utils/src/oapp/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { IEndpoint } from '@layerzerolabs/protocol-utils'
import type { Address, Bytes32, Factory, OmniGraph, OmniTransaction, IOmniSDK, OmniPoint } from '@layerzerolabs/utils'
import type { IEndpoint, Timeout } from '@layerzerolabs/protocol-utils'
import type { Address, Factory, IOmniSDK, OmniGraph, OmniPoint, OmniTransaction } from '@layerzerolabs/utils'
import type { Bytes32 } from '@layerzerolabs/utils'
import type { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'

export interface IOApp extends IOmniSDK {
getEndpointSDK(): Promise<IEndpoint>
getPeer(eid: EndpointId): Promise<Bytes32 | undefined>
hasPeer(eid: EndpointId, address: Bytes32 | Address | null | undefined): Promise<boolean>
setPeer(eid: EndpointId, peer: Bytes32 | Address | null | undefined): Promise<OmniTransaction>
callEndpoint(callData: string): Promise<OmniTransaction>
}

export type OAppOmniGraph = OmniGraph<unknown, unknown>
export interface ReceiveLibraryConfig {
receiveLibrary: string
gracePeriod: number
}
export interface OAppEdgeConfig {
sendLibrary?: string
receiveLibraryConfig?: ReceiveLibraryConfig
receiveLibraryTimeoutConfig?: Timeout
sendConfig?: {
executorConfig: Uln302ExecutorConfig
ulnConfig: Uln302UlnConfig
}
receiveConfig?: {
ulnConfig: Uln302UlnConfig
}
}

export type OAppOmniGraph = OmniGraph<unknown, OAppEdgeConfig>

export type OAppFactory<TOApp extends IOApp = IOApp, TOmniPoint = OmniPoint> = Factory<[TOmniPoint], TOApp>

0 comments on commit 09ebe6a

Please sign in to comment.