Skip to content

Add getChain helper to remove foundry chain hardcode #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bitter-berries-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pimlico/mock-paymaster": patch
---

Remove foundry chain hardcode by introducing getChain helper
7 changes: 5 additions & 2 deletions packages/mock-paymaster/helpers/erc20-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
44 changes: 33 additions & 11 deletions packages/mock-paymaster/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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<Transport, Chain> => {
): Promise<PublicClient<Transport, Chain>> => {
const transport = http(anvilRpc, {
// onFetchRequest: async (req) => {
// console.log(await req.json(), "request")
Expand All @@ -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<Transport, Chain, Account>
> => {
return createWalletClient({
account: mnemonicToAccount(
Expand All @@ -53,7 +75,7 @@ export const getAnvilWalletClient = ({
addressIndex
}
),
chain: foundry,
chain: await getChain(anvilRpc),
transport: http(anvilRpc)
})
}
Expand Down
9 changes: 4 additions & 5 deletions packages/mock-paymaster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
})

Expand Down
Loading