Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #49 from itzmeanjan/develop
Browse files Browse the repository at this point in the history
REST & GraphQL API for fetching event log using block hash/ number & log index
  • Loading branch information
itzmeanjan authored Jan 20, 2021
2 parents 769db23 + e9bba2b commit f296956
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ Query Params | Method | Description
Query Params | Method | Description
--- | --- | ---
`blockHash=0x...` | GET | Given blockhash, retrieves all events emitted by tx(s) present in block
`blockHash=0x...&logIndex=1` | GET | Given blockhash and log index in block, attempts to retrieve associated event
`blockNumber=123456&logIndex=2` | GET | Given block number and log index in block, attempts to retrieve associated event
`txHash=0x...` | GET | Given txhash, retrieves all events emitted during execution of this transaction
`count=50&contract=0x...` | GET | Returns last **x** _( <=50 )_ events emitted by this contract
`fromBlock=1&toBlock=10&contract=0x...&topic0=0x...&topic1=0x...&topic2=0x...&topic3=0x...` | GET | Finding event(s) emitted from contract within given block range & also matching topic signatures _{0, 1, 2, 3}_
Expand Down Expand Up @@ -386,6 +388,8 @@ type Transaction {
from: String!
to: String!
contract: String!
value: String!
data: String!
gas: String!
gasPrice: String!
cost: String!
Expand All @@ -412,6 +416,8 @@ type Query {
eventsFromContractWithTopicsByNumberRange(contract: String!, from: String!, to: String!, topics: [String!]!): [Event!]!
eventsFromContractWithTopicsByTimeRange(contract: String!, from: String!, to: String!, topics: [String!]!): [Event!]!
lastXEventsFromContract(contract: String!, x: Int!): [Event!]!
eventByBlockHashAndLogIndex(hash: String!, index: String!): Event!
eventByBlockNumberAndLogIndex(number: String!, index: String!): Event!
}
```

Expand Down
36 changes: 36 additions & 0 deletions app/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,39 @@ func GetLastXEventsFromContract(db *gorm.DB, contract common.Address, x int) *da
}

}

// GetEventByBlockHashAndLogIndex - Given block hash and log index in block
// return respective event log, if any exists
func GetEventByBlockHashAndLogIndex(db *gorm.DB, hash common.Hash, index uint) *data.Event {

var event data.Event

if err := db.Model(&Events{}).Where("blockhash = ? and index = ?", hash.Hex(), index).First(&event).Error; err != nil {
return nil
}

return &event

}

// GetEventByBlockNumberAndLogIndex - Given block number and log index in block
// return respective event log, if any exists
func GetEventByBlockNumberAndLogIndex(db *gorm.DB, number uint64, index uint) *data.Event {

block := GetBlockByNumber(db, number)
// seems bad block number or may be `ette`
// hasn't synced upto this point or missed
// this block some how
if block == nil {
return nil
}

var event data.Event

if err := db.Model(&Events{}).Where("blockhash = ? and index = ?", block.Hash, index).First(&event).Error; err != nil {
return nil
}

return &event

}
194 changes: 194 additions & 0 deletions app/rest/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/rest/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ type Query {
eventsFromContractWithTopicsByNumberRange(contract: String!, from: String!, to: String!, topics: [String!]!): [Event!]!
eventsFromContractWithTopicsByTimeRange(contract: String!, from: String!, to: String!, topics: [String!]!): [Event!]!
lastXEventsFromContract(contract: String!, x: Int!): [Event!]!
eventByBlockHashAndLogIndex(hash: String!, index: String!): Event!
eventByBlockNumberAndLogIndex(number: String!, index: String!): Event!
}
31 changes: 31 additions & 0 deletions app/rest/graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,37 @@ func (r *queryResolver) LastXEventsFromContract(ctx context.Context, contract st
return getGraphQLCompatibleEvents(_db.GetLastXEventsFromContract(db, common.HexToAddress(contract), x))
}

func (r *queryResolver) EventByBlockHashAndLogIndex(ctx context.Context, hash string, index string) (*model.Event, error) {

if !(strings.HasPrefix(hash, "0x") && len(hash) == 66) {
return nil, errors.New("Bad Block Hash")
}

_index, err := strconv.ParseUint(index, 10, 64)
if err != nil {
return nil, errors.New("Bad Log Index")
}

return getGraphQLCompatibleEvent(_db.GetEventByBlockHashAndLogIndex(db, common.HexToHash(hash), uint(_index)))

}

func (r *queryResolver) EventByBlockNumberAndLogIndex(ctx context.Context, number string, index string) (*model.Event, error) {

_number, err := strconv.ParseUint(number, 10, 64)
if err != nil {
return nil, errors.New("Bad Block Number")
}

_index, err := strconv.ParseUint(index, 10, 64)
if err != nil {
return nil, errors.New("Bad Log Index")
}

return getGraphQLCompatibleEvent(_db.GetEventByBlockNumberAndLogIndex(db, _number, uint(_index)))

}

// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

Expand Down
Loading

0 comments on commit f296956

Please sign in to comment.