2019-07-22 04:17:27 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2019-01-21 08:38:13 -06:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Package graphql provides a GraphQL interface to Ethereum node data.
|
|
|
|
package graphql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-18 01:24:43 -05:00
|
|
|
"errors"
|
2021-01-05 04:22:32 -06:00
|
|
|
"fmt"
|
2021-06-08 05:05:41 -05:00
|
|
|
"math/big"
|
2022-08-31 09:14:53 -05:00
|
|
|
"sort"
|
2021-01-05 04:22:32 -06:00
|
|
|
"strconv"
|
2023-04-25 07:02:54 -05:00
|
|
|
"strings"
|
2023-03-28 02:08:10 -05:00
|
|
|
"sync"
|
2019-01-21 08:38:13 -06:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-06-08 05:05:41 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2023-08-01 04:58:45 -05:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
|
2019-01-21 08:38:13 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2022-05-17 03:31:17 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-01-21 08:38:13 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2019-07-25 01:29:53 -05:00
|
|
|
var (
|
2019-09-26 03:47:31 -05:00
|
|
|
errBlockInvariant = errors.New("block objects must be instantiated with at least one of num or hash")
|
2019-07-25 01:29:53 -05:00
|
|
|
)
|
2019-03-18 01:24:43 -05:00
|
|
|
|
2021-01-05 04:22:32 -06:00
|
|
|
type Long int64
|
|
|
|
|
|
|
|
// ImplementsGraphQLType returns true if Long implements the provided GraphQL type.
|
|
|
|
func (b Long) ImplementsGraphQLType(name string) bool { return name == "Long" }
|
|
|
|
|
|
|
|
// UnmarshalGraphQL unmarshals the provided GraphQL query data.
|
|
|
|
func (b *Long) UnmarshalGraphQL(input interface{}) error {
|
|
|
|
var err error
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
|
|
|
// uncomment to support hex values
|
2023-04-25 07:02:54 -05:00
|
|
|
if strings.HasPrefix(input, "0x") {
|
|
|
|
// apply leniency and support hex representations of longs.
|
|
|
|
value, err := hexutil.DecodeUint64(input)
|
|
|
|
*b = Long(value)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
value, err := strconv.ParseInt(input, 10, 64)
|
|
|
|
*b = Long(value)
|
|
|
|
return err
|
|
|
|
}
|
2021-01-05 04:22:32 -06:00
|
|
|
case int32:
|
|
|
|
*b = Long(input)
|
|
|
|
case int64:
|
|
|
|
*b = Long(input)
|
2022-05-12 14:10:08 -05:00
|
|
|
case float64:
|
|
|
|
*b = Long(input)
|
2021-01-05 04:22:32 -06:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unexpected type %T for Long", input)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
// Account represents an Ethereum account at a particular block.
|
|
|
|
type Account struct {
|
2022-08-03 12:08:32 -05:00
|
|
|
r *Resolver
|
2019-09-26 03:47:31 -05:00
|
|
|
address common.Address
|
|
|
|
blockNrOrHash rpc.BlockNumberOrHash
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// getState fetches the StateDB object for an account.
|
|
|
|
func (a *Account) getState(ctx context.Context) (*state.StateDB, error) {
|
2022-08-03 12:08:32 -05:00
|
|
|
state, _, err := a.r.backend.StateAndHeaderByNumberOrHash(ctx, a.blockNrOrHash)
|
2023-03-30 05:57:49 -05:00
|
|
|
return state, err
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Address(ctx context.Context) (common.Address, error) {
|
|
|
|
return a.address, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Balance(ctx context.Context) (hexutil.Big, error) {
|
|
|
|
state, err := a.getState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Big{}, err
|
|
|
|
}
|
2021-06-22 04:13:48 -05:00
|
|
|
balance := state.GetBalance(a.address)
|
|
|
|
if balance == nil {
|
|
|
|
return hexutil.Big{}, fmt.Errorf("failed to load balance %x", a.address)
|
|
|
|
}
|
|
|
|
return hexutil.Big(*balance), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
|
2022-02-22 03:45:11 -06:00
|
|
|
// Ask transaction pool for the nonce which includes pending transactions
|
|
|
|
if blockNr, ok := a.blockNrOrHash.Number(); ok && blockNr == rpc.PendingBlockNumber {
|
2022-08-03 12:08:32 -05:00
|
|
|
nonce, err := a.r.backend.GetPoolNonce(ctx, a.address)
|
2022-02-22 03:45:11 -06:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return hexutil.Uint64(nonce), nil
|
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
state, err := a.getState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return hexutil.Uint64(state.GetNonce(a.address)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Code(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
state, err := a.getState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
2020-11-25 14:00:23 -06:00
|
|
|
return state.GetCode(a.address), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Storage(ctx context.Context, args struct{ Slot common.Hash }) (common.Hash, error) {
|
|
|
|
state, err := a.getState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return state.GetState(a.address, args.Slot), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log represents an individual log message. All arguments are mandatory.
|
|
|
|
type Log struct {
|
2022-08-03 12:08:32 -05:00
|
|
|
r *Resolver
|
2019-01-21 08:38:13 -06:00
|
|
|
transaction *Transaction
|
|
|
|
log *types.Log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Transaction(ctx context.Context) *Transaction {
|
|
|
|
return l.transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Account(ctx context.Context, args BlockNumberArgs) *Account {
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: l.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: l.log.Address,
|
|
|
|
blockNrOrHash: args.NumberOrLatest(),
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (l *Log) Index(ctx context.Context) hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(l.log.Index)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Topics(ctx context.Context) []common.Hash {
|
|
|
|
return l.log.Topics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Log) Data(ctx context.Context) hexutil.Bytes {
|
2020-11-25 14:00:23 -06:00
|
|
|
return l.log.Data
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2021-04-06 08:58:36 -05:00
|
|
|
// AccessTuple represents EIP-2930
|
|
|
|
type AccessTuple struct {
|
|
|
|
address common.Address
|
2021-09-28 17:28:17 -05:00
|
|
|
storageKeys []common.Hash
|
2021-04-06 08:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (at *AccessTuple) Address(ctx context.Context) common.Address {
|
|
|
|
return at.address
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:28:17 -05:00
|
|
|
func (at *AccessTuple) StorageKeys(ctx context.Context) []common.Hash {
|
2021-04-06 08:58:36 -05:00
|
|
|
return at.storageKeys
|
|
|
|
}
|
|
|
|
|
2023-06-06 11:33:25 -05:00
|
|
|
// Withdrawal represents a withdrawal of value from the beacon chain
|
|
|
|
// by a validator. For details see EIP-4895.
|
|
|
|
type Withdrawal struct {
|
|
|
|
index uint64
|
|
|
|
validator uint64
|
|
|
|
address common.Address
|
|
|
|
amount uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Withdrawal) Index(ctx context.Context) hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(w.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Withdrawal) Validator(ctx context.Context) hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(w.validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Withdrawal) Address(ctx context.Context) common.Address {
|
|
|
|
return w.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Withdrawal) Amount(ctx context.Context) hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(w.amount)
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:19:43 -06:00
|
|
|
// Transaction represents an Ethereum transaction.
|
2019-01-21 08:38:13 -06:00
|
|
|
// backend and hash are mandatory; all others will be fetched when required.
|
|
|
|
type Transaction struct {
|
2023-03-28 02:08:10 -05:00
|
|
|
r *Resolver
|
|
|
|
hash common.Hash // Must be present after initialization
|
|
|
|
mu sync.Mutex
|
|
|
|
// mu protects following resources
|
2022-08-03 12:08:32 -05:00
|
|
|
tx *types.Transaction
|
|
|
|
block *Block
|
|
|
|
index uint64
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve returns the internal transaction object, fetching it if needed.
|
2023-05-02 03:57:07 -05:00
|
|
|
// It also returns the block the tx belongs to, unless it is a pending tx.
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, *Block) {
|
2023-03-28 02:08:10 -05:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if t.tx != nil {
|
2023-06-05 04:07:01 -05:00
|
|
|
return t.tx, t.block
|
2023-03-28 02:08:10 -05:00
|
|
|
}
|
|
|
|
// Try to return an already finalized transaction
|
|
|
|
tx, blockHash, _, index, err := t.r.backend.GetTransaction(ctx, t.hash)
|
|
|
|
if err == nil && tx != nil {
|
|
|
|
t.tx = tx
|
|
|
|
blockNrOrHash := rpc.BlockNumberOrHashWithHash(blockHash, false)
|
|
|
|
t.block = &Block{
|
|
|
|
r: t.r,
|
|
|
|
numberOrHash: &blockNrOrHash,
|
|
|
|
hash: blockHash,
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
t.index = index
|
2023-06-05 04:07:01 -05:00
|
|
|
return t.tx, t.block
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
// No finalized transaction, try to retrieve it from the pool
|
|
|
|
t.tx = t.r.backend.GetPoolTransaction(t.hash)
|
2023-06-05 04:07:01 -05:00
|
|
|
return t.tx, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2019-07-25 01:29:53 -05:00
|
|
|
func (t *Transaction) Hash(ctx context.Context) common.Hash {
|
|
|
|
return t.hash
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) InputData(ctx context.Context) hexutil.Bytes {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Bytes{}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return tx.Data()
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) Gas(ctx context.Context) hexutil.Uint64 {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return 0
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Uint64(tx.Gas())
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) GasPrice(ctx context.Context) hexutil.Big {
|
|
|
|
tx, block := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Big{}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2021-06-08 05:05:41 -05:00
|
|
|
switch tx.Type() {
|
|
|
|
case types.AccessListTxType:
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*tx.GasPrice())
|
2021-06-08 05:05:41 -05:00
|
|
|
case types.DynamicFeeTxType:
|
2023-03-28 02:08:10 -05:00
|
|
|
if block != nil {
|
|
|
|
if baseFee, _ := block.BaseFeePerGas(ctx); baseFee != nil {
|
2023-08-04 18:08:53 -05:00
|
|
|
// price = min(gasTipCap + baseFee, gasFeeCap)
|
2023-06-05 04:07:01 -05:00
|
|
|
return (hexutil.Big)(*math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee.ToInt()), tx.GasFeeCap()))
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*tx.GasPrice())
|
2021-06-08 05:05:41 -05:00
|
|
|
default:
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*tx.GasPrice())
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 00:23:50 -05:00
|
|
|
func (t *Transaction) EffectiveGasPrice(ctx context.Context) (*hexutil.Big, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
tx, block := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil, nil
|
2021-06-16 00:23:50 -05:00
|
|
|
}
|
2022-03-10 06:59:22 -06:00
|
|
|
// Pending tx
|
2023-03-28 02:08:10 -05:00
|
|
|
if block == nil {
|
2022-03-10 06:59:22 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
header, err := block.resolveHeader(ctx)
|
2021-06-16 00:23:50 -05:00
|
|
|
if err != nil || header == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header.BaseFee == nil {
|
|
|
|
return (*hexutil.Big)(tx.GasPrice()), nil
|
|
|
|
}
|
|
|
|
return (*hexutil.Big)(math.BigMin(new(big.Int).Add(tx.GasTipCap(), header.BaseFee), tx.GasFeeCap())), nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) MaxFeePerGas(ctx context.Context) *hexutil.Big {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
|
|
|
switch tx.Type() {
|
|
|
|
case types.AccessListTxType:
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
case types.DynamicFeeTxType:
|
2023-06-05 04:07:01 -05:00
|
|
|
return (*hexutil.Big)(tx.GasFeeCap())
|
2021-06-08 05:05:41 -05:00
|
|
|
default:
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) MaxPriorityFeePerGas(ctx context.Context) *hexutil.Big {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
|
|
|
switch tx.Type() {
|
|
|
|
case types.AccessListTxType:
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
case types.DynamicFeeTxType:
|
2023-06-05 04:07:01 -05:00
|
|
|
return (*hexutil.Big)(tx.GasTipCap())
|
2021-06-08 05:05:41 -05:00
|
|
|
default:
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2021-06-08 05:05:41 -05:00
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2022-03-10 06:59:22 -06:00
|
|
|
func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
tx, block := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil, nil
|
2022-03-10 06:59:22 -06:00
|
|
|
}
|
|
|
|
// Pending tx
|
2023-03-28 02:08:10 -05:00
|
|
|
if block == nil {
|
2022-03-10 06:59:22 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
header, err := block.resolveHeader(ctx)
|
2022-03-10 06:59:22 -06:00
|
|
|
if err != nil || header == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header.BaseFee == nil {
|
|
|
|
return (*hexutil.Big)(tx.GasPrice()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tip, err := tx.EffectiveGasTip(header.BaseFee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*hexutil.Big)(tip), nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Big{}, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2021-06-22 04:13:48 -05:00
|
|
|
if tx.Value() == nil {
|
|
|
|
return hexutil.Big{}, fmt.Errorf("invalid transaction value %x", t.hash)
|
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
return hexutil.Big(*tx.Value()), nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) Nonce(ctx context.Context) hexutil.Uint64 {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return 0
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Uint64(tx.Nonce())
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) To(ctx context.Context, args BlockNumberArgs) *Account {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
to := tx.To()
|
|
|
|
if to == nil {
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: t.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: *to,
|
|
|
|
blockNrOrHash: args.NumberOrLatest(),
|
2023-06-05 04:07:01 -05:00
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) From(ctx context.Context, args BlockNumberArgs) *Account {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2022-08-03 12:08:32 -05:00
|
|
|
signer := types.LatestSigner(t.r.backend.ChainConfig())
|
2019-01-21 08:38:13 -06:00
|
|
|
from, _ := types.Sender(signer, tx)
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: t.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: from,
|
|
|
|
blockNrOrHash: args.NumberOrLatest(),
|
2023-06-05 04:07:01 -05:00
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) Block(ctx context.Context) *Block {
|
|
|
|
_, block := t.resolve(ctx)
|
|
|
|
return block
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) Index(ctx context.Context) *hexutil.Uint64 {
|
|
|
|
_, block := t.resolve(ctx)
|
2023-03-28 02:08:10 -05:00
|
|
|
// Pending tx
|
|
|
|
if block == nil {
|
2023-06-05 04:07:01 -05:00
|
|
|
return nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
index := hexutil.Uint64(t.index)
|
2023-06-05 04:07:01 -05:00
|
|
|
return &index
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// getReceipt returns the receipt associated with this transaction, if any.
|
|
|
|
func (t *Transaction) getReceipt(ctx context.Context) (*types.Receipt, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
_, block := t.resolve(ctx)
|
2023-03-28 02:08:10 -05:00
|
|
|
// Pending tx
|
|
|
|
if block == nil {
|
2019-01-21 08:38:13 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
receipts, err := block.resolveReceipts(ctx)
|
2019-01-21 08:38:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return receipts[t.index], nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (t *Transaction) Status(ctx context.Context) (*hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
receipt, err := t.getReceipt(ctx)
|
|
|
|
if err != nil || receipt == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-05 09:21:08 -06:00
|
|
|
if len(receipt.PostState) != 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
ret := hexutil.Uint64(receipt.Status)
|
2019-01-21 08:38:13 -06:00
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (t *Transaction) GasUsed(ctx context.Context) (*hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
receipt, err := t.getReceipt(ctx)
|
|
|
|
if err != nil || receipt == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
ret := hexutil.Uint64(receipt.GasUsed)
|
2019-01-21 08:38:13 -06:00
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
receipt, err := t.getReceipt(ctx)
|
|
|
|
if err != nil || receipt == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
ret := hexutil.Uint64(receipt.CumulativeGasUsed)
|
2019-01-21 08:38:13 -06:00
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transaction) CreatedContract(ctx context.Context, args BlockNumberArgs) (*Account, error) {
|
|
|
|
receipt, err := t.getReceipt(ctx)
|
|
|
|
if err != nil || receipt == nil || receipt.ContractAddress == (common.Address{}) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: t.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: receipt.ContractAddress,
|
|
|
|
blockNrOrHash: args.NumberOrLatest(),
|
2019-01-21 08:38:13 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
_, block := t.resolve(ctx)
|
2023-03-28 02:08:10 -05:00
|
|
|
// Pending tx
|
|
|
|
if block == nil {
|
2022-08-19 04:14:59 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
h, err := block.Hash(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-19 04:14:59 -05:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
return t.getLogs(ctx, h)
|
2022-08-19 04:14:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// getLogs returns log objects for the given tx.
|
|
|
|
// Assumes block hash is resolved.
|
2023-03-28 02:08:10 -05:00
|
|
|
func (t *Transaction) getLogs(ctx context.Context, hash common.Hash) (*[]*Log, error) {
|
2022-08-19 04:14:59 -05:00
|
|
|
var (
|
|
|
|
filter = t.r.filterSystem.NewBlockFilter(hash, nil, nil)
|
|
|
|
logs, err = filter.Logs(ctx)
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-31 09:14:53 -05:00
|
|
|
var ret []*Log
|
|
|
|
// Select tx logs from all block logs
|
2022-09-13 14:49:52 -05:00
|
|
|
ix := sort.Search(len(logs), func(i int) bool { return uint64(logs[i].TxIndex) >= t.index })
|
2022-08-31 09:14:53 -05:00
|
|
|
for ix < len(logs) && uint64(logs[ix].TxIndex) == t.index {
|
2019-01-21 08:38:13 -06:00
|
|
|
ret = append(ret, &Log{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: t.r,
|
2019-01-21 08:38:13 -06:00
|
|
|
transaction: t,
|
2022-08-31 09:14:53 -05:00
|
|
|
log: logs[ix],
|
2019-01-21 08:38:13 -06:00
|
|
|
})
|
2022-08-31 09:14:53 -05:00
|
|
|
ix++
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) Type(ctx context.Context) *hexutil.Uint64 {
|
|
|
|
tx, _ := t.resolve(ctx)
|
2023-04-25 07:02:54 -05:00
|
|
|
txType := hexutil.Uint64(tx.Type())
|
2023-06-05 04:07:01 -05:00
|
|
|
return &txType
|
2021-04-06 08:58:36 -05:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) AccessList(ctx context.Context) *[]*AccessTuple {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return nil
|
2021-04-06 08:58:36 -05:00
|
|
|
}
|
|
|
|
accessList := tx.AccessList()
|
|
|
|
ret := make([]*AccessTuple, 0, len(accessList))
|
|
|
|
for _, al := range accessList {
|
|
|
|
ret = append(ret, &AccessTuple{
|
|
|
|
address: al.Address,
|
2021-09-28 17:28:17 -05:00
|
|
|
storageKeys: al.StorageKeys,
|
2021-04-06 08:58:36 -05:00
|
|
|
})
|
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return &ret
|
2021-04-06 08:58:36 -05:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) R(ctx context.Context) hexutil.Big {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Big{}
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
_, r, _ := tx.RawSignatureValues()
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*r)
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) S(ctx context.Context) hexutil.Big {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Big{}
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
_, _, s := tx.RawSignatureValues()
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*s)
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (t *Transaction) V(ctx context.Context) hexutil.Big {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Big{}
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
v, _, _ := tx.RawSignatureValues()
|
2023-06-05 04:07:01 -05:00
|
|
|
return hexutil.Big(*v)
|
2020-02-09 14:50:44 -06:00
|
|
|
}
|
|
|
|
|
2023-08-09 10:57:57 -05:00
|
|
|
func (t *Transaction) YParity(ctx context.Context) (*hexutil.Uint64, error) {
|
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil || tx.Type() == types.LegacyTxType {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
v, _, _ := tx.RawSignatureValues()
|
|
|
|
ret := hexutil.Uint64(v.Int64())
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 03:31:17 -05:00
|
|
|
func (t *Transaction) Raw(ctx context.Context) (hexutil.Bytes, error) {
|
2023-06-05 04:07:01 -05:00
|
|
|
tx, _ := t.resolve(ctx)
|
|
|
|
if tx == nil {
|
|
|
|
return hexutil.Bytes{}, nil
|
2022-05-17 03:31:17 -05:00
|
|
|
}
|
|
|
|
return tx.MarshalBinary()
|
|
|
|
}
|
|
|
|
|
2022-05-04 07:31:07 -05:00
|
|
|
func (t *Transaction) RawReceipt(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
receipt, err := t.getReceipt(ctx)
|
|
|
|
if err != nil || receipt == nil {
|
2022-05-17 03:31:17 -05:00
|
|
|
return hexutil.Bytes{}, err
|
2022-05-04 07:31:07 -05:00
|
|
|
}
|
|
|
|
return receipt.MarshalBinary()
|
|
|
|
}
|
|
|
|
|
2019-03-18 01:24:43 -05:00
|
|
|
type BlockType int
|
|
|
|
|
|
|
|
// Block represents an Ethereum block.
|
2019-09-26 03:47:31 -05:00
|
|
|
// backend, and numberOrHash are mandatory. All other fields are lazily fetched
|
2019-01-21 08:38:13 -06:00
|
|
|
// when required.
|
|
|
|
type Block struct {
|
2022-08-03 12:08:32 -05:00
|
|
|
r *Resolver
|
2023-03-28 02:08:10 -05:00
|
|
|
numberOrHash *rpc.BlockNumberOrHash // Field resolvers assume numberOrHash is always present
|
|
|
|
mu sync.Mutex
|
|
|
|
// mu protects following resources
|
|
|
|
hash common.Hash // Must be resolved during initialization
|
|
|
|
header *types.Header
|
|
|
|
block *types.Block
|
|
|
|
receipts []*types.Receipt
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve returns the internal Block object representing this block, fetching
|
|
|
|
// it if necessary.
|
|
|
|
func (b *Block) resolve(ctx context.Context) (*types.Block, error) {
|
2023-03-28 02:08:10 -05:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2019-01-21 08:38:13 -06:00
|
|
|
if b.block != nil {
|
|
|
|
return b.block, nil
|
|
|
|
}
|
2019-09-26 03:47:31 -05:00
|
|
|
if b.numberOrHash == nil {
|
|
|
|
latest := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
|
|
|
|
b.numberOrHash = &latest
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2019-09-26 03:47:31 -05:00
|
|
|
var err error
|
2022-08-03 12:08:32 -05:00
|
|
|
b.block, err = b.r.backend.BlockByNumberOrHash(ctx, *b.numberOrHash)
|
2023-03-28 02:08:10 -05:00
|
|
|
if b.block != nil {
|
|
|
|
b.hash = b.block.Hash()
|
|
|
|
if b.header == nil {
|
|
|
|
b.header = b.block.Header()
|
2019-09-26 03:47:31 -05:00
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
return b.block, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveHeader returns the internal Header object for this block, fetching it
|
|
|
|
// if necessary. Call this function instead of `resolve` unless you need the
|
|
|
|
// additional data (transactions and uncles).
|
|
|
|
func (b *Block) resolveHeader(ctx context.Context) (*types.Header, error) {
|
2023-03-28 02:08:10 -05:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.header != nil {
|
|
|
|
return b.header, nil
|
|
|
|
}
|
2019-09-26 03:47:31 -05:00
|
|
|
if b.numberOrHash == nil && b.hash == (common.Hash{}) {
|
2019-07-25 01:29:53 -05:00
|
|
|
return nil, errBlockInvariant
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
2019-07-25 01:29:53 -05:00
|
|
|
var err error
|
2023-03-28 02:08:10 -05:00
|
|
|
b.header, err = b.r.backend.HeaderByNumberOrHash(ctx, *b.numberOrHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if b.hash == (common.Hash{}) {
|
|
|
|
b.hash = b.header.Hash()
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
return b.header, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolveReceipts returns the list of receipts for this block, fetching them
|
|
|
|
// if necessary.
|
|
|
|
func (b *Block) resolveReceipts(ctx context.Context) ([]*types.Receipt, error) {
|
2023-03-28 02:08:10 -05:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.receipts != nil {
|
|
|
|
return b.receipts, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
receipts, err := b.r.backend.GetReceipts(ctx, b.hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.receipts = receipts
|
|
|
|
return receipts, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) Number(ctx context.Context) (hexutil.Uint64, error) {
|
2019-09-26 03:47:31 -05:00
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2019-09-26 03:47:31 -05:00
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
return hexutil.Uint64(header.Number.Uint64()), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Hash(ctx context.Context) (common.Hash, error) {
|
2023-03-28 02:08:10 -05:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2019-01-21 08:38:13 -06:00
|
|
|
return b.hash, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) GasLimit(ctx context.Context) (hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
return hexutil.Uint64(header.GasLimit), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) GasUsed(ctx context.Context) (hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
return hexutil.Uint64(header.GasUsed), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2021-06-08 05:05:41 -05:00
|
|
|
func (b *Block) BaseFeePerGas(ctx context.Context) (*hexutil.Big, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header.BaseFee == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return (*hexutil.Big)(header.BaseFee), nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:59:22 -06:00
|
|
|
func (b *Block) NextBaseFeePerGas(ctx context.Context) (*hexutil.Big, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-03 12:08:32 -05:00
|
|
|
chaincfg := b.r.backend.ChainConfig()
|
2022-03-10 06:59:22 -06:00
|
|
|
if header.BaseFee == nil {
|
|
|
|
// Make sure next block doesn't enable EIP-1559
|
|
|
|
if !chaincfg.IsLondon(new(big.Int).Add(header.Number, common.Big1)) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 04:58:45 -05:00
|
|
|
nextBaseFee := eip1559.CalcBaseFee(chaincfg, header)
|
2022-03-10 06:59:22 -06:00
|
|
|
return (*hexutil.Big)(nextBaseFee), nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
func (b *Block) Parent(ctx context.Context) (*Block, error) {
|
2022-01-05 09:22:46 -06:00
|
|
|
if _, err := b.resolveHeader(ctx); err != nil {
|
|
|
|
return nil, err
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2022-01-05 09:22:46 -06:00
|
|
|
if b.header == nil || b.header.Number.Uint64() < 1 {
|
|
|
|
return nil, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
var (
|
|
|
|
num = rpc.BlockNumber(b.header.Number.Uint64() - 1)
|
|
|
|
hash = b.header.ParentHash
|
|
|
|
numOrHash = rpc.BlockNumberOrHash{
|
|
|
|
BlockNumber: &num,
|
|
|
|
BlockHash: &hash,
|
|
|
|
}
|
|
|
|
)
|
2022-01-05 09:22:46 -06:00
|
|
|
return &Block{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
2023-03-28 02:08:10 -05:00
|
|
|
numberOrHash: &numOrHash,
|
|
|
|
hash: hash,
|
2022-01-05 09:22:46 -06:00
|
|
|
}, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Difficulty(ctx context.Context) (hexutil.Big, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Big{}, err
|
|
|
|
}
|
|
|
|
return hexutil.Big(*header.Difficulty), nil
|
|
|
|
}
|
|
|
|
|
2019-04-02 15:28:48 -05:00
|
|
|
func (b *Block) Timestamp(ctx context.Context) (hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
2019-04-02 15:28:48 -05:00
|
|
|
return 0, err
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2019-04-02 15:28:48 -05:00
|
|
|
return hexutil.Uint64(header.Time), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Nonce(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
2020-11-25 14:00:23 -06:00
|
|
|
return header.Nonce[:], nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) MixHash(ctx context.Context) (common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return header.MixDigest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) TransactionsRoot(ctx context.Context) (common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return header.TxHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) StateRoot(ctx context.Context) (common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return header.Root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) ReceiptsRoot(ctx context.Context) (common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return header.ReceiptHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) OmmerHash(ctx context.Context) (common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return header.UncleHash, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) OmmerCount(ctx context.Context) (*hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
count := hexutil.Uint64(len(block.Uncles()))
|
2019-01-21 08:38:13 -06:00
|
|
|
return &count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Ommers(ctx context.Context) (*[]*Block, error) {
|
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Block, 0, len(block.Uncles()))
|
|
|
|
for _, uncle := range block.Uncles() {
|
2019-09-26 03:47:31 -05:00
|
|
|
blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
|
2019-01-21 08:38:13 -06:00
|
|
|
ret = append(ret, &Block{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
numberOrHash: &blockNumberOrHash,
|
|
|
|
header: uncle,
|
2023-03-28 02:08:10 -05:00
|
|
|
hash: uncle.Hash(),
|
2019-01-21 08:38:13 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) ExtraData(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
2020-11-25 14:00:23 -06:00
|
|
|
return header.Extra, nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
2020-11-25 14:00:23 -06:00
|
|
|
return header.Bloom.Bytes(), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
|
2023-03-28 02:08:10 -05:00
|
|
|
hash, err := b.Hash(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Big{}, err
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
td := b.r.backend.GetTd(ctx, hash)
|
2021-06-22 04:13:48 -05:00
|
|
|
if td == nil {
|
2023-03-28 02:08:10 -05:00
|
|
|
return hexutil.Big{}, fmt.Errorf("total difficulty not found %x", hash)
|
2021-06-22 04:13:48 -05:00
|
|
|
}
|
|
|
|
return hexutil.Big(*td), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2022-05-17 03:31:17 -05:00
|
|
|
func (b *Block) RawHeader(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
|
|
|
return rlp.EncodeToBytes(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Raw(ctx context.Context) (hexutil.Bytes, error) {
|
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
|
|
|
return rlp.EncodeToBytes(block)
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
// BlockNumberArgs encapsulates arguments to accessors that specify a block number.
|
|
|
|
type BlockNumberArgs struct {
|
2019-09-26 03:47:31 -05:00
|
|
|
// TODO: Ideally we could use input unions to allow the query to specify the
|
|
|
|
// block parameter by hash, block number, or tag but input unions aren't part of the
|
|
|
|
// standard GraphQL schema SDL yet, see: https://github.com/graphql/graphql-spec/issues/488
|
2023-04-25 07:02:54 -05:00
|
|
|
Block *Long
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2019-09-26 03:47:31 -05:00
|
|
|
// NumberOr returns the provided block number argument, or the "current" block number or hash if none
|
2019-01-21 08:38:13 -06:00
|
|
|
// was provided.
|
2019-09-26 03:47:31 -05:00
|
|
|
func (a BlockNumberArgs) NumberOr(current rpc.BlockNumberOrHash) rpc.BlockNumberOrHash {
|
2019-01-21 08:38:13 -06:00
|
|
|
if a.Block != nil {
|
2019-09-26 03:47:31 -05:00
|
|
|
blockNr := rpc.BlockNumber(*a.Block)
|
|
|
|
return rpc.BlockNumberOrHashWithNumber(blockNr)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2019-09-26 03:47:31 -05:00
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOrLatest returns the provided block number argument, or the "latest" block number if none
|
|
|
|
// was provided.
|
|
|
|
func (a BlockNumberArgs) NumberOrLatest() rpc.BlockNumberOrHash {
|
|
|
|
return a.NumberOr(rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Miner(ctx context.Context, args BlockNumberArgs) (*Account, error) {
|
2019-07-25 01:29:53 -05:00
|
|
|
header, err := b.resolveHeader(ctx)
|
2019-01-21 08:38:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: header.Coinbase,
|
|
|
|
blockNrOrHash: args.NumberOrLatest(),
|
2019-01-21 08:38:13 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) TransactionCount(ctx context.Context) (*hexutil.Uint64, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
count := hexutil.Uint64(len(block.Transactions()))
|
2019-01-21 08:38:13 -06:00
|
|
|
return &count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Transactions(ctx context.Context) (*[]*Transaction, error) {
|
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Transaction, 0, len(block.Transactions()))
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
ret = append(ret, &Transaction{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
|
|
|
hash: tx.Hash(),
|
|
|
|
tx: tx,
|
|
|
|
block: b,
|
|
|
|
index: uint64(i),
|
2019-01-21 08:38:13 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) TransactionAt(ctx context.Context, args struct{ Index Long }) (*Transaction, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-25 01:29:53 -05:00
|
|
|
txs := block.Transactions()
|
|
|
|
if args.Index < 0 || int(args.Index) >= len(txs) {
|
2019-01-21 08:38:13 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-07-25 01:29:53 -05:00
|
|
|
tx := txs[args.Index]
|
2019-01-21 08:38:13 -06:00
|
|
|
return &Transaction{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
|
|
|
hash: tx.Hash(),
|
|
|
|
tx: tx,
|
|
|
|
block: b,
|
|
|
|
index: uint64(args.Index),
|
2019-01-21 08:38:13 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (b *Block) OmmerAt(ctx context.Context, args struct{ Index Long }) (*Block, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uncles := block.Uncles()
|
|
|
|
if args.Index < 0 || int(args.Index) >= len(uncles) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
uncle := uncles[args.Index]
|
2019-09-26 03:47:31 -05:00
|
|
|
blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
|
2019-01-21 08:38:13 -06:00
|
|
|
return &Block{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
numberOrHash: &blockNumberOrHash,
|
|
|
|
header: uncle,
|
2023-03-28 02:08:10 -05:00
|
|
|
hash: uncle.Hash(),
|
2019-01-21 08:38:13 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 11:33:25 -05:00
|
|
|
func (b *Block) WithdrawalsRoot(ctx context.Context) (*common.Hash, error) {
|
|
|
|
header, err := b.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Pre-shanghai blocks
|
|
|
|
if header.WithdrawalsHash == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return header.WithdrawalsHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Withdrawals(ctx context.Context) (*[]*Withdrawal, error) {
|
|
|
|
block, err := b.resolve(ctx)
|
|
|
|
if err != nil || block == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Pre-shanghai blocks
|
|
|
|
if block.Header().WithdrawalsHash == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ret := make([]*Withdrawal, 0, len(block.Withdrawals()))
|
|
|
|
for _, w := range block.Withdrawals() {
|
|
|
|
ret = append(ret, &Withdrawal{
|
|
|
|
index: w.Index,
|
|
|
|
validator: w.Validator,
|
|
|
|
address: w.Address,
|
|
|
|
amount: w.Amount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
// BlockFilterCriteria encapsulates criteria passed to a `logs` accessor inside
|
|
|
|
// a block.
|
|
|
|
type BlockFilterCriteria struct {
|
|
|
|
Addresses *[]common.Address // restricts matches to events created by specific contracts
|
|
|
|
|
|
|
|
// The Topic list restricts matches to particular event topics. Each event has a list
|
|
|
|
// of topics. Topics matches a prefix of that list. An empty element slice matches any
|
|
|
|
// topic. Non-empty elements represent an alternative that matches any of the
|
|
|
|
// contained topics.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// {} or nil matches any topic list
|
|
|
|
// {{A}} matches topic A in first position
|
|
|
|
// {{}, {B}} matches any topic in first position, B in second position
|
|
|
|
// {{A}, {B}} matches topic A in first position, B in second position
|
|
|
|
// {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
|
|
|
|
Topics *[][]common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// runFilter accepts a filter and executes it, returning all its results as
|
|
|
|
// `Log` objects.
|
2022-08-03 12:08:32 -05:00
|
|
|
func runFilter(ctx context.Context, r *Resolver, filter *filters.Filter) ([]*Log, error) {
|
2019-01-21 08:38:13 -06:00
|
|
|
logs, err := filter.Logs(ctx)
|
|
|
|
if err != nil || logs == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Log, 0, len(logs))
|
|
|
|
for _, log := range logs {
|
|
|
|
ret = append(ret, &Log{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: r,
|
|
|
|
transaction: &Transaction{r: r, hash: log.TxHash},
|
2019-01-21 08:38:13 -06:00
|
|
|
log: log,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Logs(ctx context.Context, args struct{ Filter BlockFilterCriteria }) ([]*Log, error) {
|
|
|
|
var addresses []common.Address
|
|
|
|
if args.Filter.Addresses != nil {
|
|
|
|
addresses = *args.Filter.Addresses
|
|
|
|
}
|
|
|
|
var topics [][]common.Hash
|
|
|
|
if args.Filter.Topics != nil {
|
|
|
|
topics = *args.Filter.Topics
|
|
|
|
}
|
|
|
|
// Construct the range filter
|
2023-03-28 02:08:10 -05:00
|
|
|
hash, err := b.Hash(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-19 04:14:59 -05:00
|
|
|
filter := b.r.filterSystem.NewBlockFilter(hash, addresses, topics)
|
2019-01-21 08:38:13 -06:00
|
|
|
|
|
|
|
// Run the filter and return all the logs
|
2022-08-03 12:08:32 -05:00
|
|
|
return runFilter(ctx, b.r, filter)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2019-03-18 01:24:43 -05:00
|
|
|
func (b *Block) Account(ctx context.Context, args struct {
|
|
|
|
Address common.Address
|
|
|
|
}) (*Account, error) {
|
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: b.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: args.Address,
|
|
|
|
blockNrOrHash: *b.numberOrHash,
|
2019-03-18 01:24:43 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallData encapsulates arguments to `call` or `estimateGas`.
|
|
|
|
// All arguments are optional.
|
|
|
|
type CallData struct {
|
2021-06-08 05:05:41 -05:00
|
|
|
From *common.Address // The Ethereum address the call is from.
|
|
|
|
To *common.Address // The Ethereum address the call is to.
|
2023-04-25 07:02:54 -05:00
|
|
|
Gas *Long // The amount of gas provided for the call.
|
2021-06-08 05:05:41 -05:00
|
|
|
GasPrice *hexutil.Big // The price of each unit of gas, in wei.
|
|
|
|
MaxFeePerGas *hexutil.Big // The max price of each unit of gas, in wei (1559).
|
|
|
|
MaxPriorityFeePerGas *hexutil.Big // The max tip of each unit of gas, in wei (1559).
|
|
|
|
Value *hexutil.Big // The value sent along with the call.
|
|
|
|
Data *hexutil.Bytes // Any data sent with the call.
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CallResult encapsulates the result of an invocation of the `call` accessor.
|
|
|
|
type CallResult struct {
|
2023-04-25 07:02:54 -05:00
|
|
|
data hexutil.Bytes // The return data from the call
|
|
|
|
gasUsed hexutil.Uint64 // The amount of gas used
|
|
|
|
status hexutil.Uint64 // The return status of the call - 0 for failure or 1 for success.
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CallResult) Data() hexutil.Bytes {
|
|
|
|
return c.data
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (c *CallResult) GasUsed() hexutil.Uint64 {
|
2019-03-18 01:24:43 -05:00
|
|
|
return c.gasUsed
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (c *CallResult) Status() hexutil.Uint64 {
|
2019-03-18 01:24:43 -05:00
|
|
|
return c.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) Call(ctx context.Context, args struct {
|
2021-05-25 15:30:21 -05:00
|
|
|
Data ethapi.TransactionArgs
|
2019-03-18 01:24:43 -05:00
|
|
|
}) (*CallResult, error) {
|
2023-05-02 03:28:43 -05:00
|
|
|
result, err := ethapi.DoCall(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, nil, b.r.backend.RPCEVMTimeout(), b.r.backend.RPCGasCap())
|
2020-04-22 03:25:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
status := hexutil.Uint64(1)
|
2020-04-22 03:25:36 -05:00
|
|
|
if result.Failed() {
|
2019-03-18 01:24:43 -05:00
|
|
|
status = 0
|
|
|
|
}
|
2020-06-08 03:09:49 -05:00
|
|
|
|
2019-03-18 01:24:43 -05:00
|
|
|
return &CallResult{
|
2020-06-08 03:09:49 -05:00
|
|
|
data: result.ReturnData,
|
2023-04-25 07:02:54 -05:00
|
|
|
gasUsed: hexutil.Uint64(result.UsedGas),
|
2019-03-18 01:24:43 -05:00
|
|
|
status: status,
|
2020-04-22 03:25:36 -05:00
|
|
|
}, nil
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) EstimateGas(ctx context.Context, args struct {
|
2021-05-25 15:30:21 -05:00
|
|
|
Data ethapi.TransactionArgs
|
2023-04-25 07:02:54 -05:00
|
|
|
}) (hexutil.Uint64, error) {
|
2023-08-07 09:03:57 -05:00
|
|
|
return ethapi.DoEstimateGas(ctx, b.r.backend, args.Data, *b.numberOrHash, nil, b.r.backend.RPCGasCap())
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Pending struct {
|
2022-08-03 12:08:32 -05:00
|
|
|
r *Resolver
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:54 -05:00
|
|
|
func (p *Pending) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
|
2022-08-03 12:08:32 -05:00
|
|
|
txs, err := p.r.backend.GetPoolTransactions()
|
2023-04-25 07:02:54 -05:00
|
|
|
return hexutil.Uint64(len(txs)), err
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pending) Transactions(ctx context.Context) (*[]*Transaction, error) {
|
2022-08-03 12:08:32 -05:00
|
|
|
txs, err := p.r.backend.GetPoolTransactions()
|
2019-03-18 01:24:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Transaction, 0, len(txs))
|
|
|
|
for i, tx := range txs {
|
|
|
|
ret = append(ret, &Transaction{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: p.r,
|
|
|
|
hash: tx.Hash(),
|
|
|
|
tx: tx,
|
|
|
|
index: uint64(i),
|
2019-03-18 01:24:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pending) Account(ctx context.Context, args struct {
|
|
|
|
Address common.Address
|
|
|
|
}) *Account {
|
2019-09-26 03:47:31 -05:00
|
|
|
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
|
2019-03-18 01:24:43 -05:00
|
|
|
return &Account{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: p.r,
|
2019-09-26 03:47:31 -05:00
|
|
|
address: args.Address,
|
|
|
|
blockNrOrHash: pendingBlockNr,
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pending) Call(ctx context.Context, args struct {
|
2021-05-25 15:30:21 -05:00
|
|
|
Data ethapi.TransactionArgs
|
2019-03-18 01:24:43 -05:00
|
|
|
}) (*CallResult, error) {
|
2019-09-26 03:47:31 -05:00
|
|
|
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
|
2023-05-02 03:28:43 -05:00
|
|
|
result, err := ethapi.DoCall(ctx, p.r.backend, args.Data, pendingBlockNr, nil, nil, p.r.backend.RPCEVMTimeout(), p.r.backend.RPCGasCap())
|
2020-04-22 03:25:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:54 -05:00
|
|
|
status := hexutil.Uint64(1)
|
2020-04-22 03:25:36 -05:00
|
|
|
if result.Failed() {
|
2019-03-18 01:24:43 -05:00
|
|
|
status = 0
|
|
|
|
}
|
2020-06-08 03:09:49 -05:00
|
|
|
|
2019-03-18 01:24:43 -05:00
|
|
|
return &CallResult{
|
2020-06-08 03:09:49 -05:00
|
|
|
data: result.ReturnData,
|
2023-04-25 07:02:54 -05:00
|
|
|
gasUsed: hexutil.Uint64(result.UsedGas),
|
2019-03-18 01:24:43 -05:00
|
|
|
status: status,
|
2020-04-22 03:25:36 -05:00
|
|
|
}, nil
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pending) EstimateGas(ctx context.Context, args struct {
|
2021-05-25 15:30:21 -05:00
|
|
|
Data ethapi.TransactionArgs
|
2023-04-25 07:02:54 -05:00
|
|
|
}) (hexutil.Uint64, error) {
|
2023-05-11 07:23:05 -05:00
|
|
|
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
|
2023-08-07 09:03:57 -05:00
|
|
|
return ethapi.DoEstimateGas(ctx, p.r.backend, args.Data, latestBlockNr, nil, p.r.backend.RPCGasCap())
|
2019-03-18 01:24:43 -05:00
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
// Resolver is the top-level object in the GraphQL hierarchy.
|
|
|
|
type Resolver struct {
|
2022-08-19 04:14:59 -05:00
|
|
|
backend ethapi.Backend
|
|
|
|
filterSystem *filters.FilterSystem
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) Block(ctx context.Context, args struct {
|
2021-01-05 04:22:32 -06:00
|
|
|
Number *Long
|
2019-01-21 08:38:13 -06:00
|
|
|
Hash *common.Hash
|
|
|
|
}) (*Block, error) {
|
2023-08-10 06:00:52 -05:00
|
|
|
if args.Number != nil && args.Hash != nil {
|
|
|
|
return nil, errors.New("only one of number or hash must be specified")
|
|
|
|
}
|
2023-03-28 02:08:10 -05:00
|
|
|
var numberOrHash rpc.BlockNumberOrHash
|
2019-01-21 08:38:13 -06:00
|
|
|
if args.Number != nil {
|
2021-01-05 04:22:32 -06:00
|
|
|
if *args.Number < 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-11-25 14:00:23 -06:00
|
|
|
number := rpc.BlockNumber(*args.Number)
|
2023-03-28 02:08:10 -05:00
|
|
|
numberOrHash = rpc.BlockNumberOrHashWithNumber(number)
|
2019-01-21 08:38:13 -06:00
|
|
|
} else if args.Hash != nil {
|
2023-03-28 02:08:10 -05:00
|
|
|
numberOrHash = rpc.BlockNumberOrHashWithHash(*args.Hash, false)
|
2019-01-21 08:38:13 -06:00
|
|
|
} else {
|
2023-03-28 02:08:10 -05:00
|
|
|
numberOrHash = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
|
|
|
|
}
|
|
|
|
block := &Block{
|
|
|
|
r: r,
|
|
|
|
numberOrHash: &numberOrHash,
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2019-07-25 01:29:53 -05:00
|
|
|
// Resolve the header, return nil if it doesn't exist.
|
|
|
|
// Note we don't resolve block directly here since it will require an
|
|
|
|
// additional network request for light client.
|
|
|
|
h, err := block.resolveHeader(ctx)
|
2019-01-21 08:38:13 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-25 01:29:53 -05:00
|
|
|
} else if h == nil {
|
2019-01-21 08:38:13 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) Blocks(ctx context.Context, args struct {
|
2021-01-05 04:22:32 -06:00
|
|
|
From *Long
|
|
|
|
To *Long
|
2019-01-21 08:38:13 -06:00
|
|
|
}) ([]*Block, error) {
|
2021-01-05 04:22:32 -06:00
|
|
|
from := rpc.BlockNumber(*args.From)
|
2019-01-21 08:38:13 -06:00
|
|
|
|
|
|
|
var to rpc.BlockNumber
|
|
|
|
if args.To != nil {
|
|
|
|
to = rpc.BlockNumber(*args.To)
|
|
|
|
} else {
|
2023-03-02 00:29:15 -06:00
|
|
|
to = rpc.BlockNumber(r.backend.CurrentBlock().Number.Int64())
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
if to < from {
|
|
|
|
return []*Block{}, nil
|
|
|
|
}
|
2023-08-08 06:35:58 -05:00
|
|
|
var ret []*Block
|
2019-01-21 08:38:13 -06:00
|
|
|
for i := from; i <= to; i++ {
|
2019-09-26 03:47:31 -05:00
|
|
|
numberOrHash := rpc.BlockNumberOrHashWithNumber(i)
|
2022-01-05 09:11:46 -06:00
|
|
|
block := &Block{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: r,
|
2019-09-26 03:47:31 -05:00
|
|
|
numberOrHash: &numberOrHash,
|
2022-01-05 09:11:46 -06:00
|
|
|
}
|
|
|
|
// Resolve the header to check for existence.
|
|
|
|
// Note we don't resolve block directly here since it will require an
|
|
|
|
// additional network request for light client.
|
|
|
|
h, err := block.resolveHeader(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if h == nil {
|
|
|
|
// Blocks after must be non-existent too, break.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ret = append(ret, block)
|
2023-08-08 06:35:58 -05:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 01:24:43 -05:00
|
|
|
func (r *Resolver) Pending(ctx context.Context) *Pending {
|
2022-08-03 12:08:32 -05:00
|
|
|
return &Pending{r}
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 04:07:01 -05:00
|
|
|
func (r *Resolver) Transaction(ctx context.Context, args struct{ Hash common.Hash }) *Transaction {
|
2019-01-21 08:38:13 -06:00
|
|
|
tx := &Transaction{
|
2022-08-03 12:08:32 -05:00
|
|
|
r: r,
|
|
|
|
hash: args.Hash,
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
// Resolve the transaction; if it doesn't exist, return nil.
|
2023-06-05 04:07:01 -05:00
|
|
|
t, _ := tx.resolve(ctx)
|
|
|
|
if t == nil {
|
|
|
|
return nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2023-06-05 04:07:01 -05:00
|
|
|
return tx
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) SendRawTransaction(ctx context.Context, args struct{ Data hexutil.Bytes }) (common.Hash, error) {
|
|
|
|
tx := new(types.Transaction)
|
2021-02-25 08:26:57 -06:00
|
|
|
if err := tx.UnmarshalBinary(args.Data); err != nil {
|
2019-01-21 08:38:13 -06:00
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
hash, err := ethapi.SubmitTransaction(ctx, r.backend, tx)
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:19:43 -06:00
|
|
|
// FilterCriteria encapsulates the arguments to `logs` on the root resolver object.
|
2019-01-21 08:38:13 -06:00
|
|
|
type FilterCriteria struct {
|
2023-04-25 07:02:54 -05:00
|
|
|
FromBlock *Long // beginning of the queried range, nil means genesis block
|
|
|
|
ToBlock *Long // end of the range, nil means latest block
|
2019-01-21 08:38:13 -06:00
|
|
|
Addresses *[]common.Address // restricts matches to events created by specific contracts
|
|
|
|
|
|
|
|
// The Topic list restricts matches to particular event topics. Each event has a list
|
|
|
|
// of topics. Topics matches a prefix of that list. An empty element slice matches any
|
|
|
|
// topic. Non-empty elements represent an alternative that matches any of the
|
|
|
|
// contained topics.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
// {} or nil matches any topic list
|
|
|
|
// {{A}} matches topic A in first position
|
|
|
|
// {{}, {B}} matches any topic in first position, B in second position
|
|
|
|
// {{A}, {B}} matches topic A in first position, B in second position
|
|
|
|
// {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
|
|
|
|
Topics *[][]common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) Logs(ctx context.Context, args struct{ Filter FilterCriteria }) ([]*Log, error) {
|
|
|
|
// Convert the RPC block numbers into internal representations
|
|
|
|
begin := rpc.LatestBlockNumber.Int64()
|
|
|
|
if args.Filter.FromBlock != nil {
|
|
|
|
begin = int64(*args.Filter.FromBlock)
|
|
|
|
}
|
|
|
|
end := rpc.LatestBlockNumber.Int64()
|
|
|
|
if args.Filter.ToBlock != nil {
|
|
|
|
end = int64(*args.Filter.ToBlock)
|
|
|
|
}
|
|
|
|
var addresses []common.Address
|
|
|
|
if args.Filter.Addresses != nil {
|
|
|
|
addresses = *args.Filter.Addresses
|
|
|
|
}
|
|
|
|
var topics [][]common.Hash
|
|
|
|
if args.Filter.Topics != nil {
|
|
|
|
topics = *args.Filter.Topics
|
|
|
|
}
|
|
|
|
// Construct the range filter
|
2022-08-19 04:14:59 -05:00
|
|
|
filter := r.filterSystem.NewRangeFilter(begin, end, addresses, topics)
|
2022-08-03 12:08:32 -05:00
|
|
|
return runFilter(ctx, r, filter)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) GasPrice(ctx context.Context) (hexutil.Big, error) {
|
core, eth, internal, les: RPC methods and fields for EIP 1559 (#22964)
* internal/ethapi: add baseFee to RPCMarshalHeader
* internal/ethapi: add FeeCap, Tip and correct GasPrice to EIP-1559 RPCTransaction results
* core,eth,les,internal: add support for tip estimation in gas price oracle
* internal/ethapi,eth/gasprice: don't suggest tip larger than fee cap
* core/types,internal: use correct eip1559 terminology for json marshalling
* eth, internal/ethapi: fix rebase problems
* internal/ethapi: fix rpc name of basefee
* internal/ethapi: address review concerns
* core, eth, internal, les: simplify gasprice oracle (#25)
* core, eth, internal, les: simplify gasprice oracle
* eth/gasprice: fix typo
* internal/ethapi: minor tweak in tx args
* internal/ethapi: calculate basefee for pending block
* internal/ethapi: fix panic
* internal/ethapi, eth/tracers: simplify txargs ToMessage
* internal/ethapi: remove unused param
* core, eth, internal: fix regressions wrt effective gas price in the evm
* eth/gasprice: drop weird debug println
* internal/jsre/deps: hack in 1559 gas conversions into embedded web3
* internal/jsre/deps: hack basFee to decimal conversion
* internal/ethapi: init feecap and tipcap for legacy txs too
* eth, graphql, internal, les: fix gas price suggestion on all combos
* internal/jsre/deps: handle decimal tipcap and feecap
* eth, internal: minor review fixes
* graphql, internal: export max fee cap RPC endpoint
* internal/ethapi: fix crash in transaction_args
* internal/ethapi: minor refactor to make the code safer
Co-authored-by: Ryan Schneider <ryanleeschneider@gmail.com>
Co-authored-by: lightclient@protonmail.com <lightclient@protonmail.com>
Co-authored-by: gary rong <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-06-02 08:13:10 -05:00
|
|
|
tipcap, err := r.backend.SuggestGasTipCap(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Big{}, err
|
|
|
|
}
|
|
|
|
if head := r.backend.CurrentHeader(); head.BaseFee != nil {
|
|
|
|
tipcap.Add(tipcap, head.BaseFee)
|
|
|
|
}
|
|
|
|
return (hexutil.Big)(*tipcap), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) MaxPriorityFeePerGas(ctx context.Context) (hexutil.Big, error) {
|
|
|
|
tipcap, err := r.backend.SuggestGasTipCap(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Big{}, err
|
|
|
|
}
|
|
|
|
return (hexutil.Big)(*tipcap), nil
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:38:56 -05:00
|
|
|
func (r *Resolver) ChainID(ctx context.Context) (hexutil.Big, error) {
|
|
|
|
return hexutil.Big(*r.backend.ChainConfig().ChainID), nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 08:38:13 -06:00
|
|
|
// SyncState represents the synchronisation status returned from the `syncing` accessor.
|
|
|
|
type SyncState struct {
|
|
|
|
progress ethereum.SyncProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncState) StartingBlock() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.StartingBlock)
|
|
|
|
}
|
|
|
|
func (s *SyncState) CurrentBlock() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.CurrentBlock)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HighestBlock() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HighestBlock)
|
|
|
|
}
|
2021-11-26 05:26:03 -06:00
|
|
|
func (s *SyncState) SyncedAccounts() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedAccounts)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
2021-11-26 05:26:03 -06:00
|
|
|
func (s *SyncState) SyncedAccountBytes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedAccountBytes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) SyncedBytecodes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedBytecodes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) SyncedBytecodeBytes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedBytecodeBytes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) SyncedStorage() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedStorage)
|
|
|
|
}
|
|
|
|
func (s *SyncState) SyncedStorageBytes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.SyncedStorageBytes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealedTrienodes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealedTrienodes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealedTrienodeBytes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealedTrienodeBytes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealedBytecodes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealedBytecodes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealedBytecodeBytes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealedBytecodeBytes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealingTrienodes() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealingTrienodes)
|
|
|
|
}
|
|
|
|
func (s *SyncState) HealingBytecode() hexutil.Uint64 {
|
|
|
|
return hexutil.Uint64(s.progress.HealingBytecode)
|
2019-01-21 08:38:13 -06:00
|
|
|
}
|
|
|
|
|
2023-05-02 03:57:07 -05:00
|
|
|
// Syncing returns false in case the node is currently not syncing with the network. It can be up-to-date or has not
|
2019-01-21 08:38:13 -06:00
|
|
|
// yet received the latest block headers from its pears. In case it is synchronizing:
|
2023-05-02 03:57:07 -05:00
|
|
|
// - startingBlock: block number this node started to synchronize from
|
2021-11-26 05:26:03 -06:00
|
|
|
// - currentBlock: block number this node is currently importing
|
|
|
|
// - highestBlock: block number of the highest block header this node has received from peers
|
|
|
|
// - syncedAccounts: number of accounts downloaded
|
|
|
|
// - syncedAccountBytes: number of account trie bytes persisted to disk
|
|
|
|
// - syncedBytecodes: number of bytecodes downloaded
|
|
|
|
// - syncedBytecodeBytes: number of bytecode bytes downloaded
|
|
|
|
// - syncedStorage: number of storage slots downloaded
|
|
|
|
// - syncedStorageBytes: number of storage trie bytes persisted to disk
|
|
|
|
// - healedTrienodes: number of state trie nodes downloaded
|
|
|
|
// - healedTrienodeBytes: number of state trie bytes persisted to disk
|
|
|
|
// - healedBytecodes: number of bytecodes downloaded
|
|
|
|
// - healedBytecodeBytes: number of bytecodes persisted to disk
|
|
|
|
// - healingTrienodes: number of state trie nodes pending
|
|
|
|
// - healingBytecode: number of bytecodes pending
|
2019-01-21 08:38:13 -06:00
|
|
|
func (r *Resolver) Syncing() (*SyncState, error) {
|
2021-09-10 02:55:48 -05:00
|
|
|
progress := r.backend.SyncProgress()
|
2019-01-21 08:38:13 -06:00
|
|
|
|
|
|
|
// Return not syncing if the synchronisation already completed
|
|
|
|
if progress.CurrentBlock >= progress.HighestBlock {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Otherwise gather the block sync stats
|
|
|
|
return &SyncState{progress}, nil
|
|
|
|
}
|