Skip to content

Commit

Permalink
feat: keep unpriced balances (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsign authored Nov 30, 2023
1 parent 6fddb60 commit 90f8b30
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion scripts/utils/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export function printBalances(balances: PricedBalance[]) {
category: balance.category,
symbol: balance.symbol,
balance: parseFloatBI(balance.amount, decimals).toLocaleString(),
balanceUSD: `$${balance.balanceUSD !== undefined ? balance.balanceUSD.toLocaleString() : 0}`,
balanceUSD: `${
balance.balanceUSD !== undefined ? `$${balance.balanceUSD.toLocaleString()}` : 'Missing price'
}`,
stable: Boolean(balance.stable || balance.underlyings?.every((underlying) => underlying.stable)),
}

Expand Down
20 changes: 10 additions & 10 deletions src/db/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ export function toStorage(balances: Balance[]) {
category,
group_idx: groupIdx,
amount,
price: price || 0,
balance_usd: balanceUSD || 0,
reward_usd: rewardUSD || 0,
debt_usd: debtUSD || 0,
health_factor: healthFactor || 0,
price,
balance_usd: balanceUSD,
reward_usd: rewardUSD,
debt_usd: debtUSD,
health_factor: healthFactor,
})
})

Expand Down Expand Up @@ -379,10 +379,10 @@ export async function selectLatestProtocolsBalancesByFromAddresses(client: Click
return {
...balance,
from_address: row.from_address,
balanceUSD: parseFloat(balance.balance_usd),
debtUSD: parseFloat(balance.debt_usd),
rewardUSD: parseFloat(balance.reward_usd),
healthFactor: parseFloat(balance.health_factor),
balanceUSD: balance.balance_usd != null ? parseFloat(balance.balance_usd) : undefined,
debtUSD: balance.debt_usd != null ? parseFloat(balance.debt_usd) : undefined,
rewardUSD: balance.reward_usd != null ? parseFloat(balance.reward_usd) : undefined,
healthFactor: balance.health_factor != null ? parseFloat(balance.health_factor) : undefined,
apy: row.apy != null ? parseFloat(row.apy) : undefined,
apyBase: row.apy_base != null ? parseFloat(row.apy_base) : undefined,
apyReward: row.apy_reward != null ? parseFloat(row.apy_reward) : undefined,
Expand Down Expand Up @@ -418,7 +418,7 @@ export async function selectLatestProtocolsBalancesByFromAddresses(client: Click
stable: tokens[0].stable,
price: tokens[0].price,
amount: sumBI(tokens.map((balance) => BigInt(balance.amount || 0))).toString(),
balanceUSD: sum(tokens.map((balance) => balance.balanceUSD || 0)),
balanceUSD: tokens[0].price != null ? sum(tokens.map((balance) => balance.balanceUSD || 0)) : undefined,
})
}

Expand Down
10 changes: 8 additions & 2 deletions src/lib/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,20 @@ export async function resolveBalances<C extends GetContractsHandler>(
return balances.flat(2).filter(isNotNullish)
}

/**
* NOTE: consider unpriced balance as valid
*/
function isPricedBalanceInRange(balance: PricedBalance, key: 'balanceUSD' | 'claimableUSD' = 'balanceUSD') {
const value = balance[key]
return value != null && value >= MIN_BALANCE_USD && value <= MAX_BALANCE_USD
return value == null || (value >= MIN_BALANCE_USD && value <= MAX_BALANCE_USD)
}

/**
* NOTE: consider unpriced balance as valid
*/
function isPricedBalanceLtMax(balance: PricedBalance, key: 'balanceUSD' | 'claimableUSD' = 'balanceUSD') {
const value = balance[key]
return value != null && value <= MAX_BALANCE_USD
return value == null || value <= MAX_BALANCE_USD
}

export function sanitizePricedBalances<T extends PricedBalance>(balances: T[]) {
Expand Down
1 change: 0 additions & 1 deletion src/lib/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export async function getPricedBalances(balances: Balance[]): Promise<(Balance |
return {
...price,
...balance,
priceTimestamp: price.timestamp ? new Date(price.timestamp * 1000) : undefined,
balanceUSD: mulPrice(balance.amount || 0n, Number(decimals), price.price),
claimableUSD: balance.claimable ? mulPrice(balance.claimable || 0n, decimals, price.price) : undefined,
}
Expand Down

0 comments on commit 90f8b30

Please sign in to comment.