-
Notifications
You must be signed in to change notification settings - Fork 180
/
tx.go
259 lines (227 loc) · 8.46 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package cli
import (
"bufio"
"fmt"
"strings"
"time"
"github.com/okex/exchain/libs/cosmos-sdk/client/context"
client "github.com/okex/exchain/libs/cosmos-sdk/client/flags"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/x/auth"
"github.com/okex/exchain/libs/cosmos-sdk/x/auth/client/utils"
"github.com/okex/exchain/x/ammswap/types"
"github.com/spf13/cobra"
)
// flags
const (
flagMinLiquidity = "min-liquidity"
flagMaxBaseAmount = "max-base-amount"
flagQuoteAmount = "quote-amount"
flagDeadlineDuration = "deadline-duration"
flagLiquidity = "liquidity"
flagMinBaseAmount = "min-base-amount"
flagMinQuoteAmount = "min-quote-amount"
flagSellAmount = "sell-amount"
flagMinBuyAmount = "min-buy-amount"
flagRecipient = "recipient"
flagToken0 = "token0"
flagToken1 = "token1"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "swap",
Short: "Swap transactions subcommands",
}
txCmd.AddCommand(client.PostCommands(
getCmdAddLiquidity(cdc),
getCmdRemoveLiquidity(cdc),
getCmdCreateExchange(cdc),
getCmdTokenSwap(cdc),
)...)
return txCmd
}
func getCmdAddLiquidity(cdc *codec.Codec) *cobra.Command {
// flags
var minLiquidity string
var maxBaseAmount string
var quoteAmount string
var deadlineDuration string
cmd := &cobra.Command{
Use: "add-liquidity",
Short: "add liquidity",
Long: strings.TrimSpace(
fmt.Sprintf(`add liquidity.
Example:
$ exchaincli tx swap add-liquidity --max-base-amount 10eth-355 --quote-amount 100btc-366 --min-liquidity 0.001
`),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
minLiquidityDec, sdkErr := sdk.NewDecFromStr(minLiquidity)
if sdkErr != nil {
return sdkErr
}
maxBaseAmountDecCoin, err := sdk.ParseDecCoin(maxBaseAmount)
if err != nil {
return err
}
quoteAmountDecCoin, err := sdk.ParseDecCoin(quoteAmount)
if err != nil {
return err
}
duration, err := time.ParseDuration(deadlineDuration)
if err != nil {
return err
}
deadline := time.Now().Add(duration).Unix()
msg := types.NewMsgAddLiquidity(minLiquidityDec, maxBaseAmountDecCoin, quoteAmountDecCoin, deadline, cliCtx.FromAddress)
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().StringVarP(&minLiquidity, flagMinLiquidity, "l", "", "Minimum number of sender will mint if total pool token supply is greater than 0")
cmd.Flags().StringVarP(&maxBaseAmount, flagMaxBaseAmount, "", "", "Maximum number of base amount deposited. Deposits max amount if total pool token supply is 0. For example \"100xxb\"")
cmd.Flags().StringVarP("eAmount, flagQuoteAmount, "q", "", "The number of quote amount. For example \"100okb\"")
cmd.Flags().StringVarP(&deadlineDuration, flagDeadlineDuration, "d", "30s", "Duration after which this transaction can no longer be executed. such as \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
cmd.MarkFlagRequired(flagMinLiquidity)
cmd.MarkFlagRequired(flagMaxBaseAmount)
cmd.MarkFlagRequired(flagQuoteAmount)
return cmd
}
func getCmdRemoveLiquidity(cdc *codec.Codec) *cobra.Command {
// flags
var liquidity string
var minBaseAmount string
var minQuoteAmount string
var deadlineDuration string
cmd := &cobra.Command{
Use: "remove-liquidity",
Short: "remove liquidity",
Long: strings.TrimSpace(
fmt.Sprintf(`remove liquidity.
Example:
$ exchaincli tx swap remove-liquidity --liquidity 1 --min-base-amount 10eth-355 --min-quote-amount 1btc-366
`),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
liquidityDec, sdkErr := sdk.NewDecFromStr(liquidity)
if sdkErr != nil {
return sdkErr
}
minBaseAmountDecCoin, err := sdk.ParseDecCoin(minBaseAmount)
if err != nil {
return err
}
minQuoteAmountDecCoin, err := sdk.ParseDecCoin(minQuoteAmount)
if err != nil {
return err
}
duration, err := time.ParseDuration(deadlineDuration)
if err != nil {
return err
}
deadline := time.Now().Add(duration).Unix()
msg := types.NewMsgRemoveLiquidity(liquidityDec, minBaseAmountDecCoin, minQuoteAmountDecCoin, deadline, cliCtx.FromAddress)
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().StringVarP(&liquidity, flagLiquidity, "l", "", "Liquidity amount of sender will burn")
cmd.Flags().StringVarP(&minBaseAmount, flagMinBaseAmount, "", "", "Minimum number of base amount withdrawn")
cmd.Flags().StringVarP(&minQuoteAmount, flagMinQuoteAmount, "q", "", "Minimum number of quote amount withdrawn")
cmd.Flags().StringVarP(&deadlineDuration, flagDeadlineDuration, "d", "30s", "Duration after which this transaction can no longer be executed. such as \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
cmd.MarkFlagRequired(flagLiquidity)
cmd.MarkFlagRequired(flagMinBaseAmount)
cmd.MarkFlagRequired(flagMinQuoteAmount)
return cmd
}
func getCmdCreateExchange(cdc *codec.Codec) *cobra.Command {
// flags
var token0 string
var token1 string
cmd := &cobra.Command{
Use: "create-pair",
Short: "create token pair",
Long: strings.TrimSpace(
fmt.Sprintf(`create token pair.
Example:
$ exchaincli tx swap create-pair --token0 eth-355 --token1 btc-366 --fees 0.01okt
`),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
msg := types.NewMsgCreateExchange(token0, token1, cliCtx.FromAddress)
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().StringVar(&token0, flagToken0, "", "the base token name is required to create an AMM swap pair")
cmd.Flags().StringVar(&token1, flagToken1, "", "the quote token name is required to create an AMM swap pair")
cmd.MarkFlagRequired(flagToken0)
cmd.MarkFlagRequired(flagToken1)
return cmd
}
func getCmdTokenSwap(cdc *codec.Codec) *cobra.Command {
// flags
var soldTokenAmount string
var minBoughtTokenAmount string
var deadline string
var recipient string
cmd := &cobra.Command{
Use: "token",
Short: "swap token",
Long: strings.TrimSpace(
fmt.Sprintf(`swap token.
Example:
$ exchaincli tx swap token --sell-amount 1eth-355 --min-buy-amount 60btc-366
`),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
soldTokenAmount, err := sdk.ParseDecCoin(soldTokenAmount)
if err != nil {
return err
}
minBoughtTokenAmount, err := sdk.ParseDecCoin(minBoughtTokenAmount)
if err != nil {
return err
}
dur, err := time.ParseDuration(deadline)
if err != nil {
return err
}
deadline := time.Now().Add(dur).Unix()
var recip sdk.AccAddress
if recipient == "" {
recip = cliCtx.FromAddress
} else {
recip, err = sdk.AccAddressFromBech32(recipient)
if err != nil {
return err
}
}
msg := types.NewMsgTokenToToken(soldTokenAmount, minBoughtTokenAmount,
deadline, recip, cliCtx.FromAddress)
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})
},
}
cmd.Flags().StringVarP(&soldTokenAmount, flagSellAmount, "", "",
"Amount expected to sell")
cmd.Flags().StringVarP(&minBoughtTokenAmount, flagMinBuyAmount, "", "",
"Minimum amount expected to buy")
cmd.Flags().StringVarP(&recipient, flagRecipient, "", "",
"The address to receive the amount bought")
cmd.Flags().StringVarP(&deadline, flagDeadlineDuration, "", "100s",
"Duration after which this transaction can no longer be executed. such as \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")
cmd.MarkFlagRequired(flagSellAmount)
cmd.MarkFlagRequired(flagMinBuyAmount)
return cmd
}