Skip to content

Commit

Permalink
chore: init testClients in network setup
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Aug 23, 2024
1 parent 2350dde commit 6013394
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 27 deletions.
6 changes: 3 additions & 3 deletions tests/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChainConfig, initChain, killAllNetworks } from "./test.utils"
import { type NetworkConfig, initNetwork, killAllNetworks } from "./test.utils"

export async function setup({ provide }) {
const network = await initChain()
const network = await initNetwork()
const { bundlerInstance, instance, ...serializeableConfig } = network
provide("globalNetwork", serializeableConfig)
}
Expand All @@ -12,6 +12,6 @@ export async function teardown() {

declare module "vitest" {
export interface ProvidedContext {
globalNetwork: ChainConfig
globalNetwork: NetworkConfig
}
}
12 changes: 8 additions & 4 deletions tests/instances/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ import {
killNetwork,
toTestClient
} from "../test.utils"
import type { ChainConfig, MasterClient } from "../test.utils"
import { type TestFileNetworkType, toNetwork } from "../testSetup"
import type {
MasterClient,
NetworkConfig,
NetworkConfigWithBundler
} from "../test.utils"
import { type TestFileNetworkType, aaTest, toNetwork } from "../testSetup"

const NETWORK_TYPE: TestFileNetworkType = "LOCAL"

describe("account", () => {
let network: ChainConfig
let network: NetworkConfig
let chain: Chain
let bundlerUrl: string
let testClient: MasterClient
Expand Down Expand Up @@ -103,7 +107,7 @@ describe("account", () => {
])
})

test("should check bytecode at Counter contract", async () => {
test("should", async () => {
const counterAddress = network.deployment.counterAddress
const byteCode = await testClient.getBytecode({ address: counterAddress })
expect(byteCode).toBeTruthy()
Expand Down
4 changes: 2 additions & 2 deletions tests/instances/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
killNetwork,
toTestClient
} from "../test.utils"
import type { ChainConfig, MasterClient } from "../test.utils"
import type { MasterClient, NetworkConfig } from "../test.utils"
import { type TestFileNetworkType, toNetwork } from "../testSetup"

const NETWORK_TYPE: TestFileNetworkType = "LOCAL"

describe("bundler", () => {
let network: ChainConfig
let network: NetworkConfig
let chain: Chain
let bundlerUrl: string
let testClient: MasterClient
Expand Down
4 changes: 2 additions & 2 deletions tests/instances/hook.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
killNetwork,
toTestClient
} from "../test.utils"
import type { ChainConfig, MasterClient } from "../test.utils"
import type { MasterClient, NetworkConfig } from "../test.utils"
import { type TestFileNetworkType, toNetwork } from "../testSetup"

const NETWORK_TYPE: TestFileNetworkType = "LOCAL"

describe("hook.module", () => {
let network: ChainConfig
let network: NetworkConfig
let chain: Chain
let bundlerUrl: string
let testClient: MasterClient
Expand Down
4 changes: 2 additions & 2 deletions tests/instances/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
killNetwork,
toTestClient
} from "../test.utils"
import type { ChainConfig, MasterClient } from "../test.utils"
import type { MasterClient, NetworkConfig } from "../test.utils"
import { type TestFileNetworkType, toNetwork } from "../testSetup"

const NETWORK_TYPE: TestFileNetworkType = "LOCAL"

describe("modules", () => {
let network: ChainConfig
let network: NetworkConfig
let chain: Chain
let bundlerUrl: string
let testClient: MasterClient
Expand Down
76 changes: 71 additions & 5 deletions tests/test.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type Hex,
type SetCodeParameters,
createTestClient,
createWalletClient,
encodeAbiParameters,
parseAbi,
parseAbiParameters,
Expand All @@ -17,7 +18,12 @@ import {
} from "viem"
import { mnemonicToAccount } from "viem/accounts"
import { anvil as anvilChain } from "viem/chains"
import type { EIP712DomainReturn, NexusSmartAccount } from "../src"
import {
type EIP712DomainReturn,
type NexusSmartAccount,
type NexusSmartAccountConfig,
createSmartAccountClient
} from "../src"
import { getCustomChain } from "../src/account/utils"
import { Logger } from "../src/account/utils/Logger"
import { ENTRYPOINT_ADDRESS, ENTRYPOINT_SIMULATIONS } from "../src/contracts"
Expand Down Expand Up @@ -55,9 +61,9 @@ export type AnvilDto = {
instance: AnvilInstance
deployment: Deployment
}
export type ChainConfigWithBundler = AnvilDto & BundlerDto
export type ChainConfig = Omit<
ChainConfigWithBundler,
export type NetworkConfigWithBundler = AnvilDto & BundlerDto
export type NetworkConfig = Omit<
NetworkConfigWithBundler,
"instance" | "bundlerInstance"
>
export const pKey =
Expand Down Expand Up @@ -91,7 +97,7 @@ export const killNetwork = (ids: number[]) =>
})
)

export const initChain = async (): Promise<ChainConfigWithBundler> => {
export const initNetwork = async (): Promise<NetworkConfigWithBundler> => {
const configuredChain = await initAnvilPayload()
const bundlerConfig = await initBundlerInstance({
rpcUrl: configuredChain.rpcUrl
Expand Down Expand Up @@ -197,6 +203,66 @@ export const nonZeroBalance = async (
)
}

export type FundedTestClients = Awaited<ReturnType<typeof toFundedTestClients>>
export const toFundedTestClients = async (
network: NetworkConfigWithBundler
) => {
const testConfig: Partial<NexusSmartAccountConfig> = {
factoryAddress: network.deployment.k1FactoryAddress,
k1ValidatorAddress: network.deployment.k1ValidatorAddress
}

const chain = network.chain
const bundlerUrl = network.bundlerUrl

const account = getTestAccount(2)
const recipientAccount = getTestAccount(3)

const walletClient = createWalletClient({
account,
chain,
transport: http()
})

const recipientWalletClient = createWalletClient({
account: recipientAccount,
chain,
transport: http()
})

const testClient = toTestClient(chain, getTestAccount())

const smartAccount = await createSmartAccountClient({
signer: walletClient,
bundlerUrl,
chain,
...testConfig
})

const recipientSmartAccount = await createSmartAccountClient({
signer: recipientWalletClient,
bundlerUrl,
chain,
...testConfig
})

const smartAccountAddress = await smartAccount.getAddress()
const recipientSmartAccountAddress = await recipientSmartAccount.getAddress()
await fundAndDeploy(testClient, [smartAccount, recipientSmartAccount])

return {
account,
recipientAccount,
walletClient,
recipientWalletClient,
testClient,
smartAccount,
recipientSmartAccount,
smartAccountAddress,
recipientSmartAccountAddress
}
}

export const fundAndDeploy = async (
testClient: MasterClient,
smartAccounts: NexusSmartAccount[]
Expand Down
28 changes: 19 additions & 9 deletions tests/testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { inject, test } from "vitest"
import { type ChainConfigWithBundler, initChain } from "./test.utils"
import {
type FundedTestClients,
type NetworkConfigWithBundler,
initNetwork,
toFundedTestClients
} from "./test.utils"

export const testWithBundler = test.extend<{
rpc: ChainConfigWithBundler
export type NetworkConfigWithTestClients = NetworkConfigWithBundler & {
fundedTestClients: FundedTestClients
}

export const aaTest = test.extend<{
config: NetworkConfigWithTestClients
}>({
// biome-ignore lint/correctness/noEmptyPattern: Needed in vitest :/
rpc: async ({}, use) => {
const testChain = await initChain()
await use({ ...testChain })
config: async ({}, use) => {
const testNetwork = await initNetwork()
const fundedTestClients = await toFundedTestClients(testNetwork)
await use({ ...testNetwork, fundedTestClients })
await Promise.all([
testChain.instance.stop(),
testChain.bundlerInstance.stop()
testNetwork.instance.stop(),
testNetwork.bundlerInstance.stop()
])
}
})

export type TestFileNetworkType = "LOCAL" | "GLOBAL"
export const toNetwork = async (networkType: TestFileNetworkType) =>
// @ts-ignore
await (networkType === "GLOBAL" ? inject("globalNetwork") : initChain())
await (networkType === "GLOBAL" ? inject("globalNetwork") : initNetwork())

0 comments on commit 6013394

Please sign in to comment.