diff --git a/packages/thegraph-data-access/src/queries.ts b/packages/thegraph-data-access/src/queries.ts index 81472ea9b4..42c811902a 100644 --- a/packages/thegraph-data-access/src/queries.ts +++ b/packages/thegraph-data-access/src/queries.ts @@ -74,19 +74,22 @@ export const GetTransactionsByHashQuery = gql` } `; -export const GetChannelsByTopicsQuery = gql` - ${TransactionsBodyFragment} - query GetChannelsByTopics($topics: [String!]!) { - ${metaQueryBody} +export const GetTransactionsByTopics = gql` +${TransactionsBodyFragment} + +query GetTransactionsByTopics($topics: [String!]!){ + ${metaQueryBody} + channels( + where: { topics_contains: $topics } + ){ transactions( - where: { topics_contains: $topics } - orderBy: blockTimestamp + orderBy: blockTimestamp, orderDirection: asc ) { - channelId + ...TransactionsBody } } -`; +}`; export const GetBlockQuery = gql` query GetBlock { diff --git a/packages/thegraph-data-access/src/subgraph-client.ts b/packages/thegraph-data-access/src/subgraph-client.ts index ccb78708b9..175f32c838 100644 --- a/packages/thegraph-data-access/src/subgraph-client.ts +++ b/packages/thegraph-data-access/src/subgraph-client.ts @@ -2,10 +2,10 @@ import { DataAccessTypes, StorageTypes } from '@requestnetwork/types'; import { GraphQLClient } from 'graphql-request'; import { GetBlockQuery, - GetChannelsByTopicsQuery, GetTransactionByDataHashQuery, GetTransactionsByChannelIdQuery, GetTransactionsByHashQuery, + GetTransactionsByTopics, Meta, Transaction, TransactionsBody, @@ -48,29 +48,14 @@ export class SubgraphClient implements StorageTypes.IIndexer { }); } - // FIXME: this should be possible to do in a single query to the subgraph, - // but currently one transaction doesn't contain topics from previous ones on the same channel. - // This could be fixed on the Subgraph indexer code for optimization. public async getTransactionsByTopics( topics: string[], ): Promise { - const { _meta, transactions } = await this.graphql.request< - Meta & { transactions: { channelId: string }[] } - >(GetChannelsByTopicsQuery, { topics }); + const { _meta, channels } = await this.graphql.request< + Meta & { channels: { transactions: Transaction[] }[] } + >(GetTransactionsByTopics, { topics }); - const channelIds = transactions - .map((x) => x.channelId) - .filter((val, i, self) => self.indexOf(val) === i); - const transactionsByChannel = await Promise.all( - channelIds.map((channelId) => - this.graphql - .request(GetTransactionsByChannelIdQuery, { - channelId, - ...this.getTimeVariables({}), - }) - .then((x) => x.transactions), - ), - ).then((x) => x.flat()); + const transactionsByChannel = channels.map(({ transactions }) => transactions).flat(); return { transactions: transactionsByChannel.map(this.toIndexedTransaction),