Skip to content

Commit d9ade62

Browse files
authored
fix (#2765)
* fix
1 parent 3220b35 commit d9ade62

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/config/data/ccip/utils.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { SupportedChain } from "~/config/types.ts"
22
import { chainToTechnology } from "~/config/chains.ts"
33
import { NetworkFeeStructure, PoolType, TokenMechanism, LaneSpecificFeeKey, RateLimiterConfig } from "./types.ts"
44
import { networkFees } from "./data.ts"
5-
import BigNumberJs from "bignumber.js"
65
import { commify } from "~/utils/index.js"
6+
import { formatUnits } from "ethers"
77

88
// Define valid pool type combinations and their corresponding mechanisms
99
const POOL_MECHANISM_MAP: Record<string, TokenMechanism> = {
@@ -116,13 +116,6 @@ export const calculateMessaingNetworkFees = (sourceChain: SupportedChain, destin
116116
return calculateMessagingNetworkFeesDirect(laneSpecificFeeKey)
117117
}
118118

119-
const normalizeNumber = (bigNum: BigNumberJs, decimals = 18) => {
120-
const divisor = new BigNumberJs(10).pow(decimals)
121-
const normalized = bigNum.dividedBy(divisor)
122-
123-
return normalized.toNumber()
124-
}
125-
126119
const formatTime = (seconds: number) => {
127120
const minute = 60
128121
const hour = 3600 // 60*60
@@ -154,25 +147,45 @@ const formatTime = (seconds: number) => {
154147
}
155148
}
156149

150+
/**
151+
* Modern capacity display using ethers.js v6 formatUnits
152+
*/
157153
export const displayCapacity = (decimals = 18, token: string, rateLimiterConfig?: RateLimiterConfig) => {
158154
if (!rateLimiterConfig?.isEnabled) {
159155
return "N/A"
160156
}
161157

162-
const capacity = String(rateLimiterConfig?.capacity || 0)
163-
const numberWithoutDecimals = normalizeNumber(new BigNumberJs(capacity), decimals).toString()
164-
return `${commify(numberWithoutDecimals)} ${token}`
158+
const capacity = rateLimiterConfig?.capacity || "0"
159+
// Use ethers.js formatUnits for precise decimal conversion
160+
const formattedCapacity = formatUnits(capacity, decimals)
161+
162+
// Remove trailing zeros and unnecessary decimal point
163+
const cleanedCapacity = formattedCapacity.replace(/\.?0+$/, "")
164+
165+
return `${commify(cleanedCapacity)} ${token}`
165166
}
166167

168+
/**
169+
* Modern rate display using ethers.js v6 formatUnits
170+
*/
167171
export const displayRate = (capacity: string, rate: string, symbol: string, decimals = 18) => {
168-
const capacityNormalized = normalizeNumber(new BigNumberJs(capacity), decimals) // normalize capacity
169-
const rateNormalized = normalizeNumber(new BigNumberJs(rate), decimals) // normalize capacity
172+
// Use ethers.js formatUnits for precise decimal conversion
173+
const capacityFormatted = formatUnits(capacity, decimals)
174+
const rateFormatted = formatUnits(rate, decimals)
175+
176+
// Convert to numbers for time calculation
177+
const capacityNum = parseFloat(capacityFormatted)
178+
const rateNum = parseFloat(rateFormatted)
170179

171-
const totalRefillTime = capacityNormalized / rateNormalized // in seconds
180+
const totalRefillTime = capacityNum / rateNum // in seconds
172181
const displayTime = `${formatTime(totalRefillTime)}`
173182

183+
// Clean up formatting
184+
const cleanedRate = rateFormatted.replace(/\.?0+$/, "")
185+
const cleanedCapacity = capacityFormatted.replace(/\.?0+$/, "")
186+
174187
return {
175-
rateSecond: `${commify(rateNormalized)} ${symbol}/second`,
176-
maxThroughput: `Refills from 0 to ${commify(capacityNormalized)} ${symbol} in ${displayTime}`,
188+
rateSecond: `${commify(cleanedRate)} ${symbol}/second`,
189+
maxThroughput: `Refills from 0 to ${commify(cleanedCapacity)} ${symbol} in ${displayTime}`,
177190
}
178191
}

0 commit comments

Comments
 (0)