-
Notifications
You must be signed in to change notification settings - Fork 40
/
tx_open.go
87 lines (72 loc) · 2.28 KB
/
tx_open.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
package cli
import (
"errors"
"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/perpetual/types"
"github.com/spf13/cobra"
)
func CmdOpen() *cobra.Command {
cmd := &cobra.Command{
Use: "open [position] [leverage] [trading-asset] [collateral] [flags]",
Short: "Open perpetual position",
Example: `Infinte profitability:
elysd tx perpetual open long 5 uatom 100000000uusdc --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000
Finite profitability:
elysd tx perpetual open short 5 uatom 100000000uusdc --take-profit 100 --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) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
signer := clientCtx.GetFromAddress()
if signer == nil {
return errors.New("signer address is missing")
}
argPosition := types.GetPositionFromString(args[0])
argLeverage, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err
}
argTradingAsset := args[2]
argCollateral, err := sdk.ParseCoinNormalized(args[3])
if err != nil {
return err
}
takeProfitPriceStr, err := cmd.Flags().GetString(FlagTakeProfitPrice)
if err != nil {
return err
}
var takeProfitPrice sdk.Dec
if takeProfitPriceStr != types.InfinitePriceString {
takeProfitPrice, err = sdk.NewDecFromStr(takeProfitPriceStr)
if err != nil {
return errors.New("invalid take profit price")
}
} else {
takeProfitPrice, err = sdk.NewDecFromStr(types.TakeProfitPriceDefault)
if err != nil {
return errors.New("failed to set default take profit price")
}
}
msg := types.NewMsgOpen(
signer.String(),
argPosition,
argLeverage,
argTradingAsset,
argCollateral,
takeProfitPrice,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagTakeProfitPrice, types.InfinitePriceString, "Optional take profit price")
flags.AddTxFlagsToCmd(cmd)
return cmd
}