Skip to content

Commit

Permalink
🪚 OmniGraph™ Base SDKs - classes and interfaces (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 11, 2023
1 parent 2ec19b2 commit 5fd7196
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 49 deletions.
28 changes: 11 additions & 17 deletions packages/protocol-utils-evm/src/endpoint/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import type { IEndpoint } from '@layerzerolabs/protocol-utils'
import { formatEid, type Address, type OmniTransaction } from '@layerzerolabs/utils'
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import { ignoreZero, makeZeroAddress, omniContractToPoint, type OmniContract } from '@layerzerolabs/utils-evm'
import { ignoreZero, makeZeroAddress, OmniSDK } from '@layerzerolabs/utils-evm'

export class Endpoint implements IEndpoint {
constructor(public readonly contract: OmniContract) {}

async getDefaultReceiveLibrary(eid: EndpointId): Promise<string | undefined> {
export class Endpoint extends OmniSDK implements IEndpoint {
async getDefaultReceiveLibrary(eid: EndpointId): Promise<Address | undefined> {
return ignoreZero(await this.contract.contract.defaultReceiveLibrary(eid))
}

async getSendLibrary(sender: Address, dstEid: EndpointId): Promise<string | undefined> {
async getSendLibrary(sender: Address, dstEid: EndpointId): Promise<Address | undefined> {
return ignoreZero(await this.contract.contract.getSendLibrary(sender, dstEid))
}

async getReceiveLibrary(receiver: Address, srcEid: EndpointId): Promise<[string | undefined, boolean]> {
async getReceiveLibrary(
receiver: Address,
srcEid: EndpointId
): Promise<[address: Address | undefined, isDefault: boolean]> {
return await this.contract.contract.getReceiveLibrary(receiver, srcEid)
}

async setDefaultReceiveLibrary(
eid: EndpointId,
lib: string | null | undefined,
lib: Address | null | undefined,
gracePeriod: number = 0
): Promise<OmniTransaction> {
const data = this.contract.contract.interface.encodeFunctionData('setDefaultReceiveLibrary', [
Expand All @@ -35,7 +36,7 @@ export class Endpoint implements IEndpoint {
}
}

async getDefaultSendLibrary(eid: EndpointId): Promise<string | undefined> {
async getDefaultSendLibrary(eid: EndpointId): Promise<Address | undefined> {
return ignoreZero(await this.contract.contract.defaultSendLibrary(eid))
}

Expand All @@ -55,19 +56,12 @@ export class Endpoint implements IEndpoint {
return this.contract.contract.isRegisteredLibrary(lib)
}

async registerLibrary(lib: string): Promise<OmniTransaction> {
async registerLibrary(lib: Address): Promise<OmniTransaction> {
const data = this.contract.contract.interface.encodeFunctionData('registerLibrary', [lib])

return {
...this.createTransaction(data),
description: `Registering library ${lib}`,
}
}

protected createTransaction(data: string): OmniTransaction {
return {
point: omniContractToPoint(this.contract),
data,
}
}
}
13 changes: 2 additions & 11 deletions packages/protocol-utils-evm/src/uln302/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { IUln302, Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-utils'
import { Address, formatEid, type OmniTransaction } from '@layerzerolabs/utils'
import { omniContractToPoint, type OmniContract, makeZeroAddress } from '@layerzerolabs/utils-evm'
import { makeZeroAddress, OmniSDK } from '@layerzerolabs/utils-evm'
import { Uln302ExecutorConfigSchema, Uln302UlnConfigInputSchema, Uln302UlnConfigSchema } from './schema'

export class Uln302 implements IUln302 {
constructor(public readonly contract: OmniContract) {}

export class Uln302 extends OmniSDK implements IUln302 {
async getUlnConfig(eid: EndpointId, address?: Address | null | undefined): Promise<Uln302UlnConfig> {
const config = await this.contract.contract.getUlnConfig(makeZeroAddress(address), eid)

Expand Down Expand Up @@ -51,11 +49,4 @@ export class Uln302 implements IUln302 {
description: `Setting default ULN config for ${formatEid(eid)}: ${JSON.stringify(serializedConfig)}`,
}
}

protected createTransaction(data: string): OmniTransaction {
return {
point: omniContractToPoint(this.contract),
data,
}
}
}
18 changes: 14 additions & 4 deletions packages/protocol-utils/src/endpoint/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { Address, OmniGraph, OmniPointBasedFactory, OmniTransaction } from '@layerzerolabs/utils'
import type {
Address,
OmniGraph,
OmniPointBasedFactory,
OmniTransaction,
IOmniSDK,
Bytes32,
} from '@layerzerolabs/utils'
import type { EndpointId } from '@layerzerolabs/lz-definitions'

export interface IEndpoint {
export interface IEndpoint extends IOmniSDK {
getDefaultReceiveLibrary(eid: EndpointId): Promise<Address | undefined>
setDefaultReceiveLibrary(
eid: EndpointId,
Expand All @@ -15,8 +22,11 @@ export interface IEndpoint {
isRegisteredLibrary(lib: Address): Promise<boolean>
registerLibrary(lib: Address): Promise<OmniTransaction>

getSendLibrary(sender: Address, dstEid: EndpointId): Promise<string | undefined>
getReceiveLibrary(receiver: Address, srcEid: EndpointId): Promise<[string | undefined, boolean]>
getSendLibrary(sender: Address, dstEid: EndpointId): Promise<Address | undefined>
getReceiveLibrary(
receiver: Address,
srcEid: EndpointId
): Promise<[address: Bytes32 | undefined, isDefault: boolean]>
}

export interface EndpointEdgeConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol-utils/src/uln302/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Address, OmniGraph, OmniPointBasedFactory, OmniTransaction } from '@layerzerolabs/utils'
import type { Address, OmniGraph, OmniPointBasedFactory, OmniTransaction, IOmniSDK } from '@layerzerolabs/utils'
import type { EndpointId } from '@layerzerolabs/lz-definitions'

export interface IUln302 {
export interface IUln302 extends IOmniSDK {
getUlnConfig(eid: EndpointId, address?: Address | null | undefined): Promise<Uln302UlnConfig>
getExecutorConfig(eid: EndpointId, address?: Address | null | undefined): Promise<Uln302ExecutorConfig>
setDefaultExecutorConfig(eid: EndpointId, config: Uln302ExecutorConfig): Promise<OmniTransaction>
Expand Down
19 changes: 7 additions & 12 deletions packages/ua-utils-evm/src/oapp/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { IOApp } from '@layerzerolabs/ua-utils'
import type { Bytes32, Address, OmniTransaction } from '@layerzerolabs/utils'
import {
omniContractToPoint,
OmniContract,
type OmniContract,
ignoreZero,
makeBytes32,
areBytes32Equal,
Expand All @@ -11,12 +10,15 @@ import {
} from '@layerzerolabs/utils-evm'
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { EndpointFactory, IEndpoint } from '@layerzerolabs/protocol-utils'
import { OmniSDK } from '@layerzerolabs/utils-evm'

export class OApp implements IOApp {
export class OApp extends OmniSDK implements IOApp {
constructor(
public readonly contract: OmniContract,
contract: OmniContract,
private readonly endpointFactory: EndpointFactory
) {}
) {
super(contract)
}

async getEndpoint(): Promise<IEndpoint> {
let address: string
Expand Down Expand Up @@ -53,11 +55,4 @@ export class OApp implements IOApp {
const data = this.contract.contract.interface.encodeFunctionData('setPeer', [eid, makeBytes32(address)])
return this.createTransaction(data)
}

protected createTransaction(data: string): OmniTransaction {
return {
point: omniContractToPoint(this.contract),
data,
}
}
}
4 changes: 2 additions & 2 deletions packages/ua-utils/src/oapp/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { IEndpoint } from '@layerzerolabs/protocol-utils'
import type { Address, OmniGraph, OmniTransaction } from '@layerzerolabs/utils'
import type { Address, OmniGraph, OmniTransaction, IOmniSDK } from '@layerzerolabs/utils'
import type { Bytes32 } from '@layerzerolabs/utils'
import type { OmniPointBasedFactory } from '@layerzerolabs/utils'

export interface IOApp {
export interface IOApp extends IOmniSDK {
getEndpoint(): Promise<IEndpoint>
getPeer(eid: EndpointId): Promise<Bytes32 | undefined>
hasPeer(eid: EndpointId, address: Bytes32 | Address | null | undefined): Promise<boolean>
Expand Down
1 change: 1 addition & 0 deletions packages/utils-evm/src/omnigraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './coordinates'
export * from './format'
export * from './sdk'
export * from './types'
22 changes: 22 additions & 0 deletions packages/utils-evm/src/omnigraph/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { OmniPoint, OmniTransaction } from '@layerzerolabs/utils'
import { IOmniSDK, OmniContract } from './types'
import { omniContractToPoint } from './coordinates'

/**
* Base class for all EVM SDKs, providing some common functionality
* to reduce the boilerplate
*/
export abstract class OmniSDK implements IOmniSDK {
constructor(public readonly contract: OmniContract) {}

get point(): OmniPoint {
return omniContractToPoint(this.contract)
}

protected createTransaction(data: string): OmniTransaction {
return {
point: this.point,
data,
}
}
}
9 changes: 8 additions & 1 deletion packages/utils-evm/src/omnigraph/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import type { Contract } from '@ethersproject/contracts'
import type { OmniPoint, WithEid } from '@layerzerolabs/utils'
import type { IOmniSDK as IOmniSDKAbstract, OmniPoint, WithEid } from '@layerzerolabs/utils'

export type OmniContract<TContract extends Contract = Contract> = WithEid<{
contract: TContract
}>

export type OmniContractFactory = (point: OmniPoint) => OmniContract | Promise<OmniContract>

/**
* Base interface for all EVM SDKs, adding the EVM specific attributes
*/
export interface IOmniSDK extends IOmniSDKAbstract {
contract: OmniContract
}
7 changes: 7 additions & 0 deletions packages/utils/src/omnigraph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ export type OmniPointBasedFactory<TValue> = (point: OmniPoint) => TValue | Promi
* Helper type that adds eid property to an underlying type
*/
export type WithEid<TValue> = TValue & { eid: EndpointId }

/**
* Base interface for all SDKs
*/
export interface IOmniSDK {
point: OmniPoint
}

0 comments on commit 5fd7196

Please sign in to comment.