@sentio/sdk 1.7.19 → 1.7.20
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/error.d.ts +5 -0
- package/lib/error.js +6 -4
- package/lib/error.js.map +1 -1
- package/lib/generic-processor.d.ts +3 -3
- package/lib/generic-processor.js +8 -11
- package/lib/generic-processor.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/meter.d.ts +1 -0
- package/lib/meter.js +7 -1
- package/lib/meter.js.map +1 -1
- package/lib/meter.test.d.ts +1 -0
- package/lib/meter.test.js +16 -0
- package/lib/meter.test.js.map +1 -0
- package/lib/numberish.js.map +1 -1
- package/lib/utils/convert.d.ts +3 -1
- package/lib/utils/convert.js +19 -1
- package/lib/utils/convert.js.map +1 -1
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +2 -1
- package/lib/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/src/error.ts +6 -4
- package/src/generic-processor.ts +8 -11
- package/src/index.ts +1 -0
- package/src/meter.test.ts +16 -0
- package/src/meter.ts +7 -0
- package/src/numberish.ts +2 -1
- package/src/utils/convert.ts +21 -1
- package/src/utils/index.ts +1 -1
package/lib/error.d.ts
CHANGED
package/lib/error.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformEtherError = void 0;
|
|
3
|
+
exports.transformEtherError = exports.EthersError = void 0;
|
|
4
4
|
const ethers_1 = require("ethers");
|
|
5
5
|
class EthersError extends Error {
|
|
6
|
-
|
|
6
|
+
e;
|
|
7
|
+
constructor(message, e) {
|
|
7
8
|
super(message);
|
|
8
|
-
this.stack = stack;
|
|
9
|
+
this.stack = e.stack;
|
|
9
10
|
}
|
|
10
11
|
toString() {
|
|
11
12
|
return this.message + '\n' + this.stack?.toString();
|
|
12
13
|
}
|
|
13
14
|
}
|
|
15
|
+
exports.EthersError = EthersError;
|
|
14
16
|
function transformEtherError(e, ctx) {
|
|
15
17
|
let msg = '';
|
|
16
18
|
// @ts-ignore expected error fields
|
|
@@ -28,7 +30,7 @@ function transformEtherError(e, ctx) {
|
|
|
28
30
|
msg = "jsonrpc eth_call return '0x' (likely contract not existed): " + JSON.stringify(e);
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
|
-
return new EthersError(msg, e
|
|
33
|
+
return new EthersError(msg, e);
|
|
32
34
|
}
|
|
33
35
|
if (e instanceof EthersError) {
|
|
34
36
|
return e;
|
package/lib/error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAEA,mCAA+B;AAE/B,
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAEA,mCAA+B;AAE/B,MAAa,WAAY,SAAQ,KAAK;IACpC,CAAC,CAAO;IAER,YAAY,OAAe,EAAE,CAAQ;QACnC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAA;IACtB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;IACrD,CAAC;CACF;AAXD,kCAWC;AAED,SAAgB,mBAAmB,CAAC,CAAQ,EAAE,GAAkC;IAC9E,IAAI,GAAG,GAAG,EAAE,CAAA;IACZ,mCAAmC;IACnC,IAAI,CAAC,CAAC,IAAI,KAAK,eAAM,CAAC,cAAc,EAAE;QACpC,mCAAmC;QACnC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,EAAE;YACnB,IAAI,GAAG,EAAE;gBACP,GAAG;oBACD,sEAAsE;wBACtE,GAAG,CAAC,OAAO;wBACX,IAAI;wBACJ,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;aACpB;iBAAM;gBACL,GAAG,GAAG,8DAA8D,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;aACzF;SACF;QACD,OAAO,IAAI,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;KAC/B;IAED,IAAI,CAAC,YAAY,WAAW,EAAE;QAC5B,OAAO,CAAC,CAAA;KACT;IAED,qCAAqC;IAErC,GAAG,GAAG,qBAAqB,GAAG,CAAC,CAAC,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;IACpE,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,CAAA;AACvB,CAAC;AA3BD,kDA2BC","sourcesContent":["// Transform error in more readable format\nimport { Context } from './context'\nimport { errors } from 'ethers'\n\nexport class EthersError extends Error {\n e: Error\n\n constructor(message: string, e: Error) {\n super(message)\n this.stack = e.stack\n }\n\n toString() {\n return this.message + '\\n' + this.stack?.toString()\n }\n}\n\nexport function transformEtherError(e: Error, ctx: Context<any, any> | undefined): Error {\n let msg = ''\n // @ts-ignore expected error fields\n if (e.code === errors.CALL_EXCEPTION) {\n // @ts-ignore expected error fields\n if (e.data === '0x') {\n if (ctx) {\n msg =\n \"jsonrpc eth_call return '0x' (likely contract not existed) at chain \" +\n ctx.chainId +\n ': ' +\n JSON.stringify(e)\n } else {\n msg = \"jsonrpc eth_call return '0x' (likely contract not existed): \" + JSON.stringify(e)\n }\n }\n return new EthersError(msg, e)\n }\n\n if (e instanceof EthersError) {\n return e\n }\n\n // TODO gracefully handle more errors\n\n msg = 'ethers call error\\n' + e.message + '\\n' + e.stack?.toString()\n return new Error(msg)\n}\n"]}
|
|
@@ -3,8 +3,8 @@ import { BoundContractView, ContractView } from './context';
|
|
|
3
3
|
import { BindOptions } from './bind-options';
|
|
4
4
|
import { BaseProcessor } from './base-processor';
|
|
5
5
|
export declare class GenericProcessor extends BaseProcessor<BaseContract, BoundContractView<BaseContract, ContractView<BaseContract>>> {
|
|
6
|
-
|
|
7
|
-
constructor(
|
|
6
|
+
eventABI: string[];
|
|
7
|
+
constructor(eventABI: string[], options: BindOptions);
|
|
8
8
|
protected CreateBoundContractView(): BoundContractView<BaseContract, ContractView<BaseContract>>;
|
|
9
|
-
static bind(
|
|
9
|
+
static bind(eventABI: string[] | string, options: BindOptions): GenericProcessor;
|
|
10
10
|
}
|
package/lib/generic-processor.js
CHANGED
|
@@ -7,27 +7,24 @@ const base_processor_1 = require("./base-processor");
|
|
|
7
7
|
const provider_1 = require("./provider");
|
|
8
8
|
const binds_1 = require("./binds");
|
|
9
9
|
class GenericProcessor extends base_processor_1.BaseProcessor {
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
10
|
+
eventABI;
|
|
11
|
+
constructor(eventABI, options) {
|
|
12
12
|
super(options);
|
|
13
|
-
this.
|
|
13
|
+
this.eventABI = eventABI;
|
|
14
14
|
}
|
|
15
15
|
CreateBoundContractView() {
|
|
16
|
-
const contract = new contracts_1.BaseContract(this.config.address, this.
|
|
16
|
+
const contract = new contracts_1.BaseContract(this.config.address, this.eventABI, (0, provider_1.getProvider)(this.config.network));
|
|
17
17
|
return new context_1.BoundContractView(new context_1.ContractView(contract));
|
|
18
18
|
}
|
|
19
|
-
static bind(
|
|
19
|
+
static bind(eventABI, options) {
|
|
20
20
|
const AbiName = 'Generic';
|
|
21
|
-
if (!Array.isArray(
|
|
22
|
-
|
|
21
|
+
if (!Array.isArray(eventABI)) {
|
|
22
|
+
eventABI = [eventABI];
|
|
23
23
|
}
|
|
24
|
-
// let processor = getProcessor(AbiName, options) as GenericProcessor;
|
|
25
|
-
// if (!processor) {
|
|
26
24
|
const finalOptions = Object.assign({}, options);
|
|
27
25
|
finalOptions.name = (0, binds_1.getContractName)(AbiName, options.name, options.address, options.network);
|
|
28
|
-
const processor = new GenericProcessor(
|
|
26
|
+
const processor = new GenericProcessor(eventABI, finalOptions);
|
|
29
27
|
(0, binds_1.addProcessor)(AbiName, options, processor);
|
|
30
|
-
// }
|
|
31
28
|
return processor;
|
|
32
29
|
}
|
|
33
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generic-processor.js","sourceRoot":"","sources":["../src/generic-processor.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,uCAA2D;AAE3D,qDAAgD;AAChD,yCAAwC;AACxC,mCAAuD;AAEvD,MAAa,gBAAiB,SAAQ,8BAGrC;IACC,QAAQ,CAAU;IAClB,YAAY,QAAkB,EAAE,OAAoB;QAClD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAES,uBAAuB;QAC/B,MAAM,QAAQ,GAAG,IAAI,wBAAY,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAA,sBAAW,EAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;QACvG,OAAO,IAAI,2BAAiB,CAAC,IAAI,sBAAY,CAAe,QAAQ,CAAC,CAAC,CAAA;IACxE,CAAC;IAEM,MAAM,CAAC,IAAI,CAAC,QAA2B,EAAE,OAAoB;QAClE,MAAM,OAAO,GAAG,SAAS,CAAA;QACzB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YAC5B,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAA;SACtB;QAED,
|
|
1
|
+
{"version":3,"file":"generic-processor.js","sourceRoot":"","sources":["../src/generic-processor.ts"],"names":[],"mappings":";;;AAAA,wDAAuD;AACvD,uCAA2D;AAE3D,qDAAgD;AAChD,yCAAwC;AACxC,mCAAuD;AAEvD,MAAa,gBAAiB,SAAQ,8BAGrC;IACC,QAAQ,CAAU;IAClB,YAAY,QAAkB,EAAE,OAAoB;QAClD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAES,uBAAuB;QAC/B,MAAM,QAAQ,GAAG,IAAI,wBAAY,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAA,sBAAW,EAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;QACvG,OAAO,IAAI,2BAAiB,CAAC,IAAI,sBAAY,CAAe,QAAQ,CAAC,CAAC,CAAA;IACxE,CAAC;IAEM,MAAM,CAAC,IAAI,CAAC,QAA2B,EAAE,OAAoB;QAClE,MAAM,OAAO,GAAG,SAAS,CAAA;QACzB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YAC5B,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAA;SACtB;QAED,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAA;QAC/C,YAAY,CAAC,IAAI,GAAG,IAAA,uBAAe,EAAC,OAAO,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;QAC5F,MAAM,SAAS,GAAG,IAAI,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QAC9D,IAAA,oBAAY,EAAC,OAAO,EAAE,OAAO,EAAE,SAAS,CAAC,CAAA;QACzC,OAAO,SAAS,CAAA;IAClB,CAAC;CACF;AA3BD,4CA2BC","sourcesContent":["import { BaseContract } from '@ethersproject/contracts'\nimport { BoundContractView, ContractView } from './context'\nimport { BindOptions } from './bind-options'\nimport { BaseProcessor } from './base-processor'\nimport { getProvider } from './provider'\nimport { addProcessor, getContractName } from './binds'\n\nexport class GenericProcessor extends BaseProcessor<\n BaseContract,\n BoundContractView<BaseContract, ContractView<BaseContract>>\n> {\n eventABI: string[]\n constructor(eventABI: string[], options: BindOptions) {\n super(options)\n this.eventABI = eventABI\n }\n\n protected CreateBoundContractView(): BoundContractView<BaseContract, ContractView<BaseContract>> {\n const contract = new BaseContract(this.config.address, this.eventABI, getProvider(this.config.network))\n return new BoundContractView(new ContractView<BaseContract>(contract))\n }\n\n public static bind(eventABI: string[] | string, options: BindOptions): GenericProcessor {\n const AbiName = 'Generic'\n if (!Array.isArray(eventABI)) {\n eventABI = [eventABI]\n }\n\n const finalOptions = Object.assign({}, options)\n finalOptions.name = getContractName(AbiName, options.name, options.address, options.network)\n const processor = new GenericProcessor(eventABI, finalOptions)\n addProcessor(AbiName, options, processor)\n return processor\n }\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { BindOptions, SolanaBindOptions } from './bind-options';
|
|
|
11
11
|
export { transformEtherError } from './error';
|
|
12
12
|
export { ProcessorState } from './processor-state';
|
|
13
13
|
export { BigNumber as BigDecimal } from 'bignumber.js';
|
|
14
|
+
export { EthersError } from './error';
|
|
14
15
|
export { getProcessor, addProcessor, getContractByABI, addContractByABI, getContractName } from './binds';
|
|
15
16
|
export * from './gen/processor/protos/processor';
|
|
16
17
|
export { SPLTokenProcessor } from './solana/builtin';
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.SPLTokenProcessor = exports.getContractName = exports.addContractByABI = exports.getContractByABI = exports.addProcessor = exports.getProcessor = exports.BigDecimal = exports.ProcessorState = exports.transformEtherError = exports.SolanaBindOptions = exports.BindOptions = exports.ContractNamer = exports.SolanaBaseProcessor = exports.DummyProvider = exports.setProvider = exports.getProvider = exports.Gauge = exports.Meter = exports.Counter = exports.ProcessorServiceImpl = exports.SolanaContext = exports.BoundContractView = exports.ContractView = exports.Context = exports.BaseProcessorTemplate = exports.GenericProcessor = exports.BaseProcessor = void 0;
|
|
17
|
+
exports.SPLTokenProcessor = exports.getContractName = exports.addContractByABI = exports.getContractByABI = exports.addProcessor = exports.getProcessor = exports.EthersError = exports.BigDecimal = exports.ProcessorState = exports.transformEtherError = exports.SolanaBindOptions = exports.BindOptions = exports.ContractNamer = exports.SolanaBaseProcessor = exports.DummyProvider = exports.setProvider = exports.getProvider = exports.Gauge = exports.Meter = exports.Counter = exports.ProcessorServiceImpl = exports.SolanaContext = exports.BoundContractView = exports.ContractView = exports.Context = exports.BaseProcessorTemplate = exports.GenericProcessor = exports.BaseProcessor = void 0;
|
|
18
18
|
var base_processor_1 = require("./base-processor");
|
|
19
19
|
Object.defineProperty(exports, "BaseProcessor", { enumerable: true, get: function () { return base_processor_1.BaseProcessor; } });
|
|
20
20
|
var generic_processor_1 = require("./generic-processor");
|
|
@@ -49,6 +49,8 @@ var processor_state_1 = require("./processor-state");
|
|
|
49
49
|
Object.defineProperty(exports, "ProcessorState", { enumerable: true, get: function () { return processor_state_1.ProcessorState; } });
|
|
50
50
|
var bignumber_js_1 = require("bignumber.js");
|
|
51
51
|
Object.defineProperty(exports, "BigDecimal", { enumerable: true, get: function () { return bignumber_js_1.BigNumber; } });
|
|
52
|
+
var error_2 = require("./error");
|
|
53
|
+
Object.defineProperty(exports, "EthersError", { enumerable: true, get: function () { return error_2.EthersError; } });
|
|
52
54
|
var binds_1 = require("./binds");
|
|
53
55
|
Object.defineProperty(exports, "getProcessor", { enumerable: true, get: function () { return binds_1.getProcessor; } });
|
|
54
56
|
Object.defineProperty(exports, "addProcessor", { enumerable: true, get: function () { return binds_1.addProcessor; } });
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAgD;AAAvC,+GAAA,aAAa,OAAA;AACtB,yDAAsD;AAA7C,qHAAA,gBAAgB,OAAA;AACzB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AAChD,uDAAwD;AAA/C,uHAAA,mBAAmB,OAAA;AAC5B,mDAAgD;AAAvC,+GAAA,aAAa,OAAA;AACtB,+CAA+D;AAAtD,2GAAA,WAAW,OAAA;AAAE,iHAAA,iBAAiB,OAAA;AACvC,iCAA6C;AAApC,4GAAA,mBAAmB,OAAA;AAC5B,qDAAkD;AAAzC,iHAAA,cAAc,OAAA;AACvB,6CAAsD;AAA7C,0GAAA,SAAS,OAAc;AAChC,iCAAqC;AAA5B,oGAAA,WAAW,OAAA;AAEpB,iCAAyG;AAAhG,qGAAA,YAAY,OAAA;AAAE,qGAAA,YAAY,OAAA;AAAE,yGAAA,gBAAgB,OAAA;AAAE,yGAAA,gBAAgB,OAAA;AAAE,wGAAA,eAAe,OAAA;AAExF,mEAAgD;AAEhD,4CAAoD;AAA3C,4GAAA,iBAAiB,OAAA","sourcesContent":["export { BaseProcessor } from './base-processor'\nexport { GenericProcessor } from './generic-processor'\nexport { BaseProcessorTemplate } from './base-processor-template'\nexport { Context, ContractView, BoundContractView, SolanaContext } from './context'\nexport { ProcessorServiceImpl } from './service'\nexport { Counter, Meter, Gauge } from './meter'\nexport { getProvider, setProvider, DummyProvider } from './provider'\nexport { SolanaBaseProcessor } from './solana-processor'\nexport { ContractNamer } from './contract-namer'\nexport { BindOptions, SolanaBindOptions } from './bind-options'\nexport { transformEtherError } from './error'\nexport { ProcessorState } from './processor-state'\nexport { BigNumber as BigDecimal } from 'bignumber.js'\nexport { EthersError } from './error'\n\nexport { getProcessor, addProcessor, getContractByABI, addContractByABI, getContractName } from './binds'\n\nexport * from './gen/processor/protos/processor'\n\nexport { SPLTokenProcessor } from './solana/builtin'\n"]}
|
package/lib/meter.d.ts
CHANGED
package/lib/meter.js
CHANGED
|
@@ -3,11 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Meter = exports.Gauge = exports.Counter = void 0;
|
|
6
|
+
exports.Meter = exports.Gauge = exports.Counter = exports.normalizeName = void 0;
|
|
7
7
|
const context_1 = require("./context");
|
|
8
8
|
const numberish_1 = require("./numberish");
|
|
9
9
|
const long_1 = __importDefault(require("long"));
|
|
10
|
+
function normalizeName(name) {
|
|
11
|
+
const regex = new RegExp('![_.a-zA-Z0-9]');
|
|
12
|
+
return name.slice(0, 100).replace(regex, '_');
|
|
13
|
+
}
|
|
14
|
+
exports.normalizeName = normalizeName;
|
|
10
15
|
function GetRecordMetaData(ctx, name, labels) {
|
|
16
|
+
name = normalizeName(name);
|
|
11
17
|
if (ctx instanceof context_1.Context) {
|
|
12
18
|
if (ctx.log) {
|
|
13
19
|
return {
|
package/lib/meter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"meter.js","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":";;;;;;AACA,uCAA8D;AAC9D,2CAAsD;AACtD,gDAAuB;AAEvB,SAAS,iBAAiB,CAAC,GAA+B,EAAE,IAAY,EAAE,MAAc;IACtF,IAAI,GAAG,YAAY,iBAAO,EAAE;QAC1B,IAAI,GAAG,CAAC,GAAG,EAAE;YACX,OAAO;gBACL,eAAe,EAAE,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO;gBACjD,WAAW,EAAE,cAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC;gBACvD,gBAAgB,EAAE,GAAG,CAAC,GAAG,CAAC,gBAAgB;gBAC1C,QAAQ,EAAE,GAAG,CAAC,GAAG,CAAC,QAAQ;gBAC1B,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE;gBAC/B,IAAI,EAAE,IAAI;gBACV,MAAM,EAAE,MAAM;aACf,CAAA;SACF;QACD,IAAI,GAAG,CAAC,KAAK,EAAE;YACb,OAAO;gBACL,eAAe,EA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A/BD,sBA+BC","sourcesContent":["import { RecordMetaData } from './gen/processor/protos/processor'\nimport { Context, EthContext, SolanaContext } from './context'\nimport { toMetricValue, Numberish } from './numberish'\nimport Long from 'long'\n\nfunction GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels: Labels): RecordMetaData {\n if (ctx instanceof Context) {\n if (ctx.log) {\n return {\n contractAddress: ctx.contract.rawContract.address,\n blockNumber: Long.fromNumber(ctx.log.blockNumber, true), // TODO need number type to be long\n transactionIndex: ctx.log.transactionIndex,\n logIndex: ctx.log.logIndex,\n chainId: ctx.chainId.toString(),\n name: name,\n labels: labels,\n }\n }\n if (ctx.block) {\n return {\n contractAddress: ctx.contract.rawContract.address,\n blockNumber: Long.fromNumber(ctx.block.number, true),\n transactionIndex: -1,\n logIndex: -1,\n chainId: ctx.chainId.toString(),\n name: name,\n labels: labels,\n }\n }\n } else if (ctx instanceof SolanaContext) {\n return {\n contractAddress: ctx.address,\n blockNumber: Long.ZERO, // TODO need number type to be long\n transactionIndex: 0,\n logIndex: 0,\n chainId: 'SOL:mainnet', // TODO set in context\n name: name,\n labels: labels,\n }\n }\n throw new Error(\"This can't happen\")\n}\n\nexport type Labels = { [key: string]: string }\n\nexport class Counter {\n private readonly ctx: EthContext | SolanaContext\n private readonly name: string\n\n constructor(name: string, ctx: EthContext | SolanaContext) {\n this.name = name\n this.ctx = ctx\n }\n\n add(value: Numberish, labels: Labels = {}) {\n this.record(value, labels, true)\n }\n\n sub(value: Numberish, labels: Labels = {}) {\n this.record(value, labels, false)\n }\n\n private record(value: Numberish, labels: Labels, add: boolean) {\n this.ctx.counters.push({\n metadata: GetRecordMetaData(this.ctx, this.name, labels),\n metricValue: toMetricValue(value),\n add: add,\n runtimeInfo: undefined,\n })\n }\n}\n\nexport class Gauge {\n private readonly name: string\n private readonly ctx: EthContext | SolanaContext\n\n constructor(name: string, ctx: EthContext | SolanaContext) {\n this.name = name\n this.ctx = ctx\n }\n\n record(value: Numberish, labels: Labels = {}) {\n this.ctx.gauges.push({\n metadata: GetRecordMetaData(this.ctx, this.name, labels),\n metricValue: toMetricValue(value),\n runtimeInfo: undefined,\n })\n }\n}\n\nexport class Meter {\n private readonly ctx: EthContext | SolanaContext\n\n // TODO is map necessary since we are sending request remotely?\n // counterMap = new Map<string, Counter>()\n // gaugeMap = new Map<string, Gauge>()\n\n constructor(ctx: EthContext | SolanaContext) {\n this.ctx = ctx\n }\n\n Counter(name: string): Counter {\n // let counter = this.counterMap.get(name)\n\n // if (!counter) {\n // counter = new Counter(name, this.ctx)\n // }\n // return counter\n\n return new Counter(name, this.ctx)\n }\n\n Gauge(name: string): Gauge {\n // let gauge = this.gaugeMap.get(name)\n //\n // if (!gauge) {\n // gauge = new Gauge(name, this.ctx)\n // }\n // return gauge\n return new Gauge(name, this.ctx)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"meter.js","sourceRoot":"","sources":["../src/meter.ts"],"names":[],"mappings":";;;;;;AACA,uCAA8D;AAC9D,2CAAsD;AACtD,gDAAuB;AAEvB,SAAgB,aAAa,CAAC,IAAY;IACxC,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,gBAAgB,CAAC,CAAA;IAC1C,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;AAC/C,CAAC;AAHD,sCAGC;AAED,SAAS,iBAAiB,CAAC,GAA+B,EAAE,IAAY,EAAE,MAAc;IACtF,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC,CAAA;IAE1B,IAAI,GAAG,YAAY,iBAAO,EAAE;QAC1B,IAAI,GAAG,CAAC,GAAG,EAAE;YACX,OAAO;gBACL,eAAe,EAAE,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO;gBACjD,WAAW,EAAE,cAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC;gBACvD,gBAAgB,EAAE,GA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AAO,IAAI,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;IACpC,CAAC;IAED,KAAK,CAAC,IAAY;QAChB,sCAAsC;QACtC,EAAE;QACF,gBAAgB;QAChB,wCAAwC;QACxC,IAAI;QACJ,eAAe;QACf,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;IAClC,CAAC;CACF;AA/BD,sBA+BC","sourcesContent":["import { RecordMetaData } from './gen/processor/protos/processor'\nimport { Context, EthContext, SolanaContext } from './context'\nimport { toMetricValue, Numberish } from './numberish'\nimport Long from 'long'\n\nexport function normalizeName(name: string) {\n const regex = new RegExp('![_.a-zA-Z0-9]')\n return name.slice(0, 100).replace(regex, '_')\n}\n\nfunction GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels: Labels): RecordMetaData {\n name = normalizeName(name)\n\n if (ctx instanceof Context) {\n if (ctx.log) {\n return {\n contractAddress: ctx.contract.rawContract.address,\n blockNumber: Long.fromNumber(ctx.log.blockNumber, true), // TODO need number type to be long\n transactionIndex: ctx.log.transactionIndex,\n logIndex: ctx.log.logIndex,\n chainId: ctx.chainId.toString(),\n name: name,\n labels: labels,\n }\n }\n if (ctx.block) {\n return {\n contractAddress: ctx.contract.rawContract.address,\n blockNumber: Long.fromNumber(ctx.block.number, true),\n transactionIndex: -1,\n logIndex: -1,\n chainId: ctx.chainId.toString(),\n name: name,\n labels: labels,\n }\n }\n } else if (ctx instanceof SolanaContext) {\n return {\n contractAddress: ctx.address,\n blockNumber: Long.ZERO, // TODO need number type to be long\n transactionIndex: 0,\n logIndex: 0,\n chainId: 'SOL:mainnet', // TODO set in context\n name: name,\n labels: labels,\n }\n }\n throw new Error(\"This can't happen\")\n}\n\nexport type Labels = { [key: string]: string }\n\nexport class Counter {\n private readonly ctx: EthContext | SolanaContext\n private readonly name: string\n\n constructor(name: string, ctx: EthContext | SolanaContext) {\n this.name = name\n this.ctx = ctx\n }\n\n add(value: Numberish, labels: Labels = {}) {\n this.record(value, labels, true)\n }\n\n sub(value: Numberish, labels: Labels = {}) {\n this.record(value, labels, false)\n }\n\n private record(value: Numberish, labels: Labels, add: boolean) {\n this.ctx.counters.push({\n metadata: GetRecordMetaData(this.ctx, this.name, labels),\n metricValue: toMetricValue(value),\n add: add,\n runtimeInfo: undefined,\n })\n }\n}\n\nexport class Gauge {\n private readonly name: string\n private readonly ctx: EthContext | SolanaContext\n\n constructor(name: string, ctx: EthContext | SolanaContext) {\n this.name = name\n this.ctx = ctx\n }\n\n record(value: Numberish, labels: Labels = {}) {\n this.ctx.gauges.push({\n metadata: GetRecordMetaData(this.ctx, this.name, labels),\n metricValue: toMetricValue(value),\n runtimeInfo: undefined,\n })\n }\n}\n\nexport class Meter {\n private readonly ctx: EthContext | SolanaContext\n\n // TODO is map necessary since we are sending request remotely?\n // counterMap = new Map<string, Counter>()\n // gaugeMap = new Map<string, Gauge>()\n\n constructor(ctx: EthContext | SolanaContext) {\n this.ctx = ctx\n }\n\n Counter(name: string): Counter {\n // let counter = this.counterMap.get(name)\n\n // if (!counter) {\n // counter = new Counter(name, this.ctx)\n // }\n // return counter\n\n return new Counter(name, this.ctx)\n }\n\n Gauge(name: string): Gauge {\n // let gauge = this.gaugeMap.get(name)\n //\n // if (!gauge) {\n // gauge = new Gauge(name, this.ctx)\n // }\n // return gauge\n return new Gauge(name, this.ctx)\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chai_1 = require("chai");
|
|
4
|
+
const meter_1 = require("./meter");
|
|
5
|
+
describe('meter tests', () => {
|
|
6
|
+
test('test normalization ', async () => {
|
|
7
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('abc') === 'abc');
|
|
8
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('a-b-c') === 'a-b-c');
|
|
9
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('_a-B-1.') === '_a-B-1.');
|
|
10
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('a/b\\c\n') === 'abc');
|
|
11
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('abc abc') === 'abc_abc');
|
|
12
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('*&~') === '___');
|
|
13
|
+
(0, chai_1.expect)((0, meter_1.normalizeName)('x'.repeat(200)).length === 100);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=meter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meter.test.js","sourceRoot":"","sources":["../src/meter.test.ts"],"names":[],"mappings":";;AAAA,+BAA6B;AAC7B,mCAAuC;AAEvC,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,IAAI,CAAC,qBAAqB,EAAE,KAAK,IAAI,EAAE;QACrC,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAA;QACtC,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAA;QAC1C,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,SAAS,CAAC,KAAK,SAAS,CAAC,CAAA;QAE9C,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,UAAU,CAAC,KAAK,KAAK,CAAC,CAAA;QAC3C,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,SAAS,CAAC,KAAK,SAAS,CAAC,CAAA;QAC9C,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAA;QAEtC,IAAA,aAAM,EAAC,IAAA,qBAAa,EAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,KAAK,GAAG,CAAC,CAAA;IACvD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { expect } from 'chai'\nimport { normalizeName } from './meter'\n\ndescribe('meter tests', () => {\n test('test normalization ', async () => {\n expect(normalizeName('abc') === 'abc')\n expect(normalizeName('a-b-c') === 'a-b-c')\n expect(normalizeName('_a-B-1.') === '_a-B-1.')\n\n expect(normalizeName('a/b\\\\c\\n') === 'abc')\n expect(normalizeName('abc abc') === 'abc_abc')\n expect(normalizeName('*&~') === '___')\n\n expect(normalizeName('x'.repeat(200)).length === 100)\n })\n})\n"]}
|
package/lib/numberish.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numberish.js","sourceRoot":"","sources":["../src/numberish.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAClC,gEAA0E;AAC1E,wBAA8B;AAC9B,kDAA0C;AAI1C,SAAgB,aAAa,CAAC,KAAgB;IAC5C,IAAI,KAAK,YAAY,kBAAS,EAAE;QAC9B,OAAO,uBAAW,CAAC,WAAW,CAAC;YAC7B,UAAU,EAAE,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;SAC3C,CAAC,CAAA;KACH;IACD,IAAI,KAAK,YAAY,aAAU,EAAE;QAC/B,uCAAuC;QACvC,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE;YACrB,OAAO,uBAAW,CAAC,WAAW,CAAC;gBAC7B,UAAU,EAAE,sBAAsB,CAAC,KAAK,CAAC;aAC1C,CAAC,CAAA;SACH;aAAM;YACL,OAAO,uBAAW,CAAC,WAAW,CAAC;gBAC7B,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAE,sBAAsB;aACrD,CAAC,CAAA;SACH;KACF;IACD,IAAI,WAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;QAClB,OAAO,uBAAW,CAAC,WAAW,CAAC;YAC7B,UAAU,EAAE,cAAc,CAAC,KAAK,CAAC;SAClC,CAAC,CAAA;KACH;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ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AQ,EAAE,CAAC,CAAA;KACrC;IACD,OAAO,eAAe,CAAC,CAAC,CAAC,CAAA;IAEzB,uCAAuC;IACvC,kBAAkB;IAClB,wBAAwB;IACxB,aAAa;IACb,0BAA0B;IAC1B,IAAI;IACJ,EAAE;IACF,WAAW;IACX,sDAAsD;IACtD,IAAI;AACN,CAAC;AAtBD,oCAsBC;AAED,SAAS,eAAe,CAAC,GAAW,EAAE,QAAiB;IACrD,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE;QACxB,GAAG,GAAG,GAAG,GAAG,GAAG,CAAA;KAChB;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAEtC,OAAO;QACL,QAAQ,EAAE,QAAQ;QAClB,IAAI,EAAE,IAAI,UAAU,CAAC,MAAM,CAAC;KAC7B,CAAA;AACH,CAAC","sourcesContent":["import { BigNumber } from 'ethers'\nimport { BigInteger, MetricValue } from './gen/processor/protos/processor'\nimport { BigDecimal } from '.'\nimport { BN } from '@project-serum/anchor'\n\nexport type Numberish = number | BigNumber | bigint | BigDecimal\n\nexport function toMetricValue(value: Numberish): MetricValue {\n if (value instanceof BigNumber) {\n return MetricValue.fromPartial({\n bigInteger: toBigInteger(value.toBigInt()),\n })\n }\n if (value instanceof BigDecimal) {\n // Carefully consider the use case here\n if (value.isInteger()) {\n return MetricValue.fromPartial({\n bigInteger: bigDecimalToBigInteger(value),\n })\n } else {\n return MetricValue.fromPartial({\n bigDecimal: value.toString(), // e.g. -7.350918e-428\n })\n }\n }\n if (BN.isBN(value)) {\n return MetricValue.fromPartial({\n bigInteger: bnToBigInteger(value),\n })\n }\n if (typeof value === 'bigint' || Number.isInteger(value)) {\n return MetricValue.fromPartial({\n bigInteger: toBigInteger(value),\n })\n }\n\n return MetricValue.fromPartial({\n doubleValue: Number(value),\n })\n}\n\nfunction bigDecimalToBigInteger(a: BigDecimal): BigInteger {\n const negative = a.isNegative()\n if (negative) {\n a = a.abs()\n }\n return hexToBigInteger(a.toString(16), negative)\n}\n\nfunction bnToBigInteger(a: BN): BigInteger {\n const negative = a.isNeg()\n if (negative) {\n a = a.abs()\n }\n return hexToBigInteger(a.toString(16), negative)\n}\n\nfunction intToBigInteger(a: bigint | number): BigInteger {\n const negative = a < 0\n if (negative) {\n a = -a\n }\n return hexToBigInteger(a.toString(16), negative)\n}\n\nexport function toBigInteger(a: Numberish): BigInteger {\n if (a instanceof BigDecimal) {\n return bigDecimalToBigInteger(a)\n }\n if (a instanceof BN) {\n return bnToBigInteger(a)\n }\n if (a instanceof BigNumber) {\n return intToBigInteger(a.toBigInt())\n }\n return intToBigInteger(a)\n\n // Following code is actually very slow\n // while (a > 0) {\n // const d = a & 0xffn\n // a >>= 8n\n // value.push(Number(d))\n // }\n //\n // return {\n // negative, value: new Uint8Array(value.reverse()),\n // }\n}\n\nfunction hexToBigInteger(hex: string, negative: boolean): BigInteger {\n if (hex.length % 2 === 1) {\n hex = '0' + hex\n }\n const buffer = Buffer.from(hex, 'hex')\n\n return {\n negative: negative,\n data: new Uint8Array(buffer),\n }\n}\n"]}
|
package/lib/utils/convert.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import { BigNumber } from 'ethers';
|
|
2
|
-
import { BigDecimal } from '@sentio/sdk';
|
|
2
|
+
import { BigDecimal, MetricValue } from '@sentio/sdk';
|
|
3
|
+
import { Numberish } from '../numberish';
|
|
3
4
|
export declare function toBigDecimal(n: BigNumber): BigDecimal;
|
|
5
|
+
export declare function metricValueToNumberish(v: MetricValue): Numberish;
|
package/lib/utils/convert.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toBigDecimal = void 0;
|
|
3
|
+
exports.metricValueToNumberish = exports.toBigDecimal = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
4
5
|
const sdk_1 = require("@sentio/sdk");
|
|
5
6
|
function toBigDecimal(n) {
|
|
6
7
|
return new sdk_1.BigDecimal(n.toString());
|
|
7
8
|
}
|
|
8
9
|
exports.toBigDecimal = toBigDecimal;
|
|
10
|
+
function metricValueToNumberish(v) {
|
|
11
|
+
if (v.doubleValue) {
|
|
12
|
+
return v.doubleValue;
|
|
13
|
+
}
|
|
14
|
+
if (v.bigInteger) {
|
|
15
|
+
const bn = ethers_1.BigNumber.from(v.bigInteger.data);
|
|
16
|
+
if (v.bigInteger.negative) {
|
|
17
|
+
return ethers_1.BigNumber.from(0).sub(bn);
|
|
18
|
+
}
|
|
19
|
+
return bn;
|
|
20
|
+
}
|
|
21
|
+
if (v.bigDecimal) {
|
|
22
|
+
return new sdk_1.BigDecimal(v.bigDecimal);
|
|
23
|
+
}
|
|
24
|
+
throw Error("MetricValue doesn't contain any of possible value");
|
|
25
|
+
}
|
|
26
|
+
exports.metricValueToNumberish = metricValueToNumberish;
|
|
9
27
|
//# sourceMappingURL=convert.js.map
|
package/lib/utils/convert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.js","sourceRoot":"","sources":["../../src/utils/convert.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"convert.js","sourceRoot":"","sources":["../../src/utils/convert.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAClC,qCAAqD;AAGrD,SAAgB,YAAY,CAAC,CAAY;IACvC,OAAO,IAAI,gBAAU,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;AACrC,CAAC;AAFD,oCAEC;AAED,SAAgB,sBAAsB,CAAC,CAAc;IACnD,IAAI,CAAC,CAAC,WAAW,EAAE;QACjB,OAAO,CAAC,CAAC,WAAW,CAAA;KACrB;IACD,IAAI,CAAC,CAAC,UAAU,EAAE;QAChB,MAAM,EAAE,GAAG,kBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAC5C,IAAI,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE;YACzB,OAAO,kBAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;SACjC;QACD,OAAO,EAAE,CAAA;KACV;IAED,IAAI,CAAC,CAAC,UAAU,EAAE;QAChB,OAAO,IAAI,gBAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;KACpC;IAED,MAAM,KAAK,CAAC,mDAAmD,CAAC,CAAA;AAClE,CAAC;AAjBD,wDAiBC","sourcesContent":["import { BigNumber } from 'ethers'\nimport { BigDecimal, MetricValue } from '@sentio/sdk'\nimport { Numberish } from '../numberish'\n\nexport function toBigDecimal(n: BigNumber) {\n return new BigDecimal(n.toString())\n}\n\nexport function metricValueToNumberish(v: MetricValue): Numberish {\n if (v.doubleValue) {\n return v.doubleValue\n }\n if (v.bigInteger) {\n const bn = BigNumber.from(v.bigInteger.data)\n if (v.bigInteger.negative) {\n return BigNumber.from(0).sub(bn)\n }\n return bn\n }\n\n if (v.bigDecimal) {\n return new BigDecimal(v.bigDecimal)\n }\n\n throw Error(\"MetricValue doesn't contain any of possible value\")\n}\n"]}
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { getChainName } from './chainmap';
|
|
2
|
-
export { toBigDecimal } from './convert';
|
|
2
|
+
export { toBigDecimal, metricValueToNumberish } from './convert';
|
package/lib/utils/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toBigDecimal = exports.getChainName = void 0;
|
|
3
|
+
exports.metricValueToNumberish = exports.toBigDecimal = exports.getChainName = void 0;
|
|
4
4
|
var chainmap_1 = require("./chainmap");
|
|
5
5
|
Object.defineProperty(exports, "getChainName", { enumerable: true, get: function () { return chainmap_1.getChainName; } });
|
|
6
6
|
var convert_1 = require("./convert");
|
|
7
7
|
Object.defineProperty(exports, "toBigDecimal", { enumerable: true, get: function () { return convert_1.toBigDecimal; } });
|
|
8
|
+
Object.defineProperty(exports, "metricValueToNumberish", { enumerable: true, get: function () { return convert_1.metricValueToNumberish; } });
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/lib/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,uCAAyC;AAAhC,wGAAA,YAAY,OAAA;AACrB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,uCAAyC;AAAhC,wGAAA,YAAY,OAAA;AACrB,qCAAgE;AAAvD,uGAAA,YAAY,OAAA;AAAE,iHAAA,sBAAsB,OAAA","sourcesContent":["export { getChainName } from './chainmap'\nexport { toBigDecimal, metricValueToNumberish } from './convert'\n"]}
|
package/package.json
CHANGED
package/src/error.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
import { Context } from './context'
|
|
3
3
|
import { errors } from 'ethers'
|
|
4
4
|
|
|
5
|
-
class EthersError extends Error {
|
|
6
|
-
|
|
5
|
+
export class EthersError extends Error {
|
|
6
|
+
e: Error
|
|
7
|
+
|
|
8
|
+
constructor(message: string, e: Error) {
|
|
7
9
|
super(message)
|
|
8
|
-
this.stack = stack
|
|
10
|
+
this.stack = e.stack
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
toString() {
|
|
@@ -29,7 +31,7 @@ export function transformEtherError(e: Error, ctx: Context<any, any> | undefined
|
|
|
29
31
|
msg = "jsonrpc eth_call return '0x' (likely contract not existed): " + JSON.stringify(e)
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
|
-
return new EthersError(msg, e
|
|
34
|
+
return new EthersError(msg, e)
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
if (e instanceof EthersError) {
|
package/src/generic-processor.ts
CHANGED
|
@@ -9,30 +9,27 @@ export class GenericProcessor extends BaseProcessor<
|
|
|
9
9
|
BaseContract,
|
|
10
10
|
BoundContractView<BaseContract, ContractView<BaseContract>>
|
|
11
11
|
> {
|
|
12
|
-
|
|
13
|
-
constructor(
|
|
12
|
+
eventABI: string[]
|
|
13
|
+
constructor(eventABI: string[], options: BindOptions) {
|
|
14
14
|
super(options)
|
|
15
|
-
this.
|
|
15
|
+
this.eventABI = eventABI
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
protected CreateBoundContractView(): BoundContractView<BaseContract, ContractView<BaseContract>> {
|
|
19
|
-
const contract = new BaseContract(this.config.address, this.
|
|
19
|
+
const contract = new BaseContract(this.config.address, this.eventABI, getProvider(this.config.network))
|
|
20
20
|
return new BoundContractView(new ContractView<BaseContract>(contract))
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
public static bind(
|
|
23
|
+
public static bind(eventABI: string[] | string, options: BindOptions): GenericProcessor {
|
|
24
24
|
const AbiName = 'Generic'
|
|
25
|
-
if (!Array.isArray(
|
|
26
|
-
|
|
25
|
+
if (!Array.isArray(eventABI)) {
|
|
26
|
+
eventABI = [eventABI]
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
// let processor = getProcessor(AbiName, options) as GenericProcessor;
|
|
30
|
-
// if (!processor) {
|
|
31
29
|
const finalOptions = Object.assign({}, options)
|
|
32
30
|
finalOptions.name = getContractName(AbiName, options.name, options.address, options.network)
|
|
33
|
-
const processor = new GenericProcessor(
|
|
31
|
+
const processor = new GenericProcessor(eventABI, finalOptions)
|
|
34
32
|
addProcessor(AbiName, options, processor)
|
|
35
|
-
// }
|
|
36
33
|
return processor
|
|
37
34
|
}
|
|
38
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { BindOptions, SolanaBindOptions } from './bind-options'
|
|
|
11
11
|
export { transformEtherError } from './error'
|
|
12
12
|
export { ProcessorState } from './processor-state'
|
|
13
13
|
export { BigNumber as BigDecimal } from 'bignumber.js'
|
|
14
|
+
export { EthersError } from './error'
|
|
14
15
|
|
|
15
16
|
export { getProcessor, addProcessor, getContractByABI, addContractByABI, getContractName } from './binds'
|
|
16
17
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { expect } from 'chai'
|
|
2
|
+
import { normalizeName } from './meter'
|
|
3
|
+
|
|
4
|
+
describe('meter tests', () => {
|
|
5
|
+
test('test normalization ', async () => {
|
|
6
|
+
expect(normalizeName('abc') === 'abc')
|
|
7
|
+
expect(normalizeName('a-b-c') === 'a-b-c')
|
|
8
|
+
expect(normalizeName('_a-B-1.') === '_a-B-1.')
|
|
9
|
+
|
|
10
|
+
expect(normalizeName('a/b\\c\n') === 'abc')
|
|
11
|
+
expect(normalizeName('abc abc') === 'abc_abc')
|
|
12
|
+
expect(normalizeName('*&~') === '___')
|
|
13
|
+
|
|
14
|
+
expect(normalizeName('x'.repeat(200)).length === 100)
|
|
15
|
+
})
|
|
16
|
+
})
|
package/src/meter.ts
CHANGED
|
@@ -3,7 +3,14 @@ import { Context, EthContext, SolanaContext } from './context'
|
|
|
3
3
|
import { toMetricValue, Numberish } from './numberish'
|
|
4
4
|
import Long from 'long'
|
|
5
5
|
|
|
6
|
+
export function normalizeName(name: string) {
|
|
7
|
+
const regex = new RegExp('![_.a-zA-Z0-9]')
|
|
8
|
+
return name.slice(0, 100).replace(regex, '_')
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
function GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels: Labels): RecordMetaData {
|
|
12
|
+
name = normalizeName(name)
|
|
13
|
+
|
|
7
14
|
if (ctx instanceof Context) {
|
|
8
15
|
if (ctx.log) {
|
|
9
16
|
return {
|
package/src/numberish.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { BigInteger, MetricValue } from './gen/processor/protos/processor'
|
|
|
3
3
|
import { BigDecimal } from '.'
|
|
4
4
|
import { BN } from '@project-serum/anchor'
|
|
5
5
|
|
|
6
|
-
export type Numberish = number | BigNumber | bigint | BigDecimal
|
|
6
|
+
export type Numberish = number | BigNumber | bigint | BigDecimal
|
|
7
7
|
|
|
8
8
|
export function toMetricValue(value: Numberish): MetricValue {
|
|
9
9
|
if (value instanceof BigNumber) {
|
|
@@ -38,6 +38,7 @@ export function toMetricValue(value: Numberish): MetricValue {
|
|
|
38
38
|
doubleValue: Number(value),
|
|
39
39
|
})
|
|
40
40
|
}
|
|
41
|
+
|
|
41
42
|
function bigDecimalToBigInteger(a: BigDecimal): BigInteger {
|
|
42
43
|
const negative = a.isNegative()
|
|
43
44
|
if (negative) {
|
package/src/utils/convert.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { BigNumber } from 'ethers'
|
|
2
|
-
import { BigDecimal } from '@sentio/sdk'
|
|
2
|
+
import { BigDecimal, MetricValue } from '@sentio/sdk'
|
|
3
|
+
import { Numberish } from '../numberish'
|
|
3
4
|
|
|
4
5
|
export function toBigDecimal(n: BigNumber) {
|
|
5
6
|
return new BigDecimal(n.toString())
|
|
6
7
|
}
|
|
8
|
+
|
|
9
|
+
export function metricValueToNumberish(v: MetricValue): Numberish {
|
|
10
|
+
if (v.doubleValue) {
|
|
11
|
+
return v.doubleValue
|
|
12
|
+
}
|
|
13
|
+
if (v.bigInteger) {
|
|
14
|
+
const bn = BigNumber.from(v.bigInteger.data)
|
|
15
|
+
if (v.bigInteger.negative) {
|
|
16
|
+
return BigNumber.from(0).sub(bn)
|
|
17
|
+
}
|
|
18
|
+
return bn
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (v.bigDecimal) {
|
|
22
|
+
return new BigDecimal(v.bigDecimal)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw Error("MetricValue doesn't contain any of possible value")
|
|
26
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { getChainName } from './chainmap'
|
|
2
|
-
export { toBigDecimal } from './convert'
|
|
2
|
+
export { toBigDecimal, metricValueToNumberish } from './convert'
|