Skip to content

Commit

Permalink
add distribution query client to e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 24, 2024
1 parent eae55f2 commit 5ea0fac
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 169 deletions.
64 changes: 46 additions & 18 deletions e2e/e2etests/test_precompiles_distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -108,40 +109,67 @@ func TestPrecompilesDistribute(r *runner.E2ERunner, args []string) {
balanceShouldBe(r, 500, checkZRC20Balance(r, lockerAddress))
balanceShouldBe(r, 500, checkCosmosBalance(r, feeCollectorAddress, zrc20Denom))

eventDitributed, err := dstrContract.ParseDistributed(*receipt.Logs[0])
require.NoError(r, err)
require.Equal(r, zrc20Address, eventDitributed.Zrc20Token)
require.Equal(r, spenderAddress, eventDitributed.Zrc20Distributor)
require.Equal(r, fiveHundred.Uint64(), eventDitributed.Amount.Uint64())

// After one block the rewards should have been distributed and fee collector should have 0 ZRC20 balance.
r.WaitForBlocks(1)
balanceShouldBe(r, 0, checkCosmosBalance(r, feeCollectorAddress, zrc20Denom))
res, err := r.DistributionClient.ValidatorDistributionInfo(
r.Ctx,
&distributiontypes.QueryValidatorDistributionInfoRequest{
ValidatorAddress: validators[0].OperatorAddress,
},
)
require.NoError(r, err)
fmt.Printf("Validator 0 distribution info: %+v\n", res)

accAddress := types.AccAddress("zetavaloper16vuh496n7wahw6m8dmzc0p5meymmctleuaz562")
valBal := checkCosmosBalance(r, accAddress, zrc20Denom)
fmt.Println(valBal)

accAddress = types.AccAddress(validators[0].OperatorAddress)
valBal = checkCosmosBalance(r, accAddress, zrc20Denom)
fmt.Println(valBal)
res2, err := r.DistributionClient.ValidatorOutstandingRewards(r.Ctx, &distributiontypes.QueryValidatorOutstandingRewardsRequest{
ValidatorAddress: validators[0].OperatorAddress,
})
require.NoError(r, err)
fmt.Printf("Validator 0 outstanding rewards: %+v\n", res2)

coins := checkAllCosmosBalance(r, accAddress)
fmt.Printf("Coins: %+v\n", coins)
}
res3, err := r.DistributionClient.ValidatorCommission(r.Ctx, &distributiontypes.QueryValidatorCommissionRequest{
ValidatorAddress: validators[0].OperatorAddress,
})
require.NoError(r, err)
fmt.Printf("Validator 0 commission: %+v\n", res3)

func checkCosmosBalance(r *runner.E2ERunner, address types.AccAddress, denom string) *big.Int {
bal, err := r.BankClient.Balance(
// Validator 1
res, err = r.DistributionClient.ValidatorDistributionInfo(
r.Ctx,
&banktypes.QueryBalanceRequest{Address: address.String(), Denom: denom},
&distributiontypes.QueryValidatorDistributionInfoRequest{
ValidatorAddress: validators[1].OperatorAddress,
},
)
require.NoError(r, err)
fmt.Printf("Validator 1 distribution info: %+v\n", res)

return bal.Balance.Amount.BigInt()
res2, err = r.DistributionClient.ValidatorOutstandingRewards(r.Ctx, &distributiontypes.QueryValidatorOutstandingRewardsRequest{
ValidatorAddress: validators[1].OperatorAddress,
})
require.NoError(r, err)
fmt.Printf("Validator 1 outstanding rewards: %+v\n", res2)

res3, err = r.DistributionClient.ValidatorCommission(r.Ctx, &distributiontypes.QueryValidatorCommissionRequest{
ValidatorAddress: validators[1].OperatorAddress,
})
require.NoError(r, err)
fmt.Printf("Validator 1 commission: %+v\n", res3)
}

func checkAllCosmosBalance(r *runner.E2ERunner, address types.AccAddress) types.Coins {
bal, err := r.BankClient.AllBalances(
func checkCosmosBalance(r *runner.E2ERunner, address types.AccAddress, denom string) *big.Int {
bal, err := r.BankClient.Balance(
r.Ctx,
&banktypes.QueryAllBalancesRequest{Address: address.String()},
&banktypes.QueryBalanceRequest{Address: address.String(), Denom: denom},
)
require.NoError(r, err)

return bal.Balances
return bal.Balance.Amount.BigInt()
}

func resetTest(r *runner.E2ERunner, lockerAddress common.Address, previousGasLimit uint64) {
Expand Down
121 changes: 0 additions & 121 deletions e2e/e2etests/test_precompiles_distribute_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,125 +8,4 @@ import (

func TestPrecompilesDistributeThroughContract(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)

// // Get ERC20ZRC20.
// txHash := r.DepositERC20WithAmountAndMessage(r.EVMAddress(), oneThousand, []byte{})
// utils.WaitCctxMinedByInboundHash(r.Ctx, txHash.Hex(), r.CctxClient, r.Logger, r.CctxTimeout)

// bankPrecompileCaller, err := bank.NewIBank(bank.ContractAddress, r.ZEVMClient)
// require.NoError(r, err, "Failed to create bank precompile caller")

// // Deploy the TestBank. Ensure the transaction is successful.
// _, tx, testBank, err := testbank.DeployTestBank(r.ZEVMAuth, r.ZEVMClient)
// require.NoError(r, err)
// receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
// utils.RequireTxSuccessful(r, receipt, "Deployment of TestBank contract failed")

// previousGasLimit := r.ZEVMAuth.GasLimit
// r.ZEVMAuth.GasLimit = 10_000_000
// defer func() {
// r.ZEVMAuth.GasLimit = previousGasLimit

// // Reset the allowance to 0; this is needed when running upgrade tests where this test runs twice.
// approveAllowance(r, bank.ContractAddress, big.NewInt(0))

// // Reset balance to 0; this is needed when running upgrade tests where this test runs twice.
// tx, err = r.ERC20ZRC20.Transfer(
// r.ZEVMAuth,
// common.HexToAddress("0x000000000000000000000000000000000000dEaD"),
// oneThousand,
// )
// require.NoError(r, err)
// receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
// utils.RequireTxSuccessful(r, receipt, "Resetting balance failed")
// }()

// // Check initial balances.
// balanceShouldBe(r, 0, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 0, 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

// // Allow 500 ZRC20 to bank precompile.
// approveAllowance(r, bankAddress, fiveHundred)

// // Deposit 501 ERC20ZRC20 tokens to the bank contract, through TestBank.
// // It's higher than allowance but lower than balance, should fail.
// receipt = depositThroughTestBank(r, testBank, zrc20Address, fiveHundredOne)
// utils.RequiredTxFailed(r, receipt, "Depositting an amount higher than allowed should fail")

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

// // Allow 1000 ZRC20 to bank precompile.
// approveAllowance(r, bankAddress, oneThousand)

// // Deposit 1001 ERC20ZRC20 tokens to the bank contract.
// // It's higher than spender balance but within approved allowance, should fail.
// receipt = depositThroughTestBank(r, testBank, zrc20Address, oneThousandOne)
// utils.RequiredTxFailed(r, receipt, "Depositting an amount higher than balance should fail")

// // Balances shouldn't change.
// balanceShouldBe(r, 0, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 0, 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 500, checkZRC20Balance(r, bankAddress))

// // Check the deposit event.
// eventDeposit, err := bankPrecompileCaller.ParseDeposit(*receipt.Logs[0])
// require.NoError(r, err, "Parse Deposit event")
// require.Equal(r, r.EVMAddress(), eventDeposit.Zrc20Depositor, "Deposit event token should be r.EVMAddress()")
// require.Equal(r, r.ERC20ZRC20Addr, eventDeposit.Zrc20Token, "Deposit event token should be ERC20ZRC20Addr")
// require.Equal(r, fiveHundred, eventDeposit.Amount, "Deposit event amount should be 500")

// // Should faild to withdraw more than cosmos balance.
// receipt = withdrawThroughTestBank(r, testBank, zrc20Address, fiveHundredOne)
// utils.RequiredTxFailed(r, receipt, "Withdrawing an amount higher than balance should fail")

// // Balances shouldn't change.
// balanceShouldBe(r, 500, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 500, 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
// balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
// balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

// // Check the withdraw event.
// eventWithdraw, err := bankPrecompileCaller.ParseWithdraw(*receipt.Logs[0])
// require.NoError(r, err, "Parse Withdraw event")
// require.Equal(r, r.EVMAddress(), eventWithdraw.Zrc20Withdrawer, "Withdrawer should be r.EVMAddress()")
// require.Equal(r, r.ERC20ZRC20Addr, eventWithdraw.Zrc20Token, "Withdraw event token should be ERC20ZRC20Addr")
// require.Equal(r, fiveHundred, eventWithdraw.Amount, "Withdraw event amount should be 500")
}
39 changes: 21 additions & 18 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcd/rpcclient"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -86,14 +87,15 @@ type E2ERunner struct {
SolanaClient *rpc.Client

// zetacored grpc clients
AuthorityClient authoritytypes.QueryClient
CctxClient crosschaintypes.QueryClient
FungibleClient fungibletypes.QueryClient
AuthClient authtypes.QueryClient
BankClient banktypes.QueryClient
StakingClient stakingtypes.QueryClient
ObserverClient observertypes.QueryClient
LightclientClient lightclienttypes.QueryClient
AuthorityClient authoritytypes.QueryClient
CctxClient crosschaintypes.QueryClient
FungibleClient fungibletypes.QueryClient
AuthClient authtypes.QueryClient
BankClient banktypes.QueryClient
StakingClient stakingtypes.QueryClient
ObserverClient observertypes.QueryClient
LightclientClient lightclienttypes.QueryClient
DistributionClient distributiontypes.QueryClient

// optional zeta (cosmos) client
// typically only in test runners that need it
Expand Down Expand Up @@ -189,16 +191,17 @@ func NewE2ERunner(

Clients: clients,

ZEVMClient: clients.Zevm,
EVMClient: clients.Evm,
AuthorityClient: clients.Zetacore.Authority,
CctxClient: clients.Zetacore.Crosschain,
FungibleClient: clients.Zetacore.Fungible,
AuthClient: clients.Zetacore.Auth,
BankClient: clients.Zetacore.Bank,
StakingClient: clients.Zetacore.Staking,
ObserverClient: clients.Zetacore.Observer,
LightclientClient: clients.Zetacore.Lightclient,
ZEVMClient: clients.Zevm,
EVMClient: clients.Evm,
AuthorityClient: clients.Zetacore.Authority,
CctxClient: clients.Zetacore.Crosschain,
FungibleClient: clients.Zetacore.Fungible,
AuthClient: clients.Zetacore.Auth,
BankClient: clients.Zetacore.Bank,
StakingClient: clients.Zetacore.Staking,
ObserverClient: clients.Zetacore.Observer,
LightclientClient: clients.Zetacore.Lightclient,
DistributionClient: clients.Zetacore.Distribution,

EVMAuth: clients.EvmAuth,
ZEVMAuth: clients.ZevmAuth,
Expand Down
14 changes: 9 additions & 5 deletions pkg/rpc/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"
Expand Down Expand Up @@ -35,6 +36,8 @@ type Clients struct {
Staking stakingtypes.QueryClient
// Upgrade is a github.com/cosmos/cosmos-sdk/x/upgrade/types QueryClient
Upgrade upgradetypes.QueryClient
// Distribution is a "github.com/cosmos/cosmos-sdk/x/distribution/types" QueryClient
Distribution distributiontypes.QueryClient

// ZetaCore specific clients

Expand Down Expand Up @@ -65,11 +68,12 @@ type Clients struct {
func newClients(ctx client.Context) (Clients, error) {
return Clients{
// Cosmos SDK clients
Auth: authtypes.NewQueryClient(ctx),
Bank: banktypes.NewQueryClient(ctx),
Staking: stakingtypes.NewQueryClient(ctx),
Upgrade: upgradetypes.NewQueryClient(ctx),
Authority: authoritytypes.NewQueryClient(ctx),
Auth: authtypes.NewQueryClient(ctx),
Bank: banktypes.NewQueryClient(ctx),
Staking: stakingtypes.NewQueryClient(ctx),
Upgrade: upgradetypes.NewQueryClient(ctx),
Authority: authoritytypes.NewQueryClient(ctx),
Distribution: distributiontypes.NewQueryClient(ctx),
// ZetaCore specific clients
Crosschain: crosschaintypes.NewQueryClient(ctx),
Fungible: fungibletypes.NewQueryClient(ctx),
Expand Down
4 changes: 4 additions & 0 deletions precompiles/staking/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package staking

const (
DistributeMethodName = "distribute"
DisitributeEventName = "Distributed"
DistributeMethodGas = 10000

GetAllValidatorsMethodName = "getAllValidators"
GetSharesMethodName = "getShares"

MoveStakeMethodName = "moveStake"
MoveStakeEventName = "MoveStake"
MoveStakeMethodGas = 10000

StakeMethodName = "stake"
StakeEventName = "Stake"
StakeMethodGas = 10000

UnstakeMethodName = "unstake"
UnstakeEventName = "Unstake"
UnstakeMethodGas = 1000
)
8 changes: 1 addition & 7 deletions precompiles/staking/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/zeta-chain/node/precompiles/logs"
)

const (
StakeEventName = "Stake"
UnstakeEventName = "Unstake"
MoveStakeEventName = "MoveStake"
)

func (c *Contract) addStakeLog(
ctx sdk.Context,
stateDB vm.StateDB,
Expand Down Expand Up @@ -131,7 +125,7 @@ func (c *Contract) addDistributeLog(
zrc20Token common.Address,
amount *big.Int,
) error {
event := c.Abi().Events[MoveStakeEventName]
event := c.Abi().Events[DisitributeEventName]

topics, err := logs.MakeTopics(
event,
Expand Down

0 comments on commit 5ea0fac

Please sign in to comment.