-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New NCFX market status endpoint (#3387)
- Loading branch information
1 parent
e01c723
commit ddda456
Showing
10 changed files
with
252 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@chainlink/ncfx-adapter': minor | ||
--- | ||
|
||
New market-status endpoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { cryptoEndpoint as crypto } from './crypto' | ||
export { cryptoLwbaEndpoint as lwba } from './crypto-lwba' | ||
export { forexEndpoint as forex } from './forex' | ||
export { marketStatusEndpoint as marketStatus } from './market-status' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
MarketStatusEndpoint, | ||
MarketStatusResultResponse, | ||
} from '@chainlink/external-adapter-framework/adapter' | ||
import { InputParameters } from '@chainlink/external-adapter-framework/validation' | ||
|
||
import { config } from '../config' | ||
import { markets, transport } from '../transport/market-status' | ||
|
||
const inputParameters = new InputParameters({ | ||
market: { | ||
aliases: [], | ||
type: 'string', | ||
description: 'The name of the market', | ||
options: markets, | ||
required: true, | ||
}, | ||
}) | ||
|
||
export type BaseEndpointTypes = { | ||
Parameters: typeof inputParameters.definition | ||
Response: MarketStatusResultResponse | ||
Settings: typeof config.settings | ||
} | ||
|
||
export const marketStatusEndpoint = new MarketStatusEndpoint({ | ||
name: 'market-status', | ||
aliases: ['forex-market-status', 'metals-market-status'], | ||
transport, | ||
inputParameters, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { MarketStatus } from '@chainlink/external-adapter-framework/adapter' | ||
import { WebSocketTransport } from '@chainlink/external-adapter-framework/transports' | ||
import { makeLogger, ProviderResult } from '@chainlink/external-adapter-framework/util' | ||
import type { BaseEndpointTypes } from '../endpoint/market-status' | ||
|
||
export const markets = ['forex', 'metals'] as const | ||
|
||
export type Market = (typeof markets)[number] | ||
|
||
const marketToNcfxMarket: Record<Market, keyof WsMessage['marketStatus']> = { | ||
forex: 'fx', | ||
metals: 'metals', | ||
} | ||
|
||
type WsMessage = { | ||
marketStatus: { | ||
fx: string | ||
metals: string | ||
} | ||
timestamp: string | ||
} | ||
|
||
type WsTransportTypes = BaseEndpointTypes & { | ||
Provider: { | ||
WsMessage: WsMessage | ||
} | ||
} | ||
|
||
const logger = makeLogger('NcfxMarketStatusEndpoint') | ||
|
||
export const transport = new WebSocketTransport<WsTransportTypes>({ | ||
url: (context) => context.adapterSettings.MARKET_STATUS_WS_API_ENDPOINT, | ||
options: (context) => { | ||
return { headers: { 'x-api-key': context.adapterSettings.MARKET_STATUS_WS_API_KEY } } | ||
}, | ||
handlers: { | ||
message(message: WsMessage): ProviderResult<WsTransportTypes>[] { | ||
if (!('marketStatus' in message) || typeof message.marketStatus !== 'object') { | ||
logger.warn('Invalid marketStatus field in response') | ||
return [] | ||
} | ||
return markets.map((market) => { | ||
const marketStatus = parseMarketStatus(message.marketStatus[marketToNcfxMarket[market]]) | ||
return { | ||
params: { market }, | ||
response: { | ||
result: marketStatus, | ||
data: { | ||
result: marketStatus, | ||
}, | ||
timestamps: { | ||
providerIndicatedTimeUnixMs: new Date(message.timestamp).getTime(), | ||
}, | ||
}, | ||
} | ||
}) | ||
}, | ||
}, | ||
}) | ||
|
||
export function parseMarketStatus(marketStatus: string): MarketStatus { | ||
if (marketStatus === 'open') { | ||
return MarketStatus.OPEN | ||
} | ||
if (marketStatus === 'closed') { | ||
return MarketStatus.CLOSED | ||
} | ||
logger.warn(`Unexpected market status value: ${marketStatus}`) | ||
return MarketStatus.UNKNOWN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.