Skip to content

Commit ca40868

Browse files
authored
Make nav-fund-services generic (#4169)
* Make nav-fund-services generic * Comments
1 parent e25897e commit ca40868

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

.changeset/fuzzy-apes-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/nav-fund-services-adapter': patch
3+
---
4+
5+
Make EA generic

packages/sources/nav-fund-services/src/config/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ import { AdapterConfig } from '@chainlink/external-adapter-framework/config'
22

33
export const config = new AdapterConfig(
44
{
5-
API_KEY: {
6-
description: 'An API key for Data Provider',
7-
type: 'string',
8-
required: true,
9-
sensitive: true,
10-
},
11-
SECRET_KEY: {
12-
description: 'A key for Data Provider used in hashing the API key',
13-
type: 'string',
14-
required: true,
15-
sensitive: true,
16-
},
175
API_ENDPOINT: {
186
description: 'An API endpoint for Data Provider',
197
type: 'string',

packages/sources/nav-fund-services/src/endpoint/nav.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
22
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
3+
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
34
import { config } from '../config'
5+
import { getApiKeys } from '../transport/creds'
46
import { navTransport } from '../transport/nav'
57

68
export const inputParameters = new InputParameters(
79
{
810
globalFundID: {
911
required: true,
1012
type: 'number',
11-
description: 'The global fund ID',
13+
description: 'Used to match API_KEY_${fund} SECRET_KEY_${fund} env variables',
1214
},
1315
},
1416
[
@@ -36,4 +38,8 @@ export const endpoint = new AdapterEndpoint({
3638
name: 'nav',
3739
transport: navTransport,
3840
inputParameters,
41+
customInputValidation: (req): AdapterInputError | undefined => {
42+
getApiKeys(req.requestContext.data.globalFundID)
43+
return
44+
},
3945
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
2+
3+
export const getApiKeys = (globalFundID: number) => {
4+
const apiKeyName = `API_KEY_${globalFundID}`
5+
const secretKeyName = `SECRET_KEY_${globalFundID}`
6+
7+
const apiKey = process.env[apiKeyName]
8+
const secretKey = process.env[secretKeyName]
9+
10+
if (!apiKey || !secretKey) {
11+
throw new AdapterInputError({
12+
statusCode: 400,
13+
message: `Missing '${apiKeyName}' or '${secretKeyName}' environment variables.`,
14+
})
15+
}
16+
17+
return [apiKey, secretKey]
18+
}

packages/sources/nav-fund-services/src/transport/nav.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SubscriptionTransport } from '@chainlink/external-adapter-framework/tra
88
import { AdapterResponse, makeLogger, sleep } from '@chainlink/external-adapter-framework/util'
99
import { Requester } from '@chainlink/external-adapter-framework/util/requester'
1010
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
11+
import { getApiKeys } from './creds'
1112
import { clampStartByBusinessDays, parseDateString, toDateString } from './date-utils'
1213

1314
const logger = makeLogger('NavTransport')
@@ -66,11 +67,14 @@ export class NavTransport extends SubscriptionTransport<BaseEndpointTypes> {
6667
): Promise<AdapterResponse<BaseEndpointTypes['Response']>> {
6768
const providerDataRequestedUnixMs = Date.now()
6869
logger.debug(`Handling request for globalFundID: ${param.globalFundID}`)
70+
71+
const [apiKey, secret] = getApiKeys(param.globalFundID)
72+
6973
const { FromDate: earliestPossibleFromStr, ToDate: latestPossibleToStr } = await getFundDates({
7074
globalFundID: param.globalFundID,
7175
baseURL: this.config.API_ENDPOINT,
72-
apiKey: this.config.API_KEY,
73-
secret: this.config.SECRET_KEY,
76+
apiKey,
77+
secret,
7478
requester: this.requester,
7579
})
7680

@@ -92,8 +96,8 @@ export class NavTransport extends SubscriptionTransport<BaseEndpointTypes> {
9296
fromDate: toDateString(preferredFrom),
9397
toDate: toDateString(latestPossibleTo),
9498
baseURL: this.config.API_ENDPOINT,
95-
apiKey: this.config.API_KEY,
96-
secret: this.config.SECRET_KEY,
99+
apiKey,
100+
secret,
97101
requester: this.requester,
98102
})
99103

packages/sources/nav-fund-services/test/integration/adapter.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ describe('execute', () => {
1212

1313
beforeAll(async () => {
1414
oldEnv = JSON.parse(JSON.stringify(process.env))
15-
process.env.API_KEY = process.env.API_KEY ?? 'fake-api-key'
16-
process.env.SECRET_KEY = 'SOME_SECRET_KEY'
17-
process.env.BACKGROUND_EXECUTE_MS = process.env.BACKGROUND_EXECUTE_MS ?? '0'
15+
process.env.API_KEY_1234 = 'fake-api-key'
16+
process.env.SECRET_KEY_1234 = 'SOME_SECRET_KEY'
17+
process.env.BACKGROUND_EXECUTE_MS = '0'
1818
const mockDate = new Date('2001-01-01T00:00:00.000Z')
1919
spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime())
2020

packages/sources/nav-fund-services/test/unit/nav.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ let transport: NavTransport
1717
// adapter settings stub
1818
const adapterSettings = makeStub('adapterSettings', {
1919
API_ENDPOINT: 'https://api.navfund.com',
20-
API_KEY: 'apiKey',
21-
SECRET_KEY: 'secret',
2220
BACKGROUND_EXECUTE_MS: 0,
2321
WARMUP_SUBSCRIPTION_TTL: 10_000,
2422
} as unknown as BaseEndpointTypes['Settings'])
2523

24+
process.env.API_KEY_123 = 'apiKey'
25+
process.env.SECRET_KEY_123 = 'secret'
26+
2627
// requester stub that we'll control per‑test
2728
const requester = makeStub('requester', { request: jest.fn() })
2829
const responseCache = makeStub('responseCache', { write: jest.fn() })

0 commit comments

Comments
 (0)