// @ts-expect-error import { beforeAll, describe, expect, test } from "bun:test" import { toMultiChainECDSAValidator } from "@zerodev/multi-chain-validator" import { createKernelAccount, createKernelAccountClient } from "@zerodev/sdk" import { KERNEL_V3_1 } from "@zerodev/sdk/constants" import { type BundlerClient, ENTRYPOINT_ADDRESS_V07, createBundlerClient } from "permissionless" import type { ENTRYPOINT_ADDRESS_V07_TYPE } from "permissionless/types" import { http, type Hex, type PublicClient, type Transport, createPublicClient, encodeFunctionData, erc20Abi } from "viem" import { arbitrum, optimism } from "viem/chains" import { type KernelCABClient, createKernelCABClient, supportedSwapTokens, supportedTokensV2 } from "../src/index" import { accountWallet, cabPaymasterUrl, opBundlerUrl } from "./utils/constants" const TEST_TIMEOUT = 1000000 describe("SendUserOperationCab", () => { const entryPoint = ENTRYPOINT_ADDRESS_V07 const kernelVersion = KERNEL_V3_1 let publicClient: PublicClient let bundlerClient: BundlerClient let cabClient: KernelCABClient< ENTRYPOINT_ADDRESS_V07_TYPE, Transport, typeof optimism > beforeAll(async () => { publicClient = createPublicClient({ chain: optimism, transport: http(opBundlerUrl) }) bundlerClient = createBundlerClient({ chain: optimism, entryPoint: entryPoint, transport: http(opBundlerUrl, { timeout: 60000 }) }) 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("kernelAccount address", kernelAccount.address) const kernelClient = createKernelAccountClient({ account: kernelAccount, chain: optimism, entryPoint: entryPoint, bundlerTransport: http(opBundlerUrl) }) cabClient = createKernelCABClient(kernelClient, { transport: http(cabPaymasterUrl), entryPoint }) }) test( "transfer 0.00001 WETH on optimism, repay on arbitrum", async () => { await cabClient.enableCAB({ tokens: [{ name: "USDC", networks: [arbitrum.id, optimism.id] }] }) // transfer 0.00001 WETH to CAB Owner Address <-> ~ 0.02 USDC const calls = [ { to: supportedSwapTokens[optimism.id].WETH, data: encodeFunctionData({ abi: erc20Abi, functionName: "transfer", args: [ "0x999f8012B114600D2B2995e14ED975322c30391E", 10000000000000n ] }) as Hex, value: 0n } ] // repay on optimism const repayTokens = [ { address: supportedTokensV2.USDC[arbitrum.id].token, chainId: arbitrum.id } ] const { userOperation, repayTokensInfo, sponsorTokensInfo } = await cabClient.prepareUserOperationRequestCAB({ account: cabClient.account, calls: calls, repayTokens: repayTokens }) expect(userOperation).toBeDefined() expect(Array.isArray(repayTokensInfo)).toBe(true) expect(Array.isArray(sponsorTokensInfo)).toBe(true) expect(repayTokensInfo.length).toBeGreaterThan(0) expect(sponsorTokensInfo.length).toBeGreaterThan(0) const userOpHash = await cabClient.sendUserOperationCAB({ userOperation }) console.log("transfer with cab userOpHash:", userOpHash) const ret = await bundlerClient.waitForUserOperationReceipt({ hash: userOpHash }) console.log( "transfer with cab transactionHash:", ret.receipt.transactionHash ) }, TEST_TIMEOUT ) })