Skip to content

Commit

Permalink
e2e: finish distribute test
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Nov 15, 2024
1 parent 125a645 commit 910c75c
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 125 deletions.
5 changes: 1 addition & 4 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/node/app"
zetae2econfig "github.com/zeta-chain/node/cmd/zetae2e/config"
"github.com/zeta-chain/node/e2e/config"
Expand Down Expand Up @@ -165,13 +164,11 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
utils.EmergencyPolicyName,
utils.OperationalPolicyName,
utils.AdminPolicyName,
sdk.AccAddress(conf.AdditionalAccounts.UserPrecompile.EVMAddress().Bytes()).String(),
},
[]string{
conf.PolicyAccounts.EmergencyPolicyAccount.RawPrivateKey.String(),
conf.PolicyAccounts.OperationalPolicyAccount.RawPrivateKey.String(),
conf.PolicyAccounts.AdminPolicyAccount.RawPrivateKey.String(),
conf.AdditionalAccounts.UserPrecompile.RawPrivateKey.String(),
},
conf.ZetaChainID,
)
Expand Down Expand Up @@ -238,7 +235,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
}
noError(deployerRunner.FundEmissionsPool())

deployerRunner.MintERC20OnEvm(1000000)
deployerRunner.MintERC20OnEvm(1e10)

logger.Print("✅ setup completed in %s", time.Since(startTime))
}
Expand Down
22 changes: 20 additions & 2 deletions cmd/zetae2e/local/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fatih/color"

"github.com/zeta-chain/node/e2e/config"
"github.com/zeta-chain/node/e2e/e2etests"
"github.com/zeta-chain/node/e2e/runner"
"github.com/zeta-chain/node/e2e/txserver"
)

// statefulPrecompilesTestRoutine runs steateful precompiles related e2e tests
Expand All @@ -27,17 +29,33 @@ func statefulPrecompilesTestRoutine(
deployerRunner,
account,
runner.NewLogger(verbose, color.FgRed, "precompiles"),
runner.WithZetaTxServer(deployerRunner.ZetaTxServer),
//runner.WithZetaTxServer(deployerRunner.ZetaTxServer),
)
if err != nil {
return err
}

zetaTxServer, err := txserver.NewZetaTxServer(
conf.RPCs.ZetaCoreRPC,
[]string{
sdk.AccAddress(conf.AdditionalAccounts.UserPrecompile.EVMAddress().Bytes()).String(),
},
[]string{
conf.AdditionalAccounts.UserPrecompile.RawPrivateKey.String(),
},
conf.ZetaChainID,
)
if err != nil {
return err
}

precompileRunner.ZetaTxServer = zetaTxServer

precompileRunner.Logger.Print("🏃 starting stateful precompiled contracts tests")
startTime := time.Now()

// Send ERC20 that will be depositted into ERC20ZRC20 tokens.
txERC20Send := deployerRunner.SendERC20OnEvm(account.EVMAddress(), 10000)
txERC20Send := deployerRunner.SendERC20OnEvm(account.EVMAddress(), 1e7)
precompileRunner.WaitForTxReceiptOnEvm(txERC20Send)

testsToRun, err := precompileRunner.GetE2ETestsToRunByName(
Expand Down
63 changes: 33 additions & 30 deletions e2e/e2etests/test_precompiles_bank_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0, "No arguments expected")

spender := r.EVMAddress()
bankAddress := bank.ContractAddress
zrc20Address := r.ERC20ZRC20Addr
oneThousand := big.NewInt(1e3)
oneThousandOne := big.NewInt(1001)
fiveHundred := big.NewInt(500)
fiveHundredOne := big.NewInt(501)
var (
spender = r.EVMAddress()
bankAddress = bank.ContractAddress
zrc20Address = r.ERC20ZRC20Addr
oneThousand = big.NewInt(1e3)
oneThousandOne = big.NewInt(1001)
fiveHundred = big.NewInt(500)
fiveHundredOne = big.NewInt(501)
zero = big.NewInt(0)
)

// Get ERC20ZRC20.
txHash := r.DepositERC20WithAmountAndMessage(r.EVMAddress(), oneThousand, []byte{})
Expand Down Expand Up @@ -58,18 +61,18 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
}()

// Check initial balances.
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, zero, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spender))
balanceShouldBe(r, zero, checkZRC20Balance(r, bankAddress))

// Deposit without previous alllowance should fail.
receipt = depositThroughTestBank(r, testBank, zrc20Address, oneThousand)
utils.RequiredTxFailed(r, receipt, "Deposit ERC20ZRC20 without allowance should fail")

// Check balances, should be the same.
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, zero, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spender))
balanceShouldBe(r, zero, checkZRC20Balance(r, bankAddress))

// Allow 500 ZRC20 to bank precompile.
approveAllowance(r, bankAddress, fiveHundred)
Expand All @@ -80,9 +83,9 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
utils.RequiredTxFailed(r, receipt, "Depositting an amount higher than allowed should fail")

// Balances shouldn't change.
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, zero, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spender))
balanceShouldBe(r, zero, checkZRC20Balance(r, bankAddress))

// Allow 1000 ZRC20 to bank precompile.
approveAllowance(r, bankAddress, oneThousand)
Expand All @@ -93,18 +96,18 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
utils.RequiredTxFailed(r, receipt, "Depositting an amount higher than balance should fail")

// Balances shouldn't change.
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, zero, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spender))
balanceShouldBe(r, zero, checkZRC20Balance(r, bankAddress))

// Deposit 500 ERC20ZRC20 tokens to the bank contract, it's within allowance and balance. Should pass.
receipt = depositThroughTestBank(r, testBank, zrc20Address, fiveHundred)
utils.RequireTxSuccessful(r, receipt, "Depositting a correct amount should pass")

// Balances should be transferred. Bank now locks 500 ZRC20 tokens.
balanceShouldBe(r, 500, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, fiveHundred, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, spender))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, bankAddress))

// Check the deposit event.
eventDeposit, err := bankPrecompileCaller.ParseDeposit(*receipt.Logs[0])
Expand All @@ -118,18 +121,18 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
utils.RequiredTxFailed(r, receipt, "Withdrawing an amount higher than balance should fail")

// Balances shouldn't change.
balanceShouldBe(r, 500, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, fiveHundred, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, spender))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, bankAddress))

// Try to withdraw 500 ERC20ZRC20 tokens. Should pass.
receipt = withdrawThroughTestBank(r, testBank, zrc20Address, fiveHundred)
utils.RequireTxSuccessful(r, receipt, "Withdraw correct amount should pass")

// Balances should be reverted to initial state.
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))
balanceShouldBe(r, zero, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spender))
balanceShouldBe(r, zero, checkZRC20Balance(r, bankAddress))

// Check the withdraw event.
eventWithdraw, err := bankPrecompileCaller.ParseWithdraw(*receipt.Logs[0])
Expand All @@ -146,8 +149,8 @@ func approveAllowance(r *runner.E2ERunner, target common.Address, amount *big.In
utils.RequireTxSuccessful(r, receipt, "Approve ERC20ZRC20 allowance tx failed")
}

func balanceShouldBe(r *runner.E2ERunner, expected uint64, balance *big.Int) {
require.Equal(r, expected, balance.Uint64(), "Balance should be %d, got: %d", expected, balance.Uint64())
func balanceShouldBe(r *runner.E2ERunner, expected *big.Int, balance *big.Int) {
require.Equal(r, expected.Uint64(), balance.Uint64(), "Balance should be %d, got: %d", expected, balance.Uint64())
}

func checkZRC20Balance(r *runner.E2ERunner, target common.Address) *big.Int {
Expand Down
Loading

0 comments on commit 910c75c

Please sign in to comment.