Skip to content

Commit

Permalink
refactor(e2e): move all addresses and private keys to config
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Jun 26, 2024
1 parent 47d835a commit a371762
Show file tree
Hide file tree
Showing 47 changed files with 468 additions and 354 deletions.
12 changes: 1 addition & 11 deletions cmd/zetae2e/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"errors"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -54,21 +52,13 @@ func runBalances(cmd *cobra.Command, args []string) error {
// initialize context
ctx, cancel := context.WithCancel(context.Background())

// get EVM address from config
evmAddr := conf.Accounts.EVMAddress
if !ethcommon.IsHexAddress(evmAddr) {
cancel()
return errors.New("invalid EVM address")
}

// initialize deployer runner with config
r, err := zetae2econfig.RunnerFromConfig(
ctx,
"e2e",
cancel,
conf,
ethcommon.HexToAddress(evmAddr),
conf.Accounts.EVMPrivKey,
conf.Accounts.Deployer,
logger,
)
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions cmd/zetae2e/bitcoin_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"errors"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -54,21 +52,13 @@ func runBitcoinAddress(cmd *cobra.Command, args []string) error {
// initialize context
ctx, cancel := context.WithCancel(context.Background())

// get EVM address from config
evmAddr := conf.Accounts.EVMAddress
if !ethcommon.IsHexAddress(evmAddr) {
cancel()
return errors.New("invalid EVM address")
}

// initialize deployer runner with config
r, err := zetae2econfig.RunnerFromConfig(
ctx,
"e2e",
cancel,
conf,
ethcommon.HexToAddress(evmAddr),
conf.Accounts.EVMPrivKey,
conf.Accounts.Deployer,
logger,
)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions cmd/zetae2e/config/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"google.golang.org/grpc"

Expand All @@ -20,7 +19,7 @@ import (
)

// getClientsFromConfig get clients from config
func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey string) (
func getClientsFromConfig(ctx context.Context, conf config.Config, account config.Account) (
*rpcclient.Client,
*ethclient.Client,
*bind.TransactOpts,
Expand All @@ -38,7 +37,7 @@ func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey st
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get btc client: %w", err)
}
evmClient, evmAuth, err := getEVMClient(ctx, conf.RPCs.EVM, evmPrivKey)
evmClient, evmAuth, err := getEVMClient(ctx, conf.RPCs.EVM, account)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get evm client: %w", err)
}
Expand All @@ -48,7 +47,7 @@ func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey st
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get zeta clients: %w", err)
}
zevmClient, zevmAuth, err := getEVMClient(ctx, conf.RPCs.Zevm, evmPrivKey)
zevmClient, zevmAuth, err := getEVMClient(ctx, conf.RPCs.Zevm, account)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get zevm client: %w", err)
}
Expand Down Expand Up @@ -91,7 +90,7 @@ func getBtcClient(rpcConf config.BitcoinRPC) (*rpcclient.Client, error) {
}

// getEVMClient get evm client
func getEVMClient(ctx context.Context, rpc, privKey string) (*ethclient.Client, *bind.TransactOpts, error) {
func getEVMClient(ctx context.Context, rpc string, account config.Account) (*ethclient.Client, *bind.TransactOpts, error) {
evmClient, err := ethclient.Dial(rpc)
if err != nil {
return nil, nil, fmt.Errorf("failed to dial evm client: %w", err)
Expand All @@ -101,11 +100,11 @@ func getEVMClient(ctx context.Context, rpc, privKey string) (*ethclient.Client,
if err != nil {
return nil, nil, fmt.Errorf("failed to get chain id: %w", err)
}
deployerPrivkey, err := crypto.HexToECDSA(privKey)
privKey, err := account.PrivateKey()
if err != nil {
return nil, nil, fmt.Errorf("failed to get deployer privkey: %w", err)
}
evmAuth, err := bind.NewKeyedTransactorWithChainID(deployerPrivkey, chainid)
evmAuth, err := bind.NewKeyedTransactorWithChainID(privKey, chainid)
if err != nil {
return nil, nil, fmt.Errorf("failed to get keyed transactor: %w", err)
}
Expand Down
42 changes: 19 additions & 23 deletions cmd/zetae2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

ethcommon "github.com/ethereum/go-ethereum/common"

"github.com/zeta-chain/zetacore/e2e/config"
"github.com/zeta-chain/zetacore/e2e/runner"
)
Expand All @@ -16,8 +14,7 @@ func RunnerFromConfig(
name string,
ctxCancel context.CancelFunc,
conf config.Config,
evmUserAddr ethcommon.Address,
evmUserPrivKey string,
account config.Account,
logger *runner.Logger,
opts ...runner.E2ERunnerOption,
) (*runner.E2ERunner, error) {
Expand All @@ -33,7 +30,7 @@ func RunnerFromConfig(
lightClient,
zevmClient,
zevmAuth,
err := getClientsFromConfig(ctx, conf, evmUserPrivKey)
err := getClientsFromConfig(ctx, conf, account)
if err != nil {
return nil, fmt.Errorf("failed to get clients from config: %w", err)
}
Expand All @@ -43,8 +40,7 @@ func RunnerFromConfig(
ctx,
name,
ctxCancel,
evmUserAddr,
evmUserPrivKey,
account,
evmClient,
zevmClient,
cctxClient,
Expand Down Expand Up @@ -79,23 +75,23 @@ func RunnerFromConfig(
// ExportContractsFromRunner export contracts from the runner to config using a source config
func ExportContractsFromRunner(r *runner.E2ERunner, conf config.Config) config.Config {
// copy contracts from deployer runner
conf.Contracts.EVM.ZetaEthAddress = r.ZetaEthAddr.Hex()
conf.Contracts.EVM.ConnectorEthAddr = r.ConnectorEthAddr.Hex()
conf.Contracts.EVM.CustodyAddr = r.ERC20CustodyAddr.Hex()
conf.Contracts.EVM.ERC20 = r.ERC20Addr.Hex()
conf.Contracts.EVM.TestDappAddr = r.EvmTestDAppAddr.Hex()
conf.Contracts.EVM.ZetaEthAddr = config.DoubleQuotedString(r.ZetaEthAddr.Hex())
conf.Contracts.EVM.ConnectorEthAddr = config.DoubleQuotedString(r.ConnectorEthAddr.Hex())
conf.Contracts.EVM.CustodyAddr = config.DoubleQuotedString(r.ERC20CustodyAddr.Hex())
conf.Contracts.EVM.ERC20 = config.DoubleQuotedString(r.ERC20Addr.Hex())
conf.Contracts.EVM.TestDappAddr = config.DoubleQuotedString(r.EvmTestDAppAddr.Hex())

conf.Contracts.ZEVM.SystemContractAddr = r.SystemContractAddr.Hex()
conf.Contracts.ZEVM.ETHZRC20Addr = r.ETHZRC20Addr.Hex()
conf.Contracts.ZEVM.ERC20ZRC20Addr = r.ERC20ZRC20Addr.Hex()
conf.Contracts.ZEVM.BTCZRC20Addr = r.BTCZRC20Addr.Hex()
conf.Contracts.ZEVM.UniswapFactoryAddr = r.UniswapV2FactoryAddr.Hex()
conf.Contracts.ZEVM.UniswapRouterAddr = r.UniswapV2RouterAddr.Hex()
conf.Contracts.ZEVM.ConnectorZEVMAddr = r.ConnectorZEVMAddr.Hex()
conf.Contracts.ZEVM.WZetaAddr = r.WZetaAddr.Hex()
conf.Contracts.ZEVM.ZEVMSwapAppAddr = r.ZEVMSwapAppAddr.Hex()
conf.Contracts.ZEVM.ContextAppAddr = r.ContextAppAddr.Hex()
conf.Contracts.ZEVM.TestDappAddr = r.ZevmTestDAppAddr.Hex()
conf.Contracts.ZEVM.SystemContractAddr = config.DoubleQuotedString(r.SystemContractAddr.Hex())
conf.Contracts.ZEVM.ETHZRC20Addr = config.DoubleQuotedString(r.ETHZRC20Addr.Hex())
conf.Contracts.ZEVM.ERC20ZRC20Addr = config.DoubleQuotedString(r.ERC20ZRC20Addr.Hex())
conf.Contracts.ZEVM.BTCZRC20Addr = config.DoubleQuotedString(r.BTCZRC20Addr.Hex())
conf.Contracts.ZEVM.UniswapFactoryAddr = config.DoubleQuotedString(r.UniswapV2FactoryAddr.Hex())
conf.Contracts.ZEVM.UniswapRouterAddr = config.DoubleQuotedString(r.UniswapV2RouterAddr.Hex())
conf.Contracts.ZEVM.ConnectorZEVMAddr = config.DoubleQuotedString(r.ConnectorZEVMAddr.Hex())
conf.Contracts.ZEVM.WZetaAddr = config.DoubleQuotedString(r.WZetaAddr.Hex())
conf.Contracts.ZEVM.ZEVMSwapAppAddr = config.DoubleQuotedString(r.ZEVMSwapAppAddr.Hex())
conf.Contracts.ZEVM.ContextAppAddr = config.DoubleQuotedString(r.ContextAppAddr.Hex())
conf.Contracts.ZEVM.TestDappAddr = config.DoubleQuotedString(r.ZevmTestDAppAddr.Hex())

return conf
}
Loading

0 comments on commit a371762

Please sign in to comment.