Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: distribute ZRC20 rewards function #3019

Merged
merged 23 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [2987](https://github.com/zeta-chain/node/pull/2987) - add non-EVM standard inbound memo package
* [2979](https://github.com/zeta-chain/node/pull/2979) - add fungible keeper ability to lock/unlock ZRC20 tokens
* [3012](https://github.com/zeta-chain/node/pull/3012) - integrate authenticated calls erc20 smart contract functionality into protocol
* [3019](https://github.com/zeta-chain/node/pull/3019) - add ditribute functions to staking precompile
fbac marked this conversation as resolved.
Show resolved Hide resolved

### Refactor

Expand Down
2 changes: 2 additions & 0 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
e2etests.TestPrecompilesBankName,
e2etests.TestPrecompilesBankFailName,
e2etests.TestPrecompilesBankThroughContractName,
e2etests.TestPrecompilesDistributeName,
e2etests.TestPrecompilesDistributeThroughContractName,
}
}

Expand Down
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"
},
fbac marked this conversation as resolved.
Show resolved Hide resolved
{
"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"
},
fbac marked this conversation as resolved.
Show resolved Hide resolved
{
"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"
}
fbac marked this conversation as resolved.
Show resolved Hide resolved
],
"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
);
fbac marked this conversation as resolved.
Show resolved Hide resolved

IDistribute distr = IDistribute(0x0000000000000000000000000000000000000066);

address immutable owner;

constructor() {
owner = msg.sender;
}
fbac marked this conversation as resolved.
Show resolved Hide resolved

modifier onlyOwner() {
require(msg.sender == owner);
_;
}
fbac marked this conversation as resolved.
Show resolved Hide resolved

function distributeThroughContract(
address zrc20,
uint256 amount
) external onlyOwner returns (bool) {
return distr.distribute(zrc20, amount);
}
fbac marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -167,13 +167,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 @@ -997,4 +999,16 @@ var AllE2ETests = []runner.E2ETest{
[]runner.ArgDefinition{},
TestPrecompilesBankThroughContract,
),
runner.NewE2ETest(
TestPrecompilesDistributeName,
"test stateful precompiled contracts distribute",
fbac marked this conversation as resolved.
Show resolved Hide resolved
[]runner.ArgDefinition{},
TestPrecompilesDistribute,
),
runner.NewE2ETest(
TestPrecompilesDistributeThroughContractName,
"test stateful precompiled contracts distribute through contract",
[]runner.ArgDefinition{},
TestPrecompilesDistributeThroughContract,
),
fbac marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading