Skip to content

Commit

Permalink
feat: options builder (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding authored Jan 5, 2024
1 parent abbb813 commit 4a97fe2
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 26 deletions.
27 changes: 22 additions & 5 deletions packages/omnicounter-devtools-evm/src/omnicounter/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IOmniCounter } from '@layerzerolabs/omnicounter-devtools'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { IncrementOutput, IncrementType, IOmniCounter } from '@layerzerolabs/omnicounter-devtools'
import { EndpointFactory } from '@layerzerolabs/protocol-devtools'
import { OApp } from '@layerzerolabs/ua-devtools-evm'
import { OmniTransaction } from '@layerzerolabs/devtools'
import { OmniContract } from '@layerzerolabs/devtools-evm'
import { Address } from '@layerzerolabs/devtools'
import { makeBytes32, OmniContract } from '@layerzerolabs/devtools-evm'

export class OmniCounter extends OApp implements IOmniCounter {
public constructor(
Expand All @@ -12,8 +13,24 @@ export class OmniCounter extends OApp implements IOmniCounter {
super(contract, endpointFactory)
}

public async increment(eid: number, type: number, options: string): Promise<OmniTransaction> {
public async increment(
eid: EndpointId,
type: IncrementType,
options: Uint8Array,
receiver: Address
): Promise<IncrementOutput> {
const data = this.contract.contract.interface.encodeFunctionData('increment', [eid, type, options])
return super.createTransaction(data)
const endpointSdk = await super.getEndpointSDK()
const messagingFee = await endpointSdk.quote(
{ dstEid: eid, options, message: data, receiver: makeBytes32(receiver), payInLzToken: false },
this.contract.contract.address
)
const gasLimit = (await this.contract.contract.estimateGas.increment!(eid, type, options)).toBigInt()

return {
omniTransaction: super.createTransaction(data),
messagingFee,
gasLimit,
}
}
}
1 change: 1 addition & 0 deletions packages/omnicounter-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"devDependencies": {
"@layerzerolabs/devtools": "~0.0.1",
"@layerzerolabs/lz-definitions": "~2.0.7",
"@layerzerolabs/protocol-devtools": "~0.0.1",
"@types/jest": "^29.5.11",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
Expand Down
18 changes: 16 additions & 2 deletions packages/omnicounter-devtools/src/omnicounter/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { OmniTransaction } from '@layerzerolabs/devtools'
import { Address, OmniTransaction } from '@layerzerolabs/devtools'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { MessagingFee } from '@layerzerolabs/protocol-devtools'

export type IncrementOutput = {
omniTransaction: OmniTransaction
messagingFee: MessagingFee
gasLimit: bigint
}

export enum IncrementType {
VANILLA_TYPE = 1,
COMPOSED_TYPE = 2,
ABA_TYPE = 3,
COMPOSED_ABA_TYPE = 4,
}

export interface IOmniCounter {
increment(eid: EndpointId, type: number, options: string): Promise<OmniTransaction>
increment(eid: EndpointId, type: IncrementType, options: Uint8Array, receiver: Address): Promise<IncrementOutput>
}
9 changes: 9 additions & 0 deletions packages/protocol-devtools-evm/src/endpoint/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MessageParams, MessagingFee } from '@layerzerolabs/protocol-devtools'
import assert from 'assert'
import type {
IEndpoint,
Expand Down Expand Up @@ -198,4 +199,12 @@ export class Endpoint extends OmniSDK implements IEndpoint {
description: `Registering library ${lib}`,
}
}

public async quote(params: MessageParams, sender: Address): Promise<MessagingFee> {
const { nativeFee, lzTokenFee } = await this.contract.contract.quote(params, sender)
return {
nativeFee: BigInt(nativeFee),
lzTokenFee: BigInt(lzTokenFee),
}
}
}
15 changes: 15 additions & 0 deletions packages/protocol-devtools/src/endpoint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export interface IEndpoint extends IOmniSDK {

getUlnConfig(oapp: Address, lib: Address, eid: EndpointId): Promise<Uln302UlnConfig>
setUlnConfig(oapp: Address, lib: Address, setUlnConfig: Uln302SetUlnConfig[]): Promise<OmniTransaction>

quote(params: MessageParams, sender: Address): Promise<MessagingFee>
}

export type Uln302SetExecutorConfig = { eid: EndpointId; executorConfig: Uln302ExecutorConfig }
Expand All @@ -59,6 +61,19 @@ export interface SetConfigParam {
config: string
}

export interface MessageParams {
dstEid: EndpointId
receiver: Address
message: string | Uint8Array
options: string | Uint8Array
payInLzToken: boolean
}

export interface MessagingFee {
nativeFee: bigint
lzTokenFee: bigint
}

export interface Timeout {
lib: string
expiry: number
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions tests/ua-devtools-evm-hardhat-test/deploy/001_bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { type DeployFunction } from 'hardhat-deploy/types'
import { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'
import { formatEid } from '@layerzerolabs/devtools'
import { wrapEIP1193Provider } from '@layerzerolabs/devtools-evm-hardhat'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import assert from 'assert'
import { Contract } from 'ethers'
import { BigNumber, Contract } from 'ethers'
import { parseEther } from 'ethers/lib/utils'
import { type DeployFunction } from 'hardhat-deploy/types'
import { HardhatRuntimeEnvironment } from 'hardhat/types'

const DEFAULT_NATIVE_DECIMALS_RATE = '18' //ethers.utils.parseUnits('1', 18).toString()
Expand All @@ -28,6 +30,13 @@ const deploy: DeployFunction = async ({ getUnnamedAccounts, deployments, network
assert(deployer, 'Missing deployer')
const signer = wrapEIP1193Provider(network.provider).getSigner()

// TODO: move price configuration to a separate sdk and make bootstrap of the configuration optional
const dstEid = BigNumber.from(
network.config.eid === EndpointId.ETHEREUM_V2_MAINNET
? EndpointId.AVALANCHE_V2_MAINNET
: EndpointId.ETHEREUM_V2_MAINNET
)

await deployments.delete('EndpointV2')
const endpointV2Deployment = await deployments.deploy('EndpointV2', {
from: deployer,
Expand Down Expand Up @@ -78,6 +87,19 @@ const deploy: DeployFunction = async ({ getUnnamedAccounts, deployments, network
},
},
})
const priceFeedContract = new Contract(priceFeed.address, priceFeed.abi).connect(signer)
const setPriceResp: TransactionResponse = await priceFeedContract.setPrice([
{
eid: dstEid,
price: {
priceRatio: '100000000000000000000',
gasPriceInUnit: 1,
gasPerByte: 1,
},
},
])
const setPriceReceipt = await setPriceResp.wait()
assert(setPriceReceipt?.status === 1)

await deployments.delete('ExecutorFeeLib')
const executorFeeLib = await deployments.deploy('ExecutorFeeLib', {
Expand Down Expand Up @@ -128,6 +150,23 @@ const deploy: DeployFunction = async ({ getUnnamedAccounts, deployments, network
'Executor worker fee lib not set correctly'
)

const nativeCap = parseEther('0.25')
const baseGas = BigNumber.from(200000)
const setDstConfigResp: TransactionResponse = await executorContract.setDstConfig([
{
dstEid,
baseGas,
multiplierBps: BigNumber.from(0),
floorMarginUSD: BigNumber.from(0),
nativeCap,
},
])
const setDstConfigReceipt: TransactionReceipt = await setDstConfigResp.wait()
assert(setDstConfigReceipt?.status === 1)
const polledDstConfig = await executorContract.dstConfig(dstEid)
assert(BigNumber.from(polledDstConfig[0]).eq(baseGas), 'Executor dst config baseGas not set correctly')
assert(BigNumber.from(polledDstConfig[3]).eq(nativeCap), 'Executor dst config nativeCap not set correctly')

await deployments.delete('DVN')
const dvn = await deployments.deploy('DVN', {
from: deployer,
Expand Down
2 changes: 1 addition & 1 deletion tests/ua-devtools-evm-hardhat-test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
preset: 'ts-jest',
cache: false,
testEnvironment: 'node',
testTimeout: 15000,
testTimeout: 150000,
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
Expand Down
3 changes: 3 additions & 0 deletions tests/ua-devtools-evm-hardhat-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@layerzerolabs/lz-evm-sdk-v1": "~2.0.7",
"@layerzerolabs/lz-evm-sdk-v2": "~2.0.7",
"@layerzerolabs/lz-utility-v2": "~2.0.7",
"@layerzerolabs/omnicounter-devtools": "~0.0.1",
"@layerzerolabs/omnicounter-devtools-evm": "~0.0.1",
"@layerzerolabs/protocol-devtools": "~0.0.1",
"@layerzerolabs/protocol-devtools-evm": "~0.0.1",
"@layerzerolabs/toolbox-hardhat": "~0.0.1",
Expand All @@ -41,6 +43,7 @@
"@openzeppelin/contracts": "^4.9.5",
"@types/jest": "^29.5.11",
"ethers": "^5.7.0",
"fast-check": "^3.15.0",
"hardhat": "^2.19.4",
"hardhat-deploy": "^0.11.45",
"jest": "^29.7.0",
Expand Down
11 changes: 0 additions & 11 deletions tests/ua-devtools-evm-hardhat-test/test/__utils__/omnicounter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { createGetHreByEid } from '@layerzerolabs/devtools-evm-hardhat'

export const deployOmniCounter = async () => {
const environmentFactory = createGetHreByEid()
const eth = await environmentFactory(EndpointId.ETHEREUM_V2_MAINNET)
const avax = await environmentFactory(EndpointId.AVALANCHE_V2_MAINNET)

await Promise.all([
eth.deployments.run('OmniCounter', { writeDeploymentsToFiles: true }),
avax.deployments.run('OmniCounter', { writeDeploymentsToFiles: true }),
])
}

export const deployOmniCounterFixture = async () => {
const environmentFactory = createGetHreByEid()
const eth = await environmentFactory(EndpointId.ETHEREUM_V2_MAINNET)
Expand Down
10 changes: 5 additions & 5 deletions tests/ua-devtools-evm-hardhat-test/test/oapp/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { OAppEdgeConfig } from '@layerzerolabs/ua-devtools'
import { OmniTransaction } from '@layerzerolabs/devtools'

type OAppTestConfig = {
export type OAppTestConfig = {
sendLibrary: string
receiveLibrary: string
executorLibrary: string
Expand Down Expand Up @@ -204,7 +204,7 @@ const getLibraryAddress = async (library: OmniPointHardhat): Promise<string> =>
return executorPoint.address
}

const setUpConfig = async (testConfig: OAppTestConfig): Promise<OAppEdgeConfig> => {
export const setUpConfig = async (testConfig: OAppTestConfig): Promise<OAppEdgeConfig> => {
return {
sendLibrary: testConfig.sendLibrary,
receiveLibraryConfig: {
Expand Down Expand Up @@ -242,7 +242,7 @@ const setUpConfig = async (testConfig: OAppTestConfig): Promise<OAppEdgeConfig>
}
}

const setUpOmniGraphHardhat = (
export const setUpOmniGraphHardhat = (
ethContract: OmniPointHardhat,
ethOAppConfig: OAppEdgeConfig,
avaxContract,
Expand Down Expand Up @@ -272,7 +272,7 @@ const setUpOmniGraphHardhat = (
}
}

const getDefaultEthConfig = async (): Promise<OAppTestConfig> => {
export const getDefaultEthConfig = async (): Promise<OAppTestConfig> => {
const ethDVNAddress = await getLibraryAddress(ethDvn)
const avaxDvnPoint = await getLibraryAddress(avaxDvn)
const ethSendUlnRequiredDVNs: string[] = [avaxDvnPoint]
Expand All @@ -299,7 +299,7 @@ const getDefaultEthConfig = async (): Promise<OAppTestConfig> => {
}
}

const getDefaultAvaxConfig = async (): Promise<OAppTestConfig> => {
export const getDefaultAvaxConfig = async (): Promise<OAppTestConfig> => {
const ethDVNAddress = await getLibraryAddress(ethDvn)
const avaxDvnPoint = await getLibraryAddress(avaxDvn)
const avaxSendUlnRequiredDVNs: string[] = [ethDVNAddress]
Expand Down
Loading

0 comments on commit 4a97fe2

Please sign in to comment.