Skip to content

Commit

Permalink
e2e: distribute through contract
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Nov 15, 2024
1 parent af1360f commit 3502cea
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
26 changes: 13 additions & 13 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
light = must(cmd.Flags().GetBool(flagLight))
setupOnly = must(cmd.Flags().GetBool(flagSetupOnly))
skipSetup = must(cmd.Flags().GetBool(flagSkipSetup))
skipBitcoinSetup = must(cmd.Flags().GetBool(flagSkipBitcoinSetup))
skipHeaderProof = must(cmd.Flags().GetBool(flagSkipHeaderProof))
skipTrackerCheck = must(cmd.Flags().GetBool(flagSkipTrackerCheck))
testTSSMigration = must(cmd.Flags().GetBool(flagTestTSSMigration))
testV2 = must(cmd.Flags().GetBool(flagTestV2))
testV2Migration = must(cmd.Flags().GetBool(flagTestV2Migration))
skipPrecompiles = must(cmd.Flags().GetBool(flagSkipPrecompiles))
upgradeContracts = must(cmd.Flags().GetBool(flagUpgradeContracts))
//skipBitcoinSetup = must(cmd.Flags().GetBool(flagSkipBitcoinSetup))
skipHeaderProof = must(cmd.Flags().GetBool(flagSkipHeaderProof))
skipTrackerCheck = must(cmd.Flags().GetBool(flagSkipTrackerCheck))
testTSSMigration = must(cmd.Flags().GetBool(flagTestTSSMigration))
testV2 = must(cmd.Flags().GetBool(flagTestV2))
testV2Migration = must(cmd.Flags().GetBool(flagTestV2Migration))
skipPrecompiles = must(cmd.Flags().GetBool(flagSkipPrecompiles))
upgradeContracts = must(cmd.Flags().GetBool(flagUpgradeContracts))
)

logger := runner.NewLogger(verbose, color.FgWhite, "setup")
Expand Down Expand Up @@ -359,11 +359,11 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
}

eg.Go(statefulPrecompilesTestRoutine(conf, deployerRunner, verbose, precompiledContractTests...))
eg.Go(erc20TestRoutine(conf, deployerRunner, verbose, erc20Tests...))
eg.Go(zetaTestRoutine(conf, deployerRunner, verbose, zetaTests...))
eg.Go(zevmMPTestRoutine(conf, deployerRunner, verbose, zevmMPTests...))
eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, bitcoinTests...))
eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, ethereumTests...))
// eg.Go(erc20TestRoutine(conf, deployerRunner, verbose, erc20Tests...))
// eg.Go(zetaTestRoutine(conf, deployerRunner, verbose, zetaTests...))
// eg.Go(zevmMPTestRoutine(conf, deployerRunner, verbose, zevmMPTests...))
// eg.Go(bitcoinTestRoutine(conf, deployerRunner, verbose, !skipBitcoinSetup, bitcoinTests...))
// eg.Go(ethereumTestRoutine(conf, deployerRunner, verbose, ethereumTests...))
}

if testAdmin {
Expand Down
1 change: 0 additions & 1 deletion cmd/zetae2e/local/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func statefulPrecompilesTestRoutine(
deployerRunner,
account,
runner.NewLogger(verbose, color.FgRed, "precompiles"),
//runner.WithZetaTxServer(deployerRunner.ZetaTxServer),
)
if err != nil {
return err
Expand Down
19 changes: 19 additions & 0 deletions e2e/contracts/testdistribute/TestDistribute.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;

struct DecCoin {
string denom;
uint256 amount;
}

// @dev Interface to interact with distribute.
interface IDistribute {
function distribute(
address zrc20,
uint256 amount
) external returns (bool success);

function claimRewards(
address delegator,
string memory validator
) external returns (bool success);

function getDelegatorValidators(
address delegator
) external view returns (string[] calldata validators);

function getRewards(
address delegator,
string memory validator
) external view returns (DecCoin[] calldata rewards);
}

// @dev Call IBank contract functions
Expand Down
4 changes: 2 additions & 2 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ var AllE2ETests = []runner.E2ETest{
TestPrecompilesDistributeName,
"test stateful precompiled contracts distribute",
[]runner.ArgDefinition{},
TestPrecompilesDistribute,
TestPrecompilesDistributeAndClaim,
),
runner.NewE2ETest(
TestPrecompilesDistributeNonZRC20Name,
Expand All @@ -1069,6 +1069,6 @@ var AllE2ETests = []runner.E2ETest{
TestPrecompilesDistributeThroughContractName,
"test stateful precompiled contracts distribute through contract",
[]runner.ArgDefinition{},
TestPrecompilesDistributeThroughContract,
TestPrecompilesDistributeAndClaimThroughContract,
),
}
8 changes: 7 additions & 1 deletion e2e/e2etests/test_precompiles_distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
precompiletypes "github.com/zeta-chain/node/precompiles/types"
)

func TestPrecompilesDistribute(r *runner.E2ERunner, args []string) {
func TestPrecompilesDistributeAndClaim(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0, "No arguments expected")

var (
Expand Down Expand Up @@ -185,6 +185,12 @@ func TestPrecompilesDistribute(r *runner.E2ERunner, args []string) {
require.True(r, ok)
balanceShouldBe(r, zrc20RewardsAmt, checkZRC20Balance(r, staker))

eventClaimed, err := distrContract.ParseClaimedRewards(*receipt.Logs[0])
require.NoError(r, err)
require.Equal(r, zrc20Address, eventClaimed.Zrc20Token)
require.Equal(r, staker, eventClaimed.ClaimAddress)
require.Equal(r, zrc20RewardsAmt.Uint64(), eventClaimed.Amount.Uint64())

// Locker final balance should be zrc20Disitributed - zrc20RewardsAmt.
lockerFinalBalance := big.NewInt(0).Sub(zrc20DistrAmt, zrc20RewardsAmt)
balanceShouldBe(r, lockerFinalBalance, checkZRC20Balance(r, lockerAddress))
Expand Down
25 changes: 14 additions & 11 deletions e2e/e2etests/test_precompiles_distribute_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
precompiletypes "github.com/zeta-chain/node/precompiles/types"
)

func TestPrecompilesDistributeThroughContract(r *runner.E2ERunner, args []string) {
func TestPrecompilesDistributeAndClaimThroughContract(r *runner.E2ERunner, args []string) {
require.Len(r, args, 0, "No arguments expected")

var (
Expand All @@ -26,11 +26,14 @@ func TestPrecompilesDistributeThroughContract(r *runner.E2ERunner, args []string
zrc20Address = r.ERC20ZRC20Addr
zrc20Denom = precompiletypes.ZRC20ToCosmosDenom(zrc20Address)

oneThousand = big.NewInt(1e3)
oneThousandOne = big.NewInt(1001)
fiveHundred = big.NewInt(500)
fiveHundredOne = big.NewInt(501)
zero = big.NewInt(0)
// carry is carried from the TestPrecompilesDistributeName test. It's applicable only to locker address.
carry = big.NewInt(6210810988040846448)
oneThousand = big.NewInt(1e3)
oneThousandOne = big.NewInt(1001)
fiveHundred = big.NewInt(500)
fiveHundredCarry = new(big.Int).Add(big.NewInt(500), carry)
fiveHundredOne = big.NewInt(501)
zero = big.NewInt(0)

//previousGasLimit = r.ZEVMAuth.GasLimit
)
Expand All @@ -55,15 +58,15 @@ func TestPrecompilesDistributeThroughContract(r *runner.E2ERunner, args []string

// Check initial balances.
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spenderAddress))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, carry, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, zero, checkCosmosBalance(r, r.FeeCollectorAddress, zrc20Denom))

receipt = distributeThroughContract(r, testDstrContract, zrc20Address, oneThousand)
utils.RequiredTxFailed(r, receipt, "distribute should fail when there's no allowance")

// Balances shouldn't change after a failed attempt.
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spenderAddress))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, carry, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, zero, checkCosmosBalance(r, r.FeeCollectorAddress, zrc20Denom))

// Allow 500.
Expand All @@ -74,7 +77,7 @@ func TestPrecompilesDistributeThroughContract(r *runner.E2ERunner, args []string

// Balances shouldn't change after a failed attempt.
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spenderAddress))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, carry, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, zero, checkCosmosBalance(r, r.FeeCollectorAddress, zrc20Denom))

// Raise the allowance to 1000.
Expand All @@ -86,15 +89,15 @@ func TestPrecompilesDistributeThroughContract(r *runner.E2ERunner, args []string

// Balances shouldn't change after a failed attempt.
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, spenderAddress))
balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, carry, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, zero, checkCosmosBalance(r, r.FeeCollectorAddress, zrc20Denom))

// Should be able to distribute 500, which is within balance and allowance.
receipt = distributeThroughContract(r, testDstrContract, zrc20Address, fiveHundred)
utils.RequireTxSuccessful(r, receipt, "distribute should succeed when distributing within balance and allowance")

balanceShouldBe(r, fiveHundred, checkZRC20Balance(r, spenderAddress))
balanceShouldBe(r, oneThousand, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, fiveHundredCarry, checkZRC20Balance(r, lockerAddress)) // Carries 500 from distribute e2e.
balanceShouldBe(r, fiveHundred, checkCosmosBalance(r, r.FeeCollectorAddress, zrc20Denom))

eventDitributed, err := dstrContract.ParseDistributed(*receipt.Logs[0])
Expand Down

0 comments on commit 3502cea

Please sign in to comment.