Skip to content

Commit

Permalink
fix: safe parse decimals (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsign authored Apr 30, 2024
1 parent 6f63686 commit ed232dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/db/balances-ddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/db/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions src/lib/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -128,6 +128,8 @@ export function sanitizeBalances<T extends Balance>(balances: T[]) {
continue
}

balance.decimals = safeParseInt(balance.decimals)

const sanitizedBalance = { ...balance }

if (balance.underlyings) {
Expand All @@ -138,6 +140,7 @@ export function sanitizeBalances<T extends Balance>(balances: T[]) {

sanitizedBalance.underlyings = balance.underlyings.map((underlying) => ({
...underlying,
decimals: safeParseInt(underlying.decimals),
amount:
deltaMantissa > 0
? balance.amount / 10n ** BigInt(deltaMantissaAbs)
Expand All @@ -148,7 +151,11 @@ export function sanitizeBalances<T extends Balance>(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)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ed232dc

Please sign in to comment.