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

Fix withdrawal #7

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ 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.

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.
Expand Down
4 changes: 1 addition & 3 deletions command/polybft/polybft_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
54 changes: 32 additions & 22 deletions command/sidechain/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
}
Expand Down Expand Up @@ -77,48 +75,60 @@ 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))
if err != nil {
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 := &ethgo.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)
Expand Down
2 changes: 1 addition & 1 deletion command/sidechain/withdraw/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
}

var (
withdrawalEvent contractsapi.WithdrawalEvent
withdrawalEvent contractsapi.WithdrawalFinishedEvent
foundLog bool
)

Expand Down
6 changes: 5 additions & 1 deletion consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
"Undelegated",
"AddedToWhitelist",
"StakeChanged",
"WithdrawalFinished",
},
},
{
Expand All @@ -69,8 +70,11 @@ func main() {
[]string{
"initialize",
"distributeRewardsFor",
"claimValidatorReward()",
},
[]string{
"ValidatorRewardClaimed",
},
[]string{},
},
{
"LiquidityToken",
Expand Down
66 changes: 66 additions & 0 deletions consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions consensus/polybft/contractsapi/contractsapi_mocked.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions h_docs/polybft_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```

Expand All @@ -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

```

Expand Down Expand Up @@ -189,15 +189,15 @@ 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.

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!
Expand All @@ -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 <provided address> --jsonrpc http://localhost:8545
hydra hydragon whitelist-validator --data-dir ./node --address <provided address> --jsonrpc http://localhost:8545
```
Loading