Skip to content

Commit

Permalink
feat: add amount arg to leveragelp close cli and wasm bindings (#470)
Browse files Browse the repository at this point in the history
* feat: add amount arg to leveragelp close cli and wasm bindings

* fix: fix sender addr
  • Loading branch information
cosmic-vagabond authored Apr 26, 2024
1 parent cc23b05 commit 27dde91
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
17 changes: 12 additions & 5 deletions x/leveragelp/client/cli/tx_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ 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/leveragelp/types"
"github.com/spf13/cobra"
)

func CmdClose() *cobra.Command {
cmd := &cobra.Command{
Use: "close [position-id] [flags]",
Use: "close [position-id] [amount] [flags]",
Short: "Close leveragelp position",
Example: `elysd tx leveragelp close 1 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(1),
Example: `elysd tx leveragelp 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 @@ -27,14 +28,20 @@ func CmdClose() *cobra.Command {
return errors.New("signer address is missing")
}

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

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

msg := types.NewMsgClose(
signer.String(),
argPositionId,
argAmount,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
31 changes: 8 additions & 23 deletions x/leveragelp/client/wasm/msg_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ func (m *Messenger) msgClose(ctx sdk.Context, contractAddr sdk.AccAddress, msgCl
return nil, nil, wasmvmtypes.InvalidRequest{Err: "close wrong sender"}
}

res, err := PerformMsgClose(m.keeper, ctx, contractAddr, msgClose)
if err := msgClose.ValidateBasic(); err != nil {
return nil, nil, errorsmod.Wrap(err, "failed validating msgMsgClose")
}

msgServer := leveragelpkeeper.NewMsgServerImpl(*m.keeper)

res, err := msgServer.Close(sdk.WrapSDKContext(ctx), msgClose)
if err != nil {
return nil, nil, errorsmod.Wrap(err, "perform close")
return nil, nil, errorsmod.Wrap(err, "leveragelp close msg")
}

responseBytes, err := json.Marshal(*res)
Expand All @@ -34,24 +40,3 @@ func (m *Messenger) msgClose(ctx sdk.Context, contractAddr sdk.AccAddress, msgCl

return nil, resp, nil
}

func PerformMsgClose(f *leveragelpkeeper.Keeper, ctx sdk.Context, contractAddr sdk.AccAddress, msgClose *leveragelptypes.MsgClose) (*leveragelptypes.MsgCloseResponse, error) {
if msgClose == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "leveragelp close null leveragelp close"}
}
msgServer := leveragelpkeeper.NewMsgServerImpl(*f)

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

if err := msgMsgClose.ValidateBasic(); err != nil {
return nil, errorsmod.Wrap(err, "failed validating msgMsgClose")
}

_, err := msgServer.Close(sdk.WrapSDKContext(ctx), msgMsgClose) // Discard the response because it's empty
if err != nil {
return nil, errorsmod.Wrap(err, "leveragelp close msg")
}

resp := &leveragelptypes.MsgCloseResponse{}
return resp, nil
}
7 changes: 4 additions & 3 deletions x/leveragelp/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ var (
_ sdk.Msg = &MsgDewhitelist{}
)

func NewMsgClose(creator string, id uint64) *MsgClose {
func NewMsgClose(creator string, id uint64, amount math.Int) *MsgClose {
return &MsgClose{
Creator: creator,
Id: id,
Creator: creator,
Id: id,
LpAmount: amount,
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/perpetual/client/wasm/msg_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (m *Messenger) msgClose(ctx sdk.Context, contractAddr sdk.AccAddress, msgCl
return nil, nil, wasmvmtypes.InvalidRequest{Err: "close wrong sender"}
}

msgServer := perpetualkeeper.NewMsgServerImpl(*m.keeper)

if err := msgClose.ValidateBasic(); err != nil {
return nil, nil, errorsmod.Wrap(err, "failed validating msgMsgClose")
}

msgServer := perpetualkeeper.NewMsgServerImpl(*m.keeper)

res, err := msgServer.BrokerClose(sdk.WrapSDKContext(ctx), msgClose)
if err != nil {
return nil, nil, errorsmod.Wrap(err, "perpetual close msg")
Expand Down

0 comments on commit 27dde91

Please sign in to comment.