Skip to content

Commit

Permalink
chore: Move omnigraph stuff to utils-evm-hardhat; Use jest instead of…
Browse files Browse the repository at this point in the history
… mocha
  • Loading branch information
janjakubnanista committed Dec 1, 2023
1 parent 1dee57c commit 6deb870
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 98 deletions.
2 changes: 1 addition & 1 deletion packages/ua-utils-evm-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"build": "npx tsup",
"clean": "rm -rf dist",
"lint": "npx eslint '**/*.{js,ts,json}'",
"test": "jest"
"test": "jest --passWithNoTests"
},
"devDependencies": {
"@ethersproject/contracts": "^5.7.0",
Expand Down
1 change: 0 additions & 1 deletion packages/ua-utils-evm-hardhat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export * from './omnigraph'
10 changes: 6 additions & 4 deletions packages/utils-evm-hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { HardhatUserConfig } from 'hardhat/types'
const config: HardhatUserConfig = {
networks: {
'ethereum-mainnet': {
url: 'https://eth.llamarpc.com',
saveDeployments: true,
url: 'no:///way',
saveDeployments: false,
eid: EndpointId.ETHEREUM_MAINNET,
},
'ethereum-testnet': {
url: 'https://eth-goerli.public.blastapi.io',
url: 'no:///way',
saveDeployments: false,
eid: EndpointId.ETHEREUM_TESTNET,
},
'bsc-testnet': {
url: 'https://bsc-testnet.publicnode.com',
url: 'no:///way',
saveDeployments: false,
accounts: {
mnemonic: 'test test test test test test test test test test test junk',
},
Expand Down
8 changes: 8 additions & 0 deletions packages/utils-evm-hardhat/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
};
8 changes: 4 additions & 4 deletions packages/utils-evm-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"clean": "rm -rf dist",
"dev": "npx tsup --watch",
"lint": "npx eslint '**/*.{js,ts,json}'",
"test": "npx hardhat test"
"test": "jest"
},
"dependencies": {
"micro-memoize": "~4.1.2",
Expand All @@ -51,14 +51,14 @@
"@layerzerolabs/test-utils": "~0.0.1",
"@layerzerolabs/utils-evm": "~0.0.1",
"@types/chai-as-promised": "^7.1.7",
"@types/mocha": "^10.0.6",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"@types/jest": "^29.5.10",
"fast-check": "^3.14.0",
"hardhat": "^2.19.0",
"hardhat-deploy": "^0.11.22",
"jest": "^29.7.0",
"p-memoize": "~4.0.1",
"sinon": "^17.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsup": "~8.0.1",
"typescript": "^5.2.2",
Expand Down
1 change: 1 addition & 0 deletions packages/utils-evm-hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './type-extensions'
export * from './config'
export * from './internal'
export * from './logger'
export * from './omnigraph'
export * from './provider'
export * from './runtime'
export * from './signer'
Expand Down
5 changes: 3 additions & 2 deletions packages/utils-evm-hardhat/src/internal/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export interface HardhatRuntimeEnvironmentWithDeployments extends HardhatRuntime
deployments: DeploymentsExtension
}

export const assertHardhatDeploy = (
export function assertHardhatDeploy(
hre: HardhatRuntimeEnvironment
): asserts hre is HardhatRuntimeEnvironmentWithDeployments =>
): asserts hre is HardhatRuntimeEnvironmentWithDeployments {
assert(hre.deployments, `You don't seem to be using hardhat-deploy in your project`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { OmniContractFactory, OmniGraphHardhat } from './types'
import { OmniGraphBuilder } from '@layerzerolabs/utils'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'

export class OmniGraphBuilderHardhat<TNodeConfig, TEdgeConfig> extends OmniGraphBuilder<TNodeConfig, TEdgeConfig> {
export class OmniGraphBuilderHardhat {
static async fromConfig<TNodeConfig, TEdgeConfig>(
graph: OmniGraphHardhat<TNodeConfig, TEdgeConfig>,
contractFactory: OmniContractFactory
): Promise<OmniGraphBuilderHardhat<TNodeConfig, TEdgeConfig>> {
const builder = new OmniGraphBuilderHardhat<TNodeConfig, TEdgeConfig>()
): Promise<OmniGraphBuilder<TNodeConfig, TEdgeConfig>> {
const builder = new OmniGraphBuilder<TNodeConfig, TEdgeConfig>()

const nodes: OmniNode<TNodeConfig>[] = await Promise.all(
graph.contracts.map(async ({ contract, config }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import { OmniContract } from '@layerzerolabs/utils-evm'
import { Contract } from '@ethersproject/contracts'
import assert from 'assert'
import { OmniContractFactory } from './types'
import {
assertHardhatDeploy,
createNetworkEnvironmentFactory,
getDefaultRuntimeEnvironment,
} from '@layerzerolabs/utils-evm-hardhat'
import { createNetworkEnvironmentFactory, getDefaultRuntimeEnvironment } from '@/runtime'
import { assertHardhatDeploy } from '@/internal/assertions'

export interface OmniDeployment {
eid: EndpointId
Expand All @@ -33,11 +30,6 @@ export const createContractFactory = (
const env = await environmentFactory(eid)
assertHardhatDeploy(env)

assert(
contractName != null || address != null,
'At least one of contractName, address must be specified for OmniPointHardhat'
)

// If we have both the contract name & address, we go off artifacts
if (contractName != null && address != null) {
const artifact = await env.deployments.getArtifact(contractName)
Expand All @@ -49,17 +41,19 @@ export const createContractFactory = (
// If we have the contract name but no address, we need to get it from the deployments by name
if (contractName != null && address == null) {
const deployment = await env.deployments.getOrNull(contractName)
assert(deployment != null, `Could not find a deployment for contract '${contractName}`)
assert(deployment != null, `Could not find a deployment for contract '${contractName}'`)

return omniDeploymentToContract({ eid, deployment })
}

// And if we only have the address, we need to go get it from deployments by address
if (address != null) {
const [deployment] = await env.deployments.getDeploymentsFromAddress(address)
assert(deployment != null, `Could not find a deployment for address '${address}`)
assert(deployment != null, `Could not find a deployment for address '${address}'`)

return omniDeploymentToContract({ eid, deployment })
}

assert(false, 'At least one of contractName, address must be specified for OmniPointHardhat')
})
}
16 changes: 7 additions & 9 deletions packages/utils-evm-hardhat/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { expect } from 'chai'
import { describe } from 'mocha'
import { withLayerZeroArtifacts, withLayerZeroDeployments } from '../src/config'
import { dirname, join } from 'path'

Expand All @@ -13,7 +11,7 @@ describe('config', () => {
it('should add no external deployments if no networks have been specified', () => {
const config = {}

expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({
expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({
external: {
deployments: {},
},
Expand All @@ -27,7 +25,7 @@ describe('config', () => {
},
}

expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({
expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({
networks: {
'vengaboys-testnet': {},
},
Expand All @@ -46,7 +44,7 @@ describe('config', () => {
},
}

expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({
expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({
networks: {
'vengaboys-testnet': {
eid: 0,
Expand All @@ -67,7 +65,7 @@ describe('config', () => {
},
}

expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({
expect(withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({
networks: {
'vengaboys-testnet': {
eid: EndpointId.ARBITRUM_MAINNET,
Expand Down Expand Up @@ -96,7 +94,7 @@ describe('config', () => {
)(config)
const configWithSomePathAgain = withLayerZeroDeployments('@layerzerolabs/lz-evm-sdk-v1')(configWithSomePath)

expect(configWithSomePathAgain).to.eql({
expect(configWithSomePathAgain).toEqual({
networks: {
'vengaboys-testnet': {
eid: EndpointId.BSC_TESTNET,
Expand All @@ -121,7 +119,7 @@ describe('config', () => {
networks: {},
}

expect(withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(config)).to.eql({
expect(withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(config)).toEqual({
networks: {},
external: {
contracts: [
Expand Down Expand Up @@ -154,7 +152,7 @@ describe('config', () => {
)(config)
const configWithSomePathAgain = withLayerZeroArtifacts('@layerzerolabs/lz-evm-sdk-v1')(configWithSomePath)

expect(configWithSomePathAgain).to.eql({
expect(configWithSomePathAgain).toEqual({
networks: {},
external: {
contracts: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fc from 'fast-check'
import hre from 'hardhat'
import { DeploymentsManager } from 'hardhat-deploy/dist/src/DeploymentsManager'
import { Deployment, DeploymentSubmission } from 'hardhat-deploy/dist/types'
import { endpointArbitrary, evmAddressArbitrary } from '@layerzerolabs/test-utils'
import { OmniDeployment, createContractFactory, omniDeploymentToContract, omniDeploymentToPoint } from '@/omnigraph'
Expand All @@ -9,6 +10,8 @@ import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { Contract } from '@ethersproject/contracts'
import { makeZero } from '@layerzerolabs/utils-evm'

jest.spyOn(DeploymentsManager.prototype, 'getChainId').mockResolvedValue('1')

describe('omnigraph/coordinates', () => {
describe('omniDeploymentToPoint', () => {
it('should just work', () => {
Expand Down Expand Up @@ -39,47 +42,35 @@ describe('omnigraph/coordinates', () => {
})

describe('createContractFactory', () => {
// Hardhat deploy will try to get the chain ID from the RPC so we can't let it
const mockSend = (env: HardhatRuntimeEnvironment) => {
env.network.provider.send = jest.fn().mockResolvedValue(1)
}

describe('when called with OmniPointContractName', () => {
it('should reject when eid does not exist', async () => {
const deploymentFactory = createContractFactory(hre)
const contractFactory = createContractFactory(hre)

await expect(() =>
deploymentFactory({ eid: EndpointId.CANTO_TESTNET, contractName: 'MyContract' })
contractFactory({ eid: EndpointId.CANTO_TESTNET, contractName: 'MyContract' })
).rejects.toBeTruthy()
})

it('should reject when contract has not been deployed', async () => {
const environmentFactory = createNetworkEnvironmentFactory(hre)
const deploymentFactory = createContractFactory(hre)

const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
mockSend(env)
const contractFactory = createContractFactory(hre)

await expect(() =>
deploymentFactory({ eid: EndpointId.ETHEREUM_MAINNET, contractName: 'MyContract' })
contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, contractName: 'MyContract' })
).rejects.toBeTruthy()
})

it('should resolve when contract has been deployed', async () => {
const environmentFactory = createNetworkEnvironmentFactory(hre)
const deploymentFactory = createContractFactory(hre)
const contractFactory = createContractFactory(hre, environmentFactory)

const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
mockSend(env)

// We'll create a dummy deployment first
await env.deployments.save('MyContract', {
jest.spyOn(env.deployments, 'getOrNull').mockResolvedValue({
address: makeZero(undefined),
abi: [],
} as DeploymentSubmission)
})

// Then check whether the factory will get it for us
const deployment = await deploymentFactory({
const deployment = await contractFactory({
eid: EndpointId.ETHEREUM_MAINNET,
contractName: 'MyContract',
})
Expand All @@ -88,17 +79,18 @@ describe('omnigraph/coordinates', () => {
eid: EndpointId.ETHEREUM_MAINNET,
contract: expect.any(Contract),
})
expect(env.deployments.getOrNull).toHaveBeenCalledWith('MyContract')
})
})

describe('when called with OmniPoint', () => {
it('should reject when eid does not exist', async () => {
await fc.assert(
fc.asyncProperty(evmAddressArbitrary, async (address) => {
const deploymentFactory = createContractFactory(hre)
const contractFactory = createContractFactory(hre)

await expect(() =>
deploymentFactory({ eid: EndpointId.CANTO_TESTNET, address })
contractFactory({ eid: EndpointId.CANTO_TESTNET, address })
).rejects.toBeTruthy()
})
)
Expand All @@ -107,11 +99,11 @@ describe('omnigraph/coordinates', () => {
it('should reject when contract has not been deployed', async () => {
await fc.assert(
fc.asyncProperty(evmAddressArbitrary, async (address) => {
const deploymentFactory = createContractFactory(hre)
const contractFactory = createContractFactory(hre)

await expect(() =>
deploymentFactory({ eid: EndpointId.ETHEREUM_MAINNET, address })
).rejects.toBeTruthy()
contractFactory({ eid: EndpointId.ETHEREUM_MAINNET, address })
).rejects.toThrow(/Could not find a deployment for address/)
})
)
})
Expand All @@ -120,16 +112,18 @@ describe('omnigraph/coordinates', () => {
await fc.assert(
fc.asyncProperty(evmAddressArbitrary, async (address) => {
const environmentFactory = createNetworkEnvironmentFactory(hre)
const deploymentFactory = createContractFactory(hre)
const contractFactory = createContractFactory(hre, environmentFactory)

const env = await environmentFactory(EndpointId.ETHEREUM_MAINNET)
mockSend(env)

// We'll create a dummy deployment with the specified address first
await env.deployments.save('MyContract', { address, abi: [] } as DeploymentSubmission)
jest.spyOn(env.deployments, 'getDeploymentsFromAddress').mockResolvedValue([
{
address: makeZero(undefined),
abi: [],
},
])

// Then check whether the factory will get it for us
const deployment = await deploymentFactory({
const deployment = await contractFactory({
eid: EndpointId.ETHEREUM_MAINNET,
address,
})
Expand All @@ -138,6 +132,7 @@ describe('omnigraph/coordinates', () => {
eid: EndpointId.ETHEREUM_MAINNET,
contract: expect.any(Contract),
})
expect(env.deployments.getOrNull).toHaveBeenCalledWith('MyContract')
})
)
})
Expand Down
Loading

0 comments on commit 6deb870

Please sign in to comment.