Skip to content

Commit

Permalink
Updating endpoint and uln302 types in protocol-utils. Updating endpoi…
Browse files Browse the repository at this point in the history
…nt sdk in protocol-utils-evm
  • Loading branch information
sirarthurmoney committed Dec 13, 2023
1 parent 66d923a commit 97b9b9f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
106 changes: 105 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 { Timeout, CONFIG_TYPE_EXECUTOR, CONFIG_TYPE_ULN } from '@layerzerolabs/protocol-utils'
import { defaultAbiCoder } from '@ethersproject/abi'

export class Endpoint extends OmniSDK implements IEndpoint {
constructor(
Expand Down Expand Up @@ -71,6 +80,101 @@ 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,
requiredDVNCount,

Check failure on line 168 in packages/protocol-utils-evm/src/endpoint/sdk.ts

View workflow job for this annotation

GitHub Actions / Check code / Build, Lint & Test

'requiredDVNCount' is assigned a value but never used
optionalDVNCount,

Check failure on line 169 in packages/protocol-utils-evm/src/endpoint/sdk.ts

View workflow job for this annotation

GitHub Actions / Check code / Build, Lint & Test

'optionalDVNCount' is assigned a value but never used
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
37 changes: 36 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,41 @@ 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 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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol-utils/src/uln302/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Uln302UlnConfig {
optionalDVNThreshold: number
requiredDVNs: string[]
optionalDVNs: string[]
requiredDVNCount?: number
optionalDVNCount?: number
}

export interface Uln302NodeConfig {
Expand Down

0 comments on commit 97b9b9f

Please sign in to comment.