@chainlink/external-adapter-framework 0.0.15 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapter.js +128 -0
- package/background-executor.js +45 -0
- package/cache/factory.js +58 -0
- package/cache/index.js +173 -0
- package/cache/local.js +83 -0
- package/cache/metrics.js +120 -0
- package/cache/redis.js +100 -0
- package/chainlink-external-adapter-framework-v0.0.6.tgz +0 -0
- package/config/index.js +366 -0
- package/config/provider-limits.js +74 -0
- package/examples/bank-frick/accounts.js +192 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.js +15 -0
- package/examples/bank-frick/util.js +39 -0
- package/examples/coingecko/src/config/index.js +13 -0
- package/examples/coingecko/src/config/overrides.json +10826 -0
- package/examples/coingecko/src/cryptoUtils.js +41 -0
- package/examples/coingecko/src/endpoint/coins.js +33 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +46 -0
- package/examples/coingecko/src/endpoint/crypto-volume.js +46 -0
- package/examples/coingecko/src/endpoint/crypto.js +47 -0
- package/examples/coingecko/src/endpoint/dominance.js +26 -0
- package/examples/coingecko/src/endpoint/global-marketcap.js +26 -0
- package/examples/coingecko/src/endpoint/index.js +15 -0
- package/examples/coingecko/src/globalUtils.js +48 -0
- package/examples/coingecko/src/index.js +14 -0
- package/examples/coingecko/test/e2e/adapter.test.js +262 -0
- package/examples/coingecko/test/integration/adapter.test.js +264 -0
- package/examples/coingecko/test/integration/capturedRequests.json +1 -0
- package/examples/coingecko/test/integration/fixtures.js +41 -0
- package/examples/coingecko-old/batch-warming.js +53 -0
- package/examples/coingecko-old/index.js +11 -0
- package/examples/coingecko-old/rest.js +51 -0
- package/examples/ncfx/config/index.js +15 -0
- package/examples/ncfx/index.js +11 -0
- package/examples/ncfx/websocket.js +73 -0
- package/index.js +127 -0
- package/metrics/constants.js +25 -0
- package/metrics/index.js +122 -0
- package/metrics/util.js +9 -0
- package/package.json +5 -15
- package/rate-limiting/background/fixed-frequency.js +35 -0
- package/rate-limiting/index.js +84 -0
- package/rate-limiting/metrics.js +44 -0
- package/rate-limiting/request/simple-counting.js +62 -0
- package/test.js +6 -0
- package/transports/batch-warming.js +101 -0
- package/transports/index.js +87 -0
- package/transports/metrics.js +105 -0
- package/transports/rest.js +138 -0
- package/transports/util.js +86 -0
- package/transports/websocket.js +166 -0
- package/util/index.js +35 -0
- package/util/logger.js +62 -0
- package/util/recordRequests.js +45 -0
- package/util/request.js +2 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.js +79 -0
- package/validation/index.js +91 -0
- package/validation/input-params.js +30 -0
- package/validation/override-functions.js +40 -0
- package/validation/overrideFunctions.js +40 -0
- package/validation/preset-tokens.json +23 -0
- package/validation/validator.js +303 -0
- package/.c8rc.json +0 -3
- package/.eslintignore +0 -10
- package/.eslintrc.js +0 -96
- package/.github/README.MD +0 -42
- package/.github/actions/setup/action.yaml +0 -13
- package/.github/workflows/label.yaml +0 -39
- package/.github/workflows/main.yaml +0 -39
- package/.github/workflows/publish.yaml +0 -17
- package/.prettierignore +0 -13
- package/.yarnrc +0 -0
- package/README.md +0 -103
- package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +0 -82953
- package/dist/examples/coingecko/test/integration/adapter.test.ts.js +0 -91672
- package/dist/main.js +0 -72703
- package/docker-compose.yaml +0 -35
- package/env.sh +0 -54
- package/env2.sh +0 -55
- package/jest.config.js +0 -5
- package/publish.sh +0 -0
- package/src/adapter.ts +0 -263
- package/src/background-executor.ts +0 -52
- package/src/cache/factory.ts +0 -26
- package/src/cache/index.ts +0 -258
- package/src/cache/local.ts +0 -73
- package/src/cache/metrics.ts +0 -112
- package/src/cache/redis.ts +0 -93
- package/src/config/index.ts +0 -517
- package/src/config/provider-limits.ts +0 -127
- package/src/examples/bank-frick/README.MD +0 -10
- package/src/examples/bank-frick/accounts.ts +0 -246
- package/src/examples/bank-frick/config/index.ts +0 -53
- package/src/examples/bank-frick/index.ts +0 -13
- package/src/examples/bank-frick/types.d.ts +0 -38
- package/src/examples/bank-frick/util.ts +0 -55
- package/src/examples/coingecko/src/config/index.ts +0 -12
- package/src/examples/coingecko/src/config/overrides.json +0 -10826
- package/src/examples/coingecko/src/cryptoUtils.ts +0 -88
- package/src/examples/coingecko/src/endpoint/coins.ts +0 -54
- package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto-volume.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto.ts +0 -63
- package/src/examples/coingecko/src/endpoint/dominance.ts +0 -40
- package/src/examples/coingecko/src/endpoint/global-marketcap.ts +0 -40
- package/src/examples/coingecko/src/endpoint/index.ts +0 -6
- package/src/examples/coingecko/src/globalUtils.ts +0 -78
- package/src/examples/coingecko/src/index.ts +0 -17
- package/src/examples/coingecko/test/e2e/adapter.test.ts +0 -278
- package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +0 -15
- package/src/examples/coingecko/test/integration/adapter.test.ts +0 -281
- package/src/examples/coingecko/test/integration/capturedRequests.json +0 -1
- package/src/examples/coingecko/test/integration/fixtures.ts +0 -42
- package/src/examples/coingecko-old/batch-warming.ts +0 -79
- package/src/examples/coingecko-old/index.ts +0 -9
- package/src/examples/coingecko-old/rest.ts +0 -77
- package/src/examples/ncfx/config/index.ts +0 -12
- package/src/examples/ncfx/index.ts +0 -9
- package/src/examples/ncfx/websocket.ts +0 -99
- package/src/index.ts +0 -149
- package/src/metrics/constants.ts +0 -23
- package/src/metrics/index.ts +0 -115
- package/src/metrics/util.ts +0 -18
- package/src/rate-limiting/background/fixed-frequency.ts +0 -45
- package/src/rate-limiting/index.ts +0 -100
- package/src/rate-limiting/metrics.ts +0 -18
- package/src/rate-limiting/request/simple-counting.ts +0 -76
- package/src/transports/batch-warming.ts +0 -127
- package/src/transports/index.ts +0 -152
- package/src/transports/metrics.ts +0 -95
- package/src/transports/rest.ts +0 -168
- package/src/transports/util.ts +0 -63
- package/src/transports/websocket.ts +0 -245
- package/src/util/index.ts +0 -23
- package/src/util/logger.ts +0 -69
- package/src/util/recordRequests.ts +0 -47
- package/src/util/request.ts +0 -117
- package/src/util/subscription-set/expiring-sorted-set.ts +0 -54
- package/src/util/subscription-set/subscription-set.ts +0 -35
- package/src/util/test-payload-loader.ts +0 -87
- package/src/validation/error.ts +0 -116
- package/src/validation/index.ts +0 -110
- package/src/validation/input-params.ts +0 -45
- package/src/validation/override-functions.ts +0 -44
- package/src/validation/overrideFunctions.ts +0 -44
- package/src/validation/preset-tokens.json +0 -23
- package/src/validation/validator.ts +0 -384
- package/test/adapter.test.ts +0 -27
- package/test/background-executor.test.ts +0 -108
- package/test/cache/cache-key.test.ts +0 -114
- package/test/cache/helper.ts +0 -100
- package/test/cache/local.test.ts +0 -54
- package/test/cache/redis.test.ts +0 -89
- package/test/correlation.test.ts +0 -114
- package/test/index.test.ts +0 -37
- package/test/metrics/feed-id.test.ts +0 -38
- package/test/metrics/helper.ts +0 -14
- package/test/metrics/labels.test.ts +0 -36
- package/test/metrics/metrics.test.ts +0 -267
- package/test/metrics/redis-metrics.test.ts +0 -113
- package/test/metrics/warmer-metrics.test.ts +0 -193
- package/test/metrics/ws-metrics.test.ts +0 -225
- package/test/rate-limit-config.test.ts +0 -242
- package/test/smoke/smoke.test.ts +0 -166
- package/test/smoke/test-payload-fail.json +0 -3
- package/test/smoke/test-payload.js +0 -22
- package/test/smoke/test-payload.json +0 -7
- package/test/transports/batch.test.ts +0 -466
- package/test/transports/rest.test.ts +0 -242
- package/test/transports/websocket.test.ts +0 -183
- package/test/tsconfig.json +0 -5
- package/test/util.ts +0 -77
- package/test/validation.test.ts +0 -178
- package/test.sh +0 -20
- package/test2.sh +0 -2
- package/tsconfig.json +0 -28
- package/typedoc.json +0 -6
- package/webpack.config.js +0 -57
- package/yarn-error.log +0 -3778
package/src/util/request.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify'
|
|
2
|
-
import { InitializedAdapter } from '../adapter'
|
|
3
|
-
import { AdapterError } from '../validation/error'
|
|
4
|
-
declare module 'fastify' {
|
|
5
|
-
// eslint-disable-next-line no-shadow
|
|
6
|
-
export interface FastifyRequest {
|
|
7
|
-
requestContext: AdapterRequestContext
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface AdapterRequestBody<T = AdapterRequestData> {
|
|
12
|
-
endpoint?: string
|
|
13
|
-
data: T
|
|
14
|
-
id?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type AdapterRequestContext<T = AdapterRequestData> = {
|
|
18
|
-
id: string
|
|
19
|
-
endpointName: string
|
|
20
|
-
cacheKey: string
|
|
21
|
-
data: T // AdapterRequestData
|
|
22
|
-
meta?: AdapterRequestMeta
|
|
23
|
-
// MetricsMeta?: AdapterMetricsMeta
|
|
24
|
-
// Debug?: AdapterDebug
|
|
25
|
-
// RateLimitMaxAge?: number
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type AdapterRouteGeneric<T = AdapterRequestData> = {
|
|
29
|
-
Body: AdapterRequestBody<T>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type AdapterRequest<T = AdapterRequestData> = FastifyRequest<AdapterRouteGeneric<T>> & {
|
|
33
|
-
// TODO: Uncomment line below & fix compile issues
|
|
34
|
-
// body: never // So the user does not access the raw request data and uses the Validated one instead
|
|
35
|
-
requestContext: AdapterRequestContext<T>
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Export type AdapterRequestMeta = {
|
|
39
|
-
// AvailableFunds?: number
|
|
40
|
-
// EligibleToSubmit?: boolean
|
|
41
|
-
// LatestAnswer?: number
|
|
42
|
-
// OracleCount?: number
|
|
43
|
-
// PaymentAmount?: number
|
|
44
|
-
// ReportableRoundID?: number
|
|
45
|
-
// StartedAt?: number
|
|
46
|
-
// Timeout?: number
|
|
47
|
-
// }
|
|
48
|
-
|
|
49
|
-
// Export type AdapterDebug = {
|
|
50
|
-
// Ws?: boolean
|
|
51
|
-
// Warmer?: boolean
|
|
52
|
-
// CacheHit?: boolean
|
|
53
|
-
// Staleness?: number
|
|
54
|
-
// Performance?: number
|
|
55
|
-
// ProviderCost?: number
|
|
56
|
-
// BatchablePropertyPath?: cacheWarmer.reducer.BatchableProperty[]
|
|
57
|
-
// NormalizedRequest?: Record<string, unknown>
|
|
58
|
-
// }
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Meta info that pertains to exposing metrics
|
|
62
|
-
*/
|
|
63
|
-
export interface AdapterRequestMeta {
|
|
64
|
-
metrics?: AdapterMetricsMeta
|
|
65
|
-
error?: AdapterError | Error
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Meta info that pertains to exposing metrics
|
|
70
|
-
*/
|
|
71
|
-
export interface AdapterMetricsMeta {
|
|
72
|
-
feedId?: string
|
|
73
|
-
cacheHit?: boolean
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export type AdapterRequestData = Record<string, unknown> & {
|
|
77
|
-
endpoint?: string
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Export interface AdapterRequest<T = AdapterRequestData> extends Request {
|
|
81
|
-
// AdapterContext: {
|
|
82
|
-
// Id: string
|
|
83
|
-
// Endpoint: string
|
|
84
|
-
// Data: T //AdapterRequestData
|
|
85
|
-
// CacheKey: string
|
|
86
|
-
// // meta?: AdapterRequestMeta
|
|
87
|
-
// // metricsMeta?: AdapterMetricsMeta
|
|
88
|
-
// // debug?: AdapterDebug
|
|
89
|
-
// // rateLimitMaxAge?: number
|
|
90
|
-
// }
|
|
91
|
-
// }
|
|
92
|
-
|
|
93
|
-
export interface ProviderResult<Params> {
|
|
94
|
-
params: Params
|
|
95
|
-
value: unknown
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export type AdapterResponse<T = unknown> = {
|
|
99
|
-
// JobRunID: string
|
|
100
|
-
statusCode: number
|
|
101
|
-
data: T // Response data, holds "result" for Flux Monitor. Correct way.
|
|
102
|
-
result: unknown // Result for OCR
|
|
103
|
-
maxAge?: number // TODO: Replace with new telemetry structure
|
|
104
|
-
meta?: AdapterRequestMeta
|
|
105
|
-
// Debug?: AdapterDebug
|
|
106
|
-
providerStatusCode?: number
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export type Middleware =
|
|
110
|
-
| ((
|
|
111
|
-
req: AdapterRequest,
|
|
112
|
-
reply: FastifyReply,
|
|
113
|
-
done: HookHandlerDoneFunction,
|
|
114
|
-
) => FastifyReply | void)
|
|
115
|
-
| ((req: AdapterRequest, reply: FastifyReply) => Promise<FastifyReply | void>)
|
|
116
|
-
|
|
117
|
-
export type AdapterMiddlewareBuilder = (adapter: InitializedAdapter) => Middleware
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { SubscriptionSet } from './subscription-set'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* An object describing an entry in the expiring sorted set.
|
|
5
|
-
* @typeParam T - the type of the entry's value
|
|
6
|
-
*/
|
|
7
|
-
interface ExpiringSortedSetEntry<T> {
|
|
8
|
-
value: T
|
|
9
|
-
expirationTimestamp: number
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* This class implements a set of unique items, each of which has an expiration timestamp.
|
|
14
|
-
* On reads, items that have expired will be deleted from the set and not returned.
|
|
15
|
-
*
|
|
16
|
-
* @typeParam T - the type of the set entries' values
|
|
17
|
-
*/
|
|
18
|
-
export class ExpiringSortedSet<T> implements SubscriptionSet<T> {
|
|
19
|
-
map = new Map<string, ExpiringSortedSetEntry<T>>()
|
|
20
|
-
|
|
21
|
-
add(key: string, value: T, ttl: number) {
|
|
22
|
-
this.map.set(key, {
|
|
23
|
-
value,
|
|
24
|
-
expirationTimestamp: Date.now() + ttl,
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get(key: string) {
|
|
29
|
-
const entry = this.map.get(key)
|
|
30
|
-
if (!entry) {
|
|
31
|
-
return
|
|
32
|
-
} else if (entry.expirationTimestamp < Date.now()) {
|
|
33
|
-
return entry.value
|
|
34
|
-
} else {
|
|
35
|
-
this.map.delete(key)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
getAll(): T[] {
|
|
40
|
-
const results: T[] = []
|
|
41
|
-
const now = Date.now()
|
|
42
|
-
|
|
43
|
-
// Since we're iterating, might as well prune here
|
|
44
|
-
for (const [key, entry] of this.map.entries()) {
|
|
45
|
-
if (entry.expirationTimestamp < now) {
|
|
46
|
-
this.map.delete(key) // In theory, this shouldn't happen frequently for feeds
|
|
47
|
-
} else {
|
|
48
|
-
results.push(entry.value)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return results
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { PromiseOrValue } from '..'
|
|
2
|
-
import { AdapterConfig } from '../../config'
|
|
3
|
-
import { ExpiringSortedSet } from './expiring-sorted-set'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Set to hold items to subscribe to from a provider (regardless of protocol)
|
|
7
|
-
*/
|
|
8
|
-
export interface SubscriptionSet<T> {
|
|
9
|
-
/** Add a new subscription to the set */
|
|
10
|
-
add(key: string, value: T, ttl: number): PromiseOrValue<void>
|
|
11
|
-
|
|
12
|
-
/** Get a specific subscription from the set */
|
|
13
|
-
get(key: string): PromiseOrValue<T | undefined>
|
|
14
|
-
|
|
15
|
-
/** Get all subscriptions from the set as a list */
|
|
16
|
-
getAll(): PromiseOrValue<T[]>
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class SubscriptionSetFactory {
|
|
20
|
-
private cacheType: AdapterConfig['CACHE_TYPE']
|
|
21
|
-
|
|
22
|
-
constructor(config: AdapterConfig) {
|
|
23
|
-
this.cacheType = config.CACHE_TYPE
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
buildSet<T>(): SubscriptionSet<T> {
|
|
27
|
-
switch (this.cacheType) {
|
|
28
|
-
case 'local':
|
|
29
|
-
return new ExpiringSortedSet<T>()
|
|
30
|
-
case 'redis':
|
|
31
|
-
// TODO: Implement redis set
|
|
32
|
-
return new ExpiringSortedSet<T>()
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import Ajv from 'ajv'
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
import { makeLogger } from './logger'
|
|
5
|
-
import { AdapterRequestData } from './request'
|
|
6
|
-
|
|
7
|
-
const logger = makeLogger('TestPayloadLoader')
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The test payload read in from filesystem
|
|
11
|
-
*/
|
|
12
|
-
export interface Payload {
|
|
13
|
-
requests: Array<AdapterRequestData>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Test payload with discriminated union so we can tell when we should just do
|
|
18
|
-
* a simple liveness check rather than a sample request
|
|
19
|
-
*/
|
|
20
|
-
type TestPayload = (Payload & { isDefault: false }) | { isDefault: true }
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Load in a JSON file containing a test payload for the current adapter,
|
|
24
|
-
* used in healthchecks to make sample requests
|
|
25
|
-
*
|
|
26
|
-
* @param fileName - name of file that contains the test payload data for the smoke endpoint
|
|
27
|
-
* @returns the parsed payload with individual requests
|
|
28
|
-
*/
|
|
29
|
-
export function loadTestPayload(fileName?: string): TestPayload {
|
|
30
|
-
const ajv = new Ajv()
|
|
31
|
-
const schema = {
|
|
32
|
-
type: 'object',
|
|
33
|
-
required: ['requests'],
|
|
34
|
-
properties: {
|
|
35
|
-
requests: {
|
|
36
|
-
type: 'array',
|
|
37
|
-
items: {
|
|
38
|
-
type: 'object',
|
|
39
|
-
required: [],
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
const validate = ajv.compile(schema)
|
|
45
|
-
try {
|
|
46
|
-
const payload = resolvePayload(fileName)
|
|
47
|
-
if (!validate(payload) || !payload?.requests) {
|
|
48
|
-
throw Error(JSON.stringify(validate?.errors || 'Could not validate schema for test payload'))
|
|
49
|
-
}
|
|
50
|
-
return { ...payload, isDefault: false }
|
|
51
|
-
} catch (e: unknown) {
|
|
52
|
-
logger.warn(`Could not load payload: ${(e as Error).message}`)
|
|
53
|
-
logger.warn('Falling back to default empty payload')
|
|
54
|
-
return { isDefault: true }
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function resolvePayload(fileName?: string): Payload | null {
|
|
59
|
-
try {
|
|
60
|
-
let payload = null
|
|
61
|
-
// Find test payload by specified name, fallback to default names if not found
|
|
62
|
-
if (fileName && fs.existsSync(path.join(process.cwd(), fileName))) {
|
|
63
|
-
payload = require(path.join(process.cwd(), fileName))
|
|
64
|
-
} else if (fileName && fs.existsSync(`./${fileName}`)) {
|
|
65
|
-
payload = require(`./${fileName}`)
|
|
66
|
-
}
|
|
67
|
-
// Search for test payload js first if no filename specified or found
|
|
68
|
-
else if (fs.existsSync(path.resolve('.', 'test-payload.js'))) {
|
|
69
|
-
payload = require(path.join(process.cwd(), 'test-payload.js'))
|
|
70
|
-
} else if (fs.existsSync(`./test-payload.js`)) {
|
|
71
|
-
payload = require(`./test-payload.js`)
|
|
72
|
-
}
|
|
73
|
-
// Search for test payload json second if no filename specified or found
|
|
74
|
-
else if (fs.existsSync(path.join(process.cwd(), 'test-payload.json'))) {
|
|
75
|
-
payload = require(path.join(process.cwd(), 'test-payload.json'))
|
|
76
|
-
} else if (fs.existsSync(`./test-payload.json`)) {
|
|
77
|
-
payload = require(`./test-payload.json`)
|
|
78
|
-
}
|
|
79
|
-
if (typeof payload === 'string') {
|
|
80
|
-
return JSON.parse(payload)
|
|
81
|
-
} else {
|
|
82
|
-
return payload
|
|
83
|
-
}
|
|
84
|
-
} catch {
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
}
|
package/src/validation/error.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { HttpRequestType } from '../metrics/constants'
|
|
2
|
-
|
|
3
|
-
type ErrorBasic = {
|
|
4
|
-
name: string
|
|
5
|
-
message: string
|
|
6
|
-
}
|
|
7
|
-
type ErrorFull = ErrorBasic & {
|
|
8
|
-
stack: string
|
|
9
|
-
cause: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type AdapterErrorResponse = {
|
|
13
|
-
jobRunID: string
|
|
14
|
-
status: string
|
|
15
|
-
statusCode: number
|
|
16
|
-
providerStatusCode?: number
|
|
17
|
-
error: ErrorBasic | ErrorFull
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class AdapterError extends Error {
|
|
21
|
-
jobRunID: string
|
|
22
|
-
status: string
|
|
23
|
-
statusCode: number
|
|
24
|
-
cause: any
|
|
25
|
-
url?: string
|
|
26
|
-
errorResponse: any
|
|
27
|
-
feedID?: string
|
|
28
|
-
providerStatusCode?: number
|
|
29
|
-
metricsLabel?: HttpRequestType
|
|
30
|
-
|
|
31
|
-
override name: string
|
|
32
|
-
override message: string
|
|
33
|
-
|
|
34
|
-
constructor({
|
|
35
|
-
jobRunID = '1',
|
|
36
|
-
status = 'errored',
|
|
37
|
-
statusCode = 500,
|
|
38
|
-
name = 'AdapterError',
|
|
39
|
-
message = 'An error occurred.',
|
|
40
|
-
cause,
|
|
41
|
-
url,
|
|
42
|
-
errorResponse,
|
|
43
|
-
feedID,
|
|
44
|
-
providerStatusCode,
|
|
45
|
-
metricsLabel = HttpRequestType.ADAPTER_ERROR,
|
|
46
|
-
}: Partial<AdapterError>) {
|
|
47
|
-
super(message)
|
|
48
|
-
|
|
49
|
-
this.jobRunID = jobRunID
|
|
50
|
-
this.status = status
|
|
51
|
-
this.statusCode = statusCode
|
|
52
|
-
this.name = name
|
|
53
|
-
this.message = message
|
|
54
|
-
this.cause = cause
|
|
55
|
-
if (url) {
|
|
56
|
-
this.url = url
|
|
57
|
-
}
|
|
58
|
-
if (feedID) {
|
|
59
|
-
this.feedID = feedID
|
|
60
|
-
}
|
|
61
|
-
this.errorResponse = errorResponse
|
|
62
|
-
this.providerStatusCode = providerStatusCode
|
|
63
|
-
this.metricsLabel = metricsLabel
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
toJSONResponse(): AdapterErrorResponse {
|
|
67
|
-
const showDebugInfo =
|
|
68
|
-
process.env['NODE_ENV'] === 'development' || process.env['DEBUG'] === 'true'
|
|
69
|
-
const errorBasic = {
|
|
70
|
-
name: this.name,
|
|
71
|
-
message: this.message,
|
|
72
|
-
url: this.url,
|
|
73
|
-
errorResponse: this.errorResponse,
|
|
74
|
-
feedID: this.feedID,
|
|
75
|
-
}
|
|
76
|
-
const errorFull = { ...errorBasic, stack: this.stack, cause: this.cause }
|
|
77
|
-
return {
|
|
78
|
-
jobRunID: this.jobRunID,
|
|
79
|
-
status: this.status,
|
|
80
|
-
statusCode: this.statusCode,
|
|
81
|
-
providerStatusCode: this.providerStatusCode,
|
|
82
|
-
error: showDebugInfo ? errorFull : errorBasic,
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export class AdapterInputError extends AdapterError {
|
|
88
|
-
constructor(input: Partial<AdapterError>) {
|
|
89
|
-
super({ ...input, metricsLabel: HttpRequestType.INPUT_ERROR })
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
export class AdapterRateLimitError extends AdapterError {
|
|
93
|
-
constructor(input: Partial<AdapterError>) {
|
|
94
|
-
super({ ...input, metricsLabel: HttpRequestType.RATE_LIMIT_ERROR })
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
export class AdapterTimeoutError extends AdapterError {
|
|
98
|
-
constructor(input: Partial<AdapterError>) {
|
|
99
|
-
super({ ...input, metricsLabel: HttpRequestType.TIMEOUT_ERROR })
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
export class AdapterDataProviderError extends AdapterError {
|
|
103
|
-
constructor(input: Partial<AdapterError>) {
|
|
104
|
-
super({ ...input, metricsLabel: HttpRequestType.DP_ERROR })
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
export class AdapterConnectionError extends AdapterError {
|
|
108
|
-
constructor(input: Partial<AdapterError>) {
|
|
109
|
-
super({ ...input, metricsLabel: HttpRequestType.CONNECTION_ERROR })
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
export class AdapterCustomError extends AdapterError {
|
|
113
|
-
constructor(input: Partial<AdapterError>) {
|
|
114
|
-
super({ ...input, metricsLabel: HttpRequestType.CUSTOM_ERROR })
|
|
115
|
-
}
|
|
116
|
-
}
|
package/src/validation/index.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify'
|
|
2
|
-
import { InitializedAdapter } from '../adapter'
|
|
3
|
-
import { calculateCacheKey } from '../cache'
|
|
4
|
-
import { getMetricsMeta } from '../metrics/util'
|
|
5
|
-
import { makeLogger } from '../util'
|
|
6
|
-
import { AdapterMiddlewareBuilder, AdapterRequest } from '../util/request'
|
|
7
|
-
import { AdapterError, AdapterInputError } from './error'
|
|
8
|
-
import { Validator } from './validator'
|
|
9
|
-
import { performSymbolOverrides } from './override-functions'
|
|
10
|
-
export { InputParameters } from './input-params'
|
|
11
|
-
|
|
12
|
-
const errorCatcherLogger = makeLogger('ErrorCatchingMiddleware')
|
|
13
|
-
|
|
14
|
-
export const validatorMiddleware: AdapterMiddlewareBuilder =
|
|
15
|
-
(adapter: InitializedAdapter) =>
|
|
16
|
-
(req: AdapterRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => {
|
|
17
|
-
if (req.headers['content-type'] !== 'application/json') {
|
|
18
|
-
throw new AdapterInputError({
|
|
19
|
-
message: 'Content type not "application/json", returning 400',
|
|
20
|
-
statusCode: 400,
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (!req.body) {
|
|
25
|
-
throw new AdapterInputError({
|
|
26
|
-
message: 'Body not present in adapter request, returning 400',
|
|
27
|
-
statusCode: 400,
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Make endpoints case insensitive
|
|
32
|
-
const endpointParam =
|
|
33
|
-
req.body.endpoint?.toLowerCase() ||
|
|
34
|
-
req.body.data?.endpoint?.toLowerCase() ||
|
|
35
|
-
adapter.defaultEndpoint
|
|
36
|
-
if (!endpointParam) {
|
|
37
|
-
throw new AdapterInputError({
|
|
38
|
-
message: `Request body does not specify an endpoint, and there is no default endpoint configured for this adapter.`,
|
|
39
|
-
statusCode: 400,
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const endpoint = adapter.endpointsMap[endpointParam]
|
|
44
|
-
if (!endpoint) {
|
|
45
|
-
throw new AdapterInputError({
|
|
46
|
-
message: `Adapter does not have a "${endpointParam}" endpoint.`,
|
|
47
|
-
statusCode: 404,
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Validate data using validator from v2
|
|
52
|
-
// TODO: See if we want to change this whole thing
|
|
53
|
-
const validator = new Validator(req.body, endpoint.inputParameters)
|
|
54
|
-
|
|
55
|
-
req.requestContext = {
|
|
56
|
-
id: req.body.id || '1',
|
|
57
|
-
cacheKey: '',
|
|
58
|
-
data: validator.validated.data,
|
|
59
|
-
endpointName: endpoint.name,
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
63
|
-
// Add metrics meta which includes feedId to the request
|
|
64
|
-
// Perform prior to overrides to maintain consistent Feed IDs across adapters
|
|
65
|
-
const metrics = getMetricsMeta(
|
|
66
|
-
{
|
|
67
|
-
adapterEndpoint: endpoint,
|
|
68
|
-
adapterConfig: adapter.config,
|
|
69
|
-
},
|
|
70
|
-
validator.validated.data,
|
|
71
|
-
)
|
|
72
|
-
req.requestContext = { ...req.requestContext, meta: { metrics } }
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// TODO: Support `includes` and `tokenOverrides` overrides as needed
|
|
76
|
-
const requestParams = req.requestContext.data ?? {}
|
|
77
|
-
if (requestParams['tokenOverrides']) {
|
|
78
|
-
throw new Error('Token overrides not yet supported')
|
|
79
|
-
}
|
|
80
|
-
if (requestParams['includes']) {
|
|
81
|
-
throw new Error('Includes not yet supported')
|
|
82
|
-
}
|
|
83
|
-
// Swaps the 'base' parameter if any overrides are found in the request or the adapter configuration
|
|
84
|
-
// Supports 'base' input as string or string[]
|
|
85
|
-
performSymbolOverrides(adapter, req)
|
|
86
|
-
|
|
87
|
-
req.requestContext.cacheKey = calculateCacheKey(
|
|
88
|
-
{
|
|
89
|
-
adapterEndpoint: endpoint,
|
|
90
|
-
adapterConfig: adapter.config,
|
|
91
|
-
},
|
|
92
|
-
req.requestContext.data,
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
done()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export const errorCatchingMiddleware = (err: Error, req: FastifyRequest, res: FastifyReply) => {
|
|
99
|
-
// Add adapter or generic error to request meta for metrics use
|
|
100
|
-
req.requestContext.meta = { ...req.requestContext.meta, error: err }
|
|
101
|
-
if (err instanceof AdapterError) {
|
|
102
|
-
// We want to log these as warn, because although they are to be expected, NOPs should
|
|
103
|
-
// Only use "correct" job specs and therefore not hit adapters with invalid requests.
|
|
104
|
-
errorCatcherLogger.warn(err)
|
|
105
|
-
res.status(err.statusCode).send(err.toJSONResponse())
|
|
106
|
-
} else {
|
|
107
|
-
errorCatcherLogger.error(err)
|
|
108
|
-
res.status(200).send('There was an unexpected error in the adapter.')
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/* INPUT TYPE VALIDATIONS */
|
|
2
|
-
export type Override = Map<string, Map<string, string>>
|
|
3
|
-
|
|
4
|
-
export type InputParameter = {
|
|
5
|
-
aliases?: string[]
|
|
6
|
-
description?: string
|
|
7
|
-
type?: 'bigint' | 'boolean' | 'array' | 'number' | 'object' | 'string'
|
|
8
|
-
required?: boolean
|
|
9
|
-
options?: unknown[] // Enumerated options, ex. ['ADA', 'BTC', 'ETH']
|
|
10
|
-
default?: unknown
|
|
11
|
-
dependsOn?: string[] // Other inputs this one depends on
|
|
12
|
-
exclusive?: string[] // Other inputs that cannot be present with this one
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type InputParameters = {
|
|
16
|
-
[name: string]: InputParameter | boolean | string[]
|
|
17
|
-
}
|
|
18
|
-
export const baseInputParameters: InputParameters = {
|
|
19
|
-
endpoint: {
|
|
20
|
-
description: 'The External Adapter "endpoint" name to use.',
|
|
21
|
-
required: false,
|
|
22
|
-
type: 'string',
|
|
23
|
-
},
|
|
24
|
-
resultPath: {
|
|
25
|
-
description: 'The path to key into the API response the retrieve the result',
|
|
26
|
-
required: false,
|
|
27
|
-
// Type: 'string', TODO: Once multiple types are supported this could be string or array of strings
|
|
28
|
-
},
|
|
29
|
-
overrides: {
|
|
30
|
-
description: 'Override the mapping of token symbols to another token symbol',
|
|
31
|
-
required: false,
|
|
32
|
-
// Type: 'string', TODO: Once complex types are supported this could be { [adapter: string]: { [token: string]: string } }
|
|
33
|
-
},
|
|
34
|
-
tokenOverrides: {
|
|
35
|
-
description: 'Override the mapping of token symbols to smart contract address',
|
|
36
|
-
required: false,
|
|
37
|
-
// Type: 'string', TODO: Once complex types are supported this could be { [network: string]: { [token: string]: string } }
|
|
38
|
-
},
|
|
39
|
-
includes: {
|
|
40
|
-
description:
|
|
41
|
-
'Override the array of includes that holds additional input parameters when matching a pair of symbols',
|
|
42
|
-
required: false,
|
|
43
|
-
// 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 }] } }[]
|
|
44
|
-
},
|
|
45
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { InitializedAdapter } from '../adapter'
|
|
2
|
-
import { AdapterRequest } from '../util/request'
|
|
3
|
-
|
|
4
|
-
type Overrides = Record<string, Record<string, string>>
|
|
5
|
-
|
|
6
|
-
export const performSymbolOverrides = (adapter: InitializedAdapter, req: AdapterRequest) => {
|
|
7
|
-
let adapterOverrides = {} as Record<string, string>
|
|
8
|
-
if (
|
|
9
|
-
req.requestContext.data &&
|
|
10
|
-
req.requestContext.data['overrides'] &&
|
|
11
|
-
(req.requestContext.data['overrides'] as Overrides)[adapter.name]
|
|
12
|
-
) {
|
|
13
|
-
adapterOverrides = (req.requestContext.data['overrides'] as Overrides)[adapter.name]
|
|
14
|
-
}
|
|
15
|
-
if (!Array.isArray(req.requestContext.data['base'])) {
|
|
16
|
-
// Perform overrides specified in the request payload
|
|
17
|
-
req.requestContext.data['base'] =
|
|
18
|
-
adapterOverrides[req.requestContext.data['base'] as string] ?? req.requestContext.data['base']
|
|
19
|
-
// Perform hardcoded overrides
|
|
20
|
-
if (adapter.overrides) {
|
|
21
|
-
req.requestContext.data['base'] =
|
|
22
|
-
adapter.overrides[req.requestContext.data['base'] as string] ??
|
|
23
|
-
req.requestContext.data['base']
|
|
24
|
-
}
|
|
25
|
-
return
|
|
26
|
-
}
|
|
27
|
-
let requestedSymbols = req.requestContext.data['base']
|
|
28
|
-
for (let i = 0; i < requestedSymbols.length; i++) {
|
|
29
|
-
const symbol = requestedSymbols[i]
|
|
30
|
-
// Perform overrides specified in the request payload
|
|
31
|
-
let overriddenSymbol =
|
|
32
|
-
adapterOverrides[symbol.toUpperCase()] ?? adapterOverrides[symbol.toLowerCase()]
|
|
33
|
-
// Perform hardcoded overrides
|
|
34
|
-
if (adapter.overrides) {
|
|
35
|
-
overriddenSymbol =
|
|
36
|
-
adapter.overrides[symbol.toUpperCase()] ??
|
|
37
|
-
adapter.overrides[symbol.toLowerCase()] ??
|
|
38
|
-
overriddenSymbol
|
|
39
|
-
}
|
|
40
|
-
requestedSymbols[i] = overriddenSymbol ?? requestedSymbols[i]
|
|
41
|
-
}
|
|
42
|
-
requestedSymbols = requestedSymbols.length > 1 ? requestedSymbols : requestedSymbols[1]
|
|
43
|
-
req.requestContext.data['base'] = requestedSymbols
|
|
44
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { InitializedAdapter } from '../adapter'
|
|
2
|
-
import { AdapterRequest } from '../util/request'
|
|
3
|
-
|
|
4
|
-
type Overrides = Record<string, Record<string, string>>
|
|
5
|
-
|
|
6
|
-
export const performSymbolOverrides = (adapter: InitializedAdapter, req: AdapterRequest) => {
|
|
7
|
-
let adapterOverrides = {} as Record<string, string>
|
|
8
|
-
if (
|
|
9
|
-
req.requestContext.data &&
|
|
10
|
-
req.requestContext.data['overrides'] &&
|
|
11
|
-
(req.requestContext.data['overrides'] as Overrides)[adapter.name]
|
|
12
|
-
) {
|
|
13
|
-
adapterOverrides = (req.requestContext.data['overrides'] as Overrides)[adapter.name]
|
|
14
|
-
}
|
|
15
|
-
if (!Array.isArray(req.requestContext.data['base'])) {
|
|
16
|
-
// Perform overrides specified in the request payload
|
|
17
|
-
req.requestContext.data['base'] =
|
|
18
|
-
adapterOverrides[req.requestContext.data['base'] as string] ?? req.requestContext.data['base']
|
|
19
|
-
// Perform hardcoded overrides
|
|
20
|
-
if (adapter.overrides) {
|
|
21
|
-
req.requestContext.data['base'] =
|
|
22
|
-
adapter.overrides[req.requestContext.data['base'] as string] ??
|
|
23
|
-
req.requestContext.data['base']
|
|
24
|
-
}
|
|
25
|
-
return
|
|
26
|
-
}
|
|
27
|
-
let requestedSymbols = req.requestContext.data['base']
|
|
28
|
-
for (let i = 0; i < requestedSymbols.length; i++) {
|
|
29
|
-
const symbol = requestedSymbols[i]
|
|
30
|
-
// Perform overrides specified in the request payload
|
|
31
|
-
let overriddenSymbol =
|
|
32
|
-
adapterOverrides[symbol.toUpperCase()] ?? adapterOverrides[symbol.toLowerCase()]
|
|
33
|
-
// Perform hardcoded overrides
|
|
34
|
-
if (adapter.overrides) {
|
|
35
|
-
overriddenSymbol =
|
|
36
|
-
adapter.overrides[symbol.toUpperCase()] ??
|
|
37
|
-
adapter.overrides[symbol.toLowerCase()] ??
|
|
38
|
-
overriddenSymbol
|
|
39
|
-
}
|
|
40
|
-
requestedSymbols[i] = overriddenSymbol ?? requestedSymbols[i]
|
|
41
|
-
}
|
|
42
|
-
requestedSymbols = requestedSymbols.length > 1 ? requestedSymbols : requestedSymbols[1]
|
|
43
|
-
req.requestContext.data['base'] = requestedSymbols
|
|
44
|
-
}
|
|
@@ -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
|
-
}
|