Skip to content

Commit

Permalink
add WS_ENABLED env var support (#3163)
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-stepanyan committed Jan 24, 2024
1 parent fdb266e commit 8f266fa
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-hairs-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/coinpaprika-adapter': patch
---

Added support for WS_ENABLED env var for websocket endpoints
5 changes: 5 additions & 0 deletions packages/sources/coinpaprika/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const config = new AdapterConfig({
default: 'wss://streaming.coinpaprika.com/ticks',
type: 'string',
},
WS_ENABLED: {
description: 'Whether data should be returned from websocket or not',
type: 'boolean',
default: false,
},
})

export const getApiEndpoint = (settings: typeof config.settings): string =>
Expand Down
9 changes: 7 additions & 2 deletions packages/sources/coinpaprika/src/endpoint/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { CryptoPriceEndpoint } from '@chainlink/external-adapter-framework/adapter'
import { TransportRoutes } from '@chainlink/external-adapter-framework/transports'
import { buildCryptoHttpTransport, buildWebsocketTransport } from '../transport/utils'
import { BaseEndpointTypes, cryptoInputParameters, customInputValidation } from './utils'
import {
BaseEndpointTypes,
cryptoInputParameters,
customInputValidation,
customRouter,
} from './utils'
import overrides from '../config/overrides.json'

export const endpoint = new CryptoPriceEndpoint({
Expand All @@ -10,7 +15,7 @@ export const endpoint = new CryptoPriceEndpoint({
transportRoutes: new TransportRoutes<BaseEndpointTypes>()
.register('ws', buildWebsocketTransport('p'))
.register('rest', buildCryptoHttpTransport('price')),
defaultTransport: 'rest',
customRouter,
inputParameters: cryptoInputParameters,
overrides: overrides.coinpaprika,
customInputValidation,
Expand Down
9 changes: 7 additions & 2 deletions packages/sources/coinpaprika/src/endpoint/marketcap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { PriceEndpoint } from '@chainlink/external-adapter-framework/adapter'
import overrides from '../config/overrides.json'
import { TransportRoutes } from '@chainlink/external-adapter-framework/transports'
import { buildCryptoHttpTransport, buildWebsocketTransport } from '../transport/utils'
import { BaseEndpointTypes, cryptoInputParameters, customInputValidation } from './utils'
import {
BaseEndpointTypes,
cryptoInputParameters,
customInputValidation,
customRouter,
} from './utils'

export const endpoint = new PriceEndpoint({
name: 'marketcap',
transportRoutes: new TransportRoutes<BaseEndpointTypes>()
.register('ws', buildWebsocketTransport('m'))
.register('rest', buildCryptoHttpTransport('market_cap')),
defaultTransport: 'rest',
customRouter,
inputParameters: cryptoInputParameters,
overrides: overrides.coinpaprika,
customInputValidation,
Expand Down
12 changes: 12 additions & 0 deletions packages/sources/coinpaprika/src/endpoint/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
import {
AdapterRequest,
AdapterRequestData,
SingleNumberResultResponse,
} from '@chainlink/external-adapter-framework/util'

Expand Down Expand Up @@ -81,3 +82,14 @@ export function customInputValidation(
}
return
}

export const customRouter = (
req: AdapterRequest<typeof cryptoInputParameters.validated>,
adapterConfig: typeof config.settings,
) => {
const rawRequestBody = req.body as unknown as { data: AdapterRequestData }
if (rawRequestBody.data?.transport) {
return rawRequestBody.data?.transport
}
return adapterConfig.WS_ENABLED ? 'ws' : 'rest'
}
9 changes: 7 additions & 2 deletions packages/sources/coinpaprika/src/endpoint/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { PriceEndpoint } from '@chainlink/external-adapter-framework/adapter'
import overrides from '../config/overrides.json'
import { TransportRoutes } from '@chainlink/external-adapter-framework/transports'
import { buildCryptoHttpTransport, buildWebsocketTransport } from '../transport/utils'
import { BaseEndpointTypes, cryptoInputParameters, customInputValidation } from './utils'
import {
BaseEndpointTypes,
cryptoInputParameters,
customInputValidation,
customRouter,
} from './utils'

export const endpoint = new PriceEndpoint({
name: 'volume',
transportRoutes: new TransportRoutes<BaseEndpointTypes>()
.register('ws', buildWebsocketTransport('v24h'))
.register('rest', buildCryptoHttpTransport('volume_24h')),
defaultTransport: 'rest',
customRouter,
inputParameters: cryptoInputParameters,
overrides: overrides.coinpaprika,
customInputValidation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('websocket', () => {
base: 'AAA',
coinid: 'eth-ethereum',
quote: 'USD',
transport: 'ws',
endpoint: 'marketcap',
}

Expand All @@ -44,6 +43,7 @@ describe('websocket', () => {
process.env['CACHE_POLLING_MAX_RETRIES'] = process.env['CACHE_POLLING_MAX_RETRIES'] ?? '0'
process.env['METRICS_ENABLED'] = process.env['METRICS_ENABLED'] ?? 'false'
process.env['WS_API_ENDPOINT'] = process.env['WS_API_ENDPOINT'] ?? wsEndpoint
process.env['WS_ENABLED'] = process.env['WS_ENABLED'] ?? 'true'

mockWebSocketProvider(WebSocketClassProvider)
mockWsServer = mockCryptoWebSocketServer(wsEndpoint)
Expand Down

0 comments on commit 8f266fa

Please sign in to comment.