Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor relayer package #104

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/0xsequence/ethkit/go-ethereum/core/types"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/core"
"github.com/0xsequence/go-sequence/relayer/proto"
)

type RelayerSimulateResult struct {
Expand Down Expand Up @@ -47,6 +48,9 @@ type Relayer interface {
// ..
Wait(ctx context.Context, metaTxnID MetaTxnID, optTimeout ...time.Duration) (MetaTxnStatus, *types.Receipt, error)

// ..
Client() proto.Relayer

// TODO, in future when needed..
// GasRefundOptions()
}
Expand Down
6 changes: 6 additions & 0 deletions relayer/local_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xsequence/go-sequence"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/core"
"github.com/0xsequence/go-sequence/relayer/proto"
)

// LocalRelayer is a simple implementation of a relayer which will dispatch
Expand Down Expand Up @@ -238,3 +239,8 @@ func (r *LocalRelayer) IsDeployTransaction(signedTxs *sequence.SignedTransaction
}
return false
}

func (r *LocalRelayer) Client() proto.Relayer {
// no relayer used
return nil
}
73 changes: 0 additions & 73 deletions relayer/proto/relayer.go

This file was deleted.

73 changes: 73 additions & 0 deletions relayer/relayer.go
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
package relayer

import (
"fmt"
"net/http"

"github.com/0xsequence/go-sequence"
"github.com/0xsequence/go-sequence/relayer/proto"
)

type Options struct {
HTTPClient proto.HTTPClient
JWTAuthToken string
}

func NewRelayerClient(relayerServiceURL string, projectAccessKey string, options ...Options) proto.Relayer {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}

client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

return proto.NewRelayerClient(relayerServiceURL, client)
}

type httpclient struct {
client proto.HTTPClient
jwtAuthHeader string
projectAccessKey string
}

func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}

func MetaTxnStatusFromString(s string) sequence.MetaTxnStatus {
var ethTxnStatus proto.ETHTxnStatus
ethTxnStatus.UnmarshalJSON([]byte(s))

switch ethTxnStatus {
case proto.ETHTxnStatus_UNKNOWN:
return sequence.MetaTxnStatusUnknown
case proto.ETHTxnStatus_DROPPED:
return sequence.MetaTxnStatusUnknown
case proto.ETHTxnStatus_QUEUED:
return sequence.MetaTxnStatusUnknown
case proto.ETHTxnStatus_SENT:
return sequence.MetaTxnStatusUnknown
case proto.ETHTxnStatus_SUCCEEDED:
return sequence.MetaTxnExecuted
case proto.ETHTxnStatus_PARTIALLY_FAILED:
return sequence.MetaTxnFailed
case proto.ETHTxnStatus_FAILED:
return sequence.MetaTxnFailed
default:
return sequence.MetaTxnStatusUnknown
}
}
6 changes: 5 additions & 1 deletion relayer/rpc_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (r *RpcRelayer) waitMetaTxnReceipt(ctx context.Context, metaTxnID sequence.
if err != nil {
return 0, nil, fmt.Errorf("failed to decode txn receipt data: %w", err)
}
return proto.MetaTxnStatusFromString(metaTxnReceipt.Status), receipt, nil
return MetaTxnStatusFromString(metaTxnReceipt.Status), receipt, nil
}
}

Expand All @@ -269,6 +269,10 @@ func (r *RpcRelayer) IsDeployTransaction(signedTxs *sequence.SignedTransactions)
return false
}

func (r *RpcRelayer) Client() proto.Relayer {
return r.Service
}

func (r *RpcRelayer) protoConfig(ctx context.Context, config core.WalletConfig, walletAddress common.Address) (*proto.WalletConfig, error) {
if walletConfigV1, ok := config.(*v1.WalletConfig); ok {
var signers []*proto.WalletSigner
Expand Down
Loading