Change getLogs GQL API to take nil contract address.

This commit is contained in:
Arijit Das 2021-09-02 19:12:33 +05:30
parent 967c148eff
commit 121c75cc1d
3 changed files with 24 additions and 14 deletions

View File

@ -45,10 +45,14 @@ func NewClient(endpoint string) *Client {
return &Client{client: client} return &Client{client: client}
} }
func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address common.Address) ([]LogResponse, error) { func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address *common.Address) ([]LogResponse, error) {
getLogsQuery := fmt.Sprintf(` params := fmt.Sprintf(`blockHash: "%s"`, hash.String())
query{ if address != nil {
getLogs(blockHash: "%s", contract: "%s") { params += fmt.Sprintf(`, contract: "%s"`, address.String())
}
getLogsQuery := fmt.Sprintf(`query{
getLogs(%s) {
data data
topics topics
transaction { transaction {
@ -57,8 +61,7 @@ func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address common.A
status status
receiptCID receiptCID
} }
} }`, params)
`, hash.String(), address.String())
req := gqlclient.NewRequest(getLogsQuery) req := gqlclient.NewRequest(getLogsQuery)
req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Cache-Control", "no-cache")

View File

@ -1025,7 +1025,11 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct {
BlockHash common.Hash BlockHash common.Hash
Contract *common.Address Contract *common.Address
}) (*[]*Log, error) { }) (*[]*Log, error) {
ret := make([]*Log, 0, 10)
var filter eth.ReceiptFilter
if args.Contract != nil {
filter.LogAddresses = []string{args.Contract.String()}
}
// Begin tx // Begin tx
tx, err := r.backend.DB.Beginx() tx, err := r.backend.DB.Beginx()
@ -1033,10 +1037,6 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct {
return nil, err return nil, err
} }
filter := eth.ReceiptFilter{
LogAddresses: []string{args.Contract.String()},
}
filteredLogs, err := r.backend.Retriever.RetrieveFilteredGQLLogs(tx, filter, &args.BlockHash) filteredLogs, err := r.backend.Retriever.RetrieveFilteredGQLLogs(tx, filter, &args.BlockHash)
if err != nil { if err != nil {
return nil, err return nil, err
@ -1051,6 +1051,7 @@ func (r *Resolver) GetLogs(ctx context.Context, args struct {
return nil, err return nil, err
} }
ret := make([]*Log, 0, 10)
for _, l := range rctLog { for _, l := range rctLog {
ret = append(ret, &Log{ ret = append(ret, &Log{
backend: r.backend, backend: r.backend,

View File

@ -183,7 +183,7 @@ var _ = Describe("GraphQL", func() {
Describe("eth_getLogs", func() { Describe("eth_getLogs", func() {
It("Retrieves logs that matches the provided blockHash and contract address", func() { It("Retrieves logs that matches the provided blockHash and contract address", func() {
logs, err := client.GetLogs(ctx, blockHash, contractAddress) logs, err := client.GetLogs(ctx, blockHash, &contractAddress)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
expectedLogs := []graphql.LogResponse{ expectedLogs := []graphql.LogResponse{
@ -200,7 +200,7 @@ var _ = Describe("GraphQL", func() {
}) })
It("Retrieves logs for the failed receipt status that matches the provided blockHash and another contract address", func() { It("Retrieves logs for the failed receipt status that matches the provided blockHash and another contract address", func() {
logs, err := client.GetLogs(ctx, blockHash, test_helpers.AnotherAddress2) logs, err := client.GetLogs(ctx, blockHash, &test_helpers.AnotherAddress2)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
expectedLogs := []graphql.LogResponse{ expectedLogs := []graphql.LogResponse{
@ -216,8 +216,14 @@ var _ = Describe("GraphQL", func() {
Expect(logs).To(Equal(expectedLogs)) Expect(logs).To(Equal(expectedLogs))
}) })
It("Retrieves all the logs for the receipt that matches the provided blockHash and nil contract address", func() {
logs, err := client.GetLogs(ctx, blockHash, nil)
Expect(err).ToNot(HaveOccurred())
Expect(len(logs)).To(Equal(6))
})
It("Retrieves logs with random hash", func() { It("Retrieves logs with random hash", func() {
logs, err := client.GetLogs(ctx, randomHash, contractAddress) logs, err := client.GetLogs(ctx, randomHash, &contractAddress)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(len(logs)).To(Equal(0)) Expect(len(logs)).To(Equal(0))
}) })