Skip to content

Commit

Permalink
feat: add param SmallLiquidationSize (#723) (#728)
Browse files Browse the repository at this point in the history
(cherry picked from commit d9903f7)

Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com>
  • Loading branch information
mergify[bot] and toteki committed Mar 30, 2022
1 parent 5f20742 commit 4750b65
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 46 deletions.
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/

### Improvements

- [723](https://github.com/umee-network/umee/pull/723) Add leverage parameter SmallLiquidationSize, which determines the USD value at which a borrow is considered small enough to be liquidated in a single transaction.
- [711](https://github.com/umee-network/umee/pull/711) Clarify error message for negative elapsed time case.

## [v2.0.0](https://github.com/umee-network/umee/releases/tag/v2.0.0) - 2022-03-25
Expand Down
7 changes: 7 additions & 0 deletions proto/umee/leverage/v1beta1/leverage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ message Params {
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"oracle_reward_factor\""
];
// The small_liquidation_size determines the USD value at which a borrow is considered small
// enough to be liquidated in a single transaction, bypassing dynamic close factor.
string small_liquidation_size = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"small_liquidation_size\""
];
}

// Token defines a token, along with its capital metadata, in the Umee capital
Expand Down
7 changes: 7 additions & 0 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ func (k Keeper) LiquidationParams(
}

params := k.GetParams(ctx)

// special case: If borrowed value is less than small liquidation size,
// close factor is always 1
if borrowed.LTE(params.SmallLiquidationSize) {
return liquidationIncentive, sdk.OneDec(), nil
}

// special case: If complete liquidation threshold is zero, close factor is always 1
if params.CompleteLiquidationThreshold.IsZero() {
return liquidationIncentive, sdk.OneDec(), nil
Expand Down
13 changes: 13 additions & 0 deletions x/leverage/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
completeLiquidationThresholdKey = "complete_liquidation_threshold"
minimumCloseFactorKey = "minimum_close_factor"
oracleRewardFactorKey = "oracle_reward_factor"
smallLiquidationSizeKey = "small_liquidation_size"
)

// GenCompleteLiquidationThreshold produces a randomized CompleteLiquidationThreshold in the range of [0.050, 0.100]
Expand All @@ -32,6 +33,11 @@ func GenOracleRewardFactor(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(005, 3).Add(sdk.NewDecWithPrec(int64(r.Intn(995)), 3))
}

// GenSmallLiquidationSize produces a randomized SmallLiquidationSize in the range of [0, 1000]
func GenSmallLiquidationSize(r *rand.Rand) sdk.Dec {
return sdk.NewDec(int64(r.Intn(1000)))
}

// RandomizedGenState generates a random GenesisState for oracle
func RandomizedGenState(simState *module.SimulationState) {
var completeLiquidationThreshold sdk.Dec
Expand All @@ -52,11 +58,18 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { oracleRewardFactor = GenOracleRewardFactor(r) },
)

var smallLiquidationSize sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, smallLiquidationSizeKey, &smallLiquidationSize, simState.Rand,
func(r *rand.Rand) { smallLiquidationSize = GenSmallLiquidationSize(r) },
)

leverageGenesis := types.NewGenesisState(
types.Params{
CompleteLiquidationThreshold: completeLiquidationThreshold,
MinimumCloseFactor: minimumCloseFactor,
OracleRewardFactor: oracleRewardFactor,
SmallLiquidationSize: smallLiquidationSize,
},
[]types.Token{},
[]types.AdjustedBorrow{},
Expand Down
5 changes: 5 additions & 0 deletions x/leverage/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ func ParamChanges(r *rand.Rand) []simtypes.ParamChange {
return fmt.Sprintf("\"%s\"", GenOracleRewardFactor(r))
},
),
simulation.NewSimParamChange(types.ModuleName, string(types.KeySmallLiquidationSize),
func(r *rand.Rand) string {
return fmt.Sprintf("\"%s\"", GenSmallLiquidationSize(r))
},
),
}
}
12 changes: 9 additions & 3 deletions x/leverage/spec/07_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ The leverage module contains the following parameters:
| CompleteLiquidationThreshold | sdk.Dec | 0.1 |
| MinimumCloseFactor | sdk.Dec | 0.01 |
| OracleRewardFactor | sdk.Dec | 0.01 |
| SmallLiquidationSize | sdk.Dec | 100.00 |

## CompleteLiquidationThreshold

CompleteLiquidationThreshold governs how far above their borrow limit a borrower
CompleteLiquidationThreshold governs how far above their liquidation limit a borrower
must be to have a [Close Factor](01_concepts.md#Close-Factor) of 1.0 - that is,
to be eligible for full liquidation in a single liquidation event.

## MinimumCloseFactor

MinimumCloseFactor is the [Close Factor](01_concepts.md#Close-Factor) for
borrows that are just above their borrow limit.
borrows that are just above their liquidation limit.

## OracleRewardFactor

OracleRewardFactor is the portion of borrow interest accrued that goes to fund
the `x/oracle` reward pool.
the `x/oracle` reward pool.

## SmallLiquidationSize

SmallLiquidationSize is the borrow value in USD below which [Close Factor](01_concepts.md#Close-Factor)
is always 1.
137 changes: 94 additions & 43 deletions x/leverage/types/leverage.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions x/leverage/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ var (
KeyCompleteLiquidationThreshold = []byte("CompleteLiquidationThreshold")
KeyMinimumCloseFactor = []byte("MinimumCloseFactor")
KeyOracleRewardFactor = []byte("OracleRewardFactor")
KeySmallLiquidationSize = []byte("SmallLiquidationSize")
)

var (
defaultCompleteLiquidationThreshold = sdk.MustNewDecFromStr("0.1")
defaultMinimumCloseFactor = sdk.MustNewDecFromStr("0.01")
defaultOracleRewardFactor = sdk.MustNewDecFromStr("0.01")
defaultSmallLiquidationSize = sdk.MustNewDecFromStr("100.00")
)

func NewParams() Params {
Expand All @@ -45,6 +47,11 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
&p.OracleRewardFactor,
validateOracleRewardFactor,
),
paramtypes.NewParamSetPair(
KeySmallLiquidationSize,
&p.SmallLiquidationSize,
validateSmallLiquidationSize,
),
}
}

Expand All @@ -66,6 +73,7 @@ func DefaultParams() Params {
CompleteLiquidationThreshold: defaultCompleteLiquidationThreshold,
MinimumCloseFactor: defaultMinimumCloseFactor,
OracleRewardFactor: defaultOracleRewardFactor,
SmallLiquidationSize: defaultSmallLiquidationSize,
}
}

Expand All @@ -80,6 +88,9 @@ func (p Params) Validate() error {
if err := validateOracleRewardFactor(p.OracleRewardFactor); err != nil {
return err
}
if err := validateSmallLiquidationSize(p.SmallLiquidationSize); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -127,3 +138,16 @@ func validateOracleRewardFactor(i interface{}) error {

return nil
}

func validateSmallLiquidationSize(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNegative() {
return fmt.Errorf("small liquidation size cannot be negative: %d", v)
}

return nil
}

0 comments on commit 4750b65

Please sign in to comment.