diff --git a/x/leverage/keeper/borrows.go b/x/leverage/keeper/borrows.go index 14723bd40a..fa2bf0f039 100644 --- a/x/leverage/keeper/borrows.go +++ b/x/leverage/keeper/borrows.go @@ -14,7 +14,10 @@ import ( // unless the remaining collateral is enough to cover all borrows. // This should be checked in msg_server.go at the end of any transaction which is restricted // by borrow limits, i.e. Borrow, Decollateralize, Withdraw, MaxWithdraw. -func (k Keeper) assertBorrowerHealth(ctx sdk.Context, borrowerAddr sdk.AccAddress) error { +// MaxUsage sets the maximum percent of a user's borrow limit that can be in use: set to 1 +// to allow up to 100% borrow limit, or a lower value (e.g. 0.9) if a transaction should fail +// if a safety margin is desired (e.g. <90% borrow limit). +func (k Keeper) assertBorrowerHealth(ctx sdk.Context, borrowerAddr sdk.AccAddress, maxUsage sdk.Dec) error { borrowed := k.GetBorrowerBorrows(ctx, borrowerAddr) collateral := k.GetBorrowerCollateral(ctx, borrowerAddr) diff --git a/x/leverage/keeper/msg_server.go b/x/leverage/keeper/msg_server.go index 53f2bc51db..d4252f9000 100644 --- a/x/leverage/keeper/msg_server.go +++ b/x/leverage/keeper/msg_server.go @@ -77,7 +77,7 @@ func (s msgServer) Withdraw( // Fail here if supplier ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the borrower's collateral can cover all borrows if isFromCollateral { - err = s.keeper.assertBorrowerHealth(ctx, supplierAddr) + err = s.keeper.assertBorrowerHealth(ctx, supplierAddr, sdk.OneDec()) if err != nil { return nil, err } @@ -150,7 +150,7 @@ func (s msgServer) MaxWithdraw( // Fail here if supplier ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the borrower's collateral can cover all borrows if isFromCollateral { - err = s.keeper.assertBorrowerHealth(ctx, supplierAddr) + err = s.keeper.assertBorrowerHealth(ctx, supplierAddr, sdk.OneDec()) if err != nil { return nil, err } @@ -288,7 +288,7 @@ func (s msgServer) Decollateralize( // Fail here if borrower ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the borrower's collateral can cover all borrows - err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr) + err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr, sdk.OneDec()) if err != nil { return nil, err } @@ -321,7 +321,7 @@ func (s msgServer) Borrow( // Fail here if borrower ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the borrower's collateral can cover all borrows - err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr) + err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr, sdk.OneDec()) if err != nil { return nil, err } @@ -390,7 +390,7 @@ func (s msgServer) MaxBorrow( // Fail here if borrower ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the borrower's collateral can cover all borrows - err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr) + err = s.keeper.assertBorrowerHealth(ctx, borrowerAddr, sdk.OneDec()) if err != nil { return nil, err } @@ -510,7 +510,7 @@ func (s msgServer) FastLiquidate( // Fail here if liquidator ends up over their borrow limit under current or historic prices // Tolerates missing collateral prices if the rest of the liquidator's collateral can cover all borrows - err = s.keeper.assertBorrowerHealth(ctx, liquidator) + err = s.keeper.assertBorrowerHealth(ctx, liquidator, sdk.MustNewDecFromStr("0.9")) if err != nil { return nil, err }