Skip to content

Commit

Permalink
New method to create websocket provider
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Dec 24, 2024
1 parent 28a28aa commit 4fb440a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.1
require (
github.com/NethermindEth/juno v0.12.2
github.com/ethereum/go-ethereum v1.14.8
github.com/gorilla/websocket v1.5.3
github.com/joho/godotenv v1.4.0
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
github.com/pkg/errors v0.9.1
Expand All @@ -23,7 +24,6 @@ require (
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
17 changes: 16 additions & 1 deletion rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/NethermindEth/juno/core/felt"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/websocket"
"golang.org/x/net/publicsuffix"
)

Expand All @@ -22,7 +23,7 @@ type Provider struct {
chainID string
}

// NewProvider creates a new rpc Provider instance.
// NewProvider creates a new HTTP rpc Provider instance.
func NewProvider(url string, options ...ethrpc.ClientOption) (*Provider, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
Expand All @@ -40,6 +41,20 @@ func NewProvider(url string, options ...ethrpc.ClientOption) (*Provider, error)
return &Provider{c: c}, nil
}

// NewWebsocketProvider creates a new Websocket rpc Provider instance.
func NewWebsocketProvider(url string, options ...ethrpc.ClientOption) (*Provider, error) {
var dialer websocket.Dialer
// prepend the custom client to allow users to override
options = append([]ethrpc.ClientOption{ethrpc.WithWebsocketDialer(dialer)}, options...)
c, err := ethrpc.DialOptions(context.Background(), url, options...)

if err != nil {
return nil, err
}

return &Provider{c: c}, nil
}

//go:generate mockgen -destination=../mocks/mock_rpc_provider.go -package=mocks -source=provider.go api
type RpcProvider interface {
AddInvokeTransaction(ctx context.Context, invokeTxn BroadcastInvokeTxnType) (*AddInvokeTransactionResponse, error)
Expand Down

0 comments on commit 4fb440a

Please sign in to comment.