Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/thegraph-data-access/src/queries.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 {
Expand Down
25 changes: 5 additions & 20 deletions packages/thegraph-data-access/src/subgraph-client.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -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,
Expand DownExpand Up@@ -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<StorageTypes.IGetTransactionsResponse> {
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<TransactionsBody>(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),
Expand Down