-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtx.go
129 lines (106 loc) · 3.27 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
package cli
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
cosmosevmtypes "github.com/cosmos/evm/types"
"github.com/cosmos/evm/x/erc20/types"
"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"
)
// NewTxCmd returns a root CLI command handler for erc20 transaction commands
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "erc20 subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
NewConvertCoinCmd(),
NewConvertERC20Cmd(),
)
return txCmd
}
// NewConvertERC20Cmd returns a CLI command handler for converting an ERC20
func NewConvertERC20Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert-erc20 CONTRACT_ADDRESS AMOUNT [RECEIVER]",
Short: "Convert an ERC20 token to Cosmos coin. When the receiver [optional] is omitted, the Cosmos coins are transferred to the sender.",
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
contract := args[0]
if err := cosmosevmtypes.ValidateAddress(contract); err != nil {
return fmt.Errorf("invalid ERC20 contract address %w", err)
}
amount, ok := math.NewIntFromString(args[1])
if !ok {
return fmt.Errorf("invalid amount %s", args[1])
}
from := common.BytesToAddress(cliCtx.GetFromAddress().Bytes())
receiver := cliCtx.GetFromAddress()
if len(args) == 3 {
receiver, err = sdk.AccAddressFromBech32(args[2])
if err != nil {
return err
}
}
msg := &types.MsgConvertERC20{
ContractAddress: contract,
Amount: amount,
Receiver: receiver.String(),
Sender: from.Hex(),
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewConvertCoinCmd returns a CLI command handler for converting a Cosmos coin
func NewConvertCoinCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert-coin COIN [RECEIVER_HEX]",
Short: "Convert a Cosmos coin to ERC20. When the receiver [optional] is omitted, the ERC20 tokens are transferred to the sender.",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
coin, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}
var receiver string
sender := cliCtx.GetFromAddress()
if len(args) == 2 {
receiver = args[1]
if err := cosmosevmtypes.ValidateAddress(receiver); err != nil {
return fmt.Errorf("invalid receiver hex address %w", err)
}
} else {
receiver = common.BytesToAddress(sender).Hex()
}
msg := &types.MsgConvertCoin{
Coin: coin,
Receiver: receiver,
Sender: sender.String(),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}