Skip to content

Commit

Permalink
🧹 specify endpoint "v2" version in appropriate situations (#375)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding authored Feb 9, 2024
1 parent 626f5b6 commit 6e464f2
Show file tree
Hide file tree
Showing 34 changed files with 257 additions and 234 deletions.
16 changes: 16 additions & 0 deletions .changeset/fresh-pumas-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@layerzerolabs/ua-devtools-evm-hardhat-test": minor
"@layerzerolabs/omnicounter-devtools-evm": minor
"@layerzerolabs/ua-devtools-evm-hardhat": minor
"@layerzerolabs/protocol-devtools-evm": minor
"@layerzerolabs/devtools-evm-hardhat": minor
"@layerzerolabs/protocol-devtools": minor
"@layerzerolabs/ua-devtools-evm": minor
"@layerzerolabs/devtools-evm": minor
"@layerzerolabs/ua-devtools": minor
"@layerzerolabs/devtools": minor
"@layerzerolabs/oapp-example": minor
"@layerzerolabs/oft-example": minor
---

Specify EndpointV2 with the "V2" suffix wherever appropriate
18 changes: 9 additions & 9 deletions examples/oapp/test/hardhat/MyOApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('MyOApp Test', function () {
let endpointOwner: SignerWithAddress
let myOAppA: Contract
let myOAppB: Contract
let mockEndpointA: Contract
let mockEndpointB: Contract
let mockEndpointV2A: Contract
let mockEndpointV2B: Contract

// Before hook for setup that runs once before all tests in the block
before(async function () {
Expand All @@ -45,17 +45,17 @@ describe('MyOApp Test', function () {

// beforeEach hook for setup that runs before each test in the block
beforeEach(async function () {
// Deploying a mock LZEndpoint with the given Endpoint ID
mockEndpointA = await EndpointV2Mock.deploy(eidA)
mockEndpointB = await EndpointV2Mock.deploy(eidB)
// Deploying a mock LZ EndpointV2 with the given Endpoint ID
mockEndpointV2A = await EndpointV2Mock.deploy(eidA)
mockEndpointV2B = await EndpointV2Mock.deploy(eidB)

// Deploying two instances of MyOApp contract and linking them to the mock LZEndpoint
myOAppA = await MyOApp.deploy(mockEndpointA.address, ownerA.address)
myOAppB = await MyOApp.deploy(mockEndpointB.address, ownerB.address)
myOAppA = await MyOApp.deploy(mockEndpointV2A.address, ownerA.address)
myOAppB = await MyOApp.deploy(mockEndpointV2B.address, ownerB.address)

// Setting destination endpoints in the LZEndpoint mock for each MyOApp instance
await mockEndpointA.setDestLzEndpoint(myOAppB.address, mockEndpointB.address)
await mockEndpointB.setDestLzEndpoint(myOAppA.address, mockEndpointA.address)
await mockEndpointV2A.setDestLzEndpoint(myOAppB.address, mockEndpointV2B.address)
await mockEndpointV2B.setDestLzEndpoint(myOAppA.address, mockEndpointV2A.address)

// Setting each MyOApp instance as a peer of the other
await myOAppA.connect(ownerA).setPeer(eidB, ethers.utils.zeroPad(myOAppB.address, 32))
Expand Down
16 changes: 8 additions & 8 deletions examples/oft/test/hardhat/MyOFT.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('MyOFT Test', function () {
let endpointOwner: SignerWithAddress
let myOFTA: Contract
let myOFTB: Contract
let mockEndpointA: Contract
let mockEndpointB: Contract
let mockEndpointV2A: Contract
let mockEndpointV2B: Contract

// Before hook for setup that runs once before all tests in the block
before(async function () {
Expand Down Expand Up @@ -46,16 +46,16 @@ describe('MyOFT Test', function () {
// beforeEach hook for setup that runs before each test in the block
beforeEach(async function () {
// Deploying a mock LZEndpoint with the given Endpoint ID
mockEndpointA = await EndpointV2Mock.deploy(eidA)
mockEndpointB = await EndpointV2Mock.deploy(eidB)
mockEndpointV2A = await EndpointV2Mock.deploy(eidA)
mockEndpointV2B = await EndpointV2Mock.deploy(eidB)

// Deploying two instances of MyOFT contract with different identifiers and linking them to the mock LZEndpoint
myOFTA = await MyOFT.deploy('aOFT', 'aOFT', mockEndpointA.address, ownerA.address)
myOFTB = await MyOFT.deploy('bOFT', 'bOFT', mockEndpointB.address, ownerB.address)
myOFTA = await MyOFT.deploy('aOFT', 'aOFT', mockEndpointV2A.address, ownerA.address)
myOFTB = await MyOFT.deploy('bOFT', 'bOFT', mockEndpointV2B.address, ownerB.address)

// Setting destination endpoints in the LZEndpoint mock for each MyOFT instance
await mockEndpointA.setDestLzEndpoint(myOFTB.address, mockEndpointB.address)
await mockEndpointB.setDestLzEndpoint(myOFTA.address, mockEndpointA.address)
await mockEndpointV2A.setDestLzEndpoint(myOFTB.address, mockEndpointV2B.address)
await mockEndpointV2B.setDestLzEndpoint(myOFTA.address, mockEndpointV2A.address)

// Setting each MyOFT instance as a peer of the other in the mock LZEndpoint
await myOFTA.connect(ownerA).setPeer(eidB, ethers.utils.zeroPad(myOFTB.address, 32))
Expand Down
15 changes: 8 additions & 7 deletions packages/omnicounter-devtools-evm/src/omnicounter/factory.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import pMemoize from 'p-memoize'
import { OmniCounter } from '@/omnicounter/sdk'
import { createEndpointFactory } from '@layerzerolabs/protocol-devtools-evm'
import { EndpointFactory } from '@layerzerolabs/protocol-devtools'
import { createEndpointV2Factory } from '@layerzerolabs/protocol-devtools-evm'
import { EndpointV2Factory } from '@layerzerolabs/protocol-devtools'
import { OAppFactory } from '@layerzerolabs/ua-devtools'
import { OmniContractFactory } from '@layerzerolabs/devtools-evm'

/**
* Syntactic sugar that creates an instance of EVM `OmniCounter` SDK based on an `OmniPoint` with help of an
* `OmniContractFactory` and an (optional) `EndpointFactory`
* `OmniContractFactory` and an (optional) `EndpointV2Factory`
*
* @param {OmniContractFactory} contractFactory
* @param {EndpointFactory} [endpointFactory]
* @returns {EndpointFactory<Endpoint>}
* @param {EndpointV2Factory} [EndpointV2Factory]
* @returns {EndpointV2Factory<Endpoint>}
*/
export const createOmniCounterFactory = (
contractFactory: OmniContractFactory,
endpointFactory: EndpointFactory = createEndpointFactory(contractFactory)
): OAppFactory<OmniCounter> => pMemoize(async (point) => new OmniCounter(await contractFactory(point), endpointFactory))
EndpointV2Factory: EndpointV2Factory = createEndpointV2Factory(contractFactory)
): OAppFactory<OmniCounter> =>
pMemoize(async (point) => new OmniCounter(await contractFactory(point), EndpointV2Factory))
10 changes: 5 additions & 5 deletions packages/omnicounter-devtools-evm/src/omnicounter/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { IncrementOutput, IncrementType, IOmniCounter } from '@layerzerolabs/omnicounter-devtools'
import { EndpointFactory } from '@layerzerolabs/protocol-devtools'
import { EndpointV2Factory } from '@layerzerolabs/protocol-devtools'
import { OApp } from '@layerzerolabs/ua-devtools-evm'
import { type OmniAddress, makeBytes32 } from '@layerzerolabs/devtools'
import { OmniContract } from '@layerzerolabs/devtools-evm'

export class OmniCounter extends OApp implements IOmniCounter {
public constructor(
public override contract: OmniContract,
protected override endpointFactory: EndpointFactory
protected override endpointV2Factory: EndpointV2Factory
) {
super(contract, endpointFactory)
super(contract, endpointV2Factory)
}

public async increment(
Expand All @@ -20,8 +20,8 @@ export class OmniCounter extends OApp implements IOmniCounter {
receiver: OmniAddress
): Promise<IncrementOutput> {
const data = this.contract.contract.interface.encodeFunctionData('increment', [eid, type, options])
const endpointSdk = await super.getEndpointSDK()
const messagingFee = await endpointSdk.quote(
const endpointV2Sdk = await super.getEndpointSDK()
const messagingFee = await endpointV2Sdk.quote(
{ dstEid: eid, options, message: data, receiver: makeBytes32(receiver), payInLzToken: false },
this.contract.contract.address
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import pMemoize from 'p-memoize'
import type { EndpointFactory, Uln302Factory } from '@layerzerolabs/protocol-devtools'
import type { EndpointV2Factory, Uln302Factory } from '@layerzerolabs/protocol-devtools'
import type { OmniPoint } from '@layerzerolabs/devtools'
import type { OmniContractFactory } from '@layerzerolabs/devtools-evm'
import { Endpoint } from './sdk'
import { EndpointV2 } from './sdk'
import { createUln302Factory } from '@/uln302/factory'

/**
* Syntactic sugar that creates an instance of EVM `Endpoint` SDK
* based on an `OmniPoint` with help of an `OmniContractFactory`
*
* @param {OmniContractFactory} contractFactory
* @returns {EndpointFactory<Endpoint>}
* @param {Uln302Factory} uln302Factory
* @returns {EndpointV2Factory<EndpointV2>}
*/
export const createEndpointFactory = <TOmniPoint = never>(
export const createEndpointV2Factory = <TOmniPoint = never>(
contractFactory: OmniContractFactory<TOmniPoint | OmniPoint>,
uln302Factory: Uln302Factory = createUln302Factory(contractFactory)
): EndpointFactory<Endpoint, TOmniPoint | OmniPoint> =>
pMemoize(async (point) => new Endpoint(await contractFactory(point), uln302Factory))
): EndpointV2Factory<EndpointV2, TOmniPoint | OmniPoint> =>
pMemoize(async (point) => new EndpointV2(await contractFactory(point), uln302Factory))
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MessageParams, MessagingFee, TimeoutSchema } from '@layerzerolabs/protocol-devtools'
import assert from 'assert'
import type {
IEndpoint,
IEndpointV2,
IUln302,
SetConfigParam,
Uln302ExecutorConfig,
Expand All @@ -28,11 +28,11 @@ const CONFIG_TYPE_EXECUTOR = 1
const CONFIG_TYPE_ULN = 2

/**
* EVM-specific SDK for EnvpointV2 contracts
* EVM-specific SDK for EndpointV2 contracts
*
* @implements {IEndpoint}
* @implements {IEndpointV2}
*/
export class Endpoint extends OmniSDK implements IEndpoint {
export class EndpointV2 extends OmniSDK implements IEndpointV2 {
constructor(
contract: OmniContract,
private readonly uln302Factory: Uln302Factory
Expand All @@ -45,7 +45,7 @@ export class Endpoint extends OmniSDK implements IEndpoint {

assert(
!isZero(address),
`Uln302 cannot be instantiated: Uln302 address cannot be a zero value for Endpoint ${formatOmniPoint(
`Uln302 cannot be instantiated: Uln302 address cannot be a zero value for EndpointV2 ${formatOmniPoint(
this.point
)}`
)
Expand Down Expand Up @@ -260,7 +260,7 @@ export class Endpoint extends OmniSDK implements IEndpoint {
}

/**
* @see {@link IEndpoint.hasAppExecutorConfig}
* @see {@link IEndpointV2.hasAppExecutorConfig}
*/
async hasAppExecutorConfig(
oapp: OmniAddress,
Expand Down Expand Up @@ -294,7 +294,7 @@ export class Endpoint extends OmniSDK implements IEndpoint {
}

/**
* @see {@link IEndpoint.hasAppUlnConfig}
* @see {@link IEndpointV2.hasAppUlnConfig}
*/
async hasAppUlnConfig(oapp: string, uln: OmniAddress, eid: EndpointId, config: Uln302UlnConfig): Promise<boolean> {
const ulnSdk = await this.getUln302SDK(uln)
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-devtools-evm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './dvn'
export * from './endpoint'
export * from './endpointv2'
export * from './executor'
export * from './priceFeed'
export * from './uln302'
6 changes: 3 additions & 3 deletions packages/protocol-devtools-evm/test/endpoint/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fc from 'fast-check'
import { endpointArbitrary, evmAddressArbitrary } from '@layerzerolabs/test-devtools'
import type { Contract } from '@ethersproject/contracts'
import { makeZeroAddress, type OmniContract } from '@layerzerolabs/devtools-evm'
import { Endpoint } from '@/endpoint'
import { EndpointV2 } from '../../src/endpointv2'
import { isZero, makeBytes32 } from '@layerzerolabs/devtools'

describe('endpoint/sdk', () => {
Expand All @@ -28,7 +28,7 @@ describe('endpoint/sdk', () => {
it('should reject if the address is a zeroish address', async () => {
await fc.assert(
fc.asyncProperty(omniContractArbitrary, zeroishAddressArbitrary, async (omniContract, address) => {
const sdk = new Endpoint(omniContract, uln302Factory)
const sdk = new EndpointV2(omniContract, uln302Factory)

await expect(sdk.getUln302SDK(address)).rejects.toThrow(
/Uln302 cannot be instantiated: Uln302 address cannot be a zero value for Endpoint/
Expand All @@ -48,7 +48,7 @@ describe('endpoint/sdk', () => {

uln302Factory.mockReset().mockResolvedValue(uln302Sdk)

const sdk = new Endpoint(omniContract, uln302Factory)
const sdk = new EndpointV2(omniContract, uln302Factory)
const uln302 = await sdk.getUln302SDK(address)

expect(uln302).toBe(uln302Sdk)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { flattenTransactions, type OmniTransaction, OmniPointMap, Bytes32 } from '@layerzerolabs/devtools'
import type { EndpointFactory, EndpointOmniGraph, IEndpoint } from './types'
import type { EndpointV2Factory, EndpointV2OmniGraph, IEndpointV2 } from './types'

export type EndpointConfigurator = (graph: EndpointOmniGraph, createSdk: EndpointFactory) => Promise<OmniTransaction[]>
export type EndpointV2Configurator = (
graph: EndpointV2OmniGraph,
createSdk: EndpointV2Factory
) => Promise<OmniTransaction[]>

export const configureEndpoint: EndpointConfigurator = async (graph, createSdk) =>
export const configureEndpointV2: EndpointV2Configurator = async (graph, createSdk) =>
flattenTransactions([
await configureEndpointRegisterLibraries(graph, createSdk),
await configureEndpointDefaultReceiveLibraries(graph, createSdk),
await configureEndpointDefaultSendLibraries(graph, createSdk),
await configureEndpointV2RegisterLibraries(graph, createSdk),
await configureEndpointV2DefaultReceiveLibraries(graph, createSdk),
await configureEndpointV2DefaultSendLibraries(graph, createSdk),
])

export const configureEndpointRegisterLibraries: EndpointConfigurator = async (graph, createSdk) => {
export const configureEndpointV2RegisterLibraries: EndpointV2Configurator = async (graph, createSdk) => {
const librariesByEndpoint = graph.connections.reduce(
(librariesByEndpoint, { vector: { from }, config }) =>
librariesByEndpoint.set(
Expand All @@ -34,7 +37,7 @@ export const configureEndpointRegisterLibraries: EndpointConfigurator = async (g
)
}

export const configureEndpointDefaultReceiveLibraries: EndpointConfigurator = async (graph, createSdk) =>
export const configureEndpointV2DefaultReceiveLibraries: EndpointV2Configurator = async (graph, createSdk) =>
flattenTransactions(
await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
Expand All @@ -55,7 +58,7 @@ export const configureEndpointDefaultReceiveLibraries: EndpointConfigurator = as
)
)

export const configureEndpointDefaultSendLibraries: EndpointConfigurator = async (graph, createSdk) =>
export const configureEndpointV2DefaultSendLibraries: EndpointV2Configurator = async (graph, createSdk) =>
flattenTransactions(
await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => {
Expand All @@ -70,7 +73,7 @@ export const configureEndpointDefaultSendLibraries: EndpointConfigurator = async
)
)

const registerLibraries = async (sdk: IEndpoint, libraries: string[]): Promise<OmniTransaction[]> =>
const registerLibraries = async (sdk: IEndpointV2, libraries: string[]): Promise<OmniTransaction[]> =>
flattenTransactions(
await Promise.all(
libraries.map(async (address) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type { IUln302, Uln302ExecutorConfig, Uln302UlnConfig } from '@/uln302/types'

export interface IEndpoint extends IOmniSDK {
export interface IEndpointV2 extends IOmniSDK {
getUln302SDK(address: OmniAddress): Promise<IUln302>

getDefaultReceiveLibrary(eid: EndpointId): Promise<OmniAddress | undefined>
Expand Down Expand Up @@ -206,15 +206,15 @@ export interface Timeout {
expiry: bigint
}

export interface EndpointEdgeConfig {
export interface EndpointV2EdgeConfig {
defaultReceiveLibrary: OmniAddress
defaultReceiveLibraryGracePeriod?: bigint
defaultSendLibrary: OmniAddress
}

export type EndpointOmniGraph = OmniGraph<unknown, EndpointEdgeConfig>
export type EndpointV2OmniGraph = OmniGraph<unknown, EndpointV2EdgeConfig>

export type EndpointFactory<TEndpoint extends IEndpoint = IEndpoint, TOmniPoint = OmniPoint> = Factory<
export type EndpointV2Factory<TEndpointV2 extends IEndpointV2 = IEndpointV2, TOmniPoint = OmniPoint> = Factory<
[TOmniPoint],
TEndpoint
TEndpointV2
>
2 changes: 1 addition & 1 deletion packages/protocol-devtools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './dvn'
export * from './endpoint'
export * from './endpointv2'
export * from './executor'
export * from './priceFeed'
export * from './uln302'
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { EndpointEdgeConfig, EndpointOmniGraph, IEndpoint, configureEndpointRegisterLibraries } from '@/endpoint'
import {
EndpointV2EdgeConfig,
EndpointV2OmniGraph,
IEndpointV2,
configureEndpointV2RegisterLibraries,
} from '@/endpointv2/index'
import { OmniPoint, arePointsEqual } from '@layerzerolabs/devtools'
import fc from 'fast-check'
import { addressArbitrary, pointArbitrary } from '@layerzerolabs/test-devtools'

describe('endpoint/config', () => {
describe('configureEndpointRegisterLibraries', () => {
describe('EndpointV2/config', () => {
describe('configureEndpointV2RegisterLibraries', () => {
// We'll create a simple mock SDK to work with library registration
class MockSDK {
constructor(readonly point: OmniPoint) {}
Expand All @@ -27,12 +32,12 @@ describe('endpoint/config', () => {
fc.pre(!arePointsEqual(pointB, pointC))
fc.pre(!arePointsEqual(pointC, pointA))

const config: EndpointEdgeConfig = {
const config: EndpointV2EdgeConfig = {
defaultReceiveLibrary: libraryAddress,
defaultSendLibrary: libraryAddress,
}

const graph: EndpointOmniGraph = {
const graph: EndpointV2OmniGraph = {
contracts: [],
connections: [
{
Expand Down Expand Up @@ -62,8 +67,8 @@ describe('endpoint/config', () => {
],
}

const createSdk = jest.fn((point: OmniPoint) => new MockSDK(point) as unknown as IEndpoint)
const result = await configureEndpointRegisterLibraries(graph, createSdk)
const createSdk = jest.fn((point: OmniPoint) => new MockSDK(point) as unknown as IEndpointV2)
const result = await configureEndpointV2RegisterLibraries(graph, createSdk)

// The result should not contain any duplicate library registrations
expect(result).toEqual([
Expand Down
Loading

0 comments on commit 6e464f2

Please sign in to comment.