-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
109 lines (87 loc) · 2.7 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
package cli
import (
"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/spf13/cobra"
feeutils "github.com/terra-money/core/custom/auth/client/utils"
"github.com/terra-money/core/x/market/types"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
marketTxCmd := &cobra.Command{
Use: "market",
Short: "Market transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
marketTxCmd.AddCommand(
GetSwapCmd(),
)
return marketTxCmd
}
// GetSwapCmd will create and send a MsgSwap
func GetSwapCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "swap [offer-coin] [ask-denom] [to-address]",
Args: cobra.RangeArgs(2, 3),
Short: "Atomically swap currencies at their target exchange rate",
Long: strings.TrimSpace(`
Swap the offer-coin to the ask-denom currency at the oracle's effective exchange rate.
$ terrad market swap "1000ukrw" "uusd"
The to-address can be specified. A default to-address is trader.
$ terrad market swap "1000ukrw" "uusd" "terra1..."
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// Generate transaction factory for gas simulation
txf := tx.NewFactoryCLI(clientCtx, cmd.Flags())
offerCoinStr := args[0]
offerCoin, err := sdk.ParseCoinNormalized(offerCoinStr)
if err != nil {
return err
}
askDenom := args[1]
fromAddress := clientCtx.GetFromAddress()
var msg sdk.Msg
if len(args) == 3 {
toAddress, err := sdk.AccAddressFromBech32(args[2])
if err != nil {
return err
}
msg = types.NewMsgSwapSend(fromAddress, toAddress, offerCoin, askDenom)
if err = msg.ValidateBasic(); err != nil {
return err
}
if !clientCtx.GenerateOnly && txf.Fees().IsZero() {
// estimate tax and gas
stdFee, err := feeutils.ComputeFeesWithCmd(clientCtx, cmd.Flags(), msg)
if err != nil {
return err
}
// override gas and fees
txf = txf.
WithFees(stdFee.Amount.String()).
WithGas(stdFee.Gas).
WithSimulateAndExecute(false).
WithGasPrices("")
}
} else {
msg = types.NewMsgSwap(fromAddress, offerCoin, askDenom)
if err = msg.ValidateBasic(); err != nil {
return err
}
}
// build and sign the transaction, then broadcast to Tendermint
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}