Skip to content

Commit

Permalink
refactor: ♻️ mev: handle nil result
Browse files Browse the repository at this point in the history
Signed-off-by: thanhpp <thanhphanphu18@gmail.com>
  • Loading branch information
thanhpp committed Jul 11, 2024
1 parent 1f7d86e commit 2397d93
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/mev/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ type SendBundleResult struct {
Message string `json:"message,omitempty"`
}

func (r *SendBundleResult) UnmarshalJSON(b []byte) error {
if str := string(b); (str == "\"nil\"" || str == "\"null\"") && r != nil {
*r = SendBundleResult{}
return nil
}

// Otherwise, unmarshal the data as usual
// disable this UnmarshalJSON function
type Alias SendBundleResult
alias := &struct {
*Alias
}{
Alias: (*Alias)(r),
}

return json.Unmarshal(b, &alias)
}

type SendBundleResults struct {
GasUsed int `json:"gasUsed,omitempty"`
TxHash string `json:"txHash,omitempty"`
Expand Down
23 changes: 23 additions & 0 deletions pkg/mev/pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mev_test

import (
"encoding/json"
"testing"

"github.com/KyberNetwork/tradinglib/pkg/mev"
"github.com/test-go/testify/require"
)

func TestUnmarshalSendBundleResponse1(t *testing.T) {
raws := []string{
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"nil\"}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{}}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"bundleHash\": \"0x0\"}}",
}

for _, raw := range raws {
var resp mev.SendBundleResponse
require.NoError(t, json.Unmarshal([]byte(raw), &resp))
t.Logf("%+v\n", resp)
}
}

0 comments on commit 2397d93

Please sign in to comment.