forked from terra-money/classic-core
/
main.go
111 lines (91 loc) · 3.09 KB
/
main.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
package cli
import (
"strings"
"terra/x/market"
"terra/x/market/client/util"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
flagOfferCoin = "offerCoin"
flagOfferDenom = "offerDenom"
flagAskDenom = "askDenom"
flagTraderAddress = "traderAddress"
)
// GetSwapCmd will create and send a SwapMsg
func GetSwapCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swap [offerCoin] [askDenom]",
Short: "Atomically swap currencies at their target exchange rate",
Long: strings.TrimSpace(`
Swap the offerCoin to the askDenom currency at the oracle's effective exchange rate.
$ terracli market swap --offerCoin="1000krw" --askDenom="usd"
`),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)
txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
askDenom := viper.GetString(flagAskDenom)
offerCoin, err := sdk.ParseCoin(viper.GetString(flagOfferCoin))
if err != nil {
return err
}
fromAddress := cliCtx.GetFromAddress()
// build and sign the transaction, then broadcast to Tendermint
msg := market.NewSwapMsg(fromAddress, offerCoin, askDenom)
err = msg.ValidateBasic()
if err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
},
}
cmd.Flags().String(flagOfferCoin, "", "The asset to swap from e.g. 1000krw")
cmd.Flags().String(flagAskDenom, "", "Denom of the asset to swap to")
return cmd
}
// GetCmdQueryActive implements the query active command.
func GetCmdQueryHistory(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "history [traderAddress] [offerDenom] [askDenom]",
Short: "Query history of atomic swaps filtered by three optional variables.",
Long: strings.TrimSpace(`
Query history of atomic swaps filtered by three optional variables.
$ terracli query market history --offerDenom="usd" --askDenom="krw"
Return item count paginated by units of 30 values.
`),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
var traderAddress sdk.AccAddress
offerDenom := viper.GetString(flagOfferDenom)
askDenom := viper.GetString(flagAskDenom)
params := util.QueryHistoryParams{
TraderAddress: traderAddress,
AskDenom: askDenom,
OfferDenom: offerDenom,
}
traderAddrStr := viper.GetString(flagTraderAddress)
account, err := cliCtx.GetAccount([]byte(traderAddrStr))
if err == nil {
params.TraderAddress = account.GetAddress()
}
res, err := util.QueryHistoryByTxQuery(cdc, cliCtx, params)
if err != nil {
return err
}
var swaps market.SwapHistory
err = cdc.UnmarshalJSON(res, &swaps)
if err != nil {
return err
}
return cliCtx.PrintOutput(swaps)
},
}
return cmd
}