Skip to content

Commit

Permalink
refactor setup functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Dec 9, 2024
1 parent 31a0f25 commit 1fa308c
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 259 deletions.
8 changes: 2 additions & 6 deletions cmd/zetae2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func ExportContractsFromRunner(r *runner.E2ERunner, conf config.Config) config.C
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.EVM.Gateway = config.DoubleQuotedString(r.GatewayEVMAddr.Hex())
conf.Contracts.EVM.TestDAppV2Addr = config.DoubleQuotedString(r.TestDAppV2EVMAddr.Hex())

conf.Contracts.ZEVM.SystemContractAddr = config.DoubleQuotedString(r.SystemContractAddr.Hex())
conf.Contracts.ZEVM.ETHZRC20Addr = config.DoubleQuotedString(r.ETHZRC20Addr.Hex())
Expand All @@ -78,12 +80,6 @@ func ExportContractsFromRunner(r *runner.E2ERunner, conf config.Config) config.C
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())

// v2
conf.Contracts.EVM.Gateway = config.DoubleQuotedString(r.GatewayEVMAddr.Hex())
conf.Contracts.EVM.ERC20CustodyNew = config.DoubleQuotedString(r.ERC20CustodyV2Addr.Hex())
conf.Contracts.EVM.TestDAppV2Addr = config.DoubleQuotedString(r.TestDAppV2EVMAddr.Hex())

conf.Contracts.ZEVM.Gateway = config.DoubleQuotedString(r.GatewayZEVMAddr.Hex())
conf.Contracts.ZEVM.TestDAppV2Addr = config.DoubleQuotedString(r.TestDAppV2ZEVMAddr.Hex())

Expand Down
14 changes: 1 addition & 13 deletions cmd/zetae2e/config/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"fmt"

"github.com/gagliardetto/solana-go"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/erc20custody.sol"
zetaeth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zeta.eth.sol"
zetaconnectoreth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zetaconnector.eth.sol"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/zevm/systemcontract.sol"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/zevm/wzeta.sol"
connectorzevm "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/zevm/zetaconnectorzevm.sol"
"github.com/zeta-chain/protocol-contracts/v1/pkg/uniswap/v2-core/contracts/uniswapv2factory.sol"
uniswapv2router "github.com/zeta-chain/protocol-contracts/v1/pkg/uniswap/v2-periphery/contracts/uniswapv2router02.sol"
erc20custodyv2 "github.com/zeta-chain/protocol-contracts/v2/pkg/erc20custody.sol"
"github.com/zeta-chain/protocol-contracts/v2/pkg/erc20custody.sol"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayevm.sol"
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayzevm.sol"
"github.com/zeta-chain/protocol-contracts/v2/pkg/zrc20.sol"
Expand Down Expand Up @@ -253,17 +252,6 @@ func setContractsFromConfig(r *runner.E2ERunner, conf config.Config) error {
}
}

if c := conf.Contracts.EVM.ERC20CustodyNew; c != "" {
r.ERC20CustodyV2Addr, err = c.AsEVMAddress()
if err != nil {
return fmt.Errorf("invalid ERC20CustodyV2Addr: %w", err)
}
r.ERC20CustodyV2, err = erc20custodyv2.NewERC20Custody(r.ERC20CustodyV2Addr, r.EVMClient)
if err != nil {
return err
}
}

if c := conf.Contracts.EVM.TestDAppV2Addr; c != "" {
r.TestDAppV2EVMAddr, err = c.AsEVMAddress()
if err != nil {
Expand Down
22 changes: 10 additions & 12 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,23 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
// TODO: merge v1 and v2 together
// https://github.com/zeta-chain/node/issues/2627

deployerRunner.LegacySetupEVM(contractsDeployed, true)

// setup protocol contracts on the connected EVM chain
deployerRunner.SetupEVM()

//setup protocol contracts v1 as they are still supported for now
deployerRunner.LegacySetupEVM(contractsDeployed)

if testSolana {
deployerRunner.SetupSolana(
conf.Contracts.Solana.GatewayProgramID.String(),
conf.AdditionalAccounts.UserSolana.SolanaPrivateKey.String(),
)
}

deployerRunner.SetZEVMSystemContracts()

// NOTE: v2 (gateway) setup called here because system contract needs to be set first, then gateway, then zrc20
deployerRunner.SetZEVMContractsV2()
deployerRunner.SetupZEVMProtocolContracts()

deployerRunner.SetupLegacyZEVMContracts()

Check failure on line 239 in cmd/zetae2e/local/local.go

View workflow job for this annotation

GitHub Actions / build-and-test

deployerRunner.SetupLegacyZEVMContracts undefined (type *runner.E2ERunner has no field or method SetupLegacyZEVMContracts)

Check failure on line 239 in cmd/zetae2e/local/local.go

View workflow job for this annotation

GitHub Actions / build-and-test

deployerRunner.SetupLegacyZEVMContracts undefined (type *runner.E2ERunner has no field or method SetupLegacyZEVMContracts)

Check failure on line 239 in cmd/zetae2e/local/local.go

View workflow job for this annotation

GitHub Actions / lint

deployerRunner.SetupLegacyZEVMContracts undefined (type *runner.E2ERunner has no field or method SetupLegacyZEVMContracts) (typecheck)

Check failure on line 239 in cmd/zetae2e/local/local.go

View workflow job for this annotation

GitHub Actions / lint

deployerRunner.SetupLegacyZEVMContracts undefined (type *runner.E2ERunner has no field or method SetupLegacyZEVMContracts)) (typecheck)

zrc20Deployment := txserver.ZRC20Deployment{
ERC20Addr: deployerRunner.ERC20Addr,
Expand All @@ -243,14 +245,10 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
if testSolana {
zrc20Deployment.SPLAddr = deployerRunner.SPLAddr.ToPointer()
}
deployerRunner.SetupZEVMZRC20s(zrc20Deployment)

deployerRunner.SetZEVMZRC20s(zrc20Deployment)

// Update the chain params to use v2 contract for ERC20Custody
// TODO: this function should be removed and the chain params should be directly set to use v2 contract
// https://github.com/zeta-chain/node/issues/2627
deployerRunner.UpdateChainParamsV2Contracts()
deployerRunner.ERC20CustodyAddr = deployerRunner.ERC20CustodyV2Addr
// Update the chain params to contains protocol contract addresses
deployerRunner.UpdateProtocolContractsInChainParams()

deployerRunner.MintERC20OnEVM(1e10)

Expand Down
6 changes: 3 additions & 3 deletions cmd/zetae2e/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ func StressTest(cmd *cobra.Command, _ []string) {

// setup TSS addresses
noError(e2eTest.SetTSSAddresses())
e2eTest.LegacySetupEVM(stressTestArgs.contractsDeployed, true)
e2eTest.LegacySetupEVM(stressTestArgs.contractsDeployed)

// If stress test is running on local docker environment
switch stressTestArgs.network {
case "LOCAL":
// deploy and set zevm contract
e2eTest.SetZEVMSystemContracts()
e2eTest.SetZEVMZRC20s(txserver.ZRC20Deployment{
e2eTest.SetupZEVMProtocolContracts()
e2eTest.SetupZEVMZRC20s(txserver.ZRC20Deployment{
ERC20Addr: e2eTest.ERC20Addr,
SPLAddr: nil, // no stress tests for solana atm
})
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_migrate_chain_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMigrateChainSupport(r *runner.E2ERunner, _ []string) {
newRunner, err := configureEVM2(r)
require.NoError(r, err)

newRunner.LegacySetupEVM(false, false)
newRunner.LegacySetupEVM(false)

// mint some ERC20
newRunner.MintERC20OnEVM(10000)
Expand Down
18 changes: 3 additions & 15 deletions e2e/runner/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,14 @@ func (r *E2ERunner) checkERC20TSSBalance() error {
return err
}

custodyFullBalance := custodyBalance

// take into account the balance of the new ERC20 custody contract as v2 test use this contract
// if both addresses are equal, then there is no need to check the balance of the new contract
if r.ERC20CustodyAddr.Hex() != r.ERC20CustodyV2Addr.Hex() {
custodyV2Balance, err := r.ERC20.BalanceOf(&bind.CallOpts{}, r.ERC20CustodyV2Addr)
if err != nil {
return err
}
custodyFullBalance = big.NewInt(0).Add(custodyBalance, custodyV2Balance)
}

erc20zrc20Supply, err := r.ERC20ZRC20.TotalSupply(&bind.CallOpts{})
if err != nil {
return err
}
if custodyFullBalance.Cmp(erc20zrc20Supply) < 0 {
return fmt.Errorf("ERC20: TSS balance (%d) < ZRC20 TotalSupply (%d) ", custodyFullBalance, erc20zrc20Supply)
if custodyBalance.Cmp(erc20zrc20Supply) < 0 {
return fmt.Errorf("ERC20: TSS balance (%d) < ZRC20 TotalSupply (%d) ", custodyBalance, erc20zrc20Supply)
}
r.Logger.Info("ERC20: TSS balance (%d) >= ERC20 ZRC20 TotalSupply (%d)", custodyFullBalance, erc20zrc20Supply)
r.Logger.Info("ERC20: TSS balance (%d) >= ERC20 ZRC20 TotalSupply (%d)", custodyBalance, erc20zrc20Supply)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions e2e/runner/admin_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (r *E2ERunner) UpdateTSSAddressForConnector() {
func (r *E2ERunner) UpdateTSSAddressForERC20custody() {
require.NoError(r, r.SetTSSAddresses())

tx, err := r.ERC20CustodyV2.UpdateTSSAddress(r.EVMAuth, r.TSSAddress)
tx, err := r.ERC20Custody.UpdateTSSAddress(r.EVMAuth, r.TSSAddress)
require.NoError(r, err)
r.Logger.Info("TSS ERC20 Address Update Tx: %s", tx.Hash().String())
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)

// we have to reference ERC20CustodyV2 since it's `TssAddress` on v2 and `TSSAddress` on v1
tssAddressOnCustody, err := r.ERC20CustodyV2.TssAddress(&bind.CallOpts{Context: r.Ctx})
// we have to reference ERC20Custody since it's `TssAddress` on v2 and `TSSAddress` on v1
tssAddressOnCustody, err := r.ERC20Custody.TssAddress(&bind.CallOpts{Context: r.Ctx})
require.NoError(r, err)
require.Equal(r, r.TSSAddress, tssAddressOnCustody)
}
6 changes: 6 additions & 0 deletions e2e/runner/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/zeta-chain/protocol-contracts/v2/pkg/gatewayevm.sol"

"github.com/zeta-chain/node/e2e/utils"
"github.com/zeta-chain/node/pkg/constant"
)

// ETHDeposit calls Deposit of Gateway with gas token on EVM
Expand Down Expand Up @@ -199,6 +200,11 @@ func (r *E2ERunner) ApproveERC20ZRC20(allowed ethcommon.Address) {
}
}

// DonateEtherToTSS donates ether to TSS
func (r *E2ERunner) DonateEtherToTSS(amount *big.Int) (*ethtypes.Transaction, error) {
return r.LegacySendEther(r.TSSAddress, amount, []byte(constant.DonationMessage))
}

// AnvilMineBlocks mines blocks on Anvil localnet
// the block time is provided in seconds
// the method returns a function to stop the mining
Expand Down
51 changes: 2 additions & 49 deletions e2e/runner/legacy_setup_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/erc20custody.sol"
zetaeth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zeta.eth.sol"
zetaconnectoreth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zetaconnector.eth.sol"

"github.com/zeta-chain/node/e2e/config"
"github.com/zeta-chain/node/e2e/contracts/erc20"
"github.com/zeta-chain/node/e2e/contracts/testdapp"
"github.com/zeta-chain/node/e2e/utils"
"github.com/zeta-chain/node/pkg/chains"
Expand Down Expand Up @@ -41,16 +39,14 @@ func (r *E2ERunner) LegacySetEVMContractsFromConfig() {
}

// LegacySetupEVM setup legacy contracts on EVM for e2e test
func (r *E2ERunner) LegacySetupEVM(contractsDeployed bool, whitelistERC20 bool) {
r.Logger.Print("⚙️ setting up EVM network")
func (r *E2ERunner) LegacySetupEVM(contractsDeployed bool) {
r.Logger.Print("⚙️ setting up EVM network legacy contracts")
startTime := time.Now()
defer func() {
r.Logger.Info("EVM setup took %s\n", time.Since(startTime))
}()

// TODO: put this logic outside of this function
// We use this config to be consistent with the old implementation
// https://github.com/zeta-chain/node-private/issues/41
if contractsDeployed {
r.LegacySetEVMContractsFromConfig()
return
Expand Down Expand Up @@ -99,30 +95,6 @@ func (r *E2ERunner) LegacySetupEVM(contractsDeployed bool, whitelistERC20 bool)
txConnector.Hash().Hex(),
)

r.Logger.Info("Deploying ERC20Custody contract")
erc20CustodyAddr, txCustody, ERC20Custody, err := erc20custody.DeployERC20Custody(
r.EVMAuth,
r.EVMClient,
r.EVMAddress(),
r.EVMAddress(),
big.NewInt(0),
big.NewInt(1e18),
ethcommon.HexToAddress("0x"),
)
require.NoError(r, err)

r.ERC20CustodyAddr = erc20CustodyAddr
r.ERC20Custody = ERC20Custody
r.Logger.Info("ERC20Custody contract address: %s, tx hash: %s", erc20CustodyAddr.Hex(), txCustody.Hash().Hex())

r.Logger.Info("Deploying ERC20 contract")
erc20Addr, txERC20, erc20, err := erc20.DeployERC20(r.EVMAuth, r.EVMClient, "TESTERC20", "TESTERC20", 6)
require.NoError(r, err)

r.ERC20 = erc20
r.ERC20Addr = erc20Addr
r.Logger.Info("ERC20 contract address: %s, tx hash: %s", erc20Addr.Hex(), txERC20.Hash().Hex())

// deploy TestDApp contract
appAddr, txApp, _, err := testdapp.DeployTestDApp(
r.EVMAuth,
Expand All @@ -144,26 +116,8 @@ func (r *E2ERunner) LegacySetupEVM(contractsDeployed bool, whitelistERC20 bool)
ensureTxReceipt(txDonation, "EVM donation tx failed")
ensureTxReceipt(txZetaEth, "ZetaEth deployment failed")
ensureTxReceipt(txConnector, "ZetaConnectorEth deployment failed")
ensureTxReceipt(txCustody, "ERC20Custody deployment failed")
ensureTxReceipt(txERC20, "ERC20 deployment failed")
ensureTxReceipt(txApp, "TestDApp deployment failed")

// initialize custody contract
r.Logger.Info("Whitelist ERC20")
if whitelistERC20 {
txWhitelist, err := ERC20Custody.Whitelist(r.EVMAuth, erc20Addr)
require.NoError(r, err)
ensureTxReceipt(txWhitelist, "ERC20 whitelist failed")
}

r.Logger.Info("Set TSS address")
txCustody, err = ERC20Custody.UpdateTSSAddress(r.EVMAuth, r.TSSAddress)
require.NoError(r, err)

ensureTxReceipt(txCustody, "ERC20 update TSS address failed")

r.Logger.Info("TSS set receipt tx hash: %s", txCustody.Hash().Hex())

// save config containing contract addresses
// TODO: put this logic outside of this function in a general config
// We use this config to be consistent with the old implementation
Expand All @@ -181,7 +135,6 @@ func (r *E2ERunner) LegacySetupEVM(contractsDeployed bool, whitelistERC20 bool)
require.NoError(r, err, "failed to get chain params for chain %d", chains.GoerliLocalnet.ChainId)

chainParams := currentChainParamsRes.ChainParams
chainParams.Erc20CustodyContractAddress = r.ERC20CustodyAddr.Hex()
chainParams.ConnectorContractAddress = r.ConnectorEthAddr.Hex()
chainParams.ZetaTokenContractAddress = r.ZetaEthAddr.Hex()

Expand Down
50 changes: 19 additions & 31 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/erc20custody.sol"
zetaeth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zeta.eth.sol"
zetaconnectoreth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zetaconnector.eth.sol"
"github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/zevm/systemcontract.sol"
Expand Down Expand Up @@ -116,15 +115,19 @@ type E2ERunner struct {
SPLAddr solana.PublicKey

// contracts evm
ZetaEthAddr ethcommon.Address
ZetaEth *zetaeth.ZetaEth
ConnectorEthAddr ethcommon.Address
ConnectorEth *zetaconnectoreth.ZetaConnectorEth
ERC20CustodyAddr ethcommon.Address
ERC20Custody *erc20custody.ERC20Custody
ERC20Addr ethcommon.Address
ERC20 *erc20.ERC20
EvmTestDAppAddr ethcommon.Address
ZetaEthAddr ethcommon.Address
ZetaEth *zetaeth.ZetaEth
ConnectorEthAddr ethcommon.Address
ConnectorEth *zetaconnectoreth.ZetaConnectorEth
ERC20CustodyAddr ethcommon.Address
ERC20Custody *erc20custodyv2.ERC20Custody
ERC20Addr ethcommon.Address
ERC20 *erc20.ERC20
EvmTestDAppAddr ethcommon.Address
GatewayEVMAddr ethcommon.Address
GatewayEVM *gatewayevm.GatewayEVM
TestDAppV2EVMAddr ethcommon.Address
TestDAppV2EVM *testdappv2.TestDAppV2

// contracts zevm
ERC20ZRC20Addr ethcommon.Address
Expand Down Expand Up @@ -154,6 +157,10 @@ type E2ERunner struct {
SystemContractAddr ethcommon.Address
SystemContract *systemcontract.SystemContract
ZevmTestDAppAddr ethcommon.Address
GatewayZEVMAddr ethcommon.Address
GatewayZEVM *gatewayzevm.GatewayZEVM
TestDAppV2ZEVMAddr ethcommon.Address
TestDAppV2ZEVM *testdappv2.TestDAppV2

// config
CctxTimeout time.Duration
Expand All @@ -166,20 +173,6 @@ type E2ERunner struct {
Logger *Logger
BitcoinParams *chaincfg.Params
mutex sync.Mutex

// evm v2
GatewayEVMAddr ethcommon.Address
GatewayEVM *gatewayevm.GatewayEVM
ERC20CustodyV2Addr ethcommon.Address
ERC20CustodyV2 *erc20custodyv2.ERC20Custody
TestDAppV2EVMAddr ethcommon.Address
TestDAppV2EVM *testdappv2.TestDAppV2

// zevm v2
GatewayZEVMAddr ethcommon.Address
GatewayZEVM *gatewayzevm.GatewayZEVM
TestDAppV2ZEVMAddr ethcommon.Address
TestDAppV2ZEVM *testdappv2.TestDAppV2
}

func NewE2ERunner(
Expand Down Expand Up @@ -268,7 +261,7 @@ func (r *E2ERunner) CopyAddressesFrom(other *E2ERunner) (err error) {
if err != nil {
return err
}
r.ERC20Custody, err = erc20custody.NewERC20Custody(r.ERC20CustodyAddr, r.EVMClient)
r.ERC20Custody, err = erc20custodyv2.NewERC20Custody(r.ERC20CustodyAddr, r.EVMClient)
if err != nil {
return err
}
Expand Down Expand Up @@ -332,11 +325,7 @@ func (r *E2ERunner) CopyAddressesFrom(other *E2ERunner) (err error) {
if err != nil {
return err
}
r.ERC20CustodyV2Addr = other.ERC20CustodyV2Addr
r.ERC20CustodyV2, err = erc20custodyv2.NewERC20Custody(r.ERC20CustodyV2Addr, r.EVMClient)
if err != nil {
return err
}

r.TestDAppV2EVMAddr = other.TestDAppV2EVMAddr
r.TestDAppV2EVM, err = testdappv2.NewTestDAppV2(r.TestDAppV2EVMAddr, r.EVMClient)
if err != nil {
Expand Down Expand Up @@ -409,7 +398,6 @@ func (r *E2ERunner) PrintContractAddresses() {

r.Logger.Print(" --- 📜EVM v2 contracts ---")
r.Logger.Print("GatewayEVM: %s", r.GatewayEVMAddr.Hex())
r.Logger.Print("ERC20CustodyV2: %s", r.ERC20CustodyV2Addr.Hex())
r.Logger.Print("TestDAppV2EVM: %s", r.TestDAppV2EVMAddr.Hex())
}

Expand Down
Loading

0 comments on commit 1fa308c

Please sign in to comment.