diff --git a/packages/protocol-utils-evm/src/endpoint/sdk.ts b/packages/protocol-utils-evm/src/endpoint/sdk.ts index 51f7c9fa1..3f64cc71f 100644 --- a/packages/protocol-utils-evm/src/endpoint/sdk.ts +++ b/packages/protocol-utils-evm/src/endpoint/sdk.ts @@ -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 { +export class Endpoint extends OmniSDK implements IEndpoint { + async getDefaultReceiveLibrary(eid: EndpointId): Promise
{ return ignoreZero(await this.contract.contract.defaultReceiveLibrary(eid)) } - async getSendLibrary(sender: Address, dstEid: EndpointId): Promise { + async getSendLibrary(sender: Address, dstEid: EndpointId): Promise
{ 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 { const data = this.contract.contract.interface.encodeFunctionData('setDefaultReceiveLibrary', [ @@ -35,7 +36,7 @@ export class Endpoint implements IEndpoint { } } - async getDefaultSendLibrary(eid: EndpointId): Promise { + async getDefaultSendLibrary(eid: EndpointId): Promise
{ return ignoreZero(await this.contract.contract.defaultSendLibrary(eid)) } @@ -55,7 +56,7 @@ export class Endpoint implements IEndpoint { return this.contract.contract.isRegisteredLibrary(lib) } - async registerLibrary(lib: string): Promise { + async registerLibrary(lib: Address): Promise { const data = this.contract.contract.interface.encodeFunctionData('registerLibrary', [lib]) return { @@ -63,11 +64,4 @@ export class Endpoint implements IEndpoint { description: `Registering library ${lib}`, } } - - protected createTransaction(data: string): OmniTransaction { - return { - point: omniContractToPoint(this.contract), - data, - } - } } diff --git a/packages/protocol-utils-evm/src/uln302/sdk.ts b/packages/protocol-utils-evm/src/uln302/sdk.ts index 54928a94f..24ab124df 100644 --- a/packages/protocol-utils-evm/src/uln302/sdk.ts +++ b/packages/protocol-utils-evm/src/uln302/sdk.ts @@ -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 { const config = await this.contract.contract.getUlnConfig(makeZeroAddress(address), eid) @@ -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, - } - } } diff --git a/packages/protocol-utils/src/endpoint/types.ts b/packages/protocol-utils/src/endpoint/types.ts index 93b80c4f9..4789f8dea 100644 --- a/packages/protocol-utils/src/endpoint/types.ts +++ b/packages/protocol-utils/src/endpoint/types.ts @@ -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
setDefaultReceiveLibrary( eid: EndpointId, @@ -15,8 +22,11 @@ export interface IEndpoint { isRegisteredLibrary(lib: Address): Promise registerLibrary(lib: Address): Promise - getSendLibrary(sender: Address, dstEid: EndpointId): Promise - getReceiveLibrary(receiver: Address, srcEid: EndpointId): Promise<[string | undefined, boolean]> + getSendLibrary(sender: Address, dstEid: EndpointId): Promise
+ getReceiveLibrary( + receiver: Address, + srcEid: EndpointId + ): Promise<[address: Bytes32 | undefined, isDefault: boolean]> } export interface EndpointEdgeConfig { diff --git a/packages/protocol-utils/src/uln302/types.ts b/packages/protocol-utils/src/uln302/types.ts index 1e59bb93f..f5b5ee3f0 100644 --- a/packages/protocol-utils/src/uln302/types.ts +++ b/packages/protocol-utils/src/uln302/types.ts @@ -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 getExecutorConfig(eid: EndpointId, address?: Address | null | undefined): Promise setDefaultExecutorConfig(eid: EndpointId, config: Uln302ExecutorConfig): Promise diff --git a/packages/ua-utils-evm/src/oapp/sdk.ts b/packages/ua-utils-evm/src/oapp/sdk.ts index 0d9a26a2b..b7ebeea82 100644 --- a/packages/ua-utils-evm/src/oapp/sdk.ts +++ b/packages/ua-utils-evm/src/oapp/sdk.ts @@ -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, @@ -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 { let address: string @@ -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, - } - } } diff --git a/packages/ua-utils/src/oapp/types.ts b/packages/ua-utils/src/oapp/types.ts index 320e5f2a7..dc3c177a7 100644 --- a/packages/ua-utils/src/oapp/types.ts +++ b/packages/ua-utils/src/oapp/types.ts @@ -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 getPeer(eid: EndpointId): Promise hasPeer(eid: EndpointId, address: Bytes32 | Address | null | undefined): Promise diff --git a/packages/utils-evm/src/omnigraph/index.ts b/packages/utils-evm/src/omnigraph/index.ts index 01b288f65..911698dcd 100644 --- a/packages/utils-evm/src/omnigraph/index.ts +++ b/packages/utils-evm/src/omnigraph/index.ts @@ -1,3 +1,4 @@ export * from './coordinates' export * from './format' +export * from './sdk' export * from './types' diff --git a/packages/utils-evm/src/omnigraph/sdk.ts b/packages/utils-evm/src/omnigraph/sdk.ts new file mode 100644 index 000000000..6c669f099 --- /dev/null +++ b/packages/utils-evm/src/omnigraph/sdk.ts @@ -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, + } + } +} diff --git a/packages/utils-evm/src/omnigraph/types.ts b/packages/utils-evm/src/omnigraph/types.ts index b5950bf48..1edde4a17 100644 --- a/packages/utils-evm/src/omnigraph/types.ts +++ b/packages/utils-evm/src/omnigraph/types.ts @@ -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 = WithEid<{ contract: TContract }> export type OmniContractFactory = (point: OmniPoint) => OmniContract | Promise + +/** + * Base interface for all EVM SDKs, adding the EVM specific attributes + */ +export interface IOmniSDK extends IOmniSDKAbstract { + contract: OmniContract +} diff --git a/packages/utils/src/omnigraph/types.ts b/packages/utils/src/omnigraph/types.ts index 2782d646e..abfdb15ad 100644 --- a/packages/utils/src/omnigraph/types.ts +++ b/packages/utils/src/omnigraph/types.ts @@ -68,3 +68,10 @@ export type OmniPointBasedFactory = (point: OmniPoint) => TValue | Promi * Helper type that adds eid property to an underlying type */ export type WithEid = TValue & { eid: EndpointId } + +/** + * Base interface for all SDKs + */ +export interface IOmniSDK { + point: OmniPoint +}