Skip to content

Commit

Permalink
chore: Merge branch '114-market-order' of github.com:cosmosquad-labs/…
Browse files Browse the repository at this point in the history
…squad into 50-genesis
  • Loading branch information
hallazzang committed Jan 26, 2022
2 parents 10f3844 + 1338143 commit 9fcdd7e
Show file tree
Hide file tree
Showing 29 changed files with 1,525 additions and 558 deletions.
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ paths:
last_swap_request_id:
type: string
format: uint64
last_cancel_swap_request_id:
last_cancel_order_request_id:
type: string
format: uint64
last_price:
Expand Down Expand Up @@ -2301,7 +2301,7 @@ paths:
last_swap_request_id:
type: string
format: uint64
last_cancel_swap_request_id:
last_cancel_order_request_id:
type: string
format: uint64
last_price:
Expand Down Expand Up @@ -5213,7 +5213,7 @@ definitions:
last_swap_request_id:
type: string
format: uint64
last_cancel_swap_request_id:
last_cancel_order_request_id:
type: string
format: uint64
last_price:
Expand Down Expand Up @@ -5543,7 +5543,7 @@ definitions:
last_swap_request_id:
type: string
format: uint64
last_cancel_swap_request_id:
last_cancel_order_request_id:
type: string
format: uint64
last_price:
Expand Down Expand Up @@ -5573,7 +5573,7 @@ definitions:
last_swap_request_id:
type: string
format: uint64
last_cancel_swap_request_id:
last_cancel_order_request_id:
type: string
format: uint64
last_price:
Expand Down
2 changes: 1 addition & 1 deletion proto/squad/liquidity/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ message GenesisState {

repeated SwapRequest swap_requests = 8 [(gogoproto.nullable) = false];

repeated CancelSwapRequest cancel_swap_requests = 9 [(gogoproto.nullable) = false];
repeated CancelOrderRequest cancel_order_requests = 9 [(gogoproto.nullable) = false];
}
8 changes: 4 additions & 4 deletions proto/squad/liquidity/v1beta1/liquidity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ message Pair {

uint64 last_swap_request_id = 5;

uint64 last_cancel_swap_request_id = 6;
uint64 last_cancel_order_request_id = 6;

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

Expand Down Expand Up @@ -169,8 +169,8 @@ message SwapRequest {
SwapRequestStatus status = 14;
}

// CancelSwapRequest defines a request to cancel a swap request.
message CancelSwapRequest {
// CancelOrderRequest defines a request to cancel an order.
message CancelOrderRequest {
uint64 id = 1;

uint64 pair_id = 2;
Expand Down Expand Up @@ -241,4 +241,4 @@ enum SwapRequestStatus {

// SWAP_REQUEST_STATUS_EXPIRED indicates the request is expired
SWAP_REQUEST_STATUS_EXPIRED = 6 [(gogoproto.enumvalue_customname) = "SwapRequestStatusExpired"];
}
}
58 changes: 44 additions & 14 deletions proto/squad/liquidity/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ service Msg {
// WithdrawBatch defines a method for withdrawing pool coin from the pool
rpc WithdrawBatch(MsgWithdrawBatch) returns (MsgWithdrawBatchResponse);

// SwapBatch defines a method for swapping X coin to Y coin from the pool
rpc SwapBatch(MsgSwapBatch) returns (MsgSwapBatchResponse);
// LimitOrderBatch defines a method for making a limit order
rpc LimitOrderBatch(MsgLimitOrderBatch) returns (MsgLimitOrderBatchResponse);

// CancelSwapBatch defines a method for cancelling a swap request
rpc CancelSwapBatch(MsgCancelSwapBatch) returns (MsgCancelSwapBatchResponse);
// MarketOrderBatch defines a method for making a market order
rpc MarketOrderBatch(MsgMarketOrderBatch) returns (MsgMarketOrderBatchResponse);

// CancelOrderBatch defines a method for cancelling an order
rpc CancelOrderBatch(MsgCancelOrderBatch) returns (MsgCancelOrderBatchResponse);
}

// MsgCreatePair defines an SDK message for creating a pair.
Expand Down Expand Up @@ -91,9 +94,9 @@ message MsgWithdrawBatch {
// MsgWithdrawBatchResponse defines the Msg/WithdrawBatch response type.
message MsgWithdrawBatchResponse {}

// MsgSwapBatch defines an SDK message for swapping X coin to Y coin from the pool
message MsgSwapBatch {
// orderer specifies the bech32-encoded address that makes an order to swap from the pool
// MsgLimitOrderBatch defines an SDK message for making a limit order
message MsgLimitOrderBatch {
// orderer specifies the bech32-encoded address that makes an order
string orderer = 1;

// pair_id specifies the pair id
Expand All @@ -118,12 +121,39 @@ message MsgSwapBatch {
google.protobuf.Duration order_lifespan = 8 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
}

// MsgSwapBatchResponse defines the Msg/SwapBatch response type.
message MsgSwapBatchResponse {}
// MsgLimitOrderBatchResponse defines the Msg/LimitOrderBatch response type.
message MsgLimitOrderBatchResponse {}

// MsgMarketOrderBatch defines an SDK message for making a market order
message MsgMarketOrderBatch {
// orderer specifies the bech32-encoded address that makes an order
string orderer = 1;

// pair_id specifies the pair id
uint64 pair_id = 2;

// direction specifies the swap direction(buy or sell)
SwapDirection direction = 3;

// offer_coin specifies the amount of coin the orderer offers
cosmos.base.v1beta1.Coin offer_coin = 4 [(gogoproto.nullable) = false];

// demand_coin_denom specifies the demand coin denom
string demand_coin_denom = 5;

// amount specifies the amount of base coin the orderer wants to buy or sell
string amount = 6 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];

// order_lifespan specifies the order lifespan
google.protobuf.Duration order_lifespan = 7 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
}

// MsgMarketOrderBatchResponse defines the Msg/MarketOrderBatch response type.
message MsgMarketOrderBatchResponse {}

// MsgCancelSwapBatch defines an SDK message for cancelling a swap request
message MsgCancelSwapBatch {
// orderer specifies the bech32-encoded address that makes an order to swap from the pool
// MsgCancelOrderBatch defines an SDK message for cancelling an order
message MsgCancelOrderBatch {
// orderer specifies the bech32-encoded address that makes an order
string orderer = 1;

// pair_id specifies the pair id
Expand All @@ -133,5 +163,5 @@ message MsgCancelSwapBatch {
uint64 swap_request_id = 3;
}

// MsgCancelSwapBatchResponse defines the Msg/CancelSwapBatch response type.
message MsgCancelSwapBatchResponse {}
// MsgCancelOrderBatchResponse defines the Msg/CancelOrderBatch response type.
message MsgCancelOrderBatchResponse {}
116 changes: 101 additions & 15 deletions x/liquidity/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func GetTxCmd() *cobra.Command {
NewCreatePoolCmd(),
NewDepositBatchCmd(),
NewWithdrawBatchCmd(),
NewSwapBatchCmd(),
NewCancelSwapBatchCmd(),
NewLimitOrderBatchCmd(),
NewMarketOrderBatchCmd(),
NewCancelOrderBatchCmd(),
)

return cmd
Expand Down Expand Up @@ -196,23 +197,23 @@ $ %s tx %s withdraw 1 10000pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2D
return cmd
}

func NewSwapBatchCmd() *cobra.Command {
func NewLimitOrderBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "swap [pair-id] [direction] [offer-coin] [demand-coin-denom] [price] [base-coin-amount] [order-life-span]",
Use: "limit-order [pair-id] [direction] [offer-coin] [demand-coin-denom] [price] [base-coin-amount] [order-lifespan]",
Args: cobra.ExactArgs(7),
Short: "Swap coins within a pair",
Short: "Make a limit order",
Long: strings.TrimSpace(
fmt.Sprintf(`Swap coins within a pair.
fmt.Sprintf(`Make a limit order.
Example:
$ %s tx %s swap 1 SWAP_DIRECTION_BUY 10000usquad uatom 1.0 10000 10s --from mykey
$ %s tx %s limit-order 1 SWAP_DIRECTION_BUY 10000usquad uatom 1.0 10000 10s --from mykey
[pair-id]: pair id to swap with
[direction]: swap direction (one of: SWAP_DIRECTION_BUY,SWAP_DIRECTION_SELL)
[offer-coin]: the amount of offer coin to swap
[demand-coin-denom]: the denom to exchange with the offer coin
[price]: the limit order price for the swap; the exchange ratio is X/Y where X is the amount of quote coin and Y is the amount of base coin
[base-coin-amount]: the amount of base coin to buy or sell
[order-life-span]: the time duration that the swap order request lives until it is executed; valid time units are "ns", "us", "ms", "s", "m", and "h"
[order-lifespan]: the time duration that the swap order request lives until it is executed; valid time units are "ns", "us", "ms", "s", "m", and "h"
`,
version.AppName, types.ModuleName,
),
Expand Down Expand Up @@ -262,7 +263,7 @@ $ %s tx %s swap 1 SWAP_DIRECTION_BUY 10000usquad uatom 1.0 10000 10s --from myke
return fmt.Errorf("invalid order lifespan: %w", err)
}

msg := types.NewMsgSwapBatch(
msg := types.NewMsgLimitOrderBatch(
clientCtx.GetFromAddress(),
pairId,
dir,
Expand All @@ -282,15 +283,100 @@ $ %s tx %s swap 1 SWAP_DIRECTION_BUY 10000usquad uatom 1.0 10000 10s --from myke
return cmd
}

func NewCancelSwapBatchCmd() *cobra.Command {
func NewMarketOrderBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-swap [pair-id] [swap-request-id]",
Use: "marker-order [pair-id] [direction] [offer-coin] [demand-coin-denom] [base-coin-amount] [order-lifespan]",
Args: cobra.ExactArgs(7),
Short: "Make a market order",
Long: strings.TrimSpace(
fmt.Sprintf(`Make a market order.
Example:
$ %s tx %s market-order 1 SWAP_DIRECTION_BUY 10000usquad uatom 10000 10s --from mykey
[pair-id]: pair id to swap with
[direction]: swap direction (one of: SWAP_DIRECTION_BUY,SWAP_DIRECTION_SELL)
[offer-coin]: the amount of offer coin to swap
[demand-coin-denom]: the denom to exchange with the offer coin
[base-coin-amount]: the amount of base coin to buy or sell
[order-lifespan]: the time duration that the swap order request lives until it is executed; valid time units are "ns", "us", "ms", "s", "m", and "h"
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

pairId, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("parse pair id: %w", err)
}

rawDir, ok := types.SwapDirection_value[args[1]]
if !ok {
return fmt.Errorf("unknown swap direction: %s", args[1])
}
dir := types.SwapDirection(rawDir)
if dir != types.SwapDirectionBuy && dir != types.SwapDirectionSell {
return fmt.Errorf("invalid swap direction: %s", dir)
}

offerCoin, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return fmt.Errorf("invalid offer coin: %w", err)
}

demandCoinDenom := args[3]
if err := sdk.ValidateDenom(demandCoinDenom); err != nil {
return fmt.Errorf("invalid demand coin denom: %w", err)
}

price, err := sdk.NewDecFromStr(args[4])
if err != nil {
return fmt.Errorf("invalid price: %w", err)
}

amt, ok := sdk.NewIntFromString(args[5])
if !ok {
return fmt.Errorf("invalid amount: %s", args[5])
}

orderLifespan, err := time.ParseDuration(args[6])
if err != nil {
return fmt.Errorf("invalid order lifespan: %w", err)
}

msg := types.NewMsgLimitOrderBatch(
clientCtx.GetFromAddress(),
pairId,
dir,
offerCoin,
demandCoinDenom,
price,
amt,
orderLifespan,
)

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func NewCancelOrderBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-order [pair-id] [swap-request-id]",
Args: cobra.ExactArgs(2),
Short: "Cancel a swap request",
Short: "Cancel an order",
Long: strings.TrimSpace(
fmt.Sprintf(`Cancel a swap request.
fmt.Sprintf(`Cancel an order.
Example:
$ %s tx %s cancel-swap 1 --from mykey
$ %s tx %s cancel-order 1 --from mykey
`,
version.AppName, types.ModuleName,
),
Expand All @@ -311,7 +397,7 @@ $ %s tx %s cancel-swap 1 --from mykey
return err
}

msg := types.NewMsgCancelSwapBatch(
msg := types.NewMsgCancelOrderBatch(
clientCtx.GetFromAddress(),
pairId,
swapRequestId,
Expand Down
11 changes: 7 additions & 4 deletions x/liquidity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgWithdrawBatch:
res, err := msgServer.WithdrawBatch(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgSwapBatch:
res, err := msgServer.SwapBatch(sdk.WrapSDKContext(ctx), msg)
case *types.MsgLimitOrderBatch:
res, err := msgServer.LimitOrderBatch(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgCancelSwapBatch:
res, err := msgServer.CancelSwapBatch(sdk.WrapSDKContext(ctx), msg)
case *types.MsgMarketOrderBatch:
res, err := msgServer.MarketOrderBatch(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgCancelOrderBatch:
res, err := msgServer.CancelOrderBatch(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

default:
Expand Down
8 changes: 4 additions & 4 deletions x/liquidity/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

func (k Keeper) ExecuteRequests(ctx sdk.Context) {
k.IterateAllCancelSwapRequests(ctx, func(req types.CancelSwapRequest) (stop bool) {
k.IterateAllCancelOrderRequests(ctx, func(req types.CancelOrderRequest) (stop bool) {
if req.Status == types.RequestStatusNotExecuted {
if err := k.ExecuteCancelSwapRequest(ctx, req); err != nil {
if err := k.ExecuteCancelOrderRequest(ctx, req); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -66,9 +66,9 @@ func (k Keeper) DeleteOutdatedRequests(ctx sdk.Context) {
}
return false
})
k.IterateAllCancelSwapRequests(ctx, func(req types.CancelSwapRequest) (stop bool) {
k.IterateAllCancelOrderRequests(ctx, func(req types.CancelOrderRequest) (stop bool) {
if req.Status.ShouldBeDeleted() {
k.DeleteCancelSwapRequest(ctx, req.PairId, req.Id)
k.DeleteCancelOrderRequest(ctx, req.PairId, req.Id)
}
return false
})
Expand Down
Loading

0 comments on commit 9fcdd7e

Please sign in to comment.