Skip to content

Commit

Permalink
fix: represent bad_debt as Coin instead of Dec (#685)
Browse files Browse the repository at this point in the history
* change: make bad debt a coin

* solve unit tests

Co-authored-by: AgentSmithMatrix <[email protected]>
Co-authored-by: Walter White <[email protected]>
Co-authored-by: Agent Smith <[email protected]>
  • Loading branch information
4 people authored Jul 8, 2022
1 parent b080568 commit 1d909d2
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 176 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

- [#686](https://github.com/NibiruChain/nibiru/pull/686) Add changelog enforcer to github actions.

- [#686](https://github.com/NibiruChain/nibiru/pull/686) Reorganize PerpKeeper methods

### State Machine Breaking Change

- [*687](https://github.com/NibiruChain/nibiru/pull/686) Emit `PositionChangedEvent` upon changing margin.
- [#687](https://github.com/NibiruChain/nibiru/pull/687) Emit `PositionChangedEvent` upon changing margin.
- [#685](https://github.com/NibiruChain/nibiru/pull/685) Represent `PositionChangedEvent` bad debt as Coin.
76 changes: 0 additions & 76 deletions go.sum

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions proto/perp/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ message PositionChangedEvent {
];

// Amount of bad debt cleared by the PerpEF during the change. Bad debt is negative net margin past the liquidation point of a position.
string bad_debt = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
cosmos.base.v1beta1.Coin bad_debt = 10 [(gogoproto.nullable) = false];

// amt of margin (y) lost due to liquidation
string liquidation_penalty = 11 [
Expand Down Expand Up @@ -130,12 +127,10 @@ message PositionLiquidatedEvent {
];

// Bad debt (margin units) cleared by the PerpEF during the tx. Bad debt is negative net margin past the liquidation point of a position.
string bad_debt = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
cosmos.base.v1beta1.Coin bad_debt = 8 [(gogoproto.nullable) = false];


// Remaining margin in the position after liquidation
// Remaining margin in the position after liquidation
cosmos.base.v1beta1.Coin margin = 9 [
(gogoproto.moretags) = "yaml:\"margin\"",
(gogoproto.nullable) = false
Expand Down
2 changes: 1 addition & 1 deletion x/perp/keeper/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (k Keeper) afterPositionUpdate(
PositionSize: positionResp.Position.Size_,
RealizedPnl: positionResp.RealizedPnl,
UnrealizedPnlAfter: positionResp.UnrealizedPnlAfter,
BadDebt: positionResp.BadDebt,
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), positionResp.BadDebt.RoundInt()),
LiquidationPenalty: sdk.ZeroDec(),
SpotPrice: spotPrice,
FundingPayment: positionResp.FundingPayment,
Expand Down
2 changes: 1 addition & 1 deletion x/perp/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ func TestClosePosition(t *testing.T) {
PositionSize: sdk.ZeroDec(),
RealizedPnl: tc.expectedRealizedPnl,
UnrealizedPnlAfter: sdk.ZeroDec(),
BadDebt: sdk.ZeroDec(),
BadDebt: sdk.NewCoin(common.PairBTCStable.GetQuoteTokenDenom(), sdk.ZeroInt()),
LiquidationPenalty: sdk.ZeroDec(),
SpotPrice: tc.newPositionNotional.Quo(tc.initialPosition.Size_.Abs()),
FundingPayment: sdk.MustNewDecFromStr("0.02").Mul(tc.initialPosition.Size_),
Expand Down
4 changes: 2 additions & 2 deletions x/perp/keeper/liquidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (k Keeper) ExecuteFullLiquidation(
LiquidatorAddress: liquidator.String(),
FeeToLiquidator: sdk.NewCoin(position.Pair.GetQuoteTokenDenom(), feeToLiquidator.RoundInt()),
FeeToEcosystemFund: sdk.NewCoin(position.Pair.GetQuoteTokenDenom(), feeToPerpEcosystemFund.RoundInt()),
BadDebt: totalBadDebt,
BadDebt: sdk.NewCoin(position.Pair.GetQuoteTokenDenom(), totalBadDebt.RoundInt()),
Margin: sdk.NewCoin(position.Pair.GetQuoteTokenDenom(), liquidationResp.PositionResp.Position.Margin.RoundInt()),
PositionNotional: liquidationResp.PositionResp.PositionNotional,
PositionSize: liquidationResp.PositionResp.Position.Size_,
Expand Down Expand Up @@ -324,7 +324,7 @@ func (k Keeper) ExecutePartialLiquidation(
LiquidatorAddress: liquidator.String(),
FeeToLiquidator: sdk.NewCoin(currentPosition.Pair.GetQuoteTokenDenom(), feeToLiquidator.RoundInt()),
FeeToEcosystemFund: sdk.NewCoin(currentPosition.Pair.GetQuoteTokenDenom(), feeToPerpEcosystemFund.RoundInt()),
BadDebt: liquidationResponse.BadDebt.ToDec(),
BadDebt: sdk.NewCoin(currentPosition.Pair.GetQuoteTokenDenom(), liquidationResponse.BadDebt),
Margin: sdk.NewCoin(currentPosition.Pair.GetQuoteTokenDenom(), liquidationResponse.PositionResp.Position.Margin.RoundInt()),
PositionNotional: liquidationResponse.PositionResp.PositionNotional,
PositionSize: liquidationResponse.PositionResp.Position.Size_,
Expand Down
4 changes: 2 additions & 2 deletions x/perp/keeper/liquidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestExecuteFullLiquidation(t *testing.T) {
LiquidatorAddress: liquidatorAddr.String(),
FeeToLiquidator: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.FeeToLiquidator),
FeeToEcosystemFund: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.FeeToPerpEcosystemFund),
BadDebt: liquidationResp.BadDebt.ToDec(),
BadDebt: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.BadDebt),
Margin: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), sdk.ZeroInt()),
PositionNotional: liquidationResp.PositionResp.PositionNotional,
PositionSize: sdk.ZeroDec(),
Expand Down Expand Up @@ -606,7 +606,7 @@ func TestExecutePartialLiquidation(t *testing.T) {
LiquidatorAddress: liquidator.String(),
FeeToLiquidator: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.FeeToLiquidator),
FeeToEcosystemFund: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.FeeToPerpEcosystemFund),
BadDebt: liquidationResp.BadDebt.ToDec(),
BadDebt: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), liquidationResp.BadDebt),
Margin: sdk.NewCoin(tokenPair.GetQuoteTokenDenom(), newPosition.Margin.RoundInt()),
PositionNotional: liquidationResp.PositionResp.PositionNotional,
PositionSize: newPosition.Size_,
Expand Down
8 changes: 4 additions & 4 deletions x/perp/keeper/liquidate_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestLiquidateIntoPartialLiquidation(t *testing.T) {
LiquidatorAddress: liquidatorAddr.String(),
FeeToLiquidator: tc.expectedLiquidatorFee,
FeeToEcosystemFund: tc.expectedPerpEFFee,
BadDebt: sdk.ZeroDec(),
BadDebt: sdk.NewCoin(common.DenomStable, sdk.ZeroInt()),
Margin: sdk.NewCoin(common.DenomStable, tc.expectedPositionMargin.RoundInt()),
PositionNotional: tc.newPositionNotional.Sub(tc.exchangedNotional),
PositionSize: tc.initialPositionSize.Sub(tc.exchangedSize),
Expand Down Expand Up @@ -353,7 +353,7 @@ func TestLiquidateIntoFullLiquidation(t *testing.T) {
LiquidatorAddress: liquidatorAddr.String(),
FeeToLiquidator: tc.expectedLiquidatorFee,
FeeToEcosystemFund: tc.expectedPerpEFFee,
BadDebt: sdk.ZeroDec(),
BadDebt: sdk.NewCoin(common.DenomStable, sdk.ZeroInt()),
Margin: sdk.NewCoin(common.DenomStable, sdk.ZeroInt()),
PositionNotional: sdk.ZeroDec(), // always zero
PositionSize: sdk.ZeroDec(), // always zero
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestLiquidateIntoFullLiquidationWithBadDebt(t *testing.T) {
LiquidatorAddress: liquidatorAddr.String(),
FeeToLiquidator: tc.expectedLiquidatorFee,
FeeToEcosystemFund: tc.expectedPerpEFFee,
BadDebt: tc.expectedLiquidationBadDebt.Add(tc.expectedPositionBadDebt),
BadDebt: sdk.NewCoin(common.DenomStable, tc.expectedLiquidationBadDebt.Add(tc.expectedPositionBadDebt).RoundInt()),
Margin: sdk.NewInt64Coin(common.DenomStable, 0),
PositionNotional: sdk.ZeroDec(), // always zero
PositionSize: sdk.ZeroDec(), // always zero
Expand Down Expand Up @@ -996,7 +996,7 @@ func TestExecuteFullLiquidation(t *testing.T) {
LiquidatorAddress: liquidatorAddr.String(),
FeeToLiquidator: sdk.NewCoin(common.PairBTCStable.GetQuoteTokenDenom(), tc.expectedFundsToLiquidator),
FeeToEcosystemFund: sdk.NewCoin(common.PairBTCStable.GetQuoteTokenDenom(), tc.expectedFundsToPerpEF),
BadDebt: tc.expectedLiquidationBadDebt.ToDec(),
BadDebt: sdk.NewCoin(common.DenomStable, tc.expectedLiquidationBadDebt),
Margin: sdk.NewCoin(common.PairBTCStable.GetQuoteTokenDenom(), newPosition.Margin.RoundInt()),
PositionNotional: positionResp.PositionNotional,
PositionSize: newPosition.Size_,
Expand Down
4 changes: 2 additions & 2 deletions x/perp/keeper/margin.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) AddMargin(
PositionSize: position.Size_,
RealizedPnl: sdk.ZeroDec(), // always zero when adding margin
UnrealizedPnlAfter: unrealizedPnl,
BadDebt: remainingMargin.BadDebt, // always zero when adding margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), remainingMargin.BadDebt.RoundInt()), // always zero when adding margin
FundingPayment: remainingMargin.FundingPayment,
SpotPrice: spotPrice,
BlockHeight: ctx.BlockHeight(),
Expand Down Expand Up @@ -203,7 +203,7 @@ func (k Keeper) RemoveMargin(
PositionSize: position.Size_,
RealizedPnl: sdk.ZeroDec(), // always zero when removing margin
UnrealizedPnlAfter: unrealizedPnl,
BadDebt: remainingMargin.BadDebt, // always zero when removing margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), remainingMargin.BadDebt.RoundInt()), // always zero when removing margin
FundingPayment: remainingMargin.FundingPayment,
SpotPrice: spotPrice,
BlockHeight: ctx.BlockHeight(),
Expand Down
2 changes: 1 addition & 1 deletion x/perp/keeper/margin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func TestRemoveMargin(t *testing.T) {
PositionSize: sdk.MustNewDecFromStr("299.910026991902429271"),
RealizedPnl: sdk.ZeroDec(), // always zero when removing margin
UnrealizedPnlAfter: sdk.ZeroDec(),
BadDebt: sdk.ZeroDec(), // always zero when removing margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin
FundingPayment: sdk.ZeroDec(),
SpotPrice: sdk.MustNewDecFromStr("1.00060009"),
BlockHeight: ctx.BlockHeight(),
Expand Down
6 changes: 3 additions & 3 deletions x/perp/keeper/margin_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func TestRemoveMargin(t *testing.T) {
PositionSize: sdk.NewDec(1000),
RealizedPnl: sdk.ZeroDec(), // always zero when removing margin
UnrealizedPnlAfter: sdk.ZeroDec(),
BadDebt: sdk.ZeroDec(), // always zero when removing margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin
FundingPayment: sdk.ZeroDec(),
SpotPrice: sdk.OneDec(),
BlockHeight: ctx.BlockHeight(),
Expand Down Expand Up @@ -630,7 +630,7 @@ func TestAddMargin(t *testing.T) {
PositionSize: sdk.NewDec(1000),
RealizedPnl: sdk.ZeroDec(), // always zero when adding margin
UnrealizedPnlAfter: sdk.ZeroDec(),
BadDebt: sdk.ZeroDec(), // always zero when adding margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin
FundingPayment: sdk.ZeroDec(),
SpotPrice: sdk.OneDec(),
BlockHeight: ctx.BlockHeight(),
Expand Down Expand Up @@ -715,7 +715,7 @@ func TestAddMargin(t *testing.T) {
PositionSize: sdk.NewDec(1000),
RealizedPnl: sdk.ZeroDec(), // always zero when adding margin
UnrealizedPnlAfter: sdk.ZeroDec(),
BadDebt: sdk.ZeroDec(), // always zero when adding margin
BadDebt: sdk.NewCoin(pair.GetQuoteTokenDenom(), sdk.ZeroInt()), // always zero when adding margin
FundingPayment: sdk.OneDec(),
SpotPrice: sdk.OneDec(),
BlockHeight: ctx.BlockHeight(),
Expand Down
Loading

0 comments on commit 1d909d2

Please sign in to comment.