Skip to content

Commit

Permalink
feat: update margin close api
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Nov 29, 2023
1 parent 326301f commit 02dfc07
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 68 deletions.
13 changes: 13 additions & 0 deletions proto/elys/margin/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package elys.margin;
option go_package = "github.com/elys-network/elys/x/margin/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "elys/margin/params.proto";
import "elys/margin/types.proto";

Expand Down Expand Up @@ -36,10 +37,16 @@ message MsgOpenResponse {
message MsgClose {
string creator = 1;
uint64 id = 2;
cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false
];
}

message MsgCloseResponse {
uint64 id = 1;
cosmos.base.v1beta1.Coin amount = 2 [
(gogoproto.nullable) = false
];
}

message MsgBrokerOpen {
Expand All @@ -61,10 +68,16 @@ message MsgBrokerClose {
string creator = 1;
uint64 id = 2;
string owner = 3;
cosmos.base.v1beta1.Coin amount = 4 [
(gogoproto.nullable) = false
];
}

message MsgBrokerCloseResponse {
uint64 id = 1;
cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false
];
}

message MsgUpdateParams {
Expand Down
13 changes: 10 additions & 3 deletions x/margin/client/cli/tx_broker_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/margin/types"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +19,7 @@ func CmdBrokerClose() *cobra.Command {
Use: "broker-close [mtp-id] [mtp-owner] [flags]",
Short: "Close margin position as a broker",
Example: `elysd tx margin broker-close 1 sif123 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -29,8 +30,8 @@ func CmdBrokerClose() *cobra.Command {
return errors.New("signer address is missing")
}

argMtpId, ok := strconv.ParseUint(args[0], 10, 64)
if ok != nil {
argMtpId, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return errors.New("invalid mtp id")
}

Expand All @@ -39,10 +40,16 @@ func CmdBrokerClose() *cobra.Command {
return errors.New("invalid mtp owner address")
}

argAmount, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return errors.New("invalid amount")
}

msg := types.NewMsgBrokerClose(
signer.String(),
argMtpId,
argMtpOwner,
argAmount,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
17 changes: 12 additions & 5 deletions x/margin/client/cli/tx_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/margin/types"
"github.com/spf13/cobra"
)
Expand All @@ -15,10 +16,10 @@ var _ = strconv.Itoa(0)

func CmdClose() *cobra.Command {
cmd := &cobra.Command{
Use: "close [mtp-id] [flags]",
Use: "close [mtp-id] [amount] [flags]",
Short: "Close margin position",
Example: `elysd tx margin close 1 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(1),
Example: `elysd tx margin close 1 10000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -29,14 +30,20 @@ func CmdClose() *cobra.Command {
return errors.New("signer address is missing")
}

argMtpId, ok := strconv.ParseUint(args[0], 10, 64)
if ok != nil {
argMtpId, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return errors.New("invalid mtp id")
}

argAmount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return errors.New("invalid amount")
}

msg := types.NewMsgClose(
signer.String(),
argMtpId,
argAmount,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions x/margin/client/cli/tx_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestClosePosition(t *testing.T) {
// ...
args := []string{
"1",
"10000000uusd",
"--from=" + val.Address.String(),
"-y",
}
Expand Down
2 changes: 1 addition & 1 deletion x/margin/client/wasm/msg_broker_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func PerformMsgBrokerClose(f *marginkeeper.Keeper, ctx sdk.Context, contractAddr
}
msgServer := marginkeeper.NewMsgServerImpl(*f)

msgMsgBrokerClose := margintypes.NewMsgBrokerClose(msgBrokerClose.Creator, uint64(msgBrokerClose.Id), msgBrokerClose.Owner)
msgMsgBrokerClose := margintypes.NewMsgBrokerClose(msgBrokerClose.Creator, uint64(msgBrokerClose.Id), msgBrokerClose.Owner, msgBrokerClose.Amount)

if err := msgMsgBrokerClose.ValidateBasic(); err != nil {
return nil, errorsmod.Wrap(err, "failed validating msgMsgBrokerClose")
Expand Down
2 changes: 1 addition & 1 deletion x/margin/client/wasm/msg_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func PerformMsgClose(f *marginkeeper.Keeper, ctx sdk.Context, contractAddr sdk.A
}
msgServer := marginkeeper.NewMsgServerImpl(*f)

msgMsgClose := margintypes.NewMsgClose(msgClose.Creator, uint64(msgClose.Id))
msgMsgClose := margintypes.NewMsgClose(msgClose.Creator, uint64(msgClose.Id), msgClose.Amount)

if err := msgMsgClose.ValidateBasic(); err != nil {
return nil, errorsmod.Wrap(err, "failed validating msgMsgClose")
Expand Down
2 changes: 1 addition & 1 deletion x/margin/keeper/broker_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (k Keeper) BrokerClose(ctx sdk.Context, msg *types.MsgBrokerClose) (*types.MsgBrokerCloseResponse, error) {
msgClose := types.NewMsgClose(msg.Owner, msg.Id)
msgClose := types.NewMsgClose(msg.Owner, msg.Id, msg.Amount)

res, err := k.Close(ctx, msgClose)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions x/margin/keeper/invariant_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestCheckBalanceInvariant_InvalidBalance(t *testing.T) {
msg3 := margintypes.NewMsgClose(
addr[0].String(),
mtpId,
sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000000)),
)

_, err = mk.Close(ctx, msg3)
Expand Down
6 changes: 4 additions & 2 deletions x/margin/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ var (
_ sdk.Msg = &MsgDewhitelist{}
)

func NewMsgClose(creator string, id uint64) *MsgClose {
func NewMsgClose(creator string, id uint64, amount sdk.Coin) *MsgClose {
return &MsgClose{
Creator: creator,
Id: id,
Amount: amount,
}
}

Expand Down Expand Up @@ -104,11 +105,12 @@ func (msg *MsgOpen) ValidateBasic() error {
return nil
}

func NewMsgBrokerClose(creator string, id uint64, owner string) *MsgBrokerClose {
func NewMsgBrokerClose(creator string, id uint64, owner string, amount sdk.Coin) *MsgBrokerClose {
return &MsgBrokerClose{
Creator: creator,
Id: id,
Owner: owner,
Amount: amount,
}
}

Expand Down
Loading

0 comments on commit 02dfc07

Please sign in to comment.