Skip to content

Commit

Permalink
relayer: helper for txn listening
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Dec 2, 2023
1 parent 6ad0779 commit 30cb84d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
4 changes: 4 additions & 0 deletions relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type Relayer interface {

type MetaTxnID string

func (id MetaTxnID) String() string {
return string(id)
}

type MetaTxnStatus uint8

const (
Expand Down
26 changes: 26 additions & 0 deletions relayer/proto/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package proto
import (
"fmt"
"net/http"

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

type Options struct {
Expand Down Expand Up @@ -45,3 +47,27 @@ func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
}
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
}
}
48 changes: 44 additions & 4 deletions relayer/rpc_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package relayer

import (
"context"
"encoding/json"
"fmt"
"math/big"
"net/url"
Expand Down Expand Up @@ -167,13 +168,12 @@ func (r *RpcRelayer) Relay(ctx context.Context, signedTxs *sequence.SignedTransa

// ....
func (r *RpcRelayer) Wait(ctx context.Context, metaTxnID sequence.MetaTxnID, optTimeout ...time.Duration) (sequence.MetaTxnStatus, *types.Receipt, error) {
// Fetch the meta transaction receipt from the relayer service
if r.receiptListener == nil {
return 0, nil, fmt.Errorf("relayer: failed to wait for metaTxnID as receiptListener is not set")
return r.waitMetaTxnReceipt(ctx, metaTxnID, optTimeout...)
}

// TODO: call rpcRelayer host RPC method GetMetaTxnReceipt()
// which in the future will be renamed to WaitTransactionReceipt()

// Fetch the meta transaction receipt from the receipt listener
result, receipt, _, err := sequence.FetchMetaTransactionReceipt(ctx, r.receiptListener, metaTxnID, optTimeout...)
if err != nil {
return 0, nil, err
Expand All @@ -185,6 +185,46 @@ func (r *RpcRelayer) Wait(ctx context.Context, metaTxnID sequence.MetaTxnID, opt
return status, receipt.Receipt(), nil
}

func (r *RpcRelayer) waitMetaTxnReceipt(ctx context.Context, metaTxnID sequence.MetaTxnID, optTimeout ...time.Duration) (sequence.MetaTxnStatus, *types.Receipt, error) {
// TODO: in future GetMetaTxnReceipt() will be renamed to WaitTransactionReceipt()

var clear context.CancelFunc
if len(optTimeout) > 0 {
ctx, clear = context.WithTimeout(ctx, optTimeout[0])
defer clear()
}

for {
select {
case <-ctx.Done():
err := ctx.Err()
if err != nil {
return 0, nil, err
}
return 0, nil, nil
default:
}

metaTxnReceipt, err := r.Service.GetMetaTxnReceipt(ctx, metaTxnID.String())
if metaTxnReceipt == nil && err == nil {
// currently we assume that if the receipt is nil, and error is nil, then
// we're still searching for the transaction. This is a hack, and we should
// use proper error codes when we upgrade webrpc version
continue
}
if err != nil {
return sequence.MetaTxnStatusUnknown, nil, err
}
txnReceipt := metaTxnReceipt.TxnReceipt
var receipt *types.Receipt
err = json.Unmarshal([]byte(txnReceipt), &receipt)
if err != nil {
return 0, nil, fmt.Errorf("failed to decode txn receipt data: %w", err)
}
return proto.MetaTxnStatusFromString(metaTxnReceipt.Status), receipt, nil
}
}

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

0 comments on commit 30cb84d

Please sign in to comment.