From 9e1c68102d42e4fcc027a7e1b1039af03ee58a39 Mon Sep 17 00:00:00 2001 From: William Hua Date: Tue, 4 Mar 2025 12:12:57 -0500 Subject: [PATCH] remove RelayV3 --- relayer.go | 1 - relayer/local_relayer.go | 178 ++++++++++++++------------------------- relayer/rpc_relayer.go | 99 ++++++---------------- wallet.go | 41 ++------- 4 files changed, 94 insertions(+), 225 deletions(-) diff --git a/relayer.go b/relayer.go index 543660de..2a484d31 100644 --- a/relayer.go +++ b/relayer.go @@ -70,7 +70,6 @@ type Relayer interface { // responds with the native transaction hash (*types.Transaction), which means the relayer has submitted the transaction // request to the network. Clients can use WaitReceipt to wait until the metaTxnID has been mined. Relay(ctx context.Context, signedTxs *SignedTransactions, quote ...*RelayerFeeQuote) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) - RelayV3(ctx context.Context, signedTxs *SignedTransactions, quote ...*RelayerFeeQuote) (MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) // FeeOptions(ctx context.Context, signedTxs *SignedTransactions) ([]*RelayerFeeOption, *RelayerFeeQuote, error) diff --git a/relayer/local_relayer.go b/relayer/local_relayer.go index 6b8fb25e..7bab2f59 100644 --- a/relayer/local_relayer.go +++ b/relayer/local_relayer.go @@ -16,6 +16,9 @@ import ( "github.com/0xsequence/go-sequence" "github.com/0xsequence/go-sequence/contracts" "github.com/0xsequence/go-sequence/core" + v1 "github.com/0xsequence/go-sequence/core/v1" + v2 "github.com/0xsequence/go-sequence/core/v2" + v3 "github.com/0xsequence/go-sequence/core/v3" "github.com/0xsequence/go-sequence/relayer/proto" ) @@ -132,106 +135,33 @@ func (r *LocalRelayer) Relay(ctx context.Context, signedTxs *sequence.SignedTran sender := r.Sender - to, execdata, err := sequence.EncodeTransactionsForRelaying( - r, - signedTxs.WalletAddress, - signedTxs.WalletConfig, - signedTxs.WalletContext, - signedTxs.Transactions, - signedTxs.Nonce, - signedTxs.Signature, - ) - if err != nil { - return "", nil, nil, err - } - - if r.IsDeployTransaction(signedTxs) { - to = signedTxs.WalletContext.GuestModuleAddress - } else { - isDeployed, err := sequence.IsWalletDeployed(r.GetProvider(), to) - if err != nil { - return "", nil, nil, err - } - - if !isDeployed { - _, factoryAddress, deployData, err := sequence.EncodeWalletDeployment(signedTxs.WalletConfig, signedTxs.WalletContext) - if err != nil { - return "", nil, nil, err - } - - txns := sequence.Transactions{ - { - RevertOnError: true, - To: factoryAddress, - Data: deployData, - }, - { - RevertOnError: true, - To: to, - Data: execdata, - }, - } - - encodedTxns, err := txns.EncodedTransactions() - if err != nil { - return "", nil, nil, err - } - - // TODO: v1 ...? what about others..? I guess since its just for testing locally, - // we can just use v1. But I'd suggest we use v3 format once its ready. - execdata, err = contracts.V1.WalletMainModule.Encode("execute", encodedTxns, big.NewInt(0), []byte{}) - if err != nil { - return "", nil, nil, err - } - - to = signedTxs.WalletContext.GuestModuleAddress - } - } - - walletAddress, err := sequence.AddressFromWalletConfig(signedTxs.WalletConfig, signedTxs.WalletContext) - if err != nil { - return "", nil, nil, err - } - - metaTxnID, _, err := sequence.ComputeMetaTxnID(signedTxs.ChainID, walletAddress, signedTxs.Transactions, signedTxs.Nonce, sequence.MetaTxnWalletExec) - if err != nil { - return "", nil, nil, err - } - - ntx, err := sender.NewTransaction(ctx, ðtxn.TransactionRequest{ - To: &to, Data: execdata, - }) - if err != nil { - return metaTxnID, nil, nil, err - } - - signedTx, err := sender.SignTx(ntx, signedTxs.ChainID) - if err != nil { - return metaTxnID, nil, nil, err - } - - ntx, waitReceipt, err := sender.SendTransaction(ctx, signedTx) - if err != nil { - return metaTxnID, nil, nil, err + var to common.Address + var execdata []byte + var err error + switch signedTxs.WalletConfig.(type) { + case *v1.WalletConfig, *v2.WalletConfig: + to, execdata, err = sequence.EncodeTransactionsForRelaying( + r, + signedTxs.WalletAddress, + signedTxs.WalletConfig, + signedTxs.WalletContext, + signedTxs.Transactions, + signedTxs.Nonce, + signedTxs.Signature, + ) + case *v3.WalletConfig: + to, execdata, err = sequence.EncodeTransactionsForRelayingV3( + r, + signedTxs.WalletAddress, + signedTxs.ChainID, + signedTxs.WalletConfig, + signedTxs.WalletContext, + signedTxs.Transactions, + signedTxs.Space, + signedTxs.Nonce, + signedTxs.Signature, + ) } - - return metaTxnID, ntx, waitReceipt, nil -} - -func (r *LocalRelayer) RelayV3(ctx context.Context, signedTxs *sequence.SignedTransactions, quote ...*sequence.RelayerFeeQuote) (sequence.MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) { - sender := r.Sender - - to, execdata, err := sequence.EncodeTransactionsForRelayingV3( - r, - signedTxs.WalletAddress, - signedTxs.ChainID, - signedTxs.WalletConfig, - signedTxs.WalletContext, - signedTxs.Transactions, - signedTxs.Space, - signedTxs.Nonce, - signedTxs.Signature, - ) if err != nil { return "", nil, nil, err } @@ -263,24 +193,38 @@ func (r *LocalRelayer) RelayV3(ctx context.Context, signedTxs *sequence.SignedTr }, } - _, execdata, err = sequence.EncodeTransactionsForRelayingV3( - r, - signedTxs.WalletAddress, - signedTxs.ChainID, - signedTxs.WalletConfig, - signedTxs.WalletContext, - txns, - signedTxs.Space, - signedTxs.Nonce, - signedTxs.Signature, - ) - if err != nil { - return "", nil, nil, err - } - - execdata, err = contracts.V3.WalletStage1Module.Encode("execute", execdata, []byte{}) - if err != nil { - return "", nil, nil, err + switch signedTxs.WalletConfig.(type) { + case *v1.WalletConfig, *v2.WalletConfig: + encodedTxns, err := txns.EncodedTransactions() + if err != nil { + return "", nil, nil, err + } + + execdata, err = contracts.V1.WalletMainModule.Encode("execute", encodedTxns, big.NewInt(0), []byte{}) + if err != nil { + return "", nil, nil, err + } + + case *v3.WalletConfig: + _, execdata, err = sequence.EncodeTransactionsForRelayingV3( + r, + signedTxs.WalletAddress, + signedTxs.ChainID, + signedTxs.WalletConfig, + signedTxs.WalletContext, + txns, + signedTxs.Space, + signedTxs.Nonce, + signedTxs.Signature, + ) + if err != nil { + return "", nil, nil, err + } + + execdata, err = contracts.V3.WalletStage1Module.Encode("execute", execdata, []byte{}) + if err != nil { + return "", nil, nil, err + } } to = signedTxs.WalletContext.GuestModuleAddress diff --git a/relayer/rpc_relayer.go b/relayer/rpc_relayer.go index 89a0825e..d2c06095 100644 --- a/relayer/rpc_relayer.go +++ b/relayer/rpc_relayer.go @@ -17,6 +17,9 @@ import ( "github.com/0xsequence/ethkit/go-ethereum/core/types" "github.com/0xsequence/go-sequence" "github.com/0xsequence/go-sequence/core" + v1 "github.com/0xsequence/go-sequence/core/v1" + v2 "github.com/0xsequence/go-sequence/core/v2" + v3 "github.com/0xsequence/go-sequence/core/v3" "github.com/0xsequence/go-sequence/relayer/proto" ) @@ -155,78 +158,32 @@ func (r *RpcRelayer) Relay(ctx context.Context, signedTxs *sequence.SignedTransa } } - to, execdata, err := sequence.EncodeTransactionsForRelaying( - r, - signedTxs.WalletAddress, - signedTxs.WalletConfig, - signedTxs.WalletContext, - signedTxs.Transactions, - signedTxs.Nonce, - signedTxs.Signature, - ) - if err != nil { - return "", nil, nil, err - } - - if r.IsDeployTransaction(signedTxs) { - to = signedTxs.WalletContext.GuestModuleAddress - } - - call := &proto.MetaTxn{ - Contract: to.Hex(), - Input: hexutil.Encode(execdata), - WalletAddress: walletAddress.Hex(), - } - - var txQuote *string - if len(quote) > 0 { - txQuote = (*string)(quote[0]) - } - - // TODO: check contents of Contract and input, if empty, lets not even bother asking the server.. - - ok, metaTxnID, err := r.Service.SendMetaTxn(ctx, call, txQuote, nil) - if err != nil { - return sequence.MetaTxnID(metaTxnID), nil, nil, err + var to common.Address + var execdata []byte + switch signedTxs.WalletConfig.(type) { + case *v1.WalletConfig, *v2.WalletConfig: + to, execdata, err = sequence.EncodeTransactionsForRelaying( + r, + signedTxs.WalletAddress, + signedTxs.WalletConfig, + signedTxs.WalletContext, + signedTxs.Transactions, + signedTxs.Nonce, + signedTxs.Signature, + ) + case *v3.WalletConfig: + to, execdata, err = sequence.EncodeTransactionsForRelayingV3( + r, + signedTxs.WalletAddress, + signedTxs.ChainID, + signedTxs.WalletConfig, + signedTxs.WalletContext, + signedTxs.Transactions, + signedTxs.Space, + signedTxs.Nonce, + signedTxs.Signature, + ) } - if !ok { - return sequence.MetaTxnID(metaTxnID), nil, nil, fmt.Errorf("failed to relay meta transaction: unknown reason") - } - if metaTxnID == "" { - return "", nil, nil, fmt.Errorf("failed to relay meta transaction: server returned empty metaTxnID") - } - - waitReceipt := func(ctx context.Context) (*types.Receipt, error) { - // NOTE: to timeout the request, pass a ctx from context.WithTimeout - _, receipt, err := r.Wait(ctx, sequence.MetaTxnID(metaTxnID)) - return receipt, err - } - - return sequence.MetaTxnID(metaTxnID), nil, waitReceipt, nil -} - -func (r *RpcRelayer) RelayV3(ctx context.Context, signedTxs *sequence.SignedTransactions, quote ...*sequence.RelayerFeeQuote) (sequence.MetaTxnID, *types.Transaction, ethtxn.WaitReceipt, error) { - walletAddress := signedTxs.WalletAddress - var err error - - if walletAddress == (common.Address{}) { - walletAddress, err = sequence.AddressFromWalletConfig(signedTxs.WalletConfig, signedTxs.WalletContext) - if err != nil { - return "", nil, nil, err - } - } - - to, execdata, err := sequence.EncodeTransactionsForRelayingV3( - r, - signedTxs.WalletAddress, - signedTxs.ChainID, - signedTxs.WalletConfig, - signedTxs.WalletContext, - signedTxs.Transactions, - signedTxs.Space, - signedTxs.Nonce, - signedTxs.Signature, - ) if err != nil { return "", nil, nil, err } diff --git a/wallet.go b/wallet.go index 3d8683b7..ed21d050 100644 --- a/wallet.go +++ b/wallet.go @@ -659,13 +659,8 @@ func (w *Wallet[C]) SignTransactions(ctx context.Context, txns Transactions) (*S } } - // Check if the config is v1, v2, or v3 - _, isV1 := core.WalletConfig(w.config).(*v1.WalletConfig) - _, isV2 := core.WalletConfig(w.config).(*v2.WalletConfig) - _, isV3 := core.WalletConfig(w.config).(*v3.WalletConfig) - - // If the config is v1 or v2, encode the transactions in a legacy behavior - if isV1 || isV2 { + switch core.WalletConfig(w.config).(type) { + case *v1.WalletConfig, *v2.WalletConfig: bundle := Transaction{ Transactions: txns, Nonce: nonce, @@ -693,10 +688,8 @@ func (w *Wallet[C]) SignTransactions(ctx context.Context, txns Transactions) (*S Digest: digest, Signature: sig, }, nil - } - // If the config is v3, encode the transactions as a v3 transaction - if isV3 { + case *v3.WalletConfig: // TODO: Hardcode nonce space to 0 for now space := big.NewInt(0) @@ -738,19 +731,7 @@ func (w *Wallet[C]) SendTransactions(ctx context.Context, signedTxns *SignedTran return "", nil, nil, ErrRelayerNotSet } - _, isV1 := core.WalletConfig(w.config).(*v1.WalletConfig) - _, isV2 := core.WalletConfig(w.config).(*v2.WalletConfig) - _, isV3 := core.WalletConfig(w.config).(*v3.WalletConfig) - - if isV1 || isV2 { - return w.relayer.Relay(ctx, signedTxns, feeQuote...) - } - - if isV3 { - return w.relayer.RelayV3(ctx, signedTxns, feeQuote...) - } - - return "", nil, nil, fmt.Errorf("unknown wallet config type") + return w.relayer.Relay(ctx, signedTxns, feeQuote...) } func (w *Wallet[C]) FeeOptions(ctx context.Context, txs Transactions) ([]*RelayerFeeOption, *RelayerFeeQuote, error) { @@ -854,19 +835,7 @@ func (w *Wallet[C]) Deploy(ctx context.Context) (MetaTxnID, *types.Transaction, return "", nil, nil, err } - _, isV1 := core.WalletConfig(w.config).(*v1.WalletConfig) - _, isV2 := core.WalletConfig(w.config).(*v2.WalletConfig) - _, isV3 := core.WalletConfig(w.config).(*v3.WalletConfig) - - if isV1 || isV2 { - return w.relayer.Relay(ctx, signerTxn) - } - - if isV3 { - return w.relayer.RelayV3(ctx, signerTxn) - } - - return "", nil, nil, fmt.Errorf("unknown wallet config type") + return w.relayer.Relay(ctx, signerTxn) } // func (w *Wallet) UpdateConfig() // TODO in future