@sentio/sdk 1.30.0 → 1.30.2

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.
Files changed (41) hide show
  1. package/lib/cli/webpack.config.js +3 -2
  2. package/lib/core/exporter.d.ts +4 -0
  3. package/lib/core/exporter.js +7 -3
  4. package/lib/core/exporter.js.map +1 -1
  5. package/lib/core/meter.d.ts +14 -1
  6. package/lib/core/meter.js +39 -10
  7. package/lib/core/meter.js.map +1 -1
  8. package/lib/index.d.ts +0 -1
  9. package/lib/index.js +2 -3
  10. package/lib/index.js.map +1 -1
  11. package/lib/processor-runner.js +1 -1
  12. package/lib/processor-runner.js.map +1 -1
  13. package/lib/service.js +4 -2
  14. package/lib/service.js.map +1 -1
  15. package/lib/{processor-state.d.ts → state/processor-state.d.ts} +4 -7
  16. package/lib/{processor-state.js → state/processor-state.js} +1 -2
  17. package/lib/state/processor-state.js.map +1 -0
  18. package/lib/state/state-storage.d.ts +12 -0
  19. package/lib/state/state-storage.js +46 -0
  20. package/lib/state/state-storage.js.map +1 -0
  21. package/lib/state/state-storage.test.d.ts +1 -0
  22. package/lib/state/state-storage.test.js.map +1 -0
  23. package/lib/testing/test-processor-server.js +1 -1
  24. package/lib/testing/test-processor-server.js.map +1 -1
  25. package/lib/utils/dex-price.test.js.map +1 -1
  26. package/lib/utils/erc20.test.js.map +1 -1
  27. package/lib/utils/price.d.ts +45 -3
  28. package/lib/utils/price.js +47 -27
  29. package/lib/utils/price.js.map +1 -1
  30. package/package.json +3 -1
  31. package/src/cli/webpack.config.js +3 -2
  32. package/src/core/exporter.ts +6 -2
  33. package/src/core/meter.ts +41 -9
  34. package/src/index.ts +1 -1
  35. package/src/processor-runner.ts +1 -1
  36. package/src/service.ts +5 -2
  37. package/src/{processor-state.ts → state/processor-state.ts} +4 -8
  38. package/src/state/state-storage.ts +49 -0
  39. package/src/testing/test-processor-server.ts +1 -1
  40. package/src/utils/price.ts +54 -25
  41. package/lib/processor-state.js.map +0 -1
@@ -0,0 +1,12 @@
1
+ export declare abstract class StateStorage<T> {
2
+ protected constructor();
3
+ abstract initValue(): T;
4
+ key(): string;
5
+ getOrRegister(): T;
6
+ }
7
+ export declare abstract class MapStateStorage<T> extends StateStorage<Map<string, T>> {
8
+ initValue(): Map<string, T>;
9
+ getValue(key: string): T | undefined;
10
+ getValues(): T[];
11
+ getOrSetValue(key: string, value: T): T;
12
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MapStateStorage = exports.StateStorage = void 0;
4
+ class StateStorage {
5
+ // TODO learn how to define single instance for all subclasses
6
+ constructor() {
7
+ //
8
+ }
9
+ key() {
10
+ return this.constructor.name;
11
+ }
12
+ getOrRegister() {
13
+ let metricState = global.PROCESSOR_STATE.stateMap.get(this.key());
14
+ if (!metricState) {
15
+ metricState = this.initValue();
16
+ global.PROCESSOR_STATE.stateMap.set(this.key(), metricState);
17
+ }
18
+ return metricState;
19
+ }
20
+ }
21
+ exports.StateStorage = StateStorage;
22
+ class MapStateStorage extends StateStorage {
23
+ initValue() {
24
+ return new Map();
25
+ }
26
+ getValue(key) {
27
+ const m = this.getOrRegister();
28
+ return m.get(key);
29
+ }
30
+ getValues() {
31
+ const m = this.getOrRegister();
32
+ return Array.from(m.values());
33
+ }
34
+ getOrSetValue(key, value) {
35
+ const m = this.getOrRegister();
36
+ const oldValue = m.get(key);
37
+ if (oldValue) {
38
+ console.warn(key, 'has been registered twice, use the previous one');
39
+ return oldValue;
40
+ }
41
+ m.set(key, value);
42
+ return value;
43
+ }
44
+ }
45
+ exports.MapStateStorage = MapStateStorage;
46
+ //# sourceMappingURL=state-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-storage.js","sourceRoot":"","sources":["../../src/state/state-storage.ts"],"names":[],"mappings":";;;AAAA,MAAsB,YAAY;IAChC,8DAA8D;IAE9D;QACE,EAAE;IACJ,CAAC;IAID,GAAG;QACD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n // TODO learn how to define single instance for all subclasses\n\n protected constructor() {\n //\n }\n\n abstract initValue(): T\n\n key(): string {\n return this.constructor.name\n }\n\n getOrRegister(): T {\n let metricState: T = global.PROCESSOR_STATE.stateMap.get(this.key())\n if (!metricState) {\n metricState = this.initValue()\n global.PROCESSOR_STATE.stateMap.set(this.key(), metricState)\n }\n return metricState\n }\n}\n\nexport abstract class MapStateStorage<T> extends StateStorage<Map<string, T>> {\n initValue() {\n return new Map<string, T>()\n }\n\n getValue(key: string): T | undefined {\n const m = this.getOrRegister()\n return m.get(key)\n }\n\n getValues(): T[] {\n const m = this.getOrRegister()\n return Array.from(m.values())\n }\n\n getOrSetValue(key: string, value: T): T {\n const m = this.getOrRegister()\n const oldValue = m.get(key)\n if (oldValue) {\n console.warn(key, 'has been registered twice, use the previous one')\n return oldValue\n }\n m.set(key, value)\n return value\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-storage.test.js","sourceRoot":"","sources":["../../src/state/state-storage.test.ts"],"names":[],"mappings":";;AAAA,+BAA6B;AAC7B,mDAAiD;AACjD,uDAAkD;AAElD,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;IACnC,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAE7C,IAAI,CAAC,aAAa,EAAE,KAAK,IAAI,EAAE;QAC7B,MAAM,SAAU,SAAQ,+BAAoB;YAC1C,MAAM,CAAC,QAAQ,GAAG,IAAI,SAAS,EAAE,CAAA;;QAEnC,MAAM,CAAC,GAAG,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAA;QAC5C,IAAA,aAAM,EAAC,CAAC,KAAK,SAAS,CAAC,CAAA;QAEvB,MAAM,EAAE,GAAG,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;QACrD,MAAM,EAAE,GAAG,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAA;QAC5D,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,CAAC,CAAA;QACjB,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAE5C,IAAA,aAAM,EAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC,MAAM,KAAK,CAAC,CAAC,CAAA;QACnD,IAAA,aAAM,EAAC,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,CAAC,CAAA;QAClD,IAAA,aAAM,EAAC,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,KAAK,WAAW,CAAC,CAAA;IAC7E,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { assert } from 'chai'\nimport { MapStateStorage } from './state-storage'\nimport { ProcessorState } from './processor-state'\n\ndescribe('state storage tests', () => {\n global.PROCESSOR_STATE = new ProcessorState()\n\n test('test labels', async () => {\n class TestState extends MapStateStorage<any> {\n static INSTANCE = new TestState()\n }\n const m = TestState.INSTANCE.getOrRegister()\n assert(m !== undefined)\n\n const v1 = TestState.INSTANCE.getOrSetValue('k1', {})\n const v2 = TestState.INSTANCE.getOrSetValue('k1', { a: '' })\n assert(v1 === v2)\n TestState.INSTANCE.getOrSetValue('k2', 'v2')\n\n assert(TestState.INSTANCE.getValues().length === 2)\n assert(global.PROCESSOR_STATE.stateMap.size === 1)\n assert(global.PROCESSOR_STATE.stateMap.keys().next().value === 'TestState')\n })\n})\n"]}
@@ -9,7 +9,7 @@ const chain_1 = require("../utils/chain");
9
9
  const long_1 = __importDefault(require("long"));
10
10
  const providers_1 = require("@ethersproject/providers");
11
11
  const endpoints_1 = require("../endpoints");
12
- const processor_state_1 = require("../processor-state");
12
+ const processor_state_1 = require("../state/processor-state");
13
13
  const service_1 = require("../service");
14
14
  const provider_1 = require("../provider");
15
15
  exports.TEST_CONTEXT = {};
@@ -1 +1 @@
1
- {"version":3,"file":"test-processor-server.js","sourceRoot":"","sources":["../../src/testing/test-processor-server.ts"],"names":[],"mappings":";;;;;;AAAA,gCAce;AAIf,0CAA0C;AAE1C,gDAAuB;AACvB,wDAAiE;AACjE,4CAAwC;AACxC,wDAAmD;AACnD,wCAAiD;AAEjD,0CAAyC;AAE5B,QAAA,YAAY,GAA6B,EAAE,CAAA;AAExD,SAAgB,SAAS;IACvB,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAC7C,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;QACrB,MAAM,CAAC,SAAS,GAAG,IAAI,qBAAS,EAAE,CAAA;KACnC;AACH,CAAC;AALD,8BAKC;AAED,MAAa,mBAAmB;IAC9B,OAAO,CAAsB;IAC7B,cAAc,CAAkB;IAEhC,YAAY,MAAkB,EAAE,gBAAwC,EAAE;QACxE,SAAS,EAAE,CAAA;QAEX,IAAI,CAAC,OAAO,GAAG,IAAI,8BAAoB,CAAC,MAAM,CAAC,CAAA;QAC/C,MAAM,WAAW,GAAgC,EAAE,CAAA;QAEnD,KAAK,MAAM,CAAC,IAAI,iBAAS,EAAE;YACzB,MAAM,IAAI,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;YACnC,WAAW,CAAC,CAAC,CAAC,GAAG;gBACf,OAAO,EAAE,CAAC;gBACV,KAAK,EAAE,CAAC,IAAI,CAAC;aACd,CAAA;SACF;QAED,IAAA,sBAAW,EAAC,WAAW,CAAC,CAAA;IAC1B,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,UAAwB,EAAE,iBAAiB,EAAE,EAAE,EAAE,EAAE,OAAO,GAAG,oBAAY;QACnF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACtD,IAAI,CAAC,cAAc,GAAG,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;IACvD,CAAC;CACF;AArND,kDAqNC;AAED,SAAS,OAAO,CAAC,GAAQ;IACvB,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IACtC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IAC7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,GAAG,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;KAClC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC","sourcesContent":["import {\n BlockBinding,\n ContractConfig,\n DataBinding,\n HandlerType,\n ProcessBindingResponse,\n ProcessBindingsRequest,\n ProcessBlocksRequest,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessInstructionsRequest,\n ProcessorServiceImplementation,\n ProcessTransactionsRequest,\n StartRequest,\n} from '../gen'\nimport { CallContext } from 'nice-grpc-common'\nimport { Empty } from '../gen/google/protobuf/empty'\nimport { ChainConfig } from '../chain-config'\nimport { CHAIN_MAP } from '../utils/chain'\nimport { Block, Log } from '@ethersproject/abstract-provider'\nimport Long from 'long'\nimport { getNetwork, Networkish } from '@ethersproject/providers'\nimport { Endpoints } from '../endpoints'\nimport { ProcessorState } from '../processor-state'\nimport { ProcessorServiceImpl } from '../service'\nimport { Trace } from '../core/trace'\nimport { setProvider } from '../provider'\n\nexport const TEST_CONTEXT: CallContext = <CallContext>{}\n\nexport function cleanTest() {\n global.PROCESSOR_STATE = new ProcessorState()\n if (!global.ENDPOINTS) {\n global.ENDPOINTS = new Endpoints()\n }\n}\n\nexport class TestProcessorServer implements ProcessorServiceImplementation {\n service: ProcessorServiceImpl\n contractConfig: ContractConfig[]\n\n constructor(loader: () => void, httpEndpoints: Record<string, string> = {}) {\n cleanTest()\n\n this.service = new ProcessorServiceImpl(loader)\n const dummyConfig: Record<string, ChainConfig> = {}\n\n for (const k in CHAIN_MAP) {\n const http = httpEndpoints[k] || ''\n dummyConfig[k] = {\n ChainID: k,\n Https: [http],\n }\n }\n\n setProvider(dummyConfig)\n }\n\n async start(request: StartRequest = { templateInstances: [] }, context = TEST_CONTEXT): Promise<Empty> {\n const res = await this.service.start(request, context)\n this.contractConfig = (await this.getConfig({})).contractConfigs\n return res\n }\n\n stop(request: Empty, context = TEST_CONTEXT): Promise<Empty> {\n return this.service.stop(request, context)\n }\n\n getConfig(request: ProcessConfigRequest, context = TEST_CONTEXT): Promise<ProcessConfigResponse> {\n return this.service.getConfig(request, context)\n }\n\n processBlocks(request: ProcessBlocksRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processBlocks(request, context)\n }\n\n processInstructions(request: ProcessInstructionsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processInstructions(request, context)\n }\n\n processLogs(request: ProcessBindingsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processLogs(request, context)\n }\n\n processTraces(request: ProcessBindingsRequest, context: CallContext = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processTraces(request, context)\n }\n\n processTransactions(request: ProcessTransactionsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processTransactions(request, context)\n }\n\n testTrace(trace: Trace, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testTraces([trace], network)\n }\n\n testTraces(traces: Trace[], network: Networkish = 1): Promise<ProcessBindingResponse> {\n const bindings = []\n for (const trace of traces) {\n const binding = this.buildTraceBinding(trace, network)\n if (!binding) {\n throw Error('Invalid test trace: ' + JSON.stringify(trace))\n }\n bindings.push(binding)\n }\n return this.processTraces({\n bindings: bindings,\n })\n }\n\n buildTraceBinding(trace: Trace, network: Networkish = 1): DataBinding | undefined {\n if (trace.type !== 'call' || !trace.action.input) {\n throw Error('Invalid test trace: ' + JSON.stringify(trace))\n }\n const signature = trace.action.input.slice(0, 10)\n\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n if (trace.action.to?.toLowerCase() !== contract.contract?.address.toLowerCase()) {\n continue\n }\n for (const config of contract.traceConfigs) {\n if (config.signature == signature) {\n return {\n data: {\n raw: toBytes(trace),\n },\n handlerId: config.handlerId,\n handlerType: HandlerType.ETH_TRACE,\n }\n }\n }\n }\n return undefined\n }\n\n testLog(log: Log, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testLogs([log], network)\n }\n\n testLogs(logs: Log[], network: Networkish = 1): Promise<ProcessBindingResponse> {\n const bindings = []\n for (const log of logs) {\n const binding = this.buildLogBinding(log, network)\n if (!binding) {\n throw Error('Invalid test log: ' + JSON.stringify(log))\n }\n bindings.push(binding)\n }\n return this.processLogs({\n bindings: bindings,\n })\n }\n\n buildLogBinding(log: Log, network: Networkish = 1): DataBinding | undefined {\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n if (log.address.toLowerCase() !== contract.contract?.address.toLowerCase()) {\n continue\n }\n for (const config of contract.logConfigs) {\n for (const filter of config.filters) {\n // if (filter.topics.length != log.topics.length) {\n // continue\n // }\n\n let match = true\n for (const topicIdx in filter.topics) {\n const logTopic = log.topics[topicIdx]\n const possibleTopic = filter.topics[topicIdx].hashes\n if (possibleTopic.length === 0) {\n // match all\n continue\n }\n if (possibleTopic.find((e) => e.toLowerCase() === logTopic.toLowerCase())) {\n // find one\n continue\n }\n match = false\n break\n }\n if (match) {\n return {\n data: {\n raw: toBytes(log),\n },\n handlerId: config.handlerId,\n handlerType: HandlerType.ETH_LOG,\n }\n }\n }\n }\n }\n return undefined\n }\n\n testBlock(block: Partial<Block> & { number: number }, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testBlocks([block], network)\n }\n\n testBlocks(blocks: Partial<Block> & { number: number }[], network: Networkish = 1) {\n const bindings = []\n for (const block of blocks) {\n const binding = this.buildBlockBinding(block, network)\n if (!binding) {\n throw Error('Invalid test block: ' + JSON.stringify(block))\n }\n bindings.push(binding)\n }\n return this.processBlocks({\n blockBindings: bindings,\n })\n }\n\n buildBlockBinding(block: Partial<Block> & { number: number }, network: Networkish = 1): BlockBinding {\n const binding: BlockBinding = {\n block: {\n raw: toBytes(block),\n },\n handlerIds: [],\n }\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n const longBlockNumber = Long.fromNumber(block.number)\n if (longBlockNumber < contract.startBlock) {\n continue\n }\n if (contract.endBlock !== Long.ZERO && longBlockNumber >= contract.endBlock) {\n continue\n }\n\n for (const config of contract.intervalConfigs) {\n binding.handlerIds.push(config.handlerId)\n }\n }\n return binding\n }\n\n processBindings(\n request: ProcessBindingsRequest,\n context: CallContext = TEST_CONTEXT\n ): Promise<ProcessBindingResponse> {\n return this.service.processBindings(request, context)\n }\n}\n\nfunction toBytes(obj: any): Uint8Array {\n const logJsonStr = JSON.stringify(obj)\n const raw = new Uint8Array(logJsonStr.length)\n for (let i = 0; i < logJsonStr.length; i++) {\n raw[i] = logJsonStr.charCodeAt(i)\n }\n return raw\n}\n"]}
1
+ {"version":3,"file":"test-processor-server.js","sourceRoot":"","sources":["../../src/testing/test-processor-server.ts"],"names":[],"mappings":";;;;;;AAAA,gCAce;AAIf,0CAA0C;AAE1C,gDAAuB;AACvB,wDAAiE;AACjE,4CAAwC;AACxC,8DAAyD;AACzD,wCAAiD;AAEjD,0CAAyC;AAE5B,QAAA,YAAY,GAA6B,EAAE,CAAA;AAExD,SAAgB,SAAS;IACvB,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAC7C,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;QACrB,MAAM,CAAC,SAAS,GAAG,IAAI,qBAAS,EAAE,CAAA;KACnC;AACH,CAAC;AALD,8BAKC;AAED,MAAa,mBAAmB;IAC9B,OAAO,CAAsB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,QAAQ,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAA,sBAAU,EAAC,OAAO,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE;gBACzE,SAAQ;aACT;YACD,MAAM,eAAe,GAAG,cAAI,CAAC,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;YACrD,IAAI,eAAe,GAAG,QAAQ,CAAC,UAAU,EAAE;gBACzC,SAAQ;aACT;YACD,IAAI,QAAQ,CAAC,QAAQ,KAAK,cAAI,CAAC,IAAI,IAAI,eAAe,IAAI,QAAQ,CAAC,QAAQ,EAAE;gBAC3E,SAAQ;aACT;YAED,KAAK,MAAM,MAAM,IAAI,QAAQ,CAAC,eAAe,EAAE;gBAC7C,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;aAC1C;SACF;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,eAAe,CACb,OAA+B,EAC/B,UAAuB,oBAAY;QAEnC,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IACvD,CAAC;CACF;AArND,kDAqNC;AAED,SAAS,OAAO,CAAC,GAAQ;IACvB,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IACtC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IAC7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,GAAG,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;KAClC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC","sourcesContent":["import {\n BlockBinding,\n ContractConfig,\n DataBinding,\n HandlerType,\n ProcessBindingResponse,\n ProcessBindingsRequest,\n ProcessBlocksRequest,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessInstructionsRequest,\n ProcessorServiceImplementation,\n ProcessTransactionsRequest,\n StartRequest,\n} from '../gen'\nimport { CallContext } from 'nice-grpc-common'\nimport { Empty } from '../gen/google/protobuf/empty'\nimport { ChainConfig } from '../chain-config'\nimport { CHAIN_MAP } from '../utils/chain'\nimport { Block, Log } from '@ethersproject/abstract-provider'\nimport Long from 'long'\nimport { getNetwork, Networkish } from '@ethersproject/providers'\nimport { Endpoints } from '../endpoints'\nimport { ProcessorState } from '../state/processor-state'\nimport { ProcessorServiceImpl } from '../service'\nimport { Trace } from '../core/trace'\nimport { setProvider } from '../provider'\n\nexport const TEST_CONTEXT: CallContext = <CallContext>{}\n\nexport function cleanTest() {\n global.PROCESSOR_STATE = new ProcessorState()\n if (!global.ENDPOINTS) {\n global.ENDPOINTS = new Endpoints()\n }\n}\n\nexport class TestProcessorServer implements ProcessorServiceImplementation {\n service: ProcessorServiceImpl\n contractConfig: ContractConfig[]\n\n constructor(loader: () => void, httpEndpoints: Record<string, string> = {}) {\n cleanTest()\n\n this.service = new ProcessorServiceImpl(loader)\n const dummyConfig: Record<string, ChainConfig> = {}\n\n for (const k in CHAIN_MAP) {\n const http = httpEndpoints[k] || ''\n dummyConfig[k] = {\n ChainID: k,\n Https: [http],\n }\n }\n\n setProvider(dummyConfig)\n }\n\n async start(request: StartRequest = { templateInstances: [] }, context = TEST_CONTEXT): Promise<Empty> {\n const res = await this.service.start(request, context)\n this.contractConfig = (await this.getConfig({})).contractConfigs\n return res\n }\n\n stop(request: Empty, context = TEST_CONTEXT): Promise<Empty> {\n return this.service.stop(request, context)\n }\n\n getConfig(request: ProcessConfigRequest, context = TEST_CONTEXT): Promise<ProcessConfigResponse> {\n return this.service.getConfig(request, context)\n }\n\n processBlocks(request: ProcessBlocksRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processBlocks(request, context)\n }\n\n processInstructions(request: ProcessInstructionsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processInstructions(request, context)\n }\n\n processLogs(request: ProcessBindingsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processLogs(request, context)\n }\n\n processTraces(request: ProcessBindingsRequest, context: CallContext = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processTraces(request, context)\n }\n\n processTransactions(request: ProcessTransactionsRequest, context = TEST_CONTEXT): Promise<ProcessBindingResponse> {\n return this.service.processTransactions(request, context)\n }\n\n testTrace(trace: Trace, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testTraces([trace], network)\n }\n\n testTraces(traces: Trace[], network: Networkish = 1): Promise<ProcessBindingResponse> {\n const bindings = []\n for (const trace of traces) {\n const binding = this.buildTraceBinding(trace, network)\n if (!binding) {\n throw Error('Invalid test trace: ' + JSON.stringify(trace))\n }\n bindings.push(binding)\n }\n return this.processTraces({\n bindings: bindings,\n })\n }\n\n buildTraceBinding(trace: Trace, network: Networkish = 1): DataBinding | undefined {\n if (trace.type !== 'call' || !trace.action.input) {\n throw Error('Invalid test trace: ' + JSON.stringify(trace))\n }\n const signature = trace.action.input.slice(0, 10)\n\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n if (trace.action.to?.toLowerCase() !== contract.contract?.address.toLowerCase()) {\n continue\n }\n for (const config of contract.traceConfigs) {\n if (config.signature == signature) {\n return {\n data: {\n raw: toBytes(trace),\n },\n handlerId: config.handlerId,\n handlerType: HandlerType.ETH_TRACE,\n }\n }\n }\n }\n return undefined\n }\n\n testLog(log: Log, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testLogs([log], network)\n }\n\n testLogs(logs: Log[], network: Networkish = 1): Promise<ProcessBindingResponse> {\n const bindings = []\n for (const log of logs) {\n const binding = this.buildLogBinding(log, network)\n if (!binding) {\n throw Error('Invalid test log: ' + JSON.stringify(log))\n }\n bindings.push(binding)\n }\n return this.processLogs({\n bindings: bindings,\n })\n }\n\n buildLogBinding(log: Log, network: Networkish = 1): DataBinding | undefined {\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n if (log.address.toLowerCase() !== contract.contract?.address.toLowerCase()) {\n continue\n }\n for (const config of contract.logConfigs) {\n for (const filter of config.filters) {\n // if (filter.topics.length != log.topics.length) {\n // continue\n // }\n\n let match = true\n for (const topicIdx in filter.topics) {\n const logTopic = log.topics[topicIdx]\n const possibleTopic = filter.topics[topicIdx].hashes\n if (possibleTopic.length === 0) {\n // match all\n continue\n }\n if (possibleTopic.find((e) => e.toLowerCase() === logTopic.toLowerCase())) {\n // find one\n continue\n }\n match = false\n break\n }\n if (match) {\n return {\n data: {\n raw: toBytes(log),\n },\n handlerId: config.handlerId,\n handlerType: HandlerType.ETH_LOG,\n }\n }\n }\n }\n }\n return undefined\n }\n\n testBlock(block: Partial<Block> & { number: number }, network: Networkish = 1): Promise<ProcessBindingResponse> {\n return this.testBlocks([block], network)\n }\n\n testBlocks(blocks: Partial<Block> & { number: number }[], network: Networkish = 1) {\n const bindings = []\n for (const block of blocks) {\n const binding = this.buildBlockBinding(block, network)\n if (!binding) {\n throw Error('Invalid test block: ' + JSON.stringify(block))\n }\n bindings.push(binding)\n }\n return this.processBlocks({\n blockBindings: bindings,\n })\n }\n\n buildBlockBinding(block: Partial<Block> & { number: number }, network: Networkish = 1): BlockBinding {\n const binding: BlockBinding = {\n block: {\n raw: toBytes(block),\n },\n handlerIds: [],\n }\n for (const contract of this.contractConfig) {\n if (contract.contract?.chainId !== getNetwork(network).chainId.toString()) {\n continue\n }\n const longBlockNumber = Long.fromNumber(block.number)\n if (longBlockNumber < contract.startBlock) {\n continue\n }\n if (contract.endBlock !== Long.ZERO && longBlockNumber >= contract.endBlock) {\n continue\n }\n\n for (const config of contract.intervalConfigs) {\n binding.handlerIds.push(config.handlerId)\n }\n }\n return binding\n }\n\n processBindings(\n request: ProcessBindingsRequest,\n context: CallContext = TEST_CONTEXT\n ): Promise<ProcessBindingResponse> {\n return this.service.processBindings(request, context)\n }\n}\n\nfunction toBytes(obj: any): Uint8Array {\n const logJsonStr = JSON.stringify(obj)\n const raw = new Uint8Array(logJsonStr.length)\n for (let i = 0; i < logJsonStr.length; i++) {\n raw[i] = logJsonStr.charCodeAt(i)\n }\n return raw\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"dex-price.test.js","sourceRoot":"","sources":["../../src/utils/dex-price.test.ts"],"names":[],"mappings":";;AAAA,2CAA8D;AAC9D,wDAAmD;AACnD,4DAAmE;AAEnE,+BAA6B;AAC7B,4CAAwC;AAExC,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAC7C,MAAM,CAAC,SAAS,GAAG,IAAI,qBAAS,EAAE,CAAA;IAElC,MAAM,aAAa,GAAG,IAAA,wCAAwB,EAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;IAE1D,MAAM,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;IAE/C,MAAM,CAAC,sBAAsB,EAAE,KAAK,IAAI,EAAE;QACxC,MAAM,IAAI,GAAG,MAAM,4BAAgB,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAC9D,IAAA,aAAM,EAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAA;QAEjC,MAAM,QAAQ,GAAG,MAAM,4BAAgB,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAClE,IAAA,aAAM,EAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,qBAAqB,EAAE,KAAK,IAAI,EAAE;QACvC,MAAM,GAAG,GAAG,MAAM,0BAAc,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACzD,IAAA,aAAM,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { EthereumDexPrice, GoerliDexPrice } from './dex-price'\nimport { ProcessorState } from '../processor-state'\nimport { loadTestProvidersFromEnv } from '../testing/test-provider'\n\nimport { expect } from 'chai'\nimport { Endpoints } from '../endpoints'\n\ndescribe('dex price tests', () => {\n global.PROCESSOR_STATE = new ProcessorState()\n global.ENDPOINTS = new Endpoints()\n\n const haveProviders = loadTestProvidersFromEnv(['1', '5'])\n\n const testIf = haveProviders ? test : test.skip\n\n testIf('get price at mainnet', async () => {\n const usdc = await EthereumDexPrice.getPrice('usdc', 15677823)\n expect(usdc.price).eq(0.99991649)\n\n const compound = await EthereumDexPrice.getPrice('COMP', 15677823)\n expect(compound.price).eq(60.27)\n })\n\n testIf('get price at goerli', async () => {\n const dai = await GoerliDexPrice.getPrice('DAI', 7712734)\n expect(dai.price).eq(0.99971281)\n })\n})\n"]}
1
+ {"version":3,"file":"dex-price.test.js","sourceRoot":"","sources":["../../src/utils/dex-price.test.ts"],"names":[],"mappings":";;AAAA,2CAA8D;AAC9D,8DAAyD;AACzD,4DAAmE;AAEnE,+BAA6B;AAC7B,4CAAwC;AAExC,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAC7C,MAAM,CAAC,SAAS,GAAG,IAAI,qBAAS,EAAE,CAAA;IAElC,MAAM,aAAa,GAAG,IAAA,wCAAwB,EAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;IAE1D,MAAM,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;IAE/C,MAAM,CAAC,sBAAsB,EAAE,KAAK,IAAI,EAAE;QACxC,MAAM,IAAI,GAAG,MAAM,4BAAgB,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAC9D,IAAA,aAAM,EAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAA;QAEjC,MAAM,QAAQ,GAAG,MAAM,4BAAgB,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAClE,IAAA,aAAM,EAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,qBAAqB,EAAE,KAAK,IAAI,EAAE;QACvC,MAAM,GAAG,GAAG,MAAM,0BAAc,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACzD,IAAA,aAAM,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { EthereumDexPrice, GoerliDexPrice } from './dex-price'\nimport { ProcessorState } from '../state/processor-state'\nimport { loadTestProvidersFromEnv } from '../testing/test-provider'\n\nimport { expect } from 'chai'\nimport { Endpoints } from '../endpoints'\n\ndescribe('dex price tests', () => {\n global.PROCESSOR_STATE = new ProcessorState()\n global.ENDPOINTS = new Endpoints()\n\n const haveProviders = loadTestProvidersFromEnv(['1', '5'])\n\n const testIf = haveProviders ? test : test.skip\n\n testIf('get price at mainnet', async () => {\n const usdc = await EthereumDexPrice.getPrice('usdc', 15677823)\n expect(usdc.price).eq(0.99991649)\n\n const compound = await EthereumDexPrice.getPrice('COMP', 15677823)\n expect(compound.price).eq(60.27)\n })\n\n testIf('get price at goerli', async () => {\n const dai = await GoerliDexPrice.getPrice('DAI', 7712734)\n expect(dai.price).eq(0.99971281)\n })\n})\n"]}
@@ -1 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AC,CAAA","sourcesContent":["import { ProcessorState } from '../processor-state'\nimport { getERC20TokenInfo } from './token'\nimport { loadTestProvidersFromEnv } from '../testing/test-provider'\nimport { Endpoints } from '../endpoints'\n\ndescribe('erc20 tests', () => {\n global.PROCESSOR_STATE = new ProcessorState()\n global.ENDPOINTS = new Endpoints()\n\n const haveProviders = loadTestProvidersFromEnv('1')\n\n const testIf = haveProviders ? test : test.skip\n\n testIf('test bytes32', async () => {\n const info = await getERC20TokenInfo('0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2')\n\n expect(info.decimal).toEqual(18)\n expect(info.symbol).toEqual('MKR')\n expect(info.name).toEqual('Maker')\n })\n\n testIf('test normal', async () => {\n const info = await getERC20TokenInfo('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')\n\n expect(info.decimal).toEqual(6)\n expect(info.symbol).toEqual('USDC')\n expect(info.name).toEqual('USD Coin')\n })\n})\n"]}
1
+ {"version":3,"file":"erc20.test.js","sourceRoot":"","sources":["../../src/utils/erc20.test.ts"],"names":[],"mappings":";;AAAA,8DAAyD;AACzD,mCAA2C;AAC3C,4DAAmE;AACnE,4CAAwC;AAExC,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,MAAM,CAAC,eAAe,GAAG,IAAI,gCAAc,EAAE,CAAA;IAC7C,MAAM,CAAC,SAAS,GAAG,IAAI,qBAAS,EAAE,CAAA;IAElC,MAAM,aAAa,GAAG,IAAA,wCAAwB,EAAC,GAAG,CAAC,CAAA;IAEnD,MAAM,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;IAE/C,MAAM,CAAC,cAAc,EAAE,KAAK,IAAI,EAAE;QAChC,MAAM,IAAI,GAAG,MAAM,IAAA,yBAAiB,EAAC,4CAA4C,CAAC,CAAA;QAElF,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QAChC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAClC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACpC,CAAC,CAAC,CAAA;IAEF,MAAM,CAAC,aAAa,EAAE,KAAK,IAAI,EAAE;QAC/B,MAAM,IAAI,GAAG,MAAM,IAAA,yBAAiB,EAAC,4CAA4C,CAAC,CAAA;QAElF,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QAC/B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QACnC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IACvC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { ProcessorState } from '../state/processor-state'\nimport { getERC20TokenInfo } from './token'\nimport { loadTestProvidersFromEnv } from '../testing/test-provider'\nimport { Endpoints } from '../endpoints'\n\ndescribe('erc20 tests', () => {\n global.PROCESSOR_STATE = new ProcessorState()\n global.ENDPOINTS = new Endpoints()\n\n const haveProviders = loadTestProvidersFromEnv('1')\n\n const testIf = haveProviders ? test : test.skip\n\n testIf('test bytes32', async () => {\n const info = await getERC20TokenInfo('0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2')\n\n expect(info.decimal).toEqual(18)\n expect(info.symbol).toEqual('MKR')\n expect(info.name).toEqual('Maker')\n })\n\n testIf('test normal', async () => {\n const info = await getERC20TokenInfo('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')\n\n expect(info.decimal).toEqual(6)\n expect(info.symbol).toEqual('USDC')\n expect(info.name).toEqual('USD Coin')\n })\n})\n"]}
@@ -1,5 +1,42 @@
1
- import { PriceServiceClient } from '../gen/service/price/protos/price';
2
- export declare function getPriceClient(address?: string): PriceServiceClient;
1
+ import { RetryOptions } from 'nice-grpc-client-middleware-retry';
2
+ export declare function getPriceClient(address?: string): import("nice-grpc").RawClient<import("nice-grpc/lib/service-definitions/ts-proto").FromTsProtoServiceDefinition<{
3
+ readonly name: "PriceService";
4
+ readonly fullName: "price_service.PriceService";
5
+ readonly methods: {
6
+ readonly getPrice: {
7
+ readonly name: "GetPrice";
8
+ readonly requestType: {
9
+ encode(message: import("../gen/service/price/protos/price").GetPriceRequest, writer?: import("protobufjs").Writer): import("protobufjs").Writer;
10
+ decode(input: Uint8Array | import("protobufjs").Reader, length?: number | undefined): import("../gen/service/price/protos/price").GetPriceRequest;
11
+ fromJSON(object: any): import("../gen/service/price/protos/price").GetPriceRequest;
12
+ toJSON(message: import("../gen/service/price/protos/price").GetPriceRequest): unknown;
13
+ fromPartial(object: {
14
+ timestamp?: Date | undefined;
15
+ coinId?: {
16
+ symbol?: string | undefined;
17
+ address?: {
18
+ address?: string | undefined;
19
+ chain?: string | undefined;
20
+ } | undefined;
21
+ } | undefined;
22
+ }): import("../gen/service/price/protos/price").GetPriceRequest;
23
+ };
24
+ readonly requestStream: false;
25
+ readonly responseType: {
26
+ encode(message: import("../gen/service/price/protos/price").GetPriceResponse, writer?: import("protobufjs").Writer): import("protobufjs").Writer;
27
+ decode(input: Uint8Array | import("protobufjs").Reader, length?: number | undefined): import("../gen/service/price/protos/price").GetPriceResponse;
28
+ fromJSON(object: any): import("../gen/service/price/protos/price").GetPriceResponse;
29
+ toJSON(message: import("../gen/service/price/protos/price").GetPriceResponse): unknown;
30
+ fromPartial(object: {
31
+ price?: number | undefined;
32
+ timestamp?: Date | undefined;
33
+ }): import("../gen/service/price/protos/price").GetPriceResponse;
34
+ };
35
+ readonly responseStream: false;
36
+ readonly options: {};
37
+ };
38
+ };
39
+ }>, RetryOptions>;
3
40
  /**
4
41
  *
5
42
  * @param chainId chain id refers to CHAIN_MAP
@@ -7,4 +44,9 @@ export declare function getPriceClient(address?: string): PriceServiceClient;
7
44
  * @param date
8
45
  */
9
46
  export declare function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number>;
10
- export declare function delay(ms: number): Promise<unknown>;
47
+ /**
48
+ *
49
+ * @param symbol token symbol like BTC, etc
50
+ * @param date
51
+ */
52
+ export declare function getPriceBySymbol(symbol: string, date: Date): Promise<number>;
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.delay = exports.getPriceByType = exports.getPriceClient = void 0;
3
+ exports.getPriceBySymbol = exports.getPriceByType = exports.getPriceClient = void 0;
4
4
  const price_1 = require("../gen/service/price/protos/price");
5
5
  const nice_grpc_1 = require("nice-grpc");
6
+ const nice_grpc_client_middleware_retry_1 = require("nice-grpc-client-middleware-retry");
6
7
  function getPriceClient(address) {
7
8
  if (!address) {
8
9
  address = global.ENDPOINTS.priceFeedAPI;
9
10
  }
10
11
  const channel = (0, nice_grpc_1.createChannel)(address);
11
- return (0, nice_grpc_1.createClient)(price_1.PriceServiceDefinition, channel);
12
+ return (0, nice_grpc_1.createClientFactory)().use(nice_grpc_client_middleware_retry_1.retryMiddleware).create(price_1.PriceServiceDefinition, channel);
12
13
  }
13
14
  exports.getPriceClient = getPriceClient;
14
15
  const priceMap = new Map();
@@ -25,35 +26,54 @@ async function getPriceByType(chainId, coinType, date) {
25
26
  }
26
27
  const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-');
27
28
  const key = `${coinType}-${dateStr}`;
28
- const price = priceMap.get(key);
29
+ let price = priceMap.get(key);
29
30
  if (price) {
30
31
  return price;
31
32
  }
32
- /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
33
- while (true) {
34
- try {
35
- const response = await priceClient.getPrice({
36
- timestamp: date,
37
- coinId: {
38
- address: {
39
- chain: chainId,
40
- address: coinType,
41
- },
42
- },
43
- });
44
- const price = response.price;
45
- priceMap.set(key, price);
46
- return price;
47
- }
48
- catch (e) {
49
- console.log('error getting price', e, dateStr, coinType);
50
- await delay(1000);
51
- }
52
- }
33
+ const response = await priceClient.getPrice({
34
+ timestamp: date,
35
+ coinId: {
36
+ address: {
37
+ chain: chainId,
38
+ address: coinType,
39
+ },
40
+ },
41
+ }, {
42
+ retry: true,
43
+ retryMaxAttempts: 8,
44
+ });
45
+ price = response.price;
46
+ priceMap.set(key, price);
47
+ return price;
53
48
  }
54
49
  exports.getPriceByType = getPriceByType;
55
- function delay(ms) {
56
- return new Promise((resolve) => setTimeout(resolve, ms));
50
+ /**
51
+ *
52
+ * @param symbol token symbol like BTC, etc
53
+ * @param date
54
+ */
55
+ async function getPriceBySymbol(symbol, date) {
56
+ if (!priceClient) {
57
+ priceClient = getPriceClient();
58
+ }
59
+ const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-');
60
+ const key = `${symbol}-${dateStr}`;
61
+ let price = priceMap.get(key);
62
+ if (price) {
63
+ return price;
64
+ }
65
+ const response = await priceClient.getPrice({
66
+ timestamp: date,
67
+ coinId: {
68
+ symbol,
69
+ },
70
+ }, {
71
+ retry: true,
72
+ retryMaxAttempts: 8,
73
+ });
74
+ price = response.price;
75
+ priceMap.set(key, price);
76
+ return price;
57
77
  }
58
- exports.delay = delay;
78
+ exports.getPriceBySymbol = getPriceBySymbol;
59
79
  //# sourceMappingURL=price.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":";;;AAAA,6DAA8F;AAC9F,yCAAuD;AAEvD,SAAgB,cAAc,CAAC,OAAgB;IAC7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,YAAY,CAAA;KACxC;IACD,MAAM,OAAO,GAAG,IAAA,yBAAa,EAAC,OAAO,CAAC,CAAA;IAEtC,OAAO,IAAA,wBAAY,EAAC,8BAAsB,EAAE,OAAO,CAAC,CAAA;AACtD,CAAC;AAPD,wCAOC;AAED,MAAM,QA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AAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAC1D,CAAC;AAFD,sBAEC","sourcesContent":["import { PriceServiceClient, PriceServiceDefinition } from '../gen/service/price/protos/price'\nimport { createChannel, createClient } from 'nice-grpc'\n\nexport function getPriceClient(address?: string): PriceServiceClient {\n if (!address) {\n address = global.ENDPOINTS.priceFeedAPI\n }\n const channel = createChannel(address)\n\n return createClient(PriceServiceDefinition, channel)\n}\n\nconst priceMap = new Map<string, number>()\nlet priceClient: PriceServiceClient\n\n/**\n *\n * @param chainId chain id refers to CHAIN_MAP\n * @param coinType\n * @param date\n */\nexport async function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number> {\n if (!priceClient) {\n priceClient = getPriceClient()\n }\n\n const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\n const key = `${coinType}-${dateStr}`\n const price = priceMap.get(key)\n if (price) {\n return price\n }\n\n /*eslint no-constant-condition: [\"error\", { \"checkLoops\": false }]*/\n while (true) {\n try {\n const response = await priceClient.getPrice({\n timestamp: date,\n coinId: {\n address: {\n chain: chainId,\n address: coinType,\n },\n },\n })\n const price = response.price\n priceMap.set(key, price)\n return price\n } catch (e) {\n console.log('error getting price', e, dateStr, coinType)\n await delay(1000)\n }\n }\n}\n\nexport function delay(ms: number) {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n"]}
1
+ {"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":";;;AAAA,6DAA8F;AAC9F,yCAA8D;AAC9D,yFAAiF;AAEjF,SAAgB,cAAc,CAAC,OAAgB;IAC7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,YAAY,CAAA;KACxC;IACD,MAAM,OAAO,GAAG,IAAA,yBAAa,EAAC,OAAO,CAAC,CAAA;IAEtC,OAAO,IAAA,+BAAmB,GAAE,CAAC,GAAG,CAAC,mDAAe,CAAC,CAAC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AU,EAAE,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAC5F,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,OAAO,EAAE,CAAA;IAClC,IAAI,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC7B,IAAI,KAAK,EAAE;QACT,OAAO,KAAK,CAAA;KACb;IAED,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,QAAQ,CACzC;QACE,SAAS,EAAE,IAAI;QACf,MAAM,EAAE;YACN,MAAM;SACP;KACF,EACD;QACE,KAAK,EAAE,IAAI;QACX,gBAAgB,EAAE,CAAC;KACpB,CACF,CAAA;IACD,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAA;IACtB,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACxB,OAAO,KAAK,CAAA;AACd,CAAC;AA3BD,4CA2BC","sourcesContent":["import { PriceServiceClient, PriceServiceDefinition } from '../gen/service/price/protos/price'\nimport { createChannel, createClientFactory } from 'nice-grpc'\nimport { retryMiddleware, RetryOptions } from 'nice-grpc-client-middleware-retry'\n\nexport function getPriceClient(address?: string) {\n if (!address) {\n address = global.ENDPOINTS.priceFeedAPI\n }\n const channel = createChannel(address)\n\n return createClientFactory().use(retryMiddleware).create(PriceServiceDefinition, channel)\n}\n\nconst priceMap = new Map<string, number>()\nlet priceClient: PriceServiceClient<RetryOptions>\n\n/**\n *\n * @param chainId chain id refers to CHAIN_MAP\n * @param coinType\n * @param date\n */\nexport async function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number> {\n if (!priceClient) {\n priceClient = getPriceClient()\n }\n\n const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\n const key = `${coinType}-${dateStr}`\n let price = priceMap.get(key)\n if (price) {\n return price\n }\n\n const response = await priceClient.getPrice(\n {\n timestamp: date,\n coinId: {\n address: {\n chain: chainId,\n address: coinType,\n },\n },\n },\n {\n retry: true,\n retryMaxAttempts: 8,\n }\n )\n price = response.price\n priceMap.set(key, price)\n return price\n}\n\n/**\n *\n * @param symbol token symbol like BTC, etc\n * @param date\n */\nexport async function getPriceBySymbol(symbol: string, date: Date): Promise<number> {\n if (!priceClient) {\n priceClient = getPriceClient()\n }\n\n const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\n const key = `${symbol}-${dateStr}`\n let price = priceMap.get(key)\n if (price) {\n return price\n }\n\n const response = await priceClient.getPrice(\n {\n timestamp: date,\n coinId: {\n symbol,\n },\n },\n {\n retry: true,\n retryMaxAttempts: 8,\n }\n )\n price = response.price\n priceMap.set(key, price)\n return price\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sentio/sdk",
3
3
  "license": "Apache-2.0",
4
- "version": "1.30.0",
4
+ "version": "1.30.2",
5
5
  "scripts": {
6
6
  "compile_target": "yarn tsc -b src/target-ethers-sentio/tsconfig.json",
7
7
  "compile": "tsc -p . && cp src/cli/webpack.config.js lib/cli && cp src/utils/*.csv lib/utils",
@@ -41,6 +41,8 @@
41
41
  "google-protobuf": "^3.15.8",
42
42
  "js-yaml": "^4.1.0",
43
43
  "nice-grpc": "^2.0.0",
44
+ "nice-grpc-client-middleware-retry": "^2.0.1",
45
+ "nice-grpc-error-details": "^0.1.4",
44
46
  "node-fetch": "2",
45
47
  "p-queue": "^6.6.2",
46
48
  "ts-loader": "^9.3.0",
@@ -26,15 +26,16 @@ module.exports = {
26
26
  mode: 'production',
27
27
  externals: [
28
28
  {
29
+ protobufjs: 'commonjs2 protobufjs',
29
30
  aptos: 'commonjs2 aptos-sdk',
30
31
  ethers: 'commonjs2 ethers',
31
32
  bs58: 'commonjs2 bs58',
32
33
  "bignumber.js": 'commonjs2 bignumber.js',
33
34
  'bn.js': 'commonjs2 bn.js',
34
- 'csv-parse': 'commonjs2 csv-parse'
35
+ 'csv-parse': 'commonjs2 csv-parse',
35
36
  },
36
37
  function ({ context, request }, callback) {
37
- if (/^@(ethersproject|solana|project-serum).*$/.test(request)) {
38
+ if (/^@(ethersproject|solana|project-serum|nice-grpc).*$/.test(request)) {
38
39
  return callback(null, 'commonjs ' + request)
39
40
  }
40
41
  if (request.startsWith("@sentio/sdk")) {
@@ -1,14 +1,18 @@
1
1
  import { BaseContext } from './base-context'
2
2
  import { ExportResult } from '@sentio/sdk'
3
3
  import { NamedResultDescriptor } from './metadata'
4
+ import { MapStateStorage } from '../state/state-storage'
4
5
 
5
6
  export type Export = Record<string, any>
6
7
 
8
+ export class ExporterState extends MapStateStorage<Exporter> {
9
+ static INSTANCE = new ExporterState()
10
+ }
11
+
7
12
  export class Exporter extends NamedResultDescriptor {
8
13
  static register(name: string, channel: string) {
9
14
  const exporter = new Exporter(name, channel)
10
- global.PROCESSOR_STATE.exporters.push(exporter)
11
- return exporter
15
+ return ExporterState.INSTANCE.getOrSetValue(name, exporter)
12
16
  }
13
17
 
14
18
  channel: string
package/src/core/meter.ts CHANGED
@@ -2,6 +2,7 @@ import { BaseContext } from './base-context'
2
2
  import { toMetricValue, Numberish } from './numberish'
3
3
  import { Labels, NamedResultDescriptor } from './metadata'
4
4
  import { AggregationConfig, MetricConfig } from '../gen'
5
+ import { MapStateStorage, StateStorage } from '../state/state-storage'
5
6
 
6
7
  export function normalizeName(name: string): string {
7
8
  const regex = new RegExp('![_.a-zA-Z0-9]')
@@ -40,20 +41,50 @@ export class CounterOptions {
40
41
  sparse?: boolean
41
42
  }
42
43
 
44
+ enum MetricType {
45
+ Counter = 0,
46
+ Gauge = 1,
47
+ }
48
+
43
49
  export class Metric extends NamedResultDescriptor {
50
+ type: MetricType
44
51
  descriptor: MetricConfig
45
- constructor(name: string, option?: MetricOptions) {
52
+ constructor(type: MetricType, name: string, option?: MetricOptions) {
46
53
  super(name)
54
+ this.type = type
47
55
  this.descriptor = MetricConfig.fromPartial({ name: this.name, ...option })
48
56
  }
49
57
  }
50
58
 
59
+ export class MetricState extends MapStateStorage<Metric> {
60
+ static INSTANCE = new MetricState()
61
+
62
+ getOrRegisterMetric(type: MetricType, name: string, option?: CounterOptions | MetricOptions): Metric {
63
+ const metricMap = this.getOrRegister()
64
+ let metric = metricMap.get(name)
65
+ if (metric && metric.type !== type) {
66
+ throw Error(`redefine ${name} of metric type ${type} that is previously ${metric.type}`)
67
+ }
68
+
69
+ if (!metric) {
70
+ if (type === MetricType.Counter) {
71
+ metric = new Counter(name, option)
72
+ } else {
73
+ metric = new Gauge(name, option)
74
+ }
75
+ }
76
+ metricMap.set(name, metric)
77
+ return metric
78
+ }
79
+ }
80
+
51
81
  export class Counter extends Metric {
52
82
  static register(name: string, option?: CounterOptions): Counter {
53
- // TODO also dedup
54
- const metric = new Counter(name, option)
55
- global.PROCESSOR_STATE.metrics.push(metric)
56
- return metric
83
+ return MetricState.INSTANCE.getOrRegisterMetric(MetricType.Counter, name, option) as Counter
84
+ }
85
+
86
+ constructor(name: string, option?: MetricOptions) {
87
+ super(MetricType.Counter, name, option)
57
88
  }
58
89
 
59
90
  add(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
@@ -94,10 +125,11 @@ export class CounterBinding {
94
125
 
95
126
  export class Gauge extends Metric {
96
127
  static register(name: string, option?: MetricOptions): Gauge {
97
- // TODO also dedup
98
- const metric = new Gauge(name, option)
99
- global.PROCESSOR_STATE.metrics.push(metric)
100
- return metric
128
+ return MetricState.INSTANCE.getOrRegisterMetric(MetricType.Gauge, name, option) as Gauge
129
+ }
130
+
131
+ constructor(name: string, option?: MetricOptions) {
132
+ super(MetricType.Counter, name, option)
101
133
  }
102
134
 
103
135
  record(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { ProcessorServiceImpl } from './service'
2
2
  export { getProvider, setProvider, DummyProvider } from './provider'
3
3
  export { transformEtherError } from './error'
4
- export { ProcessorState } from './processor-state'
4
+ // export { ProcessorState } from './state/processor-state'
5
5
  export { EthersError } from './error'
6
6
 
7
7
  export { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds'
@@ -8,7 +8,7 @@ import { setProvider } from './provider'
8
8
 
9
9
  import path from 'path'
10
10
  import fs from 'fs-extra'
11
- import { ProcessorState } from './processor-state'
11
+ import { ProcessorState } from './state/processor-state'
12
12
  import { load } from './loader'
13
13
  import { CompressionAlgorithms } from '@grpc/grpc-js/build/src/compression-algorithms'
14
14
  import { Endpoints } from './endpoints'
package/src/service.ts CHANGED
@@ -34,6 +34,9 @@ import { TextDecoder } from 'util'
34
34
  import { Trace } from './core'
35
35
  import { Instruction } from '@project-serum/anchor'
36
36
  import { MoveResourcesWithVersionPayload } from './aptos/aptos-processor'
37
+ import { MetricState } from './core/meter'
38
+ import { ExporterState } from './core/exporter'
39
+
37
40
  ;(BigInt.prototype as any).toJSON = function () {
38
41
  return this.toString()
39
42
  }
@@ -100,7 +103,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
100
103
  this.exportConfigs = []
101
104
 
102
105
  // part 0, prepare metrics and event tracking configs
103
- for (const metric of global.PROCESSOR_STATE.metrics) {
106
+ for (const metric of MetricState.INSTANCE.getValues()) {
104
107
  this.metricConfigs.push({
105
108
  ...metric.descriptor,
106
109
  })
@@ -117,7 +120,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
117
120
  })
118
121
  }
119
122
 
120
- for (const exporter of global.PROCESSOR_STATE.exporters) {
123
+ for (const exporter of ExporterState.INSTANCE.getValues()) {
121
124
  this.exportConfigs.push({
122
125
  name: exporter.name,
123
126
  channel: exporter.channel,
@@ -6,13 +6,11 @@ import {
6
6
  SolanaBaseProcessor,
7
7
  SuiBaseProcessor,
8
8
  EventTracker,
9
- } from './core'
9
+ } from '../core'
10
10
 
11
11
  import { BaseContract } from 'ethers'
12
- import { TemplateInstance } from './gen'
13
- import { Metric } from './core/meter'
14
- import { Exporter } from './core/exporter'
15
- import { AptosBaseProcessor, AptosAccountProcessor } from './aptos/aptos-processor'
12
+ import { TemplateInstance } from '../gen'
13
+ import { AptosBaseProcessor, AptosAccountProcessor } from '../aptos/aptos-processor'
16
14
 
17
15
  export class ProcessorState {
18
16
  // from abiName_address_chainId => contract wrapper
@@ -35,7 +33,5 @@ export class ProcessorState {
35
33
 
36
34
  eventTrackers: EventTracker[] = []
37
35
 
38
- exporters: Exporter[] = []
39
-
40
- metrics: Metric[] = []
36
+ stateMap = new Map<string, any>()
41
37
  }
@@ -0,0 +1,49 @@
1
+ export abstract class StateStorage<T> {
2
+ // TODO learn how to define single instance for all subclasses
3
+
4
+ protected constructor() {
5
+ //
6
+ }
7
+
8
+ abstract initValue(): T
9
+
10
+ key(): string {
11
+ return this.constructor.name
12
+ }
13
+
14
+ getOrRegister(): T {
15
+ let metricState: T = global.PROCESSOR_STATE.stateMap.get(this.key())
16
+ if (!metricState) {
17
+ metricState = this.initValue()
18
+ global.PROCESSOR_STATE.stateMap.set(this.key(), metricState)
19
+ }
20
+ return metricState
21
+ }
22
+ }
23
+
24
+ export abstract class MapStateStorage<T> extends StateStorage<Map<string, T>> {
25
+ initValue() {
26
+ return new Map<string, T>()
27
+ }
28
+
29
+ getValue(key: string): T | undefined {
30
+ const m = this.getOrRegister()
31
+ return m.get(key)
32
+ }
33
+
34
+ getValues(): T[] {
35
+ const m = this.getOrRegister()
36
+ return Array.from(m.values())
37
+ }
38
+
39
+ getOrSetValue(key: string, value: T): T {
40
+ const m = this.getOrRegister()
41
+ const oldValue = m.get(key)
42
+ if (oldValue) {
43
+ console.warn(key, 'has been registered twice, use the previous one')
44
+ return oldValue
45
+ }
46
+ m.set(key, value)
47
+ return value
48
+ }
49
+ }