🌳
pt0/serverF/ethpayF/gweiUtilsF.mts
1import * as _ from 'lodash-es'
2import { formatUnits } from 'ethers'
5export const gweiRange = 50_000
6export const gweiStep = 1000
8export const roundedGwei = ({value}: {value: bigint | string}): number => {
9 const gweiF = _.toNumber(formatUnits(value, 'gwei'))
10 return _.round(gweiF)
13export const findFreeGwei = ({tgtGwei, pendingGweis}: {tgtGwei: number, pendingGweis: number[]}): number => {
14 for (let offset = 0; offset < gweiRange; offset += gweiStep) {
15 const offsetDown = tgtGwei - offset
16 if (!_.includes(pendingGweis, offsetDown)) {
17 return offsetDown
18 }
20 const offsetUp = tgtGwei + offset
21 if (!_.includes(pendingGweis, offsetUp)) {
22 return offsetUp
23 }
24 }
25 throw throPtErr('couldnt find free gwei', {tgtGwei, pendingGweis})
28export const ethToGwei = (ethAmt: number): number => _.round(ethAmt * 1e9)
30export const usdToTargetGwei = ({totalUsd, ethPrice}: {totalUsd: number, ethPrice: number}): number => {
31 return _.round(totalUsd / ethPrice * 1000 * 1000 * 1000 / gweiStep) * gweiStep