Skip to content

Commit

Permalink
DF-19409 use asset-quotes ws endpoint for coinmetrics lwba, removing …
Browse files Browse the repository at this point in the history
…pair-quotes (#3187)

* DF-19409 use asset-quotes ws endpoint for coinmetrics lwba, removing pair-quotes

* add changeset
  • Loading branch information
mmcallister-cll authored Feb 19, 2024
1 parent 734897d commit 65b5529
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-trains-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/coinmetrics-adapter': patch
---

Updated LWBA to use asset-quotes endpoint exclusively
19 changes: 3 additions & 16 deletions packages/sources/coinmetrics/src/endpoint/lwba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
LwbaResponseDataFields,
lwbaEndpointInputParametersDefinition,
} from '@chainlink/external-adapter-framework/adapter'
import { TransportRoutes } from '@chainlink/external-adapter-framework/transports'
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
import { config } from '../config'
import { assetQuoteWebsocketTransport, pairQuoteWebsocketTransport } from '../transport/lwba'
import { wsTransport } from '../transport/lwba'

export const inputParameters = new InputParameters(lwbaEndpointInputParametersDefinition, [
{
Expand All @@ -15,26 +14,14 @@ export const inputParameters = new InputParameters(lwbaEndpointInputParametersDe
},
])

const assets: string[] = ['bnb', 'uni', 'sol', 'ltc', 'xrp', 'arb']

export type BaseEndpointTypes = {
Parameters: typeof inputParameters.definition
Settings: typeof config.settings
Response: LwbaResponseDataFields
}

export const endpoint = new LwbaEndpoint<BaseEndpointTypes>({
export const endpoint = new LwbaEndpoint({
name: 'crypto-lwba',
transportRoutes: new TransportRoutes<BaseEndpointTypes>()
.register('asset', assetQuoteWebsocketTransport)
.register('pair', pairQuoteWebsocketTransport),
customRouter: (req, _) => {
const { base } = req.requestContext.data as typeof inputParameters.validated & {
transport?: string
}
const route = assets.includes(base.toLowerCase()) ? 'asset' : 'pair'

return route
},
transport: wsTransport,
inputParameters,
})
36 changes: 7 additions & 29 deletions packages/sources/coinmetrics/src/transport/lwba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,15 @@ export type WsTransportTypes = BaseEndpointTypes & {
WsMessage: WsPairQuoteMessage
}
}
export const calculatePairQuotesUrl = (
export const calculateUrl = (
context: EndpointContext<WsTransportTypes>,
desiredSubs: (typeof inputParameters.validated)[],
isAssetQuote: boolean,
): string => {
const { API_KEY, WS_API_ENDPOINT } = context.adapterSettings

let generated = new URL('/v4/timeseries-stream/pair-quotes', WS_API_ENDPOINT)

// use asset-quotes api if base=[BNB,UNI,SOL,LTC,XRP]
if (isAssetQuote) {
generated = new URL('/v4/timeseries-stream/asset-quotes', WS_API_ENDPOINT)
const assets = [...new Set(desiredSubs.map((pair) => pair.base.toLowerCase()))].sort().join(',')
generated.searchParams.append('assets', assets)
} else {
const pairs = [
...new Set(desiredSubs.map((sub) => `${sub.base.toLowerCase()}-${sub.quote.toLowerCase()}`)),
]
.sort()
.join(',')
generated.searchParams.append('pairs', pairs)
}
const generated = new URL('/v4/timeseries-stream/asset-quotes', WS_API_ENDPOINT)
const assets = [...new Set(desiredSubs.map((pair) => pair.base.toLowerCase()))].sort().join(',')
generated.searchParams.append('assets', assets)

generated.searchParams.append('api_key', API_KEY)
logger.debug(`Generated URL: ${generated.toString()}`)
Expand Down Expand Up @@ -124,19 +111,10 @@ export const handleCryptoLwbaMessage = (
}
return undefined
}
export const pairQuoteWebsocketTransport = new WebSocketTransport<WsTransportTypes>({
url: (context, desiredSubs) => {
return calculatePairQuotesUrl(context, desiredSubs, false)
},
handlers: {
message(message: WsPairQuoteMessage): ProviderResult<WsTransportTypes>[] | undefined {
return handleCryptoLwbaMessage(message)
},
},
})
export const assetQuoteWebsocketTransport = new WebSocketTransport<WsTransportTypes>({

export const wsTransport = new WebSocketTransport<WsTransportTypes>({
url: (context, desiredSubs) => {
return calculatePairQuotesUrl(context, desiredSubs, true)
return calculateUrl(context, desiredSubs)
},
handlers: {
message(message: WsPairQuoteMessage): ProviderResult<WsTransportTypes>[] | undefined {
Expand Down
5 changes: 5 additions & 0 deletions packages/sources/coinmetrics/test-payload.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"from": "LINK",
"to": "USD"
},
{
"from": "ETH",
"to": "USD",
"endpoint": "crypto-lwba"
},
{
"from": "ETH",
"to": "USD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('websocket', () => {
let testAdapter: TestAdapter
let oldEnv: NodeJS.ProcessEnv
const wsEndpoint = 'ws://localhost:9090/v4/timeseries-stream/asset-metrics'
const wsEndpointLwba = 'ws://localhost:9090/v4/timeseries-stream/pair-quotes'
const wsEndpointLwba = 'ws://localhost:9090/v4/timeseries-stream/asset-quotes'
const data = {
base: 'ETH',
quote: 'USD',
Expand Down

0 comments on commit 65b5529

Please sign in to comment.