Skip to content

Commit

Permalink
add extract rpc error data helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 19, 2024
1 parent 95e93ca commit f389895
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
1 change: 0 additions & 1 deletion bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestBridgeEventE2E(t *testing.T) {
bridge.OriginAddress,
true, nil,
)
t.Logf("BridgeAsset err: %+v", err)
require.NoError(t, err)
helpers.CommitBlocks(t, env.L1Client, 1, blockTime)
bn, err := env.L1Client.Client().BlockNumber(ctx)
Expand Down
38 changes: 14 additions & 24 deletions test/helpers/ethtxmanmock_e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func NewEthTxManMock(

_, err := client.Client().EstimateGas(ctx, msg)
if err != nil {
log.Errorf("eth_estimateGas invocation failed: %+v", err)
log.Errorf("eth_estimateGas invocation failed: %w", ExtractRPCErrorData(err))

res, err := client.Client().CallContract(ctx, msg, nil)
if err != nil {
log.Errorf("eth_call invocation failed: %+v", err)
log.Errorf("eth_call invocation failed: %w", ExtractRPCErrorData(err))
} else {
log.Debugf("contract call result: %s", hex.EncodeToString(res))
}
Expand All @@ -69,11 +69,9 @@ func NewEthTxManMock(

err = SendTx(ctx, client, auth, to, data, common.Big0)
if err != nil {
log.Errorf("failed to send transaction: %s", err)
log.Errorf("failed to send transaction: %w", err)
return
}

client.Commit()
}).
Return(common.Hash{}, nil)
ethTxMock.On("Result", mock.Anything, mock.Anything).
Expand All @@ -92,18 +90,17 @@ func SendTx(ctx context.Context, client *simulated.Backend, auth *bind.TransactO

gas := uint64(21000) //nolint:mnd

Check failure on line 91 in test/helpers/ethtxmanmock_e2e.go

View workflow job for this annotation

GitHub Actions / lint

assigned to gas, but reassigned without using the value (wastedassign)

if len(data) > 0 {
msg := ethereum.CallMsg{
From: auth.From,
To: to,
Data: data,
Value: value,
}

gas, err = client.Client().EstimateGas(ctx, msg)
if err != nil {
return err
}
// if len(data) > 0 {
msg := ethereum.CallMsg{
From: auth.From,
To: to,
Data: data,
Value: value,
}

gas, err = client.Client().EstimateGas(ctx, msg)
if err != nil {
return ExtractRPCErrorData(err)
}

price, err := client.Client().SuggestGasPrice(ctx)
Expand Down Expand Up @@ -142,12 +139,5 @@ func SendTx(ctx context.Context, client *simulated.Backend, auth *bind.TransactO

client.Commit()

receipt, err := client.Client().TransactionReceipt(ctx, signedTx.Hash())
if err != nil {
return fmt.Errorf("transaction failed: %w", err)
}

fmt.Printf("Transaction status: %d\n", receipt.Status)

return nil
}
14 changes: 14 additions & 0 deletions test/helpers/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"errors"
"fmt"
"math/big"
"testing"
Expand Down Expand Up @@ -157,6 +158,7 @@ func NewSimulatedBackend(t *testing.T,
return client, setup
}

// CreateAccount creates new private key and corresponding transaction signer
func CreateAccount(chainID *big.Int) (*bind.TransactOpts, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
Expand All @@ -165,3 +167,15 @@ func CreateAccount(chainID *big.Int) (*bind.TransactOpts, error) {

return bind.NewKeyedTransactorWithChainID(privateKey, chainID)
}

// ExtractRPCErrorData tries to extract the error data from the provided error
func ExtractRPCErrorData(err error) error {
var ed rpc.DataError
if errors.As(err, &ed) {
if eds, ok := ed.ErrorData().(string); ok {
return fmt.Errorf("%w (error data: %s)", err, eds)
}
}

return err
}

0 comments on commit f389895

Please sign in to comment.