Skip to content

Commit d081075

Browse files
authored
Refactor tokenized-equity (#4655)
1 parent 99dd03d commit d081075

3 files changed

Lines changed: 125 additions & 95 deletions

File tree

.changeset/eighty-pans-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/tokenized-equity-adapter': patch
3+
---
4+
5+
Code refactor
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { BaseEndpointTypes as DataEngineResponse } from '@chainlink/data-engine-adapter/src/endpoint/deutscheBoerseV11'
2+
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
3+
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
4+
5+
export const inputExample = {
6+
regularStreamId: '0x0',
7+
extendedStreamId: '0x0',
8+
overnightStreamId: '0x0',
9+
sessionMarket: 'nyse',
10+
sessionMarketType: '24/5',
11+
sessionBoundaries: ['04:00', '16:00', '20:00'],
12+
sessionBoundariesTimeZone: 'America/New_York',
13+
smoother: 'kalman' as const,
14+
decimals: 8,
15+
}
16+
17+
export const inputDefinition = new InputParameters(
18+
{
19+
regularStreamId: {
20+
required: true,
21+
type: 'string',
22+
description: 'Data Streams regular hour feed ID for the underlying asset',
23+
},
24+
extendedStreamId: {
25+
required: true,
26+
type: 'string',
27+
description: 'Data Streams extended hour feed ID for the underlying asset',
28+
},
29+
overnightStreamId: {
30+
required: true,
31+
type: 'string',
32+
description: 'Data Streams overnight hour feed ID for the underlying asset',
33+
},
34+
sessionMarket: {
35+
required: true,
36+
type: 'string',
37+
description:
38+
'The name of the market for session times, for example nyse. This is passed to the tradinghours adapter as the `market` parameter.',
39+
},
40+
sessionMarketType: {
41+
required: true,
42+
type: 'string',
43+
description:
44+
'The type of the market for session times, for example 24/5. This is passed to the tradinghours adapter as the `type` parameter.',
45+
},
46+
sessionBoundaries: {
47+
required: true,
48+
type: 'string',
49+
array: true,
50+
description:
51+
'(backup) A list of time where market trasition from 1 session to the next in the format of HH:MM. This is only used when the adapter is unable to fetch session times from the tradinghours EA',
52+
},
53+
sessionBoundariesTimeZone: {
54+
required: true,
55+
type: 'string',
56+
description: 'ANA Time Zone Database format',
57+
},
58+
smoother: {
59+
type: 'string',
60+
description: 'Smoothing algorithm to apply to the price',
61+
options: ['kalman', 'ema'],
62+
default: 'kalman',
63+
},
64+
decimals: {
65+
type: 'number',
66+
description: 'Decimals of output result',
67+
default: 8,
68+
},
69+
},
70+
[inputExample],
71+
)
72+
73+
export const validateSession = (sessionBoundaries: string[], sessionBoundariesTimeZone: string) => {
74+
sessionBoundaries.forEach((s) => {
75+
if (!s.match(/^(?:[01]\d|2[0-3]):[0-5]\d$/)) {
76+
throw new AdapterInputError({
77+
statusCode: 400,
78+
message: `${s} in [Param: sessionBoundaries] does not match format HH:MM`,
79+
})
80+
}
81+
})
82+
83+
try {
84+
// eslint-disable-next-line new-cap
85+
Intl.DateTimeFormat(undefined, { timeZone: sessionBoundariesTimeZone })
86+
} catch (error) {
87+
throw new AdapterInputError({
88+
statusCode: 400,
89+
message: `[Param: sessionBoundariesTimeZone] is not valid timezone: ${error}`,
90+
})
91+
}
92+
return
93+
}
94+
95+
export type output = {
96+
result: string
97+
decimals: number
98+
rawPrice: string
99+
stream: {
100+
regular: DataEngineResponse['Response']['Data']
101+
extended: DataEngineResponse['Response']['Data']
102+
overnight: DataEngineResponse['Response']['Data']
103+
}
104+
smoother: {
105+
price: string
106+
x: string
107+
p: string
108+
secondsFromTransition: number
109+
}
110+
sessionSource: 'TRADINGHOURS' | 'FALLBACK'
111+
}
Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { BaseEndpointTypes as DataEngineResponse } from '@chainlink/data-engine-adapter/src/endpoint/deutscheBoerseV11'
21
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
32
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
43
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
54
import { TypeFromDefinition } from '@chainlink/external-adapter-framework/validation/input-params'
65
import { config } from '../config'
76
import { ondoTransport } from '../transport/ondoTransport'
7+
import type { output } from './common'
8+
import { inputDefinition, inputExample, validateSession } from './common'
89

910
export const inputParameters = new InputParameters(
1011
{
@@ -18,70 +19,13 @@ export const inputParameters = new InputParameters(
1819
type: 'string',
1920
description: 'Maps to the asset in ondo’s on-chain registry',
2021
},
21-
regularStreamId: {
22-
required: true,
23-
type: 'string',
24-
description: 'Data Streams regular hour feed ID for the underlying asset',
25-
},
26-
extendedStreamId: {
27-
required: true,
28-
type: 'string',
29-
description: 'Data Streams extended hour feed ID for the underlying asset',
30-
},
31-
overnightStreamId: {
32-
required: true,
33-
type: 'string',
34-
description: 'Data Streams overnight hour feed ID for the underlying asset',
35-
},
36-
sessionMarket: {
37-
required: true,
38-
type: 'string',
39-
description:
40-
'The name of the market for session times, for example nyse. This is passed to the tradinghours adapter as the `market` parameter.',
41-
},
42-
sessionMarketType: {
43-
required: true,
44-
type: 'string',
45-
description:
46-
'The type of the market for session times, for example 24/5. This is passed to the tradinghours adapter as the `type` parameter.',
47-
},
48-
sessionBoundaries: {
49-
required: true,
50-
type: 'string',
51-
array: true,
52-
description:
53-
'(backup) A list of time where market trasition from 1 session to the next in the format of HH:MM. This is only used when the adapter is unable to fetch session times from the tradinghours EA',
54-
},
55-
sessionBoundariesTimeZone: {
56-
required: true,
57-
type: 'string',
58-
description: 'ANA Time Zone Database format',
59-
},
60-
smoother: {
61-
type: 'string',
62-
description: 'Smoothing algorithm to apply to the price',
63-
options: ['kalman', 'ema'],
64-
default: 'kalman',
65-
},
66-
decimals: {
67-
type: 'number',
68-
description: 'Decimals of output result',
69-
default: 8,
70-
},
22+
...inputDefinition.definition,
7123
},
7224
[
7325
{
7426
registry: '0x0',
7527
asset: '0x0',
76-
regularStreamId: '0x0',
77-
extendedStreamId: '0x0',
78-
overnightStreamId: '0x0',
79-
sessionMarket: 'nyse',
80-
sessionMarketType: '24/5',
81-
sessionBoundaries: ['04:00', '16:00', '20:00'],
82-
sessionBoundariesTimeZone: 'America/New_York',
83-
smoother: 'kalman',
84-
decimals: 8,
28+
...inputExample,
8529
},
8630
],
8731
)
@@ -90,26 +34,11 @@ export type BaseEndpointTypes = {
9034
Parameters: typeof inputParameters.definition
9135
Response: {
9236
Result: string
93-
Data: {
94-
result: string
95-
rawPrice: string
96-
decimals: number
37+
Data: output & {
9738
registry: {
9839
sValue: string
9940
paused: boolean
10041
}
101-
stream: {
102-
regular: DataEngineResponse['Response']['Data']
103-
extended: DataEngineResponse['Response']['Data']
104-
overnight: DataEngineResponse['Response']['Data']
105-
}
106-
smoother: {
107-
price: string
108-
x: string
109-
p: string
110-
secondsFromTransition: number
111-
}
112-
sessionSource: 'TRADINGHOURS' | 'FALLBACK'
11342
}
11443
}
11544
Settings: typeof config.settings
@@ -130,26 +59,11 @@ export const endpoint = new AdapterEndpoint({
13059
})
13160
}
13261

133-
const { sessionBoundaries, sessionBoundariesTimeZone } = req.requestContext.data
134-
135-
sessionBoundaries.forEach((s) => {
136-
if (!s.match(/^(?:[01]\d|2[0-3]):[0-5]\d$/)) {
137-
throw new AdapterInputError({
138-
statusCode: 400,
139-
message: `${s} in [Param: sessionBoundaries] does not match format HH:MM`,
140-
})
141-
}
142-
})
62+
validateSession(
63+
req.requestContext.data.sessionBoundaries,
64+
req.requestContext.data.sessionBoundariesTimeZone,
65+
)
14366

144-
try {
145-
// eslint-disable-next-line new-cap
146-
Intl.DateTimeFormat(undefined, { timeZone: sessionBoundariesTimeZone })
147-
} catch (error) {
148-
throw new AdapterInputError({
149-
statusCode: 400,
150-
message: `[Param: sessionBoundariesTimeZone] is not valid timezone: ${error}`,
151-
})
152-
}
15367
return
15468
},
15569
})

0 commit comments

Comments
 (0)