Skip to content

Commit

Permalink
add e2e base
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 24, 2024
1 parent 4728c10 commit eae55f2
Show file tree
Hide file tree
Showing 15 changed files with 968 additions and 29 deletions.
64 changes: 64 additions & 0 deletions e2e/contracts/testdistribute/TestDistribute.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "zrc20_distributor",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "zrc20_token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Distributed",
"type": "event"
},
{
"stateMutability": "payable",
"type": "fallback"
},
{
"inputs": [
{
"internalType": "address",
"name": "zrc20",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "distributeThroughContract",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
]
1 change: 1 addition & 0 deletions e2e/contracts/testdistribute/TestDistribute.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60a060405260666000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561005157600080fd5b503373ffffffffffffffffffffffffffffffffffffffff1660808173ffffffffffffffffffffffffffffffffffffffff168152505060805161034d6100a06000396000606c015261034d6000f3fe6080604052600436106100225760003560e01c806350b54e841461002b57610029565b3661002957005b005b34801561003757600080fd5b50610052600480360381019061004d9190610201565b610068565b60405161005f919061025c565b60405180910390f35b60007f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146100c257600080fd5b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663fb93210884846040518363ffffffff1660e01b815260040161011d929190610295565b6020604051808303816000875af115801561013c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061016091906102ea565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101988261016d565b9050919050565b6101a88161018d565b81146101b357600080fd5b50565b6000813590506101c58161019f565b92915050565b6000819050919050565b6101de816101cb565b81146101e957600080fd5b50565b6000813590506101fb816101d5565b92915050565b6000806040838503121561021857610217610168565b5b6000610226858286016101b6565b9250506020610237858286016101ec565b9150509250929050565b60008115159050919050565b61025681610241565b82525050565b6000602082019050610271600083018461024d565b92915050565b6102808161018d565b82525050565b61028f816101cb565b82525050565b60006040820190506102aa6000830185610277565b6102b76020830184610286565b9392505050565b6102c781610241565b81146102d257600080fd5b50565b6000815190506102e4816102be565b92915050565b600060208284031215610300576102ff610168565b5b600061030e848285016102d5565b9150509291505056fea26469706673582212205443ec313ecb8c2e08ca8a30687daed4c3b666f9318ae72ccbe9033479c8b8be64736f6c634300080a0033
420 changes: 420 additions & 0 deletions e2e/contracts/testdistribute/TestDistribute.go

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions e2e/contracts/testdistribute/TestDistribute.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "zrc20_distributor",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "zrc20_token",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Distributed",
"type": "event"
},
{
"stateMutability": "payable",
"type": "fallback"
},
{
"inputs": [
{
"internalType": "address",
"name": "zrc20",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "distributeThroughContract",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
],
"bin": "60a060405260666000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561005157600080fd5b503373ffffffffffffffffffffffffffffffffffffffff1660808173ffffffffffffffffffffffffffffffffffffffff168152505060805161034d6100a06000396000606c015261034d6000f3fe6080604052600436106100225760003560e01c806350b54e841461002b57610029565b3661002957005b005b34801561003757600080fd5b50610052600480360381019061004d9190610201565b610068565b60405161005f919061025c565b60405180910390f35b60007f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146100c257600080fd5b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663fb93210884846040518363ffffffff1660e01b815260040161011d929190610295565b6020604051808303816000875af115801561013c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061016091906102ea565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101988261016d565b9050919050565b6101a88161018d565b81146101b357600080fd5b50565b6000813590506101c58161019f565b92915050565b6000819050919050565b6101de816101cb565b81146101e957600080fd5b50565b6000813590506101fb816101d5565b92915050565b6000806040838503121561021857610217610168565b5b6000610226858286016101b6565b9250506020610237858286016101ec565b9150509250929050565b60008115159050919050565b61025681610241565b82525050565b6000602082019050610271600083018461024d565b92915050565b6102808161018d565b82525050565b61028f816101cb565b82525050565b60006040820190506102aa6000830185610277565b6102b76020830184610286565b9392505050565b6102c781610241565b81146102d257600080fd5b50565b6000815190506102e4816102be565b92915050565b600060208284031215610300576102ff610168565b5b600061030e848285016102d5565b9150509291505056fea26469706673582212205443ec313ecb8c2e08ca8a30687daed4c3b666f9318ae72ccbe9033479c8b8be64736f6c634300080a0033"
}
43 changes: 43 additions & 0 deletions e2e/contracts/testdistribute/TestDistribute.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;

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

// @dev Call IBank contract functions
contract TestDistribute {
event Distributed(
address indexed zrc20_distributor,
address indexed zrc20_token,
uint256 amount
);

IDistribute distr = IDistribute(0x0000000000000000000000000000000000000066);

address immutable owner;

constructor() {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner);
_;
}

function distributeThroughContract(
address zrc20,
uint256 amount
) external onlyOwner returns (bool) {
return distr.distribute(zrc20, amount);
}

fallback() external payable {}

receive() external payable {}
}
8 changes: 8 additions & 0 deletions e2e/contracts/testdistribute/bindings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:generate sh -c "solc TestDistribute.sol --combined-json abi,bin | jq '.contracts.\"TestDistribute.sol:TestDistribute\"' > TestDistribute.json"
//go:generate sh -c "cat TestDistribute.json | jq .abi > TestDistribute.abi"
//go:generate sh -c "cat TestDistribute.json | jq .bin | tr -d '\"' > TestDistribute.bin"
//go:generate sh -c "abigen --abi TestDistribute.abi --bin TestDistribute.bin --pkg testdistribute --type TestDistribute --out TestDistribute.go"

package testdistribute

var _ TestDistribute
42 changes: 34 additions & 8 deletions e2e/contracts/teststaking/TestStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ interface IStaking {
uint256 amount
) external returns (int64 completionTime);

function getAllValidators() external view returns (Validator[] calldata validators);
function getAllValidators()
external
view
returns (Validator[] calldata validators);

function getShares(address staker, string memory validator) external view returns (uint256 shares);
function getShares(
address staker,
string memory validator
) external view returns (uint256 shares);
}

interface WZETA {
function deposit() external payable;

function withdraw(uint256 wad) external;
}

Expand Down Expand Up @@ -94,18 +101,30 @@ contract TestStaking {
wzeta.withdraw(wad);
}

function stake(address staker, string memory validator, uint256 amount) external onlyOwner returns (bool) {
function stake(
address staker,
string memory validator,
uint256 amount
) external onlyOwner returns (bool) {
return staking.stake(staker, validator, amount);
}

function stakeWithStateUpdate(address staker, string memory validator, uint256 amount) external onlyOwner returns (bool) {
function stakeWithStateUpdate(
address staker,
string memory validator,
uint256 amount
) external onlyOwner returns (bool) {
counter = counter + 1;
bool success = staking.stake(staker, validator, amount);
counter = counter + 1;
return success;
}

function stakeAndRevert(address staker, string memory validator, uint256 amount) external onlyOwner returns (bool) {
function stakeAndRevert(
address staker,
string memory validator,
uint256 amount
) external onlyOwner returns (bool) {
counter = counter + 1;
staking.stake(staker, validator, amount);
counter = counter + 1;
Expand All @@ -129,15 +148,22 @@ contract TestStaking {
return staking.moveStake(staker, validatorSrc, validatorDst, amount);
}

function getShares(address staker, string memory validator) external view returns(uint256 shares) {
function getShares(
address staker,
string memory validator
) external view returns (uint256 shares) {
return staking.getShares(staker, validator);
}

function getAllValidators() external view returns (Validator[] memory validators) {
function getAllValidators()
external
view
returns (Validator[] memory validators)
{
return staking.getAllValidators();
}

fallback() external payable {}

receive() external payable {}
}
}
28 changes: 21 additions & 7 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ const (
/*
Stateful precompiled contracts tests
*/
TestPrecompilesPrototypeName = "precompile_contracts_prototype"
TestPrecompilesPrototypeThroughContractName = "precompile_contracts_prototype_through_contract"
TestPrecompilesStakingName = "precompile_contracts_staking"
TestPrecompilesStakingThroughContractName = "precompile_contracts_staking_through_contract"
TestPrecompilesBankName = "precompile_contracts_bank"
TestPrecompilesBankFailName = "precompile_contracts_bank_fail"
TestPrecompilesBankThroughContractName = "precompile_contracts_bank_through_contract"
TestPrecompilesPrototypeName = "precompile_contracts_prototype"
TestPrecompilesPrototypeThroughContractName = "precompile_contracts_prototype_through_contract"
TestPrecompilesStakingName = "precompile_contracts_staking"
TestPrecompilesStakingThroughContractName = "precompile_contracts_staking_through_contract"
TestPrecompilesBankName = "precompile_contracts_bank"
TestPrecompilesBankFailName = "precompile_contracts_bank_fail"
TestPrecompilesBankThroughContractName = "precompile_contracts_bank_through_contract"
TestPrecompilesDistributeName = "precompile_contracts_distribute"
TestPrecompilesDistributeThroughContractName = "precompile_contracts_distribute_through_contract"
)

// AllE2ETests is an ordered list of all e2e tests
Expand Down Expand Up @@ -956,4 +958,16 @@ var AllE2ETests = []runner.E2ETest{
[]runner.ArgDefinition{},
TestPrecompilesBankThroughContract,
),
runner.NewE2ETest(
TestPrecompilesDistributeName,
"test stateful precompiled contracts distribute",
[]runner.ArgDefinition{},
TestPrecompilesDistribute,
),
runner.NewE2ETest(
TestPrecompilesDistributeThroughContractName,
"test stateful precompiled contracts distribute through contract",
[]runner.ArgDefinition{},
TestPrecompilesDistributeThroughContract,
),
}
20 changes: 12 additions & 8 deletions e2e/e2etests/test_precompiles_bank_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
}()

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

Expand All @@ -67,7 +67,7 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
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, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

Expand All @@ -80,7 +80,7 @@ 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

Expand All @@ -93,7 +93,7 @@ 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

Expand All @@ -102,7 +102,7 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
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, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, bankAddress))

Expand All @@ -118,7 +118,7 @@ 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, checkCosmosBalance(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 500, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, spender))
balanceShouldBe(r, 500, checkZRC20Balance(r, bankAddress))

Expand All @@ -127,7 +127,7 @@ func TestPrecompilesBankThroughContract(r *runner.E2ERunner, args []string) {
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, 0, checkCosmosBalanceThroughBank(r, testBank, zrc20Address, spender))
balanceShouldBe(r, 1000, checkZRC20Balance(r, spender))
balanceShouldBe(r, 0, checkZRC20Balance(r, bankAddress))

Expand Down Expand Up @@ -156,7 +156,11 @@ func checkZRC20Balance(r *runner.E2ERunner, target common.Address) *big.Int {
return bankZRC20Balance
}

func checkCosmosBalance(r *runner.E2ERunner, bank *testbank.TestBank, zrc20, target common.Address) *big.Int {
func checkCosmosBalanceThroughBank(
r *runner.E2ERunner,
bank *testbank.TestBank,
zrc20, target common.Address,
) *big.Int {
balance, err := bank.BalanceOf(&bind.CallOpts{Context: r.Ctx, From: r.ZEVMAuth.From}, zrc20, target)
require.NoError(r, err)
return balance
Expand Down
Loading

0 comments on commit eae55f2

Please sign in to comment.