From eedc99cc95df30dc673a1cbe17af658a2bca3df3 Mon Sep 17 00:00:00 2001 From: mouseless <97399882+mouseless-eth@users.noreply.github.com> Date: Thu, 15 May 2025 07:57:12 +0100 Subject: [PATCH] Add getChain helper to remove foundry chain hardcode --- .changeset/bitter-berries-exist.md | 5 +++ .../mock-paymaster/helpers/erc20-utils.ts | 7 ++- packages/mock-paymaster/helpers/utils.ts | 44 ++++++++++++++----- packages/mock-paymaster/index.ts | 9 ++-- 4 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 .changeset/bitter-berries-exist.md diff --git a/.changeset/bitter-berries-exist.md b/.changeset/bitter-berries-exist.md new file mode 100644 index 00000000..2986ba6e --- /dev/null +++ b/.changeset/bitter-berries-exist.md @@ -0,0 +1,5 @@ +--- +"@pimlico/mock-paymaster": patch +--- + +Remove foundry chain hardcode by introducing getChain helper diff --git a/packages/mock-paymaster/helpers/erc20-utils.ts b/packages/mock-paymaster/helpers/erc20-utils.ts index c09a4540..f2ad57bf 100644 --- a/packages/mock-paymaster/helpers/erc20-utils.ts +++ b/packages/mock-paymaster/helpers/erc20-utils.ts @@ -36,7 +36,7 @@ export const deployErc20Token = async ( } export const tokenBalanceOf = async (holder: Address, anvilRpc: string) => { - const publicClient = getPublicClient(anvilRpc) + const publicClient = await getPublicClient(anvilRpc) const balance = await publicClient.call({ to: erc20Address, @@ -59,7 +59,10 @@ export const sudoMintTokens = async ({ to: Address anvilRpc: string }) => { - const walletClient = getAnvilWalletClient({ addressIndex: 0, anvilRpc }) + const walletClient = await getAnvilWalletClient({ + addressIndex: 0, + anvilRpc + }) await walletClient.sendTransaction({ to: erc20Address, diff --git a/packages/mock-paymaster/helpers/utils.ts b/packages/mock-paymaster/helpers/utils.ts index 2b4b3f68..13c11265 100644 --- a/packages/mock-paymaster/helpers/utils.ts +++ b/packages/mock-paymaster/helpers/utils.ts @@ -7,10 +7,10 @@ import { type Transport, type WalletClient, createPublicClient, - createWalletClient + createWalletClient, + defineChain } from "viem" import { mnemonicToAccount } from "viem/accounts" -import { foundry } from "viem/chains" import { erc20Address } from "./erc20-utils.js" import { RpcError, ValidationErrors } from "./schema.js" @@ -19,9 +19,33 @@ export const maxBigInt = (a: bigint, b: bigint) => { return a > b ? a : b } -export const getPublicClient = ( +export const getChain = async (anvilRpc: string) => { + const tempClient = createPublicClient({ + transport: http(anvilRpc) + }) + + const chain = defineChain({ + id: await tempClient.getChainId(), + name: "chain", + nativeCurrency: { + name: "ETH", + symbol: "ETH", + decimals: 18 + }, + rpcUrls: { + default: { + http: [], + webSocket: undefined + } + } + }) + + return chain +} + +export const getPublicClient = async ( anvilRpc: string -): PublicClient => { +): Promise> => { const transport = http(anvilRpc, { // onFetchRequest: async (req) => { // console.log(await req.json(), "request") @@ -32,19 +56,17 @@ export const getPublicClient = ( }) return createPublicClient({ - chain: foundry, + chain: await getChain(anvilRpc), transport: transport, pollingInterval: 100 }) } -export const getAnvilWalletClient = ({ +export const getAnvilWalletClient = async ({ addressIndex, anvilRpc -}: { addressIndex: number; anvilRpc: string }): WalletClient< - Transport, - Chain, - Account +}: { addressIndex: number; anvilRpc: string }): Promise< + WalletClient > => { return createWalletClient({ account: mnemonicToAccount( @@ -53,7 +75,7 @@ export const getAnvilWalletClient = ({ addressIndex } ), - chain: foundry, + chain: await getChain(anvilRpc), transport: http(anvilRpc) }) } diff --git a/packages/mock-paymaster/index.ts b/packages/mock-paymaster/index.ts index 14450e14..9ead333b 100644 --- a/packages/mock-paymaster/index.ts +++ b/packages/mock-paymaster/index.ts @@ -3,9 +3,8 @@ import Fastify from "fastify" import { defineInstance } from "prool" import { http, createPublicClient } from "viem" import { createBundlerClient } from "viem/account-abstraction" -import { foundry } from "viem/chains" import { deployErc20Token } from "./helpers/erc20-utils.js" -import { getAnvilWalletClient } from "./helpers/utils.js" +import { getAnvilWalletClient, getChain } from "./helpers/utils.js" import { createRpcHandler } from "./relay.js" import { deployPaymasters } from "./singletonPaymasters.js" @@ -23,16 +22,16 @@ export const paymaster = defineInstance( port: _port, name: "mock-paymaster", start: async ({ port = _port }) => { - const walletClient = getAnvilWalletClient({ + const walletClient = await getAnvilWalletClient({ anvilRpc, addressIndex: 1 }) const publicClient = createPublicClient({ transport: http(anvilRpc), - chain: foundry + chain: await getChain(anvilRpc) }) const bundler = createBundlerClient({ - chain: foundry, + chain: await getChain(anvilRpc), transport: http(altoRpc) })