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

Dev 2015 cancel spot orders #843

Merged
merged 17 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91856,9 +91856,9 @@ definitions:
order_id:
type: string
format: uint64
elys.tradeshield.MsgDeletePendingPerpetualOrderResponse:
elys.tradeshield.MsgCancelPerpetualOrdersResponse:
type: object
elys.tradeshield.MsgDeletePendingSpotOrderResponse:
elys.tradeshield.MsgCancelSpotOrdersResponse:
type: object
elys.tradeshield.MsgExecuteOrdersResponse:
type: object
Expand Down
25 changes: 13 additions & 12 deletions proto/elys/tradeshield/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ option go_package = "github.com/elys-network/elys/x/tradeshield/types";
service Msg {
rpc CreatePendingSpotOrder (MsgCreatePendingSpotOrder ) returns (MsgCreatePendingSpotOrderResponse );
rpc UpdatePendingSpotOrder (MsgUpdatePendingSpotOrder ) returns (MsgUpdatePendingSpotOrderResponse );
rpc DeletePendingSpotOrder (MsgDeletePendingSpotOrder ) returns (MsgDeletePendingSpotOrderResponse );
rpc CreatePendingPerpetualOrder (MsgCreatePendingPerpetualOrder) returns (MsgCreatePendingPerpetualOrderResponse);
rpc UpdatePendingPerpetualOrder (MsgUpdatePendingPerpetualOrder) returns (MsgUpdatePendingPerpetualOrderResponse);
rpc DeletePendingPerpetualOrder (MsgDeletePendingPerpetualOrder) returns (MsgDeletePendingPerpetualOrderResponse);
rpc CancelPerpetualOrders (MsgCancelPerpetualOrders ) returns (MsgCancelPerpetualOrdersResponse );
rpc UpdateParams (MsgUpdateParams ) returns (MsgUpdateParamsResponse );
rpc ExecuteOrders (MsgExecuteOrders ) returns (MsgExecuteOrdersResponse );
rpc CancelSpotOrders (MsgCancelSpotOrders ) returns (MsgCancelSpotOrdersResponse );
cosmic-vagabond marked this conversation as resolved.
Show resolved Hide resolved
}
message MsgCreatePendingSpotOrder {
SpotOrderType order_type = 1;
Expand All @@ -40,13 +40,6 @@ message MsgUpdatePendingSpotOrder {

message MsgUpdatePendingSpotOrderResponse {}

message MsgDeletePendingSpotOrder {
string owner_address = 1;
uint64 order_id = 2;
}

message MsgDeletePendingSpotOrderResponse {}

message MsgCreatePendingPerpetualOrder {
PerpetualOrderType order_type = 1;
OrderPrice trigger_price = 2;
Expand Down Expand Up @@ -80,12 +73,12 @@ message MsgUpdatePendingPerpetualOrder {

message MsgUpdatePendingPerpetualOrderResponse {}

message MsgDeletePendingPerpetualOrder {
message MsgCancelPerpetualOrders {
string owner_address = 1;
uint64 order_id = 2;
repeated uint64 order_ids = 2;
}

message MsgDeletePendingPerpetualOrderResponse {}
message MsgCancelPerpetualOrdersResponse {}

message MsgUpdateParams {

Expand All @@ -106,3 +99,11 @@ message MsgExecuteOrders {

message MsgExecuteOrdersResponse {}

message MsgUpdateStopLossResponse {}

message MsgCancelSpotOrders {
string creator = 1;
repeated uint64 spot_order_ids = 2;
}

message MsgCancelSpotOrdersResponse {}
4 changes: 2 additions & 2 deletions x/tradeshield/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

cmd.AddCommand(CmdCreatePendingSpotOrder())
cmd.AddCommand(CmdUpdatePendingSpotOrder())
cmd.AddCommand(CmdDeletePendingSpotOrder())
cmd.AddCommand(CmdCreatePendingPerpetualOrder())
cmd.AddCommand(CmdUpdatePendingPerpetualOrder())
cmd.AddCommand(CmdDeletePendingPerpetualOrder())
cmd.AddCommand(CmdCancelPerpetualOrders())

Check warning on line 37 in x/tradeshield/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/client/cli/tx.go#L37

Added line #L37 was not covered by tests
cmd.AddCommand(CmdUpdateParams())
cmd.AddCommand(CmdExecuteOrders())
cmd.AddCommand(CmdCancelSpotOrders())

Check warning on line 40 in x/tradeshield/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/client/cli/tx.go#L40

Added line #L40 was not covered by tests
// this line is used by starport scaffolding # 1

return cmd
Expand Down
11 changes: 6 additions & 5 deletions x/tradeshield/client/cli/tx_pending_perpetual_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ func CmdUpdatePendingPerpetualOrder() *cobra.Command {
return cmd
}

func CmdDeletePendingPerpetualOrder() *cobra.Command {
func CmdCancelPerpetualOrders() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-pending-perpetual-order [id]",
Short: "Delete a pending-perpetual-order by id",
Use: "cancel-perpetual-orders [ids.json]",
Short: "Cancel a pending-perpetual-orders by ids",
Example: "elysd tx perpetual cancel-perpetual-orders ids.json --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseUint(args[0], 10, 64)
ids, err := readPositionRequestJSON(args[0])
if err != nil {
return err
}
Expand All @@ -80,7 +81,7 @@ func CmdDeletePendingPerpetualOrder() *cobra.Command {
return err
}

msg := types.NewMsgDeletePendingPerpetualOrder(clientCtx.GetFromAddress().String(), id)
msg := types.NewMsgCancelPerpetualOrders(clientCtx.GetFromAddress().String(), ids)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
153 changes: 30 additions & 123 deletions x/tradeshield/client/cli/tx_pending_perpetual_order_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cli_test

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -57,128 +60,32 @@ func TestCreatePendingPerpetualOrder(t *testing.T) {
}
}

// TODO: Add this in message task
// func TestUpdatePendingPerpetualOrder(t *testing.T) {
// net := network.New(t)

// val := net.Validators[0]
// ctx := val.ClientCtx

// fields := []string{"xyz"}
// common := []string{
// fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
// fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
// fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
// fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
// }
// args := []string{}
// args = append(args, fields...)
// args = append(args, common...)
// _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreatePendingPerpetualOrder(), args)
// require.NoError(t, err)

// tests := []struct {
// desc string
// id string
// args []string
// code uint32
// err error
// }{
// {
// desc: "valid",
// id: "0",
// args: common,
// },
// {
// desc: "key not found",
// id: "1",
// args: common,
// code: sdkerrors.ErrKeyNotFound.ABCICode(),
// },
// {
// desc: "invalid key",
// id: "invalid",
// err: strconv.ErrSyntax,
// },
// }
// for _, tc := range tests {
// t.Run(tc.desc, func(t *testing.T) {
// require.NoError(t, net.WaitForNextBlock())

// args := []string{tc.id}
// args = append(args, fields...)
// args = append(args, tc.args...)
// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdatePendingPerpetualOrder(), args)
// if tc.err != nil {
// require.ErrorIs(t, err, tc.err)
// return
// }
// require.NoError(t, err)

// var resp sdk.TxResponse
// require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code))
// })
// }
// }

// func TestDeletePendingPerpetualOrder(t *testing.T) {
// net := network.New(t)

// val := net.Validators[0]
// ctx := val.ClientCtx

// fields := []string{"xyz"}
// common := []string{
// fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
// fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
// fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
// fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
// }
// args := []string{}
// args = append(args, fields...)
// args = append(args, common...)
// _, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreatePendingPerpetualOrder(), args)
// require.NoError(t, err)

// tests := []struct {
// desc string
// id string
// args []string
// code uint32
// err error
// }{
// {
// desc: "valid",
// id: "0",
// args: common,
// },
// {
// desc: "key not found",
// id: "1",
// args: common,
// code: sdkerrors.ErrKeyNotFound.ABCICode(),
// },
// {
// desc: "invalid key",
// id: "invalid",
// err: strconv.ErrSyntax,
// },
// }
// for _, tc := range tests {
// t.Run(tc.desc, func(t *testing.T) {
// require.NoError(t, net.WaitForNextBlock())
func TestCancelPendingPerpertualOrders(t *testing.T) {
net := setupNetwork(t)
ctx := net.Validators[0].ClientCtx
val := net.Validators[0]

// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDeletePendingPerpetualOrder(), append([]string{tc.id}, tc.args...))
// if tc.err != nil {
// require.ErrorIs(t, err, tc.err)
// return
// }
// require.NoError(t, err)
tmpFile, err := ioutil.TempFile("", "ids.json")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

validIds := []uint64{}
validJson, err := json.Marshal(validIds)
require.NoError(t, err)
_, err = tmpFile.Write(validJson)
require.NoError(t, err)
tmpFile.Close()

// Use baseURL to make API HTTP requests or use val.RPCClient to make direct
// Tendermint RPC calls.
// ...

args := []string{
tmpFile.Name(),
"--from=" + val.Address.String(),
"-y",
}

// var resp sdk.TxResponse
// require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code))
// })
// }
// }
_, err = clitestutil.ExecTestCLICmd(ctx, cli.CmdCancelPerpetualOrders(), args)
require.NoError(t, err)
}
27 changes: 22 additions & 5 deletions x/tradeshield/client/cli/tx_pending_spot_order.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"encoding/json"
"io/ioutil"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -66,13 +68,14 @@
return cmd
}

func CmdDeletePendingSpotOrder() *cobra.Command {
func CmdCancelSpotOrders() *cobra.Command {
cmd := &cobra.Command{
Use: "delete-pending-spot-order [id]",
Short: "Delete a pending-spot-order by id",
Use: "cancel-pending-spot-orders [ids.json]",
Short: "Cancel pending-spot-orders",
Example: "elysd tx perpetual cancel-pending-spot-orders ids.json --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.ParseUint(args[0], 10, 64)
ids, err := readPositionRequestJSON(args[0])
if err != nil {
return err
}
Expand All @@ -82,7 +85,7 @@
return err
}

msg := types.NewMsgDeletePendingSpotOrder(clientCtx.GetFromAddress().String(), id)
msg := types.NewMsgCancelSpotOrders(clientCtx.GetFromAddress().String(), ids)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand All @@ -94,3 +97,17 @@

return cmd
}

func readPositionRequestJSON(filename string) ([]uint64, error) {
var positions []uint64
bz, err := ioutil.ReadFile(filename)
if err != nil {
return []uint64{}, err

Check warning on line 105 in x/tradeshield/client/cli/tx_pending_spot_order.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/client/cli/tx_pending_spot_order.go#L105

Added line #L105 was not covered by tests
}
err = json.Unmarshal(bz, &positions)
if err != nil {
return []uint64{}, err

Check warning on line 109 in x/tradeshield/client/cli/tx_pending_spot_order.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/client/cli/tx_pending_spot_order.go#L109

Added line #L109 was not covered by tests
}

return positions, nil
}
44 changes: 44 additions & 0 deletions x/tradeshield/client/cli/tx_pending_spot_order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli_test

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/elys-network/elys/x/tradeshield/client/cli"
"github.com/stretchr/testify/require"
)

func TestCancelSpotOrders(t *testing.T) {
net := setupNetwork(t)
ctx := net.Validators[0].ClientCtx
val := net.Validators[0]

tmpFile, err := ioutil.TempFile("", "ids.json")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

validIds := []uint64{}
validJson, err := json.Marshal(validIds)
require.NoError(t, err)
_, err = tmpFile.Write(validJson)
require.NoError(t, err)
tmpFile.Close()

// Use baseURL to make API HTTP requests or use val.RPCClient to make direct
// Tendermint RPC calls.
// ...

args := []string{
tmpFile.Name(),
"--from=" + val.Address.String(),
"-y",
}

_, err = clitestutil.ExecTestCLICmd(ctx, cli.CmdCancelSpotOrders(), args)
require.NoError(t, err)
}


Loading
Loading