Skip to content

Commit

Permalink
chore: reuse http.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennampham23 committed Dec 12, 2023
1 parent b3c3f0b commit cda493a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkg/parser/oneinch/oneinch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestParseEvent(t *testing.T) {
}

func TestParseOneinchTradeLog(t *testing.T) {
t.Skip("Need to add the rpc url that enables the trace call JSON-RPC")
//t.Skip("Need to add the rpc url that enables the trace call JSON-RPC")
p := MustNewParser(os.Getenv("RPC_URL"))

client, err := ethclient.Dial(os.Getenv("RPC_URL"))
Expand All @@ -85,6 +85,8 @@ func TestParseOneinchTradeLog(t *testing.T) {

require.Equal(t, true, strings.EqualFold("0x807cF9A772d5a3f9CeFBc1192e939D62f0D9bD38", order.Maker))
require.Equal(t, strings.ToLower("0xdac3a1ba8fa517c1d98ffecf092b2ad167440131b19dee3d782d0d7eadce01a2"), order.OrderHash)
require.Equal(t, "25583325494215510852", order.MakerTokenAmount)
require.Equal(t, "40450976402181226773533785758", order.TakerTokenAmount)
}

}
12 changes: 8 additions & 4 deletions pkg/rpcnode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"io"
"net/http"
"time"
)

type Client struct {
rpcUrl string
ethClient *ethclient.Client
httpClient *http.Client
rpcUrl string
ethClient *ethclient.Client
}

func NewClient(rpcUrl string) (*Client, error) {
Expand All @@ -21,6 +23,9 @@ func NewClient(rpcUrl string) (*Client, error) {
return nil, err
}
return &Client{
httpClient: &http.Client{
Timeout: 5 * time.Second,
},
rpcUrl: rpcUrl,
ethClient: ethClient,
}, nil
Expand All @@ -47,15 +52,14 @@ func (c *Client) FetchTraceCall(ctx context.Context, txHash string) (types.CallF
return types.CallFrame{}, err
}

client := &http.Client{}
req, err := http.NewRequest("POST", c.rpcUrl, bytes.NewBuffer(b))

if err != nil {
return types.CallFrame{}, err
}
req.Header.Add("Content-Type", "application/json")

res, err := client.Do(req)
res, err := c.httpClient.Do(req)
if err != nil {
return types.CallFrame{}, err
}
Expand Down

0 comments on commit cda493a

Please sign in to comment.