Skip to content

Commit

Permalink
add fetch-price.js in src/raydium/
Browse files Browse the repository at this point in the history
  • Loading branch information
outsmartchad committed Aug 22, 2024
1 parent 459400c commit 4d783ff
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/raydium/fetch-price.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const { initSdk } = require("./raydium_config");
const {fetchAMMPoolId} = require("./Pool/fetch_pool");
const Decimal = require("decimal.js");
const {wsol} = require("./constants");
let sdkCache = { sdk: null, expiry: 0 };
async function getCurrentPriceInSOL(
tokenAddress
) {
try {
// Check if poolId is already set
let raydium = null;
if (sdkCache.sdk) {
raydium = sdkCache.sdk;
} else {
raydium = await initSdk();
sdkCache.sdk = raydium;
}
let poolId = null;
poolId = await fetchAMMPoolId(tokenAddress);

const res = await raydium.liquidity.getRpcPoolInfos([poolId]);
const poolInfo = res[poolId];

const baseMint = poolInfo.baseMint.toString();
const quoteMint = poolInfo.quoteMint.toString();
const baseDecimals = new Decimal(poolInfo.baseDecimal.toString());
const quoteDecimals = new Decimal(poolInfo.quoteDecimal.toString());
const solMintAddress = wsol;
//const solPrice = await getCurrentSolPrice();
let solReserve = null;
let tokenReserve = null;
let priceInSOL;

if (baseMint === solMintAddress) {
// baseMint is SOL, (this is pump token)
solReserve = new Decimal(poolInfo.baseReserve.toString()).div(
new Decimal(10).pow(baseDecimals)
);
tokenReserve = new Decimal(poolInfo.quoteReserve.toString()).div(
new Decimal(10).pow(quoteDecimals)
);
priceInSOL = solReserve.div(tokenReserve);
} else {
// Neither baseMint nor quoteMint is SOL, use the pool price directly
solReserve = new Decimal(poolInfo.quoteReserve.toString()).div(
new Decimal(10).pow(quoteDecimals)
);
tokenReserve = new Decimal(poolInfo.baseReserve.toString()).div(
new Decimal(10).pow(baseDecimals)
);
priceInSOL = poolInfo.poolPrice;
}
//console.log(`Current price of ${tokenAddress} in SOL: ${priceInSOL}`);
return priceInSOL;
} catch (e) {
console.log(`Error when getting current price of ${tokenAddress} `, e);
}
}
async function getCurrentSolPrice(){
try{
let raydium = null
if(sdkCache.sdk){
raydium = sdkCache.sdk;
}
else {
raydium = await initSdk();
sdkCache.sdk = raydium;
}
const sol_usdc = "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"
let res = await raydium.liquidity.getRpcPoolInfos([sol_usdc]);
while(res === undefined || res === null){
res = await raydium.liquidity.getRpcPoolInfos([sol_usdc]);
}
const poolInfo = res[sol_usdc];
return poolInfo.poolPrice;
}catch(e){
console.log("Error getting current SOL price: ", e)

}

}

async function getCurrentPriceInUSD(tokenAddress){
const priceInSOL = await getCurrentPriceInSOL(tokenAddress);
const solPrice = await getCurrentSolPrice();
return priceInSOL * solPrice;
}


async function main(){
console.log(await getCurrentPriceInSOL("3XTp12PmKMHxB6YkejaGPUjMGBLKRGgzHWgJuVTsBCoP"));
console.log(await getCurrentSolPrice());
console.log(await getCurrentPriceInUSD("3XTp12PmKMHxB6YkejaGPUjMGBLKRGgzHWgJuVTsBCoP"));
}
//main();

module.exports = {getCurrentPriceInSOL, getCurrentSolPrice, getCurrentPriceInUSD};

0 comments on commit 4d783ff

Please sign in to comment.