-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from najeal/eth-rpc-options
Eth Client: Use http-headers and query-parameters
- Loading branch information
Showing
15 changed files
with
224 additions
and
79 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package ethclient | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/ava-labs/subnet-evm/ethclient" | ||
"github.com/ava-labs/subnet-evm/rpc" | ||
) | ||
|
||
var ErrInvalidEndpoint = errors.New("invalid rpc endpoint") | ||
|
||
type Client ethclient.Client | ||
|
||
// DialWithContext returns an ethclient.Client with the internal RPC client configured with the provided options. | ||
func DialWithConfig(ctx context.Context, baseURL string, httpHeaders, queryParams map[string]string) (Client, error) { | ||
url, err := addQueryParams(baseURL, queryParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client, err := rpc.DialOptions(ctx, url, newClientHeaderOptions(httpHeaders)...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ethclient.NewClient(client), nil | ||
} | ||
|
||
// addQueryParams adds the query parameters to the url | ||
func addQueryParams(endpoint string, queryParams map[string]string) (string, error) { | ||
uri, err := url.ParseRequestURI(endpoint) | ||
if err != nil { | ||
return "", fmt.Errorf("%w: %v", ErrInvalidEndpoint, err) | ||
} | ||
values := uri.Query() | ||
for key, value := range queryParams { | ||
values.Add(key, value) | ||
} | ||
uri.RawQuery = values.Encode() | ||
return uri.String(), nil | ||
} | ||
|
||
// newClientOptions creates a ClientOption slice from httpHeaders | ||
func newClientHeaderOptions(httpHeaders map[string]string) []rpc.ClientOption { | ||
opts := make([]rpc.ClientOption, 0, len(httpHeaders)) | ||
for key, value := range httpHeaders { | ||
opts = append(opts, rpc.WithHeader(key, value)) | ||
} | ||
return opts | ||
} |
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 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package ethclient | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddQueryParams(t *testing.T) { | ||
t.Run("NoQueryParams", func(t *testing.T) { | ||
newurl, err := addQueryParams("https://avalabs.com", nil) | ||
require.NoError(t, err) | ||
require.Equal(t, "https://avalabs.com", newurl) | ||
}) | ||
t.Run("TwoQueryParams", func(t *testing.T) { | ||
newurl, err := addQueryParams("https://avalabs.com", map[string]string{ | ||
"first": "value1", | ||
"second": "value2", | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, "https://avalabs.com?first=value1&second=value2", newurl) | ||
}) | ||
t.Run("InvalidEndpoint", func(t *testing.T) { | ||
_, err := addQueryParams("invalid-endpoint", nil) | ||
require.True(t, errors.Is(err, ErrInvalidEndpoint)) | ||
}) | ||
} | ||
|
||
func TestNewClientOptions(t *testing.T) { | ||
t.Run("NoHttpHeaders", func(t *testing.T) { | ||
opts := newClientHeaderOptions(nil) | ||
require.Len(t, opts, 0) | ||
}) | ||
t.Run("TwoHttpHeaders", func(t *testing.T) { | ||
opts := newClientHeaderOptions(map[string]string{ | ||
"first": "value1", | ||
"second": "value2", | ||
}) | ||
require.Len(t, opts, 2) | ||
}) | ||
} |
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.