From 8ff149ebfb993373dbf70fb9c054243e7e63f368 Mon Sep 17 00:00:00 2001 From: Marcin Gorzynski Date: Tue, 19 Dec 2023 16:38:47 +0100 Subject: [PATCH] Refactor relayer package --- relayer.go | 4 +++ relayer/local_relayer.go | 6 ++++ relayer/proto/relayer.go | 73 ---------------------------------------- relayer/relayer.go | 73 ++++++++++++++++++++++++++++++++++++++++ relayer/rpc_relayer.go | 6 +++- 5 files changed, 88 insertions(+), 74 deletions(-) delete mode 100644 relayer/proto/relayer.go diff --git a/relayer.go b/relayer.go index 0a535c99..75832dec 100644 --- a/relayer.go +++ b/relayer.go @@ -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 { @@ -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() } diff --git a/relayer/local_relayer.go b/relayer/local_relayer.go index 51e56ebb..8332017d 100644 --- a/relayer/local_relayer.go +++ b/relayer/local_relayer.go @@ -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 @@ -238,3 +239,8 @@ func (r *LocalRelayer) IsDeployTransaction(signedTxs *sequence.SignedTransaction } return false } + +func (r *LocalRelayer) Client() proto.Relayer { + // no relayer used + return nil +} diff --git a/relayer/proto/relayer.go b/relayer/proto/relayer.go deleted file mode 100644 index 2a03d4c5..00000000 --- a/relayer/proto/relayer.go +++ /dev/null @@ -1,73 +0,0 @@ -package proto - -import ( - "fmt" - "net/http" - - "github.com/0xsequence/go-sequence" -) - -type Options struct { - HTTPClient HTTPClient - JWTAuthToken string -} - -func NewRelayer(relayerServiceURL string, projectAccessKey string, options ...Options) 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 NewRelayerClient(relayerServiceURL, client) -} - -type httpclient struct { - client 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 ETHTxnStatus - ethTxnStatus.UnmarshalJSON([]byte(s)) - - switch ethTxnStatus { - case ETHTxnStatus_UNKNOWN: - return sequence.MetaTxnStatusUnknown - case ETHTxnStatus_DROPPED: - return sequence.MetaTxnStatusUnknown - case ETHTxnStatus_QUEUED: - return sequence.MetaTxnStatusUnknown - case ETHTxnStatus_SENT: - return sequence.MetaTxnStatusUnknown - case ETHTxnStatus_SUCCEEDED: - return sequence.MetaTxnExecuted - case ETHTxnStatus_PARTIALLY_FAILED: - return sequence.MetaTxnFailed - case ETHTxnStatus_FAILED: - return sequence.MetaTxnFailed - default: - return sequence.MetaTxnStatusUnknown - } -} diff --git a/relayer/relayer.go b/relayer/relayer.go index dfd65c3e..94d93801 100644 --- a/relayer/relayer.go +++ b/relayer/relayer.go @@ -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 + } +} diff --git a/relayer/rpc_relayer.go b/relayer/rpc_relayer.go index 2e49bc8a..37c6c952 100644 --- a/relayer/rpc_relayer.go +++ b/relayer/rpc_relayer.go @@ -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 } } @@ -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