Skip to content

Instantly share code, notes, and snippets.

@denniswon
Last active April 26, 2024 03:32
Show Gist options
  • Save denniswon/025ca0aa14990c1857763f88317aac0c to your computer and use it in GitHub Desktop.
Save denniswon/025ca0aa14990c1857763f88317aac0c to your computer and use it in GitHub Desktop.
split-rpc-client.ts
const client = createBundlerClient({
chain,
transport: (opts) => {
const bundlerRpc = http(`${BUNDLER_RPC}`)(opts);
const paymasterRpc = http(`${PAYMASTER_RPC}`)(opts);
const publicRpc = http(`${PUBLIC_RPC}`)(
opts
);
return custom({
request: async (args) => {
const bundlerMethods = new Set([
"eth_sendUserOperation",
"eth_estimateUserOperationGas",
"eth_getUserOperationReceipt",
"eth_getUserOperationByHash",
"eth_supportedEntryPoints",
]);
const paymasterMethods = new Set([
"alchemy_requestPaymasterAndData",
"alchemy_requestGasAndPaymasterAndData",
]);
if (bundlerMethods.has(args.method)) {
return bundlerRpc.request(args);
} else if (paymasterMethods.has(args.method)) {
return paymasterRpc.request(args);
} else {
return publicRpc.request(args);
}
},
})(opts);
},
});
return createSmartAccountClientFromExisting({
client,
account: await createSimpleSmartAccount({
chain,
signer,
entryPoint: getEntryPoint(chain, { version: "0.7.0" }),
transport: custom(client),
accountAddress,
}),
// you can also just override the fee estimator to do whatever you want to get fee estimates.
// same for gas estimator for gas estimates, etc.
// or even paymaster middleware. If you override each middleware, you have full control
feeEstimator: async (struct) => ({
...struct,
maxFeePerGas: 1_000_000_000n,
maxPriorityFeePerGas: 500_000_000n,
}),
opts: { feeOptions },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment