Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 8092

Calculation of Tocken price (for example SOL-USDC)

$
0
0

Fetched data from a raydium pool.Which values do I need for the calculation of the "fair price" or rather current price?

Outcome should looks something like this : 1 SOL = 169.3 USDC(If the format or the way the question is asked please give me feedback!)Updated this question!import { Connection, PublicKey } from "@solana/web3.js";import { LIQUIDITY_STATE_LAYOUT_V4 } from "@raydium-io/raydium-sdk";const SOL_USDC_POOL_ID = "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"; // Your pool IDasync function getTokenBalance(connection: Connection, vault: PublicKey, decimals: number): Promise<number> {  const balance = await connection.getTokenAccountBalance(vault);  return parseFloat(balance.value.amount) / Math.pow(10, decimals); // Adjust the balance for the token's decimals}async function parsePoolInfo() {  const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");  const info = await connection.getAccountInfo(new PublicKey(SOL_USDC_POOL_ID));  if (!info) return;  // Assuming the structure was successfully decoded  const poolState = LIQUIDITY_STATE_LAYOUT_V4.decode(info.data);  // Directly use the specific decimals for SOL and USDC  const baseTokenDecimals = 9; // Decimals for SOL  const quoteTokenDecimals = 6; // Decimals for USDC  const baseTokenBalance = await getTokenBalance(connection, poolState.baseVault, baseTokenDecimals);  const quoteTokenBalance = await getTokenBalance(connection, poolState.quoteVault, quoteTokenDecimals);  // The calculation remains the same, but the clarification is that these decimals are now hardcoded  const priceOfBaseInQuote = quoteTokenBalance / baseTokenBalance;  console.log("Decoded Pool Data:", poolState);  console.log(`Price of 1 SOL in USDC: ${priceOfBaseInQuote.toFixed(4)}`); // Adjusted for decimals}parsePoolInfo();

Viewing all articles
Browse latest Browse all 8092

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>