Skip to content

Commit

Permalink
feat: options builder
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding committed Dec 11, 2023
1 parent 5fd7196 commit 0472d82
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 6 deletions.
21 changes: 21 additions & 0 deletions packages/omnicounter-utils-evm/src/omnicounter/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pMemoize from 'p-memoize'
import { OmniCounterApp } from '@/omnicounter/sdk'
import { createEndpointFactory } from '@layerzerolabs/protocol-utils-evm'
import { EndpointFactory } from '@layerzerolabs/protocol-utils/src/index'
import { OAppFactory } from '@layerzerolabs/ua-utils'
import { OmniContractFactory } from '@layerzerolabs/utils-evm'

/**
* Syntactic sugar that creates an instance of EVM `OmniCounterApp` SDK
* based on an `OmniPoint` with help of an `OmniContractFactory`
* and an (optional) `EndpointFactory`
*
* @param {OmniContractFactory} contractFactory
* @param {EndpointFactory} [endpointFactory]
* @returns {EndpointFactory<Endpoint>}
*/
export const createOmniCounterAppFactory = (
contractFactory: OmniContractFactory,
endpointFactory: EndpointFactory = createEndpointFactory(contractFactory)
): OAppFactory<OmniCounterApp> =>
pMemoize(async (point) => new OmniCounterApp(await contractFactory(point), endpointFactory))
1 change: 1 addition & 0 deletions packages/omnicounter-utils-evm/src/omnicounter/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './factory'
export * from './sdk'
9 changes: 9 additions & 0 deletions packages/omnicounter-utils-evm/src/omnicounter/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { IOmniCounterApp } from '@layerzerolabs/omnicounter-utils'
import { EndpointFactory } from '@layerzerolabs/protocol-utils'
import { OApp } from '@layerzerolabs/ua-utils-evm'
import { OmniTransaction } from '@layerzerolabs/utils'
import { OmniContract } from '@layerzerolabs/utils-evm'

export class OmniCounterApp extends OApp implements IOmniCounterApp {
public constructor(
public override contract: OmniContract,
protected override endpointFactory: EndpointFactory
) {
super(contract, endpointFactory)
}

public async increment(eid: number, type: number, options: string): Promise<OmniTransaction> {
const data = this.contract.contract.interface.encodeFunctionData('increment', [eid, type, options])
return super.createTransaction(data)
Expand Down
22 changes: 18 additions & 4 deletions packages/ua-utils-evm-hardhat-test/deploy/001_bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { type DeployFunction } from 'hardhat-deploy/types'
import assert from 'assert'
import { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
import { MainnetEndpointId } from '@layerzerolabs/lz-definitions'
import { formatEid } from '@layerzerolabs/utils'
import { wrapEIP1193Provider } from '@layerzerolabs/utils-evm-hardhat'
import env from 'hardhat'
import assert from 'assert'
import { Contract } from 'ethers'
import { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
import env from 'hardhat'
import { type DeployFunction } from 'hardhat-deploy/types'
import { avaxEndpoint, ethEndpoint } from '../test/__utils__/endpoint'

const DEFAULT_NATIVE_DECIMALS_RATE = '18' //ethers.utils.parseUnits('1', 18).toString()

Expand Down Expand Up @@ -103,6 +105,18 @@ const deploy: DeployFunction = async ({ getUnnamedAccounts, deployments, network
const setExecFeeLibReceipt: TransactionReceipt = await setExecFeeLibResp.wait()
assert(setExecFeeLibReceipt?.status === 1)

const setDstConfigTxResponse: TransactionResponse = await executorContract.setDstConfig?.([
{
dstEid: network.config.eid === MainnetEndpointId.ETHEREUM_MAINNET ? avaxEndpoint.eid : ethEndpoint.eid,
baseGas: 100,
multiplierBps: 0,
floorMarginUSD: 0,
nativeDropCap: 100,
},
])
const setDstConfigTxReceipt = await setDstConfigTxResponse.wait()
expect(setDstConfigTxReceipt?.status).toEqual(1)

await deployments.delete('DVN')
const dvn = await deployments.deploy('DVN', {
from: deployer,
Expand Down
2 changes: 1 addition & 1 deletion packages/ua-utils-evm-hardhat-test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 15000,
testTimeout: 60000,
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
Expand Down
2 changes: 2 additions & 0 deletions packages/ua-utils-evm-hardhat-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"@layerzerolabs/lz-evm-sdk-v1": "~1.5.70",
"@layerzerolabs/lz-evm-sdk-v2": "~1.5.70",
"@layerzerolabs/lz-utility-v2": "~1.5.70",
"@layerzerolabs/omnicounter-utils": "~0.0.1",
"@layerzerolabs/omnicounter-utils-evm": "~0.0.1",
"@layerzerolabs/protocol-utils": "~0.0.1",
"@layerzerolabs/protocol-utils-evm": "~0.0.1",
"@layerzerolabs/toolbox-hardhat": "~0.0.1",
Expand Down
83 changes: 83 additions & 0 deletions packages/ua-utils-evm-hardhat-test/test/oapp/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { TransactionReceipt } from '@ethersproject/providers'
import { EndpointId, MainnetEndpointId } from '@layerzerolabs/lz-definitions'
import { Options } from '@layerzerolabs/lz-utility-v2'
import { createOmniCounterAppFactory } from '@layerzerolabs/omnicounter-utils-evm'
import { configureOApp } from '@layerzerolabs/ua-utils'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'
import {
createConnectedContractFactory,
createSignerFactory,
OmniGraphBuilderHardhat,
OmniGraphHardhat,
} from '@layerzerolabs/utils-evm-hardhat'
import { setupDefaultEndpoint } from '../__utils__/endpoint'
import { deployOApp } from '../__utils__/oapp'

describe('oapp/options', () => {
const ethContract = { eid: EndpointId.ETHEREUM_MAINNET, contractName: 'DefaultOApp' }
const avaxContract = { eid: EndpointId.AVALANCHE_MAINNET, contractName: 'DefaultOApp' }

// This is the OApp config that we want to use against our contracts
const config: OmniGraphHardhat = {
contracts: [
{
contract: ethContract,
},
{
contract: avaxContract,
},
],
connections: [
{
from: ethContract,
to: avaxContract,
},
{
from: avaxContract,
to: ethContract,
},
],
}

beforeEach(async () => {
await setupDefaultEndpoint()
await deployOApp()
})

it('lzReceive option', async () => {
const contractFactory = createConnectedContractFactory()
const builder = await OmniGraphBuilderHardhat.fromConfig(config)
const sdkFactory = createOmniCounterAppFactory(contractFactory)
const signerFactory = createSignerFactory()

// And finally the test assertions
const ethPoint = omniContractToPoint(await contractFactory(ethContract))
const ethSdk = await sdkFactory(ethPoint)
const ethSigner = await signerFactory(ethContract.eid)

const avaxPoint = omniContractToPoint(await contractFactory(avaxContract))
const avaxSdk = await sdkFactory(avaxPoint)
const avaxSigner = await signerFactory(avaxContract.eid)

const transactions = await configureOApp(builder.graph, sdkFactory)
for (const transaction of transactions) {
const signer = transaction.point.eid === MainnetEndpointId.ETHEREUM_MAINNET ? ethSigner : avaxSigner
const txResponse = await signer.signAndSend(transaction)
const txReceipt: TransactionReceipt = await txResponse.wait()
expect(txReceipt.status).toBe(1)
}

expect(await ethSdk.hasPeer(avaxPoint.eid, avaxPoint.address)).toBe(true)
expect(await avaxSdk.hasPeer(ethPoint.eid, ethPoint.address)).toBe(true)

const options = Options.newOptions().addExecutorLzReceiveOption(200000)
const incrementTx = {
...(await ethSdk.increment(avaxPoint.eid, 1, options.toHex())),
gasLimit: 2000000,
value: 1000000000000000,
}
const incrementTxResponse = await ethSigner.signAndSend(incrementTx)
const incrementTxReceipt: TransactionReceipt = await incrementTxResponse.wait()
expect(incrementTxReceipt.status).toEqual(1)
})
})
2 changes: 1 addition & 1 deletion packages/ua-utils-evm/src/oapp/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { OmniSDK } from '@layerzerolabs/utils-evm'
export class OApp extends OmniSDK implements IOApp {
constructor(
contract: OmniContract,
private readonly endpointFactory: EndpointFactory
protected readonly endpointFactory: EndpointFactory
) {
super(contract)
}
Expand Down

0 comments on commit 0472d82

Please sign in to comment.