@sentio/sdk 2.11.7 → 2.12.0-rc.1
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.
- package/lib/eth/base-processor.d.ts +25 -5
- package/lib/eth/base-processor.js +80 -2
- package/lib/eth/base-processor.js.map +1 -1
- package/lib/eth/context.d.ts +4 -0
- package/lib/eth/context.js +8 -0
- package/lib/eth/context.js.map +1 -1
- package/lib/eth/eth-plugin.d.ts +2 -0
- package/lib/eth/eth-plugin.js +51 -2
- package/lib/eth/eth-plugin.js.map +1 -1
- package/lib/eth/index.d.ts +2 -2
- package/lib/eth/index.js +2 -2
- package/lib/eth/index.js.map +1 -1
- package/package.json +4 -4
- package/src/eth/base-processor.ts +115 -4
- package/src/eth/context.ts +17 -0
- package/src/eth/eth-plugin.ts +64 -2
- package/src/eth/index.ts +2 -2
@@ -1,10 +1,11 @@
|
|
1
|
-
import { BaseContract, DeferredTopicFilter } from 'ethers';
|
1
|
+
import { BaseContract, DeferredTopicFilter, TransactionResponseParams } from 'ethers';
|
2
2
|
import { BlockParams, Network } from 'ethers/providers';
|
3
|
-
import { BoundContractView, ContractContext, ContractView } from './context.js';
|
4
|
-
import { AddressType, Data_EthBlock, Data_EthLog, Data_EthTrace, EthFetchConfig, HandleInterval, ProcessResult } from '@sentio/protos';
|
3
|
+
import { BoundContractView, ContractContext, ContractView, GlobalContext } from './context.js';
|
4
|
+
import { AddressType, Data_EthBlock, Data_EthLog, Data_EthTrace, Data_EthTransaction, EthFetchConfig, HandleInterval, ProcessResult } from '@sentio/protos';
|
5
5
|
import { BindOptions } from './bind-options.js';
|
6
6
|
import { PromiseOrVoid } from '../core/promises.js';
|
7
7
|
import { TypedEvent, TypedCallTrace } from './eth.js';
|
8
|
+
import { ListStateStorage } from '@sentio/runtime';
|
8
9
|
export interface AddressOrTypeEventFilter extends DeferredTopicFilter {
|
9
10
|
addressType?: AddressType;
|
10
11
|
address?: string;
|
@@ -19,11 +20,15 @@ export declare class TraceHandler {
|
|
19
20
|
handler: (trace: Data_EthTrace) => Promise<ProcessResult>;
|
20
21
|
fetchConfig: EthFetchConfig;
|
21
22
|
}
|
22
|
-
export declare class
|
23
|
+
export declare class BlockHandler {
|
23
24
|
blockInterval?: HandleInterval;
|
24
25
|
timeIntervalInMinutes?: HandleInterval;
|
25
26
|
handler: (block: Data_EthBlock) => Promise<ProcessResult>;
|
26
27
|
}
|
28
|
+
export declare class TransactionHandler {
|
29
|
+
handler: (block: Data_EthTransaction) => Promise<ProcessResult>;
|
30
|
+
fetchConfig: EthFetchConfig;
|
31
|
+
}
|
27
32
|
declare class BindInternalOptions {
|
28
33
|
address: string;
|
29
34
|
network: Network;
|
@@ -31,8 +36,23 @@ declare class BindInternalOptions {
|
|
31
36
|
startBlock: bigint;
|
32
37
|
endBlock?: bigint;
|
33
38
|
}
|
39
|
+
export declare class GlobalProcessorState extends ListStateStorage<GlobalProcessor> {
|
40
|
+
static INSTANCE: GlobalProcessorState;
|
41
|
+
}
|
42
|
+
export declare class GlobalProcessor {
|
43
|
+
config: BindInternalOptions;
|
44
|
+
blockHandlers: BlockHandler[];
|
45
|
+
transactionHandler: TransactionHandler[];
|
46
|
+
static bind(config: Omit<BindOptions, 'address'>): GlobalProcessor;
|
47
|
+
constructor(config: Omit<BindOptions, 'address'>);
|
48
|
+
onBlockInterval(handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid, blockInterval?: number, backfillBlockInterval?: number): this;
|
49
|
+
onTimeInterval(handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid, timeIntervalInMinutes?: number, backfillTimeIntervalInMinutes?: number): this;
|
50
|
+
getChainId(): number;
|
51
|
+
onInterval(handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid, timeInterval: HandleInterval | undefined, blockInterval: HandleInterval | undefined): this;
|
52
|
+
onTransaction(handler: (transaction: TransactionResponseParams, ctx: GlobalContext) => PromiseOrVoid, fetchConfig?: Partial<EthFetchConfig>): this;
|
53
|
+
}
|
34
54
|
export declare abstract class BaseProcessor<TContract extends BaseContract, TBoundContractView extends BoundContractView<TContract, ContractView<TContract>>> {
|
35
|
-
blockHandlers:
|
55
|
+
blockHandlers: BlockHandler[];
|
36
56
|
eventHandlers: EventsHandler[];
|
37
57
|
traceHandlers: TraceHandler[];
|
38
58
|
config: BindInternalOptions;
|
@@ -1,10 +1,11 @@
|
|
1
|
-
import { ContractContext } from './context.js';
|
1
|
+
import { ContractContext, GlobalContext } from './context.js';
|
2
2
|
import { EthFetchConfig, ProcessResult, } from '@sentio/protos';
|
3
3
|
import { ServerError, Status } from 'nice-grpc';
|
4
4
|
import { fixEmptyKey, formatEthData } from './eth.js';
|
5
5
|
import * as console from 'console';
|
6
6
|
import { getNetworkFromCtxOrNetworkish } from './provider.js';
|
7
7
|
import sha3 from 'js-sha3';
|
8
|
+
import { ListStateStorage } from '@sentio/runtime';
|
8
9
|
export class EventsHandler {
|
9
10
|
filters;
|
10
11
|
handler;
|
@@ -15,11 +16,15 @@ export class TraceHandler {
|
|
15
16
|
handler;
|
16
17
|
fetchConfig;
|
17
18
|
}
|
18
|
-
export class
|
19
|
+
export class BlockHandler {
|
19
20
|
blockInterval;
|
20
21
|
timeIntervalInMinutes;
|
21
22
|
handler;
|
22
23
|
}
|
24
|
+
export class TransactionHandler {
|
25
|
+
handler;
|
26
|
+
fetchConfig;
|
27
|
+
}
|
23
28
|
class BindInternalOptions {
|
24
29
|
address;
|
25
30
|
network;
|
@@ -27,6 +32,79 @@ class BindInternalOptions {
|
|
27
32
|
startBlock;
|
28
33
|
endBlock;
|
29
34
|
}
|
35
|
+
class GlobalProcessorState extends ListStateStorage {
|
36
|
+
static INSTANCE = new GlobalProcessorState();
|
37
|
+
}
|
38
|
+
export { GlobalProcessorState };
|
39
|
+
export class GlobalProcessor {
|
40
|
+
config;
|
41
|
+
blockHandlers = [];
|
42
|
+
transactionHandler = [];
|
43
|
+
static bind(config) {
|
44
|
+
const processor = new GlobalProcessor(config);
|
45
|
+
GlobalProcessorState.INSTANCE.addValue(processor);
|
46
|
+
return processor;
|
47
|
+
}
|
48
|
+
constructor(config) {
|
49
|
+
this.config = {
|
50
|
+
address: '*',
|
51
|
+
name: config.name || 'Global',
|
52
|
+
network: getNetworkFromCtxOrNetworkish(config.network),
|
53
|
+
startBlock: 0n,
|
54
|
+
};
|
55
|
+
if (config.startBlock) {
|
56
|
+
this.config.startBlock = BigInt(config.startBlock);
|
57
|
+
}
|
58
|
+
if (config.endBlock) {
|
59
|
+
this.config.endBlock = BigInt(config.endBlock);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
onBlockInterval(handler, blockInterval = 250, backfillBlockInterval = 1000) {
|
63
|
+
return this.onInterval(handler, undefined, {
|
64
|
+
recentInterval: blockInterval,
|
65
|
+
backfillInterval: backfillBlockInterval,
|
66
|
+
});
|
67
|
+
}
|
68
|
+
onTimeInterval(handler, timeIntervalInMinutes = 60, backfillTimeIntervalInMinutes = 240) {
|
69
|
+
return this.onInterval(handler, { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes }, undefined);
|
70
|
+
}
|
71
|
+
getChainId() {
|
72
|
+
return Number(this.config.network.chainId);
|
73
|
+
}
|
74
|
+
onInterval(handler, timeInterval, blockInterval) {
|
75
|
+
const chainId = this.getChainId();
|
76
|
+
this.blockHandlers.push({
|
77
|
+
handler: async function (data) {
|
78
|
+
const { block } = formatEthData(data);
|
79
|
+
if (!block) {
|
80
|
+
throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty');
|
81
|
+
}
|
82
|
+
const ctx = new GlobalContext(chainId, new Date(block.timestamp * 1000), block, undefined, undefined);
|
83
|
+
await handler(block, ctx);
|
84
|
+
return ctx.getProcessResult();
|
85
|
+
},
|
86
|
+
timeIntervalInMinutes: timeInterval,
|
87
|
+
blockInterval: blockInterval,
|
88
|
+
});
|
89
|
+
return this;
|
90
|
+
}
|
91
|
+
onTransaction(handler, fetchConfig) {
|
92
|
+
const chainId = this.getChainId();
|
93
|
+
this.transactionHandler.push({
|
94
|
+
handler: async function (data) {
|
95
|
+
const { trace, block, transaction, transactionReceipt } = formatEthData(data);
|
96
|
+
if (!transaction) {
|
97
|
+
throw new ServerError(Status.INVALID_ARGUMENT, 'transaction is empty');
|
98
|
+
}
|
99
|
+
const ctx = new GlobalContext(chainId, data.timestamp, block, undefined, trace, transaction, transactionReceipt);
|
100
|
+
await handler(transaction, ctx);
|
101
|
+
return ctx.getProcessResult();
|
102
|
+
},
|
103
|
+
fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),
|
104
|
+
});
|
105
|
+
return this;
|
106
|
+
}
|
107
|
+
}
|
30
108
|
export class BaseProcessor {
|
31
109
|
blockHandlers = [];
|
32
110
|
eventHandlers = [];
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"base-processor.js","sourceRoot":"","sources":["../../src/eth/base-processor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAqB,eAAe,EAAgB,MAAM,cAAc,CAAA;AAC/E,OAAO,EAKL,cAAc,EAEd,aAAa,GACd,MAAM,gBAAgB,CAAA;AAGvB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,WAAW,EAA8B,aAAa,EAAE,MAAM,UAAU,CAAA;AACjF,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,IAAI,MAAM,SAAS,CAAA;AAO1B,MAAM,OAAO,aAAa;IACxB,OAAO,CAA4B;IACnC,OAAO,CAAgD;IACvD,WAAW,CAAgB;CAC5B;AAED,MAAM,OAAO,YAAY;IACvB,UAAU,CAAU;IACpB,OAAO,CAAkD;IACzD,WAAW,CAAgB;CAC5B;AAED,MAAM,OAAO,aAAa;IACxB,aAAa,CAAiB;IAC9B,qBAAqB,CAAiB;IACtC,OAAO,CAAkD;CAC1D;AAED,MAAM,mBAAmB;IACvB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,GAAG,IAAI,eAAe,CAC7B,YAAY,EACZ,YAAY,EACZ,OAAO,EACP,IAAI,CAAC,SAAS,EACd,KAAK,EACL,SAAS,EACT,KAAK,EACL,WAAW,EACX,kBAAkB,CACnB,CAAA;gBACD,MAAM,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAA;gBAC9B,OAAO,GAAG,CAAC,gBAAgB,EAAE,CAAA;YAC/B,CAAC;SACF,CAAC,CAAA;QACF,OAAO,IAAI,CAAA;IACb,CAAC;IAEM,WAAW,CAChB,OAAsG,EACtG,WAAqC;QAErC,MAAM,WAAW,GAAG,IAAI,CAAC,uBAAuB,EAAE,CAAA;QAClD,MAAM,SAAS,GAAG,EAAE,CAAA;QAEpB,KAAK,MAAM,QAAQ,IAAI,WAAW,CAAC,WAAW,CAAC,SAAS,CAAC,SAAS,EAAE;YAClE,IAAI,QAAQ,CAAC,IAAI,KAAK,UAAU,EAAE;gBAChC,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAA;gBACnC,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;gBAChD,MAAM,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;gBAC5D,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;aACxB;SACF;QACD,OAAO,IAAI,CAAC,OAAO,CACjB,SAAS,EACT,UAAU,KAAK,EAAE,GAAG;YAClB,OAAO,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAC5B,CAAC,EACD,WAAW,CACZ,CAAA;IACH,CAAC;CACF","sourcesContent":["import { BaseContract, DeferredTopicFilter } from 'ethers'\nimport { BlockParams, Network } from 'ethers/providers'\n\nimport { BoundContractView, ContractContext, ContractView } from './context.js'\nimport {\n AddressType,\n Data_EthBlock,\n Data_EthLog,\n Data_EthTrace,\n EthFetchConfig,\n HandleInterval,\n ProcessResult,\n} from '@sentio/protos'\nimport { BindOptions } from './bind-options.js'\nimport { PromiseOrVoid } from '../core/promises.js'\nimport { ServerError, Status } from 'nice-grpc'\nimport { fixEmptyKey, TypedEvent, TypedCallTrace, formatEthData } from './eth.js'\nimport * as console from 'console'\nimport { getNetworkFromCtxOrNetworkish } from './provider.js'\nimport sha3 from 'js-sha3'\n\nexport interface AddressOrTypeEventFilter extends DeferredTopicFilter {\n addressType?: AddressType\n address?: string\n}\n\nexport class EventsHandler {\n filters: AddressOrTypeEventFilter[]\n handler: (event: Data_EthLog) => Promise<ProcessResult>\n fetchConfig: EthFetchConfig\n}\n\nexport class TraceHandler {\n signatures: string[]\n handler: (trace: Data_EthTrace) => Promise<ProcessResult>\n fetchConfig: EthFetchConfig\n}\n\nexport class BlockHandlder {\n blockInterval?: HandleInterval\n timeIntervalInMinutes?: HandleInterval\n handler: (block: Data_EthBlock) => Promise<ProcessResult>\n}\n\nclass BindInternalOptions {\n address: string\n network: Network\n name: string\n startBlock: bigint\n endBlock?: bigint\n}\n\nexport abstract class BaseProcessor<\n TContract extends BaseContract,\n TBoundContractView extends BoundContractView<TContract, ContractView<TContract>>\n> {\n blockHandlers: BlockHandlder[] = []\n eventHandlers: EventsHandler[] = []\n traceHandlers: TraceHandler[] = []\n\n config: BindInternalOptions\n\n constructor(config: BindOptions) {\n this.config = {\n address: config.address,\n name: config.name || '',\n network: getNetworkFromCtxOrNetworkish(config.network),\n startBlock: 0n,\n }\n if (config.startBlock) {\n this.config.startBlock = BigInt(config.startBlock)\n }\n if (config.endBlock) {\n this.config.endBlock = BigInt(config.endBlock)\n }\n }\n\n protected abstract CreateBoundContractView(): TBoundContractView\n\n public getChainId(): number {\n return Number(this.config.network.chainId)\n }\n\n public onEvent(\n handler: (event: TypedEvent, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n filter: DeferredTopicFilter | DeferredTopicFilter[],\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const chainId = this.getChainId()\n let _filters: DeferredTopicFilter[] = []\n\n if (Array.isArray(filter)) {\n _filters = filter\n } else {\n _filters.push(filter)\n }\n\n const contractName = this.config.name\n const processor = this\n this.eventHandlers.push({\n filters: _filters,\n fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),\n handler: async function (data: Data_EthLog) {\n const { log, block, transaction, transactionReceipt } = formatEthData(data)\n if (!log) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Log is empty')\n }\n const contractView = processor.CreateBoundContractView()\n\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n data.timestamp,\n block,\n log,\n undefined,\n transaction,\n transactionReceipt\n )\n const logParam = log as any as { topics: Array<string>; data: string }\n\n const parsed = contractView.rawContract.interface.parseLog(logParam)\n\n if (parsed) {\n const event: TypedEvent = { ...log, name: parsed.name, args: fixEmptyKey(parsed) }\n await handler(event, ctx)\n return ctx.getProcessResult()\n }\n return ProcessResult.fromPartial({})\n },\n })\n return this\n }\n\n public onBlockInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n blockInterval = 250,\n backfillBlockInterval = 1000\n ): this {\n return this.onInterval(handler, undefined, {\n recentInterval: blockInterval,\n backfillInterval: backfillBlockInterval,\n })\n }\n\n public onTimeInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n timeIntervalInMinutes = 60,\n backfillTimeIntervalInMinutes = 240\n ): this {\n return this.onInterval(\n handler,\n { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },\n undefined\n )\n }\n\n public onInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n timeInterval: HandleInterval | undefined,\n blockInterval: HandleInterval | undefined\n ): this {\n const chainId = this.getChainId()\n const processor = this\n const contractName = this.config.name\n\n this.blockHandlers.push({\n handler: async function (data: Data_EthBlock) {\n const { block } = formatEthData(data)\n\n if (!block) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty')\n }\n\n const contractView = processor.CreateBoundContractView()\n\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n new Date(block.timestamp * 1000),\n block,\n undefined,\n undefined\n )\n await handler(block, ctx)\n return ctx.getProcessResult()\n },\n timeIntervalInMinutes: timeInterval,\n blockInterval: blockInterval,\n })\n return this\n }\n\n public onAllEvents(\n handler: (event: TypedEvent, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const _filters: DeferredTopicFilter[] = []\n const tmpContract = this.CreateBoundContractView()\n\n for (const fragment of tmpContract.rawContract.interface.fragments) {\n if (fragment.type === 'event') {\n const filter = tmpContract.rawContract.filters[fragment.format()]\n _filters.push(filter())\n }\n }\n return this.onEvent(\n function (log, ctx) {\n return handler(log, ctx)\n },\n _filters,\n fetchConfig\n )\n }\n\n public onTrace(\n signatures: string | string[],\n handler: (trace: TypedCallTrace, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const chainId = this.getChainId()\n const contractName = this.config.name\n const processor = this\n if (typeof signatures === 'string') {\n signatures = [signatures]\n }\n\n this.traceHandlers.push({\n signatures,\n fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),\n handler: async function (data: Data_EthTrace) {\n const contractView = processor.CreateBoundContractView()\n const contractInterface = contractView.rawContract.interface\n const { trace, block, transaction, transactionReceipt } = formatEthData(data)\n const sighash = trace?.action.input?.slice(0, 10)\n if (!sighash) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'trace has no sighash')\n }\n const fragment = contractInterface.getFunction(sighash)\n\n if (!trace || !fragment) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'trace is null')\n }\n const typedTrace = trace as TypedCallTrace\n typedTrace.name = fragment.name\n typedTrace.functionSignature = fragment.format()\n // const trace = data.trace as Trace\n if (!trace?.action.input) {\n return ProcessResult.fromPartial({})\n }\n const traceData = '0x' + trace.action.input.slice(10)\n try {\n typedTrace.args = contractInterface.getAbiCoder().decode(fragment.inputs, traceData, true)\n } catch (e) {\n if (!trace.error) {\n throw e\n }\n console.error('Failed to decode successful trace', e)\n }\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n data.timestamp,\n block,\n undefined,\n trace,\n transaction,\n transactionReceipt\n )\n await handler(typedTrace, ctx)\n return ctx.getProcessResult()\n },\n })\n return this\n }\n\n public onAllTraces(\n handler: (event: TypedCallTrace, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const tmpContract = this.CreateBoundContractView()\n const sighashes = []\n\n for (const fragment of tmpContract.rawContract.interface.fragments) {\n if (fragment.type === 'function') {\n const signature = fragment.format()\n const test = new TextEncoder().encode(signature)\n const sighash = '0x' + sha3.keccak_256(test).substring(0, 8)\n sighashes.push(sighash)\n }\n }\n return this.onTrace(\n sighashes,\n function (trace, ctx) {\n return handler(trace, ctx)\n },\n fetchConfig\n )\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"base-processor.js","sourceRoot":"","sources":["../../src/eth/base-processor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAqB,eAAe,EAAgB,aAAa,EAAE,MAAM,cAAc,CAAA;AAC9F,OAAO,EAML,cAAc,EAEd,aAAa,GACd,MAAM,gBAAgB,CAAA;AAGvB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,WAAW,EAA8B,aAAa,EAAE,MAAM,UAAU,CAAA;AACjF,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAA;AAC7D,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,OAAO,EAAE,gBAAgB,EAA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B,OAAsG,EACtG,WAAqC;QAErC,MAAM,WAAW,GAAG,IAAI,CAAC,uBAAuB,EAAE,CAAA;QAClD,MAAM,SAAS,GAAG,EAAE,CAAA;QAEpB,KAAK,MAAM,QAAQ,IAAI,WAAW,CAAC,WAAW,CAAC,SAAS,CAAC,SAAS,EAAE;YAClE,IAAI,QAAQ,CAAC,IAAI,KAAK,UAAU,EAAE;gBAChC,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAA;gBACnC,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;gBAChD,MAAM,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;gBAC5D,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;aACxB;SACF;QACD,OAAO,IAAI,CAAC,OAAO,CACjB,SAAS,EACT,UAAU,KAAK,EAAE,GAAG;YAClB,OAAO,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAC5B,CAAC,EACD,WAAW,CACZ,CAAA;IACH,CAAC;CACF","sourcesContent":["import { BaseContract, DeferredTopicFilter, TransactionResponseParams } from 'ethers'\nimport { BlockParams, Network } from 'ethers/providers'\n\nimport { BoundContractView, ContractContext, ContractView, GlobalContext } from './context.js'\nimport {\n AddressType,\n Data_EthBlock,\n Data_EthLog,\n Data_EthTrace,\n Data_EthTransaction,\n EthFetchConfig,\n HandleInterval,\n ProcessResult,\n} from '@sentio/protos'\nimport { BindOptions } from './bind-options.js'\nimport { PromiseOrVoid } from '../core/promises.js'\nimport { ServerError, Status } from 'nice-grpc'\nimport { fixEmptyKey, TypedEvent, TypedCallTrace, formatEthData } from './eth.js'\nimport * as console from 'console'\nimport { getNetworkFromCtxOrNetworkish } from './provider.js'\nimport sha3 from 'js-sha3'\nimport { ListStateStorage } from '@sentio/runtime'\n\nexport interface AddressOrTypeEventFilter extends DeferredTopicFilter {\n addressType?: AddressType\n address?: string\n}\n\nexport class EventsHandler {\n filters: AddressOrTypeEventFilter[]\n handler: (event: Data_EthLog) => Promise<ProcessResult>\n fetchConfig: EthFetchConfig\n}\n\nexport class TraceHandler {\n signatures: string[]\n handler: (trace: Data_EthTrace) => Promise<ProcessResult>\n fetchConfig: EthFetchConfig\n}\n\nexport class BlockHandler {\n blockInterval?: HandleInterval\n timeIntervalInMinutes?: HandleInterval\n handler: (block: Data_EthBlock) => Promise<ProcessResult>\n}\n\nexport class TransactionHandler {\n handler: (block: Data_EthTransaction) => Promise<ProcessResult>\n fetchConfig: EthFetchConfig\n}\n\nclass BindInternalOptions {\n address: string\n network: Network\n name: string\n startBlock: bigint\n endBlock?: bigint\n}\n\nexport class GlobalProcessorState extends ListStateStorage<GlobalProcessor> {\n static INSTANCE = new GlobalProcessorState()\n}\n\nexport class GlobalProcessor {\n config: BindInternalOptions\n blockHandlers: BlockHandler[] = []\n transactionHandler: TransactionHandler[] = []\n\n static bind(config: Omit<BindOptions, 'address'>): GlobalProcessor {\n const processor = new GlobalProcessor(config)\n GlobalProcessorState.INSTANCE.addValue(processor)\n return processor\n }\n\n constructor(config: Omit<BindOptions, 'address'>) {\n this.config = {\n address: '*',\n name: config.name || 'Global',\n network: getNetworkFromCtxOrNetworkish(config.network),\n startBlock: 0n,\n }\n if (config.startBlock) {\n this.config.startBlock = BigInt(config.startBlock)\n }\n if (config.endBlock) {\n this.config.endBlock = BigInt(config.endBlock)\n }\n }\n\n public onBlockInterval(\n handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,\n blockInterval = 250,\n backfillBlockInterval = 1000\n ): this {\n return this.onInterval(handler, undefined, {\n recentInterval: blockInterval,\n backfillInterval: backfillBlockInterval,\n })\n }\n\n public onTimeInterval(\n handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,\n timeIntervalInMinutes = 60,\n backfillTimeIntervalInMinutes = 240\n ): this {\n return this.onInterval(\n handler,\n { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },\n undefined\n )\n }\n\n public getChainId(): number {\n return Number(this.config.network.chainId)\n }\n\n public onInterval(\n handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,\n timeInterval: HandleInterval | undefined,\n blockInterval: HandleInterval | undefined\n ): this {\n const chainId = this.getChainId()\n\n this.blockHandlers.push({\n handler: async function (data: Data_EthBlock) {\n const { block } = formatEthData(data)\n\n if (!block) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty')\n }\n\n const ctx = new GlobalContext(chainId, new Date(block.timestamp * 1000), block, undefined, undefined)\n await handler(block, ctx)\n return ctx.getProcessResult()\n },\n timeIntervalInMinutes: timeInterval,\n blockInterval: blockInterval,\n })\n return this\n }\n\n public onTransaction(\n handler: (transaction: TransactionResponseParams, ctx: GlobalContext) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const chainId = this.getChainId()\n this.transactionHandler.push({\n handler: async function (data: Data_EthTransaction) {\n const { trace, block, transaction, transactionReceipt } = formatEthData(data)\n\n if (!transaction) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'transaction is empty')\n }\n const ctx = new GlobalContext(chainId, data.timestamp, block, undefined, trace, transaction, transactionReceipt)\n await handler(transaction, ctx)\n return ctx.getProcessResult()\n },\n fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),\n })\n return this\n }\n}\n\nexport abstract class BaseProcessor<\n TContract extends BaseContract,\n TBoundContractView extends BoundContractView<TContract, ContractView<TContract>>\n> {\n blockHandlers: BlockHandler[] = []\n eventHandlers: EventsHandler[] = []\n traceHandlers: TraceHandler[] = []\n\n config: BindInternalOptions\n\n constructor(config: BindOptions) {\n this.config = {\n address: config.address,\n name: config.name || '',\n network: getNetworkFromCtxOrNetworkish(config.network),\n startBlock: 0n,\n }\n if (config.startBlock) {\n this.config.startBlock = BigInt(config.startBlock)\n }\n if (config.endBlock) {\n this.config.endBlock = BigInt(config.endBlock)\n }\n }\n\n protected abstract CreateBoundContractView(): TBoundContractView\n\n public getChainId(): number {\n return Number(this.config.network.chainId)\n }\n\n public onEvent(\n handler: (event: TypedEvent, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n filter: DeferredTopicFilter | DeferredTopicFilter[],\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const chainId = this.getChainId()\n let _filters: DeferredTopicFilter[] = []\n\n if (Array.isArray(filter)) {\n _filters = filter\n } else {\n _filters.push(filter)\n }\n\n const contractName = this.config.name\n const processor = this\n this.eventHandlers.push({\n filters: _filters,\n fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),\n handler: async function (data: Data_EthLog) {\n const { log, block, transaction, transactionReceipt } = formatEthData(data)\n if (!log) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Log is empty')\n }\n const contractView = processor.CreateBoundContractView()\n\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n data.timestamp,\n block,\n log,\n undefined,\n transaction,\n transactionReceipt\n )\n const logParam = log as any as { topics: Array<string>; data: string }\n\n const parsed = contractView.rawContract.interface.parseLog(logParam)\n\n if (parsed) {\n const event: TypedEvent = { ...log, name: parsed.name, args: fixEmptyKey(parsed) }\n await handler(event, ctx)\n return ctx.getProcessResult()\n }\n return ProcessResult.fromPartial({})\n },\n })\n return this\n }\n\n public onBlockInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n blockInterval = 250,\n backfillBlockInterval = 1000\n ): this {\n return this.onInterval(handler, undefined, {\n recentInterval: blockInterval,\n backfillInterval: backfillBlockInterval,\n })\n }\n\n public onTimeInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n timeIntervalInMinutes = 60,\n backfillTimeIntervalInMinutes = 240\n ): this {\n return this.onInterval(\n handler,\n { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },\n undefined\n )\n }\n\n public onInterval(\n handler: (block: BlockParams, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n timeInterval: HandleInterval | undefined,\n blockInterval: HandleInterval | undefined\n ): this {\n const chainId = this.getChainId()\n const processor = this\n const contractName = this.config.name\n\n this.blockHandlers.push({\n handler: async function (data: Data_EthBlock) {\n const { block } = formatEthData(data)\n\n if (!block) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty')\n }\n\n const contractView = processor.CreateBoundContractView()\n\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n new Date(block.timestamp * 1000),\n block,\n undefined,\n undefined\n )\n await handler(block, ctx)\n return ctx.getProcessResult()\n },\n timeIntervalInMinutes: timeInterval,\n blockInterval: blockInterval,\n })\n return this\n }\n\n public onAllEvents(\n handler: (event: TypedEvent, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const _filters: DeferredTopicFilter[] = []\n const tmpContract = this.CreateBoundContractView()\n\n for (const fragment of tmpContract.rawContract.interface.fragments) {\n if (fragment.type === 'event') {\n const filter = tmpContract.rawContract.filters[fragment.format()]\n _filters.push(filter())\n }\n }\n return this.onEvent(\n function (log, ctx) {\n return handler(log, ctx)\n },\n _filters,\n fetchConfig\n )\n }\n\n public onTrace(\n signatures: string | string[],\n handler: (trace: TypedCallTrace, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const chainId = this.getChainId()\n const contractName = this.config.name\n const processor = this\n if (typeof signatures === 'string') {\n signatures = [signatures]\n }\n\n this.traceHandlers.push({\n signatures,\n fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),\n handler: async function (data: Data_EthTrace) {\n const contractView = processor.CreateBoundContractView()\n const contractInterface = contractView.rawContract.interface\n const { trace, block, transaction, transactionReceipt } = formatEthData(data)\n const sighash = trace?.action.input?.slice(0, 10)\n if (!sighash) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'trace has no sighash')\n }\n const fragment = contractInterface.getFunction(sighash)\n\n if (!trace || !fragment) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'trace is null')\n }\n const typedTrace = trace as TypedCallTrace\n typedTrace.name = fragment.name\n typedTrace.functionSignature = fragment.format()\n // const trace = data.trace as Trace\n if (!trace?.action.input) {\n return ProcessResult.fromPartial({})\n }\n const traceData = '0x' + trace.action.input.slice(10)\n try {\n typedTrace.args = contractInterface.getAbiCoder().decode(fragment.inputs, traceData, true)\n } catch (e) {\n if (!trace.error) {\n throw e\n }\n console.error('Failed to decode successful trace', e)\n }\n const ctx = new ContractContext<TContract, TBoundContractView>(\n contractName,\n contractView,\n chainId,\n data.timestamp,\n block,\n undefined,\n trace,\n transaction,\n transactionReceipt\n )\n await handler(typedTrace, ctx)\n return ctx.getProcessResult()\n },\n })\n return this\n }\n\n public onAllTraces(\n handler: (event: TypedCallTrace, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,\n fetchConfig?: Partial<EthFetchConfig>\n ): this {\n const tmpContract = this.CreateBoundContractView()\n const sighashes = []\n\n for (const fragment of tmpContract.rawContract.interface.fragments) {\n if (fragment.type === 'function') {\n const signature = fragment.format()\n const test = new TextEncoder().encode(signature)\n const sighash = '0x' + sha3.keccak_256(test).substring(0, 8)\n sighashes.push(sighash)\n }\n }\n return this.onTrace(\n sighashes,\n function (trace, ctx) {\n return handler(trace, ctx)\n },\n fetchConfig\n )\n }\n}\n"]}
|
package/lib/eth/context.d.ts
CHANGED
@@ -23,6 +23,10 @@ export declare abstract class EthContext extends BaseContext {
|
|
23
23
|
export declare class AccountContext extends EthContext {
|
24
24
|
protected getContractName(): string;
|
25
25
|
}
|
26
|
+
export declare class GlobalContext extends EthContext {
|
27
|
+
constructor(chainId: number, timestamp?: Date, block?: BlockParams, log?: LogParams, trace?: Trace, transaction?: TransactionResponseParams, transactionReceipt?: TransactionReceiptParams);
|
28
|
+
protected getContractName(): string;
|
29
|
+
}
|
26
30
|
export declare class ContractContext<TContract extends BaseContract, TContractBoundView extends BoundContractView<TContract, ContractView<TContract>>> extends EthContext {
|
27
31
|
contract: TContractBoundView;
|
28
32
|
contractName: string;
|
package/lib/eth/context.js
CHANGED
@@ -87,6 +87,14 @@ export class AccountContext extends EthContext {
|
|
87
87
|
return 'account';
|
88
88
|
}
|
89
89
|
}
|
90
|
+
export class GlobalContext extends EthContext {
|
91
|
+
constructor(chainId, timestamp, block, log, trace, transaction, transactionReceipt) {
|
92
|
+
super(chainId, '*', timestamp, block, log, trace, transaction, transactionReceipt);
|
93
|
+
}
|
94
|
+
getContractName() {
|
95
|
+
return '*';
|
96
|
+
}
|
97
|
+
}
|
90
98
|
export class ContractContext extends EthContext {
|
91
99
|
contract;
|
92
100
|
contractName;
|
package/lib/eth/context.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/eth/context.ts"],"names":[],"mappings":"AAKA,OAAO,EAAU,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAErD,MAAM,OAAgB,UAAW,SAAQ,WAAW;IAClD,OAAO,CAAQ;IACf,OAAO,CAAQ;IACE,GAAG,CAAY;IAChC,KAAK,CAAc;IACF,KAAK,CAAQ;IAC9B,WAAW,CAAiB;IAC5B,eAAe,CAAS;IACxB,WAAW,CAA4B;IACvC,kBAAkB,CAA2B;IAC7C,SAAS,CAAM;IAEf,YACE,OAAe,EACf,OAAe,EACf,SAAgB,EAChB,KAAmB,EACnB,GAAe,EACf,KAAa,EACb,WAAuC,EACvC,kBAA6C;QAE7C,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,KAAK,C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AO,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAA;IACtC,CAAC;CACF","sourcesContent":["import { BaseContract } from 'ethers'\nimport { LogParams, BlockParams, TransactionReceiptParams, TransactionResponseParams } from 'ethers/providers'\n\nimport { RecordMetaData } from '@sentio/protos'\nimport { Trace } from './eth.js'\nimport { Labels, normalizeLabels } from '../core/index.js'\nimport { BaseContext } from '../core/base-context.js'\n\nexport abstract class EthContext extends BaseContext {\n chainId: number\n address: string\n private readonly log?: LogParams\n block?: BlockParams\n private readonly trace?: Trace\n blockNumber: bigint | number\n transactionHash?: string\n transaction?: TransactionResponseParams\n transactionReceipt?: TransactionReceiptParams\n timestamp: Date\n\n constructor(\n chainId: number,\n address: string,\n timestamp?: Date,\n block?: BlockParams,\n log?: LogParams,\n trace?: Trace,\n transaction?: TransactionResponseParams,\n transactionReceipt?: TransactionReceiptParams\n ) {\n super()\n this.chainId = chainId\n this.log = log\n this.block = block\n this.trace = trace\n this.address = address.toLowerCase()\n this.transaction = transaction\n this.transactionReceipt = transactionReceipt\n this.timestamp = timestamp || new Date(0)\n if (log) {\n this.blockNumber = log.blockNumber\n this.transactionHash = log.transactionHash\n } else if (block) {\n this.blockNumber = block.number\n } else if (trace) {\n this.blockNumber = trace.blockNumber\n this.transactionHash = trace.transactionHash\n }\n }\n\n protected abstract getContractName(): string\n\n getChainId(): string {\n return this.chainId.toString()\n }\n\n getMetaData(name: string, labels: Labels): RecordMetaData {\n if (this.log) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: this.log.transactionIndex,\n transactionHash: this.transactionHash || '',\n logIndex: this.log.index,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n if (this.block) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: -1,\n transactionHash: '',\n logIndex: -1,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n if (this.trace) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: this.trace.transactionPosition,\n transactionHash: this.transactionHash || '',\n logIndex: -1,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n throw new Error(\"Invaid ctx argument can't happen\")\n }\n}\n\nexport class AccountContext extends EthContext {\n // constructor(chainId: number, address: string, block?: Block, log?: Log, trace?: Trace) {\n // super(chainId, address, new Date(0), block, log, trace)\n // }\n protected getContractName(): string {\n return 'account'\n }\n}\n\nexport class ContractContext<\n TContract extends BaseContract,\n TContractBoundView extends BoundContractView<TContract, ContractView<TContract>>\n> extends EthContext {\n contract: TContractBoundView\n contractName: string\n\n constructor(\n contractName: string,\n view: TContractBoundView,\n chainId: number,\n timestamp?: Date,\n block?: BlockParams,\n log?: LogParams,\n trace?: Trace,\n transaction?: TransactionResponseParams,\n transactionReceipt?: TransactionReceiptParams\n ) {\n super(chainId, view.address, timestamp, block, log, trace, transaction, transactionReceipt)\n view.context = this\n this.contractName = contractName\n this.contract = view\n }\n\n protected getContractName(): string {\n return this.contractName\n }\n}\n\nexport class ContractView<TContract extends BaseContract> {\n protected contract: TContract\n\n constructor(contract: TContract) {\n this.contract = contract\n }\n\n get rawContract() {\n return this.contract\n }\n\n get provider() {\n if (this.contract.runner?.provider) {\n return this.contract.runner.provider\n }\n throw Error(\"Can't find provider for contract\" + this.contract.toString())\n }\n}\n\nexport class BoundContractView<TContract extends BaseContract, TContractView extends ContractView<TContract>> {\n protected view: TContractView\n readonly address: string\n // context will be set right after context creation (in context's constructor)\n context: ContractContext<TContract, BoundContractView<TContract, TContractView>>\n\n constructor(address: string, view: TContractView) {\n this.address = address\n this.view = view\n }\n\n get rawContract() {\n return this.view.rawContract\n }\n\n get provider() {\n return this.view.provider\n }\n\n get filters() {\n return this.view.rawContract.filters\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","s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A;IACtB,CAAC;IAES,eAAe;QACvB,OAAO,IAAI,CAAC,YAAY,CAAA;IAC1B,CAAC;CACF;AAED,MAAM,OAAO,YAAY;IACb,QAAQ,CAAW;IAE7B,YAAY,QAAmB;QAC7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAED,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,IAAI,QAAQ;QACV,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE;YAClC,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAA;SACrC;QACD,MAAM,KAAK,CAAC,kCAAkC,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC5E,CAAC;CACF;AAED,MAAM,OAAO,iBAAiB;IAClB,IAAI,CAAe;IACpB,OAAO,CAAQ;IACxB,8EAA8E;IAC9E,OAAO,CAAyE;IAEhF,YAAY,OAAe,EAAE,IAAmB;QAC9C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,CAAA;IAC9B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAA;IAC3B,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAA;IACtC,CAAC;CACF","sourcesContent":["import { BaseContract } from 'ethers'\nimport { LogParams, BlockParams, TransactionReceiptParams, TransactionResponseParams } from 'ethers/providers'\n\nimport { RecordMetaData } from '@sentio/protos'\nimport { Trace } from './eth.js'\nimport { Labels, normalizeLabels } from '../core/index.js'\nimport { BaseContext } from '../core/base-context.js'\n\nexport abstract class EthContext extends BaseContext {\n chainId: number\n address: string\n private readonly log?: LogParams\n block?: BlockParams\n private readonly trace?: Trace\n blockNumber: bigint | number\n transactionHash?: string\n transaction?: TransactionResponseParams\n transactionReceipt?: TransactionReceiptParams\n timestamp: Date\n\n constructor(\n chainId: number,\n address: string,\n timestamp?: Date,\n block?: BlockParams,\n log?: LogParams,\n trace?: Trace,\n transaction?: TransactionResponseParams,\n transactionReceipt?: TransactionReceiptParams\n ) {\n super()\n this.chainId = chainId\n this.log = log\n this.block = block\n this.trace = trace\n this.address = address.toLowerCase()\n this.transaction = transaction\n this.transactionReceipt = transactionReceipt\n this.timestamp = timestamp || new Date(0)\n if (log) {\n this.blockNumber = log.blockNumber\n this.transactionHash = log.transactionHash\n } else if (block) {\n this.blockNumber = block.number\n } else if (trace) {\n this.blockNumber = trace.blockNumber\n this.transactionHash = trace.transactionHash\n }\n }\n\n protected abstract getContractName(): string\n\n getChainId(): string {\n return this.chainId.toString()\n }\n\n getMetaData(name: string, labels: Labels): RecordMetaData {\n if (this.log) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: this.log.transactionIndex,\n transactionHash: this.transactionHash || '',\n logIndex: this.log.index,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n if (this.block) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: -1,\n transactionHash: '',\n logIndex: -1,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n if (this.trace) {\n return {\n address: this.address,\n contractName: this.getContractName(),\n blockNumber: BigInt(this.blockNumber),\n transactionIndex: this.trace.transactionPosition,\n transactionHash: this.transactionHash || '',\n logIndex: -1,\n chainId: this.chainId.toString(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n throw new Error(\"Invaid ctx argument can't happen\")\n }\n}\n\nexport class AccountContext extends EthContext {\n // constructor(chainId: number, address: string, block?: Block, log?: Log, trace?: Trace) {\n // super(chainId, address, new Date(0), block, log, trace)\n // }\n protected getContractName(): string {\n return 'account'\n }\n}\n\nexport class GlobalContext extends EthContext {\n constructor(\n chainId: number,\n timestamp?: Date,\n block?: BlockParams,\n log?: LogParams,\n trace?: Trace,\n transaction?: TransactionResponseParams,\n transactionReceipt?: TransactionReceiptParams\n ) {\n super(chainId, '*', timestamp, block, log, trace, transaction, transactionReceipt)\n }\n protected getContractName(): string {\n return '*'\n }\n}\n\nexport class ContractContext<\n TContract extends BaseContract,\n TContractBoundView extends BoundContractView<TContract, ContractView<TContract>>\n> extends EthContext {\n contract: TContractBoundView\n contractName: string\n\n constructor(\n contractName: string,\n view: TContractBoundView,\n chainId: number,\n timestamp?: Date,\n block?: BlockParams,\n log?: LogParams,\n trace?: Trace,\n transaction?: TransactionResponseParams,\n transactionReceipt?: TransactionReceiptParams\n ) {\n super(chainId, view.address, timestamp, block, log, trace, transaction, transactionReceipt)\n view.context = this\n this.contractName = contractName\n this.contract = view\n }\n\n protected getContractName(): string {\n return this.contractName\n }\n}\n\nexport class ContractView<TContract extends BaseContract> {\n protected contract: TContract\n\n constructor(contract: TContract) {\n this.contract = contract\n }\n\n get rawContract() {\n return this.contract\n }\n\n get provider() {\n if (this.contract.runner?.provider) {\n return this.contract.runner.provider\n }\n throw Error(\"Can't find provider for contract\" + this.contract.toString())\n }\n}\n\nexport class BoundContractView<TContract extends BaseContract, TContractView extends ContractView<TContract>> {\n protected view: TContractView\n readonly address: string\n // context will be set right after context creation (in context's constructor)\n context: ContractContext<TContract, BoundContractView<TContract, TContractView>>\n\n constructor(address: string, view: TContractView) {\n this.address = address\n this.view = view\n }\n\n get rawContract() {\n return this.view.rawContract\n }\n\n get provider() {\n return this.view.provider\n }\n\n get filters() {\n return this.view.rawContract.filters\n }\n}\n"]}
|
package/lib/eth/eth-plugin.d.ts
CHANGED
@@ -5,6 +5,7 @@ export declare class EthPlugin extends Plugin {
|
|
5
5
|
private eventHandlers;
|
6
6
|
private traceHandlers;
|
7
7
|
private blockHandlers;
|
8
|
+
private transactionHandlers;
|
8
9
|
configure(config: ProcessConfigResponse): Promise<void>;
|
9
10
|
supportedHandlers: HandlerType[];
|
10
11
|
processBinding(request: DataBinding): Promise<ProcessResult>;
|
@@ -13,4 +14,5 @@ export declare class EthPlugin extends Plugin {
|
|
13
14
|
processLog(request: DataBinding): Promise<ProcessResult>;
|
14
15
|
processTrace(binding: DataBinding): Promise<ProcessResult>;
|
15
16
|
processBlock(binding: DataBinding): Promise<ProcessResult>;
|
17
|
+
processTransaction(binding: DataBinding): Promise<ProcessResult>;
|
16
18
|
}
|
package/lib/eth/eth-plugin.js
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
import {
|
1
|
+
import { errorString, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime';
|
2
2
|
import { AccountConfig, ContractConfig, HandlerType, } from '@sentio/protos';
|
3
3
|
import { ServerError, Status } from 'nice-grpc';
|
4
4
|
import { ProcessorState } from './binds.js';
|
5
5
|
import { AccountProcessorState } from './account-processor-state.js';
|
6
6
|
import { ProcessorTemplateProcessorState, TemplateInstanceState } from './base-processor-template.js';
|
7
|
+
import { GlobalProcessorState } from './base-processor.js';
|
7
8
|
export class EthPlugin extends Plugin {
|
8
9
|
name = 'EthPlugin';
|
9
10
|
eventHandlers = [];
|
10
11
|
traceHandlers = [];
|
11
12
|
blockHandlers = [];
|
13
|
+
transactionHandlers = [];
|
12
14
|
async configure(config) {
|
13
15
|
// This syntax is to copy values instead of using references
|
14
16
|
config.templateInstances = [...TemplateInstanceState.INSTANCE.getValues()];
|
@@ -87,6 +89,38 @@ export class EthPlugin extends Plugin {
|
|
87
89
|
// Finish up a contract
|
88
90
|
config.contractConfigs.push(contractConfig);
|
89
91
|
}
|
92
|
+
for (const processor of GlobalProcessorState.INSTANCE.getValues()) {
|
93
|
+
const chainId = processor.getChainId();
|
94
|
+
const contractConfig = ContractConfig.fromPartial({
|
95
|
+
processorType: USER_PROCESSOR,
|
96
|
+
contract: {
|
97
|
+
name: processor.config.name,
|
98
|
+
chainId: chainId.toString(),
|
99
|
+
address: processor.config.address,
|
100
|
+
abi: '',
|
101
|
+
},
|
102
|
+
startBlock: processor.config.startBlock,
|
103
|
+
endBlock: processor.config.endBlock,
|
104
|
+
});
|
105
|
+
for (const blockHandler of processor.blockHandlers) {
|
106
|
+
const handlerId = this.blockHandlers.push(blockHandler.handler) - 1;
|
107
|
+
contractConfig.intervalConfigs.push({
|
108
|
+
slot: 0,
|
109
|
+
slotInterval: blockHandler.blockInterval,
|
110
|
+
minutes: 0,
|
111
|
+
minutesInterval: blockHandler.timeIntervalInMinutes,
|
112
|
+
handlerId: handlerId,
|
113
|
+
});
|
114
|
+
}
|
115
|
+
for (const transactionHandler of processor.transactionHandler) {
|
116
|
+
const handlerId = this.transactionHandlers.push(transactionHandler.handler) - 1;
|
117
|
+
contractConfig.transactionConfig.push({
|
118
|
+
handlerId: handlerId,
|
119
|
+
fetchConfig: transactionHandler.fetchConfig,
|
120
|
+
});
|
121
|
+
}
|
122
|
+
config.contractConfigs.push(contractConfig);
|
123
|
+
}
|
90
124
|
// part 1.b prepare EVM account processors
|
91
125
|
for (const processor of AccountProcessorState.INSTANCE.getValues()) {
|
92
126
|
const accountConfig = AccountConfig.fromPartial({
|
@@ -134,7 +168,7 @@ export class EthPlugin extends Plugin {
|
|
134
168
|
config.accountConfigs.push(accountConfig);
|
135
169
|
}
|
136
170
|
}
|
137
|
-
supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE];
|
171
|
+
supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE, HandlerType.ETH_TRANSACTION];
|
138
172
|
processBinding(request) {
|
139
173
|
// return Promise.resolve(undefined);
|
140
174
|
switch (request.handlerType) {
|
@@ -144,6 +178,8 @@ export class EthPlugin extends Plugin {
|
|
144
178
|
return this.processTrace(request);
|
145
179
|
case HandlerType.ETH_BLOCK:
|
146
180
|
return this.processBlock(request);
|
181
|
+
case HandlerType.ETH_TRANSACTION:
|
182
|
+
return this.processTransaction(request);
|
147
183
|
default:
|
148
184
|
throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType);
|
149
185
|
}
|
@@ -209,6 +245,19 @@ export class EthPlugin extends Plugin {
|
|
209
245
|
}
|
210
246
|
return mergeProcessResults(await Promise.all(promises));
|
211
247
|
}
|
248
|
+
async processTransaction(binding) {
|
249
|
+
if (!binding.data?.ethTransaction?.transaction) {
|
250
|
+
throw new ServerError(Status.INVALID_ARGUMENT, "transaction can't be null");
|
251
|
+
}
|
252
|
+
const ethTransaction = binding.data.ethTransaction;
|
253
|
+
const promises = [];
|
254
|
+
for (const handlerId of binding.handlerIds) {
|
255
|
+
promises.push(this.transactionHandlers[handlerId](ethTransaction).catch((e) => {
|
256
|
+
throw new ServerError(Status.INTERNAL, 'error processing transaction: ' + JSON.stringify(ethTransaction.transaction) + '\n' + errorString(e));
|
257
|
+
}));
|
258
|
+
}
|
259
|
+
return mergeProcessResults(await Promise.all(promises));
|
260
|
+
}
|
212
261
|
}
|
213
262
|
PluginManager.INSTANCE.register(new EthPlugin());
|
214
263
|
//# sourceMappingURL=eth-plugin.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"eth-plugin.js","sourceRoot":"","sources":["../../src/eth/eth-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACzG,OAAO,EACL,aAAa,EACb,cAAc,EAKd,WAAW,GAMZ,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO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SOR } from '@sentio/runtime'\nimport {\n AccountConfig,\n ContractConfig,\n Data_EthBlock,\n Data_EthLog,\n Data_EthTrace,\n DataBinding,\n HandlerType,\n LogFilter,\n LogHandlerConfig,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest,\n} from '@sentio/protos'\n\nimport { ServerError, Status } from 'nice-grpc'\nimport { ProcessorState } from './binds.js'\nimport { AccountProcessorState } from './account-processor-state.js'\nimport { ProcessorTemplateProcessorState, TemplateInstanceState } from './base-processor-template.js'\n\nexport class EthPlugin extends Plugin {\n name: string = 'EthPlugin'\n\n private eventHandlers: ((event: Data_EthLog) => Promise<ProcessResult>)[] = []\n private traceHandlers: ((trace: Data_EthTrace) => Promise<ProcessResult>)[] = []\n private blockHandlers: ((block: Data_EthBlock) => Promise<ProcessResult>)[] = []\n\n async configure(config: ProcessConfigResponse) {\n // This syntax is to copy values instead of using references\n config.templateInstances = [...TemplateInstanceState.INSTANCE.getValues()]\n\n for (const processor of ProcessorState.INSTANCE.getValues()) {\n // If server favor incremental update this need to change\n // Start basic config for contract\n const chainId = processor.getChainId()\n // this.processorsByChainId.set(chainId, processor)\n\n const contractConfig = ContractConfig.fromPartial({\n processorType: USER_PROCESSOR,\n contract: {\n name: processor.config.name,\n chainId: chainId.toString(),\n address: processor.config.address,\n abi: '',\n },\n startBlock: processor.config.startBlock,\n endBlock: processor.config.endBlock,\n })\n\n // Step 1. Prepare all the block handlers\n for (const blockHandler of processor.blockHandlers) {\n const handlerId = this.blockHandlers.push(blockHandler.handler) - 1\n // TODO wrap the block handler into one\n\n contractConfig.intervalConfigs.push({\n slot: 0,\n slotInterval: blockHandler.blockInterval,\n minutes: 0,\n minutesInterval: blockHandler.timeIntervalInMinutes,\n handlerId: handlerId,\n })\n }\n\n // Step 2. Prepare all trace handlers\n for (const traceHandler of processor.traceHandlers) {\n const handlerId = this.traceHandlers.push(traceHandler.handler) - 1\n for (const signature of traceHandler.signatures) {\n contractConfig.traceConfigs.push({\n signature: signature,\n handlerId: handlerId,\n fetchConfig: traceHandler.fetchConfig,\n })\n }\n }\n\n // Step 3. Prepare all the event handlers\n for (const eventsHandler of processor.eventHandlers) {\n // associate id with filter\n const handlerId = this.eventHandlers.push(eventsHandler.handler) - 1\n const logConfig: LogHandlerConfig = {\n handlerId: handlerId,\n filters: [],\n fetchConfig: eventsHandler.fetchConfig,\n }\n\n for (const filter of eventsHandler.filters) {\n const topics = await filter.getTopicFilter()\n\n // if (!filter.topics) {\n // throw new ServerError(Status.INVALID_ARGUMENT, 'Topic should not be null')\n // }\n const logFilter: LogFilter = {\n addressType: undefined,\n address: contractConfig.contract?.address,\n topics: [],\n }\n\n for (const ts of topics) {\n let hashes: string[] = []\n if (Array.isArray(ts)) {\n hashes = hashes.concat(ts)\n } else if (ts) {\n hashes.push(ts)\n }\n logFilter.topics.push({ hashes: hashes })\n }\n logConfig.filters.push(logFilter)\n }\n contractConfig.logConfigs.push(logConfig)\n }\n\n // Finish up a contract\n config.contractConfigs.push(contractConfig)\n }\n\n // part 1.b prepare EVM account processors\n for (const processor of AccountProcessorState.INSTANCE.getValues()) {\n const accountConfig = AccountConfig.fromPartial({\n address: processor.config.address,\n chainId: processor.getChainId().toString(),\n startBlock: processor.config.startBlock ? BigInt(processor.config.startBlock) : 0n,\n })\n // TODO add interval\n for (const eventsHandler of processor.eventHandlers) {\n // associate id with filter\n const handlerId = this.eventHandlers.push(eventsHandler.handler) - 1\n const logConfig: LogHandlerConfig = {\n handlerId: handlerId,\n filters: [],\n fetchConfig: eventsHandler.fetchConfig,\n }\n\n for (const filter of eventsHandler.filters) {\n const topics = await filter.getTopicFilter()\n // if (!filter.topics) {\n // throw new ServerError(Status.INVALID_ARGUMENT, 'Topic should not be null')\n // }\n let address = undefined\n if (filter.address) {\n address = filter.address.toString()\n }\n const logFilter: LogFilter = {\n addressType: filter.addressType,\n address,\n topics: [],\n }\n\n for (const ts of topics) {\n let hashes: string[] = []\n if (Array.isArray(ts)) {\n hashes = hashes.concat(ts)\n } else if (ts) {\n hashes.push(ts)\n }\n logFilter.topics.push({ hashes: hashes })\n }\n logConfig.filters.push(logFilter)\n }\n accountConfig.logConfigs.push(logConfig)\n }\n\n config.accountConfigs.push(accountConfig)\n }\n }\n\n supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE]\n\n processBinding(request: DataBinding): Promise<ProcessResult> {\n // return Promise.resolve(undefined);\n switch (request.handlerType) {\n case HandlerType.ETH_LOG:\n return this.processLog(request)\n case HandlerType.ETH_TRACE:\n return this.processTrace(request)\n case HandlerType.ETH_BLOCK:\n return this.processBlock(request)\n default:\n throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)\n }\n }\n\n async start(request: StartRequest) {\n for (const instance of request.templateInstances) {\n const template = ProcessorTemplateProcessorState.INSTANCE.getValues()[instance.templateId]\n if (!template) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid template contract:' + instance)\n }\n if (!instance.contract) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Contract Empty from:' + instance)\n }\n template.bind({\n name: instance.contract.name,\n address: instance.contract.address,\n network: Number(instance.contract.chainId),\n startBlock: instance.startBlock,\n endBlock: instance.endBlock,\n })\n }\n }\n\n stateDiff(config: ProcessConfigResponse): boolean {\n return TemplateInstanceState.INSTANCE.getValues().length !== config.templateInstances.length\n }\n\n async processLog(request: DataBinding): Promise<ProcessResult> {\n if (!request.data?.ethLog?.log) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Log can't be null\")\n }\n const ethLog = request.data.ethLog\n\n const promises: Promise<ProcessResult>[] = []\n for (const handlerId of request.handlerIds) {\n const handler = this.eventHandlers[handlerId]\n promises.push(\n handler(ethLog).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing log: ' + JSON.stringify(ethLog.log) + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n\n async processTrace(binding: DataBinding): Promise<ProcessResult> {\n if (!binding.data?.ethTrace?.trace) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Trace can't be null\")\n }\n const ethTrace = binding.data.ethTrace\n\n const promises: Promise<ProcessResult>[] = []\n\n for (const handlerId of binding.handlerIds) {\n promises.push(\n this.traceHandlers[handlerId](ethTrace).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing trace: ' + JSON.stringify(ethTrace.trace) + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n\n async processBlock(binding: DataBinding): Promise<ProcessResult> {\n if (!binding.data?.ethBlock?.block) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Block can't be empty\")\n }\n const ethBlock = binding.data.ethBlock\n\n const promises: Promise<ProcessResult>[] = []\n for (const handlerId of binding.handlerIds) {\n promises.push(\n this.blockHandlers[handlerId](ethBlock).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing block: ' + ethBlock.block?.number + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n}\n\nPluginManager.INSTANCE.register(new EthPlugin())\n"]}
|
1
|
+
{"version":3,"file":"eth-plugin.js","sourceRoot":"","sources":["../../src/eth/eth-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACzG,OAAO,EACL,aAAa,EACb,cAAc,EAMd,WAAW,GAMZ,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,+BAA+B,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACrG,OAAO,EAAE,oBAAoB,EAAE,MAAM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IACzD,CAAC;IAED,KAAK,CAAC,kBAAkB,CAAC,OAAoB;QAC3C,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,cAAc,EAAE,WAAW,EAAE;YAC9C,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,gBAAgB,EAAE,2BAA2B,CAAC,CAAA;SAC5E;QACD,MAAM,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,cAAc,CAAA;QAElD,MAAM,QAAQ,GAA6B,EAAE,CAAA;QAE7C,KAAK,MAAM,SAAS,IAAI,OAAO,CAAC,UAAU,EAAE;YAC1C,QAAQ,CAAC,IAAI,CACX,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;gBAC9D,MAAM,IAAI,WAAW,CACnB,MAAM,CAAC,QAAQ,EACf,gCAAgC,GAAG,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,CACtG,CAAA;YACH,CAAC,CAAC,CACH,CAAA;SACF;QACD,OAAO,mBAAmB,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAA;IACzD,CAAC;CACF;AAED,aAAa,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,SAAS,EAAE,CAAC,CAAA","sourcesContent":["import { errorString, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'\nimport {\n AccountConfig,\n ContractConfig,\n Data_EthBlock,\n Data_EthLog,\n Data_EthTrace,\n Data_EthTransaction,\n DataBinding,\n HandlerType,\n LogFilter,\n LogHandlerConfig,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest,\n} from '@sentio/protos'\n\nimport { ServerError, Status } from 'nice-grpc'\nimport { ProcessorState } from './binds.js'\nimport { AccountProcessorState } from './account-processor-state.js'\nimport { ProcessorTemplateProcessorState, TemplateInstanceState } from './base-processor-template.js'\nimport { GlobalProcessorState } from './base-processor.js'\n\nexport class EthPlugin extends Plugin {\n name: string = 'EthPlugin'\n\n private eventHandlers: ((event: Data_EthLog) => Promise<ProcessResult>)[] = []\n private traceHandlers: ((trace: Data_EthTrace) => Promise<ProcessResult>)[] = []\n private blockHandlers: ((block: Data_EthBlock) => Promise<ProcessResult>)[] = []\n private transactionHandlers: ((trace: Data_EthTransaction) => Promise<ProcessResult>)[] = []\n\n async configure(config: ProcessConfigResponse) {\n // This syntax is to copy values instead of using references\n config.templateInstances = [...TemplateInstanceState.INSTANCE.getValues()]\n\n for (const processor of ProcessorState.INSTANCE.getValues()) {\n // If server favor incremental update this need to change\n // Start basic config for contract\n const chainId = processor.getChainId()\n // this.processorsByChainId.set(chainId, processor)\n\n const contractConfig = ContractConfig.fromPartial({\n processorType: USER_PROCESSOR,\n contract: {\n name: processor.config.name,\n chainId: chainId.toString(),\n address: processor.config.address,\n abi: '',\n },\n startBlock: processor.config.startBlock,\n endBlock: processor.config.endBlock,\n })\n\n // Step 1. Prepare all the block handlers\n for (const blockHandler of processor.blockHandlers) {\n const handlerId = this.blockHandlers.push(blockHandler.handler) - 1\n // TODO wrap the block handler into one\n\n contractConfig.intervalConfigs.push({\n slot: 0,\n slotInterval: blockHandler.blockInterval,\n minutes: 0,\n minutesInterval: blockHandler.timeIntervalInMinutes,\n handlerId: handlerId,\n })\n }\n\n // Step 2. Prepare all trace handlers\n for (const traceHandler of processor.traceHandlers) {\n const handlerId = this.traceHandlers.push(traceHandler.handler) - 1\n for (const signature of traceHandler.signatures) {\n contractConfig.traceConfigs.push({\n signature: signature,\n handlerId: handlerId,\n fetchConfig: traceHandler.fetchConfig,\n })\n }\n }\n\n // Step 3. Prepare all the event handlers\n for (const eventsHandler of processor.eventHandlers) {\n // associate id with filter\n const handlerId = this.eventHandlers.push(eventsHandler.handler) - 1\n const logConfig: LogHandlerConfig = {\n handlerId: handlerId,\n filters: [],\n fetchConfig: eventsHandler.fetchConfig,\n }\n\n for (const filter of eventsHandler.filters) {\n const topics = await filter.getTopicFilter()\n\n // if (!filter.topics) {\n // throw new ServerError(Status.INVALID_ARGUMENT, 'Topic should not be null')\n // }\n const logFilter: LogFilter = {\n addressType: undefined,\n address: contractConfig.contract?.address,\n topics: [],\n }\n\n for (const ts of topics) {\n let hashes: string[] = []\n if (Array.isArray(ts)) {\n hashes = hashes.concat(ts)\n } else if (ts) {\n hashes.push(ts)\n }\n logFilter.topics.push({ hashes: hashes })\n }\n logConfig.filters.push(logFilter)\n }\n contractConfig.logConfigs.push(logConfig)\n }\n\n // Finish up a contract\n config.contractConfigs.push(contractConfig)\n }\n\n for (const processor of GlobalProcessorState.INSTANCE.getValues()) {\n const chainId = processor.getChainId()\n\n const contractConfig = ContractConfig.fromPartial({\n processorType: USER_PROCESSOR,\n contract: {\n name: processor.config.name,\n chainId: chainId.toString(),\n address: processor.config.address,\n abi: '',\n },\n startBlock: processor.config.startBlock,\n endBlock: processor.config.endBlock,\n })\n\n for (const blockHandler of processor.blockHandlers) {\n const handlerId = this.blockHandlers.push(blockHandler.handler) - 1\n contractConfig.intervalConfigs.push({\n slot: 0,\n slotInterval: blockHandler.blockInterval,\n minutes: 0,\n minutesInterval: blockHandler.timeIntervalInMinutes,\n handlerId: handlerId,\n })\n }\n\n for (const transactionHandler of processor.transactionHandler) {\n const handlerId = this.transactionHandlers.push(transactionHandler.handler) - 1\n contractConfig.transactionConfig.push({\n handlerId: handlerId,\n fetchConfig: transactionHandler.fetchConfig,\n })\n }\n config.contractConfigs.push(contractConfig)\n }\n\n // part 1.b prepare EVM account processors\n for (const processor of AccountProcessorState.INSTANCE.getValues()) {\n const accountConfig = AccountConfig.fromPartial({\n address: processor.config.address,\n chainId: processor.getChainId().toString(),\n startBlock: processor.config.startBlock ? BigInt(processor.config.startBlock) : 0n,\n })\n // TODO add interval\n for (const eventsHandler of processor.eventHandlers) {\n // associate id with filter\n const handlerId = this.eventHandlers.push(eventsHandler.handler) - 1\n const logConfig: LogHandlerConfig = {\n handlerId: handlerId,\n filters: [],\n fetchConfig: eventsHandler.fetchConfig,\n }\n\n for (const filter of eventsHandler.filters) {\n const topics = await filter.getTopicFilter()\n // if (!filter.topics) {\n // throw new ServerError(Status.INVALID_ARGUMENT, 'Topic should not be null')\n // }\n let address = undefined\n if (filter.address) {\n address = filter.address.toString()\n }\n const logFilter: LogFilter = {\n addressType: filter.addressType,\n address,\n topics: [],\n }\n\n for (const ts of topics) {\n let hashes: string[] = []\n if (Array.isArray(ts)) {\n hashes = hashes.concat(ts)\n } else if (ts) {\n hashes.push(ts)\n }\n logFilter.topics.push({ hashes: hashes })\n }\n logConfig.filters.push(logFilter)\n }\n accountConfig.logConfigs.push(logConfig)\n }\n\n config.accountConfigs.push(accountConfig)\n }\n }\n\n supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE, HandlerType.ETH_TRANSACTION]\n\n processBinding(request: DataBinding): Promise<ProcessResult> {\n // return Promise.resolve(undefined);\n switch (request.handlerType) {\n case HandlerType.ETH_LOG:\n return this.processLog(request)\n case HandlerType.ETH_TRACE:\n return this.processTrace(request)\n case HandlerType.ETH_BLOCK:\n return this.processBlock(request)\n case HandlerType.ETH_TRANSACTION:\n return this.processTransaction(request)\n default:\n throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)\n }\n }\n\n async start(request: StartRequest) {\n for (const instance of request.templateInstances) {\n const template = ProcessorTemplateProcessorState.INSTANCE.getValues()[instance.templateId]\n if (!template) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid template contract:' + instance)\n }\n if (!instance.contract) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Contract Empty from:' + instance)\n }\n template.bind({\n name: instance.contract.name,\n address: instance.contract.address,\n network: Number(instance.contract.chainId),\n startBlock: instance.startBlock,\n endBlock: instance.endBlock,\n })\n }\n }\n\n stateDiff(config: ProcessConfigResponse): boolean {\n return TemplateInstanceState.INSTANCE.getValues().length !== config.templateInstances.length\n }\n\n async processLog(request: DataBinding): Promise<ProcessResult> {\n if (!request.data?.ethLog?.log) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Log can't be null\")\n }\n const ethLog = request.data.ethLog\n\n const promises: Promise<ProcessResult>[] = []\n for (const handlerId of request.handlerIds) {\n const handler = this.eventHandlers[handlerId]\n promises.push(\n handler(ethLog).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing log: ' + JSON.stringify(ethLog.log) + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n\n async processTrace(binding: DataBinding): Promise<ProcessResult> {\n if (!binding.data?.ethTrace?.trace) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Trace can't be null\")\n }\n const ethTrace = binding.data.ethTrace\n\n const promises: Promise<ProcessResult>[] = []\n\n for (const handlerId of binding.handlerIds) {\n promises.push(\n this.traceHandlers[handlerId](ethTrace).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing trace: ' + JSON.stringify(ethTrace.trace) + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n\n async processBlock(binding: DataBinding): Promise<ProcessResult> {\n if (!binding.data?.ethBlock?.block) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"Block can't be empty\")\n }\n const ethBlock = binding.data.ethBlock\n\n const promises: Promise<ProcessResult>[] = []\n for (const handlerId of binding.handlerIds) {\n promises.push(\n this.blockHandlers[handlerId](ethBlock).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing block: ' + ethBlock.block?.number + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n\n async processTransaction(binding: DataBinding): Promise<ProcessResult> {\n if (!binding.data?.ethTransaction?.transaction) {\n throw new ServerError(Status.INVALID_ARGUMENT, \"transaction can't be null\")\n }\n const ethTransaction = binding.data.ethTransaction\n\n const promises: Promise<ProcessResult>[] = []\n\n for (const handlerId of binding.handlerIds) {\n promises.push(\n this.transactionHandlers[handlerId](ethTransaction).catch((e) => {\n throw new ServerError(\n Status.INTERNAL,\n 'error processing transaction: ' + JSON.stringify(ethTransaction.transaction) + '\\n' + errorString(e)\n )\n })\n )\n }\n return mergeProcessResults(await Promise.all(promises))\n }\n}\n\nPluginManager.INSTANCE.register(new EthPlugin())\n"]}
|
package/lib/eth/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export { BaseProcessor } from './base-processor.js';
|
1
|
+
export { BaseProcessor, GlobalProcessor } from './base-processor.js';
|
2
2
|
export { GenericProcessor } from './generic-processor.js';
|
3
3
|
export { BaseProcessorTemplate } from './base-processor-template.js';
|
4
4
|
export { AccountProcessor } from './account-processor.js';
|
@@ -6,5 +6,5 @@ export { getProvider, DummyProvider, getNetworkFromCtxOrNetworkish } from './pro
|
|
6
6
|
export * from './eth.js';
|
7
7
|
export { BindOptions, AccountBindOptions } from './bind-options.js';
|
8
8
|
export { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds.js';
|
9
|
-
export { ContractContext, ContractView, BoundContractView } from './context.js';
|
9
|
+
export { ContractContext, GlobalContext, ContractView, BoundContractView } from './context.js';
|
10
10
|
export { EthPlugin } from './eth-plugin.js';
|
package/lib/eth/index.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export { BaseProcessor } from './base-processor.js';
|
1
|
+
export { BaseProcessor, GlobalProcessor } from './base-processor.js';
|
2
2
|
export { GenericProcessor } from './generic-processor.js';
|
3
3
|
export { BaseProcessorTemplate } from './base-processor-template.js';
|
4
4
|
export { AccountProcessor } from './account-processor.js';
|
@@ -6,6 +6,6 @@ export { getProvider, DummyProvider, getNetworkFromCtxOrNetworkish } from './pro
|
|
6
6
|
export * from './eth.js';
|
7
7
|
export { BindOptions, AccountBindOptions } from './bind-options.js';
|
8
8
|
export { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds.js';
|
9
|
-
export { ContractContext, ContractView, BoundContractView } from './context.js';
|
9
|
+
export { ContractContext, GlobalContext, ContractView, BoundContractView } from './context.js';
|
10
10
|
export { EthPlugin } from './eth-plugin.js';
|
11
11
|
//# sourceMappingURL=index.js.map
|
package/lib/eth/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/eth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAA;AACzF,cAAc,UAAU,CAAA;AACxB,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC3F,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAE9F,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA","sourcesContent":["export { BaseProcessor, GlobalProcessor } from './base-processor.js'\nexport { GenericProcessor } from './generic-processor.js'\nexport { BaseProcessorTemplate } from './base-processor-template.js'\nexport { AccountProcessor } from './account-processor.js'\nexport { getProvider, DummyProvider, getNetworkFromCtxOrNetworkish } from './provider.js'\nexport * from './eth.js'\nexport { BindOptions, AccountBindOptions } from './bind-options.js'\nexport { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds.js'\nexport { ContractContext, GlobalContext, ContractView, BoundContractView } from './context.js'\n\nexport { EthPlugin } from './eth-plugin.js'\n"]}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@sentio/sdk",
|
3
3
|
"license": "Apache-2.0",
|
4
|
-
"version": "2.
|
4
|
+
"version": "2.12.0-rc.1",
|
5
5
|
"type": "module",
|
6
6
|
"scripts": {
|
7
7
|
"compile": "tsc && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
|
@@ -23,8 +23,8 @@
|
|
23
23
|
"@project-serum/anchor": "^0.26.0",
|
24
24
|
"@sentio/bigdecimal": "^9.1.1-patch.3",
|
25
25
|
"@sentio/ethers-v6": "^1.0.28",
|
26
|
-
"@sentio/protos": "^2.
|
27
|
-
"@sentio/runtime": "^2.
|
26
|
+
"@sentio/protos": "^2.12.0-rc.1",
|
27
|
+
"@sentio/runtime": "^2.12.0-rc.1",
|
28
28
|
"@solana/web3.js": "^1.74.0",
|
29
29
|
"@types/prettier": "^2.7.2",
|
30
30
|
"aptos-sdk": "npm:aptos@^1.7.2",
|
@@ -88,5 +88,5 @@
|
|
88
88
|
"engines": {
|
89
89
|
"node": ">=16"
|
90
90
|
},
|
91
|
-
"gitHead": "
|
91
|
+
"gitHead": "9a36d4022c372e3d51d660f94c5f53edf72b79d1"
|
92
92
|
}
|
@@ -1,12 +1,13 @@
|
|
1
|
-
import { BaseContract, DeferredTopicFilter } from 'ethers'
|
1
|
+
import { BaseContract, DeferredTopicFilter, TransactionResponseParams } from 'ethers'
|
2
2
|
import { BlockParams, Network } from 'ethers/providers'
|
3
3
|
|
4
|
-
import { BoundContractView, ContractContext, ContractView } from './context.js'
|
4
|
+
import { BoundContractView, ContractContext, ContractView, GlobalContext } from './context.js'
|
5
5
|
import {
|
6
6
|
AddressType,
|
7
7
|
Data_EthBlock,
|
8
8
|
Data_EthLog,
|
9
9
|
Data_EthTrace,
|
10
|
+
Data_EthTransaction,
|
10
11
|
EthFetchConfig,
|
11
12
|
HandleInterval,
|
12
13
|
ProcessResult,
|
@@ -18,6 +19,7 @@ import { fixEmptyKey, TypedEvent, TypedCallTrace, formatEthData } from './eth.js
|
|
18
19
|
import * as console from 'console'
|
19
20
|
import { getNetworkFromCtxOrNetworkish } from './provider.js'
|
20
21
|
import sha3 from 'js-sha3'
|
22
|
+
import { ListStateStorage } from '@sentio/runtime'
|
21
23
|
|
22
24
|
export interface AddressOrTypeEventFilter extends DeferredTopicFilter {
|
23
25
|
addressType?: AddressType
|
@@ -36,12 +38,17 @@ export class TraceHandler {
|
|
36
38
|
fetchConfig: EthFetchConfig
|
37
39
|
}
|
38
40
|
|
39
|
-
export class
|
41
|
+
export class BlockHandler {
|
40
42
|
blockInterval?: HandleInterval
|
41
43
|
timeIntervalInMinutes?: HandleInterval
|
42
44
|
handler: (block: Data_EthBlock) => Promise<ProcessResult>
|
43
45
|
}
|
44
46
|
|
47
|
+
export class TransactionHandler {
|
48
|
+
handler: (block: Data_EthTransaction) => Promise<ProcessResult>
|
49
|
+
fetchConfig: EthFetchConfig
|
50
|
+
}
|
51
|
+
|
45
52
|
class BindInternalOptions {
|
46
53
|
address: string
|
47
54
|
network: Network
|
@@ -50,11 +57,115 @@ class BindInternalOptions {
|
|
50
57
|
endBlock?: bigint
|
51
58
|
}
|
52
59
|
|
60
|
+
export class GlobalProcessorState extends ListStateStorage<GlobalProcessor> {
|
61
|
+
static INSTANCE = new GlobalProcessorState()
|
62
|
+
}
|
63
|
+
|
64
|
+
export class GlobalProcessor {
|
65
|
+
config: BindInternalOptions
|
66
|
+
blockHandlers: BlockHandler[] = []
|
67
|
+
transactionHandler: TransactionHandler[] = []
|
68
|
+
|
69
|
+
static bind(config: Omit<BindOptions, 'address'>): GlobalProcessor {
|
70
|
+
const processor = new GlobalProcessor(config)
|
71
|
+
GlobalProcessorState.INSTANCE.addValue(processor)
|
72
|
+
return processor
|
73
|
+
}
|
74
|
+
|
75
|
+
constructor(config: Omit<BindOptions, 'address'>) {
|
76
|
+
this.config = {
|
77
|
+
address: '*',
|
78
|
+
name: config.name || 'Global',
|
79
|
+
network: getNetworkFromCtxOrNetworkish(config.network),
|
80
|
+
startBlock: 0n,
|
81
|
+
}
|
82
|
+
if (config.startBlock) {
|
83
|
+
this.config.startBlock = BigInt(config.startBlock)
|
84
|
+
}
|
85
|
+
if (config.endBlock) {
|
86
|
+
this.config.endBlock = BigInt(config.endBlock)
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
public onBlockInterval(
|
91
|
+
handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,
|
92
|
+
blockInterval = 250,
|
93
|
+
backfillBlockInterval = 1000
|
94
|
+
): this {
|
95
|
+
return this.onInterval(handler, undefined, {
|
96
|
+
recentInterval: blockInterval,
|
97
|
+
backfillInterval: backfillBlockInterval,
|
98
|
+
})
|
99
|
+
}
|
100
|
+
|
101
|
+
public onTimeInterval(
|
102
|
+
handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,
|
103
|
+
timeIntervalInMinutes = 60,
|
104
|
+
backfillTimeIntervalInMinutes = 240
|
105
|
+
): this {
|
106
|
+
return this.onInterval(
|
107
|
+
handler,
|
108
|
+
{ recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },
|
109
|
+
undefined
|
110
|
+
)
|
111
|
+
}
|
112
|
+
|
113
|
+
public getChainId(): number {
|
114
|
+
return Number(this.config.network.chainId)
|
115
|
+
}
|
116
|
+
|
117
|
+
public onInterval(
|
118
|
+
handler: (block: BlockParams, ctx: GlobalContext) => PromiseOrVoid,
|
119
|
+
timeInterval: HandleInterval | undefined,
|
120
|
+
blockInterval: HandleInterval | undefined
|
121
|
+
): this {
|
122
|
+
const chainId = this.getChainId()
|
123
|
+
|
124
|
+
this.blockHandlers.push({
|
125
|
+
handler: async function (data: Data_EthBlock) {
|
126
|
+
const { block } = formatEthData(data)
|
127
|
+
|
128
|
+
if (!block) {
|
129
|
+
throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty')
|
130
|
+
}
|
131
|
+
|
132
|
+
const ctx = new GlobalContext(chainId, new Date(block.timestamp * 1000), block, undefined, undefined)
|
133
|
+
await handler(block, ctx)
|
134
|
+
return ctx.getProcessResult()
|
135
|
+
},
|
136
|
+
timeIntervalInMinutes: timeInterval,
|
137
|
+
blockInterval: blockInterval,
|
138
|
+
})
|
139
|
+
return this
|
140
|
+
}
|
141
|
+
|
142
|
+
public onTransaction(
|
143
|
+
handler: (transaction: TransactionResponseParams, ctx: GlobalContext) => PromiseOrVoid,
|
144
|
+
fetchConfig?: Partial<EthFetchConfig>
|
145
|
+
): this {
|
146
|
+
const chainId = this.getChainId()
|
147
|
+
this.transactionHandler.push({
|
148
|
+
handler: async function (data: Data_EthTransaction) {
|
149
|
+
const { trace, block, transaction, transactionReceipt } = formatEthData(data)
|
150
|
+
|
151
|
+
if (!transaction) {
|
152
|
+
throw new ServerError(Status.INVALID_ARGUMENT, 'transaction is empty')
|
153
|
+
}
|
154
|
+
const ctx = new GlobalContext(chainId, data.timestamp, block, undefined, trace, transaction, transactionReceipt)
|
155
|
+
await handler(transaction, ctx)
|
156
|
+
return ctx.getProcessResult()
|
157
|
+
},
|
158
|
+
fetchConfig: EthFetchConfig.fromPartial(fetchConfig || {}),
|
159
|
+
})
|
160
|
+
return this
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
53
164
|
export abstract class BaseProcessor<
|
54
165
|
TContract extends BaseContract,
|
55
166
|
TBoundContractView extends BoundContractView<TContract, ContractView<TContract>>
|
56
167
|
> {
|
57
|
-
blockHandlers:
|
168
|
+
blockHandlers: BlockHandler[] = []
|
58
169
|
eventHandlers: EventsHandler[] = []
|
59
170
|
traceHandlers: TraceHandler[] = []
|
60
171
|
|
package/src/eth/context.ts
CHANGED
@@ -107,6 +107,23 @@ export class AccountContext extends EthContext {
|
|
107
107
|
}
|
108
108
|
}
|
109
109
|
|
110
|
+
export class GlobalContext extends EthContext {
|
111
|
+
constructor(
|
112
|
+
chainId: number,
|
113
|
+
timestamp?: Date,
|
114
|
+
block?: BlockParams,
|
115
|
+
log?: LogParams,
|
116
|
+
trace?: Trace,
|
117
|
+
transaction?: TransactionResponseParams,
|
118
|
+
transactionReceipt?: TransactionReceiptParams
|
119
|
+
) {
|
120
|
+
super(chainId, '*', timestamp, block, log, trace, transaction, transactionReceipt)
|
121
|
+
}
|
122
|
+
protected getContractName(): string {
|
123
|
+
return '*'
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
110
127
|
export class ContractContext<
|
111
128
|
TContract extends BaseContract,
|
112
129
|
TContractBoundView extends BoundContractView<TContract, ContractView<TContract>>
|
package/src/eth/eth-plugin.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
import {
|
1
|
+
import { errorString, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'
|
2
2
|
import {
|
3
3
|
AccountConfig,
|
4
4
|
ContractConfig,
|
5
5
|
Data_EthBlock,
|
6
6
|
Data_EthLog,
|
7
7
|
Data_EthTrace,
|
8
|
+
Data_EthTransaction,
|
8
9
|
DataBinding,
|
9
10
|
HandlerType,
|
10
11
|
LogFilter,
|
@@ -18,6 +19,7 @@ import { ServerError, Status } from 'nice-grpc'
|
|
18
19
|
import { ProcessorState } from './binds.js'
|
19
20
|
import { AccountProcessorState } from './account-processor-state.js'
|
20
21
|
import { ProcessorTemplateProcessorState, TemplateInstanceState } from './base-processor-template.js'
|
22
|
+
import { GlobalProcessorState } from './base-processor.js'
|
21
23
|
|
22
24
|
export class EthPlugin extends Plugin {
|
23
25
|
name: string = 'EthPlugin'
|
@@ -25,6 +27,7 @@ export class EthPlugin extends Plugin {
|
|
25
27
|
private eventHandlers: ((event: Data_EthLog) => Promise<ProcessResult>)[] = []
|
26
28
|
private traceHandlers: ((trace: Data_EthTrace) => Promise<ProcessResult>)[] = []
|
27
29
|
private blockHandlers: ((block: Data_EthBlock) => Promise<ProcessResult>)[] = []
|
30
|
+
private transactionHandlers: ((trace: Data_EthTransaction) => Promise<ProcessResult>)[] = []
|
28
31
|
|
29
32
|
async configure(config: ProcessConfigResponse) {
|
30
33
|
// This syntax is to copy values instead of using references
|
@@ -114,6 +117,42 @@ export class EthPlugin extends Plugin {
|
|
114
117
|
config.contractConfigs.push(contractConfig)
|
115
118
|
}
|
116
119
|
|
120
|
+
for (const processor of GlobalProcessorState.INSTANCE.getValues()) {
|
121
|
+
const chainId = processor.getChainId()
|
122
|
+
|
123
|
+
const contractConfig = ContractConfig.fromPartial({
|
124
|
+
processorType: USER_PROCESSOR,
|
125
|
+
contract: {
|
126
|
+
name: processor.config.name,
|
127
|
+
chainId: chainId.toString(),
|
128
|
+
address: processor.config.address,
|
129
|
+
abi: '',
|
130
|
+
},
|
131
|
+
startBlock: processor.config.startBlock,
|
132
|
+
endBlock: processor.config.endBlock,
|
133
|
+
})
|
134
|
+
|
135
|
+
for (const blockHandler of processor.blockHandlers) {
|
136
|
+
const handlerId = this.blockHandlers.push(blockHandler.handler) - 1
|
137
|
+
contractConfig.intervalConfigs.push({
|
138
|
+
slot: 0,
|
139
|
+
slotInterval: blockHandler.blockInterval,
|
140
|
+
minutes: 0,
|
141
|
+
minutesInterval: blockHandler.timeIntervalInMinutes,
|
142
|
+
handlerId: handlerId,
|
143
|
+
})
|
144
|
+
}
|
145
|
+
|
146
|
+
for (const transactionHandler of processor.transactionHandler) {
|
147
|
+
const handlerId = this.transactionHandlers.push(transactionHandler.handler) - 1
|
148
|
+
contractConfig.transactionConfig.push({
|
149
|
+
handlerId: handlerId,
|
150
|
+
fetchConfig: transactionHandler.fetchConfig,
|
151
|
+
})
|
152
|
+
}
|
153
|
+
config.contractConfigs.push(contractConfig)
|
154
|
+
}
|
155
|
+
|
117
156
|
// part 1.b prepare EVM account processors
|
118
157
|
for (const processor of AccountProcessorState.INSTANCE.getValues()) {
|
119
158
|
const accountConfig = AccountConfig.fromPartial({
|
@@ -164,7 +203,7 @@ export class EthPlugin extends Plugin {
|
|
164
203
|
}
|
165
204
|
}
|
166
205
|
|
167
|
-
supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE]
|
206
|
+
supportedHandlers = [HandlerType.ETH_LOG, HandlerType.ETH_BLOCK, HandlerType.ETH_TRACE, HandlerType.ETH_TRANSACTION]
|
168
207
|
|
169
208
|
processBinding(request: DataBinding): Promise<ProcessResult> {
|
170
209
|
// return Promise.resolve(undefined);
|
@@ -175,6 +214,8 @@ export class EthPlugin extends Plugin {
|
|
175
214
|
return this.processTrace(request)
|
176
215
|
case HandlerType.ETH_BLOCK:
|
177
216
|
return this.processBlock(request)
|
217
|
+
case HandlerType.ETH_TRANSACTION:
|
218
|
+
return this.processTransaction(request)
|
178
219
|
default:
|
179
220
|
throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
|
180
221
|
}
|
@@ -264,6 +305,27 @@ export class EthPlugin extends Plugin {
|
|
264
305
|
}
|
265
306
|
return mergeProcessResults(await Promise.all(promises))
|
266
307
|
}
|
308
|
+
|
309
|
+
async processTransaction(binding: DataBinding): Promise<ProcessResult> {
|
310
|
+
if (!binding.data?.ethTransaction?.transaction) {
|
311
|
+
throw new ServerError(Status.INVALID_ARGUMENT, "transaction can't be null")
|
312
|
+
}
|
313
|
+
const ethTransaction = binding.data.ethTransaction
|
314
|
+
|
315
|
+
const promises: Promise<ProcessResult>[] = []
|
316
|
+
|
317
|
+
for (const handlerId of binding.handlerIds) {
|
318
|
+
promises.push(
|
319
|
+
this.transactionHandlers[handlerId](ethTransaction).catch((e) => {
|
320
|
+
throw new ServerError(
|
321
|
+
Status.INTERNAL,
|
322
|
+
'error processing transaction: ' + JSON.stringify(ethTransaction.transaction) + '\n' + errorString(e)
|
323
|
+
)
|
324
|
+
})
|
325
|
+
)
|
326
|
+
}
|
327
|
+
return mergeProcessResults(await Promise.all(promises))
|
328
|
+
}
|
267
329
|
}
|
268
330
|
|
269
331
|
PluginManager.INSTANCE.register(new EthPlugin())
|
package/src/eth/index.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export { BaseProcessor } from './base-processor.js'
|
1
|
+
export { BaseProcessor, GlobalProcessor } from './base-processor.js'
|
2
2
|
export { GenericProcessor } from './generic-processor.js'
|
3
3
|
export { BaseProcessorTemplate } from './base-processor-template.js'
|
4
4
|
export { AccountProcessor } from './account-processor.js'
|
@@ -6,6 +6,6 @@ export { getProvider, DummyProvider, getNetworkFromCtxOrNetworkish } from './pro
|
|
6
6
|
export * from './eth.js'
|
7
7
|
export { BindOptions, AccountBindOptions } from './bind-options.js'
|
8
8
|
export { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds.js'
|
9
|
-
export { ContractContext, ContractView, BoundContractView } from './context.js'
|
9
|
+
export { ContractContext, GlobalContext, ContractView, BoundContractView } from './context.js'
|
10
10
|
|
11
11
|
export { EthPlugin } from './eth-plugin.js'
|