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(perp): multi liquidate #1195

Merged
merged 12 commits into from
Feb 13, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### API Breaking

* [#1195](https://github.com/NibiruChain/nibiru/pull/1195) - feat(perp)!: Add `MultiLiquidation` feature for perps
* [#1158](https://github.com/NibiruChain/nibiru/pull/1158) - feat(asset-registry)!: Add `AssetRegistry`
* [#1171](https://github.com/NibiruChain/nibiru/pull/1171) - refactor(asset)!: Replace `common.AssetPair` with `asset.Pair`.
* [#1164](https://github.com/NibiruChain/nibiru/pull/1164) - refactor: remove client interface for liquidate msg
Expand Down
1 change: 1 addition & 0 deletions contrib/scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ add_genesis_param '.app_state.perp.params.liquidation_fee_ratio = "0.025"'
add_genesis_param '.app_state.perp.params.partial_liquidation_ratio = "0.25"'
add_genesis_param '.app_state.perp.params.funding_rate_interval = "30 min"'
add_genesis_param '.app_state.perp.params.twap_lookback_window = "900s"'
add_genesis_param '.app_state.perp.params.whitelisted_liquidators = ["nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl"]'
add_genesis_param '.app_state.perp.pair_metadata[0].pair = "ubtc:unusd"'
add_genesis_param '.app_state.perp.pair_metadata[0].latest_cumulative_premium_fraction = "0"'
add_genesis_param '.app_state.perp.pair_metadata[1].pair = "ueth:unusd"'
Expand Down
104 changes: 68 additions & 36 deletions proto/perp/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -208,49 +208,81 @@ message PositionSettledEvent {
// Emitted when a new funding rate is calculated.
message FundingRateChangedEvent {

// The pair for which the funding rate was calculated.
string pair = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/x/common/asset.Pair",
(gogoproto.nullable) = false
];
// The pair for which the funding rate was calculated.
string pair = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/x/common/asset.Pair",
(gogoproto.nullable) = false
];

// The mark price of the pair.
string mark_price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// The mark price of the pair.
string mark_price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// The oracle index price of the pair.
string index_price = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// The oracle index price of the pair.
string index_price = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// The latest funding rate.
string latest_funding_rate = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// The latest funding rate.
string latest_funding_rate = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// The latest premium fraction just calculated.
string latest_premium_fraction = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// The latest premium fraction just calculated.
string latest_premium_fraction = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// The latest cumulative premium fraction.
// The funding payment a position will pay is the difference between this value
// and the latest cumulative premium fraction on the position, multiplied by the position size.
string cumulative_premium_fraction = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// The latest cumulative premium fraction.
// The funding payment a position will pay is the difference between this value
// and the latest cumulative premium fraction on the position, multiplied by the position size.
string cumulative_premium_fraction = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// The block number at which the funding rate was calculated.
int64 block_height = 7;

// The block time in unix milliseconds at which the funding rate was calculated.
int64 block_time_ms = 8;
}


// Emitted when liquidation fails.
message LiquidationFailedEvent {
// The pair for which we are trying to liquidate.
string pair = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/x/common/asset.Pair",
(gogoproto.nullable) = false
];

// owner of the position.
string trader = 2;

// Address of the account that executed the tx.
string liquidator = 3;


enum LiquidationFailedReason {
UNSPECIFIED = 0;

// the position is healthy and does not need to be liquidated.
POSITION_HEALTHY = 1;

// The block number at which the funding rate was calculated.
int64 block_height = 7;
// the pair does not exist.
NONEXISTENT_PAIR = 2;

// The block time in unix milliseconds at which the funding rate was calculated.
int64 block_time_ms = 8;
// the position does not exist.
NONEXISTENT_POSITION = 3;
}
// Reason for the liquidation failure.
LiquidationFailedReason reason = 4;
}

message MetricsEvent {
Expand Down
25 changes: 11 additions & 14 deletions proto/perp/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,32 @@ message MsgAddMarginResponse {
Position position = 2;
}

// -------------------------- Liquidate --------------------------

message MsgLiquidateResponse {
cosmos.base.v1beta1.Coin fee_to_liquidator = 1 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin fee_to_perp_ecosystem_fund = 2 [(gogoproto.nullable) = false];
}
// -------------------------- Liquidation --------------------------

message MsgMultiLiquidate {
string sender = 1;

message MultiLiquidation {
message Liquidation {
string pair = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/x/common/asset.Pair",
(gogoproto.nullable) = false
];
string trader = 2;
}

repeated MultiLiquidation liquidations = 2;
repeated Liquidation liquidations = 2;
}

message MsgMultiLiquidateResponse {
message MultiLiquidateResponse {
oneof response {
string error = 1;
MsgLiquidateResponse liquidation = 2;
}
message LiquidationResponse {
bool success = 1;
string error = 2;

cosmos.base.v1beta1.Coin liquidator_fee = 3 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin perp_ef_fee = 4 [(gogoproto.nullable) = false]; // perp ecosystem fund
}
repeated MultiLiquidateResponse liquidation_responses = 1;

repeated LiquidationResponse liquidations = 1;
}

// -------------------------- OpenPosition --------------------------
Expand Down
6 changes: 3 additions & 3 deletions x/common/testutil/cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ func BuildNetworkConfig(appGenesis app.GenesisState) Config {
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
StartingTokens: sdk.NewCoins(
sdk.NewCoin(denoms.NUSD, sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.NIBI, sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.USDC, sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.NUSD, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.NIBI, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.USDC, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
),
PruningStrategy: storetypes.PruningOptionNothing,
CleanupDir: true,
Expand Down
Loading