Skip to content

Commit

Permalink
Create parity_rpc client that replace input -> data field
Browse files Browse the repository at this point in the history
  • Loading branch information
SvineruS committed Mar 14, 2024
1 parent 2464a6c commit c2ff4a5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion relay/internal/networks/amb/amb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
nc "github.com/ambrosus/ambrosus-bridge/relay/internal/networks/common"
"github.com/ambrosus/ambrosus-bridge/relay/internal/networks/events"
"github.com/ambrosus/ambrosus-bridge/relay/pkg/ethclients/parity"
"github.com/ambrosus/ambrosus-bridge/relay/pkg/ethclients/parity/parity_rpc"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog"
Expand All @@ -34,7 +35,7 @@ func New(cfg *config.Network, sideBridgeName string, eventsApi events.Events, ba
// ///////////////////
origin := nc.GetAmbrosusOrigin()

rpcHTTPClient, err := rpc.DialHTTP(cfg.HttpURL)
rpcHTTPClient, err := parity_rpc.DialHTTP(cfg.HttpURL)
if err != nil {
return nil, fmt.Errorf("dial http: %w", err)
}
Expand Down
39 changes: 39 additions & 0 deletions relay/pkg/ethclients/parity/parity_rpc/parity_rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package parity_rpc

import (
"bytes"
"io"
"net/http"
"time"

"github.com/ethereum/go-ethereum/rpc"
)

// DialHTTP creates a new RPC client that connects to an RPC server over HTTP with default rate limiter options.
func DialHTTP(endpoint string) (*rpc.Client, error) {
httpClient := new(http.Client)
httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport}
return rpc.DialHTTPWithClient(endpoint, httpClient)
}

// DialHTTPWithLimitOptions creates a new RPC client that connects to an RPC server over HTTP with specified rate limiter options.
func DialHTTPWithLimitOptions(endpoint string, limitPeriod time.Duration, requestCount int) (*rpc.Client, error) {
httpClient := new(http.Client)
httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport}
return rpc.DialHTTPWithClient(endpoint, httpClient)
}

type ReplaceInputToDataTransporter struct {
roundTripperWrap http.RoundTripper
}

func (c *ReplaceInputToDataTransporter) RoundTrip(r *http.Request) (*http.Response, error) {
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
bodyBytes = bytes.Replace(bodyBytes, []byte(`"input"`), []byte(`"data"`), -1)
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
r.ContentLength = int64(len(bodyBytes))
return c.roundTripperWrap.RoundTrip(r)
}

0 comments on commit c2ff4a5

Please sign in to comment.