Skip to content

Commit

Permalink
refactor e2e test zrc20 deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Nov 8, 2024
1 parent 0341cec commit 498f4ea
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
10 changes: 9 additions & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,15 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
// NOTE: v2 (gateway) setup called here because system contract needs to be set first, then gateway, then zrc20
deployerRunner.SetZEVMContractsV2()

deployerRunner.SetZEVMZRC20s()
zrc20Deployment := txserver.ZRC20Deployment{
ERC20Addr: deployerRunner.ERC20Addr,
SPLAddr: nil,
}
if testSolana {
zrc20Deployment.SPLAddr = deployerRunner.SPLAddr.ToPointer()
}

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
Expand Down
6 changes: 5 additions & 1 deletion cmd/zetae2e/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
zetae2econfig "github.com/zeta-chain/node/cmd/zetae2e/config"
"github.com/zeta-chain/node/cmd/zetae2e/local"
"github.com/zeta-chain/node/e2e/runner"
"github.com/zeta-chain/node/e2e/txserver"
"github.com/zeta-chain/node/e2e/utils"
"github.com/zeta-chain/node/testutil"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
Expand Down Expand Up @@ -142,7 +143,10 @@ func StressTest(cmd *cobra.Command, _ []string) {
case "LOCAL":
// deploy and set zevm contract
e2eTest.SetZEVMSystemContracts()
e2eTest.SetZEVMZRC20s()
e2eTest.SetZEVMZRC20s(txserver.ZRC20Deployment{
ERC20Addr: e2eTest.ERC20Addr,
SPLAddr: nil, // no stress tests for solana atm
})

// deposit on ZetaChain
e2eTest.DepositEther()
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2etests/test_solana_whitelist_spl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSolanaWhitelistSPL(r *runner.E2ERunner, _ []string) {
require.NoError(r, err)

// deploy SPL token, but don't whitelist in gateway
spl := r.DeploySPL(&privkey, false, uint64(1_000_000))
spl := r.DeploySPL(&privkey, false)

// check that whitelist entry doesn't exist for this spl
seed := [][]byte{[]byte("whitelist"), spl.PublicKey().Bytes()}
Expand Down
2 changes: 1 addition & 1 deletion e2e/runner/setup_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *E2ERunner) SetupSolana(deployerPrivateKey string) {
require.NoError(r, err)

// deploy test spl
tokenAccount := r.DeploySPL(&privkey, true, uint64(1_000_000))
tokenAccount := r.DeploySPL(&privkey, true)
r.SPLAddr = tokenAccount.PublicKey()
}

Expand Down
21 changes: 10 additions & 11 deletions e2e/runner/setup_zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,31 @@ func (r *E2ERunner) SetZEVMSystemContracts() {
}

// SetZEVMZRC20s set ZRC20 for the ZEVM
func (r *E2ERunner) SetZEVMZRC20s() {
func (r *E2ERunner) SetZEVMZRC20s(zrc20Deployment txserver.ZRC20Deployment) {
r.Logger.Print("⚙️ deploying ZRC20s on ZEVM")
startTime := time.Now()
defer func() {
r.Logger.Info("System contract deployments took %s\n", time.Since(startTime))
}()

// deploy system contracts and ZRC20 contracts on ZetaChain
erc20zrc20Addr, splzrc20Addr, err := r.ZetaTxServer.DeployZRC20s(
e2eutils.OperationalPolicyName,
e2eutils.AdminPolicyName,
r.ERC20Addr.Hex(),
r.SPLAddr.String(),
deployedZRC20Addresses, err := r.ZetaTxServer.DeployZRC20s(
zrc20Deployment,
r.skipChainOperations,
)
require.NoError(r, err)

// Set ERC20ZRC20Addr
r.ERC20ZRC20Addr = ethcommon.HexToAddress(erc20zrc20Addr)
r.ERC20ZRC20Addr = deployedZRC20Addresses.ERC20ZRC20Addr
r.ERC20ZRC20, err = zrc20.NewZRC20(r.ERC20ZRC20Addr, r.ZEVMClient)
require.NoError(r, err)

// Set SPLZRC20Addr
r.SPLZRC20Addr = ethcommon.HexToAddress(splzrc20Addr)
r.SPLZRC20, err = zrc20.NewZRC20(r.SPLZRC20Addr, r.ZEVMClient)
require.NoError(r, err)
// Set SPLZRC20Addr if set
if deployedZRC20Addresses.SPLZRC20Addr != (ethcommon.Address{}) {
r.SPLZRC20Addr = deployedZRC20Addresses.SPLZRC20Addr
r.SPLZRC20, err = zrc20.NewZRC20(r.SPLZRC20Addr, r.ZEVMClient)
require.NoError(r, err)
}

// set ZRC20 contracts
r.SetupETHZRC20()
Expand Down
4 changes: 2 additions & 2 deletions e2e/runner/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (r *E2ERunner) SPLDepositAndCall(
return sig
}

func (r *E2ERunner) DeploySPL(privateKey *solana.PrivateKey, whitelist bool, amountToMint uint64) *solana.Wallet {
func (r *E2ERunner) DeploySPL(privateKey *solana.PrivateKey, whitelist bool) *solana.Wallet {
lamport, err := r.SolanaClient.GetMinimumBalanceForRentExemption(r.Ctx, token.MINT_SIZE, rpc.CommitmentFinalized)
require.NoError(r, err)

Expand Down Expand Up @@ -250,7 +250,7 @@ func (r *E2ERunner) DeploySPL(privateKey *solana.PrivateKey, whitelist bool, amo
// minting some tokens to deployer for testing
ata := r.FindOrCreateAssociatedTokenAccount(*privateKey, privateKey.PublicKey(), tokenAccount.PublicKey())

mintToInstruction := token.NewMintToInstruction(amountToMint, tokenAccount.PublicKey(), ata, privateKey.PublicKey(), []solana.PublicKey{}).
mintToInstruction := token.NewMintToInstruction(uint64(1_000_000), tokenAccount.PublicKey(), ata, privateKey.PublicKey(), []solana.PublicKey{}).
Build()
signedTx = r.CreateSignedTransaction(
[]solana.Instruction{mintToInstruction},
Expand Down
76 changes: 48 additions & 28 deletions e2e/txserver/zeta_tx_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/gogoproto/proto"
"github.com/ethereum/go-ethereum/common"
"github.com/gagliardetto/solana-go"
"github.com/samber/lo"
"github.com/zeta-chain/ethermint/crypto/hd"
etherminttypes "github.com/zeta-chain/ethermint/types"
Expand All @@ -56,6 +58,18 @@ type SystemContractAddresses struct {
UniswapV2FactoryAddr, UniswapV2RouterAddr, ZEVMConnectorAddr, WZETAAddr, ERC20zrc20Addr string
}

// ZRC20Deployment configures deployment of ZRC20 contracts
type ZRC20Deployment struct {
ERC20Addr common.Address
SPLAddr *solana.PublicKey // if nil - no SPL ZRC20 is deployed
}

// ZRC20Addresses contains the addresses of deployed ZRC20 contracts
type ZRC20Addresses struct {
ERC20ZRC20Addr common.Address
SPLZRC20Addr common.Address
}

// EmissionsPoolAddress is the address of the emissions pool
// This address is constant for all networks because it is derived from emissions name
const EmissionsPoolAddress = "zeta1w43fn2ze2wyhu5hfmegr6vp52c3dgn0srdgymy"
Expand Down Expand Up @@ -383,38 +397,38 @@ func (zts ZetaTxServer) DeploySystemContracts(
// DeployZRC20s deploys the ZRC20 contracts
// returns the addresses of erc20 and spl zrc20
func (zts ZetaTxServer) DeployZRC20s(
accountOperational, accountAdmin, erc20Addr string, splAddr string,
zrc20Deployment ZRC20Deployment,
skipChain func(chainID int64) bool,
) (string, string, error) {
) (*ZRC20Addresses, error) {
// retrieve account
accOperational, err := zts.clientCtx.Keyring.Key(accountOperational)
accOperational, err := zts.clientCtx.Keyring.Key(utils.OperationalPolicyName)
if err != nil {
return "", "", err
return nil, err
}
addrOperational, err := accOperational.GetAddress()
if err != nil {
return "", "", err
return nil, err
}
accAdmin, err := zts.clientCtx.Keyring.Key(accountAdmin)
accAdmin, err := zts.clientCtx.Keyring.Key(utils.AdminPolicyName)
if err != nil {
return "", "", err
return nil, err
}
addrAdmin, err := accAdmin.GetAddress()
if err != nil {
return "", "", err
return nil, err
}

// authorization for deploying new ZRC20 has changed from accountOperational to accountAdmin in v19
// we use this query to check the current authorization for the message
// if pre v19 the query is not implement and authorization is operational
deployerAccount := accountAdmin
deployerAccount := utils.AdminPolicyName
deployerAddr := addrAdmin.String()
authorization, preV19, err := zts.fetchMessagePermissions(&fungibletypes.MsgDeployFungibleCoinZRC20{})
if err != nil {
return "", "", fmt.Errorf("failed to fetch message permissions: %s", err.Error())
return nil, fmt.Errorf("failed to fetch message permissions: %s", err.Error())
}
if preV19 || authorization == authoritytypes.PolicyType_groupOperational {
deployerAccount = accountOperational
deployerAccount = utils.OperationalPolicyName
deployerAddr = addrOperational.String()
}

Expand Down Expand Up @@ -449,16 +463,6 @@ func (zts ZetaTxServer) DeployZRC20s(
coin.CoinType_Gas,
100000,
),
fungibletypes.NewMsgDeployFungibleCoinZRC20(
deployerAddr,
splAddr,
chains.SolanaLocalnet.ChainId,
9,
"USDT",
"USDT",
coin.CoinType_ERC20,
100000,
),
fungibletypes.NewMsgDeployFungibleCoinZRC20(
deployerAddr,
"",
Expand All @@ -471,7 +475,7 @@ func (zts ZetaTxServer) DeployZRC20s(
),
fungibletypes.NewMsgDeployFungibleCoinZRC20(
deployerAddr,
erc20Addr,
zrc20Deployment.ERC20Addr.Hex(),
chains.GoerliLocalnet.ChainId,
6,
"USDT",
Expand All @@ -481,6 +485,19 @@ func (zts ZetaTxServer) DeployZRC20s(
),
}

if zrc20Deployment.SPLAddr != nil {
deployMsgs = append(deployMsgs, fungibletypes.NewMsgDeployFungibleCoinZRC20(
deployerAddr,
zrc20Deployment.SPLAddr.String(),
chains.SolanaLocalnet.ChainId,
9,
"USDT",
"USDT",
coin.CoinType_ERC20,
100000,
))
}

// apply skipChain filter and convert to sdk.Msg
deployMsgsIface := lo.FilterMap(
deployMsgs,
Expand All @@ -494,12 +511,12 @@ func (zts ZetaTxServer) DeployZRC20s(

res, err := zts.BroadcastTx(deployerAccount, deployMsgsIface...)
if err != nil {
return "", "", fmt.Errorf("deploy zrc20s: %w", err)
return nil, fmt.Errorf("deploy zrc20s: %w", err)
}

deployedEvents, ok := EventsOfType[*fungibletypes.EventZRC20Deployed](res.Events)
if !ok {
return "", "", fmt.Errorf("no EventZRC20Deployed in %s", res.TxHash)
return nil, fmt.Errorf("no EventZRC20Deployed in %s", res.TxHash)
}

zrc20Addrs := lo.Map(deployedEvents, func(ev *fungibletypes.EventZRC20Deployed, _ int) string {
Expand All @@ -508,26 +525,29 @@ func (zts ZetaTxServer) DeployZRC20s(

err = zts.InitializeLiquidityCaps(zrc20Addrs...)
if err != nil {
return "", "", fmt.Errorf("initialize liquidity cap: %w", err)
return nil, fmt.Errorf("initialize liquidity cap: %w", err)
}

// find erc20 zrc20
erc20zrc20, ok := lo.Find(deployedEvents, func(ev *fungibletypes.EventZRC20Deployed) bool {
return ev.ChainId == chains.GoerliLocalnet.ChainId && ev.CoinType == coin.CoinType_ERC20
})
if !ok {
return "", "", fmt.Errorf("unable to find erc20 zrc20")
return nil, fmt.Errorf("unable to find erc20 zrc20")
}

// find spl zrc20
splzrc20, ok := lo.Find(deployedEvents, func(ev *fungibletypes.EventZRC20Deployed) bool {
return ev.ChainId == chains.SolanaLocalnet.ChainId && ev.CoinType == coin.CoinType_ERC20
})
if !ok {
return "", "", fmt.Errorf("unable to find spl zrc20")
return nil, fmt.Errorf("unable to find spl zrc20")
}

return erc20zrc20.Contract, splzrc20.Contract, nil
return &ZRC20Addresses{
ERC20ZRC20Addr: common.HexToAddress(erc20zrc20.Contract),
SPLZRC20Addr: common.HexToAddress(splzrc20.Contract),
}, nil
}

// FundEmissionsPool funds the emissions pool with the given amount
Expand Down

0 comments on commit 498f4ea

Please sign in to comment.