Skip to content

Commit

Permalink
feat: wire settlement cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Oct 10, 2023
1 parent 2714bb7 commit dd21676
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 92 deletions.
17 changes: 17 additions & 0 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,27 @@ service Msg {

rpc PartialClose(MsgPartialClose) returns (MsgPartialCloseResponse) {}

rpc SettlePosition(MsgSettlePosition) returns (MsgClosePositionResponse) {}

rpc DonateToEcosystemFund(MsgDonateToEcosystemFund)
returns (MsgDonateToEcosystemFundResponse) {}
}

// -------------------------- Settle Position --------------------------

/* MsgSettlePosition: Msg to remove margin. */
message MsgSettlePosition {
string sender = 1;

string pair = 2 [
(gogoproto.customtype) =
"github.com/NibiruChain/nibiru/x/common/asset.Pair",
(gogoproto.nullable) = false
];

uint64 version = 3;
}

// -------------------------- RemoveMargin --------------------------

/* MsgRemoveMargin: Msg to remove margin. */
Expand Down
46 changes: 46 additions & 0 deletions x/perp/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -29,6 +30,7 @@ func GetTxCmd() *cobra.Command {
AddMarginCmd(),
MarketOrderCmd(),
ClosePositionCmd(),
SettlePositionCmd(),
PartialCloseCmd(),
MultiLiquidateCmd(),
DonateToEcosystemFundCmd(),
Expand Down Expand Up @@ -315,6 +317,50 @@ func AddMarginCmd() *cobra.Command {
return cmd
}

func SettlePositionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "settle [market] [version]",
Short: "Settle position in a specific market version when the market is disabled",
Long: strings.TrimSpace(
fmt.Sprintf(`
$ %s tx perp settle osmo:nusd 1
`, version.AppName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

version, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

pair, err := asset.TryNewPair(args[0])
if err != nil {
return err
}

msg := &types.MsgSettlePosition{
Sender: clientCtx.GetFromAddress().String(),
Pair: pair,
Version: version,
}
if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func DonateToEcosystemFundCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "donate-ef [amount]",
Expand Down
18 changes: 18 additions & 0 deletions x/perp/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ func (m msgServer) MultiLiquidate(goCtx context.Context, req *types.MsgMultiLiqu
return &types.MsgMultiLiquidateResponse{Liquidations: resp}, nil
}

func (m msgServer) SettlePosition(ctx context.Context, msg *types.MsgSettlePosition) (*types.MsgClosePositionResponse, error) {
// These fields should have already been validated by MsgSettlePosition.ValidateBasic() prior to being sent to the msgServer.
traderAddr := sdk.MustAccAddressFromBech32(msg.Sender)
resp, err := m.k.SettlePosition(sdk.UnwrapSDKContext(ctx), msg.Pair, msg.Version, traderAddr)

if err != nil {
return nil, err
}

return &types.MsgClosePositionResponse{
ExchangedNotionalValue: resp.ExchangedNotionalValue,
ExchangedPositionSize: resp.ExchangedPositionSize,
FundingPayment: resp.FundingPayment,
RealizedPnl: resp.RealizedPnl,
MarginToTrader: resp.MarginToVault.Neg(),
}, nil
}

func (m msgServer) DonateToEcosystemFund(ctx context.Context, msg *types.MsgDonateToEcosystemFund) (*types.MsgDonateToEcosystemFundResponse, error) {
if err := m.k.BankKeeper.SendCoinsFromAccountToModule(
sdk.UnwrapSDKContext(ctx),
Expand Down
28 changes: 28 additions & 0 deletions x/perp/v2/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ func (m MsgClosePosition) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{signer}
}

// MsgSettlePosition

func (m MsgSettlePosition) Route() string { return "perp" }
func (m MsgSettlePosition) Type() string { return "settle_position_msg" }

func (m MsgSettlePosition) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil {
return sdkerrors.Wrapf(errors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if err := m.Pair.Validate(); err != nil {
return err
}

return nil
}

func (m MsgSettlePosition) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

func (m MsgSettlePosition) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(m.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}

// MsgDonateToEcosystemFund

func (m MsgDonateToEcosystemFund) Route() string { return "perp" }
Expand Down
42 changes: 42 additions & 0 deletions x/perp/v2/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,36 @@ func TestMsgValidateBasic(t *testing.T) {
true,
"decoding bech32 failed",
},
// MsgSettlePosition test cases
{
"Test MsgSettlePosition: Valid input",
&MsgSettlePosition{
Sender: validSender,
Pair: validPair,
Version: 1,
},
false,
"",
},
{
"Test MsgSettlePosition: Invalid pair",
&MsgSettlePosition{
Sender: validSender,
Pair: invalidPair,
Version: 1,
},
true,
"invalid base asset",
},
{
"Test MsgSettlePosition: Invalid sender",
&MsgSettlePosition{
Sender: "invalid",
Pair: validPair,
},
true,
"decoding bech32 failed",
},
// MsgPartialClose test cases
{
"Test MsgPartialClose: Valid input",
Expand Down Expand Up @@ -385,6 +415,7 @@ func TestMsg_GetSigners(t *testing.T) {
&MsgRemoveMargin{Sender: validSender},
&MsgMarketOrder{Sender: validSender},
&MsgClosePosition{Sender: validSender},
&MsgSettlePosition{Sender: validSender},
&MsgPartialClose{Sender: validSender},
&MsgDonateToEcosystemFund{Sender: validSender},
&MsgMultiLiquidate{Sender: validSender},
Expand All @@ -394,6 +425,7 @@ func TestMsg_GetSigners(t *testing.T) {
&MsgRemoveMargin{Sender: invalidSender},
&MsgMarketOrder{Sender: invalidSender},
&MsgClosePosition{Sender: invalidSender},
&MsgSettlePosition{Sender: invalidSender},
&MsgPartialClose{Sender: invalidSender},
&MsgDonateToEcosystemFund{Sender: invalidSender},
&MsgMultiLiquidate{Sender: invalidSender},
Expand Down Expand Up @@ -457,6 +489,12 @@ func TestMsg_RouteAndType(t *testing.T) {
expectedRoute: "perp",
expectedType: "close_position_msg",
},
{
name: "MsgSettlePosition",
msg: &MsgSettlePosition{},
expectedRoute: "perp",
expectedType: "settle_position_msg",
},
{
name: "MsgPartialClose",
msg: &MsgPartialClose{},
Expand Down Expand Up @@ -511,6 +549,10 @@ func TestMsg_GetSignBytes(t *testing.T) {
name: "MsgClosePosition",
msg: &MsgClosePosition{},
},
{
name: "MsgSettlePosition",
msg: &MsgSettlePosition{},
},
{
name: "MsgPartialClose",
msg: &MsgPartialClose{},
Expand Down
Loading

0 comments on commit dd21676

Please sign in to comment.