@@ -13,7 +13,76 @@ export const DeriveType = ['Unknown', 'TWAP', 'Volatility']
13
13
const empty32Buffer = Buffer . alloc ( 32 )
14
14
const PKorNull = ( data : Buffer ) => ( data . equals ( empty32Buffer ) ? null : new PublicKey ( data ) )
15
15
16
- export const parseMappingData = ( data : Buffer ) => {
16
+ export interface Base {
17
+ magic : number ;
18
+ version : number ;
19
+ type : number ;
20
+ size : number ;
21
+ }
22
+
23
+ export interface MappingData extends Base {
24
+ nextMappingAccount : PublicKey | null ;
25
+ productAccountKeys : PublicKey [ ] ,
26
+ }
27
+
28
+ export interface Product {
29
+ symbol : string ;
30
+ asset_type : string ;
31
+ quote_currency : string ;
32
+ tenor : string ;
33
+ [ index : string ] : string
34
+ }
35
+
36
+ export interface ProductData extends Base {
37
+ priceAccountKey : PublicKey ;
38
+ product : Product ,
39
+ }
40
+
41
+ export interface Price {
42
+ priceComponent : bigint ;
43
+ price : number ;
44
+ confidenceComponent : bigint ;
45
+ confidence : number ;
46
+ status : number ;
47
+ corporateAction : number ;
48
+ publishSlot : bigint ;
49
+ }
50
+
51
+ export interface PriceComponent {
52
+ publisher : PublicKey | null ;
53
+ aggregate : Price ;
54
+ latest : Price ;
55
+ }
56
+
57
+ export interface PriceData extends Base , Price {
58
+ priceType : number ;
59
+ exponent : number ;
60
+ numComponentPrices : number ;
61
+ currentSlot : bigint ;
62
+ validSlot : bigint ;
63
+ twapComponent : bigint ;
64
+ twap : number ;
65
+ avolComponent : bigint ;
66
+ avol : number ;
67
+ drv0Component : bigint ;
68
+ drv0 : number ;
69
+ drv1Component : bigint ;
70
+ drv1 : number ;
71
+ drv2Component : bigint ;
72
+ drv2 : number ;
73
+ drv3Component : bigint ;
74
+ drv3 : number ;
75
+ drv4Component : bigint ;
76
+ drv4 : number ;
77
+ drv5Component : bigint ;
78
+ drv5 : number ;
79
+ productAccountKey : PublicKey ;
80
+ nextPriceAccountKey : PublicKey | null ;
81
+ aggregatePriceUpdaterAccountKey : PublicKey ;
82
+ priceComponents : PriceComponent [ ]
83
+ }
84
+
85
+ export const parseMappingData = ( data : Buffer ) : MappingData => {
17
86
// pyth magic number
18
87
const magic = data . readUInt32LE ( 0 )
19
88
// program version
@@ -31,7 +100,7 @@ export const parseMappingData = (data: Buffer) => {
31
100
const nextMappingAccount = PKorNull ( data . slice ( 24 , 56 ) )
32
101
// read each symbol account
33
102
let offset = 56
34
- const productAccountKeys = [ ]
103
+ const productAccountKeys : PublicKey [ ] = [ ]
35
104
for ( let i = 0 ; i < numProducts ; i ++ ) {
36
105
const productAccountBytes = data . slice ( offset , offset + 32 )
37
106
const productAccountKey = new PublicKey ( productAccountBytes )
@@ -48,11 +117,7 @@ export const parseMappingData = (data: Buffer) => {
48
117
}
49
118
}
50
119
51
- interface ProductAttributes {
52
- [ index : string ] : string
53
- }
54
-
55
- export const parseProductData = ( data : Buffer ) => {
120
+ export const parseProductData = ( data : Buffer ) : ProductData => {
56
121
// pyth magic number
57
122
const magic = data . readUInt32LE ( 0 )
58
123
// program version
@@ -64,7 +129,7 @@ export const parseProductData = (data: Buffer) => {
64
129
// first price account in list
65
130
const priceAccountBytes = data . slice ( 16 , 48 )
66
131
const priceAccountKey = new PublicKey ( priceAccountBytes )
67
- const product : ProductAttributes = { }
132
+ const product = { } as Product
68
133
let idx = 48
69
134
while ( idx < size ) {
70
135
const keyLength = data [ idx ]
@@ -82,7 +147,7 @@ export const parseProductData = (data: Buffer) => {
82
147
return { magic, version, type, size, priceAccountKey, product }
83
148
}
84
149
85
- const parsePriceInfo = ( data : Buffer , exponent : number ) => {
150
+ const parsePriceInfo = ( data : Buffer , exponent : number ) : Price => {
86
151
// aggregate price
87
152
const priceComponent = readBigInt64LE ( data , 0 )
88
153
const price = Number ( priceComponent ) * 10 ** exponent
@@ -106,7 +171,7 @@ const parsePriceInfo = (data: Buffer, exponent: number) => {
106
171
}
107
172
}
108
173
109
- export const parsePriceData = ( data : Buffer ) => {
174
+ export const parsePriceData = ( data : Buffer ) : PriceData => {
110
175
// pyth magic number
111
176
const magic = data . readUInt32LE ( 0 )
112
177
// program version
@@ -154,7 +219,7 @@ export const parsePriceData = (data: Buffer) => {
154
219
const aggregatePriceUpdaterAccountKey = new PublicKey ( data . slice ( 176 , 208 ) )
155
220
const aggregatePriceInfo = parsePriceInfo ( data . slice ( 208 , 240 ) , exponent )
156
221
// price components - up to 32
157
- const priceComponents = [ ]
222
+ const priceComponents : PriceComponent [ ] = [ ]
158
223
let offset = 240
159
224
let shouldContinue = true
160
225
while ( offset < data . length && shouldContinue ) {
0 commit comments