Skip to content

Commit

Permalink
🪚 OmniGraph™ Update packages & switch to Jest for better testing (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 1, 2023
1 parent 1dee57c commit 0abd315
Show file tree
Hide file tree
Showing 25 changed files with 158 additions and 133 deletions.
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"fast-check": "^3.14.0",
"ts-node": "^10.9.1",
"tslib": "~2.6.2",
"tsup": "^7.2.0",
"tsup": "~8.0.1",
"typescript": "^5.2.2"
},
"peerDependencies": {
Expand Down
9 changes: 7 additions & 2 deletions packages/ua-utils-evm-hardhat-test/test/oapp/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { configureOApp } from '@layerzerolabs/ua-utils'
import { OApp } from '@layerzerolabs/ua-utils-evm'
import { createContractFactory, OmniGraphHardhat, OmniGraphBuilderHardhat } from '@layerzerolabs/ua-utils-evm-hardhat'
import {
createContractFactory,
createProviderFactory,
createSignerFactory,
OmniGraphBuilderHardhat,
} from '@layerzerolabs/utils-evm-hardhat'
import type { OmniGraphHardhat } from '@layerzerolabs/utils-evm-hardhat'
import type { OmniPoint } from '@layerzerolabs/utils'
import { omniContractToPoint, connectOmniContract } from '@layerzerolabs/utils-evm'
import { createProviderFactory, createSignerFactory } from '@layerzerolabs/utils-evm-hardhat'
import { expect } from 'chai'
import { describe } from 'mocha'
import { EndpointId } from '@layerzerolabs/lz-definitions'
Expand Down
1 change: 1 addition & 0 deletions packages/ua-utils-evm-hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'hardhat-deploy'
import '@layerzerolabs/utils-evm-hardhat'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { HardhatUserConfig } from 'hardhat/types'

Expand Down
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'
2 changes: 1 addition & 1 deletion packages/ua-utils-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "~2.6.2",
"tsup": "^7.2.0",
"tsup": "~8.0.1",
"typescript": "^5.2.2",
"zod": "^3.22.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ua-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "~2.6.2",
"tsup": "^7.2.0",
"tsup": "~8.0.1",
"typescript": "^5.2.2",
"zod": "^3.22.4"
},
Expand Down
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',
},
};
9 changes: 4 additions & 5 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 @@ -50,15 +50,14 @@
"@layerzerolabs/lz-evm-sdk-v1": "~1.5.65",
"@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 @@ -2,13 +2,19 @@ import type { OmniEdge, OmniNode } from '@layerzerolabs/utils'
import type { OmniContractFactory, OmniGraphHardhat } from './types'
import { OmniGraphBuilder } from '@layerzerolabs/utils'
import { omniContractToPoint } from '@layerzerolabs/utils-evm'
import assert from 'assert'

export class OmniGraphBuilderHardhat<TNodeConfig, TEdgeConfig> extends OmniGraphBuilder<TNodeConfig, TEdgeConfig> {
/**
* OmniGraphBuilderHardhat houses all hardhat-specific utilities for building OmniGraphs
*
* It is not an instantiable class - it only provides static utilities for working with OmniGraph
*/
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 All @@ -29,4 +35,11 @@ export class OmniGraphBuilderHardhat<TNodeConfig, TEdgeConfig> extends OmniGraph

return builder.addNodes(...nodes).addEdges(...edges)
}

constructor() {
assert(
false,
'OmniGraphBuilderHardhat cannot be instantiated - it only provides static utilities for working with OmniGraph'
)
}
}
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')
})
}
18 changes: 8 additions & 10 deletions packages/utils-evm-hardhat/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { expect } from 'chai'
import { describe } from 'mocha'
import { withLayerZeroArtifacts, withLayerZeroDeployments } from '../src/config'
import { withLayerZeroArtifacts, withLayerZeroDeployments } from '@/config'
import { dirname, join } from 'path'

describe('config', () => {
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
9 changes: 9 additions & 0 deletions packages/utils-evm-hardhat/test/omnigraph/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OmniGraphBuilderHardhat } from '@/omnigraph/builder'

describe('omnigraph/builder', () => {
it('should not allow instantiation', () => {
expect(() => new OmniGraphBuilderHardhat()).toThrow(
/OmniGraphBuilderHardhat cannot be instantiated - it only provides static utilities for working with OmniGraph/
)
})
})
Loading

0 comments on commit 0abd315

Please sign in to comment.