From 3dc148511362fdfc3d6202fed9f4cabdb1e02aaf Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:41:29 +0100 Subject: [PATCH 1/2] fix(thegraph): optimize getTransactionsByTopics --- packages/thegraph-data-access/src/queries.ts | 22 ++++++++-------- .../src/subgraph-client.ts | 25 ++++--------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/packages/thegraph-data-access/src/queries.ts b/packages/thegraph-data-access/src/queries.ts index 81472ea9b4..3270fc8d8e 100644 --- a/packages/thegraph-data-access/src/queries.ts +++ b/packages/thegraph-data-access/src/queries.ts @@ -74,19 +74,19 @@ export const GetTransactionsByHashQuery = gql` } `; -export const GetChannelsByTopicsQuery = gql` - ${TransactionsBodyFragment} - query GetChannelsByTopics($topics: [String!]!) { - ${metaQueryBody} - transactions( - where: { topics_contains: $topics } - orderBy: blockTimestamp - orderDirection: asc - ) { - channelId +export const GetTransactionsByTopics = gql` +${TransactionsBodyFragment} + +query GetTransactionsByTopics($topics: [String!]!){ + ${metaQueryBody} + channels( + where: { topics_contains: $topics } + ){ + transactions { + ...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), From 90ef499d9efff7f8e1917e25a30bf22b8be5eba7 Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:03:47 +0100 Subject: [PATCH 2/2] order transactions --- packages/thegraph-data-access/src/queries.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/thegraph-data-access/src/queries.ts b/packages/thegraph-data-access/src/queries.ts index 3270fc8d8e..42c811902a 100644 --- a/packages/thegraph-data-access/src/queries.ts +++ b/packages/thegraph-data-access/src/queries.ts @@ -82,7 +82,10 @@ query GetTransactionsByTopics($topics: [String!]!){ channels( where: { topics_contains: $topics } ){ - transactions { + transactions( + orderBy: blockTimestamp, + orderDirection: asc + ) { ...TransactionsBody } }