@subql/node-ethereum 1.10.1-1 → 1.10.1-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,20 @@
1
1
  import { RegisteredTypes } from '@polkadot/types/types';
2
2
  import { ReaderOptions, RunnerSpecs } from '@subql/common';
3
- import { EthereumProjectNetworkConfig, SubqlEthereumDataSource, FileType } from '@subql/common-ethereum';
3
+ import { EthereumProjectNetworkConfig, SubqlEthereumDataSource, EthereumBlockFilter, FileType } from '@subql/common-ethereum';
4
+ import Cron from 'cron-converter';
4
5
  import { GraphQLSchema } from 'graphql';
6
+ import { EthereumApi } from '../ethereum/api.ethereum';
5
7
  export declare type SubqlProjectDs = SubqlEthereumDataSource & {
6
8
  mapping: SubqlEthereumDataSource['mapping'] & {
7
9
  entryScript: string;
8
10
  };
9
11
  };
12
+ export declare type SubqlProjectBlockFilter = EthereumBlockFilter & {
13
+ cronSchedule?: {
14
+ schedule: Cron.Seeker;
15
+ next: number;
16
+ };
17
+ };
10
18
  export declare type SubqlProjectDsTemplate = Omit<SubqlProjectDs, 'startBlock'> & {
11
19
  name: string;
12
20
  };
@@ -27,3 +35,4 @@ export interface SubqueryProjectNetwork {
27
35
  dictionary?: string;
28
36
  chaintypes?: FileType;
29
37
  }
38
+ export declare function generateTimestampReferenceForBlockFilters(dataSources: SubqlProjectDs[], api: EthereumApi): Promise<SubqlProjectDs[]>;
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  // Copyright 2020-2022 OnFinality Limited authors & contributors
3
3
  // SPDX-License-Identifier: Apache-2.0
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
4
7
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.SubqueryProject = void 0;
8
+ exports.generateTimestampReferenceForBlockFilters = exports.SubqueryProject = void 0;
6
9
  const common_1 = require("@subql/common");
7
10
  const common_ethereum_1 = require("@subql/common-ethereum");
8
11
  const utils_1 = require("@subql/utils");
12
+ const cron_converter_1 = __importDefault(require("cron-converter"));
9
13
  const project_1 = require("../utils/project");
10
14
  const NOT_SUPPORT = (name) => {
11
15
  throw new Error(`Manifest specVersion ${name}() is not supported`);
@@ -57,6 +61,7 @@ async function loadProjectFromManifestBase(projectManifest, reader, path, networ
57
61
  ? await (0, project_1.getChainTypes)(reader, root, projectManifest.network.chaintypes.file)
58
62
  : undefined;
59
63
  const dataSources = await (0, project_1.updateDataSourcesV0_2_0)(projectManifest.dataSources, reader, root);
64
+ const templates = await loadProjectTemplates(projectManifest, root, reader);
60
65
  return {
61
66
  id: reader.root ? reader.root : path,
62
67
  root,
@@ -64,7 +69,7 @@ async function loadProjectFromManifestBase(projectManifest, reader, path, networ
64
69
  dataSources,
65
70
  schema,
66
71
  chainTypes,
67
- templates: [],
72
+ templates,
68
73
  };
69
74
  }
70
75
  const { name: packageName, version: packageVersion, } = require('../../package.json');
@@ -79,4 +84,50 @@ async function loadProjectFromManifest1_0_0(projectManifest, reader, path, netwo
79
84
  }
80
85
  return project;
81
86
  }
87
+ async function loadProjectTemplates(projectManifest, root, reader) {
88
+ if (projectManifest.templates && projectManifest.templates.length !== 0) {
89
+ const dsTemplates = await (0, project_1.updateDataSourcesV0_2_0)(projectManifest.templates, reader, root);
90
+ return dsTemplates.map((ds, index) => (Object.assign(Object.assign({}, ds), { name: projectManifest.templates[index].name })));
91
+ }
92
+ }
93
+ // eslint-disable-next-line @typescript-eslint/require-await
94
+ async function generateTimestampReferenceForBlockFilters(dataSources, api) {
95
+ const cron = new cron_converter_1.default();
96
+ dataSources = await Promise.all(dataSources.map(async (ds) => {
97
+ var _a;
98
+ if ((0, common_ethereum_1.isRuntimeDs)(ds)) {
99
+ const startBlock = (_a = ds.startBlock) !== null && _a !== void 0 ? _a : 1;
100
+ let block;
101
+ let timestampReference;
102
+ ds.mapping.handlers = await Promise.all(ds.mapping.handlers.map(async (handler) => {
103
+ var _a;
104
+ if (handler.kind === common_ethereum_1.EthereumHandlerKind.Block) {
105
+ if ((_a = handler.filter) === null || _a === void 0 ? void 0 : _a.timestamp) {
106
+ if (!block) {
107
+ block = await api.getBlockByHeight(startBlock);
108
+ timestampReference = new Date(block.timestamp * 1000); // Add millis
109
+ }
110
+ try {
111
+ cron.fromString(handler.filter.timestamp);
112
+ }
113
+ catch (e) {
114
+ throw new Error(`Invalid Cron string: ${handler.filter.timestamp}`);
115
+ }
116
+ const schedule = cron.schedule(timestampReference);
117
+ handler.filter.cronSchedule = {
118
+ schedule: schedule,
119
+ get next() {
120
+ return Date.parse(this.schedule.next().format());
121
+ },
122
+ };
123
+ }
124
+ }
125
+ return handler;
126
+ }));
127
+ }
128
+ return ds;
129
+ }));
130
+ return dataSources;
131
+ }
132
+ exports.generateTimestampReferenceForBlockFilters = generateTimestampReferenceForBlockFilters;
82
133
  //# sourceMappingURL=SubqueryProject.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SubqueryProject.js","sourceRoot":"","sources":["../../src/configure/SubqueryProject.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;AAGtC,0CAMuB;AACvB,4DAMgC;AAChC,wCAAqD;AAErD,8CAI0B;AAU1B,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,EAAE;IACnC,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,qBAAqB,CAAC,CAAC;AACrE,CAAC,CAAC;AAEF,MAAa,eAAe;IAU1B,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,IAAY,EACZ,gBAAwD,EACxD,aAA6B;QAE7B,kEAAkE;QAClE,qEAAqE;QACrE,MAAM,MAAM,GAAG,MAAM,sBAAa,CAAC,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;QAC/D,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,gBAAgB,EAAE,CAAC;QACtD,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,kC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AC,EAAE;QAChE,MAAM,IAAI,KAAK,CACb,+BAA+B,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,kBAAkB,cAAc,EAAE,CAC7F,CAAC;KACH;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { RegisteredTypes } from '@polkadot/types/types';\nimport {\n ReaderFactory,\n ReaderOptions,\n Reader,\n RunnerSpecs,\n validateSemver,\n} from '@subql/common';\nimport {\n EthereumProjectNetworkConfig,\n parseEthereumProjectManifest,\n SubqlEthereumDataSource,\n FileType,\n ProjectManifestV1_0_0Impl,\n} from '@subql/common-ethereum';\nimport { buildSchemaFromString } from '@subql/utils';\nimport { GraphQLSchema } from 'graphql';\nimport {\n getChainTypes,\n getProjectRoot,\n updateDataSourcesV0_2_0,\n} from '../utils/project';\n\nexport type SubqlProjectDs = SubqlEthereumDataSource & {\n mapping: SubqlEthereumDataSource['mapping'] & { entryScript: string };\n};\n\nexport type SubqlProjectDsTemplate = Omit<SubqlProjectDs, 'startBlock'> & {\n name: string;\n};\n\nconst NOT_SUPPORT = (name: string) => {\n throw new Error(`Manifest specVersion ${name}() is not supported`);\n};\n\nexport class SubqueryProject {\n id: string;\n root: string;\n network: Partial<EthereumProjectNetworkConfig>;\n dataSources: SubqlProjectDs[];\n schema: GraphQLSchema;\n templates: SubqlProjectDsTemplate[];\n chainTypes?: RegisteredTypes;\n runner?: RunnerSpecs;\n\n static async create(\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n readerOptions?: ReaderOptions,\n ): Promise<SubqueryProject> {\n // We have to use reader here, because path can be remote or local\n // and the `loadProjectManifest(projectPath)` only support local mode\n const reader = await ReaderFactory.create(path, readerOptions);\n const projectSchema = await reader.getProjectSchema();\n if (projectSchema === undefined) {\n throw new Error(`Get manifest from project path ${path} failed`);\n }\n const manifest = parseEthereumProjectManifest(projectSchema);\n\n if (manifest.isV1_0_0) {\n return loadProjectFromManifest1_0_0(\n manifest.asV1_0_0,\n reader,\n path,\n networkOverrides,\n );\n } else {\n NOT_SUPPORT(manifest.specVersion);\n }\n }\n}\n\nexport interface SubqueryProjectNetwork {\n chainId: string;\n endpoint?: string;\n dictionary?: string;\n chaintypes?: FileType;\n}\n\nfunction processChainId(network: any): SubqueryProjectNetwork {\n if (network.chainId && network.genesisHash) {\n throw new Error('Please only provide one of chainId and genesisHash');\n } else if (network.genesisHash && !network.chainId) {\n network.chainId = network.genesisHash;\n }\n delete network.genesisHash;\n return network;\n}\n\ntype SUPPORT_MANIFEST = ProjectManifestV1_0_0Impl;\n\nasync function loadProjectFromManifestBase(\n projectManifest: SUPPORT_MANIFEST,\n reader: Reader,\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n): Promise<SubqueryProject> {\n const root = await getProjectRoot(reader);\n\n const network = processChainId({\n ...projectManifest.network,\n ...networkOverrides,\n });\n\n if (!network.endpoint) {\n throw new Error(\n `Network endpoint must be provided for network. chainId=\"${network.chainId}\"`,\n );\n }\n\n let schemaString: string;\n try {\n schemaString = await reader.getFile(projectManifest.schema.file);\n } catch (e) {\n throw new Error(\n `unable to fetch the schema from ${projectManifest.schema.file}`,\n );\n }\n const schema = buildSchemaFromString(schemaString);\n\n const chainTypes = projectManifest.network.chaintypes\n ? await getChainTypes(reader, root, projectManifest.network.chaintypes.file)\n : undefined;\n\n const dataSources = await updateDataSourcesV0_2_0(\n projectManifest.dataSources,\n reader,\n root,\n );\n return {\n id: reader.root ? reader.root : path, //TODO, need to method to get project_id\n root,\n network,\n dataSources,\n schema,\n chainTypes,\n templates: [],\n };\n}\n\nconst {\n name: packageName,\n version: packageVersion,\n} = require('../../package.json');\n\nasync function loadProjectFromManifest1_0_0(\n projectManifest: ProjectManifestV1_0_0Impl,\n reader: Reader,\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n): Promise<SubqueryProject> {\n const project = await loadProjectFromManifestBase(\n projectManifest,\n reader,\n path,\n networkOverrides,\n );\n project.runner = projectManifest.runner;\n if (packageName !== project.runner.node.name) {\n throw new Error(\n `Runner requires ${project.runner.node.name}, current node ${packageName}`,\n );\n }\n if (!validateSemver(packageVersion, project.runner.node.version)) {\n throw new Error(\n `Runner require node version ${project.runner.node.version}, current node ${packageVersion}`,\n );\n }\n return project;\n}\n"]}
1
+ {"version":3,"file":"SubqueryProject.js","sourceRoot":"","sources":["../../src/configure/SubqueryProject.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;AAItC,0CAMuB;AACvB,4DASgC;AAChC,wCAAqD;AACrD,oEAAkC;AAGlC,8CAI0B;AAkB1B,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,EAAE;IACnC,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,qBAAqB,CAAC,CAAC;AACrE,CAAC,CAAC;AAEF,MAAa,eAAe;IAU1B,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,IAAY,EACZ,gBAAwD,EACxD,aAA6B;QAE7B,kEAAkE;QAClE,qEAAqE;QACrE,MAAM,MAAM,GAAG,MAAM,sBAAa,CAAC,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;QAC/D,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,gBAAgB,EAAE,CAAC;QACtD,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,kCAAkC,IAAI,SAAS,CAAC,CAAC;SAClE;QACD,MAAM,QAAQ,GAAG,IAAA,8CAA4B,EAAC,aAAa,CAAC,CAAC;QAE7D,IAAI,QAAQ,CAAC,QAAQ,EAAE;YACrB,OAAO,4BAA4B,CACjC,QAAQ,CAAC,QAAQ,EACjB,MAAM,EACN,IAAI,EACJ,gBAAgB,CACjB,CAAC;SACH;aAAM;YACL,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;SACnC;IACH,CAAC;CACF;AAnCD,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BACD,IAAI;4BACF,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;yBAC3C;wBAAC,OAAO,CAAC,EAAE;4BACV,MAAM,IAAI,KAAK,CACb,wBAAwB,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,CACnD,CAAC;yBACH;wBAED,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC;wBAClD,OAAO,CAAC,MAAkC,CAAC,YAAY,GAAG;4BACzD,QAAQ,EAAE,QAAQ;4BAClB,IAAI,IAAI;gCACN,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC;4BACnD,CAAC;yBACF,CAAC;qBACH;iBACF;gBACD,OAAO,OAAO,CAAC;YACjB,CAAC,CAAC,CACH,CAAC;SACH;QACD,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CACH,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC;AA/CD,8FA+CC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Block } from '@ethersproject/abstract-provider';\nimport { RegisteredTypes } from '@polkadot/types/types';\nimport {\n ReaderFactory,\n ReaderOptions,\n Reader,\n RunnerSpecs,\n validateSemver,\n} from '@subql/common';\nimport {\n EthereumProjectNetworkConfig,\n parseEthereumProjectManifest,\n SubqlEthereumDataSource,\n EthereumBlockFilter,\n FileType,\n ProjectManifestV1_0_0Impl,\n isRuntimeDs,\n EthereumHandlerKind,\n} from '@subql/common-ethereum';\nimport { buildSchemaFromString } from '@subql/utils';\nimport Cron from 'cron-converter';\nimport { GraphQLSchema } from 'graphql';\nimport { EthereumApi } from '../ethereum/api.ethereum';\nimport {\n getChainTypes,\n getProjectRoot,\n updateDataSourcesV0_2_0,\n} from '../utils/project';\n// import { getBlockByHeight, getTimestamp } from '../utils/substrate';\n\nexport type SubqlProjectDs = SubqlEthereumDataSource & {\n mapping: SubqlEthereumDataSource['mapping'] & { entryScript: string };\n};\n\nexport type SubqlProjectBlockFilter = EthereumBlockFilter & {\n cronSchedule?: {\n schedule: Cron.Seeker;\n next: number;\n };\n};\n\nexport type SubqlProjectDsTemplate = Omit<SubqlProjectDs, 'startBlock'> & {\n name: string;\n};\n\nconst NOT_SUPPORT = (name: string) => {\n throw new Error(`Manifest specVersion ${name}() is not supported`);\n};\n\nexport class SubqueryProject {\n id: string;\n root: string;\n network: Partial<EthereumProjectNetworkConfig>;\n dataSources: SubqlProjectDs[];\n schema: GraphQLSchema;\n templates: SubqlProjectDsTemplate[];\n chainTypes?: RegisteredTypes;\n runner?: RunnerSpecs;\n\n static async create(\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n readerOptions?: ReaderOptions,\n ): Promise<SubqueryProject> {\n // We have to use reader here, because path can be remote or local\n // and the `loadProjectManifest(projectPath)` only support local mode\n const reader = await ReaderFactory.create(path, readerOptions);\n const projectSchema = await reader.getProjectSchema();\n if (projectSchema === undefined) {\n throw new Error(`Get manifest from project path ${path} failed`);\n }\n const manifest = parseEthereumProjectManifest(projectSchema);\n\n if (manifest.isV1_0_0) {\n return loadProjectFromManifest1_0_0(\n manifest.asV1_0_0,\n reader,\n path,\n networkOverrides,\n );\n } else {\n NOT_SUPPORT(manifest.specVersion);\n }\n }\n}\n\nexport interface SubqueryProjectNetwork {\n chainId: string;\n endpoint?: string;\n dictionary?: string;\n chaintypes?: FileType;\n}\n\nfunction processChainId(network: any): SubqueryProjectNetwork {\n if (network.chainId && network.genesisHash) {\n throw new Error('Please only provide one of chainId and genesisHash');\n } else if (network.genesisHash && !network.chainId) {\n network.chainId = network.genesisHash;\n }\n delete network.genesisHash;\n return network;\n}\n\ntype SUPPORT_MANIFEST = ProjectManifestV1_0_0Impl;\n\nasync function loadProjectFromManifestBase(\n projectManifest: SUPPORT_MANIFEST,\n reader: Reader,\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n): Promise<SubqueryProject> {\n const root = await getProjectRoot(reader);\n\n const network = processChainId({\n ...projectManifest.network,\n ...networkOverrides,\n });\n\n if (!network.endpoint) {\n throw new Error(\n `Network endpoint must be provided for network. chainId=\"${network.chainId}\"`,\n );\n }\n\n let schemaString: string;\n try {\n schemaString = await reader.getFile(projectManifest.schema.file);\n } catch (e) {\n throw new Error(\n `unable to fetch the schema from ${projectManifest.schema.file}`,\n );\n }\n const schema = buildSchemaFromString(schemaString);\n\n const chainTypes = projectManifest.network.chaintypes\n ? await getChainTypes(reader, root, projectManifest.network.chaintypes.file)\n : undefined;\n\n const dataSources = await updateDataSourcesV0_2_0(\n projectManifest.dataSources,\n reader,\n root,\n );\n\n const templates = await loadProjectTemplates(projectManifest, root, reader);\n\n return {\n id: reader.root ? reader.root : path, //TODO, need to method to get project_id\n root,\n network,\n dataSources,\n schema,\n chainTypes,\n templates,\n };\n}\n\nconst {\n name: packageName,\n version: packageVersion,\n} = require('../../package.json');\n\nasync function loadProjectFromManifest1_0_0(\n projectManifest: ProjectManifestV1_0_0Impl,\n reader: Reader,\n path: string,\n networkOverrides?: Partial<EthereumProjectNetworkConfig>,\n): Promise<SubqueryProject> {\n const project = await loadProjectFromManifestBase(\n projectManifest,\n reader,\n path,\n networkOverrides,\n );\n project.runner = projectManifest.runner;\n if (packageName !== project.runner.node.name) {\n throw new Error(\n `Runner requires ${project.runner.node.name}, current node ${packageName}`,\n );\n }\n if (!validateSemver(packageVersion, project.runner.node.version)) {\n throw new Error(\n `Runner require node version ${project.runner.node.version}, current node ${packageVersion}`,\n );\n }\n return project;\n}\n\nasync function loadProjectTemplates(\n projectManifest: ProjectManifestV1_0_0Impl,\n root: string,\n reader: Reader,\n): Promise<SubqlProjectDsTemplate[]> {\n if (projectManifest.templates && projectManifest.templates.length !== 0) {\n const dsTemplates = await updateDataSourcesV0_2_0(\n projectManifest.templates,\n reader,\n root,\n );\n return dsTemplates.map((ds, index) => ({\n ...ds,\n name: projectManifest.templates[index].name,\n }));\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/require-await\nexport async function generateTimestampReferenceForBlockFilters(\n dataSources: SubqlProjectDs[],\n api: EthereumApi,\n): Promise<SubqlProjectDs[]> {\n const cron = new Cron();\n\n dataSources = await Promise.all(\n dataSources.map(async (ds) => {\n if (isRuntimeDs(ds)) {\n const startBlock = ds.startBlock ?? 1;\n let block: Block;\n let timestampReference: Date;\n\n ds.mapping.handlers = await Promise.all(\n ds.mapping.handlers.map(async (handler) => {\n if (handler.kind === EthereumHandlerKind.Block) {\n if (handler.filter?.timestamp) {\n if (!block) {\n block = await api.getBlockByHeight(startBlock);\n timestampReference = new Date(block.timestamp * 1000); // Add millis\n }\n try {\n cron.fromString(handler.filter.timestamp);\n } catch (e) {\n throw new Error(\n `Invalid Cron string: ${handler.filter.timestamp}`,\n );\n }\n\n const schedule = cron.schedule(timestampReference);\n (handler.filter as SubqlProjectBlockFilter).cronSchedule = {\n schedule: schedule,\n get next() {\n return Date.parse(this.schedule.next().format());\n },\n };\n }\n }\n return handler;\n }),\n );\n }\n return ds;\n }),\n );\n\n return dataSources;\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import { Block } from '@ethersproject/abstract-provider';
1
2
  import { RuntimeDataSourceV0_2_0 } from '@subql/common-ethereum';
2
3
  import { ApiWrapper, BlockWrapper, EthereumBlockWrapper, EthereumTransaction, EthereumResult, EthereumLog } from '@subql/types-ethereum';
3
4
  export declare class EthereumApi implements ApiWrapper<EthereumBlockWrapper> {
@@ -14,6 +15,7 @@ export declare class EthereumApi implements ApiWrapper<EthereumBlockWrapper> {
14
15
  getGenesisHash(): string;
15
16
  getSpecName(): string;
16
17
  getFinalizedBlockHeight(): Promise<number>;
18
+ getBlockByHeight(height: number): Promise<Block>;
17
19
  fetchBlocks(bufferBlocks: number[]): Promise<EthereumBlockWrapper[]>;
18
20
  freezeApi(processor: any, blockContent: BlockWrapper): void;
19
21
  private buildInterface;
@@ -7,16 +7,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.EthereumApi = void 0;
9
9
  const fs_1 = __importDefault(require("fs"));
10
- const http_1 = __importDefault(require("http"));
11
- const https_1 = __importDefault(require("https"));
12
10
  const abi_1 = require("@ethersproject/abi");
13
11
  const providers_1 = require("@ethersproject/providers");
14
12
  const node_core_1 = require("@subql/node-core");
15
13
  const utils_1 = require("ethers/lib/utils");
16
14
  const block_ethereum_1 = require("./block.ethereum");
17
15
  const utils_ethereum_1 = require("./utils.ethereum");
18
- const Web3HttpProvider = require('web3-providers-http');
19
- const Web3WsProvider = require('web3-providers-ws');
20
16
  // eslint-disable-next-line @typescript-eslint/no-var-requires
21
17
  const { version: packageVersion } = require('../../package.json');
22
18
  const logger = (0, node_core_1.getLogger)('api.ethereum');
@@ -40,60 +36,29 @@ class EthereumApi {
40
36
  this.endpoint = endpoint;
41
37
  this.contractInterfaces = {};
42
38
  const { hostname, pathname, port, protocol, searchParams } = new URL(endpoint);
43
- const httpAgent = new http_1.default.Agent({ keepAlive: true, maxSockets: 10 });
44
- const httpsAgent = new https_1.default.Agent({ keepAlive: true, maxSockets: 10 });
45
39
  const protocolStr = protocol.replace(':', '');
46
- const portNum = port
47
- ? parseInt(port, 10)
48
- : protocolStr === 'https'
49
- ? undefined
50
- : 80;
51
- let provider;
52
40
  if (protocolStr === 'https' || protocolStr === 'http') {
53
- const options = {
54
- keepAlive: true,
55
- headers: [
56
- {
57
- name: 'User-Agent',
58
- value: `Subquery-Node ${packageVersion}`,
59
- },
60
- ],
61
- agent: {
62
- http: httpAgent,
63
- https: httpsAgent,
64
- },
65
- };
66
- if (searchParams.apiKey) {
67
- options.headers.apiKey = searchParams.get('apiKey');
68
- }
69
- provider = new Web3HttpProvider(this.endpoint, options);
70
- }
71
- else if (protocolStr === 'ws' || protocolStr === 'wss') {
72
- const options = {
41
+ const connection = {
42
+ url: this.endpoint,
73
43
  headers: {
74
44
  'User-Agent': `Subquery-Node ${packageVersion}`,
75
45
  },
76
- clientConfig: {
77
- keepAlive: true,
78
- },
79
46
  };
80
47
  if (searchParams.apiKey) {
81
- options.headers.apiKey = searchParams.get('apiKey');
48
+ connection.headers.apiKey = searchParams.get('apiKey');
82
49
  }
83
- provider = new Web3WsProvider(endpoint, options);
50
+ this.client = new providers_1.JsonRpcProvider(connection);
51
+ }
52
+ else if (protocolStr === 'ws' || protocolStr === 'wss') {
53
+ this.client = new providers_1.WebSocketProvider(this.endpoint);
84
54
  }
85
55
  else {
86
56
  throw new Error(`Unsupported protocol: ${protocol}`);
87
57
  }
88
- this.client = new providers_1.Web3Provider(provider);
89
58
  }
90
59
  async init() {
91
- this.genesisBlock = await this.client.send('eth_getBlockByNumber', [
92
- (0, utils_1.hexValue)(0),
93
- true,
94
- ]);
95
- logger.info(this.endpoint);
96
- this.chainId = await this.client.send('net_version', []);
60
+ this.genesisBlock = await this.client.getBlock(0);
61
+ this.chainId = (await this.client.getNetwork()).chainId;
97
62
  }
98
63
  async getLastHeight() {
99
64
  return this.client.getBlockNumber();
@@ -114,6 +79,9 @@ class EthereumApi {
114
79
  // Doesn't seem to be a difference between finalized and latest
115
80
  return this.client.getBlockNumber();
116
81
  }
82
+ async getBlockByHeight(height) {
83
+ return this.client.getBlock(height);
84
+ }
117
85
  async fetchBlocks(bufferBlocks) {
118
86
  return Promise.all(bufferBlocks.map(async (num) => {
119
87
  try {
@@ -129,7 +97,7 @@ class EthereumApi {
129
97
  const transactions = await Promise.all(block.transactions.map(async (tx) => {
130
98
  //logger.info(JSON.stringify(tx))
131
99
  const transaction = (0, utils_ethereum_1.formatTransaction)(tx);
132
- const receipt = await this.client.send('eth_getTransactionReceipt', [tx.hash]);
100
+ const receipt = await this.client.getTransactionReceipt(tx.hash);
133
101
  transaction.receipt = (0, utils_ethereum_1.formatReceipt)(receipt, block);
134
102
  return transaction;
135
103
  }));
@@ -1 +1 @@
1
- {"version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ality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport fs from 'fs';\nimport http from 'http';\nimport https from 'https';\nimport { Interface } from '@ethersproject/abi';\nimport { Web3Provider } from '@ethersproject/providers';\nimport { RuntimeDataSourceV0_2_0 } from '@subql/common-ethereum';\nimport { getLogger } from '@subql/node-core';\nimport {\n ApiWrapper,\n BlockWrapper,\n EthereumBlockWrapper,\n EthereumTransaction,\n EthereumResult,\n EthereumLog,\n} from '@subql/types-ethereum';\nimport { hexDataSlice, hexValue } from 'ethers/lib/utils';\nimport { EthereumBlockWrapped } from './block.ethereum';\nimport {\n formatBlock,\n formatReceipt,\n formatTransaction,\n} from './utils.ethereum';\nconst Web3HttpProvider = require('web3-providers-http');\nconst Web3WsProvider = require('web3-providers-ws');\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst { version: packageVersion } = require('../../package.json');\n\nconst logger = getLogger('api.ethereum');\n\nasync function loadAssets(\n ds: RuntimeDataSourceV0_2_0,\n): Promise<Record<string, string>> {\n if (!ds.assets) {\n return {};\n }\n const res: Record<string, string> = {};\n\n for (const [name, { file }] of Object.entries(ds.assets)) {\n try {\n res[name] = await fs.promises.readFile(file, { encoding: 'utf8' });\n } catch (e) {\n throw new Error(`Failed to load datasource asset ${file}`);\n }\n }\n\n return res;\n}\n\nexport class EthereumApi implements ApiWrapper<EthereumBlockWrapper> {\n private client: Web3Provider;\n private genesisBlock: Record<string, any>;\n private contractInterfaces: Record<string, Interface> = {};\n private chainId: number;\n\n constructor(private endpoint: string) {\n const { hostname, pathname, port, protocol, searchParams } = new URL(\n endpoint,\n );\n const httpAgent = new http.Agent({ keepAlive: true, maxSockets: 10 });\n const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: 10 });\n\n const protocolStr = protocol.replace(':', '');\n const portNum = port\n ? parseInt(port, 10)\n : protocolStr === 'https'\n ? undefined\n : 80;\n\n let provider;\n if (protocolStr === 'https' || protocolStr === 'http') {\n const options = {\n keepAlive: true,\n headers: [\n {\n name: 'User-Agent',\n value: `Subquery-Node ${packageVersion}`,\n },\n ],\n agent: {\n http: httpAgent,\n https: httpsAgent,\n },\n };\n if ((searchParams as any).apiKey) {\n (options.headers as any).apiKey = searchParams.get('apiKey');\n }\n provider = new Web3HttpProvider(this.endpoint, options);\n } else if (protocolStr === 'ws' || protocolStr === 'wss') {\n const options = {\n headers: {\n 'User-Agent': `Subquery-Node ${packageVersion}`,\n },\n clientConfig: {\n keepAlive: true,\n },\n };\n if ((searchParams as any).apiKey) {\n (options.headers as any).apiKey = searchParams.get('apiKey');\n }\n\n provider = new Web3WsProvider(endpoint, options);\n } else {\n throw new Error(`Unsupported protocol: ${protocol}`);\n }\n\n this.client = new Web3Provider(provider);\n }\n\n async init(): Promise<void> {\n this.genesisBlock = await this.client.send('eth_getBlockByNumber', [\n hexValue(0),\n true,\n ]);\n logger.info(this.endpoint);\n\n this.chainId = await this.client.send('net_version', []);\n }\n\n async getLastHeight(): Promise<number> {\n return this.client.getBlockNumber();\n }\n\n getRuntimeChain(): string {\n return 'ethereum';\n }\n\n getChainId(): number {\n return this.chainId;\n }\n\n getGenesisHash(): string {\n return this.genesisBlock.hash;\n }\n\n getSpecName(): string {\n return 'ethereum';\n }\n\n async getFinalizedBlockHeight(): Promise<number> {\n // Doesn't seem to be a difference between finalized and latest\n return this.client.getBlockNumber();\n }\n\n async fetchBlocks(bufferBlocks: number[]): Promise<EthereumBlockWrapper[]> {\n return Promise.all(\n bufferBlocks.map(async (num) => {\n try {\n // Fetch Block\n const block_promise = await this.client.send('eth_getBlockByNumber', [\n hexValue(num),\n true,\n ]);\n\n const block = formatBlock(block_promise);\n //const block = this.client.formatter.blockWithTransactions(rawBlock);\n block.stateRoot = this.client.formatter.hash(block.stateRoot);\n // Get transaction receipts\n const transactions = await Promise.all(\n block.transactions.map(async (tx) => {\n //logger.info(JSON.stringify(tx))\n const transaction = formatTransaction(tx);\n const receipt = await this.client.send(\n 'eth_getTransactionReceipt',\n [tx.hash],\n );\n\n transaction.receipt = formatReceipt(receipt, block);\n return transaction;\n }),\n );\n return new EthereumBlockWrapped(block, transactions);\n } catch (e) {\n // Wrap error from an axios error to fix issue with error being undefined\n const error = new Error(e.message);\n logger.error(error, `Failed to fetch block at height ${num}`);\n throw error;\n }\n }),\n );\n }\n\n freezeApi(processor: any, blockContent: BlockWrapper): void {\n processor.freeze(this.client, 'api');\n }\n\n private buildInterface(\n abiName: string,\n assets: Record<string, string>,\n ): Interface | undefined {\n if (!assets[abiName]) {\n throw new Error(`ABI named \"${abiName}\" not referenced in assets`);\n }\n\n // This assumes that all datasources have a different abi name or they are the same abi\n if (!this.contractInterfaces[abiName]) {\n // Constructing the interface validates the ABI\n try {\n let abiObj = JSON.parse(assets[abiName]);\n\n /*\n * Allows parsing JSON artifacts as well as ABIs\n * https://trufflesuite.github.io/artifact-updates/background.html#what-are-artifacts\n */\n if (!Array.isArray(abiObj) && abiObj.abi) {\n abiObj = abiObj.abi;\n }\n\n this.contractInterfaces[abiName] = new Interface(abiObj);\n } catch (e) {\n logger.error(`Unable to parse ABI: ${e.message}`);\n throw new Error('ABI is invalid');\n }\n }\n\n return this.contractInterfaces[abiName];\n }\n\n async parseLog<T extends EthereumResult = EthereumResult>(\n log: EthereumLog,\n ds: RuntimeDataSourceV0_2_0,\n ): Promise<EthereumLog<T> | EthereumLog> {\n try {\n if (!ds?.options?.abi) {\n logger.warn('No ABI provided for datasource');\n return log;\n }\n const iface = this.buildInterface(ds.options.abi, await loadAssets(ds));\n return {\n ...log,\n args: iface?.parseLog(log).args as T,\n };\n } catch (e) {\n logger.warn(`Failed to parse log data: ${e.message}`);\n return log;\n }\n }\n\n async parseTransaction<T extends EthereumResult = EthereumResult>(\n transaction: EthereumTransaction,\n ds: RuntimeDataSourceV0_2_0,\n ): Promise<EthereumTransaction<T> | EthereumTransaction> {\n try {\n if (!ds?.options?.abi) {\n logger.warn('No ABI provided for datasource');\n return transaction;\n }\n const assets = await loadAssets(ds);\n const iface = this.buildInterface(ds.options.abi, assets);\n const func = iface.getFunction(hexDataSlice(transaction.input, 0, 4));\n const args = iface.decodeFunctionData(func, transaction.input) as T;\n return {\n ...transaction,\n args,\n };\n } catch (e) {\n logger.warn(`Failed to parse transaction data: ${e.message}`);\n return transaction;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"api.ethereum.js","sourceRoot":"","sources":["../../src/ethereum/api.ethereum.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;AAEtC,4CAAoB;AAGpB,4CAA+C;AAE/C,wDAA8E;AAE9E,gDAA6C;AAS7C,4CAA0E;AAC1E,qDAAwD;AACxD,qDAI0B;AAE1B,8DAA8D;AAC9D,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAElE,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,cAAc,CAAC,CAAC;AAEzC,KAAK,UAAU,UAAU,CACvB,EAA2B;IAE3B,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE;QACd,OAAO,EAAE,CAAC;KACX;IACD,MAAM,GAAG,GAA2B,EAAE,CAAC;IAEvC,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE;QACxD,IAAI;YACF,GAAG,CAAC,IAAI,CAAC,GAAG,MAAM,YAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;SACpE;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,mCAAmC,IAAI,EAAE,CAAC,CAAC;SAC5D;KACF;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAa,WAAW;IAMtB,YAAoB,QAAgB;QAAhB,aAAQ,GAAR,QAAQ,CAAQ;QAH5B,uBAAkB,GAA8B,EAAE,CAAC;QAIzD,MA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,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACtE,MAAM,IAAI,GAAG,KAAK,CAAC,kBAAkB,CAAC,IAAI,EAAE,WAAW,CAAC,KAAK,CAAM,CAAC;YACpE,uCACK,WAAW,KACd,IAAI,IACJ;SACH;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9D,OAAO,WAAW,CAAC;SACpB;IACH,CAAC;CACF;AApLD,kCAoLC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport fs from 'fs';\nimport http from 'http';\nimport https from 'https';\nimport { Interface } from '@ethersproject/abi';\nimport { Block } from '@ethersproject/abstract-provider';\nimport { JsonRpcProvider, WebSocketProvider } from '@ethersproject/providers';\nimport { RuntimeDataSourceV0_2_0 } from '@subql/common-ethereum';\nimport { getLogger } from '@subql/node-core';\nimport {\n ApiWrapper,\n BlockWrapper,\n EthereumBlockWrapper,\n EthereumTransaction,\n EthereumResult,\n EthereumLog,\n} from '@subql/types-ethereum';\nimport { ConnectionInfo, hexDataSlice, hexValue } from 'ethers/lib/utils';\nimport { EthereumBlockWrapped } from './block.ethereum';\nimport {\n formatBlock,\n formatReceipt,\n formatTransaction,\n} from './utils.ethereum';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst { version: packageVersion } = require('../../package.json');\n\nconst logger = getLogger('api.ethereum');\n\nasync function loadAssets(\n ds: RuntimeDataSourceV0_2_0,\n): Promise<Record<string, string>> {\n if (!ds.assets) {\n return {};\n }\n const res: Record<string, string> = {};\n\n for (const [name, { file }] of Object.entries(ds.assets)) {\n try {\n res[name] = await fs.promises.readFile(file, { encoding: 'utf8' });\n } catch (e) {\n throw new Error(`Failed to load datasource asset ${file}`);\n }\n }\n\n return res;\n}\n\nexport class EthereumApi implements ApiWrapper<EthereumBlockWrapper> {\n private client: JsonRpcProvider;\n private genesisBlock: Record<string, any>;\n private contractInterfaces: Record<string, Interface> = {};\n private chainId: number;\n\n constructor(private endpoint: string) {\n const { hostname, pathname, port, protocol, searchParams } = new URL(\n endpoint,\n );\n\n const protocolStr = protocol.replace(':', '');\n\n if (protocolStr === 'https' || protocolStr === 'http') {\n const connection: ConnectionInfo = {\n url: this.endpoint,\n headers: {\n 'User-Agent': `Subquery-Node ${packageVersion}`,\n },\n };\n if ((searchParams as any).apiKey) {\n (connection.headers as any).apiKey = searchParams.get('apiKey');\n }\n this.client = new JsonRpcProvider(connection);\n } else if (protocolStr === 'ws' || protocolStr === 'wss') {\n this.client = new WebSocketProvider(this.endpoint);\n } else {\n throw new Error(`Unsupported protocol: ${protocol}`);\n }\n }\n\n async init(): Promise<void> {\n this.genesisBlock = await this.client.getBlock(0);\n\n this.chainId = (await this.client.getNetwork()).chainId;\n }\n\n async getLastHeight(): Promise<number> {\n return this.client.getBlockNumber();\n }\n\n getRuntimeChain(): string {\n return 'ethereum';\n }\n\n getChainId(): number {\n return this.chainId;\n }\n\n getGenesisHash(): string {\n return this.genesisBlock.hash;\n }\n\n getSpecName(): string {\n return 'ethereum';\n }\n\n async getFinalizedBlockHeight(): Promise<number> {\n // Doesn't seem to be a difference between finalized and latest\n return this.client.getBlockNumber();\n }\n\n async getBlockByHeight(height: number): Promise<Block> {\n return this.client.getBlock(height);\n }\n\n async fetchBlocks(bufferBlocks: number[]): Promise<EthereumBlockWrapper[]> {\n return Promise.all(\n bufferBlocks.map(async (num) => {\n try {\n // Fetch Block\n const block_promise = await this.client.send('eth_getBlockByNumber', [\n hexValue(num),\n true,\n ]);\n\n const block = formatBlock(block_promise);\n\n //const block = this.client.formatter.blockWithTransactions(rawBlock);\n block.stateRoot = this.client.formatter.hash(block.stateRoot);\n // Get transaction receipts\n const transactions = await Promise.all(\n block.transactions.map(async (tx) => {\n //logger.info(JSON.stringify(tx))\n const transaction = formatTransaction(tx);\n const receipt = await this.client.getTransactionReceipt(tx.hash);\n\n transaction.receipt = formatReceipt(receipt, block);\n return transaction;\n }),\n );\n return new EthereumBlockWrapped(block, transactions);\n } catch (e) {\n // Wrap error from an axios error to fix issue with error being undefined\n const error = new Error(e.message);\n logger.error(error, `Failed to fetch block at height ${num}`);\n throw error;\n }\n }),\n );\n }\n\n freezeApi(processor: any, blockContent: BlockWrapper): void {\n processor.freeze(this.client, 'api');\n }\n\n private buildInterface(\n abiName: string,\n assets: Record<string, string>,\n ): Interface | undefined {\n if (!assets[abiName]) {\n throw new Error(`ABI named \"${abiName}\" not referenced in assets`);\n }\n\n // This assumes that all datasources have a different abi name or they are the same abi\n if (!this.contractInterfaces[abiName]) {\n // Constructing the interface validates the ABI\n try {\n let abiObj = JSON.parse(assets[abiName]);\n\n /*\n * Allows parsing JSON artifacts as well as ABIs\n * https://trufflesuite.github.io/artifact-updates/background.html#what-are-artifacts\n */\n if (!Array.isArray(abiObj) && abiObj.abi) {\n abiObj = abiObj.abi;\n }\n\n this.contractInterfaces[abiName] = new Interface(abiObj);\n } catch (e) {\n logger.error(`Unable to parse ABI: ${e.message}`);\n throw new Error('ABI is invalid');\n }\n }\n\n return this.contractInterfaces[abiName];\n }\n\n async parseLog<T extends EthereumResult = EthereumResult>(\n log: EthereumLog,\n ds: RuntimeDataSourceV0_2_0,\n ): Promise<EthereumLog<T> | EthereumLog> {\n try {\n if (!ds?.options?.abi) {\n logger.warn('No ABI provided for datasource');\n return log;\n }\n const iface = this.buildInterface(ds.options.abi, await loadAssets(ds));\n return {\n ...log,\n args: iface?.parseLog(log).args as T,\n };\n } catch (e) {\n logger.warn(`Failed to parse log data: ${e.message}`);\n return log;\n }\n }\n\n async parseTransaction<T extends EthereumResult = EthereumResult>(\n transaction: EthereumTransaction,\n ds: RuntimeDataSourceV0_2_0,\n ): Promise<EthereumTransaction<T> | EthereumTransaction> {\n try {\n if (!ds?.options?.abi) {\n logger.warn('No ABI provided for datasource');\n return transaction;\n }\n const assets = await loadAssets(ds);\n const iface = this.buildInterface(ds.options.abi, assets);\n const func = iface.getFunction(hexDataSlice(transaction.input, 0, 4));\n const args = iface.decodeFunctionData(func, transaction.input) as T;\n return {\n ...transaction,\n args,\n };\n } catch (e) {\n logger.warn(`Failed to parse transaction data: ${e.message}`);\n return transaction;\n }\n }\n}\n"]}
@@ -11,11 +11,13 @@ export declare class DynamicDsService {
11
11
  private readonly dsProcessorService;
12
12
  private readonly project;
13
13
  private metaDataRepo;
14
+ private tempDsRecords;
14
15
  constructor(dsProcessorService: DsProcessorService, project: SubqueryProject);
15
16
  init(metaDataRepo: MetadataRepo): void;
16
17
  private _datasources;
17
18
  createDynamicDatasource(params: DatasourceParams, tx: Transaction): Promise<SubqlProjectDs>;
18
19
  getDynamicDatasources(): Promise<SubqlProjectDs[]>;
20
+ deleteTempDsRecords(blockHeight: number): void;
19
21
  private getDynamicDatasourceParams;
20
22
  private saveDynamicDatasourceParams;
21
23
  private getDatasource;
@@ -19,10 +19,12 @@ const assert_1 = __importDefault(require("assert"));
19
19
  const common_1 = require("@nestjs/common");
20
20
  const common_ethereum_1 = require("@subql/common-ethereum");
21
21
  const node_core_1 = require("@subql/node-core");
22
+ const lodash_1 = require("lodash");
22
23
  const SubqueryProject_1 = require("../configure/SubqueryProject");
23
24
  const ds_processor_service_1 = require("./ds-processor.service");
24
25
  const logger = (0, node_core_1.getLogger)('dynamic-ds');
25
26
  const METADATA_KEY = 'dynamicDatasources';
27
+ const TEMP_DS_PREFIX = 'ds_';
26
28
  let DynamicDsService = class DynamicDsService {
27
29
  constructor(dsProcessorService, project) {
28
30
  this.dsProcessorService = dsProcessorService;
@@ -59,22 +61,39 @@ let DynamicDsService = class DynamicDsService {
59
61
  }
60
62
  return this._datasources;
61
63
  }
62
- async getDynamicDatasourceParams() {
64
+ deleteTempDsRecords(blockHeight) {
65
+ delete this.tempDsRecords[TEMP_DS_PREFIX + blockHeight];
66
+ }
67
+ async getDynamicDatasourceParams(blockHeight) {
68
+ var _a;
63
69
  (0, assert_1.default)(this.metaDataRepo, `Model _metadata does not exist`);
64
70
  const record = await this.metaDataRepo.findByPk(METADATA_KEY);
65
- const results = record === null || record === void 0 ? void 0 : record.value;
71
+ let results = record === null || record === void 0 ? void 0 : record.value;
66
72
  if (!results || typeof results !== 'string') {
67
- return [];
73
+ if (blockHeight !== undefined) {
74
+ results = (_a = this.tempDsRecords) === null || _a === void 0 ? void 0 : _a[TEMP_DS_PREFIX + blockHeight];
75
+ if (!results || typeof results !== 'string') {
76
+ return [];
77
+ }
78
+ }
79
+ else {
80
+ return [];
81
+ }
68
82
  }
69
83
  return JSON.parse(results);
70
84
  }
71
85
  async saveDynamicDatasourceParams(dsParams, tx) {
72
- const existing = await this.getDynamicDatasourceParams();
86
+ const existing = await this.getDynamicDatasourceParams(dsParams.startBlock);
73
87
  (0, assert_1.default)(this.metaDataRepo, `Model _metadata does not exist`);
74
- await this.metaDataRepo.upsert({ key: METADATA_KEY, value: JSON.stringify([...existing, dsParams]) }, { transaction: tx });
88
+ const dsRecords = JSON.stringify([...existing, dsParams]);
89
+ await this.metaDataRepo
90
+ .upsert({ key: METADATA_KEY, value: dsRecords }, { transaction: tx })
91
+ .then(() => {
92
+ this.tempDsRecords = Object.assign(Object.assign({}, this.tempDsRecords), { [TEMP_DS_PREFIX + dsParams.startBlock]: dsRecords });
93
+ });
75
94
  }
76
95
  async getDatasource(params) {
77
- const template = this.project.templates.find((t) => t.name === params.templateName);
96
+ const template = (0, lodash_1.cloneDeep)(this.project.templates.find((t) => t.name === params.templateName));
78
97
  if (!template) {
79
98
  throw new Error(`Unable to find matching template in project for name: "${params.templateName}"`);
80
99
  }
@@ -1 +1 @@
1
- {"version":3,"file":"dynamic-ds.service.js","sourceRoot":"","sources":["../../src/indexer/dynamic-ds.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,oDAA4B;AAC5B,2CAA4C;AAC5C,4DAAiE;AACjE,gDAA2D;AAE3D,kEAA+E;AAC/E,iEAA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,MAAM,CAAC,YAAY,CACtC,CAAC;QAEF,IAAI,CAAC,QAAQ,EAAE;YACb,MAAM,IAAI,KAAK,CACb,0DAA0D,MAAM,CAAC,YAAY,GAAG,CACjF,CAAC;SACH;QAED,MAAM,CAAC,IAAI,CACT,kDAAkD,MAAM,CAAC,YAAY,GAAG,CACzE,CAAC;QAEF,MAAM,KAAK,GAAG,gCACT,QAAQ,KACX,UAAU,EAAE,MAAM,CAAC,UAAU,GACZ,CAAC;QACpB,OAAO,KAAK,CAAC,IAAI,CAAC;QAClB,IAAI;YACF,IAAI,IAAA,4BAAU,EAAC,KAAK,CAAC,EAAE;gBACrB,KAAK,CAAC,SAAS,CAAC,OAAO,mCAClB,KAAK,CAAC,SAAS,CAAC,OAAO,GACvB,MAAM,CAAC,IAAI,CACf,CAAC;gBACF,MAAM,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;aACzD;iBAAM,IAAI,IAAA,6BAAW,EAAC,KAAK,CAAC,EAAE;gBAC7B,2CAA2C;aAC5C;YAED,OAAO,KAAK,CAAC;SACd;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;SACxE;IACH,CAAC;CACF,CAAA;AArHY,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;qCAK4B,yCAAkB;QAC7B,iCAAe;GALhC,gBAAgB,CAqH5B;AArHY,4CAAgB","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport { Injectable } from '@nestjs/common';\nimport { isCustomDs, isRuntimeDs } from '@subql/common-ethereum';\nimport { getLogger, MetadataRepo } from '@subql/node-core';\nimport { Transaction } from 'sequelize/types';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { DsProcessorService } from './ds-processor.service';\n\nconst logger = getLogger('dynamic-ds');\n\nconst METADATA_KEY = 'dynamicDatasources';\n\ninterface DatasourceParams {\n templateName: string;\n args?: Record<string, unknown>;\n startBlock: number;\n}\n\n@Injectable()\nexport class DynamicDsService {\n private metaDataRepo: MetadataRepo;\n\n constructor(\n private readonly dsProcessorService: DsProcessorService,\n private readonly project: SubqueryProject,\n ) {}\n\n init(metaDataRepo: MetadataRepo): void {\n this.metaDataRepo = metaDataRepo;\n }\n\n private _datasources: SubqlProjectDs[];\n\n async createDynamicDatasource(\n params: DatasourceParams,\n tx: Transaction,\n ): Promise<SubqlProjectDs> {\n try {\n const ds = await this.getDatasource(params);\n\n await this.saveDynamicDatasourceParams(params, tx);\n\n logger.info(\n `Created new dynamic datasource from template: \"${params.templateName}\"`,\n );\n\n if (!this._datasources) this._datasources = [];\n this._datasources.push(ds);\n\n return ds;\n } catch (e) {\n logger.error(e.message);\n process.exit(1);\n }\n }\n\n async getDynamicDatasources(): Promise<SubqlProjectDs[]> {\n if (!this._datasources) {\n try {\n const params = await this.getDynamicDatasourceParams();\n\n this._datasources = await Promise.all(\n params.map((params) => this.getDatasource(params)),\n );\n } catch (e) {\n logger.error(`Unable to get dynamic datasources:\\n${e.message}`);\n process.exit(1);\n }\n }\n\n return this._datasources;\n }\n\n private async getDynamicDatasourceParams(): Promise<DatasourceParams[]> {\n assert(this.metaDataRepo, `Model _metadata does not exist`);\n const record = await this.metaDataRepo.findByPk(METADATA_KEY);\n const results = record?.value;\n\n if (!results || typeof results !== 'string') {\n return [];\n }\n\n return JSON.parse(results);\n }\n\n private async saveDynamicDatasourceParams(\n dsParams: DatasourceParams,\n tx: Transaction,\n ): Promise<void> {\n const existing = await this.getDynamicDatasourceParams();\n\n assert(this.metaDataRepo, `Model _metadata does not exist`);\n await this.metaDataRepo.upsert(\n { key: METADATA_KEY, value: JSON.stringify([...existing, dsParams]) },\n { transaction: tx },\n );\n }\n\n private async getDatasource(\n params: DatasourceParams,\n ): Promise<SubqlProjectDs> {\n const template = this.project.templates.find(\n (t) => t.name === params.templateName,\n );\n\n if (!template) {\n throw new Error(\n `Unable to find matching template in project for name: \"${params.templateName}\"`,\n );\n }\n\n logger.info(\n `Initialised dynamic datasource from template: \"${params.templateName}\"`,\n );\n\n const dsObj = {\n ...template,\n startBlock: params.startBlock,\n } as SubqlProjectDs;\n delete dsObj.name;\n try {\n if (isCustomDs(dsObj)) {\n dsObj.processor.options = {\n ...dsObj.processor.options,\n ...params.args,\n };\n await this.dsProcessorService.validateCustomDs([dsObj]);\n } else if (isRuntimeDs(dsObj)) {\n // XXX add any modifications to the ds here\n }\n\n return dsObj;\n } catch (e) {\n throw new Error(`Unable to create dynamic datasource.\\n ${e.message}`);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"dynamic-ds.service.js","sourceRoot":"","sources":["../../src/indexer/dynamic-ds.service.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAEtC,oDAA4B;AAC5B,2CAA4C;AAC5C,4DAAiE;AACjE,gDAA2D;AAC3D,mCAAmC;AAEnC,kEAA+E;AAC/E,iEAA4D;AAE5D,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC,YAAY,CAAC,CAAC;AAEvC,MAAM,YAAY,GAAG,oBAAoB,CAAC;AAC1C,MAAM,cAAc,GAAG,KAAK,CAAC;AAStB,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAI3B,YACmB,kBAAsC,EACtC,OAAwB;QADxB,uBAAkB,GAAlB,kBAAkB,CAAoB;QACtC,YAAO,GAAP,OAAO,CAAiB;IACxC,CAAC;IAEJ,IAAI,CAAC,YAA0B;QAC7B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAID,KAAK,CAAC,uBAAuB,CAC3B,MAAwB,EACxB,EAAe;QAEf,IAAI;YACF,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;YAE5C,MAAM,IAAI,CAAC,2BAA2B,CAAC,MAA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AAO,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,YAAY,CAAC,CACnE,CAAC;QAEF,IAAI,CAAC,QAAQ,EAAE;YACb,MAAM,IAAI,KAAK,CACb,0DAA0D,MAAM,CAAC,YAAY,GAAG,CACjF,CAAC;SACH;QAED,MAAM,CAAC,IAAI,CACT,kDAAkD,MAAM,CAAC,YAAY,GAAG,CACzE,CAAC;QAEF,MAAM,KAAK,GAAG,gCACT,QAAQ,KACX,UAAU,EAAE,MAAM,CAAC,UAAU,GACZ,CAAC;QACpB,OAAO,KAAK,CAAC,IAAI,CAAC;QAClB,IAAI;YACF,IAAI,IAAA,4BAAU,EAAC,KAAK,CAAC,EAAE;gBACrB,KAAK,CAAC,SAAS,CAAC,OAAO,mCAClB,KAAK,CAAC,SAAS,CAAC,OAAO,GACvB,MAAM,CAAC,IAAI,CACf,CAAC;gBACF,MAAM,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;aACzD;iBAAM,IAAI,IAAA,6BAAW,EAAC,KAAK,CAAC,EAAE;gBAC7B,2CAA2C;aAC5C;YAED,OAAO,KAAK,CAAC;SACd;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;SACxE;IACH,CAAC;CACF,CAAA;AAxIY,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;qCAM4B,yCAAkB;QAC7B,iCAAe;GANhC,gBAAgB,CAwI5B;AAxIY,4CAAgB","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport { Injectable } from '@nestjs/common';\nimport { isCustomDs, isRuntimeDs } from '@subql/common-ethereum';\nimport { getLogger, MetadataRepo } from '@subql/node-core';\nimport { cloneDeep } from 'lodash';\nimport { Transaction } from 'sequelize/types';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { DsProcessorService } from './ds-processor.service';\n\nconst logger = getLogger('dynamic-ds');\n\nconst METADATA_KEY = 'dynamicDatasources';\nconst TEMP_DS_PREFIX = 'ds_';\n\ninterface DatasourceParams {\n templateName: string;\n args?: Record<string, unknown>;\n startBlock: number;\n}\n\n@Injectable()\nexport class DynamicDsService {\n private metaDataRepo: MetadataRepo;\n private tempDsRecords: Record<string, string>;\n\n constructor(\n private readonly dsProcessorService: DsProcessorService,\n private readonly project: SubqueryProject,\n ) {}\n\n init(metaDataRepo: MetadataRepo): void {\n this.metaDataRepo = metaDataRepo;\n }\n\n private _datasources: SubqlProjectDs[];\n\n async createDynamicDatasource(\n params: DatasourceParams,\n tx: Transaction,\n ): Promise<SubqlProjectDs> {\n try {\n const ds = await this.getDatasource(params);\n\n await this.saveDynamicDatasourceParams(params, tx);\n\n logger.info(\n `Created new dynamic datasource from template: \"${params.templateName}\"`,\n );\n\n if (!this._datasources) this._datasources = [];\n this._datasources.push(ds);\n\n return ds;\n } catch (e) {\n logger.error(e.message);\n process.exit(1);\n }\n }\n\n async getDynamicDatasources(): Promise<SubqlProjectDs[]> {\n if (!this._datasources) {\n try {\n const params = await this.getDynamicDatasourceParams();\n\n this._datasources = await Promise.all(\n params.map((params) => this.getDatasource(params)),\n );\n } catch (e) {\n logger.error(`Unable to get dynamic datasources:\\n${e.message}`);\n process.exit(1);\n }\n }\n\n return this._datasources;\n }\n\n deleteTempDsRecords(blockHeight: number) {\n delete this.tempDsRecords[TEMP_DS_PREFIX + blockHeight];\n }\n\n private async getDynamicDatasourceParams(\n blockHeight?: number,\n ): Promise<DatasourceParams[]> {\n assert(this.metaDataRepo, `Model _metadata does not exist`);\n const record = await this.metaDataRepo.findByPk(METADATA_KEY);\n let results = record?.value;\n\n if (!results || typeof results !== 'string') {\n if (blockHeight !== undefined) {\n results = this.tempDsRecords?.[TEMP_DS_PREFIX + blockHeight];\n if (!results || typeof results !== 'string') {\n return [];\n }\n } else {\n return [];\n }\n }\n\n return JSON.parse(results);\n }\n\n private async saveDynamicDatasourceParams(\n dsParams: DatasourceParams,\n tx: Transaction,\n ): Promise<void> {\n const existing = await this.getDynamicDatasourceParams(dsParams.startBlock);\n\n assert(this.metaDataRepo, `Model _metadata does not exist`);\n const dsRecords = JSON.stringify([...existing, dsParams]);\n await this.metaDataRepo\n .upsert({ key: METADATA_KEY, value: dsRecords }, { transaction: tx })\n .then(() => {\n this.tempDsRecords = {\n ...this.tempDsRecords,\n ...{ [TEMP_DS_PREFIX + dsParams.startBlock]: dsRecords },\n };\n });\n }\n\n private async getDatasource(\n params: DatasourceParams,\n ): Promise<SubqlProjectDs> {\n const template = cloneDeep(\n this.project.templates.find((t) => t.name === params.templateName),\n );\n\n if (!template) {\n throw new Error(\n `Unable to find matching template in project for name: \"${params.templateName}\"`,\n );\n }\n\n logger.info(\n `Initialised dynamic datasource from template: \"${params.templateName}\"`,\n );\n\n const dsObj = {\n ...template,\n startBlock: params.startBlock,\n } as SubqlProjectDs;\n delete dsObj.name;\n try {\n if (isCustomDs(dsObj)) {\n dsObj.processor.options = {\n ...dsObj.processor.options,\n ...params.args,\n };\n await this.dsProcessorService.validateCustomDs([dsObj]);\n } else if (isRuntimeDs(dsObj)) {\n // XXX add any modifications to the ds here\n }\n\n return dsObj;\n } catch (e) {\n throw new Error(`Unable to create dynamic datasource.\\n ${e.message}`);\n }\n }\n}\n"]}
@@ -36,6 +36,7 @@ export declare class FetchService implements OnApplicationShutdown {
36
36
  startLoop(initBlockHeight: number): Promise<void>;
37
37
  getModulos(): number[];
38
38
  getModuloBlocks(startHeight: number, endHeight: number): number[];
39
+ getEnqueuedModuloBlocks(startBlockHeight: number): number[];
39
40
  fillNextBlockBuffer(initBlockHeight: number): Promise<void>;
40
41
  private nextEndBlockHeight;
41
42
  private dictionaryValidation;
@@ -243,9 +243,14 @@ let FetchService = class FetchService {
243
243
  }
244
244
  return moduloBlocks;
245
245
  }
246
+ getEnqueuedModuloBlocks(startBlockHeight) {
247
+ return this.getModuloBlocks(startBlockHeight, this.nodeConfig.batchSize * Math.max(...this.getModulos()) +
248
+ startBlockHeight).slice(0, this.nodeConfig.batchSize);
249
+ }
246
250
  async fillNextBlockBuffer(initBlockHeight) {
247
251
  let startBlockHeight;
248
252
  let scaledBatchSize;
253
+ const handlers = [].concat(...this.project.dataSources.map((ds) => ds.mapping.handlers));
249
254
  const getStartBlockHeight = () => {
250
255
  return this.blockDispatcher.latestBufferedHeight
251
256
  ? this.blockDispatcher.latestBufferedHeight + 1
@@ -292,9 +297,13 @@ let FetchService = class FetchService {
292
297
  this.eventEmitter.emit(node_core_1.IndexerEvent.SkipDictionary);
293
298
  }
294
299
  }
295
- // the original method: fill next batch size of blocks
296
300
  const endHeight = this.nextEndBlockHeight(startBlockHeight, scaledBatchSize);
297
- this.blockDispatcher.enqueueBlocks((0, lodash_1.range)(startBlockHeight, endHeight + 1));
301
+ if (this.getModulos().length === handlers.length) {
302
+ this.blockDispatcher.enqueueBlocks(this.getEnqueuedModuloBlocks(startBlockHeight));
303
+ }
304
+ else {
305
+ this.blockDispatcher.enqueueBlocks((0, lodash_1.range)(startBlockHeight, endHeight + 1));
306
+ }
298
307
  }
299
308
  }
300
309
  nextEndBlockHeight(startBlockHeight, scaledBatchSize) {
@@ -323,6 +332,7 @@ let FetchService = class FetchService {
323
332
  }
324
333
  async resetForNewDs(blockHeight) {
325
334
  await this.syncDynamicDatascourcesFromMeta();
335
+ this.dynamicDsService.deleteTempDsRecords(blockHeight);
326
336
  this.updateDictionary();
327
337
  this.blockDispatcher.flushQueue(blockHeight);
328
338
  }