import { LAMPORTS_PER_SOL, PublicKey, TransactionResponse } from '@solana/web3.js'; import { SOLCIAL_MINT, SOLCIAL_SWAP } from '../config'; import { detectSwapOrSwapCreation, programOperationTypes } from './swap'; export interface PoolTxPriceInfo { priceLamport: bigint; price: number; userTokenBalance: string; SLCLTokenBalance: string; tokenPoolAccount: PublicKey; swapAddress: string; } /** * @function getPoolTxPriceInfo * @description Get the price of a pool transaction. * @param tx * @param solcialSwapId * @param solcialMint */ export async function getPriceForPoolTx( tx: TransactionResponse, solcialSwapId: PublicKey = SOLCIAL_SWAP, solcialMint = SOLCIAL_MINT, ): Promise { const { operationType, instructions } = detectSwapOrSwapCreation(tx, solcialSwapId); if (operationType === programOperationTypes.UNKNOWN) { throw new Error('This is not a processable swap transaction'); } const { accountKeys } = tx.transaction.message; const { postTokenBalances, preTokenBalances } = tx.meta; const { instructions: originalInstruction } = tx.transaction.message; const slclMint = solcialMint; let mint_address, tokenPoolAccount, swap_address, price; if (operationType === programOperationTypes.SWAP_CREATION) { // Get the mint from the second instruction. mint_address = accountKeys[originalInstruction[1].accounts[3]] as unknown as PublicKey | string; // Get the tokenPoolAccount tokenPoolAccount = accountKeys[instructions.accounts[1]] as unknown as PublicKey | string; // Get the swap. swap_address = accountKeys[instructions.accounts[0]] as unknown as PublicKey | string; // Convert all to strings // TODO: remove this when stable on all envs mint_address = typeof mint_address === 'string' ? mint_address : mint_address.toBase58(); tokenPoolAccount = typeof tokenPoolAccount === 'string' ? tokenPoolAccount : tokenPoolAccount.toBase58(); swap_address = typeof swap_address === 'string' ? swap_address : swap_address.toBase58(); } else { // Get the tokenPoolAccount tokenPoolAccount = accountKeys[instructions.accounts[1]] as unknown as PublicKey | string; // Ensure the tokenPoolAccount is a string. tokenPoolAccount = typeof tokenPoolAccount === 'string' ? tokenPoolAccount : tokenPoolAccount.toBase58(); // Deduce the mint address from the postTokenBalances mint_address = postTokenBalances.find( (balance) => balance.owner === tokenPoolAccount && balance.mint !== slclMint.toBase58(), )?.mint; if (!mint_address) { throw new Error(`Could not find mint address for tokenPoolAccount ${tokenPoolAccount}`); } // Get the swap. swap_address = accountKeys[instructions.accounts[0]] as unknown as string | PublicKey; // Ensure the swap_address is a string. swap_address = typeof swap_address === 'string' ? swap_address : swap_address.toBase58(); } const userTokenBalancePost = postTokenBalances.filter( (balance) => balance.mint === mint_address && balance.owner === tokenPoolAccount, )[0].uiTokenAmount.amount; const SLCLTokenBalancePost = postTokenBalances.filter( (balance) => balance.mint === slclMint.toBase58() && balance.owner === tokenPoolAccount, )[0]?.uiTokenAmount?.amount; if (!SLCLTokenBalancePost) { throw new Error( `Could not find SLCL (${slclMint} balance for tokenPoolAccount ${tokenPoolAccount} in transaction ${tx.transaction.signatures[0]}`, ); } // If we are in a swap creation, use the offset method for getting the price. Otherwise, use the // preTokenBalances & postTokenBalances. if (operationType === programOperationTypes.SWAP_CREATION) { price = await getUserTokenPriceInSLCLFromOffsetAndCurve( swap_address, userTokenBalancePost, SLCLTokenBalancePost, ); } else { const userTokenBalancePre = preTokenBalances.filter( (balance) => balance.mint === mint_address && balance.owner === tokenPoolAccount, )[0].uiTokenAmount.amount; const SLCLTokenBalancePre = preTokenBalances.filter( (balance) => balance.mint === slclMint.toBase58() && balance.owner === tokenPoolAccount, )[0]?.uiTokenAmount?.amount; price = getUserTokenPriceInSLCLFromVolume( userTokenBalancePre, SLCLTokenBalancePre, userTokenBalancePost, SLCLTokenBalancePost, ); } // Convert the price to a float. const priceInFloat = parseFloat(price.toString()) / LAMPORTS_PER_SOL; return { priceLamport: price, price: priceInFloat, userTokenBalance: userTokenBalancePost, SLCLTokenBalance: SLCLTokenBalancePost, tokenPoolAccount, swapAddress: swap_address, }; } /** * Get the price of the user token in lamports SLCL from the offset and curve. * * @param swapAddress * @param userTokenTotal * @param SLCLTokenTotal * * @returns The user token price in SLCL lamports */ export async function getUserTokenPriceInSLCLFromOffsetAndCurve( swapAddress: string | PublicKey, userTokenTotal, SLCLTokenTotal, ): Promise { if (typeof swapAddress === 'string') { swapAddress = new PublicKey(swapAddress); } const { offset, invariant } = await this.getOffsetAndInvariant( swapAddress, userTokenTotal, SLCLTokenTotal, ); // Use BN to avoid floating point errors. const userTokenTotalBI = BigInt(userTokenTotal); const SLCLTokenTotalBI = BigInt(SLCLTokenTotal); const offsetBI = BigInt(offset.toString()); const invariantBI = BigInt(invariant.toString()); const LAMPORTS_PER_SOL_BI = BigInt(LAMPORTS_PER_SOL); const value = userTokenTotalBI - invariantBI / (offsetBI + LAMPORTS_PER_SOL_BI + SLCLTokenTotalBI) || BigInt(0); // Invert big int. return (LAMPORTS_PER_SOL_BI * LAMPORTS_PER_SOL_BI) / value; } /** * Get the price of the user token in lamports SLCL from the volume. * * @param userTokenBalancePre * @param SLCLTokenBalancePre * @param userTokenBalancePost * @param SLCLTokenBalancePost */ export function getUserTokenPriceInSLCLFromVolume( userTokenBalancePre, SLCLTokenBalancePre, userTokenBalancePost, SLCLTokenBalancePost, ): bigint { // Convert all to BigInt. const userTokenBalancePreBI = BigInt(userTokenBalancePre); const SLCLTokenBalancePreBI = BigInt(SLCLTokenBalancePre); const userTokenBalancePostBI = BigInt(userTokenBalancePost); const SLCLTokenBalancePostBI = BigInt(SLCLTokenBalancePost); // Check if we are selling or buying. const isBuying = userTokenBalancePreBI - userTokenBalancePostBI < 0; // Now compute the price. const userTokenVolume = isBuying ? userTokenBalancePostBI - userTokenBalancePreBI : userTokenBalancePreBI - userTokenBalancePostBI; const SLCLTokenVolume = isBuying ? SLCLTokenBalancePreBI - SLCLTokenBalancePostBI : SLCLTokenBalancePostBI - SLCLTokenBalancePreBI; return ( (((userTokenVolume * BigInt(LAMPORTS_PER_SOL)) / SLCLTokenVolume) * BigInt(LAMPORTS_PER_SOL)) / BigInt(LAMPORTS_PER_SOL) ); }