Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make thorclient configurable + fix type error #908

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion thorclient/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ type Client struct {

// New creates a new Client with the provided URL.
func New(url string) *Client {
return NewWithHTTP(url, http.DefaultClient)
}

func NewWithHTTP(url string, c *http.Client) *Client {
return &Client{
url: url,
c: &http.Client{},
c: c,
}
}

Expand Down
10 changes: 9 additions & 1 deletion thorclient/thorclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package thorclient

import (
"fmt"
"net/http"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -44,6 +45,13 @@ func New(url string) *Client {
}
}

// NewWithHTTP creates a new Client using the provided HTTP URL and HTTP client.
func NewWithHTTP(url string, c *http.Client) *Client {
return &Client{
httpConn: httpclient.NewWithHTTP(url, c),
}
}

// NewWithWS creates a new Client using the provided HTTP and WebSocket URLs.
// Returns an error if the WebSocket connection fails.
func NewWithWS(url string) (*Client, error) {
Expand Down Expand Up @@ -202,7 +210,7 @@ func (c *Client) ChainTag() (byte, error) {
}

// SubscribeBlocks subscribes to block updates over WebSocket.
func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*blocks.JSONCollapsedBlock], error) {
func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*subscriptions.BlockMessage], error) {
if c.wsConn == nil {
return nil, fmt.Errorf("not a websocket typed client")
}
Expand Down
5 changes: 2 additions & 3 deletions thorclient/wsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/vechain/thor/v2/thor"

"github.com/gorilla/websocket"
"github.com/vechain/thor/v2/api/blocks"
"github.com/vechain/thor/v2/api/subscriptions"
"github.com/vechain/thor/v2/thorclient/common"
)
Expand Down Expand Up @@ -89,15 +88,15 @@ func (c *Client) SubscribeEvents(pos string, filter *subscriptions.EventFilter)

// SubscribeBlocks subscribes to block updates based on the provided query.
// It returns a Subscription that streams block messages or an error if the connection fails.
func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*blocks.JSONCollapsedBlock], error) {
func (c *Client) SubscribeBlocks(pos string) (*common.Subscription[*subscriptions.BlockMessage], error) {
queryValues := &url.Values{}
queryValues.Add("pos", pos)
conn, err := c.connect("/subscriptions/block", queryValues)
if err != nil {
return nil, fmt.Errorf("unable to connect - %w", err)
}

return subscribe[blocks.JSONCollapsedBlock](conn), nil
return subscribe[subscriptions.BlockMessage](conn), nil
}

// SubscribeTransfers subscribes to transfer events based on the provided query.
Expand Down
14 changes: 6 additions & 8 deletions thorclient/wsclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import (
"testing"
"time"

"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/thor"

"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/api/blocks"
"github.com/vechain/thor/v2/api/subscriptions"
"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient/common"
)

Expand Down Expand Up @@ -50,7 +48,7 @@ func TestClient_SubscribeEvents(t *testing.T) {

func TestClient_SubscribeBlocks(t *testing.T) {
pos := "best"
expectedBlock := &blocks.JSONCollapsedBlock{}
expectedBlock := &subscriptions.BlockMessage{}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/subscriptions/block", r.URL.Path)
Expand Down Expand Up @@ -288,7 +286,7 @@ func TestClient_SubscribeBlocks_ServerError(t *testing.T) {

func TestClient_SubscribeBlocks_ServerShutdown(t *testing.T) {
pos := "best"
expectedBlock := &blocks.JSONCollapsedBlock{}
expectedBlock := &subscriptions.BlockMessage{}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/subscriptions/block", r.URL.Path)
Expand Down Expand Up @@ -325,7 +323,7 @@ func TestClient_SubscribeBlocks_ServerShutdown(t *testing.T) {

func TestClient_SubscribeBlocks_ClientShutdown(t *testing.T) {
pos := "best"
expectedBlock := &blocks.JSONCollapsedBlock{}
expectedBlock := &subscriptions.BlockMessage{}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/subscriptions/block", r.URL.Path)
Expand Down Expand Up @@ -377,7 +375,7 @@ func TestClient_SubscribeBlocks_ClientShutdown(t *testing.T) {

func TestClient_SubscribeBlocks_ClientShutdown_LongBlocks(t *testing.T) {
pos := "best"
expectedBlock := &blocks.JSONCollapsedBlock{}
expectedBlock := &subscriptions.BlockMessage{}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/subscriptions/block", r.URL.Path)
Expand Down
Loading