Skip to content

Commit

Permalink
promote decode err to common (#15745)
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann authored Dec 18, 2024
1 parent 935da36 commit d128079
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
27 changes: 26 additions & 1 deletion deployment/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
chain_selectors "github.com/smartcontractkit/chain-selectors"

Expand Down Expand Up @@ -87,7 +88,7 @@ func parseError(txError error) (string, error) {
return callErr.Data, nil
}

func ParseErrorFromABI(errorString string, contractABI string) (string, error) {
func parseErrorFromABI(errorString string, contractABI string) (string, error) {
parsedAbi, err := abi.JSON(strings.NewReader(contractABI))
if err != nil {
return "", errors.Wrap(err, "error loading ABI")
Expand All @@ -112,6 +113,30 @@ func ParseErrorFromABI(errorString string, contractABI string) (string, error) {
return "", errors.New("error not found in ABI")
}

// DecodeErr decodes an error from a contract call using the contract's ABI.
// If the error is not decodable, it returns the original error.
func DecodeErr(encodedABI string, err error) error {
if err == nil {
return nil
}
//revive:disable
var d rpc.DataError
ok := errors.As(err, &d)
if ok {
encErr, ok := d.ErrorData().(string)
if !ok {
return fmt.Errorf("error without error data: %s", d.Error())
}
errStr, parseErr := parseErrorFromABI(encErr, encodedABI)
if parseErr != nil {
return fmt.Errorf("failed to decode error '%s' with abi: %w", encErr, parseErr)
}
return fmt.Errorf("contract error: %s", errStr)

}
return fmt.Errorf("cannot decode error with abi: %w", err)
}

// ContractDeploy represents the result of an EVM contract deployment
// via an abigen Go binding. It contains all the return values
// as they are useful in different ways.
Expand Down
24 changes: 3 additions & 21 deletions deployment/keystone/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/exp/maps"

"github.com/smartcontractkit/ccip-owner-contracts/pkg/proposal/mcms"
Expand Down Expand Up @@ -605,27 +604,10 @@ func DefaultCapConfig(capType uint8, nNodes int) *capabilitiespb.CapabilityConfi
}
}

// DEPRECATED: use deployment.DecodeErr instead
// todo: refactor all keystone deps to use deployment.DecodeErr
func DecodeErr(encodedABI string, err error) error {
if err == nil {
return nil
}

//revive:disable
var d rpc.DataError
ok := errors.As(err, &d)
if ok {
encErr, ok := d.ErrorData().(string)
if !ok {
return fmt.Errorf("error without error data: %s", d.Error())
}
errStr, parseErr := deployment.ParseErrorFromABI(encErr, encodedABI)
if parseErr != nil {
return fmt.Errorf("failed to decode error '%s' with abi: %w", encErr, parseErr)
}
return fmt.Errorf("contract error: %s", errStr)

}
return fmt.Errorf("cannot decode error with abi: %w", err)
return deployment.DecodeErr(encodedABI, err)
}

// register nodes
Expand Down

0 comments on commit d128079

Please sign in to comment.