github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/wrapped/wrapped.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wrapped
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"math/big"
    11  
    12  	"github.com/ethereum/go-ethereum"
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/ethersphere/bee/v2/pkg/transaction"
    16  )
    17  
    18  var (
    19  	_ transaction.Backend = (*wrappedBackend)(nil)
    20  )
    21  
    22  type wrappedBackend struct {
    23  	backend transaction.Backend
    24  	metrics metrics
    25  }
    26  
    27  func NewBackend(backend transaction.Backend) transaction.Backend {
    28  	return &wrappedBackend{
    29  		backend: backend,
    30  		metrics: newMetrics(),
    31  	}
    32  }
    33  
    34  func (b *wrappedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
    35  	b.metrics.TotalRPCCalls.Inc()
    36  	b.metrics.TransactionReceiptCalls.Inc()
    37  	receipt, err := b.backend.TransactionReceipt(ctx, txHash)
    38  	if err != nil {
    39  		if !errors.Is(err, ethereum.NotFound) {
    40  			b.metrics.TotalRPCErrors.Inc()
    41  		}
    42  		return nil, err
    43  	}
    44  	return receipt, nil
    45  }
    46  
    47  func (b *wrappedBackend) TransactionByHash(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error) {
    48  	b.metrics.TotalRPCCalls.Inc()
    49  	b.metrics.TransactionCalls.Inc()
    50  	tx, isPending, err := b.backend.TransactionByHash(ctx, hash)
    51  	if err != nil {
    52  		if !errors.Is(err, ethereum.NotFound) {
    53  			b.metrics.TotalRPCErrors.Inc()
    54  		}
    55  		return nil, false, err
    56  	}
    57  	return tx, isPending, err
    58  }
    59  
    60  func (b *wrappedBackend) BlockNumber(ctx context.Context) (uint64, error) {
    61  	b.metrics.TotalRPCCalls.Inc()
    62  	b.metrics.BlockNumberCalls.Inc()
    63  	blockNumber, err := b.backend.BlockNumber(ctx)
    64  	if err != nil {
    65  		b.metrics.TotalRPCErrors.Inc()
    66  		return 0, err
    67  	}
    68  	return blockNumber, nil
    69  }
    70  
    71  func (b *wrappedBackend) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
    72  	b.metrics.TotalRPCCalls.Inc()
    73  	b.metrics.BlockHeaderCalls.Inc()
    74  	header, err := b.backend.HeaderByNumber(ctx, number)
    75  	if err != nil {
    76  		if !errors.Is(err, ethereum.NotFound) {
    77  			b.metrics.TotalRPCErrors.Inc()
    78  		}
    79  		return nil, err
    80  	}
    81  	return header, nil
    82  }
    83  
    84  func (b *wrappedBackend) BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
    85  	b.metrics.TotalRPCCalls.Inc()
    86  	b.metrics.BalanceCalls.Inc()
    87  	balance, err := b.backend.BalanceAt(ctx, address, block)
    88  	if err != nil {
    89  		b.metrics.TotalRPCErrors.Inc()
    90  		return nil, err
    91  	}
    92  	return balance, nil
    93  }
    94  
    95  func (b *wrappedBackend) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
    96  	b.metrics.TotalRPCCalls.Inc()
    97  	b.metrics.NonceAtCalls.Inc()
    98  	nonce, err := b.backend.NonceAt(ctx, account, blockNumber)
    99  	if err != nil {
   100  		b.metrics.TotalRPCErrors.Inc()
   101  		return 0, err
   102  	}
   103  	return nonce, nil
   104  }
   105  
   106  func (b *wrappedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
   107  	b.metrics.TotalRPCCalls.Inc()
   108  	b.metrics.CodeAtCalls.Inc()
   109  	code, err := b.backend.CodeAt(ctx, contract, blockNumber)
   110  	if err != nil {
   111  		b.metrics.TotalRPCErrors.Inc()
   112  		return nil, err
   113  	}
   114  	return code, nil
   115  }
   116  
   117  func (b *wrappedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
   118  	b.metrics.TotalRPCCalls.Inc()
   119  	b.metrics.CallContractCalls.Inc()
   120  	result, err := b.backend.CallContract(ctx, call, blockNumber)
   121  	if err != nil {
   122  		b.metrics.TotalRPCErrors.Inc()
   123  		return nil, err
   124  	}
   125  	return result, nil
   126  }
   127  
   128  func (b *wrappedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
   129  	b.metrics.TotalRPCCalls.Inc()
   130  	b.metrics.PendingNonceCalls.Inc()
   131  	nonce, err := b.backend.PendingNonceAt(ctx, account)
   132  	if err != nil {
   133  		b.metrics.TotalRPCErrors.Inc()
   134  		return 0, err
   135  	}
   136  	return nonce, nil
   137  }
   138  
   139  func (b *wrappedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
   140  	b.metrics.TotalRPCCalls.Inc()
   141  	b.metrics.SuggestGasPriceCalls.Inc()
   142  	gasPrice, err := b.backend.SuggestGasPrice(ctx)
   143  	if err != nil {
   144  		b.metrics.TotalRPCErrors.Inc()
   145  		return nil, err
   146  	}
   147  	return gasPrice, nil
   148  }
   149  
   150  func (b *wrappedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
   151  	b.metrics.TotalRPCCalls.Inc()
   152  	b.metrics.SuggestGasPriceCalls.Inc()
   153  	gasTipCap, err := b.backend.SuggestGasTipCap(ctx)
   154  	if err != nil {
   155  		b.metrics.TotalRPCErrors.Inc()
   156  		return nil, err
   157  	}
   158  	return gasTipCap, nil
   159  }
   160  
   161  func (b *wrappedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
   162  	b.metrics.TotalRPCCalls.Inc()
   163  	b.metrics.EstimateGasCalls.Inc()
   164  	gas, err = b.backend.EstimateGas(ctx, call)
   165  	if err != nil {
   166  		b.metrics.TotalRPCErrors.Inc()
   167  		return 0, err
   168  	}
   169  	return gas, nil
   170  }
   171  
   172  func (b *wrappedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
   173  	b.metrics.TotalRPCCalls.Inc()
   174  	b.metrics.SendTransactionCalls.Inc()
   175  	err := b.backend.SendTransaction(ctx, tx)
   176  	if err != nil {
   177  		b.metrics.TotalRPCErrors.Inc()
   178  		return err
   179  	}
   180  	return nil
   181  }
   182  
   183  func (b *wrappedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
   184  	b.metrics.TotalRPCCalls.Inc()
   185  	b.metrics.FilterLogsCalls.Inc()
   186  	logs, err := b.backend.FilterLogs(ctx, query)
   187  	if err != nil {
   188  		b.metrics.TotalRPCErrors.Inc()
   189  		return nil, err
   190  	}
   191  	return logs, nil
   192  }
   193  
   194  func (b *wrappedBackend) ChainID(ctx context.Context) (*big.Int, error) {
   195  	b.metrics.TotalRPCCalls.Inc()
   196  	b.metrics.ChainIDCalls.Inc()
   197  	chainID, err := b.backend.ChainID(ctx)
   198  	if err != nil {
   199  		b.metrics.TotalRPCErrors.Inc()
   200  		return nil, err
   201  	}
   202  	return chainID, nil
   203  }
   204  
   205  func (b *wrappedBackend) Close() {
   206  	b.backend.Close()
   207  }