Skip to content

Commit

Permalink
Merge pull request #64 from KyberNetwork/ref/mev/handle-nil-response
Browse files Browse the repository at this point in the history
refactor: ♻️ mev: handle nil result
  • Loading branch information
thanhpp authored Jul 15, 2024
2 parents 1f7d86e + 2397d93 commit 1d62d1f
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 1d62d1f

Please sign in to comment.