From e813038bbfa07f5a850e4ecdca4b93a42232591c Mon Sep 17 00:00:00 2001 From: 0xsign <0xsign@protonmail.com> Date: Tue, 30 Apr 2024 10:33:15 +0200 Subject: [PATCH] fix: safe parse decimals --- src/db/balances-ddb.ts | 5 +++-- src/db/balances.ts | 6 +++--- src/lib/balance.ts | 11 +++++++++-- src/lib/fmt.ts | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/db/balances-ddb.ts b/src/db/balances-ddb.ts index 9f9cde6b6..1bd513984 100644 --- a/src/db/balances-ddb.ts +++ b/src/db/balances-ddb.ts @@ -4,6 +4,7 @@ import { groupBy } from '@lib/array' import { areBalancesStale } from '@lib/balance' import type { Category } from '@lib/category' import { chainById } from '@lib/chains' +import { safeParseInt } from '@lib/fmt' import { sum, sumBI } from '@lib/math' import type { UnixTimestamp } from '@lib/type' @@ -34,7 +35,7 @@ export function formatBalance(balance: any) { name: balance.name, address: balance.address, symbol: balance.symbol, - decimals: balance.decimals != null ? parseInt(balance.decimals) : balance.decimals, + decimals: safeParseInt(balance.decimals), category: balance.category as Category, stable: Boolean(balance.stable || underlyings?.every((underlying: any) => underlying.stable)), price: balance.price, @@ -50,7 +51,7 @@ export function formatBalance(balance: any) { apyReward: balance.apyReward, apyMean30d: balance.apyMean30d, ilRisk: balance.ilRisk, - unlockAt: balance.unlockAt != null ? parseInt(balance.unlockAt) : balance.unlockAt, + unlockAt: safeParseInt(balance.unlockAt), side: balance.side, margin: balance.margin, entryPrice: balance.entryPrice, diff --git a/src/db/balances.ts b/src/db/balances.ts index d5c4aeddd..b29dce201 100644 --- a/src/db/balances.ts +++ b/src/db/balances.ts @@ -5,7 +5,7 @@ import { groupBy, groupBy2 } from '@lib/array' import { areBalancesStale } from '@lib/balance' import type { Category } from '@lib/category' import { type Chain, chainByChainId, chainById } from '@lib/chains' -import { shortAddress, toDateTime, unixFromDateTime } from '@lib/fmt' +import { safeParseInt, shortAddress, toDateTime, unixFromDateTime } from '@lib/fmt' import { sum, sumBI } from '@lib/math' import type { UnixTimestamp } from '@lib/type' @@ -77,7 +77,7 @@ export function formatBalance(balance: any): FormattedBalance { name: balance.name, address: balance.address, symbol: balance.symbol, - decimals: balance.decimals != null ? parseInt(balance.decimals) : balance.decimals, + decimals: safeParseInt(balance.decimals), category: balance.category as Category, stable: Boolean(balance.stable || underlyings?.every((underlying: any) => underlying.stable)), price: balance.price, @@ -93,7 +93,7 @@ export function formatBalance(balance: any): FormattedBalance { apyReward: balance.apyReward, apyMean30d: balance.apyMean30d, ilRisk: balance.ilRisk, - unlockAt: balance.unlockAt != null ? parseInt(balance.unlockAt) : balance.unlockAt, + unlockAt: safeParseInt(balance.unlockAt), //@ts-expect-error side: balance.side, margin: balance.margin, diff --git a/src/lib/balance.ts b/src/lib/balance.ts index 66b24e58f..d96977da1 100644 --- a/src/lib/balance.ts +++ b/src/lib/balance.ts @@ -11,7 +11,7 @@ import type { Category } from '@lib/category' import { chainById } from '@lib/chains' import { ADDRESS_ZERO } from '@lib/contract' import { getBalancesOf } from '@lib/erc20' -import { unixFromDate } from '@lib/fmt' +import { safeParseInt, unixFromDate } from '@lib/fmt' import { parseFloatBI } from '@lib/math' import { multicall } from '@lib/multicall' import { sendSlackMessage } from '@lib/slack' @@ -128,6 +128,8 @@ export function sanitizeBalances(balances: T[]) { continue } + balance.decimals = safeParseInt(balance.decimals) + const sanitizedBalance = { ...balance } if (balance.underlyings) { @@ -138,6 +140,7 @@ export function sanitizeBalances(balances: T[]) { sanitizedBalance.underlyings = balance.underlyings.map((underlying) => ({ ...underlying, + decimals: safeParseInt(underlying.decimals), amount: deltaMantissa > 0 ? balance.amount / 10n ** BigInt(deltaMantissaAbs) @@ -148,7 +151,11 @@ export function sanitizeBalances(balances: T[]) { } if (balance.rewards) { - sanitizedBalance.rewards = balance.rewards.map((reward) => ({ ...reward, amount: reward.amount || 0n })) + sanitizedBalance.rewards = balance.rewards.map((reward) => ({ + ...reward, + decimals: safeParseInt(reward.decimals), + amount: reward.amount || 0n, + })) } sanitizedBalances.push(sanitizedBalance) diff --git a/src/lib/fmt.ts b/src/lib/fmt.ts index e65e891cf..6543f2db5 100644 --- a/src/lib/fmt.ts +++ b/src/lib/fmt.ts @@ -72,12 +72,12 @@ export function parseAddresses(str: string) { .sort() } -export function safeParseInt(data?: string | null, defaultValue = undefined) { +export function safeParseInt(data?: string | number | null | undefined, defaultValue = undefined) { if (data == null) { return defaultValue } - const parsed = parseInt(data) + const parsed = typeof data === 'string' ? parseInt(data) : data if (isNaN(parsed)) { return defaultValue }