-
Notifications
You must be signed in to change notification settings - Fork 44
/
tx_swap_exact_amount_out.go
84 lines (74 loc) · 2.45 KB
/
tx_swap_exact_amount_out.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
package cli
import (
"errors"
"strings"
"cosmossdk.io/math"
"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/elys-network/elys/x/amm/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)
func CmdSwapExactAmountOut() *cobra.Command {
cmd := &cobra.Command{
Use: "swap-exact-amount-out [token-out] [token-out-max-amount] [swap-route-pool-ids] [swap-route-denoms]",
Short: "Swap a maximum amount of tokens for an exact amount of another token, similar to swapping a token on the trade screen GUI.",
Example: `elysd tx amm swap-exact-amount-out 100000uatom 200000 0 uusdc --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argTokenOut, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
argTokenOutMaxAmount, ok := math.NewIntFromString(args[1])
if !ok {
return errors.New("invalid token-out-max-amount")
}
argCastSwapRoutePoolIds := strings.Split(args[2], listSeparator)
argSwapRoutePoolIds := make([]uint64, len(argCastSwapRoutePoolIds))
for i, arg := range argCastSwapRoutePoolIds {
value, err := cast.ToUint64E(arg)
if err != nil {
return err
}
argSwapRoutePoolIds[i] = value
}
argSwapRouteDenoms := strings.Split(args[3], listSeparator)
discountStr, err := cmd.Flags().GetString(FlagDiscount)
if err != nil {
return err
}
discount, err := sdk.NewDecFromStr(discountStr)
if err != nil {
return err
}
recipient, err := cmd.Flags().GetString(FlagRecipient)
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgSwapExactAmountOut(
clientCtx.GetFromAddress().String(),
recipient,
argTokenOut,
argTokenOutMaxAmount,
argSwapRoutePoolIds,
argSwapRouteDenoms,
discount,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee (only smart contract broker can apply the discount)")
cmd.Flags().String(FlagRecipient, "", "optional recipient field for the tokens swapped to be sent to")
return cmd
}