Skip to content

Commit

Permalink
fix: sanitize priced balances (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsign authored Aug 8, 2023
1 parent 76dfeb2 commit d97672c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/handlers/updateBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { BalancesGroup } from '@db/balances-groups'
import { getAllContractsInteractions, groupContracts } from '@db/contracts'
import type { Balance, BalancesConfig, BalancesContext, PricedBalance } from '@lib/adapter'
import { groupBy, groupBy2 } from '@lib/array'
import { fmtBalanceBreakdown, sanitizeBalances } from '@lib/balance'
import { fmtBalanceBreakdown, sanitizeBalances, sanitizePricedBalances } from '@lib/balance'
import { type Chain, chains } from '@lib/chains'
import { sum } from '@lib/math'
import { getPricedBalances } from '@lib/price'
Expand Down Expand Up @@ -128,16 +128,18 @@ export async function updateBalances(client: PoolClient, address: `0x${string}`)

const pricedBalances = await getPricedBalances(sanitizedBalances)

const sanitizedPricedBalances = sanitizePricedBalances(pricedBalances)

const hrend = process.hrtime(hrstart)

console.log(
`getPricedBalances ${sanitizedBalances.length} balances, found ${pricedBalances.length} balances in %ds %dms`,
`getPricedBalances ${sanitizedBalances.length} balances, found ${balances.length} balances, ${sanitizedPricedBalances.length} sanitized in %ds %dms`,
hrend[0],
hrend[1] / 1000000,
)

// Group balances back by adapter/chain
const pricedBalancesByAdapterIdChain = groupBy2(pricedBalances, 'adapterId', 'chain')
const pricedBalancesByAdapterIdChain = groupBy2(sanitizedPricedBalances, 'adapterId', 'chain')

const now = new Date()

Expand Down
14 changes: 14 additions & 0 deletions src/lib/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { providers } from '@lib/providers'
import type { Token } from '@lib/token'
import { isNotNullish } from '@lib/type'

/**
* Min and Max range for a balance unit (in USD).
* Balances that don't fit in this range are discarded in the update balance process
*/
export const MIN_BALANCE_USD = 0.00001
export const MAX_BALANCE_USD = 10_000_000_000

export async function getBalances(ctx: BalancesContext, contracts: BaseContract[]) {
const coins: Token[] = []
const tokensByChain: { [key: string]: Token[] } = {}
Expand Down Expand Up @@ -140,6 +147,13 @@ export function sanitizeBalances<T extends Balance>(balances: T[]) {
return sanitizedBalances
}

export function sanitizePricedBalances<T extends PricedBalance>(balances: T[]) {
return balances.filter(
(balance) =>
balance.balanceUSD != null && balance.balanceUSD >= MIN_BALANCE_USD && balance.balanceUSD <= MAX_BALANCE_USD,
)
}

export async function resolveBalances<C extends GetContractsHandler>(
ctx: BalancesContext,
contracts: ExcludeRawContract<Partial<Awaited<ReturnType<C>>['contracts']>>,
Expand Down

0 comments on commit d97672c

Please sign in to comment.