From 55b5a58668de4b496a762d912a0ad2707211940c Mon Sep 17 00:00:00 2001 From: millken Date: Wed, 16 Aug 2023 15:17:04 +0800 Subject: [PATCH] [api] fix revert message in eth_call (#3922) --- api/web3server.go | 7 ++++++- api/web3server_marshal.go | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api/web3server.go b/api/web3server.go index 385cd951b7..f435741bf0 100644 --- a/api/web3server.go +++ b/api/web3server.go @@ -21,6 +21,8 @@ import ( "github.com/tidwall/gjson" "go.opentelemetry.io/otel/attribute" "go.uber.org/zap" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "github.com/iotexproject/iotex-core/action" rewardingabi "github.com/iotexproject/iotex-core/action/protocol/rewarding/ethabi" @@ -387,10 +389,13 @@ func (svr *web3Handler) call(in *gjson.Result) (interface{}, error) { return "0x" + ret, nil } exec, _ := action.NewExecution(to, 0, value, gasLimit, big.NewInt(0), data) - ret, _, err := svr.coreService.ReadContract(context.Background(), callerAddr, exec) + ret, receipt, err := svr.coreService.ReadContract(context.Background(), callerAddr, exec) if err != nil { return nil, err } + if receipt != nil && len(receipt.GetExecutionRevertMsg()) > 0 { + return "0x" + ret, status.Error(codes.InvalidArgument, "execution reverted: "+receipt.GetExecutionRevertMsg()) + } return "0x" + ret, nil } diff --git a/api/web3server_marshal.go b/api/web3server_marshal.go index b0096b184b..cbd16a7f7b 100644 --- a/api/web3server_marshal.go +++ b/api/web3server_marshal.go @@ -29,8 +29,9 @@ type ( } errMessage struct { - Code int `json:"code"` - Message string `json:"message"` + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data,omitempty"` } streamResponse struct { @@ -124,6 +125,7 @@ func (obj *web3Response) MarshalJSON() ([]byte, error) { Error: errMessage{ Code: errCode, Message: errMsg, + Data: obj.result, }, }) }