UNPKG

@molecularlabs/nucleus-frontend

Version:
1 lines 4.73 kB
{"version":3,"sources":["../../src/utils/bigint.ts"],"sourcesContent":["/**\n * @fileoverview Utilities for working with bigint values and fixed-point math\n */\nimport { formatUnits } from \"viem\";\n\n/**\n * RAY precision unit (10^27)\n * Used for high precision fixed-point calculations\n * @type {{bigint: bigint, number: number}}\n */\nexport const RAY = {\n bigint: BigInt(1e27),\n number: 1e27,\n};\n\n/**\n * WAD precision unit (10^18)\n * Common precision unit for Ethereum tokens (matches ETH's 18 decimals)\n * @type {{bigint: bigint, number: number}}\n */\nexport const WAD = {\n bigint: BigInt(1e18),\n number: 1e18,\n};\n\n/**\n * Converts a bigint value to a formatted number string with specified decimal precision\n *\n * @param {bigint} value - The bigint value to convert\n * @param {Object} opts - Formatting options\n * @param {number} [opts.decimals=18] - Number of decimals to use when converting from bigint\n * @param {number} [opts.minimumFractionDigits=0] - Minimum number of fraction digits to display\n * @param {number} [opts.maximumFractionDigits=3] - Maximum number of fraction digits to display\n * @returns {string} The formatted number as a string\n *\n * @example\n * // Returns \"123.456\"\n * bigIntToNumberAsString(BigInt(\"123456000000000000000\"), { decimals: 18 })\n */\nexport function bigIntToNumberAsString(\n value: bigint,\n opts: {\n decimals?: number;\n minimumFractionDigits?: number;\n maximumFractionDigits?: number;\n } = {}\n): string {\n const {\n decimals = 18,\n minimumFractionDigits = 0,\n maximumFractionDigits = 3,\n } = opts;\n const numberValue = Number.parseFloat(formatUnits(value, decimals));\n return new Intl.NumberFormat(\"en-US\", {\n minimumFractionDigits: minimumFractionDigits,\n maximumFractionDigits: maximumFractionDigits,\n }).format(numberValue);\n}\n\n/**\n * Calculates the expected amount of shares to be minted based on deposit amount and rate\n *\n * The calculation follows the formula:\n * sharesMinted = depositAmount * ONE_SHARE / rateInQuote\n *\n * @param {bigint} depositAmount - The amount to deposit in quote asset decimals\n * @param {bigint} rateInQuote - The rate in quote representing \"quote asset per share\"\n * @param {number} shareDecimals - The decimal precision of the BoringVault shares (ONE_SHARE)\n * @returns {bigint} The expected amount of shares to be minted\n */\nexport function calculateExpectedSharesMinted(\n depositAmount: bigint,\n rateInQuote: bigint,\n shareDecimals: number\n): bigint {\n // Calculate ONE_SHARE based on the vault's share decimals\n const ONE_SHARE = BigInt(10) ** BigInt(shareDecimals);\n\n // Calculate shares minted using the formula:\n // sharesMinted = depositAmount * ONE_SHARE / rateInQuote\n const sharesMinted = (depositAmount * ONE_SHARE) / rateInQuote;\n\n return sharesMinted;\n}\n\n/**\n * Calculates the atomic price with slippage applied\n * The calculation follows the formula:\n * atomicPrice = rateInQuote * (1 - slippage)\n *\n * @param {bigint} rateInQuote - The rate in quote representing \"quote asset per share\"\n * @param {number} slippage - The maximum acceptable slippage as a decimal (e.g., 0.01 for 1%)\n * @param {number} quoteDecimals - The decimal precision of the quote asset\n * @returns {bigint} The atomic price with slippage applied\n */\nexport function calculateAtomicPrice(\n rateInQuote: bigint,\n slippage: number\n): bigint {\n // Convert slippage to basis points (1% = 100 basis points = 0.01)\n // Multiply by 10000 to get precision and subtract from 10000 to get the percentage to keep\n const basisPoints = 10000;\n const slippageInBasisPoints = Math.floor(slippage * basisPoints);\n const percentageToKeep = basisPoints - slippageInBasisPoints;\n\n // Calculate atomic price using fixed point arithmetic:\n // atomicPrice = rateInQuote * percentageToKeep / basisPoints\n const atomicPrice =\n (rateInQuote * BigInt(percentageToKeep)) / BigInt(basisPoints);\n\n return atomicPrice;\n}\n"],"mappings":"AAGA,SAAS,mBAAmB;AAOrB,MAAM,MAAM;AAAA,EACjB,QAAQ,OAAO,IAAI;AAAA,EACnB,QAAQ;AACV;AAOO,MAAM,MAAM;AAAA,EACjB,QAAQ,OAAO,IAAI;AAAA,EACnB,QAAQ;AACV;AAgBO,SAAS,uBACd,OACA,OAII,CAAC,GACG;AACR,QAAM;AAAA,IACJ,WAAW;AAAA,IACX,wBAAwB;AAAA,IACxB,wBAAwB;AAAA,EAC1B,IAAI;AACJ,QAAM,cAAc,OAAO,WAAW,YAAY,OAAO,QAAQ,CAAC;AAClE,SAAO,IAAI,KAAK,aAAa,SAAS;AAAA,IACpC;AAAA,IACA;AAAA,EACF,CAAC,EAAE,OAAO,WAAW;AACvB;AAaO,SAAS,8BACd,eACA,aACA,eACQ;AAER,QAAM,YAAY,OAAO,EAAE,KAAK,OAAO,aAAa;AAIpD,QAAM,eAAgB,gBAAgB,YAAa;AAEnD,SAAO;AACT;AAYO,SAAS,qBACd,aACA,UACQ;AAGR,QAAM,cAAc;AACpB,QAAM,wBAAwB,KAAK,MAAM,WAAW,WAAW;AAC/D,QAAM,mBAAmB,cAAc;AAIvC,QAAM,cACH,cAAc,OAAO,gBAAgB,IAAK,OAAO,WAAW;AAE/D,SAAO;AACT;","names":[]}