Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
maxBorrow now correctly returns zero on missing prices
  • Loading branch information
toteki committed Feb 15, 2023
commit 5c477f9c874b813fb0dcb857565d96a41ed1a1ba
14 changes: 12 additions & 2 deletions x/leverage/keeper/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@ func (k *Keeper) maxBorrow(ctx sdk.Context, addr sdk.AccAddress, denom string) (

// calculate borrowed value for the account, using the higher of spot or historic prices
borrowedValue, err := k.TotalTokenValue(ctx, totalBorrowed, types.PriceModeHigh)
if err != nil {
if nonOracleError(err) {
// non-oracle errors fail the transaction (or query)
return sdk.Coin{}, err
}
if err != nil {
// oracle errors cause max borrow to be zero
return sdk.NewCoin(denom, sdk.ZeroInt()), nil
}

// calculate borrow limit for the account, using only collateral whose price is known
borrowLimit, err := k.VisibleBorrowLimit(ctx, totalCollateral)
Expand All @@ -126,9 +131,14 @@ func (k *Keeper) maxBorrow(ctx sdk.Context, addr sdk.AccAddress, denom string) (

// determine max borrow, using the higher of spot or historic prices for the token to borrow
maxBorrow, err := k.TokenWithValue(ctx, denom, unusedBorrowLimit, types.PriceModeHigh)
if err != nil {
if nonOracleError(err) {
// non-oracle errors fail the transaction (or query)
return sdk.Coin{}, err
}
if err != nil {
// oracle errors cause max borrow to be zero
return sdk.NewCoin(denom, sdk.ZeroInt()), nil
}

// also cap borrow amount at available liquidity
maxBorrow.Amount = sdk.MinInt(maxBorrow.Amount, availableTokens)
Expand Down