@@ -54,31 +54,34 @@ export interface PriceComponent {
54
54
latest : Price
55
55
}
56
56
57
+ export interface Ema {
58
+ valueComponent : bigint
59
+ value : number
60
+ numerator : bigint
61
+ denominator : bigint
62
+ }
63
+
57
64
export interface PriceData extends Base , Price {
58
65
priceType : number
59
66
exponent : number
60
67
numComponentPrices : number
61
- currentSlot : bigint
68
+ lastSlot : bigint
62
69
validSlot : bigint
63
- twapComponent : bigint
64
- twap : number
65
- avolComponent : bigint
66
- avol : number
67
- drv0Component : bigint
68
- drv0 : number
70
+ twap : Ema
71
+ twac : Ema
69
72
drv1Component : bigint
70
73
drv1 : number
71
74
drv2Component : bigint
72
75
drv2 : number
73
- drv3Component : bigint
74
- drv3 : number
75
- drv4Component : bigint
76
- drv4 : number
77
- drv5Component : bigint
78
- drv5 : number
79
76
productAccountKey : PublicKey
80
77
nextPriceAccountKey : PublicKey | null
81
- aggregatePriceUpdaterAccountKey : PublicKey
78
+ previousSlot : bigint
79
+ previousPriceComponent : bigint
80
+ previousPrice : number
81
+ previousConfidenceComponent : bigint
82
+ previousConfidence : number
83
+ drv3Component : bigint
84
+ drv3 : number
82
85
priceComponents : PriceComponent [ ]
83
86
}
84
87
@@ -95,7 +98,6 @@ export const parseMappingData = (data: Buffer): MappingData => {
95
98
const numProducts = data . readUInt32LE ( 16 )
96
99
// unused
97
100
// const unused = accountInfo.data.readUInt32LE(20)
98
- // TODO: check and use this
99
101
// next mapping account (if any)
100
102
const nextMappingAccount = PKorNull ( data . slice ( 24 , 56 ) )
101
103
// read each symbol account
@@ -147,6 +149,17 @@ export const parseProductData = (data: Buffer): ProductData => {
147
149
return { magic, version, type, size, priceAccountKey, product }
148
150
}
149
151
152
+ const parseEma = ( data : Buffer , exponent : number ) : Ema => {
153
+ // current value of ema
154
+ const valueComponent = readBigInt64LE ( data , 0 )
155
+ const value = Number ( valueComponent ) * 10 ** exponent
156
+ // numerator state for next update
157
+ const numerator = readBigInt64LE ( data , 8 )
158
+ // denominator state for next update
159
+ const denominator = readBigInt64LE ( data , 16 )
160
+ return { valueComponent, value, numerator, denominator }
161
+ }
162
+
150
163
const parsePriceInfo = ( data : Buffer , exponent : number ) : Price => {
151
164
// aggregate price
152
165
const priceComponent = readBigInt64LE ( data , 0 )
@@ -188,35 +201,34 @@ export const parsePriceData = (data: Buffer): PriceData => {
188
201
const numComponentPrices = data . readUInt32LE ( 24 )
189
202
// unused
190
203
// const unused = accountInfo.data.readUInt32LE(28)
191
- // currently accumulating price slot
192
- const currentSlot = readBigUInt64LE ( data , 32 )
204
+ // slot of last valid (not unknown) aggregate price
205
+ const lastSlot = readBigUInt64LE ( data , 32 )
193
206
// valid on-chain slot of aggregate price
194
207
const validSlot = readBigUInt64LE ( data , 40 )
195
208
// time-weighted average price
196
- const twapComponent = readBigInt64LE ( data , 48 )
197
- const twap = Number ( twapComponent ) * 10 ** exponent
198
- // annualized price volatility
199
- const avolComponent = readBigUInt64LE ( data , 56 )
200
- const avol = Number ( avolComponent ) * 10 ** exponent
209
+ const twap = parseEma ( data . slice ( 48 , 72 ) , exponent )
210
+ // time-weighted average confidence interval
211
+ const twac = parseEma ( data . slice ( 72 , 96 ) , exponent )
201
212
// space for future derived values
202
- const drv0Component = readBigInt64LE ( data , 64 )
203
- const drv0 = Number ( drv0Component ) * 10 ** exponent
204
- const drv1Component = readBigInt64LE ( data , 72 )
213
+ const drv1Component = readBigInt64LE ( data , 96 )
205
214
const drv1 = Number ( drv1Component ) * 10 ** exponent
206
- const drv2Component = readBigInt64LE ( data , 80 )
215
+ const drv2Component = readBigInt64LE ( data , 104 )
207
216
const drv2 = Number ( drv2Component ) * 10 ** exponent
208
- const drv3Component = readBigInt64LE ( data , 88 )
209
- const drv3 = Number ( drv3Component ) * 10 ** exponent
210
- const drv4Component = readBigInt64LE ( data , 96 )
211
- const drv4 = Number ( drv4Component ) * 10 ** exponent
212
- const drv5Component = readBigInt64LE ( data , 104 )
213
- const drv5 = Number ( drv5Component ) * 10 ** exponent
214
217
// product id / reference account
215
218
const productAccountKey = new PublicKey ( data . slice ( 112 , 144 ) )
216
219
// next price account in list
217
220
const nextPriceAccountKey = PKorNull ( data . slice ( 144 , 176 ) )
218
- // aggregate price updater
219
- const aggregatePriceUpdaterAccountKey = new PublicKey ( data . slice ( 176 , 208 ) )
221
+ // valid slot of previous update
222
+ const previousSlot = readBigUInt64LE ( data , 176 )
223
+ // aggregate price of previous update
224
+ const previousPriceComponent = readBigInt64LE ( data , 184 )
225
+ const previousPrice = Number ( previousPriceComponent ) * 10 ** exponent
226
+ // confidence interval of previous update
227
+ const previousConfidenceComponent = readBigUInt64LE ( data , 192 )
228
+ const previousConfidence = Number ( previousConfidenceComponent ) * 10 ** exponent
229
+ // space for future derived values
230
+ const drv3Component = readBigInt64LE ( data , 200 )
231
+ const drv3 = Number ( drv3Component ) * 10 ** exponent
220
232
const aggregatePriceInfo = parsePriceInfo ( data . slice ( 208 , 240 ) , exponent )
221
233
// price components - up to 32
222
234
const priceComponents : PriceComponent [ ] = [ ]
@@ -243,27 +255,23 @@ export const parsePriceData = (data: Buffer): PriceData => {
243
255
priceType,
244
256
exponent,
245
257
numComponentPrices,
246
- currentSlot ,
258
+ lastSlot ,
247
259
validSlot,
248
- twapComponent,
249
260
twap,
250
- avolComponent,
251
- avol,
252
- drv0Component,
253
- drv0,
261
+ twac,
254
262
drv1Component,
255
263
drv1,
256
264
drv2Component,
257
265
drv2,
258
- drv3Component,
259
- drv3,
260
- drv4Component,
261
- drv4,
262
- drv5Component,
263
- drv5,
264
266
productAccountKey,
265
267
nextPriceAccountKey,
266
- aggregatePriceUpdaterAccountKey,
268
+ previousSlot,
269
+ previousPriceComponent,
270
+ previousPrice,
271
+ previousConfidenceComponent,
272
+ previousConfidence,
273
+ drv3Component,
274
+ drv3,
267
275
...aggregatePriceInfo ,
268
276
priceComponents,
269
277
}
0 commit comments