fix marshaling of empty values

This commit is contained in:
Sina Mahmoodi 2023-06-15 15:27:01 +02:00
parent 2db37b9471
commit 04965a36a0
2 changed files with 23 additions and 1 deletions

View file

@ -19,6 +19,7 @@ package ethapi
import ( import (
"context" "context"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@ -1107,7 +1108,16 @@ type callResult struct {
Logs []*types.Log `json:"logs"` Logs []*types.Log `json:"logs"`
Transfers []transfer `json:"transfers,omitempty"` Transfers []transfer `json:"transfers,omitempty"`
GasUsed hexutil.Uint64 `json:"gasUsed"` 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. // Multicall executes series of transactions on top of a base state.

View file

@ -670,10 +670,12 @@ func TestMulticall(t *testing.T) {
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x5208", GasUsed: "0x5208",
Logs: []types.Log{},
}, },
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x5208", GasUsed: "0x5208",
Logs: []types.Log{},
}, },
}}, }},
}, { }, {
@ -715,19 +717,23 @@ func TestMulticall(t *testing.T) {
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x5208", GasUsed: "0x5208",
Logs: []types.Log{},
}, },
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x5208", GasUsed: "0x5208",
Logs: []types.Log{},
}, },
}, { }, {
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x5208", GasUsed: "0x5208",
Logs: []types.Log{},
}, },
res{ res{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0x0", 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()), 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{ res{
ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000b", ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000b",
GasUsed: "0xe891", GasUsed: "0xe891",
Logs: []types.Log{},
}, },
}, { }, {
res{ res{
ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000c", ReturnValue: "0x000000000000000000000000000000000000000000000000000000000000000c",
GasUsed: "0xe891", GasUsed: "0xe891",
Logs: []types.Log{},
}, },
}}, }},
}, },
@ -830,9 +838,11 @@ func TestMulticall(t *testing.T) {
want: [][]res{{{ want: [][]res{{{
ReturnValue: "0x", ReturnValue: "0x",
GasUsed: "0xaacc", GasUsed: "0xaacc",
Logs: []types.Log{},
}, { }, {
ReturnValue: "0x0000000000000000000000000000000000000000000000000000000000000005", ReturnValue: "0x0000000000000000000000000000000000000000000000000000000000000005",
GasUsed: "0x5bb7", GasUsed: "0x5bb7",
Logs: []types.Log{},
}}}, }}},
}, },
// Test logs output. // Test logs output.
@ -920,6 +930,7 @@ func TestMulticall(t *testing.T) {
// Caller is in this case the contract that invokes ecrecover. // Caller is in this case the contract that invokes ecrecover.
ReturnValue: strings.ToLower(randomAccounts[2].addr.String()), ReturnValue: strings.ToLower(randomAccounts[2].addr.String()),
GasUsed: "0x52f6", GasUsed: "0x52f6",
Logs: []types.Log{},
}}}, }}},
}, },
// Test ether transfers. // Test ether transfers.
@ -965,6 +976,7 @@ func TestMulticall(t *testing.T) {
Value: big.NewInt(100), Value: big.NewInt(100),
}, },
}, },
Logs: []types.Log{},
}}}, }}},
}, },
} }