2019-09-15 16:12:59 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"bufio"
|
2019-11-15 17:02:13 +00:00
|
|
|
"fmt"
|
2019-09-15 16:12:59 +00:00
|
|
|
"strconv"
|
2019-11-15 17:02:13 +00:00
|
|
|
"strings"
|
2019-09-15 16:12:59 +00:00
|
|
|
|
2020-07-02 15:19:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-09-15 16:12:59 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-11-13 17:00:21 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-04-07 20:00:06 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-09-15 16:12:59 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-04-22 19:26:01 +00:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2019-11-15 17:02:13 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-04-22 19:26:01 +00:00
|
|
|
|
|
|
|
ethermintcodec "github.com/cosmos/ethermint/codec"
|
2019-11-15 17:02:13 +00:00
|
|
|
emint "github.com/cosmos/ethermint/types"
|
2019-09-15 16:12:59 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTxCmd defines the CLI commands regarding evm module transactions
|
|
|
|
func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
evmTxCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "EVM transaction subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:00:06 +00:00
|
|
|
evmTxCmd.AddCommand(flags.PostCommands(
|
2020-04-22 19:26:01 +00:00
|
|
|
GetCmdSendTx(cdc),
|
2019-11-15 17:02:13 +00:00
|
|
|
GetCmdGenCreateTx(cdc),
|
2019-09-15 16:12:59 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return evmTxCmd
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// GetCmdSendTx generates an Ethermint transaction (excludes create operations)
|
|
|
|
func GetCmdSendTx(cdc *codec.Codec) *cobra.Command {
|
2019-09-15 16:12:59 +00:00
|
|
|
return &cobra.Command{
|
2019-11-15 17:02:13 +00:00
|
|
|
Use: "send [to_address] [amount (in photons)] [<data>]",
|
|
|
|
Short: "send transaction to address (call operations included)",
|
|
|
|
Args: cobra.RangeArgs(2, 3),
|
2019-09-15 16:12:59 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-11-13 17:00:21 +00:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2019-12-13 19:50:19 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2019-09-15 16:12:59 +00:00
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
|
2019-09-15 16:12:59 +00:00
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
toAddr, err := cosmosAddressFromArg(args[0])
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return errors.Wrap(err, "must provide a valid Bech32 address for to_address")
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Ambiguously decode amount from any base
|
|
|
|
amount, err := strconv.ParseInt(args[1], 0, 64)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
var data []byte
|
|
|
|
if len(args) > 2 {
|
|
|
|
payload := args[2]
|
|
|
|
if !strings.HasPrefix(payload, "0x") {
|
|
|
|
payload = "0x" + payload
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err = hexutil.Decode(payload)
|
|
|
|
if err != nil {
|
2020-04-07 20:00:06 +00:00
|
|
|
return err
|
2019-11-15 17:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
from := cliCtx.GetFromAddress()
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
authclient.Codec = ethermintcodec.NewAppCodec(cdc)
|
|
|
|
_, seq, err := authtypes.NewAccountRetriever(authclient.Codec, cliCtx).GetAccountNumberSequence(from)
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return errors.Wrap(err, "Could not retrieve account sequence")
|
2019-09-15 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// TODO: Potentially allow overriding of gas price and gas limit
|
2020-04-01 18:49:21 +00:00
|
|
|
msg := types.NewMsgEthermint(seq, &toAddr, sdk.NewInt(amount), txBldr.Gas(),
|
2019-11-15 17:02:13 +00:00
|
|
|
sdk.NewInt(emint.DefaultGasPrice), data, from)
|
|
|
|
|
|
|
|
err = msg.ValidateBasic()
|
2019-09-15 16:12:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
2019-11-15 17:02:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
// GetCmdGenCreateTx generates an Ethermint transaction (excludes create operations)
|
2019-11-15 17:02:13 +00:00
|
|
|
func GetCmdGenCreateTx(cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "create [contract bytecode] [<amount (in photons)>]",
|
|
|
|
Short: "create contract through the evm using compiled bytecode",
|
|
|
|
Args: cobra.RangeArgs(1, 2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2019-12-13 19:50:19 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
|
2019-11-15 17:02:13 +00:00
|
|
|
|
|
|
|
payload := args[0]
|
|
|
|
if !strings.HasPrefix(payload, "0x") {
|
|
|
|
payload = "0x" + payload
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hexutil.Decode(payload)
|
|
|
|
if err != nil {
|
2020-04-07 20:00:06 +00:00
|
|
|
return err
|
2019-11-15 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var amount int64
|
|
|
|
if len(args) > 1 {
|
|
|
|
// Ambiguously decode amount from any base
|
|
|
|
amount, err = strconv.ParseInt(args[1], 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "invalid amount")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
from := cliCtx.GetFromAddress()
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
authclient.Codec = ethermintcodec.NewAppCodec(cdc)
|
|
|
|
_, seq, err := authtypes.NewAccountRetriever(authclient.Codec, cliCtx).GetAccountNumberSequence(from)
|
2019-11-15 17:02:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not retrieve account sequence")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Potentially allow overriding of gas price and gas limit
|
2020-04-01 18:49:21 +00:00
|
|
|
msg := types.NewMsgEthermint(seq, nil, sdk.NewInt(amount), txBldr.Gas(),
|
2019-11-15 17:02:13 +00:00
|
|
|
sdk.NewInt(emint.DefaultGasPrice), data, from)
|
|
|
|
|
2019-09-15 16:12:59 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:26:01 +00:00
|
|
|
if err = authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}); err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contractAddr := ethcrypto.CreateAddress(common.BytesToAddress(from.Bytes()), seq)
|
|
|
|
fmt.Printf(
|
|
|
|
"Contract will be deployed to: \nHex: %s\nCosmos Address: %s\n",
|
|
|
|
contractAddr.Hex(),
|
|
|
|
sdk.AccAddress(contractAddr.Bytes()),
|
|
|
|
)
|
|
|
|
return nil
|
2019-09-15 16:12:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|