Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autofill empty denoms in MsgLeveragedLiquidate #2121

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

- [2121](https://github.com/umee-network/umee/pull/2121) Allow `MsgLeveragedLiquidate` to auto-select repay and reward denoms if request fields left blank.
- [2114](https://github.com/umee-network/umee/pull/2114) Add borrow factor to `x/leverage`
- [2102](https://github.com/umee-network/umee/pull/2102) and [2106](https://github.com/umee-network/umee/pull/2106) Add `MsgLeveragedLiquidate` to `x/leverage`
- [2085](https://github.com/umee-network/umee/pull/2085) Add `inspect` query to leverage module, which msut be enabled on a node by running with `-l` liquidator query flag.
Expand Down
15 changes: 15 additions & 0 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ func (k Keeper) Liquidate(
func (k Keeper) LeveragedLiquidate(
ctx sdk.Context, liquidatorAddr, borrowerAddr sdk.AccAddress, repayDenom, rewardDenom string,
) (repaid sdk.Coin, reward sdk.Coin, err error) {
// If the message did not specify repay or reward denoms, select one arbitrarily (first in
// denom alphabetical order) from borrower position. Then proceed normally with the transaction.
EbrahimElbagory marked this conversation as resolved.
Show resolved Hide resolved
if repayDenom == "" {
borrowed := k.GetBorrowerBorrows(ctx, borrowerAddr)
if !borrowed.IsZero() {
repayDenom = borrowed[0].Denom
}
}
if rewardDenom == "" {
collateral := k.GetBorrowerCollateral(ctx, borrowerAddr)
if !collateral.IsZero() {
rewardDenom = types.ToTokenDenom(collateral[0].Denom)
}
}

if err := k.validateAcceptedDenom(ctx, repayDenom); err != nil {
return sdk.Coin{}, sdk.Coin{}, err
}
Expand Down
32 changes: 21 additions & 11 deletions x/leverage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2226,11 +2226,11 @@ func (s *IntegrationTestSuite) TestMsgLeveragedLiquidate() {
coin.New("u/"+atomDenom, 3_527933),
nil,
}, {
"close factor < 1",
"close factor < 1 with auto-selected repay and reward denoms",
liquidator,
closeBorrower,
umeeDenom,
umeeDenom,
"",
"",
coin.New(umeeDenom, 8_150541),
coin.New("u/"+umeeDenom, 8_965596),
nil,
Expand All @@ -2248,20 +2248,30 @@ func (s *IntegrationTestSuite) TestMsgLeveragedLiquidate() {
_, err := srv.LeveragedLiquidate(ctx, msg)
require.ErrorIs(err, tc.err, tc.msg)
} else {
baseRewardDenom := types.ToTokenDenom(tc.expectedReward.Denom)
// borrower initial state
biBalance := app.BankKeeper.GetAllBalances(ctx, tc.borrower)
biCollateral := app.LeverageKeeper.GetBorrowerCollateral(ctx, tc.borrower)
biBorrowed := app.LeverageKeeper.GetBorrowerBorrows(ctx, tc.borrower)

// adjust test case in empty-input scenarios, while preserving the msg
if msg.RepayDenom == "" {
if !biBorrowed.IsZero() {
tc.repayDenom = biBorrowed[0].Denom
}
}
if msg.RewardDenom == "" {
if !biCollateral.IsZero() {
tc.rewardDenom = types.ToTokenDenom(biCollateral[0].Denom)
}
}

// initial state (borrowed denom)
biUTokenSupply := app.LeverageKeeper.GetAllUTokenSupply(ctx)
biExchangeRate := app.LeverageKeeper.DeriveExchangeRate(ctx, tc.repayDenom)

// initial state (liquidated denom)
liUTokenSupply := app.LeverageKeeper.GetAllUTokenSupply(ctx)
liExchangeRate := app.LeverageKeeper.DeriveExchangeRate(ctx, baseRewardDenom)

// borrower initial state
biBalance := app.BankKeeper.GetAllBalances(ctx, tc.borrower)
biCollateral := app.LeverageKeeper.GetBorrowerCollateral(ctx, tc.borrower)
biBorrowed := app.LeverageKeeper.GetBorrowerBorrows(ctx, tc.borrower)
liExchangeRate := app.LeverageKeeper.DeriveExchangeRate(ctx, tc.rewardDenom)

// liquidator initial state
liBalance := app.BankKeeper.GetAllBalances(ctx, tc.liquidator)
Expand All @@ -2276,7 +2286,7 @@ func (s *IntegrationTestSuite) TestMsgLeveragedLiquidate() {

// final state (liquidated denom)
lfUTokenSupply := app.LeverageKeeper.GetAllUTokenSupply(ctx)
lfExchangeRate := app.LeverageKeeper.DeriveExchangeRate(ctx, baseRewardDenom)
lfExchangeRate := app.LeverageKeeper.DeriveExchangeRate(ctx, tc.rewardDenom)

// borrower final state
bfBalance := app.BankKeeper.GetAllBalances(ctx, tc.borrower)
Expand Down
Loading