From b90fcb53e6ffc126f3948c1c38dd84a512367b7d Mon Sep 17 00:00:00 2001 From: Ashwin Phatak Date: Tue, 25 May 2021 11:40:02 +0530 Subject: [PATCH] Get logs API, with receipt CID and raw IPLD block. --- pkg/graphql/graphql.go | 46 ++++++++++++++++++++++++++++++++++++++++++ pkg/graphql/schema.go | 11 +++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 19dfabc7..6a590ea7 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -28,6 +28,7 @@ import ( "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/rlp" "github.com/ethereum/go-ethereum/rpc" "github.com/vulcanize/ipld-eth-server/pkg/eth" @@ -954,3 +955,48 @@ func (r *Resolver) Logs(ctx context.Context, args struct{ Filter FilterCriteria filter := filters.NewRangeFilter(filters.Backend(r.backend), begin, end, addresses, topics) return runFilter(ctx, r.backend, filter) } + +func (r *Resolver) GetStorageAt(ctx context.Context, args struct { + BlockHash common.Hash + Contract common.Address + Slot common.Hash +}) (*common.Hash, error) { + ret := common.BytesToHash([]byte{}) + + return &ret, nil +} + +func (r *Resolver) GetLogs(ctx context.Context, args struct { + BlockHash common.Hash + Contract common.Address +}) (*[]*Log, error) { + ret := make([]*Log, 0, 10) + + receiptCIDs, receiptsBytes, err := r.backend.IPLDRetriever.RetrieveReceiptsByBlockHash(args.BlockHash) + if err != nil { + return nil, err + } + + receipts := make(types.Receipts, len(receiptsBytes)) + for index, receiptBytes := range receiptsBytes { + receiptCID := receiptCIDs[index] + receipt := new(types.Receipt) + if err := rlp.DecodeBytes(receiptBytes, receipt); err != nil { + return nil, err + } + + receipts[index] = receipt + for _, log := range receipt.Logs { + if log.Address == args.Contract { + ret = append(ret, &Log{ + backend: r.backend, + log: log, + cid: receiptCID, + ipldBlock: receiptBytes, + }) + } + } + } + + return &ret, nil +} diff --git a/pkg/graphql/schema.go b/pkg/graphql/schema.go index de989e74..f2208db9 100644 --- a/pkg/graphql/schema.go +++ b/pkg/graphql/schema.go @@ -65,7 +65,7 @@ const schema string = ` # Data is unindexed data for this log. data: Bytes! # Transaction is the transaction that generated this log entry. - transaction: Transaction! + transaction: Transaction # CID for the Receipt IPLD block this Log exists in. cid: String! @@ -269,12 +269,21 @@ const schema string = ` # Block fetches an Ethereum block by number or by hash. If neither is # supplied, the most recent known block is returned. block(number: Long, hash: Bytes32): Block + # Blocks returns all the blocks between two numbers, inclusive. If # to is not supplied, it defaults to the most recent known block. blocks(from: Long!, to: Long): [Block!]! + # Transaction returns a transaction specified by its hash. transaction(hash: Bytes32!): Transaction + # Logs returns log entries matching the provided filter. logs(filter: FilterCriteria!): [Log!]! + + # Get storage slot by block hash and contract address. + getStorageAt(blockHash: Bytes32!, contract: Address!, slot: Bytes32!): Bytes32 + + # Get contract logs by block hash and contract address. + getLogs(blockHash: Bytes32!, contract: Address!): [Log!] } `