@subql/node-ethereum 0.4.0 → 0.4.1-0

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.
@@ -93,6 +93,7 @@ class EthereumApi {
93
93
  return (0, project_1.retryOnFailEth)(this.client.getTransactionReceipt.bind(this.client, transactionHash));
94
94
  }
95
95
  async fetchBlock(num) {
96
+ var _a;
96
97
  const block_promise = await this.getBlockPromise(num);
97
98
  const block = (0, utils_ethereum_1.formatBlock)(block_promise);
98
99
  block.stateRoot = this.client.formatter.hash(block.stateRoot);
@@ -111,7 +112,7 @@ class EthereumApi {
111
112
  }
112
113
  catch (e) {
113
114
  // Method not avaialble https://eips.ethereum.org/EIPS/eip-1474
114
- if (e.error.code === -32601) {
115
+ if (((_a = e === null || e === void 0 ? void 0 : e.error) === null || _a === void 0 ? void 0 : _a.code) === -32601) {
115
116
  logger.warn(`The endpoint doesn't support 'eth_getBlockReceipts', individual receipts will be fetched instead, this will greatly impact performance.`);
116
117
  this.endpointSupportsGetBlockReceipts = false;
117
118
  // Should continue and use old method here
@@ -1 +1 @@
1
- {"version":3,"file":"api.ethereum.js","sourceRoot":"","sources":["../../src/ethereum/api.ethereum.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;AAEtC,oDAA4B;AAC5B,4CAAoB;AAGpB,4CAA+C;AAE/C,wDAAqD;AACrD,wDAA8E;AAE9E,gDAA6C;AAS7C,4CAA0E;AAC1E,8CAAkD;AAClD,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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AAM,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;AAxOD,kCAwOC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport fs from 'fs';\nimport http from 'http';\nimport https from 'https';\nimport { Interface } from '@ethersproject/abi';\nimport { Block, TransactionReceipt } from '@ethersproject/abstract-provider';\nimport { BigNumber } from '@ethersproject/bignumber';\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 { retryOnFailEth } from '../utils/project';\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 private endpointSupportsGetBlockReceipts = true;\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 logger.info(this.endpoint.split('?')[0]);\n if (protocolStr === 'https' || protocolStr === 'http') {\n const connection: ConnectionInfo = {\n url: this.endpoint.split('?')[0],\n headers: {\n 'User-Agent': `Subquery-Node ${packageVersion}`,\n },\n };\n searchParams.forEach((value, name, searchParams) => {\n (connection.headers as any)[name] = value;\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 getFinalizedBlockHeight(): Promise<number> {\n return (await this.client.getBlock('finalized')).number;\n }\n\n async getBestBlockHeight(): Promise<number> {\n return (await this.client.getBlock('safe')).number;\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 getBlockByHeightOrHash(heightOrHash: number | string): Promise<Block> {\n return this.client.getBlock(heightOrHash);\n }\n\n async getBlockPromise(num: number): Promise<any> {\n return retryOnFailEth(() =>\n this.client.send('eth_getBlockByNumber', [hexValue(num), true]),\n );\n }\n\n async getTransactionReceipt(\n transactionHash: string | Promise<string>,\n ): Promise<TransactionReceipt> {\n return retryOnFailEth<TransactionReceipt>(\n this.client.getTransactionReceipt.bind(this.client, transactionHash),\n );\n }\n async fetchBlock(num: number): Promise<EthereumBlockWrapper> {\n const block_promise = await this.getBlockPromise(num);\n\n const block = formatBlock(block_promise);\n block.stateRoot = this.client.formatter.hash(block.stateRoot);\n\n if (this.endpointSupportsGetBlockReceipts) {\n try {\n const rawReceipts: any[] = await this.client.send(\n 'eth_getBlockReceipts',\n [hexValue(block.number)],\n );\n\n const receipts = rawReceipts.map((receipt) =>\n formatReceipt(receipt, block),\n );\n\n const txs = block.transactions.map((tx) => {\n const transaction = formatTransaction(tx);\n transaction.receipt =\n receipts[BigNumber.from(transaction.transactionIndex).toNumber()];\n\n assert(\n transaction.hash === transaction.receipt.transactionHash,\n 'Failed to match receipt to transaction',\n );\n\n return transaction;\n });\n\n return new EthereumBlockWrapped(block, txs);\n } catch (e) {\n // Method not avaialble https://eips.ethereum.org/EIPS/eip-1474\n if (e.error.code === -32601) {\n logger.warn(\n `The endpoint doesn't support 'eth_getBlockReceipts', individual receipts will be fetched instead, this will greatly impact performance.`,\n );\n this.endpointSupportsGetBlockReceipts = false;\n\n // Should continue and use old method here\n } else {\n throw e;\n }\n }\n }\n\n const transactions = await Promise.all(\n block.transactions.map(async (tx) => {\n const transaction = formatTransaction(tx);\n const receipt = await this.getTransactionReceipt(tx.hash);\n transaction.receipt = formatReceipt(receipt, block);\n return transaction;\n }),\n );\n return new EthereumBlockWrapped(block, transactions);\n }\n\n async fetchBlocks(bufferBlocks: number[]): Promise<EthereumBlockWrapper[]> {\n return Promise.all(\n bufferBlocks.map(async (num) => {\n try {\n // Fetch Block\n return await this.fetchBlock(num);\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,oDAA4B;AAC5B,4CAAoB;AAGpB,4CAA+C;AAE/C,wDAAqD;AACrD,wDAA8E;AAE9E,gDAA6C;AAS7C,4CAA0E;AAC1E,8CAAkD;AAClD,qDAAwD;AACxD,qDAI0B;AAE1B,8DAA8D;AAC9D,MAAM,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IAAI,CAAC,cAAc,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,WAAW,CAAC,IAAA,oBAAY,EAAC,WAAW,CAAC,KAAK,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;AAxOD,kCAwOC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport assert from 'assert';\nimport fs from 'fs';\nimport http from 'http';\nimport https from 'https';\nimport { Interface } from '@ethersproject/abi';\nimport { Block, TransactionReceipt } from '@ethersproject/abstract-provider';\nimport { BigNumber } from '@ethersproject/bignumber';\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 { retryOnFailEth } from '../utils/project';\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 private endpointSupportsGetBlockReceipts = true;\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 logger.info(this.endpoint.split('?')[0]);\n if (protocolStr === 'https' || protocolStr === 'http') {\n const connection: ConnectionInfo = {\n url: this.endpoint.split('?')[0],\n headers: {\n 'User-Agent': `Subquery-Node ${packageVersion}`,\n },\n };\n searchParams.forEach((value, name, searchParams) => {\n (connection.headers as any)[name] = value;\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 getFinalizedBlockHeight(): Promise<number> {\n return (await this.client.getBlock('finalized')).number;\n }\n\n async getBestBlockHeight(): Promise<number> {\n return (await this.client.getBlock('safe')).number;\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 getBlockByHeightOrHash(heightOrHash: number | string): Promise<Block> {\n return this.client.getBlock(heightOrHash);\n }\n\n async getBlockPromise(num: number): Promise<any> {\n return retryOnFailEth(() =>\n this.client.send('eth_getBlockByNumber', [hexValue(num), true]),\n );\n }\n\n async getTransactionReceipt(\n transactionHash: string | Promise<string>,\n ): Promise<TransactionReceipt> {\n return retryOnFailEth<TransactionReceipt>(\n this.client.getTransactionReceipt.bind(this.client, transactionHash),\n );\n }\n async fetchBlock(num: number): Promise<EthereumBlockWrapper> {\n const block_promise = await this.getBlockPromise(num);\n\n const block = formatBlock(block_promise);\n block.stateRoot = this.client.formatter.hash(block.stateRoot);\n\n if (this.endpointSupportsGetBlockReceipts) {\n try {\n const rawReceipts: any[] = await this.client.send(\n 'eth_getBlockReceipts',\n [hexValue(block.number)],\n );\n\n const receipts = rawReceipts.map((receipt) =>\n formatReceipt(receipt, block),\n );\n\n const txs = block.transactions.map((tx) => {\n const transaction = formatTransaction(tx);\n transaction.receipt =\n receipts[BigNumber.from(transaction.transactionIndex).toNumber()];\n\n assert(\n transaction.hash === transaction.receipt.transactionHash,\n 'Failed to match receipt to transaction',\n );\n\n return transaction;\n });\n\n return new EthereumBlockWrapped(block, txs);\n } catch (e) {\n // Method not avaialble https://eips.ethereum.org/EIPS/eip-1474\n if (e?.error?.code === -32601) {\n logger.warn(\n `The endpoint doesn't support 'eth_getBlockReceipts', individual receipts will be fetched instead, this will greatly impact performance.`,\n );\n this.endpointSupportsGetBlockReceipts = false;\n\n // Should continue and use old method here\n } else {\n throw e;\n }\n }\n }\n\n const transactions = await Promise.all(\n block.transactions.map(async (tx) => {\n const transaction = formatTransaction(tx);\n const receipt = await this.getTransactionReceipt(tx.hash);\n transaction.receipt = formatReceipt(receipt, block);\n return transaction;\n }),\n );\n return new EthereumBlockWrapped(block, transactions);\n }\n\n async fetchBlocks(bufferBlocks: number[]): Promise<EthereumBlockWrapper[]> {\n return Promise.all(\n bufferBlocks.map(async (num) => {\n try {\n // Fetch Block\n return await this.fetchBlock(num);\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"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ // Copyright 2020-2022 OnFinality Limited authors & contributors
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const event_emitter_1 = require("@nestjs/event-emitter");
6
+ const testing_1 = require("@nestjs/testing");
7
+ const node_core_1 = require("@subql/node-core");
8
+ const graphql_1 = require("graphql");
9
+ const lodash_1 = require("lodash");
10
+ const api_service_ethereum_1 = require("./api.service.ethereum");
11
+ // Add api key to work
12
+ const WS_ENDPOINT = 'wss://eth.api.onfinality.io/ws?apikey=';
13
+ const HTTP_ENDPOINT = 'https://eth.api.onfinality.io/rpc?apikey=';
14
+ function testSubqueryProject(endpoint) {
15
+ return {
16
+ network: {
17
+ endpoint,
18
+ chainId: '1',
19
+ },
20
+ dataSources: [],
21
+ id: 'test',
22
+ root: './',
23
+ schema: new graphql_1.GraphQLSchema({}),
24
+ templates: [],
25
+ };
26
+ }
27
+ jest.setTimeout(90000);
28
+ describe('ApiService', () => {
29
+ let app;
30
+ afterEach(async () => {
31
+ return app === null || app === void 0 ? void 0 : app.close();
32
+ });
33
+ const prepareApiService = async (endpoint = HTTP_ENDPOINT) => {
34
+ const module = await testing_1.Test.createTestingModule({
35
+ providers: [
36
+ {
37
+ provide: 'ISubqueryProject',
38
+ useFactory: () => testSubqueryProject(endpoint),
39
+ },
40
+ api_service_ethereum_1.EthereumApiService,
41
+ ],
42
+ imports: [event_emitter_1.EventEmitterModule.forRoot()],
43
+ }).compile();
44
+ app = module.createNestApplication();
45
+ await app.init();
46
+ const apiService = app.get(api_service_ethereum_1.EthereumApiService);
47
+ await apiService.init();
48
+ return apiService;
49
+ };
50
+ it('can instantiate api', async () => {
51
+ const apiService = await prepareApiService();
52
+ console.log(apiService.api.getChainId());
53
+ await (0, node_core_1.delay)(0.5);
54
+ });
55
+ it('can fetch blocks', async () => {
56
+ const apiService = await prepareApiService();
57
+ await apiService.api.fetchBlocks((0, lodash_1.range)(12369621, 12369651));
58
+ await (0, node_core_1.delay)(0.5);
59
+ });
60
+ });
61
+ //# sourceMappingURL=api.service.ethereum.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.service.ethereum.test.js","sourceRoot":"","sources":["../../src/ethereum/api.service.ethereum.test.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;AAGtC,yDAA2D;AAC3D,6CAAuC;AACvC,gDAAyC;AACzC,qCAAwC;AACxC,mCAA+B;AAE/B,iEAA4D;AAE5D,sBAAsB;AACtB,MAAM,WAAW,GAAG,wCAAwC,CAAC;AAC7D,MAAM,aAAa,GAAG,2CAA2C,CAAC;AAElE,SAAS,mBAAmB,CAAC,QAAgB;IAC3C,OAAO;QACL,OAAO,EAAE;YACP,QAAQ;YACR,OAAO,EAAE,GAAG;SACb;QACD,WAAW,EAAE,EAAE;QACf,EAAE,EAAE,MAAM;QACV,IAAI,EAAE,IAAI;QACV,MAAM,EAAE,IAAI,uBAAa,CAAC,EAAE,CAAC;QAC7B,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC;AAED,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;AACvB,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,IAAI,GAAqB,CAAC;IAE1B,SAAS,CAAC,KAAK,IAAI,EAAE;QACnB,OAAO,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,KAAK,EAAE,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,MAAM,iBAAiB,GAAG,KAAK,EAC7B,WAAmB,aAAa,EACH,EAAE;QAC/B,MAAM,MAAM,GAAG,MAAM,cAAI,CAAC,mBAAmB,CAAC;YAC5C,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,kBAAkB;oBAC3B,UAAU,EAAE,GAAG,EAAE,CAAC,mBAAmB,CAAC,QAAQ,CAAC;iBAChD;gBACD,yCAAkB;aACnB;YACD,OAAO,EAAE,CAAC,kCAAkB,CAAC,OAAO,EAAE,CAAC;SACxC,CAAC,CAAC,OAAO,EAAE,CAAC;QAEb,GAAG,GAAG,MAAM,CAAC,qBAAqB,EAAE,CAAC;QACrC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QACjB,MAAM,UAAU,GAAG,GAAG,CAAC,GAAG,CAAC,yCAAkB,CAAC,CAAC;QAC/C,MAAM,UAAU,CAAC,IAAI,EAAE,CAAC;QACxB,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;IAEF,EAAE,CAAC,qBAAqB,EAAE,KAAK,IAAI,EAAE;QACnC,MAAM,UAAU,GAAG,MAAM,iBAAiB,EAAE,CAAC;QAC7C,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;QACzC,MAAM,IAAA,iBAAK,EAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kBAAkB,EAAE,KAAK,IAAI,EAAE;QAChC,MAAM,UAAU,GAAG,MAAM,iBAAiB,EAAE,CAAC;QAC7C,MAAM,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,IAAA,cAAK,EAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC5D,MAAM,IAAA,iBAAK,EAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { INestApplication } from '@nestjs/common';\nimport { EventEmitterModule } from '@nestjs/event-emitter';\nimport { Test } from '@nestjs/testing';\nimport { delay } from '@subql/node-core';\nimport { GraphQLSchema } from 'graphql';\nimport { range } from 'lodash';\nimport { SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApiService } from './api.service.ethereum';\n\n// Add api key to work\nconst WS_ENDPOINT = 'wss://eth.api.onfinality.io/ws?apikey=';\nconst HTTP_ENDPOINT = 'https://eth.api.onfinality.io/rpc?apikey=';\n\nfunction testSubqueryProject(endpoint: string): SubqueryProject {\n return {\n network: {\n endpoint,\n chainId: '1',\n },\n dataSources: [],\n id: 'test',\n root: './',\n schema: new GraphQLSchema({}),\n templates: [],\n };\n}\n\njest.setTimeout(90000);\ndescribe('ApiService', () => {\n let app: INestApplication;\n\n afterEach(async () => {\n return app?.close();\n });\n\n const prepareApiService = async (\n endpoint: string = HTTP_ENDPOINT,\n ): Promise<EthereumApiService> => {\n const module = await Test.createTestingModule({\n providers: [\n {\n provide: 'ISubqueryProject',\n useFactory: () => testSubqueryProject(endpoint),\n },\n EthereumApiService,\n ],\n imports: [EventEmitterModule.forRoot()],\n }).compile();\n\n app = module.createNestApplication();\n await app.init();\n const apiService = app.get(EthereumApiService);\n await apiService.init();\n return apiService;\n };\n\n it('can instantiate api', async () => {\n const apiService = await prepareApiService();\n console.log(apiService.api.getChainId());\n await delay(0.5);\n });\n\n it('can fetch blocks', async () => {\n const apiService = await prepareApiService();\n await apiService.api.fetchBlocks(range(12369621, 12369651));\n await delay(0.5);\n });\n});\n"]}
@@ -12,7 +12,7 @@ function calcInterval(api) {
12
12
  }
13
13
  exports.calcInterval = calcInterval;
14
14
  function handleAddress(value) {
15
- if (value === '0x') {
15
+ if (!value || value === '0x') {
16
16
  return null;
17
17
  }
18
18
  return (0, address_1.getAddress)(value);
@@ -27,105 +27,27 @@ function handleNumber(value) {
27
27
  return bignumber_1.BigNumber.from(value);
28
28
  }
29
29
  function formatBlock(block) {
30
- const newBlock = {
31
- difficulty: handleNumber(block.difficulty).toBigInt(),
32
- extDataGasUsed: block.extDataGasUsed,
33
- extDataHash: block.extDataHash,
34
- gasLimit: handleNumber(block.gasLimit).toBigInt(),
35
- gasUsed: handleNumber(block.gasUsed).toBigInt(),
36
- hash: block.hash,
37
- logsBloom: block.logsBloom,
38
- miner: block.miner,
39
- mixHash: block.mixHash,
40
- nonce: block.nonce,
41
- number: handleNumber(block.number).toNumber(),
42
- parentHash: block.parentHash,
43
- receiptsRoot: block.receiptsRoot,
44
- sha3Uncles: block.sha3Uncles,
45
- size: handleNumber(block.size).toBigInt(),
46
- stateRoot: block.stateRoot,
47
- timestamp: handleNumber(block.timestamp).toBigInt(),
48
- totalDifficulty: handleNumber(block.totalDifficulty).toBigInt(),
49
- transactions: block.transactions,
50
- transactionsRoot: block.transactionsRoot,
51
- uncles: block.uncles,
52
- baseFeePerGas: block.baseFeePerGas
30
+ return Object.assign(Object.assign({}, block), { difficulty: handleNumber(block.difficulty).toBigInt(), gasLimit: handleNumber(block.gasLimit).toBigInt(), gasUsed: handleNumber(block.gasUsed).toBigInt(), number: handleNumber(block.number).toNumber(), size: handleNumber(block.size).toBigInt(), timestamp: handleNumber(block.timestamp).toBigInt(), totalDifficulty: handleNumber(block.totalDifficulty).toBigInt(), baseFeePerGas: block.baseFeePerGas
53
31
  ? handleNumber(block.baseFeePerGas).toBigInt()
54
- : undefined,
55
- blockGasCost: block.blockGasCost
32
+ : undefined, blockGasCost: block.blockGasCost
56
33
  ? handleNumber(block.blockGasCost).toBigInt()
57
- : undefined,
58
- blockExtraData: block.blockExtraData,
59
- logs: [], // Filled in at AvalancheBlockWrapped constructor
60
- };
61
- return newBlock;
34
+ : undefined, logs: [] });
62
35
  }
63
36
  exports.formatBlock = formatBlock;
64
37
  function formatLog(log, block) {
65
- const newLog = {
66
- address: log.address,
67
- topics: log.topics,
68
- data: log.data,
69
- blockNumber: handleNumber(log.blockNumber).toNumber(),
70
- transactionHash: log.transactionHash,
71
- transactionIndex: handleNumber(log.transactionIndex).toNumber(),
72
- blockHash: log.blockHash,
73
- logIndex: handleNumber(log.logIndex).toNumber(),
74
- removed: log.removed,
75
- args: log.args,
76
- block,
77
- };
78
- return newLog;
38
+ return Object.assign(Object.assign({}, log), { address: handleAddress(log.address), blockNumber: handleNumber(log.blockNumber).toNumber(), transactionIndex: handleNumber(log.transactionIndex).toNumber(), logIndex: handleNumber(log.logIndex).toNumber(), block });
79
39
  }
80
40
  exports.formatLog = formatLog;
81
41
  function formatTransaction(tx) {
82
- const transaction = {
83
- blockHash: tx.blockHash,
84
- blockNumber: handleNumber(tx.blockNumber).toNumber(),
85
- from: tx.from,
86
- gas: handleNumber(tx.gas).toBigInt(),
87
- gasPrice: handleNumber(tx.gasPrice).toBigInt(),
88
- hash: tx.hash,
89
- input: tx.input,
90
- nonce: handleNumber(tx.nonce).toBigInt(),
91
- to: tx.to,
92
- transactionIndex: handleNumber(tx.transactionIndex).toBigInt(),
93
- value: handleNumber(tx.value).toBigInt(),
94
- type: tx.type,
95
- v: handleNumber(tx.v).toBigInt(),
96
- r: tx.r,
97
- s: tx.s,
98
- accessList: tx.accessList,
99
- chainId: tx.chainId,
100
- maxFeePerGas: tx.maxFeePerGas
42
+ return Object.assign(Object.assign({}, tx), { from: handleAddress(tx.from), to: handleAddress(tx.to), blockNumber: handleNumber(tx.blockNumber).toNumber(), gas: handleNumber(tx.gas).toBigInt(), gasPrice: handleNumber(tx.gasPrice).toBigInt(), nonce: handleNumber(tx.nonce).toBigInt(), transactionIndex: handleNumber(tx.transactionIndex).toBigInt(), value: handleNumber(tx.value).toBigInt(), v: handleNumber(tx.v).toBigInt(), maxFeePerGas: tx.maxFeePerGas
101
43
  ? handleNumber(tx.maxFeePerGas).toBigInt()
102
- : undefined,
103
- maxPriorityFeePerGas: tx.maxPriorityFeePerGas
44
+ : undefined, maxPriorityFeePerGas: tx.maxPriorityFeePerGas
104
45
  ? handleNumber(tx.maxPriorityFeePerGas).toBigInt()
105
- : undefined,
106
- receipt: undefined, // Filled in at AvalancheApi.fetchBlocks
107
- };
108
- return transaction;
46
+ : undefined, receipt: undefined });
109
47
  }
110
48
  exports.formatTransaction = formatTransaction;
111
49
  function formatReceipt(receipt, block) {
112
- const newReceipt = {
113
- blockHash: receipt.blockHash,
114
- blockNumber: handleNumber(receipt.blockNumber).toNumber(),
115
- contractAddress: receipt.contractAddress,
116
- cumulativeGasUsed: handleNumber(receipt.cumulativeGasUsed).toBigInt(),
117
- effectiveGasPrice: handleNumber(receipt.effectiveGasPrice).toBigInt(),
118
- from: receipt.from,
119
- gasUsed: handleNumber(receipt.gasUsed).toBigInt(),
120
- logs: receipt.logs.map((log) => formatLog(log, block)),
121
- logsBloom: receipt.logsBloom,
122
- status: Boolean(handleNumber(receipt.status).toNumber()),
123
- to: receipt.to,
124
- transactionHash: receipt.transactionHash,
125
- transactionIndex: handleNumber(receipt.transactionIndex).toNumber(),
126
- type: receipt.type,
127
- };
128
- return newReceipt;
50
+ return Object.assign(Object.assign({}, receipt), { from: handleAddress(receipt.from), to: handleAddress(receipt.to), blockNumber: handleNumber(receipt.blockNumber).toNumber(), cumulativeGasUsed: handleNumber(receipt.cumulativeGasUsed).toBigInt(), effectiveGasPrice: handleNumber(receipt.effectiveGasPrice).toBigInt(), gasUsed: handleNumber(receipt.gasUsed).toBigInt(), logs: receipt.logs.map((log) => formatLog(log, block)), status: Boolean(handleNumber(receipt.status).toNumber()), transactionIndex: handleNumber(receipt.transactionIndex).toNumber() });
129
51
  }
130
52
  exports.formatReceipt = formatReceipt;
131
53
  //# sourceMappingURL=utils.ethereum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.ethereum.js","sourceRoot":"","sources":["../../src/ethereum/utils.ethereum.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;AAEtC,oDAAoD;AACpD,wDAAqD;AACrD,wDAAgD;AAUhD,SAAgB,YAAY,CAAC,GAAe;IAC1C,kDAAkD;IAClD,OAAO,IAAI,CAAC;AACd,CAAC;AAHD,oCAGC;AAED,SAAS,aAAa,CAAC,KAAa;IAClC,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,OAAO,IAAI,CAAC;KACb;IACD,OAAO,IAAA,oBAAU,EAAC,KAAK,CAAC,CAAC;AAC3B,CAAC;AAED,SAAS,YAAY,CAAC,KAAsB;IAC1C,IAAI,KAAK,KAAK,SAAS,EAAE;QACvB,OAAO,gBAAI,CAAC;K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B,WAAW,EAAE,YAAY,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE;QACzD,eAAe,EAAE,OAAO,CAAC,eAAe;QACxC,iBAAiB,EAAE,YAAY,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,QAAQ,EAAE;QACrE,iBAAiB,EAAE,YAAY,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,QAAQ,EAAE;QACrE,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE;QACjD,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtD,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC;QACxD,EAAE,EAAE,OAAO,CAAC,EAAE;QACd,eAAe,EAAE,OAAO,CAAC,eAAe;QACxC,gBAAgB,EAAE,YAAY,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,QAAQ,EAAE;QACnE,IAAI,EAAE,OAAO,CAAC,IAAI;KACnB,CAAC;IACF,OAAO,UAAU,CAAC;AACpB,CAAC;AArBD,sCAqBC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getAddress } from '@ethersproject/address';\nimport { BigNumber } from '@ethersproject/bignumber';\nimport { Zero } from '@ethersproject/constants';\nimport {\n ApiWrapper,\n EthereumBlock,\n EthereumLog,\n EthereumReceipt,\n EthereumResult,\n EthereumTransaction,\n} from '@subql/types-ethereum';\n\nexport function calcInterval(api: ApiWrapper): number {\n // TODO find a way to get this from the blockchain\n return 6000;\n}\n\nfunction handleAddress(value: string): string {\n if (value === '0x') {\n return null;\n }\n return getAddress(value);\n}\n\nfunction handleNumber(value: string | number): BigNumber {\n if (value === undefined) {\n return Zero;\n }\n if (value === '0x') {\n return Zero;\n }\n return BigNumber.from(value);\n}\n\nexport function formatBlock(block: Record<string, any>): EthereumBlock {\n const newBlock: EthereumBlock = {\n difficulty: handleNumber(block.difficulty).toBigInt(),\n extDataGasUsed: block.extDataGasUsed,\n extDataHash: block.extDataHash,\n gasLimit: handleNumber(block.gasLimit).toBigInt(),\n gasUsed: handleNumber(block.gasUsed).toBigInt(),\n hash: block.hash,\n logsBloom: block.logsBloom,\n miner: block.miner,\n mixHash: block.mixHash,\n nonce: block.nonce,\n number: handleNumber(block.number).toNumber(),\n parentHash: block.parentHash,\n receiptsRoot: block.receiptsRoot,\n sha3Uncles: block.sha3Uncles,\n size: handleNumber(block.size).toBigInt(),\n stateRoot: block.stateRoot,\n timestamp: handleNumber(block.timestamp).toBigInt(),\n totalDifficulty: handleNumber(block.totalDifficulty).toBigInt(),\n transactions: block.transactions,\n transactionsRoot: block.transactionsRoot,\n uncles: block.uncles,\n baseFeePerGas: block.baseFeePerGas\n ? handleNumber(block.baseFeePerGas).toBigInt()\n : undefined,\n blockGasCost: block.blockGasCost\n ? handleNumber(block.blockGasCost).toBigInt()\n : undefined,\n blockExtraData: block.blockExtraData,\n logs: [], // Filled in at AvalancheBlockWrapped constructor\n };\n\n return newBlock;\n}\nexport function formatLog(\n log: EthereumLog<EthereumResult> | EthereumLog,\n block: EthereumBlock,\n): EthereumLog<EthereumResult> | EthereumLog {\n const newLog: EthereumLog<EthereumResult> = {\n address: log.address,\n topics: log.topics,\n data: log.data,\n blockNumber: handleNumber(log.blockNumber).toNumber(),\n transactionHash: log.transactionHash,\n transactionIndex: handleNumber(log.transactionIndex).toNumber(),\n blockHash: log.blockHash,\n logIndex: handleNumber(log.logIndex).toNumber(),\n removed: log.removed,\n args: log.args,\n block,\n };\n return newLog;\n}\n\nexport function formatTransaction(\n tx: Record<string, any>,\n): EthereumTransaction {\n const transaction: EthereumTransaction = {\n blockHash: tx.blockHash,\n blockNumber: handleNumber(tx.blockNumber).toNumber(),\n from: tx.from,\n gas: handleNumber(tx.gas).toBigInt(),\n gasPrice: handleNumber(tx.gasPrice).toBigInt(),\n hash: tx.hash,\n input: tx.input,\n nonce: handleNumber(tx.nonce).toBigInt(),\n to: tx.to,\n transactionIndex: handleNumber(tx.transactionIndex).toBigInt(),\n value: handleNumber(tx.value).toBigInt(),\n type: tx.type,\n v: handleNumber(tx.v).toBigInt(),\n r: tx.r,\n s: tx.s,\n accessList: tx.accessList,\n chainId: tx.chainId,\n maxFeePerGas: tx.maxFeePerGas\n ? handleNumber(tx.maxFeePerGas).toBigInt()\n : undefined,\n maxPriorityFeePerGas: tx.maxPriorityFeePerGas\n ? handleNumber(tx.maxPriorityFeePerGas).toBigInt()\n : undefined,\n receipt: undefined, // Filled in at AvalancheApi.fetchBlocks\n };\n return transaction;\n}\n\nexport function formatReceipt(\n receipt: Record<string, any>,\n block: EthereumBlock,\n): EthereumReceipt {\n const newReceipt: EthereumReceipt = {\n blockHash: receipt.blockHash,\n blockNumber: handleNumber(receipt.blockNumber).toNumber(),\n contractAddress: receipt.contractAddress,\n cumulativeGasUsed: handleNumber(receipt.cumulativeGasUsed).toBigInt(),\n effectiveGasPrice: handleNumber(receipt.effectiveGasPrice).toBigInt(),\n from: receipt.from,\n gasUsed: handleNumber(receipt.gasUsed).toBigInt(),\n logs: receipt.logs.map((log) => formatLog(log, block)),\n logsBloom: receipt.logsBloom,\n status: Boolean(handleNumber(receipt.status).toNumber()),\n to: receipt.to,\n transactionHash: receipt.transactionHash,\n transactionIndex: handleNumber(receipt.transactionIndex).toNumber(),\n type: receipt.type,\n };\n return newReceipt;\n}\n"]}
1
+ {"version":3,"file":"utils.ethereum.js","sourceRoot":"","sources":["../../src/ethereum/utils.ethereum.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;AAEtC,oDAAoD;AACpD,wDAAqD;AACrD,wDAAgD;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& contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getAddress } from '@ethersproject/address';\nimport { BigNumber } from '@ethersproject/bignumber';\nimport { Zero } from '@ethersproject/constants';\nimport {\n ApiWrapper,\n EthereumBlock,\n EthereumLog,\n EthereumReceipt,\n EthereumResult,\n EthereumTransaction,\n} from '@subql/types-ethereum';\n\nexport function calcInterval(api: ApiWrapper): number {\n // TODO find a way to get this from the blockchain\n return 6000;\n}\n\nfunction handleAddress(value: string): string | null {\n if (!value || value === '0x') {\n return null;\n }\n return getAddress(value);\n}\n\nfunction handleNumber(value: string | number): BigNumber {\n if (value === undefined) {\n return Zero;\n }\n if (value === '0x') {\n return Zero;\n }\n return BigNumber.from(value);\n}\n\nexport function formatBlock(block: Record<string, any>): EthereumBlock {\n return {\n ...block,\n difficulty: handleNumber(block.difficulty).toBigInt(),\n gasLimit: handleNumber(block.gasLimit).toBigInt(),\n gasUsed: handleNumber(block.gasUsed).toBigInt(),\n number: handleNumber(block.number).toNumber(),\n size: handleNumber(block.size).toBigInt(),\n timestamp: handleNumber(block.timestamp).toBigInt(),\n totalDifficulty: handleNumber(block.totalDifficulty).toBigInt(),\n baseFeePerGas: block.baseFeePerGas\n ? handleNumber(block.baseFeePerGas).toBigInt()\n : undefined,\n blockGasCost: block.blockGasCost\n ? handleNumber(block.blockGasCost).toBigInt()\n : undefined,\n logs: [], // Filled in at AvalancheBlockWrapped constructor\n } as EthereumBlock;\n}\nexport function formatLog(\n log: EthereumLog<EthereumResult> | EthereumLog,\n block: EthereumBlock,\n): EthereumLog<EthereumResult> | EthereumLog {\n return {\n ...log,\n address: handleAddress(log.address),\n blockNumber: handleNumber(log.blockNumber).toNumber(),\n transactionIndex: handleNumber(log.transactionIndex).toNumber(),\n logIndex: handleNumber(log.logIndex).toNumber(),\n block,\n } as EthereumLog<EthereumResult>;\n}\n\nexport function formatTransaction(\n tx: Record<string, any>,\n): EthereumTransaction {\n return {\n ...tx,\n from: handleAddress(tx.from),\n to: handleAddress(tx.to),\n blockNumber: handleNumber(tx.blockNumber).toNumber(),\n gas: handleNumber(tx.gas).toBigInt(),\n gasPrice: handleNumber(tx.gasPrice).toBigInt(),\n nonce: handleNumber(tx.nonce).toBigInt(),\n transactionIndex: handleNumber(tx.transactionIndex).toBigInt(),\n value: handleNumber(tx.value).toBigInt(),\n v: handleNumber(tx.v).toBigInt(),\n maxFeePerGas: tx.maxFeePerGas\n ? handleNumber(tx.maxFeePerGas).toBigInt()\n : undefined,\n maxPriorityFeePerGas: tx.maxPriorityFeePerGas\n ? handleNumber(tx.maxPriorityFeePerGas).toBigInt()\n : undefined,\n receipt: undefined, // Filled in at AvalancheApi.fetchBlocks\n } as EthereumTransaction;\n}\n\nexport function formatReceipt(\n receipt: Record<string, any>,\n block: EthereumBlock,\n): EthereumReceipt {\n return {\n ...receipt,\n from: handleAddress(receipt.from),\n to: handleAddress(receipt.to),\n blockNumber: handleNumber(receipt.blockNumber).toNumber(),\n cumulativeGasUsed: handleNumber(receipt.cumulativeGasUsed).toBigInt(),\n effectiveGasPrice: handleNumber(receipt.effectiveGasPrice).toBigInt(),\n gasUsed: handleNumber(receipt.gasUsed).toBigInt(),\n logs: receipt.logs.map((log) => formatLog(log, block)),\n status: Boolean(handleNumber(receipt.status).toNumber()),\n transactionIndex: handleNumber(receipt.transactionIndex).toNumber(),\n } as EthereumReceipt;\n}\n"]}
@@ -132,11 +132,11 @@ let IndexerManager = class IndexerManager {
132
132
  }
133
133
  async indexBlockData({ block, logs, transactions }, dataSources, getVM) {
134
134
  await this.indexBlockContent(block, dataSources, getVM);
135
- for (const log of logs) {
136
- await this.indexEvent(log, dataSources, getVM);
137
- }
138
135
  for (const tx of transactions) {
139
136
  await this.indexExtrinsic(tx, dataSources, getVM);
137
+ for (const log of logs.filter((l) => l.transactionHash === tx.hash)) {
138
+ await this.indexEvent(log, dataSources, getVM);
139
+ }
140
140
  }
141
141
  }
142
142
  async indexBlockContent(block, dataSources, getVM) {
@@ -1 +1 @@
1
- {"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,EAAE,EAAE,CAC/C,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;CACjC,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Block } from '@ethersproject/abstract-provider';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { hexToU8a, u8aEq } from '@polkadot/util';\nimport {\n isBlockHandlerProcessor,\n isCallHandlerProcessor,\n isEventHandlerProcessor,\n isCustomDs,\n isRuntimeDs,\n SubqlEthereumCustomDataSource,\n SubqlCustomHandler,\n EthereumHandlerKind,\n EthereumRuntimeHandlerInputMap,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n PoiBlock,\n StoreService,\n PoiService,\n NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { Sequelize, Transaction } from 'sequelize';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApi } from '../ethereum';\nimport { EthereumBlockWrapped } from '../ethereum/block.ethereum';\nimport { yargsOptions } from '../yargs';\nimport {\n asSecondLayerHandlerProcessor_1_0_0,\n DsProcessorService,\n} from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst NULL_MERKEL_ROOT = hexToU8a('0x00');\n\nconst logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager {\n private api: EthereumApi;\n private filteredDataSources: SubqlProjectDs[];\n\n constructor(\n private storeService: StoreService,\n private apiService: ApiService,\n private poiService: PoiService,\n private sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private dsProcessorService: DsProcessorService,\n private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n\n this.api = this.apiService.api;\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(blockContent: EthereumBlockWrapper): Promise<{\n dynamicDsCreated: boolean;\n operationHash: Uint8Array;\n reindexBlockHeight: null;\n }> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n const tx = await this.sequelize.transaction();\n this.storeService.setTransaction(tx);\n this.storeService.setBlockHeight(blockHeight);\n\n let operationHash = NULL_MERKEL_ROOT;\n let poiBlockHash: Uint8Array;\n\n try {\n this.filteredDataSources = this.filterDataSources(blockHeight);\n\n const datasources = this.filteredDataSources.concat(\n ...(await this.dynamicDsService.getDynamicDatasources()),\n );\n\n reindexBlockHeight = await this.processUnfinalizedBlocks(block, tx);\n\n await this.indexBlockData(\n blockContent,\n datasources,\n // eslint-disable-next-line @typescript-eslint/require-await\n async (ds: SubqlProjectDs) => {\n const vm = this.sandboxService.getDsProcessorWrapper(\n ds,\n this.api,\n blockContent,\n );\n\n // Inject function to create ds into vm\n vm.freeze(\n async (templateName: string, args?: Record<string, unknown>) => {\n const newDs = await this.dynamicDsService.createDynamicDatasource(\n {\n templateName,\n args,\n startBlock: blockHeight,\n },\n tx,\n );\n // Push the newly created dynamic ds to be processed this block on any future extrinsics/events\n datasources.push(newDs);\n dynamicDsCreated = true;\n },\n 'createDynamicDatasource',\n );\n\n return vm;\n },\n );\n\n await this.storeService.setMetadataBatch(\n [\n { key: 'lastProcessedHeight', value: blockHeight },\n { key: 'lastProcessedTimestamp', value: Date.now() },\n ],\n { transaction: tx },\n );\n // Db Metadata increase BlockCount, in memory ref to block-dispatcher _processedBlockCount\n await this.storeService.incrementJsonbCount('processedBlockCount', tx);\n\n // Need calculate operationHash to ensure correct offset insert all time\n operationHash = this.storeService.getOperationMerkleRoot();\n if (this.nodeConfig.proofOfIndex) {\n //check if operation is null, then poi will not be inserted\n if (!u8aEq(operationHash, NULL_MERKEL_ROOT)) {\n const poiBlock = PoiBlock.create(\n blockHeight,\n blockContent.block.hash,\n operationHash,\n await this.poiService.getLatestPoiBlockHash(),\n this.project.id,\n );\n poiBlockHash = poiBlock.hash;\n await this.storeService.setPoi(poiBlock, { transaction: tx });\n this.poiService.setLatestPoiBlockHash(poiBlockHash);\n await this.storeService.setMetadataBatch(\n [{ key: 'lastPoiHeight', value: blockHeight }],\n { transaction: tx },\n );\n }\n }\n } catch (e) {\n await tx.rollback();\n throw e;\n }\n\n await tx.commit();\n\n return {\n dynamicDsCreated,\n operationHash,\n reindexBlockHeight,\n };\n }\n\n async start(): Promise<void> {\n await this.projectService.init();\n logger.info('indexer manager started');\n }\n\n private async processUnfinalizedBlocks(\n block: EthereumBlock,\n tx: Transaction,\n ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block, tx);\n }\n return null;\n }\n\n private filterDataSources(nextProcessingHeight: number): SubqlProjectDs[] {\n const filteredDs = this.projectService.dataSources.filter(\n (ds) => ds.startBlock <= nextProcessingHeight,\n );\n\n if (filteredDs.length === 0) {\n logger.error(\n `Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${nextProcessingHeight}\n or delete your database and start again from the currently specified startBlock`,\n );\n process.exit(1);\n }\n // perform filter for custom ds\n if (!filteredDs.length) {\n logger.error(`Did not find any datasources with associated processor`);\n process.exit(1);\n }\n return filteredDs;\n }\n\n private async indexBlockData(\n { block, logs, transactions }: EthereumBlockWrapper,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n await this.indexBlockContent(block, dataSources, getVM);\n\n for (const log of logs) {\n await this.indexEvent(log, dataSources, getVM);\n }\n\n for (const tx of transactions) {\n await this.indexExtrinsic(tx, dataSources, getVM);\n }\n }\n\n private async indexBlockContent(\n block: EthereumBlock,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Block, block, ds, getVM);\n }\n }\n\n private async indexExtrinsic(\n tx: EthereumTransaction,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Call, tx, ds, getVM);\n }\n }\n\n private async indexEvent(\n log: EthereumLog,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Event, log, ds, getVM);\n }\n }\n\n private async indexData<K extends EthereumHandlerKind>(\n kind: K,\n data: EthereumRuntimeHandlerInputMap[K],\n ds: SubqlProjectDs,\n getVM: (ds: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n let vm: IndexerSandbox;\n if (isRuntimeDs(ds)) {\n const handlers = (ds.mapping.handlers as SubqlRuntimeHandler[]).filter(\n (h) =>\n h.kind === kind &&\n FilterTypeMap[kind](\n data as any,\n h.filter as any,\n ds.options?.address,\n ),\n );\n\n if (!handlers.length) {\n return;\n }\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n this.nodeConfig.profiler\n ? await profilerWrap(\n vm.securedExec.bind(vm),\n 'handlerPerformance',\n handler.handler,\n )(handler.handler, [parsedData])\n : await vm.securedExec(handler.handler, [parsedData]);\n }\n } else if (isCustomDs(ds)) {\n const handlers = this.filterCustomDsHandlers<K>(\n ds,\n data,\n ProcessorTypeMap[kind],\n (data, baseFilter) => {\n switch (kind) {\n case EthereumHandlerKind.Block:\n return EthereumBlockWrapped.filterBlocksProcessor(\n data as EthereumBlock,\n baseFilter,\n );\n case EthereumHandlerKind.Call:\n return EthereumBlockWrapped.filterTransactionsProcessor(\n data as EthereumTransaction,\n baseFilter,\n );\n case EthereumHandlerKind.Event:\n return EthereumBlockWrapped.filterLogsProcessor(\n data as EthereumLog,\n baseFilter,\n );\n default:\n throw new Error('Unsupported handler kind');\n }\n },\n );\n\n if (!handlers.length) {\n return;\n }\n\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n await this.transformAndExecuteCustomDs(ds, vm, handler, parsedData);\n }\n }\n }\n\n private filterCustomDsHandlers<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n data: EthereumRuntimeHandlerInputMap[K],\n baseHandlerCheck: ProcessorTypeMap[K],\n baseFilter: (\n data: EthereumRuntimeHandlerInputMap[K],\n baseFilter: any,\n ) => boolean,\n ): SubqlCustomHandler[] {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n\n return ds.mapping.handlers\n .filter((handler) => {\n const processor = plugin.handlerProcessors[handler.kind];\n if (baseHandlerCheck(processor)) {\n processor.baseFilter;\n\n return baseFilter(data, processor.baseFilter);\n }\n return false;\n })\n .filter((handler) => {\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n try {\n return processor.filterProcessor({\n filter: handler.filter,\n input: data,\n ds,\n });\n } catch (e) {\n logger.error(e, 'Failed to run ds processer filter.');\n throw e;\n }\n });\n }\n\n private async transformAndExecuteCustomDs<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n vm: IndexerSandbox,\n handler: SubqlCustomHandler,\n data: EthereumRuntimeHandlerInputMap[K],\n ): Promise<void> {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n const assets = await this.dsProcessorService.getAssets(ds);\n\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n const transformedData = await processor\n .transformer({\n input: data,\n ds,\n api: this.api,\n filter: handler.filter,\n assets,\n })\n .catch((e) => {\n logger.error(e, 'Failed to transform data with ds processor.');\n throw e;\n });\n\n // We can not run this in parallel. the transformed data items may be dependent on one another.\n // An example of this is with Acala EVM packing multiple EVM logs into a single Substrate event\n for (const _data of transformedData) {\n await vm.securedExec(handler.handler, [_data]);\n }\n }\n}\n\ntype ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: typeof isBlockHandlerProcessor;\n [EthereumHandlerKind.Event]: typeof isEventHandlerProcessor;\n [EthereumHandlerKind.Call]: typeof isCallHandlerProcessor;\n};\n\nconst ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: isBlockHandlerProcessor,\n [EthereumHandlerKind.Event]: isEventHandlerProcessor,\n [EthereumHandlerKind.Call]: isCallHandlerProcessor,\n};\n\nconst FilterTypeMap = {\n [EthereumHandlerKind.Block]: EthereumBlockWrapped.filterBlocksProcessor,\n [EthereumHandlerKind.Event]: EthereumBlockWrapped.filterLogsProcessor,\n [EthereumHandlerKind.Call]: EthereumBlockWrapped.filterTransactionsProcessor,\n};\n\nconst DataAbiParser = {\n [EthereumHandlerKind.Block]: () => (data: EthereumBlock) => data,\n [EthereumHandlerKind.Event]: (api: EthereumApi) => api.parseLog.bind(api),\n [EthereumHandlerKind.Call]: (api: EthereumApi) =>\n api.parseTransaction.bind(api),\n};\n"]}
1
+ {"version":3,"file":"indexer.manager.js","sourceRoot":"","sources":["../../src/indexer/indexer.manager.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,sCAAsC;;;;;;;;;;;;;;;AAGtC,2CAAoD;AACpD,yCAAiD;AACjD,4DAUgC;AAChC,gDAU0B;AAQ1B,yCAAmD;AACnD,kEAA+E;AAE/E,+DAAkE;AAClE,oCAAwC;AACxC,iEAGgC;AAChC,6DAAwD;AACxD,uDAAmD;AACnD,uDAAmD;AACnD,2EAAuE;AAEvE,MAAM,gBAAgB,GAAG,IAAA,eAAQ,EAAC,MAAM,CAAC,CAAC;AAE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C,kBAAkB,CAAC,CAAA;qCAJL,wBAAY;QACd,sBAAU;QACV,sBAAU;QACX,qBAAS;QAC0B,iCAAe;QACjD,sBAAU;QACN,gCAAc;QACZ,qCAAgB;QACR,oDAAwB;QAC9B,yCAAkB;QACtB,gCAAc;GAf7B,cAAc,CA+V1B;AA/VY,wCAAc;AAuW3B,MAAM,gBAAgB,GAAG;IACvB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,yCAAuB;IACpD,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,yCAAuB;IACpD,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,wCAAsB;CACnD,CAAC;AAEF,MAAM,aAAa,GAAG;IACpB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,qCAAoB,CAAC,qBAAqB;IACvE,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,qCAAoB,CAAC,mBAAmB;IACrE,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,qCAAoB,CAAC,2BAA2B;CAC7E,CAAC;AAEF,MAAM,aAAa,GAAG;IACpB,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,IAAmB,EAAE,EAAE,CAAC,IAAI;IAChE,CAAC,qCAAmB,CAAC,KAAK,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;IACzE,CAAC,qCAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,GAAgB,EAAE,EAAE,CAC/C,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;CACjC,CAAC","sourcesContent":["// Copyright 2020-2022 OnFinality Limited authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Block } from '@ethersproject/abstract-provider';\nimport { Inject, Injectable } from '@nestjs/common';\nimport { hexToU8a, u8aEq } from '@polkadot/util';\nimport {\n isBlockHandlerProcessor,\n isCallHandlerProcessor,\n isEventHandlerProcessor,\n isCustomDs,\n isRuntimeDs,\n SubqlEthereumCustomDataSource,\n SubqlCustomHandler,\n EthereumHandlerKind,\n EthereumRuntimeHandlerInputMap,\n} from '@subql/common-ethereum';\nimport {\n ApiService,\n PoiBlock,\n StoreService,\n PoiService,\n NodeConfig,\n getLogger,\n profiler,\n profilerWrap,\n IndexerSandbox,\n} from '@subql/node-core';\nimport {\n EthereumTransaction,\n EthereumLog,\n SubqlRuntimeHandler,\n EthereumBlockWrapper,\n EthereumBlock,\n} from '@subql/types-ethereum';\nimport { Sequelize, Transaction } from 'sequelize';\nimport { SubqlProjectDs, SubqueryProject } from '../configure/SubqueryProject';\nimport { EthereumApi } from '../ethereum';\nimport { EthereumBlockWrapped } from '../ethereum/block.ethereum';\nimport { yargsOptions } from '../yargs';\nimport {\n asSecondLayerHandlerProcessor_1_0_0,\n DsProcessorService,\n} from './ds-processor.service';\nimport { DynamicDsService } from './dynamic-ds.service';\nimport { ProjectService } from './project.service';\nimport { SandboxService } from './sandbox.service';\nimport { UnfinalizedBlocksService } from './unfinalizedBlocks.service';\n\nconst NULL_MERKEL_ROOT = hexToU8a('0x00');\n\nconst logger = getLogger('indexer');\n\n@Injectable()\nexport class IndexerManager {\n private api: EthereumApi;\n private filteredDataSources: SubqlProjectDs[];\n\n constructor(\n private storeService: StoreService,\n private apiService: ApiService,\n private poiService: PoiService,\n private sequelize: Sequelize,\n @Inject('ISubqueryProject') private readonly project: SubqueryProject,\n private nodeConfig: NodeConfig,\n private sandboxService: SandboxService,\n private dynamicDsService: DynamicDsService,\n private unfinalizedBlocksService: UnfinalizedBlocksService,\n private dsProcessorService: DsProcessorService,\n private projectService: ProjectService,\n ) {\n logger.info('indexer manager start');\n\n this.api = this.apiService.api;\n }\n\n @profiler(yargsOptions.argv.profiler)\n async indexBlock(blockContent: EthereumBlockWrapper): Promise<{\n dynamicDsCreated: boolean;\n operationHash: Uint8Array;\n reindexBlockHeight: null;\n }> {\n const { block, blockHeight } = blockContent;\n let dynamicDsCreated = false;\n let reindexBlockHeight = null;\n const tx = await this.sequelize.transaction();\n this.storeService.setTransaction(tx);\n this.storeService.setBlockHeight(blockHeight);\n\n let operationHash = NULL_MERKEL_ROOT;\n let poiBlockHash: Uint8Array;\n\n try {\n this.filteredDataSources = this.filterDataSources(blockHeight);\n\n const datasources = this.filteredDataSources.concat(\n ...(await this.dynamicDsService.getDynamicDatasources()),\n );\n\n reindexBlockHeight = await this.processUnfinalizedBlocks(block, tx);\n\n await this.indexBlockData(\n blockContent,\n datasources,\n // eslint-disable-next-line @typescript-eslint/require-await\n async (ds: SubqlProjectDs) => {\n const vm = this.sandboxService.getDsProcessorWrapper(\n ds,\n this.api,\n blockContent,\n );\n\n // Inject function to create ds into vm\n vm.freeze(\n async (templateName: string, args?: Record<string, unknown>) => {\n const newDs = await this.dynamicDsService.createDynamicDatasource(\n {\n templateName,\n args,\n startBlock: blockHeight,\n },\n tx,\n );\n // Push the newly created dynamic ds to be processed this block on any future extrinsics/events\n datasources.push(newDs);\n dynamicDsCreated = true;\n },\n 'createDynamicDatasource',\n );\n\n return vm;\n },\n );\n\n await this.storeService.setMetadataBatch(\n [\n { key: 'lastProcessedHeight', value: blockHeight },\n { key: 'lastProcessedTimestamp', value: Date.now() },\n ],\n { transaction: tx },\n );\n // Db Metadata increase BlockCount, in memory ref to block-dispatcher _processedBlockCount\n await this.storeService.incrementJsonbCount('processedBlockCount', tx);\n\n // Need calculate operationHash to ensure correct offset insert all time\n operationHash = this.storeService.getOperationMerkleRoot();\n if (this.nodeConfig.proofOfIndex) {\n //check if operation is null, then poi will not be inserted\n if (!u8aEq(operationHash, NULL_MERKEL_ROOT)) {\n const poiBlock = PoiBlock.create(\n blockHeight,\n blockContent.block.hash,\n operationHash,\n await this.poiService.getLatestPoiBlockHash(),\n this.project.id,\n );\n poiBlockHash = poiBlock.hash;\n await this.storeService.setPoi(poiBlock, { transaction: tx });\n this.poiService.setLatestPoiBlockHash(poiBlockHash);\n await this.storeService.setMetadataBatch(\n [{ key: 'lastPoiHeight', value: blockHeight }],\n { transaction: tx },\n );\n }\n }\n } catch (e) {\n await tx.rollback();\n throw e;\n }\n\n await tx.commit();\n\n return {\n dynamicDsCreated,\n operationHash,\n reindexBlockHeight,\n };\n }\n\n async start(): Promise<void> {\n await this.projectService.init();\n logger.info('indexer manager started');\n }\n\n private async processUnfinalizedBlocks(\n block: EthereumBlock,\n tx: Transaction,\n ): Promise<number | null> {\n if (this.nodeConfig.unfinalizedBlocks) {\n return this.unfinalizedBlocksService.processUnfinalizedBlocks(block, tx);\n }\n return null;\n }\n\n private filterDataSources(nextProcessingHeight: number): SubqlProjectDs[] {\n const filteredDs = this.projectService.dataSources.filter(\n (ds) => ds.startBlock <= nextProcessingHeight,\n );\n\n if (filteredDs.length === 0) {\n logger.error(\n `Your start block is greater than the current indexed block height in your database. Either change your startBlock (project.yaml) to <= ${nextProcessingHeight}\n or delete your database and start again from the currently specified startBlock`,\n );\n process.exit(1);\n }\n // perform filter for custom ds\n if (!filteredDs.length) {\n logger.error(`Did not find any datasources with associated processor`);\n process.exit(1);\n }\n return filteredDs;\n }\n\n private async indexBlockData(\n { block, logs, transactions }: EthereumBlockWrapper,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n await this.indexBlockContent(block, dataSources, getVM);\n\n for (const tx of transactions) {\n await this.indexExtrinsic(tx, dataSources, getVM);\n\n for (const log of logs.filter((l) => l.transactionHash === tx.hash)) {\n await this.indexEvent(log, dataSources, getVM);\n }\n }\n }\n\n private async indexBlockContent(\n block: EthereumBlock,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Block, block, ds, getVM);\n }\n }\n\n private async indexExtrinsic(\n tx: EthereumTransaction,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Call, tx, ds, getVM);\n }\n }\n\n private async indexEvent(\n log: EthereumLog,\n dataSources: SubqlProjectDs[],\n getVM: (d: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n for (const ds of dataSources) {\n await this.indexData(EthereumHandlerKind.Event, log, ds, getVM);\n }\n }\n\n private async indexData<K extends EthereumHandlerKind>(\n kind: K,\n data: EthereumRuntimeHandlerInputMap[K],\n ds: SubqlProjectDs,\n getVM: (ds: SubqlProjectDs) => Promise<IndexerSandbox>,\n ): Promise<void> {\n let vm: IndexerSandbox;\n if (isRuntimeDs(ds)) {\n const handlers = (ds.mapping.handlers as SubqlRuntimeHandler[]).filter(\n (h) =>\n h.kind === kind &&\n FilterTypeMap[kind](\n data as any,\n h.filter as any,\n ds.options?.address,\n ),\n );\n\n if (!handlers.length) {\n return;\n }\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n this.nodeConfig.profiler\n ? await profilerWrap(\n vm.securedExec.bind(vm),\n 'handlerPerformance',\n handler.handler,\n )(handler.handler, [parsedData])\n : await vm.securedExec(handler.handler, [parsedData]);\n }\n } else if (isCustomDs(ds)) {\n const handlers = this.filterCustomDsHandlers<K>(\n ds,\n data,\n ProcessorTypeMap[kind],\n (data, baseFilter) => {\n switch (kind) {\n case EthereumHandlerKind.Block:\n return EthereumBlockWrapped.filterBlocksProcessor(\n data as EthereumBlock,\n baseFilter,\n );\n case EthereumHandlerKind.Call:\n return EthereumBlockWrapped.filterTransactionsProcessor(\n data as EthereumTransaction,\n baseFilter,\n );\n case EthereumHandlerKind.Event:\n return EthereumBlockWrapped.filterLogsProcessor(\n data as EthereumLog,\n baseFilter,\n );\n default:\n throw new Error('Unsupported handler kind');\n }\n },\n );\n\n if (!handlers.length) {\n return;\n }\n\n const parsedData = await DataAbiParser[kind](this.api)(data, ds);\n\n for (const handler of handlers) {\n vm = vm ?? (await getVM(ds));\n await this.transformAndExecuteCustomDs(ds, vm, handler, parsedData);\n }\n }\n }\n\n private filterCustomDsHandlers<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n data: EthereumRuntimeHandlerInputMap[K],\n baseHandlerCheck: ProcessorTypeMap[K],\n baseFilter: (\n data: EthereumRuntimeHandlerInputMap[K],\n baseFilter: any,\n ) => boolean,\n ): SubqlCustomHandler[] {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n\n return ds.mapping.handlers\n .filter((handler) => {\n const processor = plugin.handlerProcessors[handler.kind];\n if (baseHandlerCheck(processor)) {\n processor.baseFilter;\n\n return baseFilter(data, processor.baseFilter);\n }\n return false;\n })\n .filter((handler) => {\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n try {\n return processor.filterProcessor({\n filter: handler.filter,\n input: data,\n ds,\n });\n } catch (e) {\n logger.error(e, 'Failed to run ds processer filter.');\n throw e;\n }\n });\n }\n\n private async transformAndExecuteCustomDs<K extends EthereumHandlerKind>(\n ds: SubqlEthereumCustomDataSource<string, any>,\n vm: IndexerSandbox,\n handler: SubqlCustomHandler,\n data: EthereumRuntimeHandlerInputMap[K],\n ): Promise<void> {\n const plugin = this.dsProcessorService.getDsProcessor(ds);\n const assets = await this.dsProcessorService.getAssets(ds);\n\n const processor = asSecondLayerHandlerProcessor_1_0_0(\n plugin.handlerProcessors[handler.kind],\n );\n\n const transformedData = await processor\n .transformer({\n input: data,\n ds,\n api: this.api,\n filter: handler.filter,\n assets,\n })\n .catch((e) => {\n logger.error(e, 'Failed to transform data with ds processor.');\n throw e;\n });\n\n // We can not run this in parallel. the transformed data items may be dependent on one another.\n // An example of this is with Acala EVM packing multiple EVM logs into a single Substrate event\n for (const _data of transformedData) {\n await vm.securedExec(handler.handler, [_data]);\n }\n }\n}\n\ntype ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: typeof isBlockHandlerProcessor;\n [EthereumHandlerKind.Event]: typeof isEventHandlerProcessor;\n [EthereumHandlerKind.Call]: typeof isCallHandlerProcessor;\n};\n\nconst ProcessorTypeMap = {\n [EthereumHandlerKind.Block]: isBlockHandlerProcessor,\n [EthereumHandlerKind.Event]: isEventHandlerProcessor,\n [EthereumHandlerKind.Call]: isCallHandlerProcessor,\n};\n\nconst FilterTypeMap = {\n [EthereumHandlerKind.Block]: EthereumBlockWrapped.filterBlocksProcessor,\n [EthereumHandlerKind.Event]: EthereumBlockWrapped.filterLogsProcessor,\n [EthereumHandlerKind.Call]: EthereumBlockWrapped.filterTransactionsProcessor,\n};\n\nconst DataAbiParser = {\n [EthereumHandlerKind.Block]: () => (data: EthereumBlock) => data,\n [EthereumHandlerKind.Event]: (api: EthereumApi) => api.parseLog.bind(api),\n [EthereumHandlerKind.Call]: (api: EthereumApi) =>\n api.parseTransaction.bind(api),\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subql/node-ethereum",
3
- "version": "0.4.0",
3
+ "version": "0.4.1-0",
4
4
  "description": "",
5
5
  "author": "Ian He",
6
6
  "license": "Apache-2.0",
@@ -73,5 +73,6 @@
73
73
  "files": [
74
74
  "/dist",
75
75
  "/bin"
76
- ]
76
+ ],
77
+ "stableVersion": "0.4.0"
77
78
  }