@chainlink/external-adapter-framework 0.0.14 → 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 (164) hide show
  1. package/adapter.js +114 -101
  2. package/cache/factory.js +3 -0
  3. package/chainlink-external-adapter-framework-v0.0.6.tgz +0 -0
  4. package/config/index.js +69 -83
  5. package/config/provider-limits.js +10 -15
  6. package/examples/bank-frick/accounts.js +3 -2
  7. package/examples/bank-frick/index.js +3 -2
  8. package/examples/coingecko/src/config/index.js +13 -0
  9. package/examples/coingecko/src/config/overrides.json +10826 -0
  10. package/examples/coingecko/src/cryptoUtils.js +41 -0
  11. package/examples/coingecko/src/endpoint/coins.js +33 -0
  12. package/examples/coingecko/src/endpoint/crypto-marketcap.js +46 -0
  13. package/examples/coingecko/src/endpoint/crypto-volume.js +46 -0
  14. package/examples/coingecko/src/endpoint/crypto.js +47 -0
  15. package/examples/coingecko/src/endpoint/dominance.js +26 -0
  16. package/examples/coingecko/src/endpoint/global-marketcap.js +26 -0
  17. package/examples/coingecko/src/endpoint/index.js +15 -0
  18. package/examples/coingecko/src/globalUtils.js +48 -0
  19. package/examples/coingecko/src/index.js +14 -0
  20. package/examples/coingecko/test/e2e/adapter.test.js +262 -0
  21. package/examples/coingecko/test/integration/adapter.test.js +264 -0
  22. package/examples/coingecko/test/integration/capturedRequests.json +1 -0
  23. package/examples/coingecko/test/integration/fixtures.js +41 -0
  24. package/{package/examples/coingecko → examples/coingecko-old}/batch-warming.js +4 -3
  25. package/examples/{coingecko → coingecko-old}/index.js +3 -2
  26. package/{package/examples/coingecko → examples/coingecko-old}/rest.js +3 -2
  27. package/examples/ncfx/index.js +3 -2
  28. package/examples/ncfx/websocket.js +3 -2
  29. package/index.js +17 -23
  30. package/package.json +5 -4
  31. package/rate-limiting/index.js +23 -2
  32. package/transports/batch-warming.js +1 -1
  33. package/transports/rest.js +9 -2
  34. package/transports/util.js +2 -1
  35. package/transports/websocket.js +4 -6
  36. package/util/recordRequests.js +45 -0
  37. package/{package/validation/override-functions.js → validation/overrideFunctions.js} +0 -0
  38. package/adapter.d.ts +0 -107
  39. package/background-executor.d.ts +0 -11
  40. package/cache/factory.d.ts +0 -6
  41. package/cache/index.d.ts +0 -94
  42. package/cache/local.d.ts +0 -23
  43. package/cache/metrics.d.ts +0 -27
  44. package/cache/redis.d.ts +0 -16
  45. package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  46. package/config/index.d.ts +0 -209
  47. package/config/provider-limits.d.ts +0 -31
  48. package/examples/bank-frick/accounts.d.ts +0 -39
  49. package/examples/bank-frick/config/index.d.ts +0 -4
  50. package/examples/bank-frick/index.d.ts +0 -2
  51. package/examples/bank-frick/util.d.ts +0 -4
  52. package/examples/coingecko/batch-warming.d.ts +0 -2
  53. package/examples/coingecko/batch-warming.js +0 -52
  54. package/examples/coingecko/index.d.ts +0 -2
  55. package/examples/coingecko/rest.d.ts +0 -2
  56. package/examples/coingecko/rest.js +0 -50
  57. package/examples/ncfx/config/index.d.ts +0 -12
  58. package/examples/ncfx/index.d.ts +0 -2
  59. package/examples/ncfx/websocket.d.ts +0 -36
  60. package/index.d.ts +0 -11
  61. package/metrics/constants.d.ts +0 -16
  62. package/metrics/index.d.ts +0 -15
  63. package/metrics/util.d.ts +0 -7
  64. package/package/adapter.d.ts +0 -88
  65. package/package/adapter.js +0 -112
  66. package/package/background-executor.d.ts +0 -11
  67. package/package/background-executor.js +0 -45
  68. package/package/cache/factory.d.ts +0 -6
  69. package/package/cache/factory.js +0 -57
  70. package/package/cache/index.d.ts +0 -90
  71. package/package/cache/index.js +0 -169
  72. package/package/cache/local.d.ts +0 -23
  73. package/package/cache/local.js +0 -83
  74. package/package/cache/metrics.d.ts +0 -27
  75. package/package/cache/metrics.js +0 -120
  76. package/package/cache/redis.d.ts +0 -16
  77. package/package/cache/redis.js +0 -100
  78. package/package/config/index.d.ts +0 -195
  79. package/package/config/index.js +0 -365
  80. package/package/config/provider-limits.d.ts +0 -31
  81. package/package/config/provider-limits.js +0 -76
  82. package/package/examples/coingecko/batch-warming.d.ts +0 -2
  83. package/package/examples/coingecko/index.d.ts +0 -2
  84. package/package/examples/coingecko/index.js +0 -10
  85. package/package/examples/coingecko/rest.d.ts +0 -2
  86. package/package/examples/ncfx/config/index.d.ts +0 -12
  87. package/package/examples/ncfx/config/index.js +0 -15
  88. package/package/examples/ncfx/index.d.ts +0 -2
  89. package/package/examples/ncfx/index.js +0 -10
  90. package/package/examples/ncfx/websocket.d.ts +0 -36
  91. package/package/examples/ncfx/websocket.js +0 -72
  92. package/package/index.d.ts +0 -12
  93. package/package/index.js +0 -92
  94. package/package/metrics/constants.d.ts +0 -16
  95. package/package/metrics/constants.js +0 -25
  96. package/package/metrics/index.d.ts +0 -15
  97. package/package/metrics/index.js +0 -123
  98. package/package/metrics/util.d.ts +0 -3
  99. package/package/metrics/util.js +0 -9
  100. package/package/package.json +0 -72
  101. package/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  102. package/package/rate-limiting/background/fixed-frequency.js +0 -37
  103. package/package/rate-limiting/index.d.ts +0 -54
  104. package/package/rate-limiting/index.js +0 -63
  105. package/package/rate-limiting/metrics.d.ts +0 -3
  106. package/package/rate-limiting/metrics.js +0 -44
  107. package/package/rate-limiting/request/simple-counting.d.ts +0 -20
  108. package/package/rate-limiting/request/simple-counting.js +0 -62
  109. package/package/test.d.ts +0 -1
  110. package/package/test.js +0 -6
  111. package/package/transports/batch-warming.d.ts +0 -34
  112. package/package/transports/batch-warming.js +0 -101
  113. package/package/transports/index.d.ts +0 -87
  114. package/package/transports/index.js +0 -87
  115. package/package/transports/metrics.d.ts +0 -21
  116. package/package/transports/metrics.js +0 -105
  117. package/package/transports/rest.d.ts +0 -43
  118. package/package/transports/rest.js +0 -129
  119. package/package/transports/util.d.ts +0 -8
  120. package/package/transports/util.js +0 -85
  121. package/package/transports/websocket.d.ts +0 -80
  122. package/package/transports/websocket.js +0 -169
  123. package/package/util/expiring-sorted-set.d.ts +0 -21
  124. package/package/util/expiring-sorted-set.js +0 -47
  125. package/package/util/index.d.ts +0 -11
  126. package/package/util/index.js +0 -35
  127. package/package/util/logger.d.ts +0 -42
  128. package/package/util/logger.js +0 -62
  129. package/package/util/request.d.ts +0 -55
  130. package/package/util/request.js +0 -2
  131. package/package/validation/error.d.ts +0 -50
  132. package/package/validation/error.js +0 -79
  133. package/package/validation/index.d.ts +0 -5
  134. package/package/validation/index.js +0 -86
  135. package/package/validation/input-params.d.ts +0 -15
  136. package/package/validation/input-params.js +0 -30
  137. package/package/validation/override-functions.d.ts +0 -3
  138. package/package/validation/preset-tokens.json +0 -23
  139. package/package/validation/validator.d.ts +0 -47
  140. package/package/validation/validator.js +0 -303
  141. package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  142. package/rate-limiting/index.d.ts +0 -54
  143. package/rate-limiting/metrics.d.ts +0 -3
  144. package/rate-limiting/request/simple-counting.d.ts +0 -20
  145. package/test.d.ts +0 -1
  146. package/transports/batch-warming.d.ts +0 -35
  147. package/transports/index.d.ts +0 -70
  148. package/transports/metrics.d.ts +0 -21
  149. package/transports/rest.d.ts +0 -44
  150. package/transports/util.d.ts +0 -8
  151. package/transports/websocket.d.ts +0 -81
  152. package/util/expiring-sorted-set.d.ts +0 -21
  153. package/util/expiring-sorted-set.js +0 -47
  154. package/util/index.d.ts +0 -12
  155. package/util/logger.d.ts +0 -42
  156. package/util/request.d.ts +0 -57
  157. package/util/subscription-set/expiring-sorted-set.d.ts +0 -22
  158. package/util/subscription-set/subscription-set.d.ts +0 -18
  159. package/util/test-payload-loader.d.ts +0 -25
  160. package/validation/error.d.ts +0 -50
  161. package/validation/index.d.ts +0 -5
  162. package/validation/input-params.d.ts +0 -15
  163. package/validation/override-functions.d.ts +0 -3
  164. package/validation/validator.d.ts +0 -47
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdapterCustomError = exports.AdapterConnectionError = exports.AdapterDataProviderError = exports.AdapterTimeoutError = exports.AdapterRateLimitError = exports.AdapterInputError = exports.AdapterError = void 0;
4
- const constants_1 = require("../metrics/constants");
5
- class AdapterError extends Error {
6
- constructor({ jobRunID = '1', status = 'errored', statusCode = 500, name = 'AdapterError', message = 'An error occurred.', cause, url, errorResponse, feedID, providerStatusCode, metricsLabel = constants_1.HttpRequestType.ADAPTER_ERROR }) {
7
- super(message);
8
- this.jobRunID = jobRunID;
9
- this.status = status;
10
- this.statusCode = statusCode;
11
- this.name = name;
12
- this.message = message;
13
- this.cause = cause;
14
- if (url) {
15
- this.url = url;
16
- }
17
- if (feedID) {
18
- this.feedID = feedID;
19
- }
20
- this.errorResponse = errorResponse;
21
- this.providerStatusCode = providerStatusCode;
22
- this.metricsLabel = metricsLabel;
23
- }
24
- toJSONResponse() {
25
- const showDebugInfo = process.env['NODE_ENV'] === 'development' || process.env['DEBUG'] === 'true';
26
- const errorBasic = {
27
- name: this.name,
28
- message: this.message,
29
- url: this.url,
30
- errorResponse: this.errorResponse,
31
- feedID: this.feedID,
32
- };
33
- const errorFull = { ...errorBasic, stack: this.stack, cause: this.cause };
34
- return {
35
- jobRunID: this.jobRunID,
36
- status: this.status,
37
- statusCode: this.statusCode,
38
- providerStatusCode: this.providerStatusCode,
39
- error: showDebugInfo ? errorFull : errorBasic,
40
- };
41
- }
42
- }
43
- exports.AdapterError = AdapterError;
44
- class AdapterInputError extends AdapterError {
45
- constructor(input) {
46
- super({ ...input, metricsLabel: constants_1.HttpRequestType.INPUT_ERROR });
47
- }
48
- }
49
- exports.AdapterInputError = AdapterInputError;
50
- class AdapterRateLimitError extends AdapterError {
51
- constructor(input) {
52
- super({ ...input, metricsLabel: constants_1.HttpRequestType.RATE_LIMIT_ERROR });
53
- }
54
- }
55
- exports.AdapterRateLimitError = AdapterRateLimitError;
56
- class AdapterTimeoutError extends AdapterError {
57
- constructor(input) {
58
- super({ ...input, metricsLabel: constants_1.HttpRequestType.TIMEOUT_ERROR });
59
- }
60
- }
61
- exports.AdapterTimeoutError = AdapterTimeoutError;
62
- class AdapterDataProviderError extends AdapterError {
63
- constructor(input) {
64
- super({ ...input, metricsLabel: constants_1.HttpRequestType.DP_ERROR });
65
- }
66
- }
67
- exports.AdapterDataProviderError = AdapterDataProviderError;
68
- class AdapterConnectionError extends AdapterError {
69
- constructor(input) {
70
- super({ ...input, metricsLabel: constants_1.HttpRequestType.CONNECTION_ERROR });
71
- }
72
- }
73
- exports.AdapterConnectionError = AdapterConnectionError;
74
- class AdapterCustomError extends AdapterError {
75
- constructor(input) {
76
- super({ ...input, metricsLabel: constants_1.HttpRequestType.CUSTOM_ERROR });
77
- }
78
- }
79
- exports.AdapterCustomError = AdapterCustomError;
@@ -1,5 +0,0 @@
1
- import { FastifyReply, FastifyRequest } from 'fastify';
2
- import { AdapterMiddlewareBuilder } from '../util/request';
3
- export { InputParameters } from './input-params';
4
- export declare const validatorMiddleware: AdapterMiddlewareBuilder;
5
- export declare const errorCatchingMiddleware: (err: Error, req: FastifyRequest, res: FastifyReply) => void;
@@ -1,86 +0,0 @@
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() || adapter.defaultEndpoint;
26
- if (!endpointParam) {
27
- throw new error_1.AdapterInputError({
28
- message: `Request body does not specify an endpoint, and there is no default endpoint configured for this adapter.`,
29
- statusCode: 400,
30
- });
31
- }
32
- const endpoint = adapter.endpointsMap[endpointParam];
33
- if (!endpoint) {
34
- throw new error_1.AdapterInputError({
35
- message: `Adapter does not have a "${req.body.endpoint}" endpoint.`,
36
- statusCode: 404,
37
- });
38
- }
39
- // Validate data using validator from v2
40
- // TODO: See if we want to change this whole thing
41
- const validator = new validator_1.Validator(req.body, endpoint.inputParameters);
42
- req.requestContext = {
43
- id: req.body.id || '1',
44
- cacheKey: '',
45
- data: validator.validated.data,
46
- endpointName: endpoint.name,
47
- };
48
- if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
49
- // Add metrics meta which includes feedId to the request
50
- // Perform prior to overrides to maintain consistent Feed IDs across adapters
51
- const metrics = (0, util_1.getMetricsMeta)(endpoint, validator.validated.data);
52
- req.requestContext = { ...req.requestContext, meta: { metrics } };
53
- }
54
- // TODO: Support `includes` and `tokenOverrides` overrides as needed
55
- const requestParams = req.requestContext.data ?? {};
56
- if (requestParams['tokenOverrides']) {
57
- throw new Error('Token overrides not yet supported');
58
- }
59
- if (requestParams['includes']) {
60
- throw new Error('Includes not yet supported');
61
- }
62
- // Swaps the 'base' parameter if any overrides are found in the request or the adapter configuration
63
- // Supports 'base' input as string or string[]
64
- (0, override_functions_1.performSymbolOverrides)(adapter, req);
65
- req.requestContext.cacheKey = (0, cache_1.calculateCacheKey)({
66
- adapterEndpoint: endpoint,
67
- adapterConfig: adapter.config,
68
- }, req.requestContext.data);
69
- done();
70
- };
71
- exports.validatorMiddleware = validatorMiddleware;
72
- const errorCatchingMiddleware = (err, req, res) => {
73
- // Add adapter or generic error to request meta for metrics use
74
- req.requestContext.meta = { ...req.requestContext.meta, error: err };
75
- if (err instanceof error_1.AdapterError) {
76
- // We want to log these as warn, because although they are to be expected, NOPs should
77
- // Only use "correct" job specs and therefore not hit adapters with invalid requests.
78
- errorCatcherLogger.warn(err);
79
- res.status(err.statusCode).send(err.toJSONResponse());
80
- }
81
- else {
82
- errorCatcherLogger.error(err);
83
- res.status(200).send('There was an unexpected error in the adapter.');
84
- }
85
- };
86
- exports.errorCatchingMiddleware = errorCatchingMiddleware;
@@ -1,15 +0,0 @@
1
- export declare type Override = Map<string, Map<string, string>>;
2
- export declare type InputParameter = {
3
- aliases?: string[];
4
- description?: string;
5
- type?: 'bigint' | 'boolean' | 'array' | 'number' | 'object' | 'string';
6
- required?: boolean;
7
- options?: unknown[];
8
- default?: unknown;
9
- dependsOn?: string[];
10
- exclusive?: string[];
11
- };
12
- export declare type InputParameters = {
13
- [name: string]: InputParameter | boolean | string[];
14
- };
15
- export declare const baseInputParameters: InputParameters;
@@ -1,30 +0,0 @@
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
- };
@@ -1,3 +0,0 @@
1
- import { InitializedAdapter } from '../adapter';
2
- import { AdapterRequest } from '../util/request';
3
- export declare const performSymbolOverrides: (adapter: InitializedAdapter, req: AdapterRequest) => void;
@@ -1,23 +0,0 @@
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
- }
@@ -1,47 +0,0 @@
1
- import { AdapterError, AdapterErrorResponse } from './error';
2
- import { InputParameters, Override } from './input-params';
3
- export declare type OverrideType = 'overrides' | 'tokenOverrides' | 'includes';
4
- declare type InputType = {
5
- id?: string;
6
- data?: any;
7
- };
8
- export interface ValidatorOptions {
9
- shouldThrowError?: boolean;
10
- includes?: any[];
11
- overrides?: any;
12
- }
13
- export declare type IncludePair = {
14
- from: string;
15
- to: string;
16
- adapters?: string[];
17
- inverse?: boolean;
18
- tokens?: boolean;
19
- };
20
- export declare type Includes = {
21
- from: string;
22
- to: string;
23
- includes: IncludePair[];
24
- };
25
- export declare class Validator {
26
- input: InputType;
27
- inputConfigs: InputParameters;
28
- inputOptions: Record<string, any[]>;
29
- validatorOptions: ValidatorOptions;
30
- validated: any;
31
- error: AdapterError | undefined;
32
- errored: AdapterErrorResponse | undefined;
33
- constructor(input?: InputType, inputConfigs?: {}, inputOptions?: {}, validatorOptions?: ValidatorOptions);
34
- validateInput(): void;
35
- validateOverrides(path: 'overrides' | 'tokenOverrides', preset: Record<string, any>): void;
36
- checkDuplicateInputParams(inputConfig: InputParameters): void;
37
- validateIncludeOverrides(): void;
38
- parseError(error: unknown): void;
39
- formatOverride: (param: any) => Override;
40
- formatIncludeOverrides: (param: any) => Override;
41
- throwInvalid: (message: string) => void;
42
- validateObjectParam(key: string, shouldThrowError?: boolean): void;
43
- validateOptionalParam(param: any, key: string, options: any[]): void;
44
- validateRequiredParam(param: any, key: string, options: any[]): void;
45
- getUsedKey(key: string, keyArray: string[]): string | undefined;
46
- }
47
- export {};
@@ -1,303 +0,0 @@
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;
@@ -1,10 +0,0 @@
1
- import { AdapterRateLimitTier, BackgroundExecuteRateLimiter } from '..';
2
- import { AdapterEndpoint } from '../../adapter';
3
- export declare const DEFAULT_SHARED_MS_BETWEEN_REQUESTS = 5000;
4
- export declare class FixedFrequencyRateLimiter implements BackgroundExecuteRateLimiter {
5
- msBetweenRequestsMap: {
6
- [endpointName: string]: number;
7
- };
8
- initialize(endpoints: AdapterEndpoint[], limits?: AdapterRateLimitTier): this;
9
- msUntilNextExecution(endpointName: string): number;
10
- }
@@ -1,54 +0,0 @@
1
- import { AdapterEndpoint } from '../adapter';
2
- export * from './request/simple-counting';
3
- export * from './background/fixed-frequency';
4
- export interface AdapterRateLimitTier {
5
- rateLimit1s?: number;
6
- rateLimit1m?: number;
7
- rateLimit1h?: number;
8
- note?: string;
9
- }
10
- /**
11
- * Common interface for all RateLimiter classes to implement
12
- */
13
- export interface RateLimiter {
14
- /**
15
- * Method to ensure all RateLimiters can be initialized in the same manner.
16
- *
17
- * @param limits - settings for how much throughput to allow for the Adapter
18
- * @param endpoints - list of adapter endpoints
19
- */
20
- initialize(endpoints: AdapterEndpoint[], limits: AdapterRateLimitTier): this;
21
- }
22
- /**
23
- * RequestRateLimiters perform checks agains imminent outbound requests for any transport.
24
- */
25
- export interface RequestRateLimiter extends RateLimiter {
26
- /**
27
- * This method will check using whatever strategy is implemented to determine if
28
- * this request can be processed. If so, it returns true; if not, returns false.
29
- */
30
- isUnderLimits(): boolean;
31
- }
32
- /**
33
- * BackgroundExecuteFrequencyRateLimiters will implement custom logic to calculate
34
- * the period of time to wait between background executions for a transport.
35
- */
36
- export interface BackgroundExecuteRateLimiter extends RateLimiter {
37
- msUntilNextExecution(endpointName: string): number;
38
- }
39
- /**
40
- * This method will convert all possible settings for a rate limit tier and
41
- * convert them all to requests per second, returning the lowest one
42
- *
43
- * @param limits - the rate limit tier set for the adapter
44
- * @returns the most restrictive of the set options, in requests per second
45
- */
46
- export declare const consolidateTierLimits: (limits?: AdapterRateLimitTier) => number;
47
- /**
48
- * Validates rate limiting tiers specified for the adapter, and returns the one to use.
49
- *
50
- * @param tiers - the adapter config listing the different available API tiers
51
- * @param selectedTier - chosen API tier from settings, if present
52
- * @returns the specified API tier, or a default one if none are specified
53
- */
54
- export declare const getRateLimitingTier: (tiers?: Record<string, AdapterRateLimitTier>, selectedTier?: string) => AdapterRateLimitTier | undefined;
@@ -1,3 +0,0 @@
1
- import * as client from 'prom-client';
2
- export declare const retrieveCost: <ProviderResponseBody>(data: ProviderResponseBody) => number;
3
- export declare const rateLimitCreditsSpentTotal: client.Counter<"feed_id" | "participant_id">;