add oracle parsing methods

This commit is contained in:
Maximilian Schneider 2022-08-18 08:02:22 +02:00
parent 6437cdb701
commit 1dcf917f7f
4 changed files with 1010 additions and 529 deletions

View File

@ -57,6 +57,7 @@
"dependencies": {
"@project-serum/serum": "^0.13.20",
"@project-serum/sol-wallet-adapter": "^0.1.4",
"@pythnetwork/client": "^2.7.3",
"@solana/spl-token": "^0.0.13",
"@solana/web3.js": "^0.90.0",
"bn.js": "^5.1.2",

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,12 @@ import {
u64,
zeros,
} from '@project-serum/serum/lib/layout';
import {
AccountType,
parseBaseData,
parsePriceData,
} from '@pythnetwork/client';
import { Aggregator } from './schema';
export const zeroKey = new PublicKey(new Uint8Array(32));
@ -383,3 +389,24 @@ export function decodeRecentEvents(buffer: Buffer, lastSeenSeqNum?: number) {
return { header, nodes };
}
const PYTH_MAGIC = Buffer.from([0xa1, 0xb2, 0xc3, 0xd4]);
export async function getOraclePrice(
connection: Connection,
oracle: PublicKey,
): Promise<number> {
const info = await connection.getAccountInfo(oracle);
if (!info || !info.data.length) {
throw new Error('account does not exist');
}
const pythBase = parseBaseData(info.data);
if (pythBase?.type == AccountType.Price) {
const price = parsePriceData(info.data);
return price.aggregate.price;
} else {
const agg = Aggregator.deserialize(info.data);
return agg.answer.median.toNumber() / Math.pow(10, agg.config.decimals);
}
}

View File

@ -558,6 +558,13 @@
bs58 "^4.0.1"
eventemitter3 "^4.0.4"
"@pythnetwork/client@^2.7.3":
version "2.7.3"
resolved "https://registry.yarnpkg.com/@pythnetwork/client/-/client-2.7.3.tgz#6075a16dc394d1734e76b04e907a680490a61536"
integrity sha512-+2k5JXxv/yUA6WMESSppJlg4T/AP+nZZfBnHmeG3RPCIJx+bargxFLCK4B2KgpQYdeTWb+2z8yRCNF7tHooCFQ==
dependencies:
buffer "^6.0.1"
"@sinonjs/commons@^1.7.0":
version "1.8.3"
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d"