Skip to content

Commit 1f774e0

Browse files
committed
upgrade to version2 structure
1 parent 2df6459 commit 1f774e0

File tree

6 files changed

+40
-34
lines changed

6 files changed

+40
-34
lines changed

src/__tests__/Example.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { clusterApiUrl, Connection, PublicKey } from '@solana/web3.js'
22
import { parseMappingData, parsePriceData, parseProductData } from '../index'
33

44
const SOLANA_CLUSTER_URL = clusterApiUrl('devnet')
5-
const ORACLE_MAPPING_PUBLIC_KEY = 'ArppEFcsybCLE8CRtQJLQ9tLv2peGmQoKWFuiUWm4KBP'
5+
const ORACLE_MAPPING_PUBLIC_KEY = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2'
66

77
test('Mapping', (done) => {
88
jest.setTimeout(60000)

src/__tests__/Mapping.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseMappingData, Magic, Version } from '../index'
44
test('Mapping', (done) => {
55
jest.setTimeout(60000)
66
const url = clusterApiUrl('devnet')
7-
const oraclePublicKey = 'ArppEFcsybCLE8CRtQJLQ9tLv2peGmQoKWFuiUWm4KBP'
7+
const oraclePublicKey = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2'
88
const connection = new Connection(url)
99
const publicKey = new PublicKey(oraclePublicKey)
1010
connection

src/__tests__/Price.test.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,33 @@ import { Magic, parseMappingData, parsePriceData, parseProductData, Version } fr
44
test('Price', (done) => {
55
jest.setTimeout(60000)
66
const url = clusterApiUrl('devnet')
7-
const oraclePublicKey = 'ArppEFcsybCLE8CRtQJLQ9tLv2peGmQoKWFuiUWm4KBP'
7+
const oraclePublicKey = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2'
88
const connection = new Connection(url)
99
const publicKey = new PublicKey(oraclePublicKey)
1010
connection
1111
.getAccountInfo(publicKey)
1212
.then((accountInfo) => {
1313
if (accountInfo && accountInfo.data) {
1414
const mapping = parseMappingData(accountInfo.data)
15-
connection
16-
.getAccountInfo(mapping.productAccountKeys[mapping.productAccountKeys.length - 1])
17-
.then((accountInfo) => {
18-
if (accountInfo && accountInfo.data) {
19-
const product = parseProductData(accountInfo.data)
20-
connection.getAccountInfo(product.priceAccountKey).then((accountInfo) => {
21-
if (accountInfo && accountInfo.data) {
22-
const price = parsePriceData(accountInfo.data)
23-
console.log(price)
24-
expect(price.magic).toBe(Magic)
25-
expect(price.version).toBe(Version)
26-
done()
27-
} else {
28-
done('No price accountInfo')
29-
}
30-
})
31-
} else {
32-
done('No product accountInfo')
33-
}
34-
})
15+
connection.getAccountInfo(mapping.productAccountKeys[0]).then((accountInfo) => {
16+
if (accountInfo && accountInfo.data) {
17+
const product = parseProductData(accountInfo.data)
18+
connection.getAccountInfo(product.priceAccountKey).then((accountInfo) => {
19+
if (accountInfo && accountInfo.data) {
20+
const price = parsePriceData(accountInfo.data)
21+
console.log(product.product.symbol)
22+
console.log(price)
23+
expect(price.magic).toBe(Magic)
24+
expect(price.version).toBe(Version)
25+
done()
26+
} else {
27+
done('No price accountInfo')
28+
}
29+
})
30+
} else {
31+
done('No product accountInfo')
32+
}
33+
})
3534
} else {
3635
done('No mapping accountInfo')
3736
}

src/__tests__/Product.ETH.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Magic, parseProductData, Version } from '../index'
44
test('Product', (done) => {
55
jest.setTimeout(60000)
66
const url = clusterApiUrl('devnet')
7-
const ethProductKey = 'HtwD2SutRJVatzAsnvRRjSujQFNdQodzF1FKo54NEzrs'
7+
const ethProductKey = '2ciUuGZiee5macAMeQ7bHGTJtwcYTgnt6jdmQnnKZrfu'
88
const connection = new Connection(url)
99
const publicKey = new PublicKey(ethProductKey)
1010
connection

src/__tests__/Product.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseMappingData, parseProductData, Magic, Version } from '../index'
44
test('Product', (done) => {
55
jest.setTimeout(60000)
66
const url = clusterApiUrl('devnet')
7-
const oraclePublicKey = 'ArppEFcsybCLE8CRtQJLQ9tLv2peGmQoKWFuiUWm4KBP'
7+
const oraclePublicKey = 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2'
88
const connection = new Connection(url)
99
const publicKey = new PublicKey(oraclePublicKey)
1010
connection

src/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { Buffer } from 'buffer'
33
import { readBigInt64LE, readBigUInt64LE } from './readBig'
44

55
export const Magic = 0xa1b2c3d4
6-
export const Version1 = 1
7-
export const Version = Version1
6+
export const Version2 = 2
7+
export const Version = Version2
88
export const PriceStatus = ['Unknown', 'Trading', 'Halted', 'Auction']
99
export const CorpAction = ['NoCorpAct']
10-
export const PriceType = ['Unknown', 'Price', 'TWAP', 'Volatility']
10+
export const PriceType = ['Unknown', 'Price']
11+
export const DeriveType = ['Unknown', 'TWAP', 'Volatility']
1112

1213
const empty32Buffer = Buffer.alloc(32)
1314
const PKorNull = (data: Buffer) => (data.equals(empty32Buffer) ? null : new PublicKey(data))
@@ -126,16 +127,21 @@ export const parsePriceData = (data: Buffer) => {
126127
const currentSlot = readBigUInt64LE(data, 32)
127128
// valid on-chain slot of aggregate price
128129
const validSlot = readBigUInt64LE(data, 40)
130+
// calculated values derived from agg. price - up to 8
131+
const derived = []
132+
for (let d = 0; d < 8; d++) {
133+
derived.push(readBigInt64LE(data, 48 + d * 8))
134+
}
129135
// product id / reference account
130-
const productAccountKey = new PublicKey(data.slice(48, 80))
136+
const productAccountKey = new PublicKey(data.slice(112, 144))
131137
// next price account in list
132-
const nextPriceAccountKey = PKorNull(data.slice(80, 112))
138+
const nextPriceAccountKey = PKorNull(data.slice(144, 176))
133139
// aggregate price updater
134-
const aggregatePriceUpdaterAccountKey = new PublicKey(data.slice(112, 144))
135-
const aggregatePriceInfo = parsePriceInfo(data.slice(144, 176), exponent)
136-
// price components - up to 16
140+
const aggregatePriceUpdaterAccountKey = new PublicKey(data.slice(176, 208))
141+
const aggregatePriceInfo = parsePriceInfo(data.slice(208, 240), exponent)
142+
// price components - up to 32
137143
const priceComponents = []
138-
let offset = 176
144+
let offset = 240
139145
let shouldContinue = true
140146
while (offset < data.length && shouldContinue) {
141147
const publisher = PKorNull(data.slice(offset, offset + 32))
@@ -160,6 +166,7 @@ export const parsePriceData = (data: Buffer) => {
160166
numComponentPrices,
161167
currentSlot,
162168
validSlot,
169+
derived,
163170
productAccountKey,
164171
nextPriceAccountKey,
165172
aggregatePriceUpdaterAccountKey,

0 commit comments

Comments
 (0)