UNPKG

@dolomite-exchange/dolomite-margin

Version:

Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol

52 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Constants_1 = require("../lib/Constants"); class DolomiteMarginMath { static getPartialRoundHalfUp(target, numerator, denominator) { const value = target.abs() .times(numerator); const halfUp = value.mod(denominator) .gte(denominator.minus(1) .dividedToIntegerBy(2) .plus(1)) ? 1 : 0; const result = value.dividedToIntegerBy(denominator) .plus(halfUp); if (target.lt(Constants_1.INTEGERS.ZERO)) { return result.negated(); } return result; } static getPartialRoundUp(target, numerator, denominator) { const result = target .abs() .times(numerator) .minus('1') .dividedToIntegerBy(denominator) .plus('1'); if (target.lt(Constants_1.INTEGERS.ZERO)) { return result.negated(); } return result; } static getPartial(target, numerator, denominator) { return target.times(numerator).dividedToIntegerBy(denominator); } static weiToPar(valueWei, index) { const base = Constants_1.INTEGERS.INTEREST_RATE_BASE; if (valueWei.lt(Constants_1.INTEGERS.ZERO)) { return DolomiteMarginMath.getPartialRoundHalfUp(valueWei.negated(), base, index.borrow.times(base)).negated(); } return DolomiteMarginMath.getPartialRoundHalfUp(valueWei, base, index.supply.times(base)); } static parToWei(valueWei, index) { const base = Constants_1.INTEGERS.INTEREST_RATE_BASE; if (valueWei.lt(Constants_1.INTEGERS.ZERO)) { return DolomiteMarginMath.getPartialRoundHalfUp(valueWei.negated(), index.borrow.times(base), base).negated(); } return DolomiteMarginMath.getPartialRoundHalfUp(valueWei, index.supply.times(base), base); } } exports.default = DolomiteMarginMath; //# sourceMappingURL=DolomiteMarginMath.js.map