forked from subquery/subql
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes and optimisations to dictionary queries (#344)
* Fixes and optimisations to dictionary queries * Fix build issue, tidy up * Update changelog
- Loading branch information
Showing
9 changed files
with
319 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import { SubqlDatasource } from '@subql/types-ethereum'; | ||
import { groupBy, partition } from 'lodash'; | ||
import { | ||
EthereumProjectDsTemplate, | ||
EthereumProjectDs, | ||
} from '../../configure/SubqueryProject'; | ||
import { SubqlRuntimeHandler } from '@subql/types-ethereum'; | ||
import { groupBy } from 'lodash'; | ||
import { EthereumProjectDs } from '../../configure/SubqueryProject'; | ||
|
||
function isTemplateDs( | ||
ds: EthereumProjectDs | EthereumProjectDsTemplate, | ||
): ds is EthereumProjectDsTemplate { | ||
return !!(ds as EthereumProjectDsTemplate)?.name; | ||
} | ||
|
||
export function ethFilterDs( | ||
dataSources: (EthereumProjectDs | EthereumProjectDsTemplate)[], | ||
): SubqlDatasource[] { | ||
const [normalDataSources, templateDataSources] = partition( | ||
dataSources, | ||
(ds) => !isTemplateDs(ds), | ||
); | ||
|
||
// Group templ | ||
const groupedDataSources = Object.values( | ||
groupBy( | ||
templateDataSources as EthereumProjectDsTemplate[], | ||
(ds) => ds.name, | ||
), | ||
).map((grouped) => { | ||
if (grouped.length === 1) { | ||
return grouped[0]; | ||
} | ||
// Gets all the unique handlers and the contract addresses that go with them | ||
export function groupedDataSources( | ||
dataSources: EthereumProjectDs[], | ||
): [SubqlRuntimeHandler, Array<string | undefined>][] { | ||
const addressHandlers: [string | undefined, SubqlRuntimeHandler][] = | ||
dataSources | ||
.map((ds) => | ||
ds.mapping.handlers.map( | ||
(h) => | ||
[ds.options?.address, h] as [ | ||
string | undefined, | ||
SubqlRuntimeHandler, | ||
], | ||
), | ||
) | ||
.flat(); | ||
|
||
const options = grouped.map((ds) => ds.options); | ||
const ref = grouped[0]; | ||
const grouped = groupBy(addressHandlers, ([, handler]) => { | ||
return `${handler.kind}:${JSON.stringify(handler.filter)}`; | ||
}); | ||
|
||
return { | ||
...ref, | ||
groupedOptions: options, | ||
}; | ||
const res = Object.values(grouped).map((grouped) => { | ||
const addresses = [...new Set(grouped.map(([address]) => address))]; // Make unique, could be duplicate handler in the same DS | ||
return [grouped[0][1], addresses] as [ | ||
SubqlRuntimeHandler, | ||
Array<string | undefined>, | ||
]; | ||
}); | ||
|
||
return [...normalDataSources, ...groupedDataSources]; | ||
return res; | ||
} | ||
|
||
export function validAddresses( | ||
addresses?: (string | undefined | null)[], | ||
): string[] { | ||
return (addresses ?? []).filter((a) => !!a) as string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.