2020-07-13 08:54:08 +00:00
|
|
|
import BigNumber from 'bignumber.js';
|
2020-11-28 12:28:50 +00:00
|
|
|
import {RAY, WAD, HALF_RAY, HALF_WAD, WAD_RAY_RATIO, HALF_PERCENTAGE, PERCENTAGE_FACTOR} from '../../../helpers/constants';
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
declare module 'bignumber.js' {
|
2020-06-12 20:12:53 +00:00
|
|
|
interface BigNumber {
|
|
|
|
ray: () => BigNumber;
|
|
|
|
wad: () => BigNumber;
|
|
|
|
halfRay: () => BigNumber;
|
|
|
|
halfWad: () => BigNumber;
|
2020-11-28 12:28:50 +00:00
|
|
|
halfPercentage: () => BigNumber;
|
2020-06-12 20:12:53 +00:00
|
|
|
wadMul: (a: BigNumber) => BigNumber;
|
|
|
|
wadDiv: (a: BigNumber) => BigNumber;
|
|
|
|
rayMul: (a: BigNumber) => BigNumber;
|
|
|
|
rayDiv: (a: BigNumber) => BigNumber;
|
2020-11-28 12:28:50 +00:00
|
|
|
percentMul: (a: BigNumber) => BigNumber;
|
|
|
|
percentDiv: (a: BigNumber) => BigNumber;
|
2020-06-12 20:12:53 +00:00
|
|
|
rayToWad: () => BigNumber;
|
|
|
|
wadToRay: () => BigNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BigNumber.prototype.ray = (): BigNumber => {
|
|
|
|
return new BigNumber(RAY).decimalPlaces(0);
|
|
|
|
};
|
|
|
|
BigNumber.prototype.wad = (): BigNumber => {
|
|
|
|
return new BigNumber(WAD).decimalPlaces(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.halfRay = (): BigNumber => {
|
|
|
|
return new BigNumber(HALF_RAY).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.halfWad = (): BigNumber => {
|
|
|
|
return new BigNumber(HALF_WAD).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.wadMul = function (b: BigNumber): BigNumber {
|
2020-07-13 08:54:08 +00:00
|
|
|
return this.halfWad().plus(this.multipliedBy(b)).div(WAD).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.wadDiv = function (a: BigNumber): BigNumber {
|
|
|
|
const halfA = a.div(2).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return halfA.plus(this.multipliedBy(WAD)).div(a).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.rayMul = function (a: BigNumber): BigNumber {
|
2020-07-13 08:54:08 +00:00
|
|
|
return this.halfRay().plus(this.multipliedBy(a)).div(RAY).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.rayDiv = function (a: BigNumber): BigNumber {
|
|
|
|
const halfA = a.div(2).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
|
|
|
|
return halfA
|
|
|
|
.plus(this.multipliedBy(RAY))
|
|
|
|
.decimalPlaces(0, BigNumber.ROUND_DOWN)
|
|
|
|
.div(a)
|
|
|
|
.decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.rayToWad = function (): BigNumber {
|
|
|
|
const halfRatio = new BigNumber(WAD_RAY_RATIO).div(2);
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
return halfRatio.plus(this).div(WAD_RAY_RATIO).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.wadToRay = function (): BigNumber {
|
2020-07-13 08:54:08 +00:00
|
|
|
return this.multipliedBy(WAD_RAY_RATIO).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
2020-06-12 20:12:53 +00:00
|
|
|
};
|
2020-11-28 12:28:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BigNumber.prototype.halfPercentage = (): BigNumber => {
|
|
|
|
return new BigNumber(HALF_PERCENTAGE).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.percentMul = function (b: BigNumber): BigNumber {
|
|
|
|
return this.halfPercentage().plus(this.multipliedBy(b)).div(PERCENTAGE_FACTOR).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|
|
|
|
|
|
|
|
BigNumber.prototype.percentDiv = function (a: BigNumber): BigNumber {
|
|
|
|
const halfA = a.div(2).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
|
|
|
|
return halfA.plus(this.multipliedBy(PERCENTAGE_FACTOR)).div(a).decimalPlaces(0, BigNumber.ROUND_DOWN);
|
|
|
|
};
|