@chainlink/external-adapter-framework 0.0.14 → 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.
- package/.c8rc.json +3 -0
- package/.eslintignore +10 -0
- package/.eslintrc.js +96 -0
- package/.github/README.MD +42 -0
- package/.github/actions/setup/action.yaml +13 -0
- package/.github/workflows/label.yaml +39 -0
- package/.github/workflows/main.yaml +39 -0
- package/.github/workflows/publish.yaml +17 -0
- package/.prettierignore +13 -0
- package/.yarnrc +0 -0
- package/README.md +103 -0
- package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +82953 -0
- package/dist/examples/coingecko/test/integration/adapter.test.ts.js +91672 -0
- package/dist/main.js +72703 -0
- package/docker-compose.yaml +35 -0
- package/env.sh +54 -0
- package/env2.sh +55 -0
- package/jest.config.js +5 -0
- package/package.json +14 -3
- package/publish.sh +0 -0
- package/src/adapter.ts +263 -0
- package/src/background-executor.ts +52 -0
- package/src/cache/factory.ts +26 -0
- package/src/cache/index.ts +258 -0
- package/src/cache/local.ts +73 -0
- package/src/cache/metrics.ts +112 -0
- package/src/cache/redis.ts +93 -0
- package/src/config/index.ts +517 -0
- package/src/config/provider-limits.ts +127 -0
- package/src/examples/bank-frick/README.MD +10 -0
- package/src/examples/bank-frick/accounts.ts +246 -0
- package/src/examples/bank-frick/config/index.ts +53 -0
- package/src/examples/bank-frick/index.ts +13 -0
- package/src/examples/bank-frick/types.d.ts +38 -0
- package/src/examples/bank-frick/util.ts +55 -0
- package/src/examples/coingecko/src/config/index.ts +12 -0
- package/src/examples/coingecko/src/config/overrides.json +10826 -0
- package/src/examples/coingecko/src/cryptoUtils.ts +88 -0
- package/src/examples/coingecko/src/endpoint/coins.ts +54 -0
- package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +66 -0
- package/src/examples/coingecko/src/endpoint/crypto-volume.ts +66 -0
- package/src/examples/coingecko/src/endpoint/crypto.ts +63 -0
- package/src/examples/coingecko/src/endpoint/dominance.ts +40 -0
- package/src/examples/coingecko/src/endpoint/global-marketcap.ts +40 -0
- package/src/examples/coingecko/src/endpoint/index.ts +6 -0
- package/src/examples/coingecko/src/globalUtils.ts +78 -0
- package/src/examples/coingecko/src/index.ts +17 -0
- package/src/examples/coingecko/test/e2e/adapter.test.ts +278 -0
- package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +15 -0
- package/src/examples/coingecko/test/integration/adapter.test.ts +281 -0
- package/src/examples/coingecko/test/integration/capturedRequests.json +1 -0
- package/src/examples/coingecko/test/integration/fixtures.ts +42 -0
- package/src/examples/coingecko-old/batch-warming.ts +79 -0
- package/src/examples/coingecko-old/index.ts +9 -0
- package/src/examples/coingecko-old/rest.ts +77 -0
- package/src/examples/ncfx/config/index.ts +12 -0
- package/src/examples/ncfx/index.ts +9 -0
- package/src/examples/ncfx/websocket.ts +99 -0
- package/src/index.ts +149 -0
- package/src/metrics/constants.ts +23 -0
- package/src/metrics/index.ts +115 -0
- package/src/metrics/util.ts +18 -0
- package/src/rate-limiting/background/fixed-frequency.ts +45 -0
- package/src/rate-limiting/index.ts +100 -0
- package/src/rate-limiting/metrics.ts +18 -0
- package/src/rate-limiting/request/simple-counting.ts +76 -0
- package/src/transports/batch-warming.ts +127 -0
- package/src/transports/index.ts +152 -0
- package/src/transports/metrics.ts +95 -0
- package/src/transports/rest.ts +168 -0
- package/src/transports/util.ts +63 -0
- package/src/transports/websocket.ts +245 -0
- package/src/util/index.ts +23 -0
- package/src/util/logger.ts +69 -0
- package/src/util/recordRequests.ts +47 -0
- package/src/util/request.ts +117 -0
- package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
- package/src/util/subscription-set/subscription-set.ts +35 -0
- package/src/util/test-payload-loader.ts +87 -0
- package/src/validation/error.ts +116 -0
- package/src/validation/index.ts +110 -0
- package/src/validation/input-params.ts +45 -0
- package/src/validation/override-functions.ts +44 -0
- package/src/validation/overrideFunctions.ts +44 -0
- package/src/validation/preset-tokens.json +23 -0
- package/src/validation/validator.ts +384 -0
- package/test/adapter.test.ts +27 -0
- package/test/background-executor.test.ts +108 -0
- package/test/cache/cache-key.test.ts +114 -0
- package/test/cache/helper.ts +100 -0
- package/test/cache/local.test.ts +54 -0
- package/test/cache/redis.test.ts +89 -0
- package/test/correlation.test.ts +114 -0
- package/test/index.test.ts +37 -0
- package/test/metrics/feed-id.test.ts +38 -0
- package/test/metrics/helper.ts +14 -0
- package/test/metrics/labels.test.ts +36 -0
- package/test/metrics/metrics.test.ts +267 -0
- package/test/metrics/redis-metrics.test.ts +113 -0
- package/test/metrics/warmer-metrics.test.ts +193 -0
- package/test/metrics/ws-metrics.test.ts +225 -0
- package/test/rate-limit-config.test.ts +242 -0
- package/test/smoke/smoke.test.ts +166 -0
- package/test/smoke/test-payload-fail.json +3 -0
- package/test/smoke/test-payload.js +22 -0
- package/test/smoke/test-payload.json +7 -0
- package/test/transports/batch.test.ts +466 -0
- package/test/transports/rest.test.ts +242 -0
- package/test/transports/websocket.test.ts +183 -0
- package/test/tsconfig.json +5 -0
- package/test/util.ts +77 -0
- package/test/validation.test.ts +178 -0
- package/test.sh +20 -0
- package/test2.sh +2 -0
- package/tsconfig.json +28 -0
- package/typedoc.json +6 -0
- package/webpack.config.js +57 -0
- package/yarn-error.log +3778 -0
- package/adapter.d.ts +0 -107
- package/adapter.js +0 -115
- package/background-executor.d.ts +0 -11
- package/background-executor.js +0 -45
- package/cache/factory.d.ts +0 -6
- package/cache/factory.js +0 -55
- package/cache/index.d.ts +0 -94
- package/cache/index.js +0 -173
- package/cache/local.d.ts +0 -23
- package/cache/local.js +0 -83
- package/cache/metrics.d.ts +0 -27
- package/cache/metrics.js +0 -120
- package/cache/redis.d.ts +0 -16
- package/cache/redis.js +0 -100
- package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/config/index.d.ts +0 -209
- package/config/index.js +0 -380
- package/config/provider-limits.d.ts +0 -31
- package/config/provider-limits.js +0 -79
- package/examples/bank-frick/accounts.d.ts +0 -39
- package/examples/bank-frick/accounts.js +0 -191
- package/examples/bank-frick/config/index.d.ts +0 -4
- package/examples/bank-frick/config/index.js +0 -54
- package/examples/bank-frick/index.d.ts +0 -2
- package/examples/bank-frick/index.js +0 -14
- package/examples/bank-frick/util.d.ts +0 -4
- package/examples/bank-frick/util.js +0 -39
- package/examples/coingecko/batch-warming.d.ts +0 -2
- package/examples/coingecko/batch-warming.js +0 -52
- package/examples/coingecko/index.d.ts +0 -2
- package/examples/coingecko/index.js +0 -10
- package/examples/coingecko/rest.d.ts +0 -2
- package/examples/coingecko/rest.js +0 -50
- package/examples/ncfx/config/index.d.ts +0 -12
- package/examples/ncfx/config/index.js +0 -15
- package/examples/ncfx/index.d.ts +0 -2
- package/examples/ncfx/index.js +0 -10
- package/examples/ncfx/websocket.d.ts +0 -36
- package/examples/ncfx/websocket.js +0 -72
- package/index.d.ts +0 -11
- package/index.js +0 -133
- package/metrics/constants.d.ts +0 -16
- package/metrics/constants.js +0 -25
- package/metrics/index.d.ts +0 -15
- package/metrics/index.js +0 -122
- package/metrics/util.d.ts +0 -7
- package/metrics/util.js +0 -9
- package/package/adapter.d.ts +0 -88
- package/package/adapter.js +0 -112
- package/package/background-executor.d.ts +0 -11
- package/package/background-executor.js +0 -45
- package/package/cache/factory.d.ts +0 -6
- package/package/cache/factory.js +0 -57
- package/package/cache/index.d.ts +0 -90
- package/package/cache/index.js +0 -169
- package/package/cache/local.d.ts +0 -23
- package/package/cache/local.js +0 -83
- package/package/cache/metrics.d.ts +0 -27
- package/package/cache/metrics.js +0 -120
- package/package/cache/redis.d.ts +0 -16
- package/package/cache/redis.js +0 -100
- package/package/config/index.d.ts +0 -195
- package/package/config/index.js +0 -365
- package/package/config/provider-limits.d.ts +0 -31
- package/package/config/provider-limits.js +0 -76
- package/package/examples/coingecko/batch-warming.d.ts +0 -2
- package/package/examples/coingecko/batch-warming.js +0 -52
- package/package/examples/coingecko/index.d.ts +0 -2
- package/package/examples/coingecko/index.js +0 -10
- package/package/examples/coingecko/rest.d.ts +0 -2
- package/package/examples/coingecko/rest.js +0 -50
- package/package/examples/ncfx/config/index.d.ts +0 -12
- package/package/examples/ncfx/config/index.js +0 -15
- package/package/examples/ncfx/index.d.ts +0 -2
- package/package/examples/ncfx/index.js +0 -10
- package/package/examples/ncfx/websocket.d.ts +0 -36
- package/package/examples/ncfx/websocket.js +0 -72
- package/package/index.d.ts +0 -12
- package/package/index.js +0 -92
- package/package/metrics/constants.d.ts +0 -16
- package/package/metrics/constants.js +0 -25
- package/package/metrics/index.d.ts +0 -15
- package/package/metrics/index.js +0 -123
- package/package/metrics/util.d.ts +0 -3
- package/package/metrics/util.js +0 -9
- package/package/package.json +0 -72
- package/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
- package/package/rate-limiting/background/fixed-frequency.js +0 -37
- package/package/rate-limiting/index.d.ts +0 -54
- package/package/rate-limiting/index.js +0 -63
- package/package/rate-limiting/metrics.d.ts +0 -3
- package/package/rate-limiting/metrics.js +0 -44
- package/package/rate-limiting/request/simple-counting.d.ts +0 -20
- package/package/rate-limiting/request/simple-counting.js +0 -62
- package/package/test.d.ts +0 -1
- package/package/test.js +0 -6
- package/package/transports/batch-warming.d.ts +0 -34
- package/package/transports/batch-warming.js +0 -101
- package/package/transports/index.d.ts +0 -87
- package/package/transports/index.js +0 -87
- package/package/transports/metrics.d.ts +0 -21
- package/package/transports/metrics.js +0 -105
- package/package/transports/rest.d.ts +0 -43
- package/package/transports/rest.js +0 -129
- package/package/transports/util.d.ts +0 -8
- package/package/transports/util.js +0 -85
- package/package/transports/websocket.d.ts +0 -80
- package/package/transports/websocket.js +0 -169
- package/package/util/expiring-sorted-set.d.ts +0 -21
- package/package/util/expiring-sorted-set.js +0 -47
- package/package/util/index.d.ts +0 -11
- package/package/util/index.js +0 -35
- package/package/util/logger.d.ts +0 -42
- package/package/util/logger.js +0 -62
- package/package/util/request.d.ts +0 -55
- package/package/util/request.js +0 -2
- package/package/validation/error.d.ts +0 -50
- package/package/validation/error.js +0 -79
- package/package/validation/index.d.ts +0 -5
- package/package/validation/index.js +0 -86
- package/package/validation/input-params.d.ts +0 -15
- package/package/validation/input-params.js +0 -30
- package/package/validation/override-functions.d.ts +0 -3
- package/package/validation/override-functions.js +0 -40
- package/package/validation/preset-tokens.json +0 -23
- package/package/validation/validator.d.ts +0 -47
- package/package/validation/validator.js +0 -303
- package/rate-limiting/background/fixed-frequency.d.ts +0 -10
- package/rate-limiting/background/fixed-frequency.js +0 -35
- package/rate-limiting/index.d.ts +0 -54
- package/rate-limiting/index.js +0 -63
- package/rate-limiting/metrics.d.ts +0 -3
- package/rate-limiting/metrics.js +0 -44
- package/rate-limiting/request/simple-counting.d.ts +0 -20
- package/rate-limiting/request/simple-counting.js +0 -62
- package/test.d.ts +0 -1
- package/test.js +0 -6
- package/transports/batch-warming.d.ts +0 -35
- package/transports/batch-warming.js +0 -101
- package/transports/index.d.ts +0 -70
- package/transports/index.js +0 -87
- package/transports/metrics.d.ts +0 -21
- package/transports/metrics.js +0 -105
- package/transports/rest.d.ts +0 -44
- package/transports/rest.js +0 -131
- package/transports/util.d.ts +0 -8
- package/transports/util.js +0 -85
- package/transports/websocket.d.ts +0 -81
- package/transports/websocket.js +0 -168
- package/util/expiring-sorted-set.d.ts +0 -21
- package/util/expiring-sorted-set.js +0 -47
- package/util/index.d.ts +0 -12
- package/util/index.js +0 -35
- package/util/logger.d.ts +0 -42
- package/util/logger.js +0 -62
- package/util/request.d.ts +0 -57
- package/util/request.js +0 -2
- package/util/subscription-set/expiring-sorted-set.d.ts +0 -22
- package/util/subscription-set/expiring-sorted-set.js +0 -47
- package/util/subscription-set/subscription-set.d.ts +0 -18
- package/util/subscription-set/subscription-set.js +0 -19
- package/util/test-payload-loader.d.ts +0 -25
- package/util/test-payload-loader.js +0 -83
- package/validation/error.d.ts +0 -50
- package/validation/error.js +0 -79
- package/validation/index.d.ts +0 -5
- package/validation/index.js +0 -91
- package/validation/input-params.d.ts +0 -15
- package/validation/input-params.js +0 -30
- package/validation/override-functions.d.ts +0 -3
- package/validation/override-functions.js +0 -40
- package/validation/preset-tokens.json +0 -23
- package/validation/validator.d.ts +0 -47
- package/validation/validator.js +0 -303
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
2
|
+
import {
|
|
3
|
+
Account,
|
|
4
|
+
AdapterInputParameters,
|
|
5
|
+
BankFrickAccountsRequestSchema,
|
|
6
|
+
BankFrickAccountsResponseSchema,
|
|
7
|
+
SigningAlgorithm,
|
|
8
|
+
} from './types'
|
|
9
|
+
import { customSettings } from './config'
|
|
10
|
+
import { Transport } from '../../transports'
|
|
11
|
+
import { generateJWT } from './util'
|
|
12
|
+
import { AdapterRequest, AdapterResponse, makeLogger, sleep } from '../../util'
|
|
13
|
+
import { AdapterConfig } from '../../config'
|
|
14
|
+
import { AdapterError } from '../../validation/error'
|
|
15
|
+
import { Cache } from '../../cache'
|
|
16
|
+
import { InputParameters } from '../../validation'
|
|
17
|
+
import { AdapterDependencies, AdapterEndpoint } from '../../adapter'
|
|
18
|
+
|
|
19
|
+
const logger = makeLogger('BankFrickTransport')
|
|
20
|
+
|
|
21
|
+
// Note: this is a shallow pattern that only checks for a country code since IBANs in the sandbox are invalid
|
|
22
|
+
const ibanPattern = /^[A-Z]{2}[A-Z\d]{14,30}$/
|
|
23
|
+
const inputParameters: InputParameters = {
|
|
24
|
+
ibanIDs: {
|
|
25
|
+
description: 'The list of account ids included in the sum of balances',
|
|
26
|
+
required: true,
|
|
27
|
+
type: 'array',
|
|
28
|
+
},
|
|
29
|
+
signingAlgorithm: {
|
|
30
|
+
description:
|
|
31
|
+
'What signing algorithm is used to sign and verify authorization data, one of rsa-sha256, rsa-sha384, or rsa-sha512',
|
|
32
|
+
required: false,
|
|
33
|
+
type: 'string',
|
|
34
|
+
default: 'rsa-sha512',
|
|
35
|
+
options: ['rsa-sha256', 'rsa-sha384', 'rsa-sha512'],
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// See here for all expected error returned by the API: https://developers.bankfrick.li/docs#errors
|
|
40
|
+
const AuthErrors: { [key: number]: string } = {
|
|
41
|
+
401: 'No JWT token provided or token is invalid',
|
|
42
|
+
403: 'API key is invalid or any other condition is hindering the login', // Unclear if this is fatal or not
|
|
43
|
+
}
|
|
44
|
+
const FatalErrors: { [key: number]: string } = {
|
|
45
|
+
400: "Invalid parameters passed to Bank Frick's API",
|
|
46
|
+
423: "Authorization is valid, but the user's account is locked",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* RestTransport implementation for Bank Frick, which has unusually complex requirements for an EA
|
|
51
|
+
* The RestTransport is generally built to make a single request and return a single response.
|
|
52
|
+
* This transport instead is used to fetch and process pages of data, and also requires a JWT to run
|
|
53
|
+
*
|
|
54
|
+
* This transport does all the heavy lifting in setup(), which is where the paging happens, and it
|
|
55
|
+
* also has complex retry logic that will attempt to refresh the JWT when certain HTTP errors occur
|
|
56
|
+
*/
|
|
57
|
+
export class BankFrickAccountsTransport
|
|
58
|
+
implements Transport<AdapterInputParameters, number, typeof customSettings>
|
|
59
|
+
{
|
|
60
|
+
// Global variable to keep the token. Token is provisioned when the accounts endpoint is hit.
|
|
61
|
+
// Each instance of the EA will have their own token by design
|
|
62
|
+
token!: string
|
|
63
|
+
cache!: Cache
|
|
64
|
+
|
|
65
|
+
async initialize(dependencies: AdapterDependencies): Promise<void> {
|
|
66
|
+
this.cache = dependencies.cache
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async hasBeenSetUp() {
|
|
70
|
+
return false // Return false since we aren't coalescing requests
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates an AxiosRequestConfig object for fetching a page of accounts from the Bank Frick API
|
|
75
|
+
*/
|
|
76
|
+
prepareRequest(
|
|
77
|
+
firstPosition: number,
|
|
78
|
+
inputParams: AdapterInputParameters,
|
|
79
|
+
config: AdapterConfig<typeof customSettings>,
|
|
80
|
+
): AxiosRequestConfig<BankFrickAccountsRequestSchema> {
|
|
81
|
+
const { API_ENDPOINT, BASE_URL, PAGE_SIZE } = config
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
baseURL: API_ENDPOINT + BASE_URL,
|
|
85
|
+
url: 'accounts',
|
|
86
|
+
method: 'GET',
|
|
87
|
+
params: {
|
|
88
|
+
firstPosition,
|
|
89
|
+
maxResults: PAGE_SIZE,
|
|
90
|
+
},
|
|
91
|
+
headers: {
|
|
92
|
+
Authorization: `Bearer ${this.token}`,
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Request with retry logic for Bank Frick's API. In addition to standard retry logic, this function
|
|
99
|
+
* compares errors against expected errors from the Bank Frick API, and will throw without retries
|
|
100
|
+
* on a known fatal error, or try to refresh the JWT on a known auth error
|
|
101
|
+
**/
|
|
102
|
+
async makeRequest(
|
|
103
|
+
axiosRequest: AxiosRequestConfig<BankFrickAccountsRequestSchema>,
|
|
104
|
+
config: AdapterConfig<typeof customSettings>,
|
|
105
|
+
signingAlgorithm?: SigningAlgorithm,
|
|
106
|
+
): Promise<AxiosResponse<BankFrickAccountsResponseSchema>> {
|
|
107
|
+
let retryNumber = 0
|
|
108
|
+
let response = await axios.request(axiosRequest)
|
|
109
|
+
while (response.status !== 200) {
|
|
110
|
+
retryNumber++
|
|
111
|
+
logger.warn(
|
|
112
|
+
'Encountered error when fetching accounts from Bank Frick:',
|
|
113
|
+
response.status,
|
|
114
|
+
response.statusText,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
// Evaluate whether the error was fatal, auth, or transient and whether we've exceeded the max number of retries.
|
|
118
|
+
// Throw on fatal, refresh token on auth error, pass on transient until we've exhausted our retries
|
|
119
|
+
if (FatalErrors[response.status]) {
|
|
120
|
+
throw new AdapterError({
|
|
121
|
+
statusCode: response.status,
|
|
122
|
+
message: response.statusText,
|
|
123
|
+
})
|
|
124
|
+
} else if (AuthErrors[response.status]) {
|
|
125
|
+
// We've encountered a known auth error, so try to refresh the token before making another request
|
|
126
|
+
logger.info('Auth error received from the Bank Frick API, attempting to refresh the token')
|
|
127
|
+
this.token = await generateJWT(config, signingAlgorithm)
|
|
128
|
+
} else if (retryNumber === config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES) {
|
|
129
|
+
throw new AdapterError({
|
|
130
|
+
statusCode: 504,
|
|
131
|
+
message: `Bank Frick transport hit the max number of retries (${config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES} retries) and aborted`,
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
logger.debug(
|
|
136
|
+
`Sleeping for ${config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES}ms before retrying`,
|
|
137
|
+
)
|
|
138
|
+
await sleep(config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES)
|
|
139
|
+
response = await axios.request(axiosRequest)
|
|
140
|
+
}
|
|
141
|
+
return response
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
validateInputParams(params: AdapterInputParameters): string[] {
|
|
145
|
+
const encounteredIds: { [key: string]: number } = {}
|
|
146
|
+
const errors: string[] = []
|
|
147
|
+
const { ibanIDs } = params
|
|
148
|
+
|
|
149
|
+
ibanIDs.forEach((v) => {
|
|
150
|
+
if (!v.match(ibanPattern)) {
|
|
151
|
+
errors.push(`Invalid IBAN: ${v}`)
|
|
152
|
+
}
|
|
153
|
+
encounteredIds[v] += 1
|
|
154
|
+
if (encounteredIds[v] > 1) {
|
|
155
|
+
errors.push(`The following IBAN appears more than once in the input parameters: ${v}`)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
return errors
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Fetches pages of data from the Bank Frick API, scans for accounts by IBAN, and returns the balance
|
|
163
|
+
* of all found accounts. Returns a 404 if any IBAN isn't found.
|
|
164
|
+
*/
|
|
165
|
+
async setup(
|
|
166
|
+
req: AdapterRequest<AdapterInputParameters>,
|
|
167
|
+
config: AdapterConfig<typeof customSettings>,
|
|
168
|
+
): Promise<AdapterResponse<number>> {
|
|
169
|
+
const { ibanIDs, signingAlgorithm } = req.requestContext.data
|
|
170
|
+
const { PAGE_SIZE = 500 } = config
|
|
171
|
+
|
|
172
|
+
logger.debug(`Validating input: ${JSON.stringify(req.requestContext.data)}`)
|
|
173
|
+
|
|
174
|
+
// Scan ibanIDs for duplicates and invalid IBANs
|
|
175
|
+
const validationErrors = this.validateInputParams(req.requestContext.data)
|
|
176
|
+
if (validationErrors.length > 0) {
|
|
177
|
+
throw new AdapterError({
|
|
178
|
+
statusCode: 420,
|
|
179
|
+
message: `Received the following errors when validating inputParameters:\n ${validationErrors.join(
|
|
180
|
+
'\n',
|
|
181
|
+
)}`,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Refresh the token if it isn't set
|
|
186
|
+
if (!this.token) {
|
|
187
|
+
this.token = await generateJWT(config, signingAlgorithm)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let sum = 0
|
|
191
|
+
let position = 0
|
|
192
|
+
const keys = ibanIDs
|
|
193
|
+
logger.info("Fetching accounts from Bank Frick's API...")
|
|
194
|
+
while (keys.length > 0) {
|
|
195
|
+
// TODO Fetching and processing pages can be run concurrently
|
|
196
|
+
const axiosRequest = this.prepareRequest(position, req.requestContext.data, config)
|
|
197
|
+
const response = await this.makeRequest(axiosRequest, config, signingAlgorithm)
|
|
198
|
+
|
|
199
|
+
logger.debug(`Evaluating accounts from page ${position / PAGE_SIZE}`)
|
|
200
|
+
response.data.accounts.forEach((v: Account) => {
|
|
201
|
+
logger.trace(`Evaluating ${v.account} (iban: ${v.iban}, type: ${v.type})`)
|
|
202
|
+
const index = ibanIDs.indexOf(v.iban)
|
|
203
|
+
if (index > -1) {
|
|
204
|
+
keys.splice(index, 1)
|
|
205
|
+
sum += v.balance
|
|
206
|
+
logger.trace(`Found account: ${v.account} (iban: ${v.iban}) with balance of ${v.balance}`)
|
|
207
|
+
logger.trace(
|
|
208
|
+
`Running sum: ${sum}, number of ibans left to find: ${keys.length}/${ibanIDs.length}`,
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
if (!response.data.moreResults) {
|
|
214
|
+
logger.debug('No more results, breaking out of account query loop')
|
|
215
|
+
break
|
|
216
|
+
}
|
|
217
|
+
position += PAGE_SIZE || 0
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 404 if one or more accounts were not found
|
|
221
|
+
if (keys.length > 0) {
|
|
222
|
+
logger.error(`Could not find all accounts, returning 404: ${keys.join(', ')}`)
|
|
223
|
+
const res = {
|
|
224
|
+
data: 0,
|
|
225
|
+
statusCode: 404,
|
|
226
|
+
result: 0,
|
|
227
|
+
}
|
|
228
|
+
await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
logger.debug('Was able to find all accounts, returning balance across all accounts: ', sum)
|
|
232
|
+
const res = {
|
|
233
|
+
data: sum,
|
|
234
|
+
statusCode: 200,
|
|
235
|
+
result: sum,
|
|
236
|
+
}
|
|
237
|
+
await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE)
|
|
238
|
+
return res
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export const accountsRestEndpoint: AdapterEndpoint = {
|
|
243
|
+
name: 'accounts',
|
|
244
|
+
transport: new BankFrickAccountsTransport(),
|
|
245
|
+
inputParameters,
|
|
246
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SettingsMap } from '../../../config'
|
|
2
|
+
import crypto from 'crypto'
|
|
3
|
+
import { SigningAlgorithm } from '../types'
|
|
4
|
+
import { makeLogger } from '../../../util'
|
|
5
|
+
|
|
6
|
+
const logger = makeLogger('BankFrickConfig')
|
|
7
|
+
|
|
8
|
+
// Used for enum options and validation
|
|
9
|
+
export const signingAlgorithms: SigningAlgorithm[] = ['rsa-sha256', 'rsa-sha384', 'rsa-sha512']
|
|
10
|
+
const MAX_PAGE_SIZE = 500
|
|
11
|
+
|
|
12
|
+
export const customSettings: SettingsMap = {
|
|
13
|
+
PAGE_SIZE: {
|
|
14
|
+
description: 'The number of accounts to fetch per call to /accounts. Must be >= 1 and <= 500.',
|
|
15
|
+
type: 'number',
|
|
16
|
+
required: false,
|
|
17
|
+
default: MAX_PAGE_SIZE,
|
|
18
|
+
validate: (value) => {
|
|
19
|
+
if (!value) {
|
|
20
|
+
return ''
|
|
21
|
+
} else if (value < 1) {
|
|
22
|
+
return `PAGE_SIZE must be at least >= 1, was ${value}`
|
|
23
|
+
} else if (value > MAX_PAGE_SIZE) {
|
|
24
|
+
return `PAGE_SIZE must be <= 500, was ${value}`
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
PRIVATE_KEY: {
|
|
29
|
+
description: '',
|
|
30
|
+
type: 'string',
|
|
31
|
+
required: true,
|
|
32
|
+
validate: (value) => {
|
|
33
|
+
const failedAlgos: SigningAlgorithm[] = []
|
|
34
|
+
for (const algorithm of signingAlgorithms) {
|
|
35
|
+
const body = { example: 123 }
|
|
36
|
+
try {
|
|
37
|
+
crypto.sign(algorithm, Buffer.from(JSON.stringify(body)), value)
|
|
38
|
+
logger.trace("Successfully tested PRIVATE_KEY by signing with algorithm '%s'", algorithm)
|
|
39
|
+
} catch {
|
|
40
|
+
logger.trace("PRIVATE_KEY failed to sign message with algorithm '%s'", algorithm)
|
|
41
|
+
failedAlgos.push(algorithm)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (failedAlgos.length > 0) {
|
|
46
|
+
return `Failed to sign a dummy body using $PRIVATE_KEY with the following algorithms ${failedAlgos.join(
|
|
47
|
+
',',
|
|
48
|
+
)}`
|
|
49
|
+
}
|
|
50
|
+
logger.debug('$PRIVATE_KEY successfully signed a dummy body with all supported algorithms')
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
} as const
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Adapter } from '../../adapter'
|
|
2
|
+
import { accountsRestEndpoint } from './accounts'
|
|
3
|
+
import { customSettings } from './config'
|
|
4
|
+
|
|
5
|
+
export const adapter: Adapter = {
|
|
6
|
+
name: 'bank-frick',
|
|
7
|
+
defaultEndpoint: 'accounts',
|
|
8
|
+
endpoints: [accountsRestEndpoint],
|
|
9
|
+
customSettings,
|
|
10
|
+
envDefaultOverrides: {
|
|
11
|
+
API_ENDPOINT: 'https://olbsandbox.bankfrick.li/webapi/v2',
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface Account {
|
|
2
|
+
account: string // The account number of the account
|
|
3
|
+
type: string // The type of the account
|
|
4
|
+
iban: string // The iban of the account if exists
|
|
5
|
+
customer: string // The customer data of the account which consists of the customer number and name
|
|
6
|
+
currency: string // The account currency
|
|
7
|
+
balance: number // The current account balance
|
|
8
|
+
available: number // The available amount of the account as defined in the online banking
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Types representing what we send/receive from the bank frick api
|
|
12
|
+
* See https://developers.bankfrick.li/docs#data-types for details
|
|
13
|
+
*/
|
|
14
|
+
export interface BankFrickAccountsResponseSchema {
|
|
15
|
+
errors?: string[]
|
|
16
|
+
date: Date
|
|
17
|
+
moreResults: boolean
|
|
18
|
+
resultSetSize: number
|
|
19
|
+
accounts: Account[]
|
|
20
|
+
}
|
|
21
|
+
export interface BankFrickAccountsRequestSchema {
|
|
22
|
+
firstPosition: number
|
|
23
|
+
maxResults: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Types representing the input parameters and response of the adapter
|
|
28
|
+
*/
|
|
29
|
+
export interface AdapterInputParameters {
|
|
30
|
+
ibanIDs: string[]
|
|
31
|
+
signingAlgorithm?: SigningAlgorithm
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* These are the options from the docs, but it's hard to see. See the first paragraph from this
|
|
36
|
+
* page for details: https://developers.bankfrick.li/docs#getting-started-signatures
|
|
37
|
+
*/
|
|
38
|
+
export type SigningAlgorithm = 'dsa-sha512' | 'rsa-sha256' | 'rsa-sha384' | 'rsa-sha512'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
2
|
+
import { SigningAlgorithm } from './types'
|
|
3
|
+
import { customSettings } from './config'
|
|
4
|
+
import axios, { AxiosRequestConfig } from 'axios'
|
|
5
|
+
import { makeLogger } from '../../util'
|
|
6
|
+
import { AdapterConfig } from '../../config'
|
|
7
|
+
|
|
8
|
+
interface TokenResponseBody {
|
|
9
|
+
errors?: string[]
|
|
10
|
+
token: string
|
|
11
|
+
}
|
|
12
|
+
interface TokenRequestBody {
|
|
13
|
+
key: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const logger = makeLogger('BankFrickUtil')
|
|
17
|
+
|
|
18
|
+
// Used by all endpoints requiring authentication
|
|
19
|
+
export const generateJWT = async (
|
|
20
|
+
config: AdapterConfig<typeof customSettings>,
|
|
21
|
+
signingAlgorithm: SigningAlgorithm = 'rsa-sha512',
|
|
22
|
+
): Promise<string> => {
|
|
23
|
+
logger.info("Generating a new JWT because we don't have one in config.token")
|
|
24
|
+
const { API_KEY, PRIVATE_KEY, API_ENDPOINT } = config
|
|
25
|
+
|
|
26
|
+
// All of these are required, so validation should have failed prior to this line
|
|
27
|
+
if (!API_KEY || !PRIVATE_KEY) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'API_KEY, PRIVATE_KEY, and PASSWORD all must be defined to get a new token\n' +
|
|
30
|
+
'Received: \n' +
|
|
31
|
+
`API_KEY: ${API_KEY}\n` +
|
|
32
|
+
`PRIVATE_KEY: ${PRIVATE_KEY}`,
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const data: TokenRequestBody = {
|
|
37
|
+
key: API_KEY,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const signature = crypto.sign(signingAlgorithm, Buffer.from(JSON.stringify(data)), PRIVATE_KEY)
|
|
41
|
+
const options: AxiosRequestConfig<TokenRequestBody> = {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
baseURL: API_ENDPOINT,
|
|
44
|
+
url: `authorize`,
|
|
45
|
+
headers: {
|
|
46
|
+
Signature: signature.toString('base64'),
|
|
47
|
+
algorithm: signingAlgorithm,
|
|
48
|
+
},
|
|
49
|
+
data,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const response = await axios.request<TokenResponseBody>(options)
|
|
53
|
+
|
|
54
|
+
return response.data.token
|
|
55
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const NAME = 'COINGECKO'
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_ENDPOINT = 'crypto'
|
|
4
|
+
export const DEFAULT_API_ENDPOINT = 'https://api.coingecko.com/api/v3'
|
|
5
|
+
export const PRO_API_ENDPOINT = 'https://pro-api.coingecko.com/api/v3'
|
|
6
|
+
|
|
7
|
+
export const customSettings = {
|
|
8
|
+
API_KEY: {
|
|
9
|
+
description: 'API key for the CoinGecko API',
|
|
10
|
+
type: 'string',
|
|
11
|
+
},
|
|
12
|
+
} as const
|