Skip to content

Commit

Permalink
chore: Support applicatives in createOmniGraphHardhatTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 13, 2023
1 parent 39c9673 commit 64dd4e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
10 changes: 6 additions & 4 deletions packages/utils-evm-hardhat/src/omnigraph/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
OmniNodeHardhat,
OmniPointHardhatTransformer,
} from './types'
import { parallel } from '@layerzerolabs/utils'

/**
* Create a function capable of transforming `OmniPointHardhat` to a regular `OmniPoint`
Expand Down Expand Up @@ -56,9 +57,10 @@ export const createOmniEdgeHardhatTransformer =
export const createOmniGraphHardhatTransformer =
<TNodeConfig, TEdgeConfig>(
nodeTransformer = createOmniNodeHardhatTransformer(),
edgeTransformer = createOmniEdgeHardhatTransformer()
edgeTransformer = createOmniEdgeHardhatTransformer(),
applicative = parallel
): OmniGraphHardhatTransformer<TNodeConfig, TEdgeConfig> =>
async (graph) => ({
contracts: await Promise.all(graph.contracts.map(nodeTransformer)),
connections: await Promise.all(graph.connections.map(edgeTransformer)),
async ({ contracts, connections }) => ({
contracts: await applicative(contracts.map((contract) => () => nodeTransformer(contract))),
connections: await applicative(connections.map((connection) => () => edgeTransformer(connection))),
})
52 changes: 40 additions & 12 deletions packages/utils-evm-hardhat/test/omnigraph/transformations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@/omnigraph/transformations'
import { Contract } from '@ethersproject/contracts'
import { endpointArbitrary, evmAddressArbitrary, nullableArbitrary, pointArbitrary } from '@layerzerolabs/test-utils'
import { isOmniPoint } from '@layerzerolabs/utils'
import { isOmniPoint, parallel, sequence } from '@layerzerolabs/utils'

describe('omnigraph/transformations', () => {
const pointHardhatArbitrary = fc.record({
Expand All @@ -17,6 +17,17 @@ describe('omnigraph/transformations', () => {
address: nullableArbitrary(evmAddressArbitrary),
})

const nodeHardhatArbitrary = fc.record({
contract: pointHardhatArbitrary,
config: fc.anything(),
})

const edgeHardhatArbitrary = fc.record({
from: pointHardhatArbitrary,
to: pointHardhatArbitrary,
config: fc.anything(),
})

describe('createOmniPointHardhatTransformer', () => {
it('should pass the original value if contract is already an OmniPoint', async () => {
await fc.assert(
Expand Down Expand Up @@ -128,17 +139,6 @@ describe('omnigraph/transformations', () => {
})

it('should call the nodeTransformer and edgeTransformer for every node and edge and return the result', async () => {
const nodeHardhatArbitrary = fc.record({
contract: pointHardhatArbitrary,
config: fc.anything(),
})

const edgeHardhatArbitrary = fc.record({
from: pointHardhatArbitrary,
to: pointHardhatArbitrary,
config: fc.anything(),
})

await fc.assert(
fc.asyncProperty(
fc.array(nodeHardhatArbitrary),
Expand All @@ -156,5 +156,33 @@ describe('omnigraph/transformations', () => {
)
)
})

it('should support sequential applicative', async () => {
await fc.assert(
fc.asyncProperty(
fc.array(nodeHardhatArbitrary),
fc.array(edgeHardhatArbitrary),
async (contracts, connections) => {
const nodeTransformer = jest.fn().mockImplementation(async (node) => ({ node }))
const edgeTransformer = jest.fn().mockImplementation(async (edge) => ({ edge }))
const transformerSequential = createOmniGraphHardhatTransformer(
nodeTransformer,
edgeTransformer,
sequence
)
const transformerParallel = createOmniGraphHardhatTransformer(
nodeTransformer,
edgeTransformer,
parallel
)

const graphSequential = await transformerSequential({ contracts, connections })
const graphParallel = await transformerParallel({ contracts, connections })

expect(graphSequential).toEqual(graphParallel)
}
)
)
})
})
})

0 comments on commit 64dd4e0

Please sign in to comment.