-
Notifications
You must be signed in to change notification settings - Fork 20.2k
/
graphql.go
1420 lines (1292 loc) · 38.7 KB
/
graphql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The go-ethereum Authors
// 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"
"errors"
"fmt"
"math/big"
"sort"
"strconv"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus/misc"
"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"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
)
var (
errBlockInvariant = errors.New("block objects must be instantiated with at least one of num or hash")
)
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
//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
//}
case int32:
*b = Long(input)
case int64:
*b = Long(input)
case float64:
*b = Long(input)
default:
err = fmt.Errorf("unexpected type %T for Long", input)
}
return err
}
// Account represents an Ethereum account at a particular block.
type Account struct {
r *Resolver
address common.Address
blockNrOrHash rpc.BlockNumberOrHash
}
// getState fetches the StateDB object for an account.
func (a *Account) getState(ctx context.Context) (*state.StateDB, error) {
state, _, err := a.r.backend.StateAndHeaderByNumberOrHash(ctx, a.blockNrOrHash)
return state, err
}
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
}
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
}
func (a *Account) TransactionCount(ctx context.Context) (hexutil.Uint64, error) {
// Ask transaction pool for the nonce which includes pending transactions
if blockNr, ok := a.blockNrOrHash.Number(); ok && blockNr == rpc.PendingBlockNumber {
nonce, err := a.r.backend.GetPoolNonce(ctx, a.address)
if err != nil {
return 0, err
}
return hexutil.Uint64(nonce), nil
}
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
}
return state.GetCode(a.address), nil
}
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 {
r *Resolver
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{
r: l.r,
address: l.log.Address,
blockNrOrHash: args.NumberOrLatest(),
}
}
func (l *Log) Index(ctx context.Context) int32 {
return int32(l.log.Index)
}
func (l *Log) Topics(ctx context.Context) []common.Hash {
return l.log.Topics
}
func (l *Log) Data(ctx context.Context) hexutil.Bytes {
return l.log.Data
}
// AccessTuple represents EIP-2930
type AccessTuple struct {
address common.Address
storageKeys []common.Hash
}
func (at *AccessTuple) Address(ctx context.Context) common.Address {
return at.address
}
func (at *AccessTuple) StorageKeys(ctx context.Context) []common.Hash {
return at.storageKeys
}
// Transaction represents an Ethereum transaction.
// backend and hash are mandatory; all others will be fetched when required.
type Transaction struct {
r *Resolver
hash common.Hash
tx *types.Transaction
block *Block
index uint64
}
// resolve returns the internal transaction object, fetching it if needed.
func (t *Transaction) resolve(ctx context.Context) (*types.Transaction, error) {
if t.tx == nil {
// 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,
}
t.index = index
return t.tx, nil
}
// No finalized transaction, try to retrieve it from the pool
t.tx = t.r.backend.GetPoolTransaction(t.hash)
}
return t.tx, nil
}
func (t *Transaction) Hash(ctx context.Context) common.Hash {
return t.hash
}
func (t *Transaction) InputData(ctx context.Context) (hexutil.Bytes, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Bytes{}, err
}
return tx.Data(), nil
}
func (t *Transaction) Gas(ctx context.Context) (hexutil.Uint64, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return 0, err
}
return hexutil.Uint64(tx.Gas()), nil
}
func (t *Transaction) GasPrice(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Big{}, err
}
switch tx.Type() {
case types.AccessListTxType:
return hexutil.Big(*tx.GasPrice()), nil
case types.DynamicFeeTxType:
if t.block != nil {
if baseFee, _ := t.block.BaseFeePerGas(ctx); baseFee != nil {
// price = min(tip, gasFeeCap - baseFee) + baseFee
return (hexutil.Big)(*math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee.ToInt()), tx.GasFeeCap())), nil
}
}
return hexutil.Big(*tx.GasPrice()), nil
default:
return hexutil.Big(*tx.GasPrice()), nil
}
}
func (t *Transaction) EffectiveGasPrice(ctx context.Context) (*hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
// Pending tx
if t.block == nil {
return nil, nil
}
header, err := t.block.resolveHeader(ctx)
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
}
func (t *Transaction) MaxFeePerGas(ctx context.Context) (*hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
switch tx.Type() {
case types.AccessListTxType:
return nil, nil
case types.DynamicFeeTxType:
return (*hexutil.Big)(tx.GasFeeCap()), nil
default:
return nil, nil
}
}
func (t *Transaction) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
switch tx.Type() {
case types.AccessListTxType:
return nil, nil
case types.DynamicFeeTxType:
return (*hexutil.Big)(tx.GasTipCap()), nil
default:
return nil, nil
}
}
func (t *Transaction) EffectiveTip(ctx context.Context) (*hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
// Pending tx
if t.block == nil {
return nil, nil
}
header, err := t.block.resolveHeader(ctx)
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
}
func (t *Transaction) Value(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Big{}, err
}
if tx.Value() == nil {
return hexutil.Big{}, fmt.Errorf("invalid transaction value %x", t.hash)
}
return hexutil.Big(*tx.Value()), nil
}
func (t *Transaction) Nonce(ctx context.Context) (hexutil.Uint64, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return 0, err
}
return hexutil.Uint64(tx.Nonce()), nil
}
func (t *Transaction) To(ctx context.Context, args BlockNumberArgs) (*Account, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
to := tx.To()
if to == nil {
return nil, nil
}
return &Account{
r: t.r,
address: *to,
blockNrOrHash: args.NumberOrLatest(),
}, nil
}
func (t *Transaction) From(ctx context.Context, args BlockNumberArgs) (*Account, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
signer := types.LatestSigner(t.r.backend.ChainConfig())
from, _ := types.Sender(signer, tx)
return &Account{
r: t.r,
address: from,
blockNrOrHash: args.NumberOrLatest(),
}, nil
}
func (t *Transaction) Block(ctx context.Context) (*Block, error) {
if _, err := t.resolve(ctx); err != nil {
return nil, err
}
return t.block, nil
}
func (t *Transaction) Index(ctx context.Context) (*int32, error) {
if _, err := t.resolve(ctx); err != nil {
return nil, err
}
if t.block == nil {
return nil, nil
}
index := int32(t.index)
return &index, nil
}
// getReceipt returns the receipt associated with this transaction, if any.
func (t *Transaction) getReceipt(ctx context.Context) (*types.Receipt, error) {
if _, err := t.resolve(ctx); err != nil {
return nil, err
}
if t.block == nil {
return nil, nil
}
receipts, err := t.block.resolveReceipts(ctx)
if err != nil {
return nil, err
}
return receipts[t.index], nil
}
func (t *Transaction) Status(ctx context.Context) (*Long, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return nil, err
}
if len(receipt.PostState) != 0 {
return nil, nil
}
ret := Long(receipt.Status)
return &ret, nil
}
func (t *Transaction) GasUsed(ctx context.Context) (*Long, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return nil, err
}
ret := Long(receipt.GasUsed)
return &ret, nil
}
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*Long, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return nil, err
}
ret := Long(receipt.CumulativeGasUsed)
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{
r: t.r,
address: receipt.ContractAddress,
blockNrOrHash: args.NumberOrLatest(),
}, nil
}
func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) {
if _, err := t.resolve(ctx); err != nil {
return nil, err
}
if t.block == nil {
return nil, nil
}
if _, ok := t.block.numberOrHash.Hash(); !ok {
header, err := t.r.backend.HeaderByNumberOrHash(ctx, *t.block.numberOrHash)
if err != nil {
return nil, err
}
hash := header.Hash()
t.block.numberOrHash.BlockHash = &hash
}
return t.getLogs(ctx)
}
// getLogs returns log objects for the given tx.
// Assumes block hash is resolved.
func (t *Transaction) getLogs(ctx context.Context) (*[]*Log, error) {
var (
hash, _ = t.block.numberOrHash.Hash()
filter = t.r.filterSystem.NewBlockFilter(hash, nil, nil)
logs, err = filter.Logs(ctx)
)
if err != nil {
return nil, err
}
var ret []*Log
// Select tx logs from all block logs
ix := sort.Search(len(logs), func(i int) bool { return uint64(logs[i].TxIndex) >= t.index })
for ix < len(logs) && uint64(logs[ix].TxIndex) == t.index {
ret = append(ret, &Log{
r: t.r,
transaction: t,
log: logs[ix],
})
ix++
}
return &ret, nil
}
func (t *Transaction) Type(ctx context.Context) (*int32, error) {
tx, err := t.resolve(ctx)
if err != nil {
return nil, err
}
txType := int32(tx.Type())
return &txType, nil
}
func (t *Transaction) AccessList(ctx context.Context) (*[]*AccessTuple, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return nil, err
}
accessList := tx.AccessList()
ret := make([]*AccessTuple, 0, len(accessList))
for _, al := range accessList {
ret = append(ret, &AccessTuple{
address: al.Address,
storageKeys: al.StorageKeys,
})
}
return &ret, nil
}
func (t *Transaction) R(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Big{}, err
}
_, r, _ := tx.RawSignatureValues()
return hexutil.Big(*r), nil
}
func (t *Transaction) S(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Big{}, err
}
_, _, s := tx.RawSignatureValues()
return hexutil.Big(*s), nil
}
func (t *Transaction) V(ctx context.Context) (hexutil.Big, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Big{}, err
}
v, _, _ := tx.RawSignatureValues()
return hexutil.Big(*v), nil
}
func (t *Transaction) Raw(ctx context.Context) (hexutil.Bytes, error) {
tx, err := t.resolve(ctx)
if err != nil || tx == nil {
return hexutil.Bytes{}, err
}
return tx.MarshalBinary()
}
func (t *Transaction) RawReceipt(ctx context.Context) (hexutil.Bytes, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return hexutil.Bytes{}, err
}
return receipt.MarshalBinary()
}
type BlockType int
// Block represents an Ethereum block.
// backend, and numberOrHash are mandatory. All other fields are lazily fetched
// when required.
type Block struct {
r *Resolver
numberOrHash *rpc.BlockNumberOrHash
hash common.Hash
header *types.Header
block *types.Block
receipts []*types.Receipt
}
// resolve returns the internal Block object representing this block, fetching
// it if necessary.
func (b *Block) resolve(ctx context.Context) (*types.Block, error) {
if b.block != nil {
return b.block, nil
}
if b.numberOrHash == nil {
latest := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
b.numberOrHash = &latest
}
var err error
b.block, err = b.r.backend.BlockByNumberOrHash(ctx, *b.numberOrHash)
if b.block != nil && b.header == nil {
b.header = b.block.Header()
if hash, ok := b.numberOrHash.Hash(); ok {
b.hash = hash
}
}
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) {
if b.numberOrHash == nil && b.hash == (common.Hash{}) {
return nil, errBlockInvariant
}
var err error
if b.header == nil {
if b.hash != (common.Hash{}) {
b.header, err = b.r.backend.HeaderByHash(ctx, b.hash)
} else {
b.header, err = b.r.backend.HeaderByNumberOrHash(ctx, *b.numberOrHash)
}
}
return b.header, err
}
// resolveReceipts returns the list of receipts for this block, fetching them
// if necessary.
func (b *Block) resolveReceipts(ctx context.Context) ([]*types.Receipt, error) {
if b.receipts == nil {
hash := b.hash
if hash == (common.Hash{}) {
header, err := b.resolveHeader(ctx)
if err != nil {
return nil, err
}
hash = header.Hash()
}
receipts, err := b.r.backend.GetReceipts(ctx, hash)
if err != nil {
return nil, err
}
b.receipts = receipts
}
return b.receipts, nil
}
func (b *Block) Number(ctx context.Context) (Long, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return 0, err
}
return Long(header.Number.Uint64()), nil
}
func (b *Block) Hash(ctx context.Context) (common.Hash, error) {
if b.hash == (common.Hash{}) {
header, err := b.resolveHeader(ctx)
if err != nil {
return common.Hash{}, err
}
b.hash = header.Hash()
}
return b.hash, nil
}
func (b *Block) GasLimit(ctx context.Context) (Long, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return 0, err
}
return Long(header.GasLimit), nil
}
func (b *Block) GasUsed(ctx context.Context) (Long, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return 0, err
}
return Long(header.GasUsed), nil
}
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
}
func (b *Block) NextBaseFeePerGas(ctx context.Context) (*hexutil.Big, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return nil, err
}
chaincfg := b.r.backend.ChainConfig()
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
}
}
nextBaseFee := misc.CalcBaseFee(chaincfg, header)
return (*hexutil.Big)(nextBaseFee), nil
}
func (b *Block) Parent(ctx context.Context) (*Block, error) {
if _, err := b.resolveHeader(ctx); err != nil {
return nil, err
}
if b.header == nil || b.header.Number.Uint64() < 1 {
return nil, nil
}
num := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(b.header.Number.Uint64() - 1))
return &Block{
r: b.r,
numberOrHash: &num,
hash: b.header.ParentHash,
}, nil
}
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
}
func (b *Block) Timestamp(ctx context.Context) (hexutil.Uint64, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return 0, err
}
return hexutil.Uint64(header.Time), nil
}
func (b *Block) Nonce(ctx context.Context) (hexutil.Bytes, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return hexutil.Bytes{}, err
}
return header.Nonce[:], nil
}
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
}
func (b *Block) OmmerCount(ctx context.Context) (*int32, error) {
block, err := b.resolve(ctx)
if err != nil || block == nil {
return nil, err
}
count := int32(len(block.Uncles()))
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() {
blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
ret = append(ret, &Block{
r: b.r,
numberOrHash: &blockNumberOrHash,
header: uncle,
})
}
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
}
return header.Extra, nil
}
func (b *Block) LogsBloom(ctx context.Context) (hexutil.Bytes, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return hexutil.Bytes{}, err
}
return header.Bloom.Bytes(), nil
}
func (b *Block) TotalDifficulty(ctx context.Context) (hexutil.Big, error) {
h := b.hash
if h == (common.Hash{}) {
header, err := b.resolveHeader(ctx)
if err != nil {
return hexutil.Big{}, err
}
h = header.Hash()
}
td := b.r.backend.GetTd(ctx, h)
if td == nil {
return hexutil.Big{}, fmt.Errorf("total difficulty not found %x", b.hash)
}
return hexutil.Big(*td), nil
}
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)
}
// BlockNumberArgs encapsulates arguments to accessors that specify a block number.
type BlockNumberArgs struct {
// 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
Block *hexutil.Uint64
}
// NumberOr returns the provided block number argument, or the "current" block number or hash if none
// was provided.
func (a BlockNumberArgs) NumberOr(current rpc.BlockNumberOrHash) rpc.BlockNumberOrHash {
if a.Block != nil {
blockNr := rpc.BlockNumber(*a.Block)
return rpc.BlockNumberOrHashWithNumber(blockNr)
}
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))
}
func (b *Block) Miner(ctx context.Context, args BlockNumberArgs) (*Account, error) {
header, err := b.resolveHeader(ctx)
if err != nil {
return nil, err
}
return &Account{
r: b.r,
address: header.Coinbase,
blockNrOrHash: args.NumberOrLatest(),
}, nil
}
func (b *Block) TransactionCount(ctx context.Context) (*int32, error) {
block, err := b.resolve(ctx)
if err != nil || block == nil {
return nil, err
}
count := int32(len(block.Transactions()))
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{
r: b.r,
hash: tx.Hash(),
tx: tx,
block: b,
index: uint64(i),
})
}
return &ret, nil
}
func (b *Block) TransactionAt(ctx context.Context, args struct{ Index int32 }) (*Transaction, error) {
block, err := b.resolve(ctx)
if err != nil || block == nil {
return nil, err
}
txs := block.Transactions()
if args.Index < 0 || int(args.Index) >= len(txs) {
return nil, nil
}
tx := txs[args.Index]
return &Transaction{
r: b.r,
hash: tx.Hash(),
tx: tx,
block: b,
index: uint64(args.Index),
}, nil
}
func (b *Block) OmmerAt(ctx context.Context, args struct{ Index int32 }) (*Block, error) {
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]
blockNumberOrHash := rpc.BlockNumberOrHashWithHash(uncle.Hash(), false)
return &Block{
r: b.r,
numberOrHash: &blockNumberOrHash,
header: uncle,
}, nil
}
// 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.
func runFilter(ctx context.Context, r *Resolver, filter *filters.Filter) ([]*Log, error) {
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{
r: r,
transaction: &Transaction{r: r, hash: log.TxHash},
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
}
hash := b.hash