Skip to content

Commit

Permalink
EVM-535: Use contractsapi bindings throught code (#1321)
Browse files Browse the repository at this point in the history
* Use contractsapi bindings

* Lint fix

* Comments fix

* CoreContracts fix

* Rebase fix

* Comments fix
  • Loading branch information
goran-ethernal authored Mar 27, 2023
1 parent dbbc230 commit 562d542
Show file tree
Hide file tree
Showing 24 changed files with 551 additions and 177 deletions.
4 changes: 3 additions & 1 deletion command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
return nil, nil, fmt.Errorf("failed to unmarshal exit event from JSON. Error: %w", err)
}

exitEventEncoded, err := polybft.ExitEventInputsABIType.Encode(exitEvent)
var exitEventAPI contractsapi.L2StateSyncedEvent

exitEventEncoded, err := exitEventAPI.Encode(exitEvent)
if err != nil {
return nil, nil, fmt.Errorf("failed to encode exit event: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions command/bridge/withdraw/withdraw_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
"github.com/0xPolygon/polygon-edge/command/sidechain"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/txrelayer"
Expand Down Expand Up @@ -211,13 +210,14 @@ func createWithdrawTxn(receiver types.Address, amount *big.Int) (*ethgo.Transact

// extractExitEventID tries to extract exit event id from provided receipt
func extractExitEventID(receipt *ethgo.Receipt) (*big.Int, error) {
var exitEvent contractsapi.L2StateSyncedEvent
for _, log := range receipt.Logs {
if !polybft.ExitEventABI.Match(log) {
doesMatch, err := exitEvent.ParseLog(log)
if !doesMatch {
continue
}

exitEvent := &contractsapi.L2StateSyncedEvent{}
if err := exitEvent.ParseLog(log); err != nil {
if err != nil {
return nil, err
}

Expand Down
55 changes: 29 additions & 26 deletions command/sidechain/registration/register_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"math/big"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/helper"
Expand All @@ -21,10 +20,8 @@ import (
)

var (
stakeManager = contracts.ValidatorSetContract
stakeFn = contractsapi.ChildValidatorSet.Abi.Methods["stake"]
newValidatorEventABI = contractsapi.ChildValidatorSet.Abi.Events["NewValidator"]
stakeEventABI = contractsapi.ChildValidatorSet.Abi.Events["Staked"]
stakeManager = contracts.ValidatorSetContract
stakeFn = contractsapi.ChildValidatorSet.Abi.Methods["stake"]
)

var params registerParams
Expand Down Expand Up @@ -127,20 +124,23 @@ func runCommand(cmd *cobra.Command, _ []string) error {
result := &registerResult{}
foundLog := false

var newValidatorEvent contractsapi.NewValidatorEvent
for _, log := range receipt.Logs {
if newValidatorEventABI.Match(log) {
event, err := newValidatorEventABI.ParseLog(log)
if err != nil {
return err
}

result.validatorAddress = event["validator"].(ethgo.Address).String() //nolint:forcetypeassert
result.stakeResult = "No stake parameters have been submitted"
result.amount = 0
foundLog = true

break
doesMatch, err := newValidatorEvent.ParseLog(log)
if !doesMatch {
continue
}

if err != nil {
return err
}

result.validatorAddress = newValidatorEvent.Validator.String()
result.stakeResult = "No stake parameters have been submitted"
result.amount = 0
foundLog = true

break
}

if !foundLog {
Expand Down Expand Up @@ -193,20 +193,23 @@ func populateStakeResults(receipt *ethgo.Receipt, result *registerResult) {
}

// check the logs to verify stake
var stakedEvent contractsapi.StakedEvent
for _, log := range receipt.Logs {
if stakeEventABI.Match(log) {
event, err := stakeEventABI.ParseLog(log)
if err != nil {
result.stakeResult = "Failed to parse stake log"

return
}
doesMatch, err := stakedEvent.ParseLog(log)
if !doesMatch {
continue
}

result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert
result.stakeResult = "Stake succeeded"
if err != nil {
result.stakeResult = "Failed to parse stake log"

return
}

result.amount = stakedEvent.Amount.Uint64()
result.stakeResult = "Stake succeeded"

return
}

result.stakeResult = "Could not find an appropriate log in receipt that stake happened"
Expand Down
38 changes: 20 additions & 18 deletions command/sidechain/staking/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
)

var (
params stakeParams
stakeEventABI = contractsapi.ChildValidatorSet.Abi.Events["Staked"]
delegateEventABI = contractsapi.ChildValidatorSet.Abi.Events["Delegated"]
params stakeParams
)

func GetCommand() *cobra.Command {
Expand Down Expand Up @@ -134,31 +132,35 @@ func runCommand(cmd *cobra.Command, _ []string) error {
validatorAddress: validatorAccount.Ecdsa.Address().String(),
}

foundLog := false
var (
stakedEvent contractsapi.StakedEvent
delegatedEvent contractsapi.DelegatedEvent
foundLog bool
)

// check the logs to check for the result
for _, log := range receipt.Logs {
if stakeEventABI.Match(log) {
event, err := stakeEventABI.ParseLog(log)
if err != nil {
return err
}
doesMatch, err := stakedEvent.ParseLog(log)
if err != nil {
return err
}

if doesMatch { // its a stake function call
result.isSelfStake = true
result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert

result.amount = stakedEvent.Amount.Uint64()
foundLog = true

break
} else if delegateEventABI.Match(log) {
event, err := delegateEventABI.ParseLog(log)
if err != nil {
return err
}
}

result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert
result.delegatedTo = event["validator"].(ethgo.Address).String() //nolint:forcetypeassert
doesMatch, err = delegatedEvent.ParseLog(log)
if err != nil {
return err
}

if doesMatch {
result.amount = delegatedEvent.Amount.Uint64()
result.delegatedTo = delegatedEvent.Validator.String()
foundLog = true

break
Expand Down
41 changes: 20 additions & 21 deletions command/sidechain/unstaking/unstake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package unstaking

import (
"fmt"
"math/big"
"time"

"github.com/0xPolygon/polygon-edge/command"
Expand All @@ -17,11 +16,7 @@ import (
"github.com/umbracle/ethgo"
)

var (
params unstakeParams
unstakeEventABI = contractsapi.ChildValidatorSet.Abi.Events["Unstaked"]
undelegateEventABI = contractsapi.ChildValidatorSet.Abi.Events["Undelegated"]
)
var params unstakeParams

func GetCommand() *cobra.Command {
unstakeCmd := &cobra.Command{
Expand Down Expand Up @@ -132,31 +127,35 @@ func runCommand(cmd *cobra.Command, _ []string) error {
validatorAddress: validatorAccount.Ecdsa.Address().String(),
}

foundLog := false
var (
unstakedEvent contractsapi.UnstakedEvent
undelegatedEvent contractsapi.UndelegatedEvent
foundLog bool
)

// check the logs to check for the result
for _, log := range receipt.Logs {
if unstakeEventABI.Match(log) {
event, err := unstakeEventABI.ParseLog(log)
if err != nil {
return err
}
doesMatch, err := unstakedEvent.ParseLog(log)
if err != nil {
return err
}

if doesMatch { // its an unstake function call
result.isSelfUnstake = true
result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert

result.amount = unstakedEvent.Amount.Uint64()
foundLog = true

break
} else if undelegateEventABI.Match(log) {
event, err := undelegateEventABI.ParseLog(log)
if err != nil {
return err
}
}

result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert
result.undelegatedFrom = event["validator"].(ethgo.Address).String() //nolint:forcetypeassert
doesMatch, err = undelegatedEvent.ParseLog(log)
if err != nil {
return err
}

if doesMatch {
result.amount = undelegatedEvent.Amount.Uint64()
result.undelegatedFrom = undelegatedEvent.Validator.String()
foundLog = true

break
Expand Down
40 changes: 21 additions & 19 deletions command/sidechain/whitelist/whitelist_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import (
"github.com/umbracle/ethgo"
)

var (
whitelistFn = contractsapi.ChildValidatorSet.Abi.Methods["addToWhitelist"]
whitelistEventABI = contractsapi.ChildValidatorSet.Abi.Events["AddedToWhitelist"]
)

var params whitelistParams

func GetCommand() *cobra.Command {
Expand Down Expand Up @@ -83,9 +78,11 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("enlist validator failed: %w", err)
}

encoded, err := whitelistFn.Encode([]interface{}{
[]types.Address{types.StringToAddress(params.newValidatorAddress)},
})
whitelistFn := &contractsapi.AddToWhitelistChildValidatorSetFn{
WhitelistAddreses: []ethgo.Address{ethgo.Address(types.StringToAddress(params.newValidatorAddress))},
}

encoded, err := whitelistFn.EncodeAbi()
if err != nil {
return fmt.Errorf("enlist validator failed: %w", err)
}
Expand All @@ -106,21 +103,26 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("enlist validator transaction failed on block %d", receipt.BlockNumber)
}

result := &enlistResult{}
foundLog := false
var (
whitelistEvent contractsapi.AddedToWhitelistEvent
result = &enlistResult{}
foundLog = false
)

for _, log := range receipt.Logs {
if whitelistEventABI.Match(log) {
event, err := whitelistEventABI.ParseLog(log)
if err != nil {
return err
}

result.newValidatorAddress = event["validator"].(ethgo.Address).String() //nolint:forcetypeassert
foundLog = true
doesMatch, err := whitelistEvent.ParseLog(log)
if !doesMatch {
continue
}

break
if err != nil {
return err
}

result.newValidatorAddress = whitelistEvent.Validator.String()
foundLog = true

break
}

if !foundLog {
Expand Down
33 changes: 17 additions & 16 deletions command/sidechain/withdraw/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package withdraw

import (
"fmt"
"math/big"
"time"

"github.com/0xPolygon/polygon-edge/command"
Expand All @@ -17,10 +16,7 @@ import (
"github.com/umbracle/ethgo"
)

var (
params withdrawParams
withdrawEventABI = contractsapi.ChildValidatorSet.Abi.Events["Withdrawal"]
)
var params withdrawParams

func GetCommand() *cobra.Command {
withdrawCmd := &cobra.Command{
Expand Down Expand Up @@ -108,21 +104,26 @@ func runCommand(cmd *cobra.Command, _ []string) error {
validatorAddress: validatorAccount.Ecdsa.Address().String(),
}

foundLog := false
var (
withdrawalEvent contractsapi.WithdrawalEvent
foundLog bool
)

for _, log := range receipt.Logs {
if withdrawEventABI.Match(log) {
event, err := withdrawEventABI.ParseLog(log)
if err != nil {
return err
}

result.amount = event["amount"].(*big.Int).Uint64() //nolint:forcetypeassert
result.withdrawnTo = event["to"].(ethgo.Address).String() //nolint:forcetypeassert
foundLog = true
doesMatch, err := withdrawalEvent.ParseLog(log)
if !doesMatch {
continue
}

break
if err != nil {
return err
}

result.amount = withdrawalEvent.Amount.Uint64()
result.withdrawnTo = withdrawalEvent.To.String()
foundLog = true

break
}

if !foundLog {
Expand Down
Loading

0 comments on commit 562d542

Please sign in to comment.