UNPKG

@molecularlabs/nucleus-frontend

Version:
1 lines 3.39 kB
{"version":3,"sources":["../../../src/vaults/withdraw/utils.ts"],"sourcesContent":["import { calculateDeadline } from \"../../utils/time\";\nimport type { UserRequest } from \"./withdraw\";\n\n/**\n * Prepares a withdrawal request with the specified parameters\n * @param redeemAmount The amount of tokens to be redeemed\n * @param atomicPrice The current atomic price for the withdrawal\n * @param deadline Optional timestamp for when the request expires\n * @returns UserRequest object with withdrawal parameters\n */\nconst prepareUserRequest = (\n offerAmount: bigint,\n atomicPrice: bigint,\n deadline: number\n): UserRequest => {\n const deadlineTimeStamp = calculateDeadline(deadline);\n return {\n deadline: deadlineTimeStamp,\n atomicPrice: atomicPrice,\n offerAmount: offerAmount,\n inSolve: false,\n };\n};\n\n/**\n * Calculates the final redeem amount accounting for exchange rate and slippage\n * @param amount The initial amount to be redeemed\n * @param exchangeRate The current exchange rate between tokens\n * @param slippage The slippage tolerance as a decimal (e.g., 0.01 for 1%)\n * @returns The final redeem amount after applying exchange rate and slippage\n */\n// ! Deprecated - marked for removal\n// const calculateWantAssetReceiveAmount = (\n// amount: bigint,\n// amountDecimals: number,\n// exchangeRate: bigint,\n// exchangeRateDecimals: number,\n// slippage: number\n// ): bigint => {\n// if (exchangeRate === BigInt(0)) {\n// return BigInt(0);\n// }\n// if (amountDecimals === exchangeRateDecimals) {\n// // all good\n// }\n\n// if (amountDecimals > exchangeRateDecimals) {\n// // normalize to larger decimals\n// }\n// // Convert amount to the target token using exchange rate\n// // Multiply by WAD (10^18) for precision and divide by exchange rate\n// const baseAmount = (amount * WAD.bigint) / exchangeRate;\n\n// // Convert slippage percentage to WAD format for calculation\n// const slippageAsBigInt = BigInt((slippage * WAD.number).toString());\n\n// // Calculate the amount to be subtracted due to slippage\n// const slippageAmount = (baseAmount * slippageAsBigInt) / WAD.bigint;\n\n// // Return the final amount after subtracting slippage\n// return baseAmount - slippageAmount;\n// };\n\nconst calculateAtomicPrice = (\n rateInQuote: bigint, // e.g., 1200000 (1.2 USDC) or 120000000 (1.2 WBTC)\n wantAssetDecimals: number, // 6 for USDC, 8 for WBTC - the \"want\" asset decimals\n slippage: number // In bps (e.g., 20 for 0.2%)\n): bigint => {\n // Step 1: Create precision multiplier for want asset\n const wantPrecision = BigInt(10 ** wantAssetDecimals);\n\n // Step 2: Convert bps to decimal for calculation (bps / 10000)\n const slippageAsBigInt = (BigInt(slippage) * wantPrecision) / BigInt(10000);\n\n // Step 3: Apply formula: atomicPrice = rateInQuote * (1 - slippage)\n return (rateInQuote * (wantPrecision - slippageAsBigInt)) / wantPrecision;\n};\n\nexport { calculateAtomicPrice, prepareUserRequest };\n"],"mappings":"AAAA,SAAS,yBAAyB;AAUlC,MAAM,qBAAqB,CACzB,aACA,aACA,aACgB;AAChB,QAAM,oBAAoB,kBAAkB,QAAQ;AACpD,SAAO;AAAA,IACL,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA,SAAS;AAAA,EACX;AACF;AAyCA,MAAM,uBAAuB,CAC3B,aACA,mBACA,aACW;AAEX,QAAM,gBAAgB,OAAO,MAAM,iBAAiB;AAGpD,QAAM,mBAAoB,OAAO,QAAQ,IAAI,gBAAiB,OAAO,GAAK;AAG1E,SAAQ,eAAe,gBAAgB,oBAAqB;AAC9D;","names":[]}