@chainlink/external-adapter-framework 0.0.12 → 0.0.15

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 (220) hide show
  1. package/.eslintignore +1 -0
  2. package/.github/README.MD +35 -10
  3. package/.github/workflows/label.yaml +39 -0
  4. package/.github/workflows/publish.yaml +0 -3
  5. package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +82953 -0
  6. package/dist/examples/coingecko/test/integration/adapter.test.ts.js +91672 -0
  7. package/dist/main.js +72703 -0
  8. package/jest.config.js +5 -0
  9. package/package.json +14 -3
  10. package/src/config/provider-limits.ts +14 -17
  11. package/src/examples/coingecko/src/config/index.ts +12 -0
  12. package/src/examples/coingecko/src/config/overrides.json +10826 -0
  13. package/src/examples/coingecko/src/cryptoUtils.ts +88 -0
  14. package/src/examples/coingecko/src/endpoint/coins.ts +54 -0
  15. package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +66 -0
  16. package/src/examples/coingecko/src/endpoint/crypto-volume.ts +66 -0
  17. package/src/examples/coingecko/src/endpoint/crypto.ts +63 -0
  18. package/src/examples/coingecko/src/endpoint/dominance.ts +40 -0
  19. package/src/examples/coingecko/src/endpoint/global-marketcap.ts +40 -0
  20. package/src/examples/coingecko/src/endpoint/index.ts +6 -0
  21. package/src/examples/coingecko/src/globalUtils.ts +78 -0
  22. package/src/examples/coingecko/src/index.ts +17 -0
  23. package/src/examples/coingecko/test/e2e/adapter.test.ts +278 -0
  24. package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +15 -0
  25. package/src/examples/coingecko/test/integration/adapter.test.ts +281 -0
  26. package/src/examples/coingecko/test/integration/capturedRequests.json +1 -0
  27. package/src/examples/coingecko/test/integration/fixtures.ts +42 -0
  28. package/src/examples/{coingecko → coingecko-old}/batch-warming.ts +1 -0
  29. package/src/examples/{coingecko → coingecko-old}/index.ts +0 -0
  30. package/src/examples/{coingecko → coingecko-old}/rest.ts +0 -0
  31. package/src/transports/batch-warming.ts +7 -2
  32. package/src/transports/rest.ts +5 -1
  33. package/src/util/index.ts +1 -0
  34. package/src/util/recordRequests.ts +47 -0
  35. package/src/validation/overrideFunctions.ts +44 -0
  36. package/test/metrics/warmer-metrics.test.ts +1 -0
  37. package/test/{smoke.test.ts → smoke/smoke.test.ts} +13 -13
  38. package/{test-payload-fail.json → test/smoke/test-payload-fail.json} +0 -0
  39. package/{test-payload.js → test/smoke/test-payload.js} +0 -0
  40. package/{test-payload.json → test/smoke/test-payload.json} +0 -0
  41. package/test/transports/batch.test.ts +1 -0
  42. package/tsconfig.json +4 -1
  43. package/webpack.config.js +39 -5
  44. package/yarn-error.log +3778 -0
  45. package/dist/src/adapter.d.ts +0 -135
  46. package/dist/src/adapter.js +0 -145
  47. package/dist/src/background-executor.d.ts +0 -11
  48. package/dist/src/background-executor.js +0 -45
  49. package/dist/src/cache/factory.d.ts +0 -6
  50. package/dist/src/cache/factory.js +0 -55
  51. package/dist/src/cache/index.d.ts +0 -94
  52. package/dist/src/cache/index.js +0 -173
  53. package/dist/src/cache/local.d.ts +0 -23
  54. package/dist/src/cache/local.js +0 -83
  55. package/dist/src/cache/metrics.d.ts +0 -27
  56. package/dist/src/cache/metrics.js +0 -120
  57. package/dist/src/cache/redis.d.ts +0 -16
  58. package/dist/src/cache/redis.js +0 -100
  59. package/dist/src/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  60. package/dist/src/config/index.d.ts +0 -214
  61. package/dist/src/config/index.js +0 -380
  62. package/dist/src/config/provider-limits.d.ts +0 -31
  63. package/dist/src/config/provider-limits.js +0 -75
  64. package/dist/src/examples/bank-frick/accounts.d.ts +0 -39
  65. package/dist/src/examples/bank-frick/accounts.js +0 -192
  66. package/dist/src/examples/bank-frick/config/index.d.ts +0 -4
  67. package/dist/src/examples/bank-frick/config/index.js +0 -54
  68. package/dist/src/examples/bank-frick/index.d.ts +0 -2
  69. package/dist/src/examples/bank-frick/index.js +0 -15
  70. package/dist/src/examples/bank-frick/util.d.ts +0 -4
  71. package/dist/src/examples/bank-frick/util.js +0 -39
  72. package/dist/src/examples/coingecko/batch-warming.d.ts +0 -7
  73. package/dist/src/examples/coingecko/batch-warming.js +0 -53
  74. package/dist/src/examples/coingecko/index.d.ts +0 -2
  75. package/dist/src/examples/coingecko/index.js +0 -11
  76. package/dist/src/examples/coingecko/rest.d.ts +0 -12
  77. package/dist/src/examples/coingecko/rest.js +0 -51
  78. package/dist/src/examples/ncfx/config/index.d.ts +0 -12
  79. package/dist/src/examples/ncfx/config/index.js +0 -15
  80. package/dist/src/examples/ncfx/index.d.ts +0 -13
  81. package/dist/src/examples/ncfx/index.js +0 -11
  82. package/dist/src/examples/ncfx/websocket.d.ts +0 -47
  83. package/dist/src/examples/ncfx/websocket.js +0 -73
  84. package/dist/src/index.d.ts +0 -11
  85. package/dist/src/index.js +0 -128
  86. package/dist/src/metrics/constants.d.ts +0 -16
  87. package/dist/src/metrics/constants.js +0 -25
  88. package/dist/src/metrics/index.d.ts +0 -15
  89. package/dist/src/metrics/index.js +0 -122
  90. package/dist/src/metrics/util.d.ts +0 -7
  91. package/dist/src/metrics/util.js +0 -9
  92. package/dist/src/package/adapter.d.ts +0 -88
  93. package/dist/src/package/adapter.js +0 -112
  94. package/dist/src/package/background-executor.d.ts +0 -11
  95. package/dist/src/package/background-executor.js +0 -45
  96. package/dist/src/package/cache/factory.d.ts +0 -6
  97. package/dist/src/package/cache/factory.js +0 -57
  98. package/dist/src/package/cache/index.d.ts +0 -90
  99. package/dist/src/package/cache/index.js +0 -169
  100. package/dist/src/package/cache/local.d.ts +0 -23
  101. package/dist/src/package/cache/local.js +0 -83
  102. package/dist/src/package/cache/metrics.d.ts +0 -27
  103. package/dist/src/package/cache/metrics.js +0 -120
  104. package/dist/src/package/cache/redis.d.ts +0 -16
  105. package/dist/src/package/cache/redis.js +0 -100
  106. package/dist/src/package/config/index.d.ts +0 -195
  107. package/dist/src/package/config/index.js +0 -365
  108. package/dist/src/package/config/provider-limits.d.ts +0 -31
  109. package/dist/src/package/config/provider-limits.js +0 -76
  110. package/dist/src/package/examples/coingecko/batch-warming.d.ts +0 -2
  111. package/dist/src/package/examples/coingecko/batch-warming.js +0 -52
  112. package/dist/src/package/examples/coingecko/index.d.ts +0 -2
  113. package/dist/src/package/examples/coingecko/index.js +0 -10
  114. package/dist/src/package/examples/coingecko/rest.d.ts +0 -2
  115. package/dist/src/package/examples/coingecko/rest.js +0 -50
  116. package/dist/src/package/examples/ncfx/config/index.d.ts +0 -12
  117. package/dist/src/package/examples/ncfx/config/index.js +0 -15
  118. package/dist/src/package/examples/ncfx/index.d.ts +0 -2
  119. package/dist/src/package/examples/ncfx/index.js +0 -10
  120. package/dist/src/package/examples/ncfx/websocket.d.ts +0 -36
  121. package/dist/src/package/examples/ncfx/websocket.js +0 -72
  122. package/dist/src/package/index.d.ts +0 -12
  123. package/dist/src/package/index.js +0 -92
  124. package/dist/src/package/metrics/constants.d.ts +0 -16
  125. package/dist/src/package/metrics/constants.js +0 -25
  126. package/dist/src/package/metrics/index.d.ts +0 -15
  127. package/dist/src/package/metrics/index.js +0 -123
  128. package/dist/src/package/metrics/util.d.ts +0 -3
  129. package/dist/src/package/metrics/util.js +0 -9
  130. package/dist/src/package/package.json +0 -72
  131. package/dist/src/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  132. package/dist/src/package/rate-limiting/background/fixed-frequency.js +0 -37
  133. package/dist/src/package/rate-limiting/index.d.ts +0 -54
  134. package/dist/src/package/rate-limiting/index.js +0 -63
  135. package/dist/src/package/rate-limiting/metrics.d.ts +0 -3
  136. package/dist/src/package/rate-limiting/metrics.js +0 -44
  137. package/dist/src/package/rate-limiting/request/simple-counting.d.ts +0 -20
  138. package/dist/src/package/rate-limiting/request/simple-counting.js +0 -62
  139. package/dist/src/package/test.d.ts +0 -1
  140. package/dist/src/package/test.js +0 -6
  141. package/dist/src/package/transports/batch-warming.d.ts +0 -34
  142. package/dist/src/package/transports/batch-warming.js +0 -101
  143. package/dist/src/package/transports/index.d.ts +0 -87
  144. package/dist/src/package/transports/index.js +0 -87
  145. package/dist/src/package/transports/metrics.d.ts +0 -21
  146. package/dist/src/package/transports/metrics.js +0 -105
  147. package/dist/src/package/transports/rest.d.ts +0 -43
  148. package/dist/src/package/transports/rest.js +0 -129
  149. package/dist/src/package/transports/util.d.ts +0 -8
  150. package/dist/src/package/transports/util.js +0 -85
  151. package/dist/src/package/transports/websocket.d.ts +0 -80
  152. package/dist/src/package/transports/websocket.js +0 -169
  153. package/dist/src/package/util/expiring-sorted-set.d.ts +0 -21
  154. package/dist/src/package/util/expiring-sorted-set.js +0 -47
  155. package/dist/src/package/util/index.d.ts +0 -11
  156. package/dist/src/package/util/index.js +0 -35
  157. package/dist/src/package/util/logger.d.ts +0 -42
  158. package/dist/src/package/util/logger.js +0 -62
  159. package/dist/src/package/util/request.d.ts +0 -55
  160. package/dist/src/package/util/request.js +0 -2
  161. package/dist/src/package/validation/error.d.ts +0 -50
  162. package/dist/src/package/validation/error.js +0 -79
  163. package/dist/src/package/validation/index.d.ts +0 -5
  164. package/dist/src/package/validation/index.js +0 -86
  165. package/dist/src/package/validation/input-params.d.ts +0 -15
  166. package/dist/src/package/validation/input-params.js +0 -30
  167. package/dist/src/package/validation/override-functions.d.ts +0 -3
  168. package/dist/src/package/validation/override-functions.js +0 -40
  169. package/dist/src/package/validation/preset-tokens.json +0 -23
  170. package/dist/src/package/validation/validator.d.ts +0 -47
  171. package/dist/src/package/validation/validator.js +0 -303
  172. package/dist/src/package.json +0 -72
  173. package/dist/src/rate-limiting/background/fixed-frequency.d.ts +0 -11
  174. package/dist/src/rate-limiting/background/fixed-frequency.js +0 -35
  175. package/dist/src/rate-limiting/index.d.ts +0 -55
  176. package/dist/src/rate-limiting/index.js +0 -63
  177. package/dist/src/rate-limiting/metrics.d.ts +0 -3
  178. package/dist/src/rate-limiting/metrics.js +0 -44
  179. package/dist/src/rate-limiting/request/simple-counting.d.ts +0 -21
  180. package/dist/src/rate-limiting/request/simple-counting.js +0 -62
  181. package/dist/src/test.d.ts +0 -1
  182. package/dist/src/test.js +0 -6
  183. package/dist/src/transports/batch-warming.d.ts +0 -35
  184. package/dist/src/transports/batch-warming.js +0 -101
  185. package/dist/src/transports/index.d.ts +0 -70
  186. package/dist/src/transports/index.js +0 -87
  187. package/dist/src/transports/metrics.d.ts +0 -22
  188. package/dist/src/transports/metrics.js +0 -105
  189. package/dist/src/transports/rest.d.ts +0 -44
  190. package/dist/src/transports/rest.js +0 -131
  191. package/dist/src/transports/util.d.ts +0 -8
  192. package/dist/src/transports/util.js +0 -85
  193. package/dist/src/transports/websocket.d.ts +0 -80
  194. package/dist/src/transports/websocket.js +0 -166
  195. package/dist/src/util/expiring-sorted-set.d.ts +0 -21
  196. package/dist/src/util/expiring-sorted-set.js +0 -47
  197. package/dist/src/util/index.d.ts +0 -12
  198. package/dist/src/util/index.js +0 -35
  199. package/dist/src/util/logger.d.ts +0 -42
  200. package/dist/src/util/logger.js +0 -62
  201. package/dist/src/util/request.d.ts +0 -57
  202. package/dist/src/util/request.js +0 -2
  203. package/dist/src/util/subscription-set/expiring-sorted-set.d.ts +0 -22
  204. package/dist/src/util/subscription-set/expiring-sorted-set.js +0 -47
  205. package/dist/src/util/subscription-set/subscription-set.d.ts +0 -18
  206. package/dist/src/util/subscription-set/subscription-set.js +0 -19
  207. package/dist/src/util/test-payload-loader.d.ts +0 -25
  208. package/dist/src/util/test-payload-loader.js +0 -83
  209. package/dist/src/validation/error.d.ts +0 -50
  210. package/dist/src/validation/error.js +0 -79
  211. package/dist/src/validation/index.d.ts +0 -5
  212. package/dist/src/validation/index.js +0 -91
  213. package/dist/src/validation/input-params.d.ts +0 -15
  214. package/dist/src/validation/input-params.js +0 -30
  215. package/dist/src/validation/override-functions.d.ts +0 -3
  216. package/dist/src/validation/override-functions.js +0 -40
  217. package/dist/src/validation/preset-tokens.json +0 -23
  218. package/dist/src/validation/validator.d.ts +0 -47
  219. package/dist/src/validation/validator.js +0 -303
  220. package/src/test.ts +0 -5
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.batchEndpoint = void 0;
4
- const batch_warming_1 = require("../../transports/batch-warming");
5
- const DEFAULT_URL = 'https://pro-api.coingecko.com/api/v3';
6
- const inputParameters = {
7
- coinid: {
8
- description: 'The CoinGecko id or array of ids of the coin(s) to query (Note: because of current limitations to use a dummy base will need to be supplied)',
9
- required: false,
10
- },
11
- base: {
12
- aliases: ['from', 'coin'],
13
- description: 'The symbol or array of symbols of the currency to query',
14
- required: true,
15
- },
16
- quote: {
17
- aliases: ['to', 'market'],
18
- description: 'The symbol of the currency to convert to',
19
- required: true,
20
- },
21
- };
22
- const batchEndpointTransport = new batch_warming_1.BatchWarmingTransport({
23
- prepareRequest: (params, context) => {
24
- return {
25
- baseURL: DEFAULT_URL,
26
- url: '/simple/price',
27
- method: 'GET',
28
- params: {
29
- ids: [...new Set(params.map((p) => p.base))].join(','),
30
- vs_currencies: [...new Set(params.map((p) => p.quote))].join(','),
31
- x_cg_pro_api_key: context.adapterConfig.API_KEY,
32
- },
33
- };
34
- },
35
- parseResponse: (res) => {
36
- const entries = [];
37
- for (const [base, entry] of Object.entries(res.data)) {
38
- for (const [quote, price] of Object.entries(entry)) {
39
- entries.push({
40
- params: { base, quote },
41
- value: price,
42
- });
43
- }
44
- }
45
- return entries;
46
- },
47
- });
48
- exports.batchEndpoint = {
49
- name: 'batch',
50
- transport: batchEndpointTransport,
51
- inputParameters,
52
- };
@@ -1,2 +0,0 @@
1
- import { Adapter } from '../../adapter';
2
- export declare const adapter: Adapter;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.adapter = void 0;
4
- const batch_warming_1 = require("./batch-warming");
5
- const rest_1 = require("./rest");
6
- exports.adapter = {
7
- name: 'coingecko',
8
- defaultEndpoint: 'batch',
9
- endpoints: [rest_1.restEndpoint, batch_warming_1.batchEndpoint],
10
- };
@@ -1,2 +0,0 @@
1
- import { AdapterEndpoint } from '../../adapter';
2
- export declare const restEndpoint: AdapterEndpoint;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.restEndpoint = void 0;
4
- const transports_1 = require("../../transports");
5
- const DEFAULT_URL = 'https://api.coingecko.com/api/v3';
6
- const inputParameters = {
7
- coinid: {
8
- description: 'The CoinGecko id or array of ids of the coin(s) to query (Note: because of current limitations to use a dummy base will need to be supplied)',
9
- required: false,
10
- },
11
- base: {
12
- aliases: ['from', 'coin'],
13
- description: 'The symbol or array of symbols of the currency to query',
14
- required: true,
15
- },
16
- quote: {
17
- aliases: ['to', 'market'],
18
- description: 'The symbol of the currency to convert to',
19
- required: true,
20
- },
21
- };
22
- const restEndpointTransport = new transports_1.RestTransport({
23
- prepareRequest: (req) => {
24
- return {
25
- baseURL: DEFAULT_URL,
26
- url: '/simple/price',
27
- method: 'GET',
28
- params: {
29
- ids: req.requestContext.data.base,
30
- vs_currencies: req.requestContext.data.quote,
31
- },
32
- };
33
- },
34
- parseResponse: (req, res) => {
35
- return {
36
- data: res.data,
37
- statusCode: 200,
38
- result: res.data[req.requestContext.data.base]?.[req.requestContext.data.quote],
39
- };
40
- },
41
- options: {
42
- coalescing: true,
43
- },
44
- });
45
- exports.restEndpoint = {
46
- name: 'rest',
47
- aliases: ['qwe'],
48
- transport: restEndpointTransport,
49
- inputParameters,
50
- };
@@ -1,12 +0,0 @@
1
- export declare const customSettings: {
2
- readonly USERNAME: {
3
- readonly description: "Username for the NCFX API";
4
- readonly type: "string";
5
- readonly required: true;
6
- };
7
- readonly PASSWORD: {
8
- readonly description: "Password for the NCFX API";
9
- readonly type: "string";
10
- readonly required: true;
11
- };
12
- };
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.customSettings = void 0;
4
- exports.customSettings = {
5
- USERNAME: {
6
- description: 'Username for the NCFX API',
7
- type: 'string',
8
- required: true,
9
- },
10
- PASSWORD: {
11
- description: 'Password for the NCFX API',
12
- type: 'string',
13
- required: true,
14
- },
15
- };
@@ -1,2 +0,0 @@
1
- import { Adapter } from '../../adapter';
2
- export declare const adapter: Adapter;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.adapter = void 0;
4
- const config_1 = require("./config");
5
- const websocket_1 = require("./websocket");
6
- exports.adapter = {
7
- name: 'ncfx',
8
- endpoints: [websocket_1.webSocketEndpoint],
9
- customSettings: config_1.customSettings,
10
- };
@@ -1,36 +0,0 @@
1
- import { AdapterEndpoint } from '../../adapter';
2
- import { WebSocketTransport } from '../../transports/websocket';
3
- import { InputParameters } from '../../validation';
4
- interface AdapterRequestParams {
5
- base: string;
6
- quote: string;
7
- }
8
- export declare const inputParameters: InputParameters;
9
- interface ProviderMessage {
10
- timestamp: string;
11
- currencyPair: string;
12
- bid: number;
13
- offer: number;
14
- mid: number;
15
- changes: [
16
- {
17
- period: string;
18
- change: number;
19
- percentage: number;
20
- }
21
- ];
22
- }
23
- export declare const websocketTransport: WebSocketTransport<AdapterRequestParams, ProviderMessage[], {
24
- readonly USERNAME: {
25
- readonly description: "Username for the NCFX API";
26
- readonly type: "string";
27
- readonly required: true;
28
- };
29
- readonly PASSWORD: {
30
- readonly description: "Password for the NCFX API";
31
- readonly type: "string";
32
- readonly required: true;
33
- };
34
- }>;
35
- export declare const webSocketEndpoint: AdapterEndpoint;
36
- export {};
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.webSocketEndpoint = exports.websocketTransport = exports.inputParameters = void 0;
4
- const websocket_1 = require("../../transports/websocket");
5
- const util_1 = require("../../util");
6
- exports.inputParameters = {
7
- base: {
8
- aliases: ['from', 'coin'],
9
- description: 'The symbol of the currency to query',
10
- required: true,
11
- },
12
- quote: {
13
- aliases: ['to', 'market'],
14
- description: 'The symbol of the currency to convert to',
15
- required: true,
16
- },
17
- };
18
- const logger = (0, util_1.makeLogger)('NcfxWebSocketTransport');
19
- exports.websocketTransport = new websocket_1.WebSocketTransport({
20
- url: 'wss://feed.newchangefx.com/cryptodata',
21
- handlers: {
22
- open(connection, context) {
23
- return new Promise((resolve, reject) => {
24
- // Set up listener
25
- connection.on('message', (data) => {
26
- const parsed = JSON.parse(data.toString());
27
- if (parsed.Message?.startsWith('Logged in as user')) {
28
- logger.info('Got logged in response, connection is ready');
29
- resolve();
30
- }
31
- else {
32
- reject(new Error('Unexpected message after WS connection open'));
33
- }
34
- });
35
- // Send login payload
36
- connection.send(JSON.stringify({
37
- request: 'login',
38
- username: context.adapterConfig.USERNAME,
39
- password: context.adapterConfig.PASSWORD,
40
- }));
41
- });
42
- },
43
- message(message) {
44
- if (!Array.isArray(message)) {
45
- logger.debug('WS message is not array, skipping');
46
- return [];
47
- }
48
- return message.map((m) => {
49
- const [base, quote] = m.currencyPair.split('/');
50
- return {
51
- params: { base, quote },
52
- value: m.offer,
53
- };
54
- });
55
- },
56
- },
57
- builders: {
58
- subscribeMessage: (params) => ({
59
- request: 'subscribe',
60
- ccy: `${params.base}/${params.quote}`,
61
- }),
62
- unsubscribeMessage: (params) => ({
63
- request: 'unsubscribe',
64
- ccy: `${params.base}/${params.quote}`,
65
- }),
66
- },
67
- });
68
- exports.webSocketEndpoint = {
69
- name: 'websocket',
70
- transport: exports.websocketTransport,
71
- inputParameters: exports.inputParameters,
72
- };
@@ -1,12 +0,0 @@
1
- import { Server } from 'http';
2
- import { Adapter } from './adapter';
3
- import { AdapterDependencies } from './transports';
4
- /**
5
- * Main function for the framework.
6
- * Initializes config and dependencies, uses those to initialize Transports, and starts listening for requests.
7
- *
8
- * @param adapter - an object describing an External Adapter
9
- * @param dependencies - an optional object with adapter dependencies to inject
10
- * @returns a Promise that resolves to the http.Server listening for connections
11
- */
12
- export declare const expose: (adapter: Adapter, dependencies?: Partial<AdapterDependencies>) => Promise<Server | undefined>;
@@ -1,92 +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.expose = void 0;
7
- const fastify_1 = __importDefault(require("fastify"));
8
- const path_1 = require("path");
9
- const adapter_1 = require("./adapter");
10
- const background_executor_1 = require("./background-executor");
11
- const cache_1 = require("./cache");
12
- const config_1 = require("./config");
13
- const metrics_1 = require("./metrics");
14
- const transports_1 = require("./transports");
15
- const util_1 = require("./util");
16
- const validation_1 = require("./validation");
17
- const logger = (0, util_1.makeLogger)('Main');
18
- const VERSION = process.env['npm_package_version'];
19
- /**
20
- * Main function for the framework.
21
- * Initializes config and dependencies, uses those to initialize Transports, and starts listening for requests.
22
- *
23
- * @param adapter - an object describing an External Adapter
24
- * @param dependencies - an optional object with adapter dependencies to inject
25
- * @returns a Promise that resolves to the http.Server listening for connections
26
- */
27
- const expose = async (adapter, dependencies) => {
28
- const config = (0, config_1.buildAdapterConfig)({
29
- overrides: adapter.envDefaultOverrides,
30
- customSettings: adapter.customSettings,
31
- });
32
- // Initialize adapter (create dependencies, inject them, build endpoint map, etc.)
33
- const initializedAdapter = await (0, adapter_1.initializeAdapter)(adapter, config, dependencies);
34
- let server = undefined;
35
- if (config.METRICS_ENABLED && config.EXPERIMENTAL_METRICS_ENABLED) {
36
- (0, metrics_1.setupMetricsServer)(adapter.name, config);
37
- }
38
- if (config.EA_MODE === 'reader' || config.EA_MODE === 'reader-writer') {
39
- // Main REST API server to handle incoming requests
40
- const app = await buildRestApi(config, initializedAdapter);
41
- // Start listening for incoming requests
42
- try {
43
- await app.listen(config.EA_PORT, config.EA_HOST);
44
- }
45
- catch (err) {
46
- logger.fatal(`There was an error when starting the EA server: ${err}`);
47
- process.exit();
48
- }
49
- logger.info(`Listening on port ${app.server.address().port}`);
50
- server = app.server;
51
- }
52
- else {
53
- logger.info('REST API is disabled; this instance will not process incoming requests.');
54
- }
55
- if (config.EA_MODE === 'writer' || config.EA_MODE === 'reader-writer') {
56
- // Start background loop that will take care of calling any async Transports
57
- logger.info('Starting background execution loop');
58
- (0, background_executor_1.callBackgroundExecutes)(initializedAdapter, server);
59
- }
60
- else {
61
- logger.info('Background executor is disabled; this instance will not perform async background executes.');
62
- }
63
- return server;
64
- };
65
- exports.expose = expose;
66
- async function buildRestApi(config, initializedAdapter) {
67
- const app = (0, fastify_1.default)();
68
- // Add healthcheck endpoint before middlewares to bypass them
69
- app.get((0, path_1.join)(config.BASE_URL, 'health'), (req, res) => {
70
- res.status(200).send({ message: 'OK', version: VERSION });
71
- });
72
- // Use global error handling
73
- app.setErrorHandler(validation_1.errorCatchingMiddleware);
74
- const transportHandler = await (0, transports_1.buildTransportHandler)(initializedAdapter);
75
- app.register(async (router) => {
76
- // Set up "middlewares" (hooks in fastify)
77
- router.addHook('preHandler', (0, validation_1.validatorMiddleware)(initializedAdapter));
78
- router.addHook('preHandler', (0, cache_1.buildCacheMiddleware)(initializedAdapter));
79
- if (config['CORRELATION_ID_ENABLED']) {
80
- router.addHook('onRequest', util_1.loggingContextMiddleware);
81
- }
82
- router.route({
83
- url: config.BASE_URL,
84
- method: 'POST',
85
- handler: transportHandler,
86
- }); // Pass config maybe? cleaner that multiple instances / dependencies
87
- if (config.METRICS_ENABLED && config.EXPERIMENTAL_METRICS_ENABLED) {
88
- router.addHook('onResponse', metrics_1.buildMetricsMiddleware);
89
- }
90
- });
91
- return app;
92
- }
@@ -1,16 +0,0 @@
1
- export declare enum HttpRequestType {
2
- CACHE_HIT = "cacheHit",
3
- DATA_PROVIDER_HIT = "dataProviderHit",
4
- ADAPTER_ERROR = "adapterError",
5
- INPUT_ERROR = "inputError",
6
- RATE_LIMIT_ERROR = "rateLimitError",
7
- DP_ERROR = "dataProviderError",
8
- TIMEOUT_ERROR = "timeoutError",
9
- CONNECTION_ERROR = "connectionError",
10
- CUSTOM_ERROR = "customError"
11
- }
12
- /**
13
- * Maxiumum number of characters that a feedId can contain.
14
- */
15
- export declare const MAX_FEED_ID_LENGTH = 300;
16
- export declare const requestDurationBuckets: number[];
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requestDurationBuckets = exports.MAX_FEED_ID_LENGTH = exports.HttpRequestType = void 0;
4
- var HttpRequestType;
5
- (function (HttpRequestType) {
6
- HttpRequestType["CACHE_HIT"] = "cacheHit";
7
- HttpRequestType["DATA_PROVIDER_HIT"] = "dataProviderHit";
8
- HttpRequestType["ADAPTER_ERROR"] = "adapterError";
9
- HttpRequestType["INPUT_ERROR"] = "inputError";
10
- HttpRequestType["RATE_LIMIT_ERROR"] = "rateLimitError";
11
- // BURST_LIMIT_ERROR = 'burstLimitError',
12
- // BACKOFF_ERROR = 'backoffError',
13
- HttpRequestType["DP_ERROR"] = "dataProviderError";
14
- HttpRequestType["TIMEOUT_ERROR"] = "timeoutError";
15
- HttpRequestType["CONNECTION_ERROR"] = "connectionError";
16
- // RES_EMPTY_ERROR = 'responseEmptyError',
17
- // RES_INVALID_ERROR = 'responseInvalidError',
18
- HttpRequestType["CUSTOM_ERROR"] = "customError";
19
- })(HttpRequestType = exports.HttpRequestType || (exports.HttpRequestType = {}));
20
- /**
21
- * Maxiumum number of characters that a feedId can contain.
22
- */
23
- exports.MAX_FEED_ID_LENGTH = 300;
24
- // We should tune these as we collect data, this is the default bucket distribution that prom comes with
25
- exports.requestDurationBuckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10];
@@ -1,15 +0,0 @@
1
- import * as client from 'prom-client';
2
- import { AdapterRequest } from '../util';
3
- import { AdapterConfig } from '../config';
4
- import { FastifyReply, HookHandlerDoneFunction } from 'fastify';
5
- export declare function setupMetricsServer(name: string, config: AdapterConfig): void;
6
- export declare const setupMetrics: (name: string, config: AdapterConfig) => void;
7
- /**
8
- * Builds metrics middleware that records end to end EA response times
9
- * and count of requests
10
- *
11
- * @returns the cache middleware function
12
- */
13
- export declare const buildMetricsMiddleware: (req: AdapterRequest, res: FastifyReply, done: HookHandlerDoneFunction) => void;
14
- export declare const httpRequestsTotal: client.Counter<"type" | "method" | "feed_id" | "status_code" | "provider_status_code" | "retry" | "is_cache_warming">;
15
- export declare const httpRequestDurationSeconds: client.Histogram<string>;
@@ -1,123 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.httpRequestDurationSeconds = exports.httpRequestsTotal = exports.buildMetricsMiddleware = exports.setupMetrics = exports.setupMetricsServer = void 0;
30
- const client = __importStar(require("prom-client"));
31
- const constants_1 = require("./constants");
32
- const util_1 = require("../util");
33
- const fastify_1 = __importDefault(require("fastify"));
34
- const path_1 = require("path");
35
- const error_1 = require("../validation/error");
36
- const logger = (0, util_1.makeLogger)('Metrics');
37
- function setupMetricsServer(name, config) {
38
- const metricsApp = (0, fastify_1.default)({
39
- logger: false,
40
- });
41
- const metricsPort = config.METRICS_PORT;
42
- const endpoint = config.METRICS_USE_BASE_URL ? (0, path_1.join)(config.BASE_URL, 'metrics') : '/metrics';
43
- const eaHost = config.EA_HOST;
44
- (0, exports.setupMetrics)(name, config);
45
- metricsApp.get(endpoint, async (_, res) => {
46
- res.type('txt');
47
- res.send(await client.register.metrics());
48
- });
49
- metricsApp.listen(metricsPort, eaHost, () => logger.info(`Monitoring listening on port ${metricsPort}!`));
50
- }
51
- exports.setupMetricsServer = setupMetricsServer;
52
- const setupMetrics = (name, config) => {
53
- client.collectDefaultMetrics();
54
- client.register.setDefaultLabels({
55
- app_name: config.METRICS_NAME || name || 'N/A',
56
- app_version: config['npm_package_version'],
57
- });
58
- };
59
- exports.setupMetrics = setupMetrics;
60
- /**
61
- * Builds metrics middleware that records end to end EA response times
62
- * and count of requests
63
- *
64
- * @returns the cache middleware function
65
- */
66
- const buildMetricsMiddleware = (req, res, done) => {
67
- const feedId = req.requestContext.meta?.metrics?.feedId || 'N/A';
68
- const labels = buildHttpRequestMetricsLabel(feedId, req.requestContext.meta?.error, req.requestContext.meta?.metrics?.cacheHit);
69
- // Record number of requests sent to EA
70
- exports.httpRequestsTotal.labels(labels).inc();
71
- // Record response time of request through entire EA
72
- exports.httpRequestDurationSeconds.observe(res.getResponseTime());
73
- logger.debug(`Response time for ${feedId}: ${res.getResponseTime()}ms`);
74
- done();
75
- };
76
- exports.buildMetricsMiddleware = buildMetricsMiddleware;
77
- const buildHttpRequestMetricsLabel = (feedId, error, cacheHit) => {
78
- const labels = {};
79
- labels.method = 'POST';
80
- labels.feed_id = feedId;
81
- if (error instanceof error_1.AdapterError) {
82
- // If error present and an instace of AdapterError, build label from error info
83
- labels.type = error?.metricsLabel || constants_1.HttpRequestType.ADAPTER_ERROR;
84
- labels.status_code = error?.statusCode;
85
- labels.provider_status_code = error?.providerStatusCode;
86
- }
87
- else if (error instanceof Error) {
88
- // If error present and not instance of generic Error, unexpected failure occurred
89
- // TODO: Build label with 500 error and withold dp status? Status sent as 200 in error handler
90
- labels.type = constants_1.HttpRequestType.ADAPTER_ERROR;
91
- labels.status_code = 500;
92
- }
93
- else {
94
- // If no error present, request went as expected
95
- labels.status_code = 200;
96
- if (cacheHit) {
97
- labels.type = constants_1.HttpRequestType.CACHE_HIT;
98
- }
99
- else {
100
- labels.type = constants_1.HttpRequestType.DATA_PROVIDER_HIT;
101
- labels.provider_status_code = 200;
102
- }
103
- }
104
- return labels;
105
- };
106
- exports.httpRequestsTotal = new client.Counter({
107
- name: 'http_requests_total',
108
- help: 'The number of http requests this external adapter has serviced for its entire uptime',
109
- labelNames: [
110
- 'method',
111
- 'status_code',
112
- 'retry',
113
- 'type',
114
- 'is_cache_warming',
115
- 'feed_id',
116
- 'provider_status_code',
117
- ],
118
- });
119
- exports.httpRequestDurationSeconds = new client.Histogram({
120
- name: 'http_request_duration_seconds',
121
- help: 'A histogram bucket of the distribution of http request durations',
122
- buckets: constants_1.requestDurationBuckets,
123
- });
@@ -1,3 +0,0 @@
1
- import { AdapterMetricsMeta, AdapterRequestData } from '../util';
2
- import { AdapterEndpoint } from '../adapter';
3
- export declare const getMetricsMeta: (endpoint: AdapterEndpoint, data: AdapterRequestData) => AdapterMetricsMeta;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMetricsMeta = void 0;
4
- const cache_1 = require("../cache");
5
- const getMetricsMeta = (endpoint, data) => {
6
- const feedId = (0, cache_1.calculateFeedId)(endpoint, data);
7
- return { feedId };
8
- };
9
- exports.getMetricsMeta = getMetricsMeta;
@@ -1,72 +0,0 @@
1
- {
2
- "name": "@chainlink/external-adapter-framework",
3
- "version": "0.0.6",
4
- "main": "dist/index.js",
5
- "license": "MIT",
6
- "dependencies": {
7
- "axios": "^0.27.2",
8
- "fastify": "^3.29.0",
9
- "ioredis": "^5.0.4",
10
- "pino": "^7.9.2",
11
- "prom-client": "13.2.0",
12
- "ts-node": "^10.9.1",
13
- "typescript": "^4.6.3",
14
- "ws": "^8.5.0"
15
- },
16
- "scripts": {
17
- "start": "ts-node src/test.ts",
18
- "generate-docs": "typedoc src/**/*.ts",
19
- "test": "LOG_LEVEL=error EA_PORT=0 c8 ava",
20
- "test-debug": "LOG_LEVEL=trace DEBUG=true EA_PORT=0 c8 ava --verbose",
21
- "build": "tsc",
22
- "lint": "eslint ./src"
23
- },
24
- "devDependencies": {
25
- "@sinonjs/fake-timers": "^9.1.2",
26
- "@types/node": "^18.6.5",
27
- "@types/sinonjs__fake-timers": "^8.1.2",
28
- "@types/ws": "^8.5.3",
29
- "@typescript-eslint/eslint-plugin": "^5.17.0",
30
- "@typescript-eslint/parser": "^5.17.0",
31
- "ava": "^4.2.0",
32
- "c8": "^7.11.2",
33
- "eslint": "^8.14.0",
34
- "eslint-config-prettier": "^8.5.0",
35
- "eslint-plugin-tsdoc": "^0.2.16",
36
- "mock-socket": "^9.1.3",
37
- "nock": "^13.2.4",
38
- "pino-pretty": "^7.6.0",
39
- "prettier": "^2.6.1",
40
- "ts-loader": "^9.3.1",
41
- "typedoc": "^0.22.15",
42
- "webpack": "^5.74.0",
43
- "webpack-cli": "^4.10.0"
44
- },
45
- "prettier": {
46
- "semi": false,
47
- "singleQuote": true,
48
- "printWidth": 100,
49
- "endOfLine": "auto",
50
- "trailingComma": "all",
51
- "arrowParens": "always"
52
- },
53
- "optionalDependencies": {
54
- "bufferutil": "^4.0.6",
55
- "utf-8-validate": "^5.0.9"
56
- },
57
- "ava": {
58
- "files": [
59
- "test/**/*.test.ts"
60
- ],
61
- "extensions": [
62
- "ts"
63
- ],
64
- "require": [
65
- "ts-node/register"
66
- ],
67
- "workerThreads": false,
68
- "environmentVariables": {
69
- "METRICS_ENABLED": "false"
70
- }
71
- }
72
- }
@@ -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
- }