@equilab/marginly-sdk
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -86,1 +86,13 @@ /** @module MarginlyPoolMath Marginly underlying math ts implementation */ | ||
export declare function convertPriceHumanToX96(price: BigNumber, baseDecimal: BigNumber, quoteDecimal: BigNumber): BigNumber; | ||
export declare const isValidNumber: (s: string) => boolean; | ||
export declare function extractFractionAndWhole(s: string): { | ||
whole: string | undefined; | ||
fraction?: string; | ||
}; | ||
/** | ||
* @param priceX price in human readable form to convert | ||
* @param baseDecimal decimals of base token | ||
* @param quoteDecimal decimals of quote token | ||
* @returns X96 representation of price | ||
*/ | ||
export declare function convertPriceStringToX96(price: string, baseDecimal: BigNumber, quoteDecimal: BigNumber): BigNumber; |
"use strict"; | ||
/** @module MarginlyPoolMath Marginly underlying math ts implementation */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertPriceHumanToX96 = exports.convertPriceX96ToHuman = exports.calcShortLiquidationPriceX96 = exports.calcLongLiquidationPriceX96 = exports.calcShortLeverage = exports.calcLongLeverage = exports.calcRealQuoteDebt = exports.calcRealBaseDebt = exports.calcRealQuoteCollateral = exports.calcRealBaseCollateral = exports.divFP96 = exports.mulFP96 = void 0; | ||
exports.convertPriceStringToX96 = exports.extractFractionAndWhole = exports.isValidNumber = exports.convertPriceHumanToX96 = exports.convertPriceX96ToHuman = exports.calcShortLiquidationPriceX96 = exports.calcLongLiquidationPriceX96 = exports.calcShortLeverage = exports.calcLongLeverage = exports.calcRealQuoteDebt = exports.calcRealBaseDebt = exports.calcRealQuoteCollateral = exports.calcRealBaseCollateral = exports.divFP96 = exports.mulFP96 = void 0; | ||
const ethers_1 = require("ethers"); | ||
const consts_js_1 = require("./consts.js"); | ||
const consts_1 = require("./consts"); | ||
/** @returns multiplication result with at least one of the multipliers in X96 format */ | ||
function mulFP96(multiplier, multiplicand) { | ||
return multiplier.mul(multiplicand).div(consts_js_1.FP96_ONE); | ||
return multiplier.mul(multiplicand).div(consts_1.FP96_ONE); | ||
} | ||
@@ -17,3 +17,3 @@ exports.mulFP96 = mulFP96; | ||
function divFP96(numerator, denominator) { | ||
return numerator.mul(consts_js_1.FP96_ONE).div(denominator); | ||
return numerator.mul(consts_1.FP96_ONE).div(denominator); | ||
} | ||
@@ -105,3 +105,3 @@ exports.divFP96 = divFP96; | ||
const power = baseDecimal.sub(quoteDecimal); | ||
return priceX96.mul(ethers_1.BigNumber.from(10).pow(power)).div(consts_js_1.FP96_ONE); | ||
return priceX96.mul(ethers_1.BigNumber.from(10).pow(power)).div(consts_1.FP96_ONE); | ||
} | ||
@@ -117,4 +117,29 @@ exports.convertPriceX96ToHuman = convertPriceX96ToHuman; | ||
const power = baseDecimal.sub(quoteDecimal); | ||
return price.mul(consts_js_1.FP96_ONE).mul(ethers_1.BigNumber.from(10).pow(power)); | ||
return price.mul(consts_1.FP96_ONE).div(ethers_1.BigNumber.from(10).pow(power)); | ||
} | ||
exports.convertPriceHumanToX96 = convertPriceHumanToX96; | ||
const isValidNumber = (s) => /^[+-]?\d*\.?\d+$/.test(s); | ||
exports.isValidNumber = isValidNumber; | ||
function extractFractionAndWhole(s) { | ||
if (!(0, exports.isValidNumber)(s)) | ||
return { whole: undefined }; | ||
const parts = s.split('.'); | ||
const whole = parts[0]; | ||
const fraction = parts[1]; | ||
return { whole, fraction }; | ||
} | ||
exports.extractFractionAndWhole = extractFractionAndWhole; | ||
/** | ||
* @param priceX price in human readable form to convert | ||
* @param baseDecimal decimals of base token | ||
* @param quoteDecimal decimals of quote token | ||
* @returns X96 representation of price | ||
*/ | ||
function convertPriceStringToX96(price, baseDecimal, quoteDecimal) { | ||
const { whole, fraction } = extractFractionAndWhole(price); | ||
const baseDecimalsNext = baseDecimal.sub(Math.max(fraction?.length || 0, 0)); | ||
const priceNext = ethers_1.BigNumber.from(`${whole}${fraction || ''}`); | ||
const power = baseDecimalsNext.sub(quoteDecimal); | ||
return priceNext.mul(consts_1.FP96_ONE).div(ethers_1.BigNumber.from(10).pow(power)); | ||
} | ||
exports.convertPriceStringToX96 = convertPriceStringToX96; |
{ | ||
"name": "@equilab/marginly-sdk", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"main": "dist/index.js", | ||
@@ -16,3 +16,3 @@ "types": "dist/index.d.ts", | ||
"build": "rm -rf ./dist && tsc --build", | ||
"test": "tsdx test --no-cache", | ||
"test": "jest", | ||
"prettify": "prettier --write ./src/**/*", | ||
@@ -27,9 +27,10 @@ "prepublish": "yarn build", | ||
"@types/mocha": "10.0.6", | ||
"chai": "^4.3.7", | ||
"ethers": "^5.7.2", | ||
"chai": "4.3.7", | ||
"ethers": "5.7.2", | ||
"jest": "29.7.0", | ||
"ts-jest": "29.1.2", | ||
"ts-node": "10.9.2", | ||
"tsdx": "^0.14.1", | ||
"typechain": "^8.3.2", | ||
"typescript": "^4.9.4" | ||
"typechain": "8.3.2", | ||
"typescript": "4.9.4" | ||
} | ||
} |
310349
4888
11