Created
June 25, 2024 05:36
-
-
Save codewithgun/d3f7e40796658fc56ce03d8cc2e8d110 to your computer and use it in GitHub Desktop.
Get meteora pool LP price
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AmmImpl, { MAINNET_POOL } from "@mercurial-finance/dynamic-amm-sdk"; | |
import { Connection, Keypair, PublicKey } from "@solana/web3.js"; | |
import { Wallet, AnchorProvider } from "@project-serum/anchor"; | |
import { TokenListProvider } from "@solana/spl-token-registry"; | |
import Decimal from "decimal.js"; | |
interface JupPriceResponse { | |
data: { | |
[address: string]: { | |
id: String; | |
mintSymbol: String; | |
vsToken: String; | |
vsTokenSymbol: String; | |
price: number; | |
}; | |
}; | |
} | |
async function getTokenPrice(tokenMints: PublicKey[]): Promise<number[]> { | |
const url = `https://price.jup.ag/v6/price?ids=${tokenMints.join(",")}`; | |
const response: JupPriceResponse = await fetch(url).then((res) => res.json()); | |
const prices = tokenMints.map((mint) => { | |
return response.data[mint.toBase58()].price; | |
}); | |
return prices; | |
} | |
const main = async () => { | |
const SOL_MSOL_POOL = new PublicKey( | |
"HcjZvfeSNJbNkfLD4eEcRBr96AD3w1GpmMppaeRZf7ur" | |
); | |
// Connection, Wallet, and AnchorProvider to interact with the network | |
const connection = new Connection("https://api.mainnet-beta.solana.com"); | |
const mockWallet = new Wallet(new Keypair()); | |
const tlp = await new TokenListProvider().resolve(); | |
const tokenList = tlp.filterByClusterSlug("mainnet-beta").getList(); | |
// Alternatively, to use Solana Wallet Adapter | |
const MSOL = tokenList.find( | |
(token) => token.address === "mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So" | |
)!; | |
const SOL = tokenList.find( | |
(token) => token.address === "So11111111111111111111111111111111111111112" | |
)!; | |
const lstPool = await AmmImpl.create(connection, SOL_MSOL_POOL, SOL, MSOL); | |
// You may refresh the pool | |
// lstPool.updateState(); | |
const lpMintMultiplier = await connection | |
.getTokenSupply(lstPool.poolState.lpMint) | |
.then((v) => new Decimal(10 ** v.value.decimals)); | |
const tokenAMultiplier = new Decimal(10 ** lstPool.tokenA.decimals); | |
const tokenBMultiplier = new Decimal(10 ** lstPool.tokenB.decimals); | |
const tokenAReserveAmount = new Decimal( | |
lstPool.poolInfo.tokenAAmount.toString() | |
).div(tokenAMultiplier); | |
const tokenBReserveAmount = new Decimal( | |
lstPool.poolInfo.tokenBAmount.toString() | |
).div(tokenBMultiplier); | |
console.log( | |
"Token reserve A amount", | |
tokenAReserveAmount, | |
"Token reserve B amount", | |
tokenBReserveAmount | |
); | |
const [tokenAPrice, tokenBPrice] = await getTokenPrice([ | |
lstPool.poolState.tokenAMint, | |
lstPool.poolState.tokenBMint, | |
]).then((prices) => prices.map((p) => new Decimal(p))); | |
const totalValueLocked = tokenAReserveAmount | |
.mul(tokenAPrice) | |
.add(tokenBReserveAmount.mul(tokenBPrice)); | |
const lpSupply = new Decimal(lstPool.poolState.lpSupply.toString()).div( | |
lpMintMultiplier | |
); | |
const lpPrice = totalValueLocked.div(lpSupply); | |
console.log("LP price", lpPrice); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment