-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
206 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/openweb3/go-rpc-provider" | ||
"github.com/openweb3/go-rpc-provider/interfaces" | ||
) | ||
|
||
type Request struct { | ||
Method string | ||
Args []any | ||
} | ||
|
||
type Response[T any] struct { | ||
Data T | ||
Error error | ||
} | ||
|
||
// BatchCall is a generic method to call RPC in batch. | ||
func BatchCall[T any](provider interfaces.Provider, requests ...Request) ([]Response[T], error) { | ||
return BatchCallContext[T](provider, context.Background(), requests...) | ||
} | ||
|
||
// BatchCallContext is a generic method to call RPC with context in batch. | ||
func BatchCallContext[T any](provider interfaces.Provider, ctx context.Context, requests ...Request) ([]Response[T], error) { | ||
batch := make([]rpc.BatchElem, 0, len(requests)) | ||
responses := make([]Response[T], len(requests)) | ||
|
||
for i, v := range requests { | ||
batch = append(batch, rpc.BatchElem{ | ||
Method: v.Method, | ||
Args: v.Args, | ||
Result: &responses[i].Data, | ||
}) | ||
} | ||
|
||
if err := provider.BatchCallContext(ctx, batch); err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, v := range batch { | ||
if v.Error != nil { | ||
responses[i].Error = v.Error | ||
} | ||
} | ||
|
||
return responses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/openweb3/go-rpc-provider/interfaces" | ||
providers "github.com/openweb3/go-rpc-provider/provider_wrapper" | ||
) | ||
|
||
// Client is a base class of any RPC client. | ||
type Client struct { | ||
*providers.MiddlewarableProvider | ||
url string | ||
} | ||
|
||
// NewClient creates a new client instance. | ||
func NewClient(url string, option ...providers.Option) (*Client, error) { | ||
var opt providers.Option | ||
if len(option) > 0 { | ||
opt = option[0] | ||
} | ||
|
||
provider, err := providers.NewProviderWithOption(url, opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{providers.NewMiddlewarableProvider(provider), url}, nil | ||
} | ||
|
||
// URL Get the RPC server URL the client connected to. | ||
func (c *Client) URL() string { | ||
return c.url | ||
} | ||
|
||
// Call is a generic method to call RPC. | ||
func Call[T any](provider interfaces.Provider, method string, args ...any) (result T, err error) { | ||
return CallContext[T](provider, context.Background(), method, args...) | ||
} | ||
|
||
// CallContext is a generic method to call RPC with context. | ||
func CallContext[T any](provider interfaces.Provider, ctx context.Context, method string, args ...any) (result T, err error) { | ||
err = provider.CallContext(ctx, &result, method, args...) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.