// @ts-expect-error import { beforeAll, describe, expect, test } from "bun:test" import { signerToEcdsaValidator } from "@zerodev/ecdsa-validator" import { toMultiChainECDSAValidator } from "@zerodev/multi-chain-validator" import { createKernelAccount, createKernelAccountClient } from "@zerodev/sdk" import { KERNEL_V3_1 } from "@zerodev/sdk/constants" import { ENTRYPOINT_ADDRESS_V07 } from "permissionless" import type { ENTRYPOINT_ADDRESS_V07_TYPE } from "permissionless/types" import { http, type PublicClient, type Transport, createPublicClient } from "viem" import { optimismSepolia, sepolia } from "viem/chains" import { CAB_V0_1, CAB_V0_2_1, type KernelCABClient, createKernelCABClient } from "../src/index" import { accountWallet, bundlerUrl, cabPaymasterUrl, magicNewton, rpcUrl } from "./utils/constants" const TEST_TIMEOUT = 1000000 describe("CAB client", () => { describe("Multichain V1_0 CAB client", () => { const entryPoint = ENTRYPOINT_ADDRESS_V07 const kernelVersion = KERNEL_V3_1 let publicClient: PublicClient let multiChainCabClient: KernelCABClient< ENTRYPOINT_ADDRESS_V07_TYPE, Transport, typeof magicNewton > beforeAll(async () => { publicClient = createPublicClient({ chain: magicNewton, transport: http() }) const multiChainValidator = await toMultiChainECDSAValidator( publicClient, { signer: accountWallet, entryPoint: entryPoint, kernelVersion: kernelVersion } ) console.log("multiChainValidator", multiChainValidator.address) const kernelAccount = await createKernelAccount(publicClient, { plugins: { sudo: multiChainValidator }, entryPoint: entryPoint, kernelVersion: kernelVersion }) console.log({ multiChainAccountAddress: kernelAccount.address }) const kernelClient = createKernelAccountClient({ account: kernelAccount, chain: magicNewton, entryPoint: entryPoint, bundlerTransport: http(bundlerUrl) }) multiChainCabClient = createKernelCABClient(kernelClient, { transport: http(cabPaymasterUrl), entryPoint, cabVersion: CAB_V0_1 }) console.log(kernelAccount.address) }) test( "enable cab and verify enabled chains", async () => { await multiChainCabClient.enableCAB({ tokens: [ { name: "6TEST", networks: [optimismSepolia.id, magicNewton.id] } ] }) // Wait for a short time to allow the changes to propagate await new Promise((resolve) => setTimeout(resolve, 2000)) const result = await multiChainCabClient.getEnabledChains() console.log("result", result) expect(result.enabledChains).toHaveLength(2) expect(result.enabledChains).toContainEqual({ chainId: optimismSepolia.id, tokens: ["6TEST"] }) expect(result.enabledChains).toContainEqual({ chainId: magicNewton.id, tokens: ["6TEST"] }) expect(result.isEnabledOnCurrentChain).toBe(true) }, TEST_TIMEOUT ) }) describe("Multichain V2_1 CAB client", () => { const entryPoint = ENTRYPOINT_ADDRESS_V07 const kernelVersion = KERNEL_V3_1 let publicClient: PublicClient let multiChainCabClient: KernelCABClient< ENTRYPOINT_ADDRESS_V07_TYPE, Transport, typeof sepolia > beforeAll(async () => { publicClient = createPublicClient({ chain: sepolia, transport: http(rpcUrl) }) const multiChainValidator = await toMultiChainECDSAValidator( publicClient, { signer: accountWallet, entryPoint: entryPoint, kernelVersion: kernelVersion } ) const kernelAccount = await createKernelAccount(publicClient, { plugins: { sudo: multiChainValidator }, entryPoint: entryPoint, kernelVersion: kernelVersion }) console.log({ multiChainAccountAddress: kernelAccount.address }) const kernelClient = createKernelAccountClient({ account: kernelAccount, chain: sepolia, entryPoint: entryPoint, bundlerTransport: http(bundlerUrl) }) multiChainCabClient = createKernelCABClient(kernelClient, { transport: http(cabPaymasterUrl), entryPoint, cabVersion: CAB_V0_2_1 }) console.log(kernelAccount.address) }) test( "enable cab and verify enabled chains", async () => { await multiChainCabClient.enableCAB({ tokens: [ { name: "6TEST", networks: [optimismSepolia.id, sepolia.id] } ] }) // Wait for a short time to allow the changes to propagate await new Promise((resolve) => setTimeout(resolve, 2000)) const result = await multiChainCabClient.getEnabledChains() expect(result.enabledChains).toHaveLength(2) expect(result.enabledChains).toContainEqual({ chainId: optimismSepolia.id, tokens: ["6TEST"] }) expect(result.enabledChains).toContainEqual({ chainId: sepolia.id, tokens: ["6TEST"] }) expect(result.isEnabledOnCurrentChain).toBe(true) }, TEST_TIMEOUT ) }) describe("ECDSA CAB client", () => { const entryPoint = ENTRYPOINT_ADDRESS_V07 const kernelVersion = KERNEL_V3_1 let publicClient: PublicClient let ecdsaCabClient: KernelCABClient< ENTRYPOINT_ADDRESS_V07_TYPE, Transport, typeof sepolia > beforeAll(async () => { publicClient = createPublicClient({ chain: sepolia, transport: http() }) const ecdsaValidator = await signerToEcdsaValidator(publicClient, { signer: accountWallet, entryPoint: entryPoint, kernelVersion: kernelVersion }) const kernelAccount = await createKernelAccount(publicClient, { plugins: { sudo: ecdsaValidator }, entryPoint: entryPoint, kernelVersion: kernelVersion }) const kernelClient = createKernelAccountClient({ account: kernelAccount, chain: sepolia, entryPoint: entryPoint, bundlerTransport: http(bundlerUrl) }) ecdsaCabClient = createKernelCABClient(kernelClient, { transport: http(cabPaymasterUrl), entryPoint, cabVersion: CAB_V0_2_1 }) }) test( "should enableCAB with only one networks", async () => { await ecdsaCabClient.enableCAB({ tokens: [{ name: "6TEST", networks: [optimismSepolia.id] }] }) }, TEST_TIMEOUT ) }) })