Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save codewithgun/d3f7e40796658fc56ce03d8cc2e8d110 to your computer and use it in GitHub Desktop.

Select an option

Save codewithgun/d3f7e40796658fc56ce03d8cc2e8d110 to your computer and use it in GitHub Desktop.

Revisions

  1. codewithgun renamed this gist Jun 25, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. codewithgun created this gist Jun 25, 2024.
    91 changes: 91 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    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();