66
77 "github.com/go-playground/validator/v10"
88 "github.com/pkg/errors"
9- kwilClientType "github.com/trufnetwork/kwil-db/core/client/types"
109 "github.com/trufnetwork/kwil-db/core/crypto/auth"
1110 "github.com/trufnetwork/kwil-db/core/gatewayclient"
1211 "github.com/trufnetwork/kwil-db/core/log"
@@ -20,10 +19,9 @@ import (
2019)
2120
2221type Client struct {
23- Signer auth.Signer `validate:"required"`
24- logger * log.Logger
25- kwilClient * gatewayclient.GatewayClient `validate:"required"`
26- kwilOptions * gatewayclient.GatewayOptions
22+ signer auth.Signer `validate:"required"`
23+ logger * log.Logger
24+ transport Transport `validate:"required"`
2725}
2826
2927var _ clientType.Client = (* Client )(nil )
@@ -32,21 +30,28 @@ type Option func(*Client)
3230
3331func NewClient (ctx context.Context , provider string , options ... Option ) (* Client , error ) {
3432 c := & Client {}
35- c .kwilOptions = & gatewayclient.GatewayOptions {
36- Options : * kwilClientType .DefaultOptions (),
37- }
33+
34+ // Apply user-provided options
3835 for _ , option := range options {
3936 option (c )
4037 }
4138
42- kwilClient , err := gatewayclient .NewClient (ctx , provider , c .kwilOptions )
43- if err != nil {
44- return nil , errors .WithStack (err )
39+ // Create default HTTPTransport if no transport was provided via options
40+ if c .transport == nil {
41+ var logger log.Logger
42+ if c .logger != nil {
43+ logger = * c .logger
44+ }
45+
46+ transport , err := NewHTTPTransport (ctx , provider , c .signer , logger )
47+ if err != nil {
48+ return nil , errors .Wrap (err , "failed to create default HTTP transport" )
49+ }
50+ c .transport = transport
4551 }
46- c .kwilClient = kwilClient
4752
4853 // Validate the client
49- if err = c .Validate (); err != nil {
54+ if err : = c .Validate (); err != nil {
5055 return nil , errors .WithStack (err )
5156 }
5257
@@ -60,28 +65,64 @@ func (c *Client) Validate() error {
6065
6166func WithSigner (signer auth.Signer ) Option {
6267 return func (c * Client ) {
63- c .kwilOptions .Signer = signer
64- c .Signer = signer
68+ c .signer = signer
6569 }
6670}
6771
6872func WithLogger (logger log.Logger ) Option {
6973 return func (c * Client ) {
7074 c .logger = & logger
71- c .kwilOptions .Logger = logger
75+ }
76+ }
77+
78+ // WithTransport configures the client to use a custom transport implementation.
79+ //
80+ // By default, the SDK uses HTTPTransport which communicates via standard net/http.
81+ // This option allows you to substitute a different transport (e.g., for Chainlink CRE,
82+ // mock testing, or custom protocols).
83+ //
84+ // Example:
85+ //
86+ // transport, _ := NewHTTPTransport(ctx, endpoint, signer, logger)
87+ // client, err := NewClient(ctx, endpoint,
88+ // tnclient.WithTransport(transport),
89+ // )
90+ //
91+ // Note: When using WithTransport, the provider URL passed to NewClient is ignored
92+ // since the transport is already configured.
93+ func WithTransport (transport Transport ) Option {
94+ return func (c * Client ) {
95+ c .transport = transport
7296 }
7397}
7498
7599func (c * Client ) GetSigner () auth.Signer {
76- return c .kwilClient .Signer ()
100+ return c .transport .Signer ()
77101}
78102
79103func (c * Client ) WaitForTx (ctx context.Context , txHash kwilType.Hash , interval time.Duration ) (* kwilType.TxQueryResponse , error ) {
80- return c .kwilClient .WaitTx (ctx , txHash , interval )
104+ return c .transport .WaitTx (ctx , txHash , interval )
81105}
82106
107+ // GetKwilClient returns the underlying GatewayClient if using HTTPTransport.
108+ //
109+ // This method provides direct access to the GatewayClient for advanced use cases
110+ // that require low-level control. For most scenarios, prefer using the Client's
111+ // high-level methods (ListStreams, DeployStream, etc.) which are transport-agnostic.
112+ //
113+ // Returns nil if using a non-HTTP transport (e.g., CRE transport).
114+ //
115+ // Example:
116+ //
117+ // if gwClient := client.GetKwilClient(); gwClient != nil {
118+ // // Direct GatewayClient access for advanced use cases
119+ // result, err := gwClient.Call(ctx, "", "custom_action", args)
120+ // }
83121func (c * Client ) GetKwilClient () * gatewayclient.GatewayClient {
84- return c .kwilClient
122+ if httpTransport , ok := c .transport .(* HTTPTransport ); ok {
123+ return httpTransport .gatewayClient
124+ }
125+ return nil
85126}
86127
87128func (c * Client ) DeployStream (ctx context.Context , streamId util.StreamId , streamType clientType.StreamType ) (types.Hash , error ) {
@@ -101,31 +142,31 @@ func (c *Client) DestroyStream(ctx context.Context, streamId util.StreamId) (typ
101142
102143func (c * Client ) LoadActions () (clientType.IAction , error ) {
103144 return tn_api .LoadAction (tn_api.NewActionOptions {
104- Client : c .kwilClient ,
145+ Client : c .GetKwilClient () ,
105146 })
106147}
107148
108149func (c * Client ) LoadPrimitiveActions () (clientType.IPrimitiveAction , error ) {
109150 return tn_api .LoadPrimitiveActions (tn_api.NewActionOptions {
110- Client : c .kwilClient ,
151+ Client : c .GetKwilClient () ,
111152 })
112153}
113154
114155func (c * Client ) LoadComposedActions () (clientType.IComposedAction , error ) {
115156 return tn_api .LoadComposedActions (tn_api.NewActionOptions {
116- Client : c .kwilClient ,
157+ Client : c .GetKwilClient () ,
117158 })
118159}
119160
120161func (c * Client ) LoadRoleManagementActions () (clientType.IRoleManagement , error ) {
121162 return tn_api .LoadRoleManagementActions (tn_api.NewRoleManagementOptions {
122- Client : c .kwilClient ,
163+ Client : c .GetKwilClient () ,
123164 })
124165}
125166
126167func (c * Client ) LoadAttestationActions () (clientType.IAttestationAction , error ) {
127168 return tn_api .LoadAttestationActions (tn_api.AttestationActionOptions {
128- Client : c .kwilClient ,
169+ Client : c .GetKwilClient () ,
129170 })
130171}
131172
@@ -140,7 +181,7 @@ func (c *Client) LoadAttestationActions() (clientType.IAttestationAction, error)
140181// txEvent, err := txActions.GetTransactionEvent(ctx, ...)
141182func (c * Client ) LoadTransactionActions () (clientType.ITransactionAction , error ) {
142183 return tn_api .LoadTransactionActions (tn_api.TransactionActionOptions {
143- Client : c .kwilClient ,
184+ Client : c .GetKwilClient () ,
144185 })
145186}
146187
@@ -152,7 +193,7 @@ func (c *Client) OwnStreamLocator(streamId util.StreamId) clientType.StreamLocat
152193}
153194
154195func (c * Client ) Address () util.EthereumAddress {
155- addr , err := auth.EthSecp256k1Authenticator {}.Identifier (c .kwilClient .Signer ().CompactID ())
196+ addr , err := auth.EthSecp256k1Authenticator {}.Identifier (c .transport .Signer ().CompactID ())
156197 if err != nil {
157198 // should never happen
158199 logging .Logger .Panic ("failed to get address from signer" , zap .Error (err ))
0 commit comments