Skip to content

Commit 142f086

Browse files
authored
feat: add types (#2)
* feat: add types * add price components type
1 parent d3668b6 commit 142f086

File tree

1 file changed

+76
-11
lines changed

1 file changed

+76
-11
lines changed

src/index.ts

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,76 @@ export const DeriveType = ['Unknown', 'TWAP', 'Volatility']
1313
const empty32Buffer = Buffer.alloc(32)
1414
const PKorNull = (data: Buffer) => (data.equals(empty32Buffer) ? null : new PublicKey(data))
1515

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 => {
1786
// pyth magic number
1887
const magic = data.readUInt32LE(0)
1988
// program version
@@ -31,7 +100,7 @@ export const parseMappingData = (data: Buffer) => {
31100
const nextMappingAccount = PKorNull(data.slice(24, 56))
32101
// read each symbol account
33102
let offset = 56
34-
const productAccountKeys = []
103+
const productAccountKeys: PublicKey[] = []
35104
for (let i = 0; i < numProducts; i++) {
36105
const productAccountBytes = data.slice(offset, offset + 32)
37106
const productAccountKey = new PublicKey(productAccountBytes)
@@ -48,11 +117,7 @@ export const parseMappingData = (data: Buffer) => {
48117
}
49118
}
50119

51-
interface ProductAttributes {
52-
[index: string]: string
53-
}
54-
55-
export const parseProductData = (data: Buffer) => {
120+
export const parseProductData = (data: Buffer): ProductData => {
56121
// pyth magic number
57122
const magic = data.readUInt32LE(0)
58123
// program version
@@ -64,7 +129,7 @@ export const parseProductData = (data: Buffer) => {
64129
// first price account in list
65130
const priceAccountBytes = data.slice(16, 48)
66131
const priceAccountKey = new PublicKey(priceAccountBytes)
67-
const product: ProductAttributes = {}
132+
const product = {} as Product
68133
let idx = 48
69134
while (idx < size) {
70135
const keyLength = data[idx]
@@ -82,7 +147,7 @@ export const parseProductData = (data: Buffer) => {
82147
return { magic, version, type, size, priceAccountKey, product }
83148
}
84149

85-
const parsePriceInfo = (data: Buffer, exponent: number) => {
150+
const parsePriceInfo = (data: Buffer, exponent: number): Price => {
86151
// aggregate price
87152
const priceComponent = readBigInt64LE(data, 0)
88153
const price = Number(priceComponent) * 10 ** exponent
@@ -106,7 +171,7 @@ const parsePriceInfo = (data: Buffer, exponent: number) => {
106171
}
107172
}
108173

109-
export const parsePriceData = (data: Buffer) => {
174+
export const parsePriceData = (data: Buffer): PriceData => {
110175
// pyth magic number
111176
const magic = data.readUInt32LE(0)
112177
// program version
@@ -154,7 +219,7 @@ export const parsePriceData = (data: Buffer) => {
154219
const aggregatePriceUpdaterAccountKey = new PublicKey(data.slice(176, 208))
155220
const aggregatePriceInfo = parsePriceInfo(data.slice(208, 240), exponent)
156221
// price components - up to 32
157-
const priceComponents = []
222+
const priceComponents: PriceComponent[] = []
158223
let offset = 240
159224
let shouldContinue = true
160225
while (offset < data.length && shouldContinue) {

0 commit comments

Comments
 (0)