Skip to content

Commit

Permalink
support new pyth oracles
Browse files Browse the repository at this point in the history
  • Loading branch information
0xodia committed Jul 15, 2024
1 parent 180046f commit 99b1866
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 6 deletions.
1 change: 1 addition & 0 deletions solend-lite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@emotion/styled": "^11.10.5",
"@next/font": "13.0.7",
"@pythnetwork/client": "^2.12.0",
"@pythnetwork/pyth-solana-receiver": "^0.8.0",
"@solana/spl-token": "^0.3.7",
"@solana/wallet-adapter-base": "^0.9.20",
"@solana/wallet-adapter-react": "^0.15.28",
Expand Down
29 changes: 28 additions & 1 deletion solend-sdk/src/core/utils/prices.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Connection, PublicKey } from "@solana/web3.js";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { parsePriceData } from "@pythnetwork/client";
import SwitchboardProgram from "@switchboard-xyz/sbv2-lite";
import { PythSolanaReceiver } from '@pythnetwork/pyth-solana-receiver';
import { getBatchMultipleAccountsInfo } from "./utils";
import { Reserve } from "../../state";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";

const SBV2_MAINNET = "SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f";

Expand All @@ -20,6 +22,7 @@ export async function fetchPrices(
);

const priceAccounts = await getBatchMultipleAccountsInfo(oracles, connection);
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet: new NodeWallet(Keypair.fromSeed(new Uint8Array(32).fill(1))) });

return parsedReserves.reduce((acc, reserve, i) => {
const pythOracleData = priceAccounts[i];
Expand All @@ -33,6 +36,29 @@ export async function fetchPrices(
| undefined;

if (pythOracleData) {
if (
pythOracleData.owner.toBase58() ===
pythSolanaReceiver.receiver.programId.toBase58()
) {
// pythData = pythSolanaReceiver.receiver.coder.accounts.decode(
// 'priceUpdateV2',
// pythOracleData.data,
// );
const priceUpdate =
pythSolanaReceiver.receiver.account.priceUpdateV2.coder.accounts.decode(
'priceUpdateV2',
pythOracleData.data,
);
const exponent = 10 ** priceUpdate.priceMessage.exponent;
const spotPrice = priceUpdate.priceMessage.price.toNumber() * exponent;
const emaPrice = priceUpdate.priceMessage.emaPrice.toNumber() * exponent;

priceData = {
spotPrice,
emaPrice,
};

} else {
const { price, previousPrice, emaPrice } = parsePriceData(
pythOracleData.data as Buffer
);
Expand All @@ -45,6 +71,7 @@ export async function fetchPrices(
};
}
}
}

// Only attempt to fetch from switchboard if not already available from pyth
if (!priceData) {
Expand Down
Loading

0 comments on commit 99b1866

Please sign in to comment.