From ef9f82dc53e5ec0d6add3eda03d8cf5ba4a63b26 Mon Sep 17 00:00:00 2001 From: Martin Liu Date: Wed, 24 Jul 2024 05:22:40 +1000 Subject: [PATCH] Add market status --- .tool-versions | 1 + src/adapter/index.ts | 5 ++- src/adapter/market-status.ts | 76 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 .tool-versions create mode 100644 src/adapter/market-status.ts diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..00945565 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +nodejs 16.14.2 diff --git a/src/adapter/index.ts b/src/adapter/index.ts index aa519dec..956dc3c2 100644 --- a/src/adapter/index.ts +++ b/src/adapter/index.ts @@ -1,5 +1,6 @@ export * from './basic' -export * from './price' export * from './endpoint' -export * from './types' export * from './lwba' +export * from './market-status' +export * from './price' +export * from './types' diff --git a/src/adapter/market-status.ts b/src/adapter/market-status.ts new file mode 100644 index 00000000..79a56a1f --- /dev/null +++ b/src/adapter/market-status.ts @@ -0,0 +1,76 @@ +import { TransportGenerics } from '../transports' +import { SingleNumberResultResponse } from '../util' +import { AdapterError } from '../validation/error' +import { InputParametersDefinition } from '../validation/input-params' +import { AdapterEndpoint } from './endpoint' +import { AdapterEndpointParams } from './index' + +/** + * Type for the base input parameter config that any [[MarketStatusEndpoint]] must extend + */ +export type MarketStatusEndpointInputParametersDefinition = InputParametersDefinition + +/** + * Base input parameter config that any [[MarketStatusEndpoint]] must extend + */ +export const marketStatusEndpointInputParametersDefinition = { + market: { + aliases: [], + type: 'string', + description: 'The name of the market', + required: true, + }, +} as const satisfies MarketStatusEndpointInputParametersDefinition + +export enum MarketStatus { + UNKNOWN = 0, + CLOSED = 1, + OPEN = 2, +} + +/** + * Helper type structure that contains the different types passed to the generic parameters of a PriceEndpoint + */ +export type MarketStatusEndpointGenerics = TransportGenerics & { + Parameters: MarketStatusEndpointInputParametersDefinition + Response: SingleNumberResultResponse +} + +export const DEFAULT_MARKET_STATUS_ALIASES = [] + +export const validateMarketStatusResponse = (marketStatus?: number): string => { + if (!marketStatus) { + return `Invariant violation. Response must contain the market status` + } + return '' +} + +/** + * An MarketStatusEndpoint is a specific type of AdapterEndpoint. Meant to comply with standard practices for + * RWA (lightweight bid/ask) Data Feeds, its InputParameters must extend the basic ones (base/quote). + */ +export class MarketStatusEndpoint< + T extends MarketStatusEndpointGenerics, +> extends AdapterEndpoint { + constructor(params: AdapterEndpointParams) { + if (!params.aliases) { + params.aliases = [] + } + for (const alias of DEFAULT_MARKET_STATUS_ALIASES) { + if (params.name !== alias && !params.aliases.includes(alias)) { + params.aliases.push(alias) + } + } + + params.customOutputValidation = (output) => { + const data = output.data as SingleNumberResultResponse['Data'] + const error = validateMarketStatusResponse(data.result) + if (error) { + throw new AdapterError({ statusCode: 500, message: error }) + } + return undefined + } + + super(params) + } +}