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

Add cli commands for the coinswap module. #4719

Merged
merged 19 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a42b045
Added query.go that includes the cli commands for querying.
aayushijain23 Jul 12, 2019
6af249b
Added transaction subcommands such as add and remove liquidity.
aayushijain23 Jul 18, 2019
7aa6998
Added tx command for swap order message type.
aayushijain23 Jul 22, 2019
f9b248e
Added REST API commands for transaction sub commands specific to the …
aayushijain23 Jul 22, 2019
39306cb
Added REST API commands for the query sub commands. Also added a stru…
aayushijain23 Jul 23, 2019
7445656
Removed some lines of code that were commented.
aayushijain23 Jul 30, 2019
920166e
Update x/coinswap/client/cli/tx.go
aayushijain23 Jul 31, 2019
405f7d8
Update x/coinswap/client/cli/tx.go
aayushijain23 Jul 31, 2019
31038a7
Update x/coinswap/internal/types/querier.go
aayushijain23 Aug 1, 2019
0c9fef3
Incorporating Colin's and Fede's review comments.
aayushijain23 Aug 1, 2019
438d219
Update x/coinswap/client/rest/query.go
aayushijain23 Aug 2, 2019
f9f3b0b
Update x/coinswap/client/rest/query.go
aayushijain23 Aug 2, 2019
03d4687
Incorporating Fede and Colin's review comments.
aayushijain23 Aug 2, 2019
de2a089
Address Colin's comments.
aayushijain23 Aug 8, 2019
81856e8
Addressing comments on handling time.parse errors.
aayushijain23 Aug 8, 2019
5f2af25
Addressed review comments about flags and splitting swap order into b…
aayushijain23 Aug 8, 2019
f8532d0
Addressed Colin's comments.
aayushijain23 Aug 9, 2019
8f1ccfb
Merge branch 'colin/4443-coinswap' of github.com:cosmos/cosmos-sdk in…
aayushijain23 Aug 9, 2019
fb30336
Addressing comment of removing req.Sender.
aayushijain23 Aug 9, 2019
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
13 changes: 11 additions & 2 deletions x/coinswap/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/coinswap/internal/types"
)

const (
nativeDenom = "atom"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be configurable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in the core code; its a parameter. The issue on the client is its useful to know what the native denom is but it is inefficient to do an extra query for it for every command especially since it doesn't change after genesis. One way of handling this is through a config file, which could be done at a later point

)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
coinswapQueryCmd := &cobra.Command{
Expand All @@ -33,13 +37,13 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// GetCmdQueryLiquidity implements the liquidity query command
func GetCmdQueryLiquidity(queryRoute string, cdc *codec.Codec) *cobra.Command {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
return &cobra.Command{
Use: "liquidity",
Use: "liquidity [denom]",
Short: "Query the current liquidity values",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the liquidity of a specific trading pair stored in the reserve pool.

Example:
$ %s query coinswap liquidity
$ %s query coinswap liquidity btc
`,
version.ClientName,
),
Expand All @@ -48,6 +52,11 @@ $ %s query coinswap liquidity
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

// Added a check to ensure that input provided is not a native denom
if strings.Compare(strings.TrimSpace(args[0]), nativeDenom) == 0 {
return fmt.Errorf("%s is not a valid denom, please input a valid denom", args[0])
}

bz, err := cdc.MarshalJSON(types.NewQueryLiquidityParams(args[0]))
if err != nil {
return err
Expand Down
180 changes: 138 additions & 42 deletions x/coinswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cli

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
Expand All @@ -18,6 +18,14 @@ import (
"github.com/cosmos/cosmos-sdk/x/coinswap/internal/types"
)

// Liquidity flags
const (
MinReward = "min-reward"
MinNative = "min-native"
Deadline = "deadline"
Recipient = "recipient"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
coinswapTxCmd := &cobra.Command{
Expand All @@ -30,22 +38,24 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {

coinswapTxCmd.AddCommand(client.PostCommands(
GetCmdAddLiquidity(cdc),
GetCmdRemoveLiquidity(cdc))...)
GetCmdRemoveLiquidity(cdc),
GetCmdBuyOrder(cdc),
GetCmdSellOrder(cdc))...)

return coinswapTxCmd
}

// GetCmdAddLiquidity implements the add liquidity command handler
func GetCmdAddLiquidity(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "add-liquidity [deposit] [deposit-amount] [min-reward] [deadline]",
Args: cobra.ExactArgs(4),
cmd := &cobra.Command{
Use: "add-liquidity [deposit-coin] [deposit]",
Args: cobra.ExactArgs(2),
Short: "Add liquidity to the reserve pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Add liquidity to the reserve pool for a trading pair.

Example:
$ %s tx coinswap add-liquidity dai 1000atom 1000 2 cosmosvaloper1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm --from mykey
$ %s tx coinswap add-liquidity dai 1000atom --min-reward 100atom --deadline 2h --from mykey
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
`,
version.ClientName,
),
Expand All @@ -54,45 +64,56 @@ $ %s tx coinswap add-liquidity dai 1000atom 1000 2 cosmosvaloper1l2rsakp388kuv9k
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(auth.DefaultTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)

deposit, err := sdk.ParseCoin(args[0])
depositCoin, err := sdk.ParseCoin(args[0])
if err != nil {
return err
}

depositAmount, ok := sdk.NewIntFromString(args[1])
if !ok {
return fmt.Errorf(types.ErrNotPositive(types.DefaultCodespace, "deposit amount provided is not positive").Error())
deposit, err := sdk.ParseCoin(args[1])
if err != nil {
return err
}

minReward, ok := sdk.NewIntFromString(args[2])
if !ok {
return fmt.Errorf(types.ErrNotPositive(types.DefaultCodespace, "minimum liquidity is not positive").Error())
minRewardArg := viper.GetString(MinReward)
minReward, err := sdk.ParseCoin(minRewardArg)
if err != nil {
return err
}

deadline, err := time.Parse(time.RFC3339, args[3])
durationArg := viper.GetString(Deadline)
duration, err := time.ParseDuration(durationArg)
if err != nil {
return err
return fmt.Errorf("failed to parse the duration : %s", err)
}
deadline := time.Now().Add(duration).UTC()

senderAddr := cliCtx.GetFromAddress()

msg := types.NewMsgAddLiquidity(deposit, depositAmount, minReward, deadline, senderAddr)
msg := types.NewMsgAddLiquidity(depositCoin, deposit.Amount, minReward.Amount, deadline, senderAddr)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().String(MinReward, "", "minimum amount of vouchers the sender is willing to accept for deposited coins (required)")
cmd.Flags().String(Deadline, "1h", "duration for which the transaction is valid (required)")

cmd.MarkFlagRequired(MinReward)
cmd.MarkFlagRequired(Deadline)

return cmd
}

// GetCmdRemoveLiquidity implements the remove liquidity command handler
func GetCmdRemoveLiquidity(cdc *codec.Codec) *cobra.Command {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
return &cobra.Command{
Use: "remove-liquidity [withdraw] [withdraw-amount] [min-native] [deadline]",
Args: cobra.ExactArgs(4),
cmd := &cobra.Command{
Use: "remove-liquidity [withdraw-coin] [pool-tokens]",
Args: cobra.ExactArgs(2),
Short: "Remove liquidity from the reserve pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Remove liquidity from the reserve pool for a trading pair.

Example:
$ %s tx coinswap remove-liquidity dai 1000atom 1000 2 cosmosvaloper1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm --from mykey
$ %s tx coinswap remove-liquidity dai 1000atom --min-native 100atom --deadline 2h --from mykey
`,
version.ClientName,
),
Expand All @@ -101,45 +122,56 @@ $ %s tx coinswap remove-liquidity dai 1000atom 1000 2 cosmosvaloper1l2rsakp388ku
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(auth.DefaultTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)

withdraw, err := sdk.ParseCoin(args[0])
withdrawCoin, err := sdk.ParseCoin(args[0])
if err != nil {
return err
}

withdrawAmount, ok := sdk.NewIntFromString(args[1])
poolTokens, ok := sdk.NewIntFromString(args[1])
if !ok {
return fmt.Errorf(types.ErrNotPositive(types.DefaultCodespace, "withdraw amount provided is not positive").Error())
return fmt.Errorf("pool-tokens %s is not a valid int, please input valid pool-tokens", args[1])
}

minNative, ok := sdk.NewIntFromString(args[2])
if !ok {
return fmt.Errorf(types.ErrNotPositive(types.DefaultCodespace, "minimum native amount is not positive").Error())
minNativeArg := viper.GetString(MinNative)
minNative, err := sdk.ParseCoin(minNativeArg)
if err != nil {
return err
}

deadline, err := time.Parse(time.RFC3339, args[3])
durationArg := viper.GetString(Deadline)
duration, err := time.ParseDuration(durationArg)
if err != nil {
return err
return fmt.Errorf("failed to parse the duration : %s", err)
}
deadline := time.Now().Add(duration).UTC()

senderAddr := cliCtx.GetFromAddress()

msg := types.NewMsgRemoveLiquidity(withdraw, withdrawAmount, minNative, deadline, senderAddr)
msg := types.NewMsgRemoveLiquidity(withdrawCoin, poolTokens, minNative.Amount, deadline, senderAddr)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().String(MinNative, "", "minimum amount of the native asset the sender is willing to accept (required)")
cmd.Flags().String(Deadline, "1h", "duration for which the transaction is valid (required)")

cmd.MarkFlagRequired(MinNative)
cmd.MarkFlagRequired(Deadline)

return cmd
}

// GetCmdSwapOrder implements the swap order command handler
func GetCmdSwapOrder(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "swap-order [input] [output] [deadline] [recipient] [is-buy-order]",
Args: cobra.ExactArgs(5),
Short: "Swap order",
// GetCmdBuyOrder implements the buy order command handler
func GetCmdBuyOrder(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "buy-order [input] [output]",
Args: cobra.ExactArgs(2),
Short: "Buy order",
Long: strings.TrimSpace(
fmt.Sprintf(`Swap order for a trading pair.
fmt.Sprintf(`Buy order for a trading pair.

Example:
$ %s tx coinswap swap-order 5atom 2eth cosmosvaloper1l2dsbkp388kuv9k8qzq6lrm9tbccae7fpx59wm false --from mykey
$ %s tx coinswap buy-order 5atom 2eth --deadline 2h --recipient recipientAddr --from mykey
`,
version.ClientName,
),
Expand All @@ -158,25 +190,89 @@ $ %s tx coinswap swap-order 5atom 2eth cosmosvaloper1l2dsbkp388kuv9k8qzq6lrm9tbc
return err
}

deadline, err := time.Parse(time.RFC3339, args[2])
durationArg := viper.GetString(Deadline)
duration, err := time.ParseDuration(durationArg)
if err != nil {
return err
return fmt.Errorf("failed to parse the duration : %s", err)
}
deadline := time.Now().Add(duration).UTC()

senderAddr := cliCtx.GetFromAddress()

recipientAddr, err := sdk.AccAddressFromBech32(args[3])
recipientAddrArg := viper.GetString(Recipient)
recipientAddr, err := sdk.AccAddressFromBech32(recipientAddrArg)
if err != nil {
return err
}

isBuyOder, err := strconv.ParseBool(args[4])
msg := types.NewMsgSwapOrder(input, output, deadline, senderAddr, recipientAddr, true)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().String(Recipient, "", "recipient's address (required)")
cmd.Flags().String(Deadline, "1h", "duration for which the transaction is valid (required)")

cmd.MarkFlagRequired(Recipient)
cmd.MarkFlagRequired(Deadline)

return cmd
}

// GetCmdSellOrder implements the sell order command handler
func GetCmdSellOrder(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "sell-order [input] [output]",
Args: cobra.ExactArgs(2),
Short: "Sell order",
Long: strings.TrimSpace(
fmt.Sprintf(`Sell order for a trading pair.

Example:
$ %s tx coinswap sell-order 2eth 5atom --deadline 2h --recipient recipientAddr --from mykey
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(auth.DefaultTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)

input, err := sdk.ParseCoin(args[0])
if err != nil {
return err
}

output, err := sdk.ParseCoin(args[1])
if err != nil {
return err
}

durationArg := viper.GetString(Deadline)
duration, err := time.ParseDuration(durationArg)
if err != nil {
return fmt.Errorf("failed to parse the duration : %s", err)
}
deadline := time.Now().Add(duration).UTC()

senderAddr := cliCtx.GetFromAddress()

recipientAddrArg := viper.GetString(Recipient)
recipientAddr, err := sdk.AccAddressFromBech32(recipientAddrArg)
if err != nil {
return err
}

msg := types.NewMsgSwapOrder(input, output, deadline, senderAddr, recipientAddr, isBuyOder)
msg := types.NewMsgSwapOrder(input, output, deadline, senderAddr, recipientAddr, false)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().String(Recipient, "", "recipient's address (required)")
cmd.Flags().String(Deadline, "1h", "duration for which the transaction is valid (required)")

cmd.MarkFlagRequired(Recipient)
cmd.MarkFlagRequired(Deadline)

return cmd
}