Skip to content

Commit

Permalink
Export connection helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwartz10 committed Sep 30, 2024
1 parent 8ff6a1b commit 9558739
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 20 additions & 4 deletions sdk/extern.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package sdk

import (
"context"
"log"
"net/http"

"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/filecoin-project/go-jsonrpc"
lotusapi "github.com/filecoin-project/lotus/api"
"github.com/glifio/go-pools/rpc"
"github.com/gorilla/websocket"
)

func (c *fevmExtern) ConnectEthClient() (*ethclient.Client, error) {
return connectEthClient(c.dialAddr, c.token)
return ConnectEthClient(c.dialAddr, c.token)
}

func connectEthClient(dialAddr string, token string) (*ethclient.Client, error) {
func ConnectEthClient(dialAddr string, token string) (*ethclient.Client, error) {
if token == "" {
return ethclient.Dial(dialAddr)
}
Expand All @@ -34,11 +36,25 @@ func connectEthClient(dialAddr string, token string) (*ethclient.Client, error)
return ethclient.NewClient(client), nil
}

func ConnectEthClientWS(dialAddr string, token string) (*ethclient.Client, error) {

tokenHeader := ethrpc.WithHeader("Authorization", "Bearer "+token)
httpClient := ethrpc.WithWebsocketDialer(websocket.Dialer{})

client, err := ethrpc.DialOptions(context.Background(), dialAddr, httpClient, tokenHeader)
if err != nil {
log.Println("error dialing eth client", err)
return nil, err
}

return ethclient.NewClient(client), nil
}

func (c *fevmExtern) ConnectLotusClient() (*lotusapi.FullNodeStruct, jsonrpc.ClientCloser, error) {
return connectLotusClient(c.dialAddr, c.token)
return ConnectLotusClient(c.dialAddr, c.token)
}

func connectLotusClient(lotusDialAddr string, lotusToken string) (*lotusapi.FullNodeStruct, jsonrpc.ClientCloser, error) {
func ConnectLotusClient(lotusDialAddr string, lotusToken string) (*lotusapi.FullNodeStruct, jsonrpc.ClientCloser, error) {
head := http.Header{}

if lotusToken != "" {
Expand Down
4 changes: 2 additions & 2 deletions sdk/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func New(
extern types.Extern,
) (types.PoolsSDK, error) {
var sdk types.PoolsSDK
ethClient, err := connectEthClient(extern.LotusDialAddr, extern.LotusToken)
ethClient, err := ConnectEthClient(extern.LotusDialAddr, extern.LotusToken)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func Init(
token string,
eventsURL string,
) error {
client, err := connectEthClient(dialAddr, token)
client, err := ConnectEthClient(dialAddr, token)
if err != nil {
return err
}
Expand Down

0 comments on commit 9558739

Please sign in to comment.