Skip to content

Commit

Permalink
feat: add function to withdraw and check delegator rewards (#3088)
Browse files Browse the repository at this point in the history
* feat: add function to withdraw delegator rewards

* add changelog

* add minor fixes

* add unit test

* add unit tests

* test: zetaTxServer modifications

* e2e: finish distribute test

* e2e: distribute through contract

* modify staking type

* restore local.go

* drop the else

* test: finish e2e through contract

* add more checks to zrc20 is valid

* add reviewed changes

* do not check for delegator in tx.origin or contract.CallerAddress

* update changelog

* fixes based on PR review

* fix test

* add big validator set test

* fix formatting
  • Loading branch information
fbac authored Nov 28, 2024
1 parent 08ff881 commit 212e269
Show file tree
Hide file tree
Showing 44 changed files with 2,465 additions and 716 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func New(
&app.FungibleKeeper,
app.StakingKeeper,
app.BankKeeper,
app.DistrKeeper,
appCodec,
storetypes.TransientGasConfig(),
),
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [3091](https://github.com/zeta-chain/node/pull/3091) - improve build reproducability. `make release{,-build-only}` checksums should now be stable.
* [3124](https://github.com/zeta-chain/node/pull/3124) - integrate SPL deposits
* [3134](https://github.com/zeta-chain/node/pull/3134) - integrate SPL tokens withdraw to Solana
* [3088](https://github.com/zeta-chain/node/pull/3088) - add functions to check and withdraw zrc20 as delegation rewards
* [3182](https://github.com/zeta-chain/node/pull/3182) - enable zetaclient pprof server on port 6061

### Tests
Expand Down
18 changes: 9 additions & 9 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
deployerRunner.UpdateChainParamsV2Contracts()
deployerRunner.ERC20CustodyAddr = deployerRunner.ERC20CustodyV2Addr

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

logger.Print("✅ setup completed in %s", time.Since(startTime))
}
Expand Down Expand Up @@ -366,14 +366,14 @@ func localE2ETest(cmd *cobra.Command, _ []string) {

if !skipPrecompiles {
precompiledContractTests = []string{
// e2etests.TestPrecompilesPrototypeName,
// e2etests.TestPrecompilesPrototypeThroughContractName,
// e2etests.TestPrecompilesStakingName,
// // Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
// // e2etests.TestPrecompilesStakingThroughContractName,
// e2etests.TestPrecompilesBankName,
// e2etests.TestPrecompilesBankFailName,
// e2etests.TestPrecompilesBankThroughContractName,
e2etests.TestPrecompilesPrototypeName,
e2etests.TestPrecompilesPrototypeThroughContractName,
e2etests.TestPrecompilesStakingName,
// Disabled until further notice, check https://github.com/zeta-chain/node/issues/3005.
// e2etests.TestPrecompilesStakingThroughContractName,
e2etests.TestPrecompilesBankName,
e2etests.TestPrecompilesBankFailName,
e2etests.TestPrecompilesBankThroughContractName,
e2etests.TestPrecompilesDistributeName,
e2etests.TestPrecompilesDistributeNonZRC20Name,
e2etests.TestPrecompilesDistributeThroughContractName,
Expand Down
22 changes: 21 additions & 1 deletion 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 @@ -32,11 +34,29 @@ func statefulPrecompilesTestRoutine(
return err
}

// Initialize a ZetaTxServer with the precompile user account.
// It's needed to send messages on behalf of the precompile user.
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
91 changes: 70 additions & 21 deletions e2e/contracts/testdistribute/TestDistribute.abi
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "zrc20_distributor",
"name": "delegator",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "zrc20_token",
"type": "address"
},
"internalType": "string",
"name": "validator",
"type": "string"
}
],
"name": "claimRewardsThroughContract",
"outputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"name": "Distributed",
"type": "event"
},
{
"stateMutability": "payable",
"type": "fallback"
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
Expand All @@ -57,6 +51,61 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "delegator",
"type": "address"
}
],
"name": "getDelegatorValidatorsThroughContract",
"outputs": [
{
"internalType": "string[]",
"name": "",
"type": "string[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"internalType": "string",
"name": "validator",
"type": "string"
}
],
"name": "getRewardsThroughContract",
"outputs": [
{
"components": [
{
"internalType": "string",
"name": "denom",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct DecCoin[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
Expand Down
2 changes: 1 addition & 1 deletion e2e/contracts/testdistribute/TestDistribute.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60a060405260666000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561005157600080fd5b503373ffffffffffffffffffffffffffffffffffffffff1660808173ffffffffffffffffffffffffffffffffffffffff168152505060805161034d6100a06000396000606c015261034d6000f3fe6080604052600436106100225760003560e01c806350b54e841461002b57610029565b3661002957005b005b34801561003757600080fd5b50610052600480360381019061004d9190610201565b610068565b60405161005f919061025c565b60405180910390f35b60007f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146100c257600080fd5b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663fb93210884846040518363ffffffff1660e01b815260040161011d929190610295565b6020604051808303816000875af115801561013c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061016091906102ea565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006101988261016d565b9050919050565b6101a88161018d565b81146101b357600080fd5b50565b6000813590506101c58161019f565b92915050565b6000819050919050565b6101de816101cb565b81146101e957600080fd5b50565b6000813590506101fb816101d5565b92915050565b6000806040838503121561021857610217610168565b5b6000610226858286016101b6565b9250506020610237858286016101ec565b9150509250929050565b60008115159050919050565b61025681610241565b82525050565b6000602082019050610271600083018461024d565b92915050565b6102808161018d565b82525050565b61028f816101cb565b82525050565b60006040820190506102aa6000830185610277565b6102b76020830184610286565b9392505050565b6102c781610241565b81146102d257600080fd5b50565b6000815190506102e4816102be565b92915050565b600060208284031215610300576102ff610168565b5b600061030e848285016102d5565b9150509291505056fea26469706673582212205443ec313ecb8c2e08ca8a30687daed4c3b666f9318ae72ccbe9033479c8b8be64736f6c634300080a0033
608060405260666000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561005157600080fd5b50610e2d806100616000396000f3fe6080604052600436106100435760003560e01c80630f4865ea1461004c57806350b54e8414610089578063834b902f146100c6578063cdc5ec4a146101035761004a565b3661004a57005b005b34801561005857600080fd5b50610073600480360381019061006e919061059d565b610140565b6040516100809190610614565b60405180910390f35b34801561009557600080fd5b506100b060048036038101906100ab9190610665565b6101e9565b6040516100bd9190610614565b60405180910390f35b3480156100d257600080fd5b506100ed60048036038101906100e8919061059d565b610292565b6040516100fa919061083b565b60405180910390f35b34801561010f57600080fd5b5061012a6004803603810190610125919061085d565b61033d565b604051610137919061094c565b60405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166354dbdc3884846040518363ffffffff1660e01b815260040161019e9291906109c7565b6020604051808303816000875af11580156101bd573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101e19190610a23565b905092915050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663fb93210884846040518363ffffffff1660e01b8152600401610247929190610a5f565b6020604051808303816000875af1158015610266573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061028a9190610a23565b905092915050565b606060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16639342879284846040518363ffffffff1660e01b81526004016102ef9291906109c7565b600060405180830381865afa15801561030c573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906103359190610c69565b905092915050565b606060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663b6a216ae836040518263ffffffff1660e01b81526004016103989190610cb2565b600060405180830381865afa1580156103b5573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906103de9190610dae565b9050919050565b6000604051905090565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610424826103f9565b9050919050565b61043481610419565b811461043f57600080fd5b50565b6000813590506104518161042b565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6104aa82610461565b810181811067ffffffffffffffff821117156104c9576104c8610472565b5b80604052505050565b60006104dc6103e5565b90506104e882826104a1565b919050565b600067ffffffffffffffff82111561050857610507610472565b5b61051182610461565b9050602081019050919050565b82818337600083830152505050565b600061054061053b846104ed565b6104d2565b90508281526020810184848401111561055c5761055b61045c565b5b61056784828561051e565b509392505050565b600082601f83011261058457610583610457565b5b813561059484826020860161052d565b91505092915050565b600080604083850312156105b4576105b36103ef565b5b60006105c285828601610442565b925050602083013567ffffffffffffffff8111156105e3576105e26103f4565b5b6105ef8582860161056f565b9150509250929050565b60008115159050919050565b61060e816105f9565b82525050565b60006020820190506106296000830184610605565b92915050565b6000819050919050565b6106428161062f565b811461064d57600080fd5b50565b60008135905061065f81610639565b92915050565b6000806040838503121561067c5761067b6103ef565b5b600061068a85828601610442565b925050602061069b85828601610650565b9150509250929050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561070b5780820151818401526020810190506106f0565b8381111561071a576000848401525b50505050565b600061072b826106d1565b61073581856106dc565b93506107458185602086016106ed565b61074e81610461565b840191505092915050565b6107628161062f565b82525050565b600060408301600083015184820360008601526107858282610720565b915050602083015161079a6020860182610759565b508091505092915050565b60006107b18383610768565b905092915050565b6000602082019050919050565b60006107d1826106a5565b6107db81856106b0565b9350836020820285016107ed856106c1565b8060005b85811015610829578484038952815161080a85826107a5565b9450610815836107b9565b925060208a019950506001810190506107f1565b50829750879550505050505092915050565b6000602082019050818103600083015261085581846107c6565b905092915050565b600060208284031215610873576108726103ef565b5b600061088184828501610442565b91505092915050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b60006108c28383610720565b905092915050565b6000602082019050919050565b60006108e28261088a565b6108ec8185610895565b9350836020820285016108fe856108a6565b8060005b8581101561093a578484038952815161091b85826108b6565b9450610926836108ca565b925060208a01995050600181019050610902565b50829750879550505050505092915050565b6000602082019050818103600083015261096681846108d7565b905092915050565b61097781610419565b82525050565b600082825260208201905092915050565b6000610999826106d1565b6109a3818561097d565b93506109b38185602086016106ed565b6109bc81610461565b840191505092915050565b60006040820190506109dc600083018561096e565b81810360208301526109ee818461098e565b90509392505050565b610a00816105f9565b8114610a0b57600080fd5b50565b600081519050610a1d816109f7565b92915050565b600060208284031215610a3957610a386103ef565b5b6000610a4784828501610a0e565b91505092915050565b610a598161062f565b82525050565b6000604082019050610a74600083018561096e565b610a816020830184610a50565b9392505050565b600067ffffffffffffffff821115610aa357610aa2610472565b5b602082029050602081019050919050565b600080fd5b600080fd5b600080fd5b6000610ad6610ad1846104ed565b6104d2565b905082815260208101848484011115610af257610af161045c565b5b610afd8482856106ed565b509392505050565b600082601f830112610b1a57610b19610457565b5b8151610b2a848260208601610ac3565b91505092915050565b600081519050610b4281610639565b92915050565b600060408284031215610b5e57610b5d610ab9565b5b610b6860406104d2565b9050600082015167ffffffffffffffff811115610b8857610b87610abe565b5b610b9484828501610b05565b6000830152506020610ba884828501610b33565b60208301525092915050565b6000610bc7610bc284610a88565b6104d2565b90508083825260208201905060208402830185811115610bea57610be9610ab4565b5b835b81811015610c3157805167ffffffffffffffff811115610c0f57610c0e610457565b5b808601610c1c8982610b48565b85526020850194505050602081019050610bec565b5050509392505050565b600082601f830112610c5057610c4f610457565b5b8151610c60848260208601610bb4565b91505092915050565b600060208284031215610c7f57610c7e6103ef565b5b600082015167ffffffffffffffff811115610c9d57610c9c6103f4565b5b610ca984828501610c3b565b91505092915050565b6000602082019050610cc7600083018461096e565b92915050565b600067ffffffffffffffff821115610ce857610ce7610472565b5b602082029050602081019050919050565b6000610d0c610d0784610ccd565b6104d2565b90508083825260208201905060208402830185811115610d2f57610d2e610ab4565b5b835b81811015610d7657805167ffffffffffffffff811115610d5457610d53610457565b5b808601610d618982610b05565b85526020850194505050602081019050610d31565b5050509392505050565b600082601f830112610d9557610d94610457565b5b8151610da5848260208601610cf9565b91505092915050565b600060208284031215610dc457610dc36103ef565b5b600082015167ffffffffffffffff811115610de257610de16103f4565b5b610dee84828501610d80565b9150509291505056fea2646970667358221220d29e8c0ffd7f95c3ae2950ad56c9ec844a4f83f78ebf290ed1f2076d3fa1537864736f6c634300080a0033
Loading

0 comments on commit 212e269

Please sign in to comment.