-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtx.go
112 lines (92 loc) · 2.84 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
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(
NewConvertERC20Cmd(),
NewMsgRegisterERC20Cmd(),
)
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
}
func NewMsgRegisterERC20Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "register-erc20 [CONTRACT_ADDRESS...]",
Short: "Register a native ERC20 token",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
for _, contract := range args {
if err := cosmosevmtypes.ValidateAddress(contract); err != nil {
return fmt.Errorf("invalid ERC20 contract address %w", err)
}
}
msg := &types.MsgRegisterERC20{
Signer: cliCtx.GetFromAddress().String(),
Erc20Addresses: args,
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}