import { BN } from "@project-serum/anchor"; const PRECISION = 10 ** 9; export function valueFunction({ reserves, weights, }: { reserves: number[]; weights: number[]; }): number { const sumWeight = weights.reduce( (previousValue, currentValue) => previousValue + currentValue ); return reserves.reduce((previousValue, currentValue, currenIndex) => { const nomalizeWeight = weights[currenIndex] / sumWeight; return previousValue * currentValue ** nomalizeWeight; }, 1); } export function calTotalSupplyPool({ reserves, weights, }: { reserves: number[]; weights: number[]; }): number { return valueFunction({ reserves, weights }) * reserves.length; } export function calOutGivenInSwap({ balanceOut, balanceIn, weightOut, weightIn, amountIn, }: { balanceOut: number; balanceIn: number; weightOut: number; weightIn: number; amountIn: number; }): number { return ( balanceOut * (1 - balanceIn / (balanceIn + amountIn) ** (weightIn / weightOut)) ); } const calLPFromATokenAmount = ({ totalLpSupply, amountIn, balanceIn, weightIn, }: { totalLpSupply: number; amountIn: number; balanceIn: number; weightIn: number; }) => { return totalLpSupply * ((1 + amountIn / balanceIn) ** weightIn - 1); }; export type tokenInfo = { amount: number; address: string; weight: number; balance: number; }; export const getLpFromMultiTokenDeposit = ({ totalLpSupply, depositedTokens, }: { totalLpSupply: number; depositedTokens: tokenInfo[]; }) => { const issuedLps = depositedTokens.map((token) => { return calLPFromATokenAmount({ totalLpSupply, amountIn: token.amount, balanceIn: token.balance, weightIn: token.weight, }); }); return issuedLps.reduce( (previousValue, currentValue) => previousValue + currentValue, 0 ); };