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

Commit

Permalink
Merge branch 'main' of github.com:ChainSafe/chainbridge-core into main
Browse files Browse the repository at this point in the history
  • Loading branch information
P1sar committed Aug 12, 2021
2 parents 97bbd5f + 3090802 commit 73dedf2
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 122 deletions.
24 changes: 21 additions & 3 deletions chains/evm/calls/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package calls

import (
"context"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -67,18 +68,35 @@ func PrepareRegisterGenericResourceInput(handler common.Address, rId [32]byte, a
return input, nil
}

func PrepareERC20BalanceInput(erc20Addr, accountAddr common.Address) ([]byte, error) {
func PrepareERC20BalanceInput(accountAddr common.Address) ([]byte, error) {
a, err := abi.JSON(strings.NewReader(ERC20PresetMinterPauserABI))
if err != nil {
return []byte{}, err
}
input, err := a.Pack("balanceOf", erc20Addr, accountAddr)
input, err := a.Pack("balanceOf", accountAddr)
if err != nil {
return []byte{}, err
}
return input, nil
}

func ParseERC20BalanceOutput(output []byte) (*big.Int, error) {
a, err := abi.JSON(strings.NewReader(ERC20PresetMinterPauserABI))
if err != nil {
return new(big.Int), err
}

res, err := a.Unpack("balanceOf", output)
if err != nil {
log.Error().Err(fmt.Errorf("unpack output error: %v", err))
return new(big.Int), err
}

balance := abi.ConvertType(res[0], new(big.Int)).(*big.Int)

return balance, nil
}

func MinterRole(chainClient ChainClient, erc20Contract common.Address) ([32]byte, error) {
a, err := abi.JSON(strings.NewReader(ERC20PresetMinterPauserABI))
if err != nil {
Expand All @@ -89,7 +107,7 @@ func MinterRole(chainClient ChainClient, erc20Contract common.Address) ([32]byte
return [32]byte{}, err
}
msg := ethereum.CallMsg{From: common.Address{}, To: &erc20Contract, Data: input}
out, err := chainClient.CallContract(context.TODO(), toCallArg(msg), nil)
out, err := chainClient.CallContract(context.TODO(), ToCallArg(msg), nil)
if err != nil {
return [32]byte{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions chains/evm/calls/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func SliceTo32Bytes(in []byte) [32]byte {
return res
}

func toCallArg(msg ethereum.CallMsg) map[string]interface{} {
func ToCallArg(msg ethereum.CallMsg) map[string]interface{} {
arg := map[string]interface{}{
"from": msg.From,
"to": msg.To,
Expand Down Expand Up @@ -108,4 +108,4 @@ func Transact(client ChainClient, txFabric TxFabric, to *common.Address, data []
}
client.UnlockNonce()
return tx.Hash(), nil
}
}
16 changes: 8 additions & 8 deletions chains/evm/cli/bridge/register-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ var registerResourceCmd = &cobra.Command{
Use: "register-resource",
Short: "Register a resource ID",
Long: "Register a resource ID with a contract address for a handler",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return RegisterResourceEVMCMD(cmd, args, txFabric)
return RegisterResourceCmd(cmd, args, txFabric)
},
}

func BindBridgeRegisterResourceCLIFlags(cli *cobra.Command) {
func BindRegisterResourceCmdFlags(cli *cobra.Command) {
cli.Flags().String("handler", "", "handler contract address")
cli.Flags().String("bridge", "", "bridge contract address")
cli.Flags().String("target", "", "contract address to be registered")
cli.Flags().String("resourceId", "", "resource ID to be registered")
}

func init() {
BindBridgeRegisterResourceCLIFlags(registerResourceCmd)
BindRegisterResourceCmdFlags(registerResourceCmd)
}

func RegisterResourceEVMCMD(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
func RegisterResourceCmd(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
handlerAddressString := cmd.Flag("handler").Value.String()
resourceId := cmd.Flag("resourceId").Value.String()
targetAddress := cmd.Flag("target").Value.String()
Expand Down Expand Up @@ -71,9 +71,9 @@ Bridge address: %s

fmt.Printf("Registering contract %s with resource ID %s on handler %s", targetAddress, resourceId, handlerAddr)

ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(),gasPrice)
ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Error().Err(err)
log.Error().Err(fmt.Errorf("eth client intialization error: %v", err))
return err
}

Expand All @@ -83,7 +83,7 @@ Bridge address: %s
return err
}

_, err = calls.Transact(ethClient,txFabric, &bridgeAddress, registerResourceInput, gasLimit)
_, err = calls.Transact(ethClient, txFabric, &bridgeAddress, registerResourceInput, gasLimit)
if err != nil {
log.Error().Err(err)
return err
Expand Down
20 changes: 11 additions & 9 deletions chains/evm/cli/bridge/set-burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ var setBurnCmd = &cobra.Command{
Use: "set-burn",
Short: "Set a token contract as mintable/burnable",
Long: "Set a token contract as mintable/burnable in a handler",
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return SetBurnEVMCMD(cmd, args, txFabric)
},
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return SetBurnCmd(cmd, args, txFabric)
},
}
func BindBridgeSetBurnCLIFlags(cli *cobra.Command) {

func BindSetBurnCmdFlags(cli *cobra.Command) {
cli.Flags().String("handler", "", "ERC20 handler contract address")
cli.Flags().String("bridge", "", "bridge contract address")
cli.Flags().String("tokenContract", "", "token contract to be registered")
}

func init() {
BindBridgeSetBurnCLIFlags(setBurnCmd)
BindSetBurnCmdFlags(setBurnCmd)
}

func SetBurnEVMCMD(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
func SetBurnCmd(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
handlerAddress := cmd.Flag("handler").Value.String()
bridgeAddress := cmd.Flag("bridge").Value.String()
tokenAddress := cmd.Flag("tokenContract").Value.String()
Expand Down Expand Up @@ -60,7 +61,7 @@ func SetBurnEVMCMD(cmd *cobra.Command, args []string, txFabric calls.TxFabric) e

ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Error().Err(err)
log.Error().Err(fmt.Errorf("eth client intialization error: %v", err))
return err
}

Expand All @@ -73,8 +74,9 @@ func SetBurnEVMCMD(cmd *cobra.Command, args []string, txFabric calls.TxFabric) e

_, err = calls.Transact(ethClient, txFabric, &bridgeAddr, setBurnableInput, gasLimit)
if err != nil {
log.Info().Msg("Burnable set")
log.Error().Err(err)
return err
}
log.Info().Msg("Burnable set")
return nil
}
12 changes: 7 additions & 5 deletions chains/evm/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

var ErrNoDeploymentFalgsProvided = errors.New("provide at least one deployment flag. For help use --help.")
var ErrErc20TokenAndSymbolNotProvided = errors.New("erc20Name and erc20Symbol flags should be provided")

var DeployEVM = &cobra.Command{
Use: "deploy",
Expand Down Expand Up @@ -92,6 +93,7 @@ func DeployCLI(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error
log.Debug().Msgf("SENDER Private key 0x%s", hex.EncodeToString(crypto.FromECDSA(senderKeyPair.PrivateKey())))
ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Error().Err(fmt.Errorf("ethereum client error: %v", err)).Msg("error initializing new EVM client")
return err
}
relayerThreshold, err := cmd.Flags().GetUint64("relayerThreshold")
Expand All @@ -101,7 +103,7 @@ func DeployCLI(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error
}
relayerAddressesStringSlice := viper.GetStringSlice(RelayersFlagName)
if err != nil {
log.Error().Err(fmt.Errorf("relayer threshold error: %v", err))
log.Error().Err(fmt.Errorf("relayer addresses error: %v", err))
return err
}
var relayerAddresses []common.Address
Expand Down Expand Up @@ -196,8 +198,8 @@ func DeployCLI(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error
name := cmd.Flag("erc20Name").Value.String()
symbol := cmd.Flag("erc20Symbol").Value.String()
if name == "" || symbol == "" {
log.Error().Err(errors.New("erc20Name and erc20Symbol flags should be provided"))
return err
log.Error().Err(ErrErc20TokenAndSymbolNotProvided)
return ErrErc20TokenAndSymbolNotProvided
}

erc20Addr, err := calls.DeployErc20(ethClient, txFabric, name, symbol)
Expand All @@ -211,8 +213,8 @@ func DeployCLI(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error
return err
}
if name == "" || symbol == "" {
log.Error().Err(errors.New("erc20Name and erc20Symbol flags should be provided"))
return err
log.Error().Err(ErrErc20TokenAndSymbolNotProvided)
return ErrErc20TokenAndSymbolNotProvided
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions chains/evm/cli/erc20/add-minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ var addMinterCmd = &cobra.Command{
Use: "add-minter",
Short: "Add a minter to an Erc20 mintable contract",
Long: "Add a minter to an Erc20 mintable contract",
RunE: CallAddMinter,
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return AddMinterCmd(cmd, args, txFabric)
},
}

func init() {
addMinterCmd.Flags().String("erc20Address", "", "ERC20 contract address")
addMinterCmd.Flags().String("minter", "", "address of minter")
func BindAddMinterCmdFlags(cli *cobra.Command) {
cli.Flags().String("erc20Address", "", "ERC20 contract address")
cli.Flags().String("minter", "", "address of minter")
}

func CallAddMinter(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return addMinter(cmd, args, txFabric)
func init() {
BindAddMinterCmdFlags(addMinterCmd)
}
func addMinter(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {

func AddMinterCmd(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
erc20Address := cmd.Flag("erc20Address").Value.String()
minterAddress := cmd.Flag("minter").Value.String()

Expand All @@ -42,18 +45,20 @@ func addMinter(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error
if !common.IsHexAddress(erc20Address) {
err := errors.New("invalid erc20Address address")
log.Error().Err(err)
return err
}
erc20Addr := common.HexToAddress(erc20Address)

if !common.IsHexAddress(minterAddress) {
err := errors.New("invalid minter address")
log.Error().Err(err)
return err
}
minterAddr := common.HexToAddress(minterAddress)

ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Error().Err(err)
log.Error().Err(fmt.Errorf("eth client intialization error: %v", err))
return err
}
mintableInput, err := calls.PrepareErc20AddMinterInput(ethClient, erc20Addr, minterAddr)
Expand Down
91 changes: 46 additions & 45 deletions chains/evm/cli/erc20/allowance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ var allowanceCmd = &cobra.Command{
Use: "allowance",
Short: "Get the allowance of a spender for an address",
Long: "Get the allowance of a spender for an address",
RunE: CallAllowance,
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return AllowanceCmd(cmd, args, txFabric)
},
}

func init() {
allowanceCmd.Flags().String("erc20Address", "", "ERC20 contract address")
allowanceCmd.Flags().String("owner", "", "address of token owner")
allowanceCmd.Flags().String("spender", "", "address of spender")
func BindAllowanceCmdFlags(cli *cobra.Command) {
cli.Flags().String("erc20Address", "", "ERC20 contract address")
cli.Flags().String("owner", "", "address of token owner")
cli.Flags().String("spender", "", "address of spender")
}

func CallAllowance(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return allowance(cmd, args, txFabric)
func init() {
BindAllowanceCmdFlags(allowanceCmd)
}

func allowance(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
func AllowanceCmd(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
erc20Address := cmd.Flag("erc20Address").Value.String()
ownerAddress := cmd.Flag("owner").Value.String()
spenderAddress := cmd.Flag("spender").Value.String()
Expand All @@ -36,44 +38,43 @@ Owner address: %s
Spender address: %s`,
erc20Address, ownerAddress, spenderAddress)
return nil
}

/*
func allowance(cctx *cli.Context) error {
url := cctx.String("url")
gasLimit := cctx.Uint64("gasLimit")
gasPrice := cctx.Uint64("gasPrice")
sender, err := cliutils.DefineSender(cctx)
if err != nil {
return err
}
erc20 := cctx.String("erc20Address")
if !common.IsHexAddress(erc20) {
return errors.New("invalid erc20Address address")
}
erc20Address := common.HexToAddress(erc20)
/*
url := cctx.String("url")
gasLimit := cctx.Uint64("gasLimit")
gasPrice := cctx.Uint64("gasPrice")
sender, err := cliutils.DefineSender(cctx)
if err != nil {
return err
}
erc20 := cctx.String("erc20Address")
if !common.IsHexAddress(erc20) {
return errors.New("invalid erc20Address address")
}
erc20Address := common.HexToAddress(erc20)
spender := cctx.String("spender")
if !common.IsHexAddress(spender) {
return errors.New("invalid spender address")
}
spenderAddress := common.HexToAddress(spender)
spender := cctx.String("spender")
if !common.IsHexAddress(spender) {
return errors.New("invalid spender address")
}
spenderAddress := common.HexToAddress(spender)
owner := cctx.String("owner")
if !common.IsHexAddress(owner) {
return errors.New("invalid owner address")
}
ownerAddress := common.HexToAddress(owner)
owner := cctx.String("owner")
if !common.IsHexAddress(owner) {
return errors.New("invalid owner address")
}
ownerAddress := common.HexToAddress(owner)
ethClient, err := client.NewClient(url, false, sender, big.NewInt(0).SetUint64(gasLimit), big.NewInt(0).SetUint64(gasPrice), big.NewFloat(1))
if err != nil {
return err
}
balance, err := utils.ERC20Allowance(ethClient, erc20Address, spenderAddress, ownerAddress)
if err != nil {
return err
}
log.Info().Msgf("allowance of %s to spend from address %s is %s", spenderAddress.String(), ownerAddress.String(), balance.String())
return nil
ethClient, err := client.NewClient(url, false, sender, big.NewInt(0).SetUint64(gasLimit), big.NewInt(0).SetUint64(gasPrice), big.NewFloat(1))
if err != nil {
log.Error().Err(fmt.Errorf("eth client intialization error: %v", err))
return err
}
balance, err := utils.ERC20Allowance(ethClient, erc20Address, spenderAddress, ownerAddress)
if err != nil {
return err
}
log.Info().Msgf("allowance of %s to spend from address %s is %s", spenderAddress.String(), ownerAddress.String(), balance.String())
return nil
*/
}
*/
Loading

0 comments on commit 73dedf2

Please sign in to comment.