Skip to content

Commit 9b3393f

Browse files
authored
Refactor tokenized-equity (#4656)
1 parent d081075 commit 9b3393f

9 files changed

Lines changed: 417 additions & 232 deletions

File tree

.changeset/short-cups-wait.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

packages/composites/tokenized-equity/src/endpoint/common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { BaseEndpointTypes as DataEngineResponse } from '@chainlink/data-engine-adapter/src/endpoint/deutscheBoerseV11'
22
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
33
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
4+
import { TypeFromDefinition } from '@chainlink/external-adapter-framework/validation/input-params'
45

56
export const inputExample = {
7+
asset: '0x0',
68
regularStreamId: '0x0',
79
extendedStreamId: '0x0',
810
overnightStreamId: '0x0',
@@ -16,6 +18,12 @@ export const inputExample = {
1618

1719
export const inputDefinition = new InputParameters(
1820
{
21+
asset: {
22+
required: true,
23+
type: 'string',
24+
description:
25+
'Unique identifier of the underlying asset. Used to maintain smoother internal state.',
26+
},
1927
regularStreamId: {
2028
required: true,
2129
type: 'string',
@@ -70,6 +78,8 @@ export const inputDefinition = new InputParameters(
7078
[inputExample],
7179
)
7280

81+
export type Smoother = TypeFromDefinition<typeof inputDefinition.definition>['smoother']
82+
7383
export const validateSession = (sessionBoundaries: string[], sessionBoundariesTimeZone: string) => {
7484
sessionBoundaries.forEach((s) => {
7585
if (!s.match(/^(?:[01]\d|2[0-3]):[0-5]\d$/)) {

packages/composites/tokenized-equity/src/endpoint/ondo.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AdapterEndpoint } from '@chainlink/external-adapter-framework/adapter'
22
import { InputParameters } from '@chainlink/external-adapter-framework/validation'
33
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
4-
import { TypeFromDefinition } from '@chainlink/external-adapter-framework/validation/input-params'
54
import { config } from '../config'
65
import { ondoTransport } from '../transport/ondoTransport'
76
import type { output } from './common'
@@ -14,17 +13,11 @@ export const inputParameters = new InputParameters(
1413
type: 'string',
1514
description: 'Ondo on-chain registry address',
1615
},
17-
asset: {
18-
required: true,
19-
type: 'string',
20-
description: 'Maps to the asset in ondo’s on-chain registry',
21-
},
2216
...inputDefinition.definition,
2317
},
2418
[
2519
{
2620
registry: '0x0',
27-
asset: '0x0',
2821
...inputExample,
2922
},
3023
],
@@ -44,8 +37,6 @@ export type BaseEndpointTypes = {
4437
Settings: typeof config.settings
4538
}
4639

47-
export type Smoother = TypeFromDefinition<BaseEndpointTypes['Parameters']>['smoother']
48-
4940
export const endpoint = new AdapterEndpoint({
5041
name: 'ondo',
5142
aliases: [],

packages/composites/tokenized-equity/src/lib/smoother/smoother.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Algorithm by @kalanyuz and @eshaqiri
2-
import { Smoother } from '../../endpoint/ondo'
2+
import { Smoother } from '../../endpoint/common'
33
import { EmaFilter } from './ema'
44
import { KalmanFilter } from './kalman'
55
import { deScale, scale } from './utils'

packages/composites/tokenized-equity/src/transport/ondoPrice.ts

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { Requester } from '@chainlink/external-adapter-framework/util/requester'
22
import { JsonRpcProvider } from 'ethers'
33

44
import { AdapterError } from '@chainlink/external-adapter-framework/validation/error'
5-
import { Smoother } from '../endpoint/ondo'
5+
import { Smoother } from '../endpoint/common'
66
import { getRegistryData } from '../lib/registry'
7-
import { calculateSecondsFromTransition } from '../lib/session/session'
8-
import { processUpdate } from '../lib/smoother/smoother'
9-
import { getPrice } from '../lib/streams'
7+
8+
import { smoothedStreamPrice } from './smoothedPrice'
109

1110
const MULTIPLIER_DECIMALS = 18n
1211

@@ -27,23 +26,9 @@ export const calculatePrice = async (param: {
2726
smoother: Smoother
2827
decimals: number
2928
}) => {
30-
const [price, { multiplier, paused }, secondsFromTransition] = await Promise.all([
31-
getPrice(
32-
param.regularStreamId,
33-
param.extendedStreamId,
34-
param.overnightStreamId,
35-
param.url,
36-
param.requester,
37-
),
29+
const [result, { multiplier, paused }] = await Promise.all([
30+
smoothedStreamPrice(param),
3831
getRegistryData(param.asset, param.registry, param.provider),
39-
calculateSecondsFromTransition(
40-
param.tradingHoursUrl,
41-
param.requester,
42-
param.sessionBoundaries,
43-
param.sessionBoundariesTimeZone,
44-
param.sessionMarket,
45-
param.sessionMarketType,
46-
),
4732
])
4833

4934
if (paused) {
@@ -53,61 +38,12 @@ export const calculatePrice = async (param: {
5338
})
5439
}
5540

56-
const common = {
57-
rawPrice: price.price,
58-
decimals: param.decimals,
41+
return result.map((r) => ({
42+
...r,
5943
registry: {
6044
sValue: multiplier.toString(),
6145
paused,
6246
},
63-
stream: price.data,
64-
}
65-
66-
return ['ema', 'kalman'].map((smoother) => {
67-
const smoothed = smooth(
68-
smoother as Smoother,
69-
param,
70-
price,
71-
secondsFromTransition.value,
72-
multiplier,
73-
)
74-
return {
75-
result: smoothed.result,
76-
...common,
77-
smoother: smoothed.smoother,
78-
sessionSource: secondsFromTransition.source,
79-
}
80-
})
81-
}
82-
83-
const smooth = (
84-
smoother: Smoother,
85-
param: { asset: string; decimals: number },
86-
price: { price: string; spread: bigint; decimals: number },
87-
secondsFromTransition: number,
88-
multiplier: bigint,
89-
) => {
90-
const smoothed = processUpdate(
91-
smoother,
92-
param.asset,
93-
BigInt(price.price),
94-
price.spread,
95-
secondsFromTransition,
96-
)
97-
98-
const result =
99-
(smoothed.price * multiplier * 10n ** BigInt(param.decimals)) /
100-
10n ** BigInt(price.decimals) /
101-
10n ** MULTIPLIER_DECIMALS
102-
103-
return {
104-
result: result.toString(),
105-
smoother: {
106-
smoother,
107-
price: smoothed.price.toString(),
108-
x: smoothed.x.toString(),
109-
p: smoothed.p.toString(),
110-
secondsFromTransition,
111-
},
112-
}
47+
result: ((BigInt(r.result) * multiplier) / 10n ** MULTIPLIER_DECIMALS).toString(),
48+
}))
11349
}

packages/composites/tokenized-equity/src/transport/ondoTransport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { AdapterResponse, makeLogger, sleep } from '@chainlink/external-adapter-
55
import { Requester } from '@chainlink/external-adapter-framework/util/requester'
66
import { AdapterError } from '@chainlink/external-adapter-framework/validation/error'
77
import { JsonRpcProvider } from 'ethers'
8-
import { BaseEndpointTypes, inputParameters, Smoother } from '../endpoint/ondo'
8+
import { Smoother } from '../endpoint/common'
9+
import { BaseEndpointTypes, inputParameters } from '../endpoint/ondo'
910
import { calculatePrice } from './ondoPrice'
1011

1112
const logger = makeLogger('OndoTransport')
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Requester } from '@chainlink/external-adapter-framework/util/requester'
2+
import { Smoother } from '../endpoint/common'
3+
import { calculateSecondsFromTransition } from '../lib/session/session'
4+
import { processUpdate } from '../lib/smoother/smoother'
5+
import { getPrice } from '../lib/streams'
6+
7+
export const smoothedStreamPrice = async (param: {
8+
asset: string
9+
regularStreamId: string
10+
extendedStreamId: string
11+
overnightStreamId: string
12+
url: string
13+
tradingHoursUrl: string
14+
requester: Requester
15+
sessionMarket: string
16+
sessionMarketType: string
17+
sessionBoundaries: string[]
18+
sessionBoundariesTimeZone: string
19+
smoother: Smoother
20+
decimals: number
21+
}) => {
22+
const [price, secondsFromTransition] = await Promise.all([
23+
getPrice(
24+
param.regularStreamId,
25+
param.extendedStreamId,
26+
param.overnightStreamId,
27+
param.url,
28+
param.requester,
29+
),
30+
calculateSecondsFromTransition(
31+
param.tradingHoursUrl,
32+
param.requester,
33+
param.sessionBoundaries,
34+
param.sessionBoundariesTimeZone,
35+
param.sessionMarket,
36+
param.sessionMarketType,
37+
),
38+
])
39+
40+
const common = {
41+
rawPrice: price.price,
42+
decimals: param.decimals,
43+
stream: price.data,
44+
}
45+
46+
return ['ema', 'kalman'].map((smoother) => {
47+
const smoothed = smooth(smoother as Smoother, param, price, secondsFromTransition.value)
48+
return {
49+
result: smoothed.result,
50+
...common,
51+
smoother: smoothed.smoother,
52+
sessionSource: secondsFromTransition.source,
53+
}
54+
})
55+
}
56+
57+
const smooth = (
58+
smoother: Smoother,
59+
param: { asset: string; decimals: number },
60+
price: { price: string; spread: bigint; decimals: number },
61+
secondsFromTransition: number,
62+
) => {
63+
const smoothed = processUpdate(
64+
smoother,
65+
param.asset,
66+
BigInt(price.price),
67+
price.spread,
68+
secondsFromTransition,
69+
)
70+
71+
const result = (smoothed.price * 10n ** BigInt(param.decimals)) / 10n ** BigInt(price.decimals)
72+
73+
return {
74+
result: result,
75+
smoother: {
76+
smoother,
77+
price: smoothed.price.toString(),
78+
x: smoothed.x.toString(),
79+
p: smoothed.p.toString(),
80+
secondsFromTransition,
81+
},
82+
}
83+
}

0 commit comments

Comments
 (0)