Skip to content

Commit

Permalink
chore: Add interface and base class for EVM SDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 11, 2023
1 parent a0f5f36 commit ca06fb0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
13 changes: 2 additions & 11 deletions packages/protocol-utils-evm/src/endpoint/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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'

export class Endpoint implements IEndpoint {
constructor(public readonly contract: OmniContract) {}
import { ignoreZero, makeZeroAddress, OmniSDK } from '@layerzerolabs/utils-evm'

export class Endpoint extends OmniSDK implements IEndpoint {
async getDefaultReceiveLibrary(eid: EndpointId): Promise<string | undefined> {
return ignoreZero(await this.contract.contract.defaultReceiveLibrary(eid))
}
Expand Down Expand Up @@ -63,11 +61,4 @@ export class Endpoint implements IEndpoint {
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,
}
}
}
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,
}
}
}
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
}

0 comments on commit ca06fb0

Please sign in to comment.