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 <[email protected]>
  • Loading branch information
thanhpp committed Jul 11, 2024
1 parent 1f7d86e commit 828789b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/mev/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ 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),
}
if err := json.Unmarshal(b, &alias); err != nil {
return err
}

return nil
}

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

import (
"encoding/json"
"fmt"
"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))
fmt.Printf("%+v\n", resp)
}
}

0 comments on commit 828789b

Please sign in to comment.