diff --git a/README.md b/README.md index 11351b31..f2a315ad 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ Send it to Hydra's team, so they can whitelist your address to be able to partic After Hydra's team confirms you are whitelisted you have to register your account as a validator and stake a given amount. ``` -hydra polybft register-validator --data-dir ./node-secrets --stake 99000000000000000000 --chain-id 8844 --jsonrpc http://localhost:8545 +hydra hydragon register-validator --data-dir ./node-secrets --stake 99000000000000000000 --chain-id 8844 --jsonrpc http://localhost:8545 ``` The above command both register the validator and stakes the specified amount. @@ -152,7 +152,7 @@ The above command both register the validator and stakes the specified amount. Use the following command in case you want to execute the stake operation only: ``` -hydra polybft stake --data-dir ./node-secrets --self true --amount 99000000000000000000 --jsonrpc http://localhost:8545 +hydra hydragon stake --data-dir ./node-secrets --self true --amount 99000000000000000000 --jsonrpc http://localhost:8545 ``` **Note:** Amounts are specified in wei. diff --git a/command/polybft/polybft_command.go b/command/polybft/polybft_command.go index e44d1a4d..d0260ccf 100644 --- a/command/polybft/polybft_command.go +++ b/command/polybft/polybft_command.go @@ -2,7 +2,7 @@ package polybft import ( // H_MODIFY: Registration module is moved to sidechain - withdraw "github.com/0xPolygon/polygon-edge/command/bridge/withdraw/erc721" + "github.com/0xPolygon/polygon-edge/command/sidechain/registration" "github.com/0xPolygon/polygon-edge/command/sidechain/staking" "github.com/0xPolygon/polygon-edge/command/sidechain/whitelist" @@ -33,8 +33,6 @@ func GetCommand() *cobra.Command { registration.GetCommand(), // sidechain (validator set) command to whitelist validators whitelist.GetCommand(), - // sidechain (validator set) command to witdhraw amount on child chain - withdraw.GetCommand(), ) return polybftCmd diff --git a/command/sidechain/rewards/rewards.go b/command/sidechain/rewards/rewards.go index 35f761c9..03080c7e 100644 --- a/command/sidechain/rewards/rewards.go +++ b/command/sidechain/rewards/rewards.go @@ -10,12 +10,10 @@ import ( "github.com/0xPolygon/polygon-edge/command" "github.com/0xPolygon/polygon-edge/command/helper" "github.com/0xPolygon/polygon-edge/command/polybftsecrets" - rootHelper "github.com/0xPolygon/polygon-edge/command/rootchain/helper" "github.com/0xPolygon/polygon-edge/command/sidechain" sidechainHelper "github.com/0xPolygon/polygon-edge/command/sidechain" "github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi" "github.com/0xPolygon/polygon-edge/contracts" - "github.com/0xPolygon/polygon-edge/helper/common" "github.com/0xPolygon/polygon-edge/txrelayer" "github.com/0xPolygon/polygon-edge/types" ) @@ -24,8 +22,8 @@ var params withdrawRewardsParams func GetCommand() *cobra.Command { unstakeCmd := &cobra.Command{ - Use: "withdraw-rewards", - Short: "Withdraws pending rewards on child chain for given validator", + Use: "claim-rewards", + Short: "Claim rewards for given validator", PreRunE: runPreRun, RunE: runCommand, } @@ -77,7 +75,6 @@ func runCommand(cmd *cobra.Command, _ []string) error { } validatorAddr := validatorAccount.Ecdsa.Address() - rewardPoolAddr := ethgo.Address(contracts.RewardPoolContract) txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(params.jsonRPC), txrelayer.WithReceiptTimeout(150*time.Millisecond)) @@ -85,40 +82,53 @@ func runCommand(cmd *cobra.Command, _ []string) error { return err } - encoded, err := contractsapi.RewardPool.Abi.Methods["pendingRewards"].Encode([]interface{}{validatorAddr}) + claimRewardsFn := contractsapi.ClaimValidatorRewardRewardPoolFn{} + encoded, err := claimRewardsFn.EncodeAbi() if err != nil { return err } - response, err := txRelayer.Call(validatorAddr, rewardPoolAddr, encoded) - if err != nil { - return err + txn := ðgo.Transaction{ + From: validatorAddr, + Input: encoded, + To: (*ethgo.Address)(&contracts.RewardPoolContract), } - amount, err := common.ParseUint256orHex(&response) + receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa) if err != nil { return err } - encoded, err = contractsapi.RewardPool.Abi.Methods["withdrawReward"].Encode([]interface{}{}) - if err != nil { - return err + if receipt.Status != uint64(types.ReceiptSuccess) { + return fmt.Errorf("claim rewards transaction failed on block: %d", receipt.BlockNumber) } - txn := rootHelper.CreateTransaction(validatorAddr, &rewardPoolAddr, encoded, nil, false) + var ( + claimRewardsEvent contractsapi.ValidatorRewardClaimedEvent + foundLog bool + ) - receipt, err := txRelayer.SendTransaction(txn, validatorAccount.Ecdsa) - if err != nil { - return err + result := &withdrawRewardResult{ + ValidatorAddress: validatorAddr.String(), } - if receipt.Status != uint64(types.ReceiptSuccess) { - return fmt.Errorf("withdraw transaction failed on block: %d", receipt.BlockNumber) + // check the logs to check for the result + for _, log := range receipt.Logs { + doesMatch, err := claimRewardsEvent.ParseLog(log) + if err != nil { + return err + } + + if doesMatch { + foundLog = true + result.RewardAmount = claimRewardsEvent.Amount.Uint64() + + break + } } - result := &withdrawRewardResult{ - ValidatorAddress: validatorAccount.Ecdsa.Address().String(), - RewardAmount: amount.Uint64(), + if !foundLog { + return fmt.Errorf("could not find an appropriate log in receipt that rewards claim happened (claim rewards)") } outputter.WriteCommandResult(result) diff --git a/command/sidechain/withdraw/withdraw.go b/command/sidechain/withdraw/withdraw.go index d8c6e851..abf7f20f 100644 --- a/command/sidechain/withdraw/withdraw.go +++ b/command/sidechain/withdraw/withdraw.go @@ -100,7 +100,7 @@ func runCommand(cmd *cobra.Command, _ []string) error { } var ( - withdrawalEvent contractsapi.WithdrawalEvent + withdrawalEvent contractsapi.WithdrawalFinishedEvent foundLog bool ) diff --git a/consensus/polybft/contractsapi/bindings-gen/main.go b/consensus/polybft/contractsapi/bindings-gen/main.go index 9c49feca..bfd9a395 100644 --- a/consensus/polybft/contractsapi/bindings-gen/main.go +++ b/consensus/polybft/contractsapi/bindings-gen/main.go @@ -60,6 +60,7 @@ func main() { "Undelegated", "AddedToWhitelist", "StakeChanged", + "WithdrawalFinished", }, }, { @@ -69,8 +70,11 @@ func main() { []string{ "initialize", "distributeRewardsFor", + "claimValidatorReward()", + }, + []string{ + "ValidatorRewardClaimed", }, - []string{}, }, { "LiquidityToken", diff --git a/consensus/polybft/contractsapi/contractsapi.go b/consensus/polybft/contractsapi/contractsapi.go index 3f2f97e9..5580315d 100644 --- a/consensus/polybft/contractsapi/contractsapi.go +++ b/consensus/polybft/contractsapi/contractsapi.go @@ -323,6 +323,32 @@ func (s *StakeChangedEvent) Decode(input []byte) error { return ValidatorSet.Abi.Events["StakeChanged"].Inputs.DecodeStruct(input, &s) } +type WithdrawalFinishedEvent struct { + Account types.Address `abi:"account"` + To types.Address `abi:"to"` + Amount *big.Int `abi:"amount"` +} + +func (*WithdrawalFinishedEvent) Sig() ethgo.Hash { + return ValidatorSet.Abi.Events["WithdrawalFinished"].ID() +} + +func (w *WithdrawalFinishedEvent) Encode() ([]byte, error) { + return ValidatorSet.Abi.Events["WithdrawalFinished"].Inputs.Encode(w) +} + +func (w *WithdrawalFinishedEvent) ParseLog(log *ethgo.Log) (bool, error) { + if !ValidatorSet.Abi.Events["WithdrawalFinished"].Match(log) { + return false, nil + } + + return true, decodeEvent(ValidatorSet.Abi.Events["WithdrawalFinished"], log, w) +} + +func (w *WithdrawalFinishedEvent) Decode(input []byte) error { + return ValidatorSet.Abi.Events["WithdrawalFinished"].Inputs.DecodeStruct(input, &w) +} + type InitializeRewardPoolFn struct { NewValidatorSet types.Address `abi:"newValidatorSet"` NewRewardWallet types.Address `abi:"newRewardWallet"` @@ -375,6 +401,46 @@ func (d *DistributeRewardsForRewardPoolFn) DecodeAbi(buf []byte) error { return decodeMethod(RewardPool.Abi.Methods["distributeRewardsFor"], buf, d) } +type ClaimValidatorRewardRewardPoolFn struct { +} + +func (c *ClaimValidatorRewardRewardPoolFn) Sig() []byte { + return RewardPool.Abi.MethodsBySignature["claimValidatorReward()"].ID() +} + +func (c *ClaimValidatorRewardRewardPoolFn) EncodeAbi() ([]byte, error) { + return RewardPool.Abi.MethodsBySignature["claimValidatorReward()"].Encode(c) +} + +func (c *ClaimValidatorRewardRewardPoolFn) DecodeAbi(buf []byte) error { + return decodeMethod(RewardPool.Abi.MethodsBySignature["claimValidatorReward()"], buf, c) +} + +type ValidatorRewardClaimedEvent struct { + Validator types.Address `abi:"validator"` + Amount *big.Int `abi:"amount"` +} + +func (*ValidatorRewardClaimedEvent) Sig() ethgo.Hash { + return RewardPool.Abi.Events["ValidatorRewardClaimed"].ID() +} + +func (v *ValidatorRewardClaimedEvent) Encode() ([]byte, error) { + return RewardPool.Abi.Events["ValidatorRewardClaimed"].Inputs.Encode(v) +} + +func (v *ValidatorRewardClaimedEvent) ParseLog(log *ethgo.Log) (bool, error) { + if !RewardPool.Abi.Events["ValidatorRewardClaimed"].Match(log) { + return false, nil + } + + return true, decodeEvent(RewardPool.Abi.Events["ValidatorRewardClaimed"], log, v) +} + +func (v *ValidatorRewardClaimedEvent) Decode(input []byte) error { + return RewardPool.Abi.Events["ValidatorRewardClaimed"].Inputs.DecodeStruct(input, &v) +} + type InitializeLiquidityTokenFn struct { Name_ string `abi:"name_"` Symbol_ string `abi:"symbol_"` diff --git a/consensus/polybft/contractsapi/contractsapi_mocked.go b/consensus/polybft/contractsapi/contractsapi_mocked.go index dac7a449..a5daef64 100644 --- a/consensus/polybft/contractsapi/contractsapi_mocked.go +++ b/consensus/polybft/contractsapi/contractsapi_mocked.go @@ -1578,31 +1578,6 @@ func (w *WithdrawalRegisteredEvent) Decode(input []byte) error { return ValidatorSet.Abi.Events["WithdrawalRegistered"].Inputs.DecodeStruct(input, &w) } -type WithdrawalEvent struct { - Account types.Address `abi:"account"` - Amount *big.Int `abi:"amount"` -} - -func (*WithdrawalEvent) Sig() ethgo.Hash { - return ValidatorSet.Abi.Events["Withdrawal"].ID() -} - -func (w *WithdrawalEvent) Encode() ([]byte, error) { - return ValidatorSet.Abi.Events["Withdrawal"].Inputs.Encode(w) -} - -func (w *WithdrawalEvent) ParseLog(log *ethgo.Log) (bool, error) { - if !ValidatorSet.Abi.Events["Withdrawal"].Match(log) { - return false, nil - } - - return true, decodeEvent(ValidatorSet.Abi.Events["Withdrawal"], log, w) -} - -func (w *WithdrawalEvent) Decode(input []byte) error { - return ValidatorSet.Abi.Events["Withdrawal"].Inputs.DecodeStruct(input, &w) -} - type InitializeEIP1559BurnFn struct { NewChildERC20Predicate types.Address `abi:"newChildERC20Predicate"` NewBurnDestination types.Address `abi:"newBurnDestination"` diff --git a/h_docs/polybft_setup.md b/h_docs/polybft_setup.md index d67261f9..a2fb57b8 100644 --- a/h_docs/polybft_setup.md +++ b/h_docs/polybft_setup.md @@ -60,7 +60,7 @@ We need to set native token to be mintable, so we can premine balances to differ 2. Use the governer (first validator by default) to whitelist the new account ``` -./hydra polybft whitelist-validator --data-dir ./test-chain-1 --address 0x7A94400e0d33B79B6C69979df3f7a46CF1963c69 --jsonrpc http://127.0.0.1:10001 +./hydra hydragon whitelist-validator --data-dir ./test-chain-1 --address 0x7A94400e0d33B79B6C69979df3f7a46CF1963c69 --jsonrpc http://127.0.0.1:10001 ``` @@ -71,7 +71,7 @@ Stake tx is made in this step as well ``` -./hydra polybft register-validator --data-dir ./test-add-chain-1 --stake 1000000000000000000 --chain-id 8844 --jsonrpc http://127.0.0.1:10001 +./hydra hydragon register-validator --data-dir ./test-add-chain-1 --stake 1000000000000000000 --chain-id 8844 --jsonrpc http://127.0.0.1:10001 ``` @@ -189,7 +189,7 @@ After Hydra's team confirms you are whitelisted you have to register your accoun In the container's shell execute: ``` -hydra polybft register-validator --data-dir ./node --stake 1000000000000000000000000 --chain-id 8844 --jsonrpc http://localhost:8545 +hydra hydragon register-validator --data-dir ./node --stake 1000000000000000000000000 --chain-id 8844 --jsonrpc http://localhost:8545 ``` The above command both register the validator and stakes the specified amount. @@ -197,7 +197,7 @@ The above command both register the validator and stakes the specified amount. Use the following command in case you want to execute the stake operation only: ``` -hydra polybft stake --data-dir ./node --self true --amount 999900000000000000000000 --jsonrpc http://localhost:8545 +hydra hydragon stake --data-dir ./node --self true --amount 999900000000000000000000 --jsonrpc http://localhost:8545 ``` Congratulations! You are now a Hydra Chain validator! @@ -221,5 +221,5 @@ Setup any compatible wallet and execute the transfer from there. In the container's shell execute: ``` -hydra polybft whitelist-validator --data-dir ./node --address --jsonrpc http://localhost:8545 +hydra hydragon whitelist-validator --data-dir ./node --address --jsonrpc http://localhost:8545 ```