Skip to content

Commit

Permalink
refactor(perp): Refactor perp state proto (#479)
Browse files Browse the repository at this point in the history
* Remove LiquidityHistoryIndex

* Refactor position address to sdk.AccAddress type

* Remove VirtualPoolInfo proto

* Replace string address with sdk.AccAddress
  • Loading branch information
NibiruHeisenberg authored May 24, 2022
1 parent 5d0b7bc commit ed1695b
Show file tree
Hide file tree
Showing 24 changed files with 620 additions and 935 deletions.
47 changes: 30 additions & 17 deletions proto/perp/v1/state.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,36 @@ message GenesisState {
// the virtual liquidity pools.
message Position {
// address identifies the address owner of this position
string address = 1;
bytes trader_address = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

// pair identifies the pair associated with this position
string pair = 2;

// Position size.
string size = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false]; // signed int

// Amount of margin remaining in the position.
string margin = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// OpenNotional is the quote denom value of the position when opening.
// Used to calculate PnL.
string open_notional = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// The last cumulative funding payment this position has applied.
// Used to calculate the next funding payment.
string last_update_cumulative_premium_fraction = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false]; // int
int64 liquidity_history_index = 7;

// BlockNumber is the block number of the change to the position.
int64 block_number = 8;
int64 block_number = 7;
}

message PositionResp {
Expand Down Expand Up @@ -94,38 +102,30 @@ message PositionResp {
(gogoproto.nullable) = false];
}

message LiquidateResp{
message LiquidateResp {
// Amount of bad debt created by the liquidation event
string bad_debt = 1[
string bad_debt = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// Fee paid to the liquidator
string fee_to_liquidator = 2[
string fee_to_liquidator = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// Fee paid to the Perp EF fund
string fee_to_perp_ecosystem_fund = 3[
string fee_to_perp_ecosystem_fund = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

// Address of the liquidator
bytes liquidator = 4[
bytes liquidator = 4 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

// Position response from the close or open reverse position
PositionResp position_resp = 5;
}

message VirtualPoolInfo {
string pair = 1;
int64 last_restriction_block = 2;
repeated string cumulative_premium_fractions = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];
}

enum Side {
BUY = 0;
SELL = 1;
Expand Down Expand Up @@ -156,38 +156,51 @@ message PairMetadata {
}

message PositionChangedEvent {
string trader = 1;
bytes trader_address = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

string pair = 2;

string margin = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string position_notional = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string exchanged_position_size = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string fee = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false];

string position_size_after = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string realized_pnl = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string unrealized_pnl_after = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string bad_debt = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string liquidation_penalty = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string spot_price = 12 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];

string funding_payment = 13 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];
Expand Down
17 changes: 13 additions & 4 deletions proto/perp/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ service Msg {

/* MsgRemoveMargin: Msg to remove margin. */
message MsgRemoveMargin {
string sender = 1;
bytes sender = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

string token_pair = 2;

cosmos.base.v1beta1.Coin margin = 3 [(gogoproto.nullable) = false];
}

Expand All @@ -53,7 +56,9 @@ message MsgRemoveMarginResponse {

/* MsgAddMargin: Msg to remove margin. */
message MsgAddMargin {
string sender = 1;
bytes sender = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

string token_pair = 2;
cosmos.base.v1beta1.Coin margin = 3 [(gogoproto.nullable) = false];
}
Expand All @@ -66,7 +71,9 @@ message MsgAddMarginResponse {

message MsgLiquidate {
// Sender is the liquidator address
string sender = 1;
bytes sender = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

// TokenPair is the identifier for the position's virtual pool
string token_pair = 2;
// Trader is the address of the owner of the position
Expand All @@ -80,7 +87,9 @@ message MsgLiquidate {
// -------------------------- OpenPosition --------------------------

message MsgOpenPosition {
string sender = 1;
bytes sender = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

string token_pair = 2;
nibiru.perp.v1.Side side = 3;
string quote_asset_amount = 4 [
Expand Down
6 changes: 3 additions & 3 deletions x/perp/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func OpenPositionCmd() *cobra.Command {
}

msg := &types.MsgOpenPosition{
Sender: clientCtx.GetFromAddress().String(),
Sender: clientCtx.GetFromAddress(),
TokenPair: args[1],
Side: side,
QuoteAssetAmount: amount,
Expand Down Expand Up @@ -185,7 +185,7 @@ func RemoveMarginCmd() *cobra.Command {
}

msg := &types.MsgRemoveMargin{
Sender: clientCtx.GetFromAddress().String(),
Sender: clientCtx.GetFromAddress(),
TokenPair: args[0],
Margin: marginToRemove,
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func AddMarginCmd() *cobra.Command {
}

msg := &types.MsgAddMargin{
Sender: clientCtx.GetFromAddress().String(),
Sender: clientCtx.GetFromAddress(),
TokenPair: args[0],
Margin: marginToAdd,
}
Expand Down
18 changes: 9 additions & 9 deletions x/perp/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ const (
)

func NewTransferEvent(
coin sdk.Coin, from string, to string,
coin sdk.Coin, from sdk.AccAddress, to sdk.AccAddress,
) sdk.Event {
const EventTypeTransfer = "transfer"
return sdk.NewEvent(
EventTypeTransfer,
sdk.NewAttribute(AttributeFromAddr, from),
sdk.NewAttribute(AttributeToAddr, to),
sdk.NewAttribute(AttributeFromAddr, from.String()),
sdk.NewAttribute(AttributeToAddr, to.String()),
sdk.NewAttribute(AttributeTokenDenom, coin.Denom),
sdk.NewAttribute(AttributeTokenAmount, coin.Amount.String()),
)
}

func EmitTransfer(
ctx sdk.Context, coin sdk.Coin, from string, to string,
ctx sdk.Context, coin sdk.Coin, from sdk.AccAddress, to sdk.AccAddress,
) {
ctx.EventManager().EmitEvent(NewTransferEvent(coin, from, to))
}
Expand Down Expand Up @@ -264,26 +264,26 @@ Args:
*/
func EmitMarginChange(
ctx sdk.Context,
owner sdk.AccAddress,
traderAddr sdk.AccAddress,
vpool string,
marginAmt sdk.Int,
fundingPayment sdk.Dec,
) {
ctx.EventManager().EmitEvent(NewMarginChangeEvent(
owner, vpool, marginAmt, fundingPayment),
traderAddr, vpool, marginAmt, fundingPayment),
)
}

func NewMarginChangeEvent(
owner sdk.AccAddress,
traderAddr sdk.AccAddress,
vpool string,
marginAmt sdk.Int,
fundingPayment sdk.Dec,
) sdk.Event {
const EventTypeMarginChange = "margin_change"
return sdk.NewEvent(
EventTypeMarginChange,
sdk.NewAttribute(AttributePositionOwner, owner.String()),
sdk.NewAttribute(AttributePositionOwner, traderAddr.String()),
sdk.NewAttribute(AttributeVpool, vpool),
sdk.NewAttribute("margin_amt", marginAmt.String()),
sdk.NewAttribute("funding_payment", fundingPayment.String()),
Expand Down Expand Up @@ -313,7 +313,7 @@ func NewInternalPositionResponseEvent(
pos := positionResp.Position
return sdk.NewEvent(
"internal_position_response",
sdk.NewAttribute(AttributePositionOwner, pos.Address),
sdk.NewAttribute(AttributePositionOwner, pos.TraderAddress.String()),
sdk.NewAttribute(AttributeVpool, pos.Pair),
sdk.NewAttribute("pos_margin", pos.Margin.String()),
sdk.NewAttribute("pos_open_notional", pos.OpenNotional.String()),
Expand Down
8 changes: 4 additions & 4 deletions x/perp/keeper/calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestCalcRemainMarginWithFundingPayment(t *testing.T) {
sdk.MustNewDecFromStr("0.9"), // 0.9 ratio
/* y */ sdk.NewDec(1_000_000), //
/* x */ sdk.NewDec(1_000_000), //
/* fluctLim */ sdk.MustNewDecFromStr("1.0"), // 100%
/* fluctuationLimit */ sdk.MustNewDecFromStr("1.0"), // 100%
/* maxOracleSpreadRatio */ sdk.MustNewDecFromStr("1.0"), // 100%
)
premiumFractions := []sdk.Dec{sdk.ZeroDec()} // fPayment -> 0
Expand All @@ -75,7 +75,7 @@ func TestCalcRemainMarginWithFundingPayment(t *testing.T) {
})

pos := &types.Position{
Address: alice.String(), Pair: pair.String(),
TraderAddress: alice, Pair: pair.String(),
Margin: sdk.NewDec(100), Size_: sdk.NewDec(200),
LastUpdateCumulativePremiumFraction: premiumFractions[0],
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestCalcRemainMarginWithFundingPayment(t *testing.T) {
sdk.MustNewDecFromStr("0.9"), // 0.9 ratio
/* y */ sdk.NewDec(1_000_000), //
/* x */ sdk.NewDec(1_000_000), //
/* fluctLim */ sdk.MustNewDecFromStr("1.0"), // 100%
/* fluctuationLimit */ sdk.MustNewDecFromStr("1.0"), // 100%
/* maxOracleSpreadRatio */ sdk.MustNewDecFromStr("1.0"), // 100%
)
premiumFractions := []sdk.Dec{
Expand All @@ -128,7 +128,7 @@ func TestCalcRemainMarginWithFundingPayment(t *testing.T) {
})

pos := &types.Position{
Address: alice.String(), Pair: pair.String(),
TraderAddress: alice, Pair: pair.String(),
Margin: sdk.NewDec(100), Size_: sdk.NewDec(200),
LastUpdateCumulativePremiumFraction: premiumFractions[1],
}
Expand Down
10 changes: 5 additions & 5 deletions x/perp/keeper/calc_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_calcFreeCollateral(t *testing.T) {
fundingPayment := sdk.ZeroDec()
the3pool := "dai:usdc:usdt"
alice := sample.AccAddress()
pos := types.ZeroPosition(ctx, common.TokenPair(the3pool), alice.String())
pos := types.ZeroPosition(ctx, common.TokenPair(the3pool), alice)
_, err := k.calcFreeCollateral(ctx, *pos, fundingPayment)
assert.Error(t, err)
assert.ErrorContains(t, err, common.ErrInvalidTokenPair.Error())
Expand All @@ -37,7 +37,7 @@ func Test_calcFreeCollateral(t *testing.T) {
fundingPayment := sdk.ZeroDec()
validPair := common.TokenPair("xxx:yyy")
alice := sample.AccAddress()
pos := types.ZeroPosition(ctx, validPair, alice.String())
pos := types.ZeroPosition(ctx, validPair, alice)
mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, validPair).
Return(false)
_, err := k.calcFreeCollateral(ctx, *pos, fundingPayment)
Expand All @@ -53,7 +53,7 @@ func Test_calcFreeCollateral(t *testing.T) {
fundingPayment := sdk.ZeroDec()
validPair := common.TokenPair("xxx:yyy")
alice := sample.AccAddress()
pos := types.ZeroPosition(ctx, validPair, alice.String())
pos := types.ZeroPosition(ctx, validPair, alice)
mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, validPair).
Return(true)
freeCollateral, err := k.calcFreeCollateral(ctx, *pos, fundingPayment)
Expand All @@ -69,7 +69,7 @@ func Test_calcFreeCollateral(t *testing.T) {
fundingPayment := sdk.NewDec(10)
validPair := common.TokenPair("xxx:yyy")
alice := sample.AccAddress()
pos := types.ZeroPosition(ctx, validPair, alice.String())
pos := types.ZeroPosition(ctx, validPair, alice)
mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, validPair).
Return(true)
freeCollateral, err := k.calcFreeCollateral(ctx, *pos, fundingPayment)
Expand All @@ -85,7 +85,7 @@ func Test_calcFreeCollateral(t *testing.T) {
fundingPayment := sdk.NewDec(-100)
validPair := common.TokenPair("xxx:yyy")
alice := sample.AccAddress()
pos := types.ZeroPosition(ctx, validPair, alice.String())
pos := types.ZeroPosition(ctx, validPair, alice)
mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, validPair).
Return(true)
freeCollateral, err := k.calcFreeCollateral(ctx, *pos, fundingPayment)
Expand Down
Loading

0 comments on commit ed1695b

Please sign in to comment.