getLogs API changes to return txHash, make contract arg optional. (#81)
* getLogs API changes to return txHash, make contract arg optional. * Populate log index. * Add test for txn hash in GetLogs request. * Convert tx string to common.Hash after fetching. Co-authored-by: Arijit Das <arijitad.in@gmail.com>
This commit was merged in pull request #81.
This commit is contained in:
co-authored by
Arijit Das
parent
afc63ac960
commit
70f7face75
+10
-2
@@ -21,8 +21,13 @@ type GetStorageAt struct {
|
||||
}
|
||||
|
||||
type LogResponse struct {
|
||||
Topics []common.Hash `json:"topics"`
|
||||
Data hexutil.Bytes `json:"data"`
|
||||
Topics []common.Hash `json:"topics"`
|
||||
Data hexutil.Bytes `json:"data"`
|
||||
Transaction TransactionResp `json:"transaction"`
|
||||
}
|
||||
|
||||
type TransactionResp struct {
|
||||
Hash common.Hash `json:"hash"`
|
||||
}
|
||||
|
||||
type GetLogs struct {
|
||||
@@ -44,6 +49,9 @@ func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address common.A
|
||||
getLogs(blockHash: "%s", contract: "%s") {
|
||||
data
|
||||
topics
|
||||
transaction {
|
||||
hash
|
||||
}
|
||||
}
|
||||
}
|
||||
`, hash.String(), address.String())
|
||||
|
||||
+10
-3
@@ -1005,15 +1005,16 @@ func (r *Resolver) GetStorageAt(ctx context.Context, args struct {
|
||||
|
||||
func (r *Resolver) GetLogs(ctx context.Context, args struct {
|
||||
BlockHash common.Hash
|
||||
Contract common.Address
|
||||
Contract *common.Address
|
||||
}) (*[]*Log, error) {
|
||||
ret := make([]*Log, 0, 10)
|
||||
|
||||
receiptCIDs, receiptsBytes, err := r.backend.IPLDRetriever.RetrieveReceiptsByBlockHash(args.BlockHash)
|
||||
receiptCIDs, receiptsBytes, txs, err := r.backend.IPLDRetriever.RetrieveReceiptsByBlockHash(args.BlockHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var logIndexInBlock uint = 0
|
||||
receipts := make(types.Receipts, len(receiptsBytes))
|
||||
for index, receiptBytes := range receiptsBytes {
|
||||
receiptCID := receiptCIDs[index]
|
||||
@@ -1024,12 +1025,18 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct {
|
||||
|
||||
receipts[index] = receipt
|
||||
for _, log := range receipt.Logs {
|
||||
if log.Address == args.Contract {
|
||||
log.Index = logIndexInBlock
|
||||
logIndexInBlock++
|
||||
|
||||
if args.Contract == nil || *args.Contract == log.Address {
|
||||
ret = append(ret, &Log{
|
||||
backend: r.backend,
|
||||
log: log,
|
||||
cid: receiptCID,
|
||||
ipldBlock: receiptBytes,
|
||||
transaction: &Transaction{
|
||||
hash: txs[index],
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +161,9 @@ var _ = Describe("GraphQL", func() {
|
||||
|
||||
expectedLogs := []graphql.LogResponse{
|
||||
{
|
||||
Topics: test_helpers.MockLog1.Topics,
|
||||
Data: hexutil.Bytes(test_helpers.MockLog1.Data),
|
||||
Topics: test_helpers.MockLog1.Topics,
|
||||
Data: hexutil.Bytes(test_helpers.MockLog1.Data),
|
||||
Transaction: graphql.TransactionResp{Hash: test_helpers.MockTransactions[0].Hash()},
|
||||
},
|
||||
}
|
||||
Expect(logs).To(Equal(expectedLogs))
|
||||
|
||||
@@ -295,6 +295,6 @@ const schema string = `
|
||||
getStorageAt(blockHash: Bytes32!, contract: Address!, slot: Bytes32!): StorageResult
|
||||
|
||||
# Get contract logs by block hash and contract address.
|
||||
getLogs(blockHash: Bytes32!, contract: Address!): [Log!]
|
||||
getLogs(blockHash: Bytes32!, contract: Address): [Log!]
|
||||
}
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user