@chainlink/external-adapter-framework 0.0.10 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapter.d.ts +22 -3
- package/adapter.js +5 -2
- package/cache/factory.js +0 -2
- package/cache/index.d.ts +6 -2
- package/cache/index.js +13 -9
- package/cache/redis.js +5 -5
- package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/config/index.d.ts +15 -1
- package/config/index.js +19 -4
- package/config/provider-limits.js +5 -1
- package/examples/bank-frick/accounts.d.ts +39 -0
- package/examples/bank-frick/accounts.js +191 -0
- package/examples/bank-frick/config/index.d.ts +4 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.d.ts +2 -0
- package/examples/bank-frick/index.js +14 -0
- package/examples/bank-frick/util.d.ts +4 -0
- package/examples/bank-frick/util.js +39 -0
- package/index.d.ts +1 -2
- package/index.js +42 -1
- package/metrics/index.js +0 -1
- package/metrics/util.d.ts +5 -1
- package/metrics/util.js +2 -2
- package/package/adapter.d.ts +88 -0
- package/package/adapter.js +112 -0
- package/package/background-executor.d.ts +11 -0
- package/package/background-executor.js +45 -0
- package/package/cache/factory.d.ts +6 -0
- package/package/cache/factory.js +57 -0
- package/package/cache/index.d.ts +90 -0
- package/package/cache/index.js +169 -0
- package/package/cache/local.d.ts +23 -0
- package/package/cache/local.js +83 -0
- package/package/cache/metrics.d.ts +27 -0
- package/package/cache/metrics.js +120 -0
- package/package/cache/redis.d.ts +16 -0
- package/package/cache/redis.js +100 -0
- package/package/config/index.d.ts +195 -0
- package/package/config/index.js +365 -0
- package/package/config/provider-limits.d.ts +31 -0
- package/package/config/provider-limits.js +76 -0
- package/package/examples/coingecko/batch-warming.d.ts +2 -0
- package/package/examples/coingecko/batch-warming.js +52 -0
- package/package/examples/coingecko/index.d.ts +2 -0
- package/package/examples/coingecko/index.js +10 -0
- package/package/examples/coingecko/rest.d.ts +2 -0
- package/package/examples/coingecko/rest.js +50 -0
- package/package/examples/ncfx/config/index.d.ts +12 -0
- package/package/examples/ncfx/config/index.js +15 -0
- package/package/examples/ncfx/index.d.ts +2 -0
- package/package/examples/ncfx/index.js +10 -0
- package/package/examples/ncfx/websocket.d.ts +36 -0
- package/package/examples/ncfx/websocket.js +72 -0
- package/package/index.d.ts +12 -0
- package/package/index.js +92 -0
- package/package/metrics/constants.d.ts +16 -0
- package/package/metrics/constants.js +25 -0
- package/package/metrics/index.d.ts +15 -0
- package/package/metrics/index.js +123 -0
- package/package/metrics/util.d.ts +3 -0
- package/package/metrics/util.js +9 -0
- package/package/package.json +72 -0
- package/package/rate-limiting/background/fixed-frequency.d.ts +10 -0
- package/package/rate-limiting/background/fixed-frequency.js +37 -0
- package/package/rate-limiting/index.d.ts +54 -0
- package/package/rate-limiting/index.js +63 -0
- package/package/rate-limiting/metrics.d.ts +3 -0
- package/package/rate-limiting/metrics.js +44 -0
- package/package/rate-limiting/request/simple-counting.d.ts +20 -0
- package/package/rate-limiting/request/simple-counting.js +62 -0
- package/package/test.d.ts +1 -0
- package/package/test.js +6 -0
- package/package/transports/batch-warming.d.ts +34 -0
- package/package/transports/batch-warming.js +101 -0
- package/package/transports/index.d.ts +87 -0
- package/package/transports/index.js +87 -0
- package/package/transports/metrics.d.ts +21 -0
- package/package/transports/metrics.js +105 -0
- package/package/transports/rest.d.ts +43 -0
- package/package/transports/rest.js +129 -0
- package/package/transports/util.d.ts +8 -0
- package/package/transports/util.js +85 -0
- package/package/transports/websocket.d.ts +80 -0
- package/package/transports/websocket.js +169 -0
- package/package/util/expiring-sorted-set.d.ts +21 -0
- package/package/util/expiring-sorted-set.js +47 -0
- package/package/util/index.d.ts +11 -0
- package/package/util/index.js +35 -0
- package/package/util/logger.d.ts +42 -0
- package/package/util/logger.js +62 -0
- package/package/util/request.d.ts +55 -0
- package/package/util/request.js +2 -0
- package/package/validation/error.d.ts +50 -0
- package/package/validation/error.js +79 -0
- package/package/validation/index.d.ts +5 -0
- package/package/validation/index.js +86 -0
- package/package/validation/input-params.d.ts +15 -0
- package/package/validation/input-params.js +30 -0
- package/package/validation/override-functions.d.ts +3 -0
- package/package/validation/override-functions.js +40 -0
- package/package/validation/preset-tokens.json +23 -0
- package/package/validation/validator.d.ts +47 -0
- package/package/validation/validator.js +303 -0
- package/package.json +5 -3
- package/rate-limiting/background/fixed-frequency.js +0 -2
- package/test.js +2 -2
- package/transports/batch-warming.d.ts +4 -3
- package/transports/batch-warming.js +4 -4
- package/transports/index.d.ts +4 -21
- package/transports/index.js +3 -3
- package/transports/metrics.d.ts +1 -1
- package/transports/metrics.js +2 -2
- package/transports/rest.d.ts +2 -1
- package/transports/rest.js +3 -1
- package/transports/websocket.d.ts +5 -4
- package/transports/websocket.js +5 -6
- package/util/index.d.ts +2 -1
- package/util/index.js +1 -1
- package/util/request.d.ts +3 -1
- package/util/subscription-set/expiring-sorted-set.d.ts +22 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.d.ts +18 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.d.ts +25 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.d.ts +2 -2
- package/validation/error.js +1 -1
- package/validation/index.js +8 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpiringSortedSet = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This class implements a set of unique items, each of which has an expiration timestamp.
|
|
6
|
+
* On reads, items that have expired will be deleted from the set and not returned.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - the type of the set entries' values
|
|
9
|
+
*/
|
|
10
|
+
class ExpiringSortedSet {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.map = new Map();
|
|
13
|
+
}
|
|
14
|
+
add(key, value, ttl) {
|
|
15
|
+
this.map.set(key, {
|
|
16
|
+
value,
|
|
17
|
+
expirationTimestamp: Date.now() + ttl,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
get(key) {
|
|
21
|
+
const entry = this.map.get(key);
|
|
22
|
+
if (!entry) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
else if (entry.expirationTimestamp < Date.now()) {
|
|
26
|
+
return entry.value;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.map.delete(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
getAll() {
|
|
33
|
+
const results = [];
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
// Since we're iterating, might as well prune here
|
|
36
|
+
for (const [key, entry] of this.map.entries()) {
|
|
37
|
+
if (entry.expirationTimestamp < now) {
|
|
38
|
+
this.map.delete(key); // In theory, this shouldn't happen frequently for feeds
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
results.push(entry.value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return results;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ExpiringSortedSet = ExpiringSortedSet;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './request';
|
|
2
|
+
export * from './logger';
|
|
3
|
+
export * from './expiring-sorted-set';
|
|
4
|
+
/**
|
|
5
|
+
* Sleeps for the provided number of milliseconds
|
|
6
|
+
* @param ms - The number of milliseconds to sleep for
|
|
7
|
+
* @returns a Promise that resolves once the specified time passes
|
|
8
|
+
*/
|
|
9
|
+
export declare const sleep: (ms: number) => Promise<void>;
|
|
10
|
+
export declare const isObject: (o: unknown) => boolean;
|
|
11
|
+
export declare const isArray: (o: unknown) => boolean;
|
|
@@ -0,0 +1,35 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.isArray = exports.isObject = exports.sleep = void 0;
|
|
18
|
+
__exportStar(require("./request"), exports);
|
|
19
|
+
__exportStar(require("./logger"), exports);
|
|
20
|
+
__exportStar(require("./expiring-sorted-set"), exports);
|
|
21
|
+
/**
|
|
22
|
+
* Sleeps for the provided number of milliseconds
|
|
23
|
+
* @param ms - The number of milliseconds to sleep for
|
|
24
|
+
* @returns a Promise that resolves once the specified time passes
|
|
25
|
+
*/
|
|
26
|
+
const sleep = (ms) => {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
setTimeout(resolve, ms);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
exports.sleep = sleep;
|
|
32
|
+
const isObject = (o) => o !== null && typeof o === 'object' && Array.isArray(o) === false;
|
|
33
|
+
exports.isObject = isObject;
|
|
34
|
+
const isArray = (o) => o !== null && typeof o === 'object' && Array.isArray(o);
|
|
35
|
+
exports.isArray = isArray;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import pino from 'pino';
|
|
3
|
+
import { AdapterRequest } from './request';
|
|
4
|
+
import { FastifyReply, HookHandlerDoneFunction } from 'fastify';
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
6
|
+
export declare const asyncLocalStorage: AsyncLocalStorage<unknown>;
|
|
7
|
+
export declare type Store = {
|
|
8
|
+
correlationId: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Instead of using a global logger instance, we want to force using a child logger
|
|
12
|
+
* with a specific layer set in it, so that we can filter logs by where they're output from.
|
|
13
|
+
*
|
|
14
|
+
* Details on what each log level represents:
|
|
15
|
+
* "trace": Forensic debugging of issues on a local machine.
|
|
16
|
+
* "debug": Detailed logging level to get more context from users on their environments.
|
|
17
|
+
* "info": High-level informational messages, to describe at a glance the high level state of the system.
|
|
18
|
+
* "warn": A mild error occurred that might require non-urgent action.
|
|
19
|
+
* "error": An unexpected error occurred during the regular operation of a well-maintained EA.
|
|
20
|
+
* "fatal": The EA encountered an unrecoverable problem and had to exit.
|
|
21
|
+
*
|
|
22
|
+
* Full reference this is based on can be found at
|
|
23
|
+
* https://github.com/smartcontractkit/documentation/blob/main/docs/Node%20Operators/configuration-variables.md#log_level
|
|
24
|
+
*
|
|
25
|
+
* @param layer - the layer name to include in the logs (e.g. "SomeMiddleware", "RedisCache", etc.)
|
|
26
|
+
* @returns a layer specific logger
|
|
27
|
+
*/
|
|
28
|
+
export declare const makeLogger: (layer: string) => pino.Logger<{
|
|
29
|
+
level: string;
|
|
30
|
+
mixin(): {};
|
|
31
|
+
transport: {
|
|
32
|
+
target: string;
|
|
33
|
+
options: {
|
|
34
|
+
levelFirst: boolean;
|
|
35
|
+
levelLabel: string;
|
|
36
|
+
ignore: string;
|
|
37
|
+
messageFormat: string;
|
|
38
|
+
translateTime: string;
|
|
39
|
+
};
|
|
40
|
+
} | undefined;
|
|
41
|
+
} & pino.ChildLoggerOptions>;
|
|
42
|
+
export declare const loggingContextMiddleware: (req: AdapterRequest, res: FastifyReply, done: HookHandlerDoneFunction) => void;
|
|
@@ -0,0 +1,62 @@
|
|
|
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.loggingContextMiddleware = exports.makeLogger = exports.asyncLocalStorage = void 0;
|
|
7
|
+
const pino_1 = __importDefault(require("pino"));
|
|
8
|
+
const config_1 = require("../config");
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
11
|
+
exports.asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
12
|
+
const debugTransport = {
|
|
13
|
+
target: 'pino-pretty',
|
|
14
|
+
options: {
|
|
15
|
+
levelFirst: true,
|
|
16
|
+
levelLabel: 'level',
|
|
17
|
+
ignore: 'layer,pid,hostname',
|
|
18
|
+
messageFormat: '[{correlationId}][{layer}] {msg}',
|
|
19
|
+
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
// Base logger, shouldn't be used because we want layers to be specified
|
|
23
|
+
const baseLogger = (0, pino_1.default)({
|
|
24
|
+
level: process.env['LOG_LEVEL']?.toLowerCase() || config_1.BaseSettings.LOG_LEVEL.default,
|
|
25
|
+
mixin() {
|
|
26
|
+
if (process.env['CORRELATION_ID_ENABLED'] === 'true') {
|
|
27
|
+
const store = exports.asyncLocalStorage.getStore();
|
|
28
|
+
if (store) {
|
|
29
|
+
return store;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return {};
|
|
33
|
+
},
|
|
34
|
+
transport: process.env['DEBUG'] === 'true' ? debugTransport : undefined,
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Instead of using a global logger instance, we want to force using a child logger
|
|
38
|
+
* with a specific layer set in it, so that we can filter logs by where they're output from.
|
|
39
|
+
*
|
|
40
|
+
* Details on what each log level represents:
|
|
41
|
+
* "trace": Forensic debugging of issues on a local machine.
|
|
42
|
+
* "debug": Detailed logging level to get more context from users on their environments.
|
|
43
|
+
* "info": High-level informational messages, to describe at a glance the high level state of the system.
|
|
44
|
+
* "warn": A mild error occurred that might require non-urgent action.
|
|
45
|
+
* "error": An unexpected error occurred during the regular operation of a well-maintained EA.
|
|
46
|
+
* "fatal": The EA encountered an unrecoverable problem and had to exit.
|
|
47
|
+
*
|
|
48
|
+
* Full reference this is based on can be found at
|
|
49
|
+
* https://github.com/smartcontractkit/documentation/blob/main/docs/Node%20Operators/configuration-variables.md#log_level
|
|
50
|
+
*
|
|
51
|
+
* @param layer - the layer name to include in the logs (e.g. "SomeMiddleware", "RedisCache", etc.)
|
|
52
|
+
* @returns a layer specific logger
|
|
53
|
+
*/
|
|
54
|
+
const makeLogger = (layer) => baseLogger.child({ layer });
|
|
55
|
+
exports.makeLogger = makeLogger;
|
|
56
|
+
const loggingContextMiddleware = (req, res, done) => {
|
|
57
|
+
const correlationId = req.headers['x-correlation-id'] || (0, crypto_1.randomUUID)();
|
|
58
|
+
exports.asyncLocalStorage.run({ correlationId: correlationId }, () => {
|
|
59
|
+
done();
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
exports.loggingContextMiddleware = loggingContextMiddleware;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify';
|
|
2
|
+
import { InitializedAdapter } from '../adapter';
|
|
3
|
+
import { AdapterError } from '../validation/error';
|
|
4
|
+
declare module 'fastify' {
|
|
5
|
+
interface FastifyRequest {
|
|
6
|
+
requestContext: AdapterRequestContext;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export interface AdapterRequestBody<T = AdapterRequestData> {
|
|
10
|
+
endpoint?: string;
|
|
11
|
+
data: T;
|
|
12
|
+
id?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare type AdapterRequestContext<T = AdapterRequestData> = {
|
|
15
|
+
id: string;
|
|
16
|
+
endpointName: string;
|
|
17
|
+
cacheKey: string;
|
|
18
|
+
data: T;
|
|
19
|
+
meta?: AdapterRequestMeta;
|
|
20
|
+
};
|
|
21
|
+
export declare type AdapterRouteGeneric<T = AdapterRequestData> = {
|
|
22
|
+
Body: AdapterRequestBody<T>;
|
|
23
|
+
};
|
|
24
|
+
export declare type AdapterRequest<T = AdapterRequestData> = FastifyRequest<AdapterRouteGeneric<T>> & {
|
|
25
|
+
requestContext: AdapterRequestContext<T>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Meta info that pertains to exposing metrics
|
|
29
|
+
*/
|
|
30
|
+
export interface AdapterRequestMeta {
|
|
31
|
+
metrics?: AdapterMetricsMeta;
|
|
32
|
+
error?: AdapterError | Error;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Meta info that pertains to exposing metrics
|
|
36
|
+
*/
|
|
37
|
+
export interface AdapterMetricsMeta {
|
|
38
|
+
feedId?: string;
|
|
39
|
+
cacheHit?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export declare type AdapterRequestData = Record<string, unknown>;
|
|
42
|
+
export interface ProviderResult<Params> {
|
|
43
|
+
params: Params;
|
|
44
|
+
value: unknown;
|
|
45
|
+
}
|
|
46
|
+
export declare type AdapterResponse<T = unknown> = {
|
|
47
|
+
statusCode: number;
|
|
48
|
+
data: T;
|
|
49
|
+
result: unknown;
|
|
50
|
+
maxAge?: number;
|
|
51
|
+
meta?: AdapterRequestMeta;
|
|
52
|
+
providerStatusCode?: number;
|
|
53
|
+
};
|
|
54
|
+
export declare type Middleware = ((req: AdapterRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => FastifyReply | void) | ((req: AdapterRequest, reply: FastifyReply) => Promise<FastifyReply | void>);
|
|
55
|
+
export declare type AdapterMiddlewareBuilder = (adapter: InitializedAdapter) => Middleware;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HttpRequestType } from "../metrics/constants";
|
|
2
|
+
declare type ErrorBasic = {
|
|
3
|
+
name: string;
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
declare type ErrorFull = ErrorBasic & {
|
|
7
|
+
stack: string;
|
|
8
|
+
cause: string;
|
|
9
|
+
};
|
|
10
|
+
export declare type AdapterErrorResponse = {
|
|
11
|
+
jobRunID: string;
|
|
12
|
+
status: string;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
providerStatusCode?: number;
|
|
15
|
+
error: ErrorBasic | ErrorFull;
|
|
16
|
+
};
|
|
17
|
+
export declare class AdapterError extends Error {
|
|
18
|
+
jobRunID: string;
|
|
19
|
+
status: string;
|
|
20
|
+
statusCode: number;
|
|
21
|
+
cause: any;
|
|
22
|
+
url?: string;
|
|
23
|
+
errorResponse: any;
|
|
24
|
+
feedID?: string;
|
|
25
|
+
providerStatusCode?: number;
|
|
26
|
+
metricsLabel?: HttpRequestType;
|
|
27
|
+
name: string;
|
|
28
|
+
message: string;
|
|
29
|
+
constructor({ jobRunID, status, statusCode, name, message, cause, url, errorResponse, feedID, providerStatusCode, metricsLabel }: Partial<AdapterError>);
|
|
30
|
+
toJSONResponse(): AdapterErrorResponse;
|
|
31
|
+
}
|
|
32
|
+
export declare class AdapterInputError extends AdapterError {
|
|
33
|
+
constructor(input: Partial<AdapterError>);
|
|
34
|
+
}
|
|
35
|
+
export declare class AdapterRateLimitError extends AdapterError {
|
|
36
|
+
constructor(input: Partial<AdapterError>);
|
|
37
|
+
}
|
|
38
|
+
export declare class AdapterTimeoutError extends AdapterError {
|
|
39
|
+
constructor(input: Partial<AdapterError>);
|
|
40
|
+
}
|
|
41
|
+
export declare class AdapterDataProviderError extends AdapterError {
|
|
42
|
+
constructor(input: Partial<AdapterError>);
|
|
43
|
+
}
|
|
44
|
+
export declare class AdapterConnectionError extends AdapterError {
|
|
45
|
+
constructor(input: Partial<AdapterError>);
|
|
46
|
+
}
|
|
47
|
+
export declare class AdapterCustomError extends AdapterError {
|
|
48
|
+
constructor(input: Partial<AdapterError>);
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,79 @@
|
|
|
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;
|
|
@@ -0,0 +1,5 @@
|
|
|
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;
|
|
@@ -0,0 +1,86 @@
|
|
|
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;
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -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,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,47 @@
|
|
|
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 {};
|