2023-02-20 09:32:45 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2023-03-28 16:22:11 +00:00
|
|
|
"strings"
|
2023-03-20 13:02:09 +00:00
|
|
|
"sync"
|
2023-02-20 09:32:45 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
2023-04-27 14:44:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-03-05 09:33:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-03-28 16:22:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/status-im/status-go/services/rpcstats"
|
|
|
|
)
|
|
|
|
|
2023-09-19 11:17:36 +00:00
|
|
|
type BatchCallClient interface {
|
|
|
|
BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
type ClientInterface interface {
|
|
|
|
BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
|
2023-09-19 11:17:36 +00:00
|
|
|
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
|
|
|
|
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
|
|
|
|
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
|
|
|
|
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
|
|
|
|
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
|
|
|
|
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
|
|
|
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
2024-02-21 10:50:46 +00:00
|
|
|
CallBlockHashByTransaction(ctx context.Context, blockNumber *big.Int, index uint) (common.Hash, error)
|
2024-02-23 14:27:08 +00:00
|
|
|
GetBaseFeeFromBlock(ctx context.Context, blockNumber *big.Int) (string, error)
|
2023-09-19 11:17:36 +00:00
|
|
|
NetworkID() uint64
|
|
|
|
ToBigInt() *big.Int
|
2023-09-20 08:41:23 +00:00
|
|
|
CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
|
|
|
|
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
|
|
|
|
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
|
|
|
|
GetWalletNotifier() func(chainId uint64, message string)
|
|
|
|
SetWalletNotifier(notifier func(chainId uint64, message string))
|
|
|
|
TransactionByHash(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error)
|
|
|
|
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
|
|
|
|
BlockNumber(ctx context.Context) (uint64, error)
|
|
|
|
SetIsConnected(value bool)
|
|
|
|
GetIsConnected() bool
|
|
|
|
bind.ContractCaller
|
|
|
|
bind.ContractTransactor
|
|
|
|
bind.ContractFilterer
|
2023-08-01 18:50:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
type ClientWithFallback struct {
|
2024-02-23 14:27:08 +00:00
|
|
|
ChainID uint64
|
|
|
|
main *ethclient.Client
|
|
|
|
fallback *ethclient.Client
|
|
|
|
mainLimiter *RPCLimiter
|
|
|
|
fallbackLimiter *RPCLimiter
|
2023-02-20 09:32:45 +00:00
|
|
|
|
|
|
|
mainRPC *rpc.Client
|
|
|
|
fallbackRPC *rpc.Client
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
WalletNotifier func(chainId uint64, message string)
|
|
|
|
|
2023-03-31 09:20:02 +00:00
|
|
|
IsConnected bool
|
|
|
|
IsConnectedLock sync.RWMutex
|
|
|
|
LastCheckedAt int64
|
2024-03-06 13:49:17 +00:00
|
|
|
|
|
|
|
circuitBreakerCmdName string
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:44:24 +00:00
|
|
|
// Don't mark connection as failed if we get one of these errors
|
|
|
|
var propagateErrors = []error{
|
2023-03-28 16:22:11 +00:00
|
|
|
vm.ErrOutOfGas,
|
|
|
|
vm.ErrCodeStoreOutOfGas,
|
|
|
|
vm.ErrDepth,
|
|
|
|
vm.ErrInsufficientBalance,
|
|
|
|
vm.ErrContractAddressCollision,
|
|
|
|
vm.ErrExecutionReverted,
|
|
|
|
vm.ErrMaxCodeSizeExceeded,
|
|
|
|
vm.ErrInvalidJump,
|
|
|
|
vm.ErrWriteProtection,
|
|
|
|
vm.ErrReturnDataOutOfBounds,
|
|
|
|
vm.ErrGasUintOverflow,
|
|
|
|
vm.ErrInvalidCode,
|
|
|
|
vm.ErrNonceUintOverflow,
|
2023-04-27 14:44:24 +00:00
|
|
|
|
|
|
|
// Used by balance history to check state
|
|
|
|
ethereum.NotFound,
|
|
|
|
bind.ErrNoCode,
|
2023-03-28 16:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandResult struct {
|
2024-03-06 13:49:17 +00:00
|
|
|
res []any
|
|
|
|
err error
|
2023-03-28 16:22:11 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
func NewSimpleClient(mainLimiter *RPCLimiter, main *rpc.Client, chainID uint64) *ClientWithFallback {
|
2024-03-06 13:49:17 +00:00
|
|
|
circuitBreakerCmdName := fmt.Sprintf("ethClient_%d", chainID)
|
|
|
|
hystrix.ConfigureCommand(circuitBreakerCmdName, hystrix.CommandConfig{
|
2023-02-20 09:32:45 +00:00
|
|
|
Timeout: 10000,
|
|
|
|
MaxConcurrentRequests: 100,
|
|
|
|
SleepWindow: 300000,
|
|
|
|
ErrorPercentThreshold: 25,
|
|
|
|
})
|
|
|
|
|
|
|
|
return &ClientWithFallback{
|
2024-03-06 13:49:17 +00:00
|
|
|
ChainID: chainID,
|
|
|
|
main: ethclient.NewClient(main),
|
|
|
|
fallback: nil,
|
|
|
|
mainLimiter: mainLimiter,
|
|
|
|
fallbackLimiter: nil,
|
|
|
|
mainRPC: main,
|
|
|
|
fallbackRPC: nil,
|
|
|
|
IsConnected: true,
|
|
|
|
LastCheckedAt: time.Now().Unix(),
|
|
|
|
circuitBreakerCmdName: circuitBreakerCmdName,
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
func NewClient(mainLimiter *RPCLimiter, main *rpc.Client, fallbackLimiter *RPCLimiter, fallback *rpc.Client, chainID uint64) *ClientWithFallback {
|
2024-03-06 13:49:17 +00:00
|
|
|
circuitBreakerCmdName := fmt.Sprintf("ethClient_%d", chainID)
|
|
|
|
hystrix.ConfigureCommand(circuitBreakerCmdName, hystrix.CommandConfig{
|
2023-03-28 12:46:46 +00:00
|
|
|
Timeout: 20000,
|
2023-02-20 09:32:45 +00:00
|
|
|
MaxConcurrentRequests: 100,
|
|
|
|
SleepWindow: 300000,
|
|
|
|
ErrorPercentThreshold: 25,
|
|
|
|
})
|
|
|
|
|
|
|
|
var fallbackEthClient *ethclient.Client
|
|
|
|
if fallback != nil {
|
|
|
|
fallbackEthClient = ethclient.NewClient(fallback)
|
|
|
|
}
|
|
|
|
return &ClientWithFallback{
|
2024-03-06 13:49:17 +00:00
|
|
|
ChainID: chainID,
|
|
|
|
main: ethclient.NewClient(main),
|
|
|
|
fallback: fallbackEthClient,
|
|
|
|
mainLimiter: mainLimiter,
|
|
|
|
fallbackLimiter: fallbackLimiter,
|
|
|
|
mainRPC: main,
|
|
|
|
fallbackRPC: fallback,
|
|
|
|
IsConnected: true,
|
|
|
|
LastCheckedAt: time.Now().Unix(),
|
|
|
|
circuitBreakerCmdName: circuitBreakerCmdName,
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) Close() {
|
|
|
|
c.main.Close()
|
|
|
|
if c.fallback != nil {
|
|
|
|
c.fallback.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 16:22:11 +00:00
|
|
|
func isVMError(err error) bool {
|
|
|
|
if strings.HasPrefix(err.Error(), "execution reverted") {
|
|
|
|
return true
|
|
|
|
}
|
2024-03-07 11:05:53 +00:00
|
|
|
if strings.Contains(err.Error(), core.ErrInsufficientFunds.Error()) {
|
|
|
|
return true
|
|
|
|
}
|
2023-04-27 14:44:24 +00:00
|
|
|
for _, vmError := range propagateErrors {
|
2024-03-07 11:05:53 +00:00
|
|
|
if err == vmError {
|
2023-03-28 16:22:11 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-03-07 11:05:53 +00:00
|
|
|
|
2023-03-28 16:22:11 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
func isRPSLimitError(err error) bool {
|
|
|
|
return strings.Contains(err.Error(), "backoff_seconds")
|
|
|
|
}
|
|
|
|
|
2023-03-31 08:47:14 +00:00
|
|
|
func (c *ClientWithFallback) SetIsConnected(value bool) {
|
2023-03-20 13:02:09 +00:00
|
|
|
c.IsConnectedLock.Lock()
|
|
|
|
defer c.IsConnectedLock.Unlock()
|
|
|
|
c.LastCheckedAt = time.Now().Unix()
|
2023-03-23 14:08:55 +00:00
|
|
|
if !value {
|
2023-03-31 08:47:14 +00:00
|
|
|
if c.IsConnected {
|
2023-03-23 14:08:55 +00:00
|
|
|
if c.WalletNotifier != nil {
|
|
|
|
c.WalletNotifier(c.ChainID, "down")
|
|
|
|
}
|
|
|
|
c.IsConnected = false
|
2023-03-20 13:02:09 +00:00
|
|
|
}
|
2023-03-23 14:08:55 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if !c.IsConnected {
|
|
|
|
c.IsConnected = true
|
2023-03-23 14:21:02 +00:00
|
|
|
if c.WalletNotifier != nil {
|
|
|
|
c.WalletNotifier(c.ChainID, "up")
|
|
|
|
}
|
2023-03-20 13:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
func (c *ClientWithFallback) GetIsConnected() bool {
|
|
|
|
c.IsConnectedLock.RLock()
|
|
|
|
defer c.IsConnectedLock.RUnlock()
|
|
|
|
return c.IsConnected
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
func (c *ClientWithFallback) makeCall(ctx context.Context, main func() ([]any, error), fallback func() ([]any, error)) ([]any, error) {
|
2023-03-28 16:22:11 +00:00
|
|
|
resultChan := make(chan CommandResult, 1)
|
2023-02-20 09:32:45 +00:00
|
|
|
c.LastCheckedAt = time.Now().Unix()
|
2024-03-06 13:49:17 +00:00
|
|
|
errChan := hystrix.Go(c.circuitBreakerCmdName, func() error {
|
2024-02-23 14:27:08 +00:00
|
|
|
err := c.mainLimiter.WaitForRequestsAvailability(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
res, err := main()
|
|
|
|
if err != nil {
|
2024-02-23 14:27:08 +00:00
|
|
|
if isRPSLimitError(err) {
|
|
|
|
c.mainLimiter.ReduceLimit()
|
2024-03-06 13:49:17 +00:00
|
|
|
} else if isVMError(err) {
|
|
|
|
resultChan <- CommandResult{err: err}
|
2023-03-29 07:03:43 +00:00
|
|
|
return nil
|
2023-03-28 16:22:11 +00:00
|
|
|
}
|
2023-02-20 09:32:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-03-06 13:49:17 +00:00
|
|
|
resultChan <- CommandResult{res: res}
|
2023-02-20 09:32:45 +00:00
|
|
|
return nil
|
|
|
|
}, func(err error) error {
|
|
|
|
if c.fallback == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
err = c.fallbackLimiter.WaitForRequestsAvailability(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
res, err := fallback()
|
|
|
|
if err != nil {
|
2024-02-23 14:27:08 +00:00
|
|
|
if isRPSLimitError(err) {
|
|
|
|
c.fallbackLimiter.ReduceLimit()
|
2024-03-06 13:49:17 +00:00
|
|
|
} else if isVMError(err) {
|
|
|
|
resultChan <- CommandResult{err: err}
|
2023-03-29 07:03:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-20 09:32:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-03-06 13:49:17 +00:00
|
|
|
resultChan <- CommandResult{res: res}
|
2023-02-20 09:32:45 +00:00
|
|
|
return nil
|
|
|
|
})
|
2023-03-29 07:03:43 +00:00
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
select {
|
|
|
|
case result := <-resultChan:
|
2024-03-06 13:49:17 +00:00
|
|
|
if result.err != nil {
|
|
|
|
return nil, result.err
|
2023-03-28 16:22:11 +00:00
|
|
|
}
|
2024-03-06 13:49:17 +00:00
|
|
|
return result.res, nil
|
2023-02-20 09:32:45 +00:00
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
|
|
|
|
rpcstats.CountCall("eth_BlockByHash")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.BlockByHash(ctx, hash); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.BlockByHash(ctx, hash); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Block), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
|
|
|
|
rpcstats.CountCall("eth_BlockByNumber")
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.BlockByNumber(ctx, number); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.BlockByNumber(ctx, number); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Block), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) BlockNumber(ctx context.Context) (uint64, error) {
|
|
|
|
rpcstats.CountCall("eth_BlockNumber")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.BlockNumber(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.BlockNumber(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint64), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PeerCount(ctx context.Context) (uint64, error) {
|
|
|
|
rpcstats.CountCall("eth_PeerCount")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PeerCount(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PeerCount(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint64), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
|
|
|
|
rpcstats.CountCall("eth_HeaderByHash")
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.HeaderByHash(ctx, hash); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.HeaderByHash(ctx, hash); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Header), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
|
|
|
|
rpcstats.CountCall("eth_HeaderByNumber")
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.HeaderByNumber(ctx, number); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.HeaderByNumber(ctx, number); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Header), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) TransactionByHash(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error) {
|
|
|
|
rpcstats.CountCall("eth_TransactionByHash")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, b, err := c.main.TransactionByHash(ctx, hash); return []any{a, b}, err },
|
|
|
|
func() ([]any, error) { a, b, err := c.fallback.TransactionByHash(ctx, hash); return []any{a, b}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Transaction), res[1].(bool), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
|
|
|
|
rpcstats.CountCall("eth_TransactionSender")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.TransactionSender(ctx, tx, block, index); return []any{a}, err },
|
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.fallback.TransactionSender(ctx, tx, block, index)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
|
|
|
return res[0].(common.Address), err
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
|
|
|
|
rpcstats.CountCall("eth_TransactionCount")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.TransactionCount(ctx, blockHash); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.TransactionCount(ctx, blockHash); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
|
|
|
|
rpcstats.CountCall("eth_TransactionInBlock")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.main.TransactionInBlock(ctx, blockHash, index)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.fallback.TransactionInBlock(ctx, blockHash, index)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Transaction), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
|
|
|
rpcstats.CountCall("eth_TransactionReceipt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.TransactionReceipt(ctx, txHash); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.TransactionReceipt(ctx, txHash); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*types.Receipt), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
|
|
|
|
rpcstats.CountCall("eth_SyncProgress")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.SyncProgress(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.SyncProgress(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*ethereum.SyncProgress), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
|
|
|
|
rpcstats.CountCall("eth_SubscribeNewHead")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.SubscribeNewHead(ctx, ch); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.SubscribeNewHead(ctx, ch); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(ethereum.Subscription), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 09:13:46 +00:00
|
|
|
func (c *ClientWithFallback) NetworkID() uint64 {
|
|
|
|
return c.ChainID
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
|
|
|
|
rpcstats.CountCall("eth_BalanceAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.BalanceAt(ctx, account, blockNumber); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.BalanceAt(ctx, account, blockNumber); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*big.Int), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_StorageAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.main.StorageAt(ctx, account, key, blockNumber)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.fallback.StorageAt(ctx, account, key, blockNumber)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_CodeAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.CodeAt(ctx, account, blockNumber); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.CodeAt(ctx, account, blockNumber); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
|
|
|
|
rpcstats.CountCall("eth_NonceAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.NonceAt(ctx, account, blockNumber); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.NonceAt(ctx, account, blockNumber); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint64), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
|
|
|
|
rpcstats.CountCall("eth_FilterLogs")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.FilterLogs(ctx, q); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.FilterLogs(ctx, q); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]types.Log), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
|
|
|
|
rpcstats.CountCall("eth_SubscribeFilterLogs")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.SubscribeFilterLogs(ctx, q, ch); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.SubscribeFilterLogs(ctx, q, ch); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(ethereum.Subscription), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingBalanceAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingBalanceAt(ctx, account); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingBalanceAt(ctx, account); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*big.Int), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingStorageAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingStorageAt(ctx, account, key); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingStorageAt(ctx, account, key); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingCodeAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingCodeAt(ctx, account); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingCodeAt(ctx, account); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingNonceAt")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingNonceAt(ctx, account); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingNonceAt(ctx, account); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint64), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingTransactionCount(ctx context.Context) (uint, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingTransactionCount")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingTransactionCount(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingTransactionCount(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
2024-01-19 15:57:04 +00:00
|
|
|
rpcstats.CountCall("eth_CallContract_" + msg.To.String())
|
2023-02-20 09:32:45 +00:00
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.CallContract(ctx, msg, blockNumber); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.CallContract(ctx, msg, blockNumber); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_CallContractAtHash")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.CallContractAtHash(ctx, msg, blockHash); return []any{a}, err },
|
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.fallback.CallContractAtHash(ctx, msg, blockHash)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
|
|
|
|
rpcstats.CountCall("eth_PendingCallContract")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.PendingCallContract(ctx, msg); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.PendingCallContract(ctx, msg); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].([]byte), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
|
|
|
rpcstats.CountCall("eth_SuggestGasPrice")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.SuggestGasPrice(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.SuggestGasPrice(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*big.Int), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
|
|
|
|
rpcstats.CountCall("eth_SuggestGasTipCap")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.SuggestGasTipCap(ctx); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.SuggestGasTipCap(ctx); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*big.Int), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
|
|
|
|
rpcstats.CountCall("eth_FeeHistory")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.main.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := c.fallback.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
|
|
|
|
return []any{a}, err
|
|
|
|
},
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(*ethereum.FeeHistory), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
|
|
|
|
rpcstats.CountCall("eth_EstimateGas")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { a, err := c.main.EstimateGas(ctx, msg); return []any{a}, err },
|
|
|
|
func() ([]any, error) { a, err := c.fallback.EstimateGas(ctx, msg); return []any{a}, err },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(uint64), nil
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SendTransaction(ctx context.Context, tx *types.Transaction) error {
|
|
|
|
rpcstats.CountCall("eth_SendTransaction")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
_, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { return nil, c.main.SendTransaction(ctx, tx) },
|
|
|
|
func() ([]any, error) { return nil, c.fallback.SendTransaction(ctx, tx) },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
2024-03-06 13:49:17 +00:00
|
|
|
return err
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
|
|
|
|
rpcstats.CountCall("eth_CallContext")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
_, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { return nil, c.mainRPC.CallContext(ctx, result, method, args...) },
|
|
|
|
func() ([]any, error) { return nil, c.fallbackRPC.CallContext(ctx, result, method, args...) },
|
2023-02-20 09:32:45 +00:00
|
|
|
)
|
2024-03-06 13:49:17 +00:00
|
|
|
return err
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
func (c *ClientWithFallback) BatchCallContext(ctx context.Context, b []rpc.BatchElem) error {
|
|
|
|
rpcstats.CountCall("eth_BatchCallContext")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
_, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) { return nil, c.mainRPC.BatchCallContext(ctx, b) },
|
|
|
|
func() ([]any, error) { return nil, c.fallbackRPC.BatchCallContext(ctx, b) },
|
2023-08-01 18:50:30 +00:00
|
|
|
)
|
2024-03-06 13:49:17 +00:00
|
|
|
return err
|
2023-08-01 18:50:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
func (c *ClientWithFallback) ToBigInt() *big.Int {
|
|
|
|
return big.NewInt(int64(c.ChainID))
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
func (c *ClientWithFallback) GetBaseFeeFromBlock(ctx context.Context, blockNumber *big.Int) (string, error) {
|
2023-02-20 09:32:45 +00:00
|
|
|
rpcstats.CountCall("eth_GetBaseFeeFromBlock")
|
2024-02-23 14:27:08 +00:00
|
|
|
|
|
|
|
feeHistory, err := c.FeeHistory(ctx, 1, blockNumber, nil)
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "the method eth_feeHistory does not exist/is not available" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseGasFee string = ""
|
2024-02-23 14:27:08 +00:00
|
|
|
if len(feeHistory.BaseFee) > 0 {
|
|
|
|
baseGasFee = feeHistory.BaseFee[0].String()
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return baseGasFee, err
|
|
|
|
}
|
2023-06-01 17:16:50 +00:00
|
|
|
|
|
|
|
// go-ethereum's `Transaction` items drop the blkHash obtained during the RPC call.
|
|
|
|
// This function preserves the additional data. This is the cheapest way to obtain
|
|
|
|
// the block hash for a given block number.
|
2024-02-21 10:50:46 +00:00
|
|
|
func (c *ClientWithFallback) CallBlockHashByTransaction(ctx context.Context, blockNumber *big.Int, index uint) (common.Hash, error) {
|
2023-06-01 17:16:50 +00:00
|
|
|
rpcstats.CountCall("eth_FullTransactionByBlockNumberAndIndex")
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
res, err := c.makeCall(
|
2024-02-23 14:27:08 +00:00
|
|
|
ctx,
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := callBlockHashByTransaction(ctx, c.mainRPC, blockNumber, index)
|
|
|
|
return []any{a}, err
|
2023-06-01 17:16:50 +00:00
|
|
|
},
|
2024-03-06 13:49:17 +00:00
|
|
|
func() ([]any, error) {
|
|
|
|
a, err := callBlockHashByTransaction(ctx, c.fallbackRPC, blockNumber, index)
|
|
|
|
return []any{a}, err
|
2023-06-01 17:16:50 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
c.toggleConnectionState(err)
|
|
|
|
|
2023-06-01 17:16:50 +00:00
|
|
|
if err != nil {
|
2024-02-21 10:50:46 +00:00
|
|
|
return common.HexToHash(""), err
|
2023-06-01 17:16:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:49:17 +00:00
|
|
|
return res[0].(common.Hash), nil
|
2023-06-01 17:16:50 +00:00
|
|
|
}
|
2023-09-20 08:41:23 +00:00
|
|
|
|
|
|
|
func (c *ClientWithFallback) GetWalletNotifier() func(chainId uint64, message string) {
|
|
|
|
return c.WalletNotifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientWithFallback) SetWalletNotifier(notifier func(chainId uint64, message string)) {
|
|
|
|
c.WalletNotifier = notifier
|
|
|
|
}
|
2024-03-06 13:49:17 +00:00
|
|
|
|
|
|
|
func (c *ClientWithFallback) toggleConnectionState(err error) {
|
|
|
|
connected := true
|
|
|
|
if err != nil {
|
|
|
|
if !isVMError(err) && err != ErrRequestsOverLimit {
|
|
|
|
connected = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.SetIsConnected(connected)
|
|
|
|
}
|