@paxoslabs/earn-sdk
Version:
Paxos Labs Earn SDK
1 lines • 5.86 kB
Source Map (JSON)
{"version":3,"sources":["../src/utils/try-catch.ts","../src/utils/bigint.ts"],"names":[],"mappings":";;;AAcA,eAAsB,SACpB,OAAA,EAC2B;AAC3B,EAAA,IAAI;AACF,IAAA,MAAM,OAAO,MAAM,OAAA;AACnB,IAAA,OAAO,EAAE,IAAA,EAAM,KAAA,EAAO,KAAA,CAAA,EAAW,SAAS,IAAA,EAAK;AAAA,EACjD,SAAS,KAAA,EAAO;AACd,IAAA,OAAO,EAAE,IAAA,EAAM,MAAA,EAAW,KAAA,EAAmB,SAAS,KAAA,EAAM;AAAA,EAC9D;AACF;ACbO,IAAM,GAAA,GAAM;AAAA,EACjB,MAAA,EAAQ,OAAO,IAAI,CAAA;AAAA,EACnB,MAAA,EAAQ;AACV;AAOO,IAAM,GAAA,GAAM;AAAA,EACjB,MAAA,EAAQ,OAAO,IAAI,CAAA;AAAA,EACnB,MAAA,EAAQ;AACV;AAgBO,SAAS,sBAAA,CACd,KAAA,EACA,IAAA,GAII,EAAC,EACG;AACR,EAAA,MAAM;AAAA,IACJ,QAAA,GAAW,EAAA;AAAA,IACX,qBAAA,GAAwB,CAAA;AAAA,IACxB,qBAAA,GAAwB;AAAA,GAC1B,GAAI,IAAA;AACJ,EAAA,MAAM,cAAc,MAAA,CAAO,UAAA,CAAW,WAAA,CAAY,KAAA,EAAO,QAAQ,CAAC,CAAA;AAClE,EAAA,OAAO,IAAI,IAAA,CAAK,YAAA,CAAa,OAAA,EAAS;AAAA,IACpC,qBAAA;AAAA,IACA;AAAA,GACD,CAAA,CAAE,MAAA,CAAO,WAAW,CAAA;AACvB;AAaO,SAAS,6BAAA,CACd,aAAA,EACA,WAAA,EACA,aAAA,EACQ;AAER,EAAA,MAAM,SAAA,GAAY,MAAA,CAAO,EAAE,CAAA,IAAK,OAAO,aAAa,CAAA;AAIpD,EAAA,MAAM,YAAA,GAAgB,gBAAgB,SAAA,GAAa,WAAA;AAEnD,EAAA,OAAO,YAAA;AACT;AAYO,SAAS,oBAAA,CACd,aACA,QAAA,EACQ;AAGR,EAAA,MAAM,WAAA,GAAc,GAAA;AACpB,EAAA,MAAM,qBAAA,GAAwB,IAAA,CAAK,KAAA,CAAM,QAAA,GAAW,WAAW,CAAA;AAC/D,EAAA,MAAM,mBAAmB,WAAA,GAAc,qBAAA;AAIvC,EAAA,MAAM,cACH,WAAA,GAAc,MAAA,CAAO,gBAAgB,CAAA,GAAK,OAAO,WAAW,CAAA;AAE/D,EAAA,OAAO,WAAA;AACT","file":"chunk-MHZTLW64.mjs","sourcesContent":["type Success<T> = {\n success: true;\n data: T;\n error?: never;\n};\n\ntype Failure<E> = {\n success: false;\n data?: never;\n error: E;\n};\n\ntype SafeResult<T, E = Error> = Success<T> | Failure<E>;\n\nexport async function tryCatch<T, E = Error>(\n promise: Promise<T>\n): Promise<SafeResult<T, E>> {\n try {\n const data = await promise;\n return { data, error: undefined, success: true };\n } catch (error) {\n return { data: undefined, error: error as E, success: false };\n }\n}\n","/**\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"]}