@chainlink/external-adapter-framework 0.0.15 → 0.0.16

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 (183) hide show
  1. package/adapter.js +128 -0
  2. package/background-executor.js +45 -0
  3. package/cache/factory.js +58 -0
  4. package/cache/index.js +173 -0
  5. package/cache/local.js +83 -0
  6. package/cache/metrics.js +120 -0
  7. package/cache/redis.js +100 -0
  8. package/chainlink-external-adapter-framework-v0.0.6.tgz +0 -0
  9. package/config/index.js +366 -0
  10. package/config/provider-limits.js +74 -0
  11. package/examples/bank-frick/accounts.js +192 -0
  12. package/examples/bank-frick/config/index.js +54 -0
  13. package/examples/bank-frick/index.js +15 -0
  14. package/examples/bank-frick/util.js +39 -0
  15. package/examples/coingecko/src/config/index.js +13 -0
  16. package/examples/coingecko/src/config/overrides.json +10826 -0
  17. package/examples/coingecko/src/cryptoUtils.js +41 -0
  18. package/examples/coingecko/src/endpoint/coins.js +33 -0
  19. package/examples/coingecko/src/endpoint/crypto-marketcap.js +46 -0
  20. package/examples/coingecko/src/endpoint/crypto-volume.js +46 -0
  21. package/examples/coingecko/src/endpoint/crypto.js +47 -0
  22. package/examples/coingecko/src/endpoint/dominance.js +26 -0
  23. package/examples/coingecko/src/endpoint/global-marketcap.js +26 -0
  24. package/examples/coingecko/src/endpoint/index.js +15 -0
  25. package/examples/coingecko/src/globalUtils.js +48 -0
  26. package/examples/coingecko/src/index.js +14 -0
  27. package/examples/coingecko/test/e2e/adapter.test.js +262 -0
  28. package/examples/coingecko/test/integration/adapter.test.js +264 -0
  29. package/examples/coingecko/test/integration/capturedRequests.json +1 -0
  30. package/examples/coingecko/test/integration/fixtures.js +41 -0
  31. package/examples/coingecko-old/batch-warming.js +53 -0
  32. package/examples/coingecko-old/index.js +11 -0
  33. package/examples/coingecko-old/rest.js +51 -0
  34. package/examples/ncfx/config/index.js +15 -0
  35. package/examples/ncfx/index.js +11 -0
  36. package/examples/ncfx/websocket.js +73 -0
  37. package/index.js +127 -0
  38. package/metrics/constants.js +25 -0
  39. package/metrics/index.js +122 -0
  40. package/metrics/util.js +9 -0
  41. package/package.json +5 -15
  42. package/rate-limiting/background/fixed-frequency.js +35 -0
  43. package/rate-limiting/index.js +84 -0
  44. package/rate-limiting/metrics.js +44 -0
  45. package/rate-limiting/request/simple-counting.js +62 -0
  46. package/test.js +6 -0
  47. package/transports/batch-warming.js +101 -0
  48. package/transports/index.js +87 -0
  49. package/transports/metrics.js +105 -0
  50. package/transports/rest.js +138 -0
  51. package/transports/util.js +86 -0
  52. package/transports/websocket.js +166 -0
  53. package/util/index.js +35 -0
  54. package/util/logger.js +62 -0
  55. package/util/recordRequests.js +45 -0
  56. package/util/request.js +2 -0
  57. package/util/subscription-set/expiring-sorted-set.js +47 -0
  58. package/util/subscription-set/subscription-set.js +19 -0
  59. package/util/test-payload-loader.js +83 -0
  60. package/validation/error.js +79 -0
  61. package/validation/index.js +91 -0
  62. package/validation/input-params.js +30 -0
  63. package/validation/override-functions.js +40 -0
  64. package/validation/overrideFunctions.js +40 -0
  65. package/validation/preset-tokens.json +23 -0
  66. package/validation/validator.js +303 -0
  67. package/.c8rc.json +0 -3
  68. package/.eslintignore +0 -10
  69. package/.eslintrc.js +0 -96
  70. package/.github/README.MD +0 -42
  71. package/.github/actions/setup/action.yaml +0 -13
  72. package/.github/workflows/label.yaml +0 -39
  73. package/.github/workflows/main.yaml +0 -39
  74. package/.github/workflows/publish.yaml +0 -17
  75. package/.prettierignore +0 -13
  76. package/.yarnrc +0 -0
  77. package/README.md +0 -103
  78. package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +0 -82953
  79. package/dist/examples/coingecko/test/integration/adapter.test.ts.js +0 -91672
  80. package/dist/main.js +0 -72703
  81. package/docker-compose.yaml +0 -35
  82. package/env.sh +0 -54
  83. package/env2.sh +0 -55
  84. package/jest.config.js +0 -5
  85. package/publish.sh +0 -0
  86. package/src/adapter.ts +0 -263
  87. package/src/background-executor.ts +0 -52
  88. package/src/cache/factory.ts +0 -26
  89. package/src/cache/index.ts +0 -258
  90. package/src/cache/local.ts +0 -73
  91. package/src/cache/metrics.ts +0 -112
  92. package/src/cache/redis.ts +0 -93
  93. package/src/config/index.ts +0 -517
  94. package/src/config/provider-limits.ts +0 -127
  95. package/src/examples/bank-frick/README.MD +0 -10
  96. package/src/examples/bank-frick/accounts.ts +0 -246
  97. package/src/examples/bank-frick/config/index.ts +0 -53
  98. package/src/examples/bank-frick/index.ts +0 -13
  99. package/src/examples/bank-frick/types.d.ts +0 -38
  100. package/src/examples/bank-frick/util.ts +0 -55
  101. package/src/examples/coingecko/src/config/index.ts +0 -12
  102. package/src/examples/coingecko/src/config/overrides.json +0 -10826
  103. package/src/examples/coingecko/src/cryptoUtils.ts +0 -88
  104. package/src/examples/coingecko/src/endpoint/coins.ts +0 -54
  105. package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +0 -66
  106. package/src/examples/coingecko/src/endpoint/crypto-volume.ts +0 -66
  107. package/src/examples/coingecko/src/endpoint/crypto.ts +0 -63
  108. package/src/examples/coingecko/src/endpoint/dominance.ts +0 -40
  109. package/src/examples/coingecko/src/endpoint/global-marketcap.ts +0 -40
  110. package/src/examples/coingecko/src/endpoint/index.ts +0 -6
  111. package/src/examples/coingecko/src/globalUtils.ts +0 -78
  112. package/src/examples/coingecko/src/index.ts +0 -17
  113. package/src/examples/coingecko/test/e2e/adapter.test.ts +0 -278
  114. package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +0 -15
  115. package/src/examples/coingecko/test/integration/adapter.test.ts +0 -281
  116. package/src/examples/coingecko/test/integration/capturedRequests.json +0 -1
  117. package/src/examples/coingecko/test/integration/fixtures.ts +0 -42
  118. package/src/examples/coingecko-old/batch-warming.ts +0 -79
  119. package/src/examples/coingecko-old/index.ts +0 -9
  120. package/src/examples/coingecko-old/rest.ts +0 -77
  121. package/src/examples/ncfx/config/index.ts +0 -12
  122. package/src/examples/ncfx/index.ts +0 -9
  123. package/src/examples/ncfx/websocket.ts +0 -99
  124. package/src/index.ts +0 -149
  125. package/src/metrics/constants.ts +0 -23
  126. package/src/metrics/index.ts +0 -115
  127. package/src/metrics/util.ts +0 -18
  128. package/src/rate-limiting/background/fixed-frequency.ts +0 -45
  129. package/src/rate-limiting/index.ts +0 -100
  130. package/src/rate-limiting/metrics.ts +0 -18
  131. package/src/rate-limiting/request/simple-counting.ts +0 -76
  132. package/src/transports/batch-warming.ts +0 -127
  133. package/src/transports/index.ts +0 -152
  134. package/src/transports/metrics.ts +0 -95
  135. package/src/transports/rest.ts +0 -168
  136. package/src/transports/util.ts +0 -63
  137. package/src/transports/websocket.ts +0 -245
  138. package/src/util/index.ts +0 -23
  139. package/src/util/logger.ts +0 -69
  140. package/src/util/recordRequests.ts +0 -47
  141. package/src/util/request.ts +0 -117
  142. package/src/util/subscription-set/expiring-sorted-set.ts +0 -54
  143. package/src/util/subscription-set/subscription-set.ts +0 -35
  144. package/src/util/test-payload-loader.ts +0 -87
  145. package/src/validation/error.ts +0 -116
  146. package/src/validation/index.ts +0 -110
  147. package/src/validation/input-params.ts +0 -45
  148. package/src/validation/override-functions.ts +0 -44
  149. package/src/validation/overrideFunctions.ts +0 -44
  150. package/src/validation/preset-tokens.json +0 -23
  151. package/src/validation/validator.ts +0 -384
  152. package/test/adapter.test.ts +0 -27
  153. package/test/background-executor.test.ts +0 -108
  154. package/test/cache/cache-key.test.ts +0 -114
  155. package/test/cache/helper.ts +0 -100
  156. package/test/cache/local.test.ts +0 -54
  157. package/test/cache/redis.test.ts +0 -89
  158. package/test/correlation.test.ts +0 -114
  159. package/test/index.test.ts +0 -37
  160. package/test/metrics/feed-id.test.ts +0 -38
  161. package/test/metrics/helper.ts +0 -14
  162. package/test/metrics/labels.test.ts +0 -36
  163. package/test/metrics/metrics.test.ts +0 -267
  164. package/test/metrics/redis-metrics.test.ts +0 -113
  165. package/test/metrics/warmer-metrics.test.ts +0 -193
  166. package/test/metrics/ws-metrics.test.ts +0 -225
  167. package/test/rate-limit-config.test.ts +0 -242
  168. package/test/smoke/smoke.test.ts +0 -166
  169. package/test/smoke/test-payload-fail.json +0 -3
  170. package/test/smoke/test-payload.js +0 -22
  171. package/test/smoke/test-payload.json +0 -7
  172. package/test/transports/batch.test.ts +0 -466
  173. package/test/transports/rest.test.ts +0 -242
  174. package/test/transports/websocket.test.ts +0 -183
  175. package/test/tsconfig.json +0 -5
  176. package/test/util.ts +0 -77
  177. package/test/validation.test.ts +0 -178
  178. package/test.sh +0 -20
  179. package/test2.sh +0 -2
  180. package/tsconfig.json +0 -28
  181. package/typedoc.json +0 -6
  182. package/webpack.config.js +0 -57
  183. package/yarn-error.log +0 -3778
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorCatchingMiddleware = exports.validatorMiddleware = void 0;
4
+ const cache_1 = require("../cache");
5
+ const util_1 = require("../metrics/util");
6
+ const util_2 = require("../util");
7
+ const error_1 = require("./error");
8
+ const validator_1 = require("./validator");
9
+ const override_functions_1 = require("./override-functions");
10
+ const errorCatcherLogger = (0, util_2.makeLogger)('ErrorCatchingMiddleware');
11
+ const validatorMiddleware = (adapter) => (req, reply, done) => {
12
+ if (req.headers['content-type'] !== 'application/json') {
13
+ throw new error_1.AdapterInputError({
14
+ message: 'Content type not "application/json", returning 400',
15
+ statusCode: 400,
16
+ });
17
+ }
18
+ if (!req.body) {
19
+ throw new error_1.AdapterInputError({
20
+ message: 'Body not present in adapter request, returning 400',
21
+ statusCode: 400,
22
+ });
23
+ }
24
+ // Make endpoints case insensitive
25
+ const endpointParam = req.body.endpoint?.toLowerCase() ||
26
+ req.body.data?.endpoint?.toLowerCase() ||
27
+ adapter.defaultEndpoint;
28
+ if (!endpointParam) {
29
+ throw new error_1.AdapterInputError({
30
+ message: `Request body does not specify an endpoint, and there is no default endpoint configured for this adapter.`,
31
+ statusCode: 400,
32
+ });
33
+ }
34
+ const endpoint = adapter.endpointsMap[endpointParam];
35
+ if (!endpoint) {
36
+ throw new error_1.AdapterInputError({
37
+ message: `Adapter does not have a "${endpointParam}" endpoint.`,
38
+ statusCode: 404,
39
+ });
40
+ }
41
+ // Validate data using validator from v2
42
+ // TODO: See if we want to change this whole thing
43
+ const validator = new validator_1.Validator(req.body, endpoint.inputParameters);
44
+ req.requestContext = {
45
+ id: req.body.id || '1',
46
+ cacheKey: '',
47
+ data: validator.validated.data,
48
+ endpointName: endpoint.name,
49
+ };
50
+ if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
51
+ // Add metrics meta which includes feedId to the request
52
+ // Perform prior to overrides to maintain consistent Feed IDs across adapters
53
+ const metrics = (0, util_1.getMetricsMeta)({
54
+ adapterEndpoint: endpoint,
55
+ adapterConfig: adapter.config,
56
+ }, validator.validated.data);
57
+ req.requestContext = { ...req.requestContext, meta: { metrics } };
58
+ }
59
+ // TODO: Support `includes` and `tokenOverrides` overrides as needed
60
+ const requestParams = req.requestContext.data ?? {};
61
+ if (requestParams['tokenOverrides']) {
62
+ throw new Error('Token overrides not yet supported');
63
+ }
64
+ if (requestParams['includes']) {
65
+ throw new Error('Includes not yet supported');
66
+ }
67
+ // Swaps the 'base' parameter if any overrides are found in the request or the adapter configuration
68
+ // Supports 'base' input as string or string[]
69
+ (0, override_functions_1.performSymbolOverrides)(adapter, req);
70
+ req.requestContext.cacheKey = (0, cache_1.calculateCacheKey)({
71
+ adapterEndpoint: endpoint,
72
+ adapterConfig: adapter.config,
73
+ }, req.requestContext.data);
74
+ done();
75
+ };
76
+ exports.validatorMiddleware = validatorMiddleware;
77
+ const errorCatchingMiddleware = (err, req, res) => {
78
+ // Add adapter or generic error to request meta for metrics use
79
+ req.requestContext.meta = { ...req.requestContext.meta, error: err };
80
+ if (err instanceof error_1.AdapterError) {
81
+ // We want to log these as warn, because although they are to be expected, NOPs should
82
+ // Only use "correct" job specs and therefore not hit adapters with invalid requests.
83
+ errorCatcherLogger.warn(err);
84
+ res.status(err.statusCode).send(err.toJSONResponse());
85
+ }
86
+ else {
87
+ errorCatcherLogger.error(err);
88
+ res.status(200).send('There was an unexpected error in the adapter.');
89
+ }
90
+ };
91
+ exports.errorCatchingMiddleware = errorCatchingMiddleware;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.baseInputParameters = void 0;
4
+ exports.baseInputParameters = {
5
+ endpoint: {
6
+ description: 'The External Adapter "endpoint" name to use.',
7
+ required: false,
8
+ type: 'string',
9
+ },
10
+ resultPath: {
11
+ description: 'The path to key into the API response the retrieve the result',
12
+ required: false,
13
+ // Type: 'string', TODO: Once multiple types are supported this could be string or array of strings
14
+ },
15
+ overrides: {
16
+ description: 'Override the mapping of token symbols to another token symbol',
17
+ required: false,
18
+ // Type: 'string', TODO: Once complex types are supported this could be { [adapter: string]: { [token: string]: string } }
19
+ },
20
+ tokenOverrides: {
21
+ description: 'Override the mapping of token symbols to smart contract address',
22
+ required: false,
23
+ // Type: 'string', TODO: Once complex types are supported this could be { [network: string]: { [token: string]: string } }
24
+ },
25
+ includes: {
26
+ description: 'Override the array of includes that holds additional input parameters when matching a pair of symbols',
27
+ required: false,
28
+ // Type: 'string', TODO: Once complex types are supported this could be { from: string, to: string, includes: [{ from: string, to: string, adapters: string[], inverse: boolean, tokens: boolean }] } }[]
29
+ },
30
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.performSymbolOverrides = void 0;
4
+ const performSymbolOverrides = (adapter, req) => {
5
+ let adapterOverrides = {};
6
+ if (req.requestContext.data &&
7
+ req.requestContext.data['overrides'] &&
8
+ req.requestContext.data['overrides'][adapter.name]) {
9
+ adapterOverrides = req.requestContext.data['overrides'][adapter.name];
10
+ }
11
+ if (!Array.isArray(req.requestContext.data['base'])) {
12
+ // Perform overrides specified in the request payload
13
+ req.requestContext.data['base'] =
14
+ adapterOverrides[req.requestContext.data['base']] ?? req.requestContext.data['base'];
15
+ // Perform hardcoded overrides
16
+ if (adapter.overrides) {
17
+ req.requestContext.data['base'] =
18
+ adapter.overrides[req.requestContext.data['base']] ??
19
+ req.requestContext.data['base'];
20
+ }
21
+ return;
22
+ }
23
+ let requestedSymbols = req.requestContext.data['base'];
24
+ for (let i = 0; i < requestedSymbols.length; i++) {
25
+ const symbol = requestedSymbols[i];
26
+ // Perform overrides specified in the request payload
27
+ let overriddenSymbol = adapterOverrides[symbol.toUpperCase()] ?? adapterOverrides[symbol.toLowerCase()];
28
+ // Perform hardcoded overrides
29
+ if (adapter.overrides) {
30
+ overriddenSymbol =
31
+ adapter.overrides[symbol.toUpperCase()] ??
32
+ adapter.overrides[symbol.toLowerCase()] ??
33
+ overriddenSymbol;
34
+ }
35
+ requestedSymbols[i] = overriddenSymbol ?? requestedSymbols[i];
36
+ }
37
+ requestedSymbols = requestedSymbols.length > 1 ? requestedSymbols : requestedSymbols[1];
38
+ req.requestContext.data['base'] = requestedSymbols;
39
+ };
40
+ exports.performSymbolOverrides = performSymbolOverrides;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.performSymbolOverrides = void 0;
4
+ const performSymbolOverrides = (adapter, req) => {
5
+ let adapterOverrides = {};
6
+ if (req.requestContext.data &&
7
+ req.requestContext.data['overrides'] &&
8
+ req.requestContext.data['overrides'][adapter.name]) {
9
+ adapterOverrides = req.requestContext.data['overrides'][adapter.name];
10
+ }
11
+ if (!Array.isArray(req.requestContext.data['base'])) {
12
+ // Perform overrides specified in the request payload
13
+ req.requestContext.data['base'] =
14
+ adapterOverrides[req.requestContext.data['base']] ?? req.requestContext.data['base'];
15
+ // Perform hardcoded overrides
16
+ if (adapter.overrides) {
17
+ req.requestContext.data['base'] =
18
+ adapter.overrides[req.requestContext.data['base']] ??
19
+ req.requestContext.data['base'];
20
+ }
21
+ return;
22
+ }
23
+ let requestedSymbols = req.requestContext.data['base'];
24
+ for (let i = 0; i < requestedSymbols.length; i++) {
25
+ const symbol = requestedSymbols[i];
26
+ // Perform overrides specified in the request payload
27
+ let overriddenSymbol = adapterOverrides[symbol.toUpperCase()] ?? adapterOverrides[symbol.toLowerCase()];
28
+ // Perform hardcoded overrides
29
+ if (adapter.overrides) {
30
+ overriddenSymbol =
31
+ adapter.overrides[symbol.toUpperCase()] ??
32
+ adapter.overrides[symbol.toLowerCase()] ??
33
+ overriddenSymbol;
34
+ }
35
+ requestedSymbols[i] = overriddenSymbol ?? requestedSymbols[i];
36
+ }
37
+ requestedSymbols = requestedSymbols.length > 1 ? requestedSymbols : requestedSymbols[1];
38
+ req.requestContext.data['base'] = requestedSymbols;
39
+ };
40
+ exports.performSymbolOverrides = performSymbolOverrides;
@@ -0,0 +1,23 @@
1
+ {
2
+ "ethereum": {
3
+ "LINK": "0x514910771af9ca656af840dff83e8264ecf986ca",
4
+ "WETH": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
5
+ "ETH": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
6
+ "stETH": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
7
+ "DIGG": "0x798d1be841a82a273720ce31c822c61a67a601c3",
8
+ "WBTC": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
9
+ "RAI": "0x03ab458634910aad20ef5f1c8ee96f1d6ac54919",
10
+ "RGT": "0xD291E7a03283640FDc51b121aC401383A46cC623",
11
+ "RARI": "0xFca59Cd816aB1eaD66534D82bc21E7515cE441CF",
12
+ "SFI": "0xb753428af26e81097e7fd17f40c88aaa3e04902c",
13
+ "LDO": "0x5a98fcbea516cf06857215779fd812ca3bef1b32",
14
+ "VSP": "0x1b40183EFB4Dd766f11bDa7A7c3AD8982e998421",
15
+ "USDC": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
16
+ "USDT": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
17
+ "DAI": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
18
+ "FRAX": "0x853d955aCEf822Db058eb8505911ED77F175b99e",
19
+ "BOND": "0x0391d2021f89dc339f60fff84546ea23e337750f",
20
+ "FEI": "0x956f47f50a910163d8bf957cf5846d573e7f87ca",
21
+ "TRIBE": "0xc7283b66Eb1EB5FB86327f08e1B5816b0720212B"
22
+ }
23
+ }
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Validator = void 0;
7
+ const error_1 = require("./error");
8
+ const input_params_1 = require("./input-params");
9
+ const util_1 = require("../util");
10
+ const preset_tokens_json_1 = __importDefault(require("./preset-tokens.json"));
11
+ // Don't want to get requester in just yet, only copying the static method 'errored'
12
+ const requesterErrored = (jobRunID = '1', error = undefined, statusCode = 500, feedID = undefined) => {
13
+ if (error instanceof error_1.AdapterError) {
14
+ error.jobRunID = jobRunID;
15
+ if (feedID) {
16
+ error.feedID = feedID;
17
+ }
18
+ return error.toJSONResponse();
19
+ }
20
+ if (error instanceof Error) {
21
+ return new error_1.AdapterError({
22
+ jobRunID,
23
+ statusCode,
24
+ message: error.message,
25
+ cause: error,
26
+ feedID,
27
+ }).toJSONResponse();
28
+ }
29
+ return new error_1.AdapterError({ jobRunID, statusCode, message: error, feedID }).toJSONResponse();
30
+ };
31
+ class Validator {
32
+ constructor(input = { id: '1', data: {} }, inputConfigs = {}, inputOptions = {}, validatorOptions = {}) {
33
+ // OverrideSymbol = (adapter: string, symbol?: string | string[]): string | string[] => {
34
+ // Const defaultSymbol = symbol || this.validated.data.base
35
+ // If (!defaultSymbol) this.throwInvalid(`Required parameter not supplied: base`)
36
+ // // TODO: Will never be reached, because the presetSymbols are used as default overrides
37
+ // If (!this.validated.overrides) return defaultSymbol
38
+ // If (!Array.isArray(defaultSymbol))
39
+ // Return (
40
+ // This.validated.overrides.get(adapter.toLowerCase())?.get(defaultSymbol.toLowerCase()) ||
41
+ // DefaultSymbol
42
+ // )
43
+ // Const multiple: string[] = []
44
+ // For (const sym of defaultSymbol) {
45
+ // Const overrided = this.validated.overrides.get(adapter.toLowerCase())?.get(sym.toLowerCase())
46
+ // If (!overrided) multiple.push(sym)
47
+ // Else multiple.push(overrided)
48
+ // }
49
+ // Return multiple
50
+ // }
51
+ // OverrideToken = (symbol: string, network = 'ethereum'): string | undefined => {
52
+ // Return this.validated.tokenOverrides?.get(network.toLowerCase())?.get(symbol.toLowerCase())
53
+ // }
54
+ // OverrideIncludes = (from: string, to: string): IncludePair | undefined => {
55
+ // // Search through `presetIncludes` to find matching override for adapter and to/from pairing.
56
+ // Const pairs = (
57
+ // This.validated.includes?.filter(
58
+ // (val: string | Includes) => typeof val !== 'string',
59
+ // ) as Includes[]
60
+ // ).filter(
61
+ // (pair) =>
62
+ // Pair.from.toLowerCase() === from.toLowerCase() &&
63
+ // Pair.to.toLowerCase() === to.toLowerCase(),
64
+ // )
65
+ // If (!pairs || !pairs[0] || !pairs[0].includes || !pairs[0].includes[0]) {
66
+ // Return
67
+ // }
68
+ // Return pairs[0].includes[0]
69
+ // }
70
+ // OverrideReverseLookup = (adapter: string, type: OverrideType, symbol: string): string => {
71
+ // Const overrides: Map<string, string> | undefined = this.validated?.[type]?.get(
72
+ // Adapter.toLowerCase(),
73
+ // )
74
+ // If (!overrides) return symbol
75
+ // Let originalSymbol: string | undefined
76
+ // Overrides.forEach((overridden, original) => {
77
+ // If (overridden.toLowerCase() === symbol.toLowerCase()) originalSymbol = original
78
+ // })
79
+ // Return originalSymbol || symbol
80
+ // }
81
+ this.formatOverride = (param) => {
82
+ const _throwInvalid = () => this.throwInvalid(`Parameter supplied with wrong format: "override"`);
83
+ if (!(0, util_1.isObject)(param)) {
84
+ _throwInvalid();
85
+ }
86
+ const _isValid = Object.values(param).every(util_1.isObject);
87
+ if (!_isValid) {
88
+ _throwInvalid();
89
+ }
90
+ const _keyToLowerCase = (entry) => {
91
+ return [entry[0].toLowerCase(), entry[1]];
92
+ };
93
+ return new Map(Object.entries(param)
94
+ .map(_keyToLowerCase)
95
+ .map(([key, value]) => [key, new Map(Object.entries(value).map(_keyToLowerCase))]));
96
+ };
97
+ this.formatIncludeOverrides = (param) => {
98
+ const _throwInvalid = () => this.throwInvalid(`Parameter supplied with wrong format: "includes"`);
99
+ if (!(0, util_1.isArray)(param)) {
100
+ _throwInvalid();
101
+ }
102
+ const _isValid = Object.values(param).every((val) => (0, util_1.isObject)(val) || typeof val === 'string');
103
+ if (!_isValid) {
104
+ _throwInvalid();
105
+ }
106
+ return param;
107
+ };
108
+ this.throwInvalid = (message) => {
109
+ throw new error_1.AdapterError({ jobRunID: this.validated.id, statusCode: 400, message });
110
+ };
111
+ this.input = { ...input };
112
+ if (!this.input.id) {
113
+ this.input.id = '1';
114
+ } // TODO Please remove these once "no any" strict typing is enabled
115
+ if (!this.input.data) {
116
+ this.input.data = {};
117
+ }
118
+ this.inputConfigs = { ...input_params_1.baseInputParameters, ...inputConfigs };
119
+ this.inputOptions = { ...inputOptions };
120
+ this.validatorOptions = {
121
+ shouldThrowError: true,
122
+ includes: [],
123
+ overrides: {},
124
+ ...validatorOptions,
125
+ };
126
+ this.validated = { id: this.input.id, data: {} };
127
+ this.validateInput();
128
+ this.validateOverrides('overrides', this.validatorOptions.overrides);
129
+ this.validateOverrides('tokenOverrides', preset_tokens_json_1.default);
130
+ this.validateIncludeOverrides();
131
+ this.checkDuplicateInputParams(inputConfigs);
132
+ }
133
+ validateInput() {
134
+ try {
135
+ for (const key in this.inputConfigs) {
136
+ this.validateObjectParam(key, this.validatorOptions.shouldThrowError);
137
+ }
138
+ }
139
+ catch (e) {
140
+ this.parseError(e);
141
+ }
142
+ }
143
+ validateOverrides(path, preset) {
144
+ try {
145
+ if (!this.input.data?.[path]) {
146
+ this.validated[path] = this.formatOverride(preset);
147
+ return;
148
+ }
149
+ this.validated[path] = this.formatOverride({ ...preset, ...this.input.data[path] });
150
+ }
151
+ catch (e) {
152
+ this.parseError(e);
153
+ }
154
+ }
155
+ checkDuplicateInputParams(inputConfig) {
156
+ let aliases = [];
157
+ for (const key in inputConfig) {
158
+ const param = inputConfig[key];
159
+ if (Array.isArray(param)) {
160
+ aliases = aliases.concat(param);
161
+ }
162
+ else if (typeof inputConfig === 'boolean') {
163
+ return;
164
+ }
165
+ else {
166
+ aliases.push(key);
167
+ if (typeof param === 'object' && 'aliases' in param && Array.isArray(param.aliases)) {
168
+ aliases = aliases.concat(param.aliases);
169
+ }
170
+ }
171
+ }
172
+ if (aliases.length !== new Set(aliases).size) {
173
+ this.throwInvalid('Duplicate Input Aliases');
174
+ }
175
+ }
176
+ validateIncludeOverrides() {
177
+ try {
178
+ this.validated.includes = this.formatIncludeOverrides([
179
+ ...(Array.isArray(this.input.data?.includes) ? this.input.data.includes : []),
180
+ ...(this.validatorOptions.includes || []),
181
+ ]);
182
+ }
183
+ catch (e) {
184
+ this.parseError(e);
185
+ }
186
+ }
187
+ parseError(error) {
188
+ if (!(error instanceof Error)) {
189
+ return;
190
+ }
191
+ const message = 'Error validating input.';
192
+ if (error instanceof error_1.AdapterError) {
193
+ this.error = error;
194
+ }
195
+ else {
196
+ this.error = new error_1.AdapterError({
197
+ jobRunID: this.validated.id,
198
+ statusCode: 400,
199
+ message,
200
+ cause: error,
201
+ });
202
+ }
203
+ this.errored = requesterErrored(this.validated.id, this.error);
204
+ if (this.validatorOptions.shouldThrowError) {
205
+ throw this.error;
206
+ }
207
+ }
208
+ validateObjectParam(key, shouldThrowError = true) {
209
+ const inputConfig = this.inputConfigs[key];
210
+ const usedKey = this.getUsedKey(key, inputConfig.aliases ?? []);
211
+ const param = usedKey
212
+ ? this.input.data[usedKey] ?? inputConfig.default
213
+ : inputConfig.default;
214
+ if (shouldThrowError) {
215
+ const paramIsDefined = !(param === undefined || param === null || param === '');
216
+ if (inputConfig.required && !paramIsDefined) {
217
+ this.throwInvalid(`Required parameter ${key} must be non-null and non-empty`);
218
+ }
219
+ if (paramIsDefined) {
220
+ if (inputConfig.type) {
221
+ const primitiveTypes = ['boolean', 'number', 'bigint', 'string'];
222
+ if (![...primitiveTypes, 'array', 'object'].includes(inputConfig.type)) {
223
+ this.throwInvalid(`${key} parameter has unrecognized type ${inputConfig.type}`);
224
+ }
225
+ if (primitiveTypes.includes(inputConfig.type) && typeof param !== inputConfig.type) {
226
+ this.throwInvalid(`${key} parameter must be of type ${inputConfig.type}`);
227
+ }
228
+ if (inputConfig.type === 'array' && (!Array.isArray(param) || param.length === 0)) {
229
+ this.throwInvalid(`${key} parameter must be a non-empty array`);
230
+ }
231
+ if (inputConfig.type === 'object' &&
232
+ (!param ||
233
+ Array.isArray(param) ||
234
+ typeof param !== inputConfig.type ||
235
+ Object.keys(param).length === 0)) {
236
+ this.throwInvalid(`${key} parameter must be an object with at least one property`);
237
+ }
238
+ }
239
+ // If (inputConfig.options) {
240
+ // Const tolcase = (o: any) => (typeof o === 'string' ? o.toLowerCase() : o)
241
+ // Const formattedOptions = inputConfig.options.map(tolcase)
242
+ // Const formattedParam = tolcase(param)
243
+ // If (!formattedOptions.includes(formattedParam))
244
+ // This.throwInvalid(
245
+ // `${key} parameter '${formattedParam}' is not in the set of available options: ${formattedOptions.join(
246
+ // ',',
247
+ // )}`,
248
+ // )
249
+ // }
250
+ // For (const dependency of inputConfig.dependsOn ?? []) {
251
+ // Const usedDependencyKey = this.getUsedKey(
252
+ // Dependency,
253
+ // (this.inputConfigs[dependency] as InputParameter).aliases ?? [],
254
+ // )
255
+ // If (!usedDependencyKey) this.throwInvalid(`${key} dependency ${dependency} not supplied`)
256
+ // }
257
+ // For (const exclusive of inputConfig.exclusive ?? []) {
258
+ // Const usedExclusiveKey = this.getUsedKey(
259
+ // Exclusive,
260
+ // (this.inputConfigs[exclusive] as InputParameter).aliases ?? [],
261
+ // )
262
+ // If (usedExclusiveKey)
263
+ // This.throwInvalid(`${key} cannot be supplied concurrently with ${exclusive}`)
264
+ // }
265
+ }
266
+ }
267
+ this.validated.data[key] = param;
268
+ }
269
+ validateOptionalParam(param, key, options) {
270
+ if (param && options) {
271
+ if (!Array.isArray(options)) {
272
+ this.throwInvalid(`Parameter options for ${key} must be of an Array type`);
273
+ }
274
+ if (!options.includes(param)) {
275
+ this.throwInvalid(`${param} is not a supported ${key} option. Must be one of ${options}`);
276
+ }
277
+ }
278
+ this.validated.data[key] = param;
279
+ }
280
+ validateRequiredParam(param, key, options) {
281
+ if (typeof param === 'undefined' || param === '') {
282
+ this.throwInvalid(`Required parameter not supplied: ${key}`);
283
+ }
284
+ if (options) {
285
+ if (!Array.isArray(options)) {
286
+ this.throwInvalid(`Parameter options for ${key} must be of an Array type`);
287
+ }
288
+ if (!options.includes(param)) {
289
+ this.throwInvalid(`${param} is not a supported ${key} option. Must be one of ${options.join(' || ')}`);
290
+ }
291
+ }
292
+ this.validated.data[key] = param;
293
+ }
294
+ getUsedKey(key, keyArray) {
295
+ const comparisonArray = [...keyArray];
296
+ if (!comparisonArray.includes(key)) {
297
+ comparisonArray.push(key);
298
+ }
299
+ const inputParamKeys = Object.keys(this.input.data);
300
+ return inputParamKeys.find((k) => comparisonArray.includes(k));
301
+ }
302
+ }
303
+ exports.Validator = Validator;
package/.c8rc.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "reporter": ["lcov", "text"]
3
- }
package/.eslintignore DELETED
@@ -1,10 +0,0 @@
1
- # don't ever lint node_modules
2
- node_modules
3
- # don't lint build output (make sure it's set to your correct build folder name)
4
- dist
5
- # don't lint coverage output
6
- coverage
7
-
8
- .yarn
9
- .vscode
10
- webpack.config.js
package/.eslintrc.js DELETED
@@ -1,96 +0,0 @@
1
- // https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/README.md
2
- module.exports = {
3
- root: true,
4
- parser: '@typescript-eslint/parser',
5
- plugins: ['@typescript-eslint', 'eslint-plugin-tsdoc'],
6
- extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
7
- rules: {
8
- 'tsdoc/syntax': 'warn',
9
-
10
- // Problems
11
- 'array-callback-return': [
12
- 'error',
13
- {
14
- allowImplicit: false,
15
- checkForEach: true,
16
- },
17
- ],
18
- 'no-constant-binary-expression': 'error',
19
- 'no-constructor-return': 'error',
20
- 'no-duplicate-imports': 'error',
21
- 'no-promise-executor-return': 'error',
22
- 'no-self-compare': 'error',
23
- 'no-template-curly-in-string': 'error',
24
- 'no-unmodified-loop-condition': 'error',
25
- 'no-unreachable-loop': 'error',
26
- 'no-unused-private-class-members': 'error',
27
- 'require-atomic-updates': 'error',
28
-
29
- // Suggestions
30
- 'capitalized-comments': ['error', 'always', { ignoreConsecutiveComments: true }],
31
- complexity: ['error', 25], // TODO: Should be lower (15?), but validator has complex methods
32
- curly: 'error',
33
- 'default-case-last': 'error',
34
- 'default-param-last': 'error',
35
- eqeqeq: ['error', 'smart'],
36
- 'func-names': 'error',
37
- 'func-style': ['error', 'declaration', { allowArrowFunctions: true }],
38
- 'grouped-accessor-pairs': ['error', 'getBeforeSet'],
39
- 'max-depth': ['error', 4], // TODO: Should be lower (3), but validator has complex methods
40
- 'max-nested-callbacks': ['error', 2],
41
- 'max-params': ['error', 4], // TODO: Should be lower (3), but validator has complex methods,
42
- 'new-cap': ['error', { newIsCapExceptions: ['ctor'] }],
43
- 'no-caller': 'error',
44
- 'no-confusing-arrow': [
45
- 'error',
46
- {
47
- allowParens: true,
48
- },
49
- ],
50
- 'no-console': ['warn'], // We want this to debug, but the check workflow should accept no warns
51
- 'no-div-regex': 'error',
52
- 'no-eval': 'error',
53
- 'no-extend-native': 'error',
54
- 'no-extra-bind': 'error',
55
- 'no-extra-label': 'error',
56
- 'no-extra-semi': 'error',
57
- 'no-floating-decimal': 'error',
58
- 'no-implied-eval': 'error',
59
- 'no-invalid-this': 'error',
60
- 'no-labels': 'error',
61
- 'no-lonely-if': 'error',
62
- 'no-multi-assign': 'error',
63
- 'no-multi-str': 'error',
64
- 'no-nested-ternary': 'error',
65
- 'no-new': 'error',
66
- 'no-new-func': 'error',
67
- 'no-new-object': 'error',
68
- 'no-new-wrappers': 'error',
69
- 'no-param-reassign': 'error',
70
- 'no-proto': 'error',
71
- 'no-return-assign': 'error',
72
- 'no-return-await': 'error',
73
- 'no-sequences': 'error',
74
- 'no-shadow': 'off',
75
- '@typescript-eslint/no-shadow': 'error', // https://stackoverflow.com/questions/63961803/eslint-says-all-enums-in-typescript-app-are-already-declared-in-the-upper-scope
76
- 'no-unneeded-ternary': 'error',
77
- 'no-useless-call': 'error',
78
- 'no-useless-computed-key': 'error',
79
- 'no-useless-concat': 'error',
80
- 'no-useless-rename': 'error',
81
- 'no-var': 'error',
82
- 'operator-assignment': ['error', 'always'],
83
- 'prefer-arrow-callback': 'error',
84
- 'prefer-const': 'error',
85
- 'prefer-exponentiation-operator': 'error',
86
- 'prefer-object-spread': 'error',
87
- 'prefer-promise-reject-errors': 'error',
88
- 'prefer-regex-literals': 'error',
89
- 'prefer-rest-params': 'error',
90
- 'prefer-spread': 'error',
91
- 'prefer-template': 'error',
92
- 'spaced-comment': ['error', 'always'],
93
- 'symbol-description': 'error',
94
- yoda: 'error',
95
- },
96
- }