-
Notifications
You must be signed in to change notification settings - Fork 14
/
tx.go
336 lines (271 loc) · 10.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package cli
// DONTCOVER
// client is excluded from test coverage in the poc phase milestone 1 and will be included in milestone 2 with completeness
import (
"fmt"
"strconv"
"strings"
"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/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
"github.com/gravity-devs/liquidity/x/liquidity/types"
)
// GetTxCmd returns a root CLI command handler for all x/liquidity transaction commands.
func GetTxCmd() *cobra.Command {
liquidityTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Liquidity transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
liquidityTxCmd.AddCommand(
NewCreatePoolCmd(),
NewDepositWithinBatchCmd(),
NewWithdrawWithinBatchCmd(),
NewSwapWithinBatchCmd(),
)
return liquidityTxCmd
}
// Create new liquidity pool with the specified pool type and deposit coins.
func NewCreatePoolCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-pool [pool-type] [deposit-coins]",
Args: cobra.ExactArgs(2),
Short: "Create liquidity pool and deposit coins",
Long: strings.TrimSpace(
fmt.Sprintf(`Create liquidity pool and deposit coins.
Example:
$ %s tx %s create-pool 1 1000000000uatom,50000000000uusd --from mykey
This example creates a liquidity pool of pool-type 1 (two coins) and deposits 1000000000uatom and 50000000000uusd.
New liquidity pools can be created only for coin combinations that do not already exist in the network.
[pool-type]: The id of the liquidity pool-type. The only supported pool type is 1
[deposit-coins]: The amount of coins to deposit to the liquidity pool. The number of deposit coins must be 2 in pool type 1.
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
poolCreator := clientCtx.GetFromAddress()
// Get pool type index
poolTypeID, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
return fmt.Errorf("pool-type %s not a valid uint, input a valid unsigned 32-bit integer for pool-type", args[0])
}
// Get deposit coins
depositCoins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
err = depositCoins.Validate()
if err != nil {
return err
}
if poolTypeID != 1 {
return types.ErrPoolTypeNotExists
}
if depositCoins.Len() != 2 {
return fmt.Errorf("the number of deposit coins must be two in pool-type 1")
}
msg := types.NewMsgCreatePool(poolCreator, uint32(poolTypeID), depositCoins)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// Deposit coins to the specified liquidity pool.
func NewDepositWithinBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit [pool-id] [deposit-coins]",
Args: cobra.ExactArgs(2),
Short: "Deposit coins to a liquidity pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Deposit coins a liquidity pool.
This deposit request is not processed immediately since it is accumulated in the liquidity pool batch.
All requests in a batch are treated equally and executed at the same swap price.
Example:
$ %s tx %s deposit 1 100000000uatom,5000000000uusd --from mykey
This example request deposits 100000000uatom and 5000000000uusd to pool-id 1.
Deposits must be the same coin denoms as the reserve coins.
[pool-id]: The pool id of the liquidity pool
[deposit-coins]: The amount of coins to deposit to the liquidity pool
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
depositor := clientCtx.GetFromAddress()
// Get pool type index
poolID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0])
}
// Get deposit coins
depositCoins, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}
err = depositCoins.Validate()
if err != nil {
return err
}
if depositCoins.Len() != 2 {
return fmt.Errorf("the number of deposit coins must be two in the pool-type 1")
}
msg := types.NewMsgDepositWithinBatch(depositor, poolID, depositCoins)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// Withdraw pool coin from the specified liquidity pool.
func NewWithdrawWithinBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw [pool-id] [pool-coin]",
Args: cobra.ExactArgs(2),
Short: "Withdraw pool coin from the specified liquidity pool",
Long: strings.TrimSpace(
fmt.Sprintf(`Withdraw pool coin from the specified liquidity pool.
This swap request is not processed immediately since it is accumulated in the liquidity pool batch.
All requests in a batch are treated equally and executed at the same swap price.
Example:
$ %s tx %s withdraw 1 10000pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295 --from mykey
This example request withdraws 10000 pool coin from the specified liquidity pool.
The appropriate pool coin must be requested from the specified pool.
[pool-id]: The pool id of the liquidity pool
[pool-coin]: The amount of pool coin to withdraw from the liquidity pool
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
withdrawer := clientCtx.GetFromAddress()
// Get pool type index
poolID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0])
}
// Get pool coin of the target pool
poolCoin, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
err = poolCoin.Validate()
if err != nil {
return err
}
msg := types.NewMsgWithdrawWithinBatch(withdrawer, poolID, poolCoin)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// Swap offer coin with demand coin from the specified liquidity pool with the given order price.
func NewSwapWithinBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "swap [pool-id] [swap-type] [offer-coin] [demand-coin-denom] [order-price] [swap-fee-rate]",
Args: cobra.ExactArgs(6),
Short: "Swap offer coin with demand coin from the liquidity pool with the given order price",
Long: strings.TrimSpace(
fmt.Sprintf(`Swap offer coin with demand coin from the liquidity pool with the given order price.
This swap request is not processed immediately since it is accumulated in the liquidity pool batch.
All requests in a batch are treated equally and executed at the same swap price.
The order of swap requests is ignored since the universal swap price is calculated in every batch to prevent front running.
The requested swap is executed with a swap price that is calculated from the given swap price function of the pool, the other swap requests, and the liquidity pool coin reserve status.
Swap orders are executed only when the execution swap price is equal to or greater than the submitted order price of the swap order.
Example:
$ %s tx %s swap 1 1 50000000uusd uatom 0.019 0.003 --from mykey
For this example, imagine that an existing liquidity pool has with 1000000000uatom and 50000000000uusd.
This example request swaps 50000000uusd for at least 950000uatom with the order price of 0.019 and swap fee rate of 0.003.
A sufficient balance of half of the swap-fee-rate of the offer coin is required to reserve the offer coin fee.
The order price is the exchange ratio of X/Y, where X is the amount of the first coin and Y is the amount of the second coin when their denoms are sorted alphabetically.
Increasing order price reduces the possibility for your request to be processed and results in buying uatom at a lower price than the pool price.
For explicit calculations, The swap fee rate must be the value that set as liquidity parameter in the current network.
The only supported swap-type is 1. For the detailed swap algorithm, see https://github.com/gravity-devs/liquidity
[pool-id]: The pool id of the liquidity pool
[swap-type]: The swap type of the swap message. The only supported swap type is 1 (instant swap).
[offer-coin]: The amount of offer coin to swap
[demand-coin-denom]: The denomination of the coin to exchange with offer coin
[order-price]: The limit order price for the swap order. The price is the exchange ratio of X/Y where X is the amount of the first coin and Y is the amount of the second coin when their denoms are sorted alphabetically
[swap-fee-rate]: The swap fee rate to pay for swap that is proportional to swap amount. The swap fee rate must be the value that set as liquidity parameter in the current network.
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
swapRequester := clientCtx.GetFromAddress()
// Get pool id
poolID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0])
}
// Get swap type
swapTypeID, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return fmt.Errorf("swap-type %s not a valid uint, input a valid unsigned 32-bit integer for swap-type", args[2])
}
if swapTypeID != 1 {
return types.ErrSwapTypeNotExists
}
// Get offer coin
offerCoin, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return err
}
err = offerCoin.Validate()
if err != nil {
return err
}
err = sdk.ValidateDenom(args[3])
if err != nil {
return err
}
orderPrice, err := sdk.NewDecFromStr(args[4])
if err != nil {
return err
}
swapFeeRate, err := sdk.NewDecFromStr(args[5])
if err != nil {
return err
}
msg := types.NewMsgSwapWithinBatch(swapRequester, poolID, uint32(swapTypeID), offerCoin, args[3], orderPrice, swapFeeRate)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}