diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 8bf32260cb..55faca0775 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -19,6 +19,7 @@ package ethapi import ( "context" "encoding/hex" + "encoding/json" "errors" "fmt" "math/big" @@ -1107,7 +1108,16 @@ type callResult struct { Logs []*types.Log `json:"logs"` Transfers []transfer `json:"transfers,omitempty"` GasUsed hexutil.Uint64 `json:"gasUsed"` - Error string `json:"error"` + Error string `json:"error,omitempty"` +} + +func (r *callResult) MarshalJSON() ([]byte, error) { + type callResultAlias callResult + // Marshal logs to be an empty array instead of nil when empty + if r.Logs == nil { + r.Logs = []*types.Log{} + } + return json.Marshal((*callResultAlias)(r)) } // Multicall executes series of transactions on top of a base state. diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 4da72ce6c9..3efc90a82d 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -670,10 +670,12 @@ func TestMulticall(t *testing.T) { res{ ReturnValue: "0x", GasUsed: "0x5208", + Logs: []types.Log{}, }, res{ ReturnValue: "0x", GasUsed: "0x5208", + Logs: []types.Log{}, }, }}, }, { @@ -715,19 +717,23 @@ func TestMulticall(t *testing.T) { res{ ReturnValue: "0x", GasUsed: "0x5208", + Logs: []types.Log{}, }, res{ ReturnValue: "0x", GasUsed: "0x5208", + Logs: []types.Log{}, }, }, { res{ ReturnValue: "0x", GasUsed: "0x5208", + Logs: []types.Log{}, }, res{ ReturnValue: "0x", GasUsed: "0x0", + Logs: []types.Log{}, Error: fmt.Sprintf("err: insufficient funds for gas * price + value: address %s have 0 want 1000 (supplied gas 9937000)", randomAccounts[3].addr.String()), }, }, @@ -766,11 +772,13 @@ func TestMulticall(t *testing.T) { res{ ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000b", GasUsed: "0xe891", + Logs: []types.Log{}, }, }, { res{ ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000c", GasUsed: "0xe891", + Logs: []types.Log{}, }, }}, }, @@ -830,9 +838,11 @@ func TestMulticall(t *testing.T) { want: [][]res{{{ ReturnValue: "0x", GasUsed: "0xaacc", + Logs: []types.Log{}, }, { ReturnValue: "0x0000000000000000000000000000000000000000000000000000000000000005", GasUsed: "0x5bb7", + Logs: []types.Log{}, }}}, }, // Test logs output. @@ -920,6 +930,7 @@ func TestMulticall(t *testing.T) { // Caller is in this case the contract that invokes ecrecover. ReturnValue: strings.ToLower(randomAccounts[2].addr.String()), GasUsed: "0x52f6", + Logs: []types.Log{}, }}}, }, // Test ether transfers. @@ -965,6 +976,7 @@ func TestMulticall(t *testing.T) { Value: big.NewInt(100), }, }, + Logs: []types.Log{}, }}}, }, }