UNPKG

@ajna-finance/sdk

Version:

A typescript SDK that can be used to create Dapps in Ajna ecosystem.

65 lines 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wsqrt = exports.wadToIntRoundingDown = exports.max = exports.min = exports.wdiv = exports.wmul = exports.toWad = exports.fromWad = void 0; const ethers_1 = require("ethers"); const common_1 = require("../constants/common"); // converts from WAD precision to human-readable string const fromWad = (value) => { return ethers_1.ethers.utils.formatEther(String(value)); }; exports.fromWad = fromWad; // converts from human-readable value to WAD precision const toWad = (value) => { return ethers_1.ethers.utils.parseEther(String(value)); }; exports.toWad = toWad; // calculates product of two WADs const wmul = (lhs, rhs) => { // (x * y + WAD / 2) / WAD; return lhs.mul(rhs).add(common_1.ONE_HALF_WAD).div(ethers_1.constants.WeiPerEther); }; exports.wmul = wmul; // calculates quotient of two WADs const wdiv = (lhs, rhs) => { // return (x * WAD + y / 2) / y; return lhs .mul(ethers_1.constants.WeiPerEther) .add(rhs.div(ethers_1.BigNumber.from(2))) .div(rhs); }; exports.wdiv = wdiv; // returns the minimum of two WADs const min = (lhs, rhs) => (lhs.lte(rhs) ? lhs : rhs); exports.min = min; // returns the maximum of two WADs const max = (lhs, rhs) => (lhs.gte(rhs) ? lhs : rhs); exports.max = max; const wadToIntRoundingDown = (wad) => { return (0, exports.wdiv)(wad, common_1.ONE_WAD).div(common_1.ONE_WAD).toNumber(); }; exports.wadToIntRoundingDown = wadToIntRoundingDown; /** * Calculates the square root of a WAD based on the Heron's method algorithm, described as a solution in * an issue by a member of ether.js library https://github.com/ethers-io/ethers.js/issues/1182#issuecomment-744142921 * @param {BigNumberish} value WAD to calculate the square root of * @returns {BigNumber} Square root * * @example * * // equals to WAD representation of 2 * wsqrt(toWad('4')) * // equals to WAD representation of 447.9979129415671731 * wsqrt(toWad('200702.13')) */ const wsqrt = (value) => { const x = (0, exports.toWad)(value); let z = x.add(ethers_1.constants.One).div(ethers_1.constants.Two); let y = x; while (z.sub(y).isNegative()) { y = z; z = x.div(z).add(z).div(ethers_1.constants.Two); } return y; }; exports.wsqrt = wsqrt; //# sourceMappingURL=numeric.js.map