Skip to content

Commit 1e828c7

Browse files
committed
update bundler fee estimation
1 parent fc325db commit 1e828c7

5 files changed

Lines changed: 35 additions & 23 deletions

File tree

packages/common/src/transports/wiresaw.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function wiresaw<const wiresawTransport extends Transport>(
3434
...rest,
3535
// TODO: type `request` so we don't have to cast
3636
async request(req): ReturnType<EIP1193RequestFn> {
37+
console.log("rpc request", req.method);
3738
try {
3839
if (req.method === "eth_chainId") {
3940
if (chainId != null) return chainId;

packages/entrykit/playground/wagmiConfig.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createWagmiConfig } from "../src/createWagmiConfig";
44
import { chainId } from "./common";
55
import { garnet, pyrope } from "@latticexyz/common/chains";
66
import { wiresaw } from "@latticexyz/common/internal";
7-
import { withFeeCache } from "../src/utils/withFeeCache";
87

98
const redstoneWithPaymaster = {
109
...redstone,
@@ -81,7 +80,7 @@ const chains = [
8180
mainnet,
8281
garnetWithPaymaster,
8382
anvilWithPaymaster,
84-
withFeeCache(redstoneWithPaymaster),
83+
redstoneWithPaymaster,
8584
pyropeWithPaymaster,
8685
] as const satisfies Chain[];
8786

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Client, EstimateFeesPerGasReturnType } from "viem";
2+
import { estimateFeesPerGas } from "viem/actions";
3+
4+
type CachedFeesPerGasOptions = {
5+
updateInterval?: number;
6+
};
7+
8+
export function cachedFeesPerGas(
9+
client: Client,
10+
options: CachedFeesPerGasOptions = { updateInterval: 30_000 },
11+
): () => Promise<EstimateFeesPerGasReturnType<"eip1559">> {
12+
let fees: EstimateFeesPerGasReturnType<"eip1559"> | null = null;
13+
14+
async function updateFees() {
15+
console.log("updating fees");
16+
fees = await estimateFeesPerGas(client);
17+
}
18+
19+
updateFees();
20+
setInterval(updateFees, options.updateInterval);
21+
22+
return async () => {
23+
console.log("asking for fees");
24+
if (fees) return fees;
25+
fees = await estimateFeesPerGas(client);
26+
return fees;
27+
};
28+
}

packages/entrykit/src/createBundlerClient.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
} from "viem/account-abstraction";
88
import { defaultClientConfig } from "./common";
99
import { getPaymaster } from "./getPaymaster";
10-
import { getAction } from "viem/utils";
11-
import { estimateFeesPerGas } from "viem/actions";
10+
import { cachedFeesPerGas } from "./actions/cachedFeesPerGas";
1211

1312
export function createBundlerClient<
1413
transport extends Transport,
@@ -59,7 +58,6 @@ function createFeeEstimator(client: Client): undefined | (() => Promise<Estimate
5958
// because viem sets fees way too high by default
6059
// https://github.com/wevm/viem/blob/253b1072ad9fe36a0e0491e173c85a6d69209ada/src/account-abstraction/actions/bundler/prepareUserOperation.ts#L436-L457
6160
if ([690, 17069, 695569].includes(client.chain.id)) {
62-
// TODO: move to fee ref or similar approach
63-
return () => getAction(client, estimateFeesPerGas, "estimateFeesPerGas")({ chain: client.chain });
61+
return cachedFeesPerGas(client);
6462
}
6563
}
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Chain, createClient, EstimateFeesPerGasReturnType, http } from "viem";
2-
import { estimateFeesPerGas } from "viem/actions";
1+
import { Chain, createClient, http } from "viem";
2+
import { cachedFeesPerGas } from "../actions/cachedFeesPerGas";
33

44
type WithFeeCacheOptions = {
55
updateInterval?: number;
@@ -11,30 +11,16 @@ export function withFeeCache(chain: Chain, options: WithFeeCacheOptions = { upda
1111
throw new Error("withFeeCache: estimateFeesPerGas already defined in chain config");
1212
}
1313

14-
let fees: EstimateFeesPerGasReturnType<"eip1559"> | null = null;
15-
1614
const client = createClient({
1715
chain,
1816
transport: http(),
1917
});
2018

21-
async function updateFees() {
22-
console.log("updating fees");
23-
fees = await estimateFeesPerGas(client);
24-
console.log("updated fees", fees);
25-
}
26-
27-
updateFees();
28-
setInterval(updateFees, options.updateInterval);
29-
3019
return {
3120
...chain,
3221
fees: {
3322
...chain.fees,
34-
estimateFeesPerGas: async (args) => {
35-
console.log("asking for fees", args);
36-
return fees;
37-
},
23+
estimateFeesPerGas: cachedFeesPerGas(client, options),
3824
},
3925
};
4026
}

0 commit comments

Comments
 (0)