Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
some changes to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
P1sar committed Aug 9, 2021
1 parent f6ee988 commit 2ea2944
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
9 changes: 2 additions & 7 deletions chains/evm/calls/bridge.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package calls

import (
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

// @dev
// inputs here and in erc20.go could get consolidated into something similar to txFabric in deploy.go

func PrepareSetBurnableInput(client ChainClient, handler, tokenAddress common.Address) ([]byte, error) {
a, err := abi.JSON(strings.NewReader(BridgeABI))
if err != nil {
Expand All @@ -35,15 +31,14 @@ func PrepareAdminSetResourceInput(handler common.Address, rId [32]byte, addr com
return input, nil
}

func PrepareErc20DepositInput(bridgeAddress, recipientAddress common.Address, amount *big.Int, rId [32]byte, destChainId uint8) ([]byte, error) {
func PrepareErc20DepositInput(destChainID uint8, resourceID [32]byte, data []byte) ([]byte, error) {
a, err := abi.JSON(strings.NewReader(BridgeABI))
if err != nil {
return []byte{}, err
}
input, err := a.Pack("deposit", bridgeAddress, recipientAddress, amount, rId, destChainId)
input, err := a.Pack("deposit", destChainID, resourceID, data)
if err != nil {
return []byte{}, err
}
input = append(input, common.FromHex(ERC20PresetMinterPauserBin)...)
return input, nil
}
1 change: 0 additions & 1 deletion chains/evm/cli/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

var AliceKp = keystore.TestKeyRing.EthereumKeys[keystore.AliceKey]

type EventSig string

Expand Down
42 changes: 25 additions & 17 deletions chains/evm/cli/erc20/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ var approveCmd = &cobra.Command{
Use: "approve",
Short: "Approve tokens in an ERC20 contract for transfer",
Long: "Approve tokens in an ERC20 contract for transfer",
RunE: CallApprove,
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return Approve(cmd, args, txFabric)
},
}

func init() {
approveCmd.Flags().String("erc20Address", "", "ERC20 contract address")
approveCmd.Flags().String("amount", "", "amount to grant allowance")
approveCmd.Flags().String("recipient", "", "address of recipient")
approveCmd.Flags().Uint64("decimals", 0, "ERC20 token decimals")
approveCmd.MarkFlagRequired("decimals")
}
func BindApproveCLIFlags(cli *cobra.Command) {
cli.Flags().String("erc20address", "", "ERC20 contract address")
cli.Flags().String("amount", "", "amount to grant allowance")
cli.Flags().String("recipient", "", "address of recipient")
cli.Flags().Uint64("decimals", 18, "ERC20 token decimals")
err := cli.MarkFlagRequired("decimals")
if err != nil {
panic(err)
}

func CallApprove(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return approve(cmd, args, txFabric)
}
func init() {
BindApproveCLIFlags(approveCmd)
}

func approve(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
erc20Address := cmd.Flag("erc20Address").Value.String()
func Approve(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
erc20Address := cmd.Flag("erc20address").Value.String()
recipientAddress := cmd.Flag("recipient").Value.String()
amount := cmd.Flag("amount").Value.String()
decimals, err := cmd.Flags().GetUint64("decimals")
Expand All @@ -48,7 +53,7 @@ Approving ERC20
ERC20 address: %s
Recipient address: %s
Amount: %s
Decimals: %s`,
Decimals: %v`,
erc20Address, recipientAddress, amount, decimals)

// fetch global flag values
Expand All @@ -72,21 +77,24 @@ Decimals: %s`,
realAmount, err := cliutils.UserAmountToWei(amount, decimalsBigInt)
if err != nil {
log.Fatal().Err(err)
return err
}

ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Fatal().Err(err)
return err
}

i, err := calls.PrepareErc20ApproveInput(erc20Addr, realAmount)
i, err := calls.PrepareErc20ApproveInput(recipientAddr, realAmount)
if err != nil {
log.Fatal().Err(err)
return err
}
_, err = calls.Transact(ethClient, txFabric, &erc20Addr, i, gasLimit)
if err != nil {
log.Fatal().Err(err)
return err
}
log.Info().Msgf("%s account granted allowance on %v tokens of %s", recipientAddr.String(), amount, erc20Addr.String())
log.Info().Msgf("%s account granted allowance on %v tokens of %s", recipientAddr.String(), amount, recipientAddr.String())
return nil
}
48 changes: 26 additions & 22 deletions chains/evm/cli/erc20/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package erc20

import (
"fmt"
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/cliutils"
"math/big"
"strconv"

Expand All @@ -18,32 +19,36 @@ var depositCmd = &cobra.Command{
Use: "deposit",
Short: "Initiate a transfer of ERC20 tokens",
Long: "Initiate a transfer of ERC20 tokens",
RunE: CallDeposit,
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return DepositCMD(cmd, args, txFabric)
},
}

func init() {
depositCmd.Flags().String("recipient", "", "address of recipient")
depositCmd.Flags().String("bridge", "", "address of bridge contract")
depositCmd.Flags().String("amount", "", "amount to deposit")
depositCmd.Flags().String("value", "0", "value of ETH that should be sent along with deposit to cover possible fees. In ETH (decimals are allowed)")
depositCmd.Flags().String("destId", "", "destination chain ID")
depositCmd.Flags().String("resourceId", "", "resource ID for transfer")
depositCmd.Flags().Uint64("decimals", 0, "ERC20 token decimals")
depositCmd.MarkFlagRequired("decimals")
func BindDepositCMDFlags(cli *cobra.Command) {
cli.Flags().String("recipient", "", "address of recipient")
cli.Flags().String("bridge", "", "address of bridge contract")
cli.Flags().String("amount", "", "amount to deposit")
cli.Flags().String("destId", "", "destination chain ID")
cli.Flags().String("resourceId", "", "resource ID for transfer")
cli.Flags().Uint64("decimals", 0, "ERC20 token decimals")
cli.MarkFlagRequired("decimals")
}

func CallDeposit(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return deposit(cmd, args, txFabric)
func init() {
BindDepositCMDFlags(depositCmd)
}

func deposit(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
recipientAddress := cmd.Flag("recipient").Value.String()
func DepositCMD(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
recipient := cmd.Flag("recipient").Value.String()
bridgeAddress := cmd.Flag("bridge").Value.String()
amount := cmd.Flag("amount").Value.String()
destinationId := cmd.Flag("destId").Value.String()
resourceId := cmd.Flag("resourceId").Value.String()

if !common.IsHexAddress(recipient) {
return fmt.Errorf("invalid recipient address %s", recipient)
}
recipientAddress := common.HexToAddress(recipient)
// fetch global flag values
url, gasLimit, gasPrice, senderKeyPair, err := flags.GlobalFlagValues(cmd)
if err != nil {
Expand All @@ -59,10 +64,9 @@ func deposit(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {

bridgeAddr := common.HexToAddress(bridgeAddress)

if !common.IsHexAddress(recipientAddress) {
if !common.IsHexAddress(recipient) {
return fmt.Errorf("invalid recipient address %s", recipientAddress)
}
recipientAddr := common.HexToAddress(recipientAddress)

realAmount, err := calls.UserAmountToWei(amount, decimals)
if err != nil {
Expand All @@ -82,22 +86,22 @@ func deposit(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
log.Error().Err(fmt.Errorf("destination ID conversion error: %v", err))
return err
}

data := cliutils.ConstructErc20DepositData(recipientAddress.Bytes(), realAmount)
// TODO: confirm correct arguments
input, err := calls.PrepareErc20DepositInput(bridgeAddr, recipientAddr, realAmount, resourceIDBytes, uint8(destinationIdInt))
input, err := calls.PrepareErc20DepositInput(uint8(destinationIdInt), resourceIDBytes, data)
if err != nil {
log.Error().Err(fmt.Errorf("erc20 deposit input error: %v", err))
return err
}
// destinationId
txHash, err := calls.Transact(ethClient, txFabric, &recipientAddr, input, gasLimit)
txHash, err := calls.Transact(ethClient, txFabric, &bridgeAddr, input, gasLimit)
if err != nil {
log.Error().Err(fmt.Errorf("erc20 deposit error: %v", err))
return err
}

log.Debug().Msgf("erc20 deposit hash: %s", txHash.Hex())

log.Info().Msgf("%s tokens were transferred to %s from %s", amount, recipientAddr.Hex(), senderKeyPair.CommonAddress().String())
log.Info().Msgf("%s tokens were transferred to %s from %s", amount, recipientAddress.Hex(), senderKeyPair.CommonAddress().String())
return nil
}
2 changes: 1 addition & 1 deletion chains/evm/cli/flags/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GlobalFlagValues(cmd *cobra.Command) (string, uint64, *big.Int, *secp256k1.
gasPriceInt, err := cmd.Flags().GetUint64("gasPrice")
if err != nil {
log.Error().Err(fmt.Errorf("gas price error: %v", err))
return "", evmclient.DefaultGasLimit, nil, nil, err
return "", evmclient.DefaultGasPrice, nil, nil, err
}

gasPrice := big.NewInt(0).SetUint64(gasPriceInt)
Expand Down

0 comments on commit 2ea2944

Please sign in to comment.