import { hexToBigInt, parseGwei } from 'viem'
import { arbitrum } from 'viem/chains'
export const getUserOperationFeesPerGas = async (publicClient) => {
const url = publicClient.chain.rpcUrls.default.http[0]
if (/alchemy/.test(url)) {
const block = await publicClient.getBlock({
blockTag: 'latest',
})
let baseFeeBuffer = 110n
let priorityFeePerGasBuffer = 110n
if (!publicClient.chain.testnet) {
if (publicClient.chain.id === arbitrum.id) {
baseFeeBuffer = 105n
priorityFeePerGasBuffer = 100n
} else {
baseFeeBuffer = 150n
priorityFeePerGasBuffer = 125n
}
}
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'rundler_maxPriorityFeePerGas',
params: [],
id: 1,
}),
}).then((response) => response.json())
const rundler_maxPriorityFeePerGas = hexToBigInt(response.result)
const baseFeePerGas = block.baseFeePerGas
const maxPriorityFeePerGas = priorityFeePerGasBuffer * rundler_maxPriorityFeePerGas
const result = {
maxFeePerGas:
(baseFeeBuffer * baseFeePerGas) / 100n + maxPriorityFeePerGas / 100n,
maxPriorityFeePerGas: maxPriorityFeePerGas / 100n,
}
return result
}
return {}
}