defi-utils
Version:
> An Accessible Defi Utility for Defi
89 lines • 3.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefiUtils = void 0;
const bignumber_js_1 = require("bignumber.js");
class DefiUtils extends bignumber_js_1.default {
constructor(n, base) {
super(n, base);
this.plus = (n, base) => {
return new DefiUtils(super.plus(n, base));
};
this.minus = (n, base) => {
return new DefiUtils(super.minus(n, base));
};
this.pow = (n, m) => {
return new DefiUtils(super.pow(n, m));
};
this.div = (n, base) => {
return new DefiUtils(super.div(n, base));
};
this.dividedBy = (n, base) => {
return new DefiUtils(super.dividedBy(n, base));
};
this.multipliedBy = (n, base) => {
return new DefiUtils(super.multipliedBy(n, base));
};
this.toBasicUnits = (decimals) => {
return new DefiUtils(this.multipliedBy(`1e${decimals}`));
};
this.toFullDecimals = (decimals) => {
return new DefiUtils(this.dividedBy(`1e${decimals}`));
};
this.toUnderlying = (exchangeRate) => {
return new DefiUtils(new DefiUtils(exchangeRate).multipliedBy(this).dividedBy(DefiUtils.WAD));
};
this.toTokens = (exchangeRate) => {
return new DefiUtils(new DefiUtils(this)
.multipliedBy(DefiUtils.WAD)
.multipliedBy(DefiUtils.WAD)
.dividedBy(exchangeRate)
.dividedBy(DefiUtils.WAD));
};
this.toUSD = (priceUSD) => {
return new DefiUtils(new DefiUtils(this).multipliedBy(priceUSD));
};
this.fromUSD = (priceUSD) => {
return new DefiUtils(new DefiUtils(this).dividedBy(priceUSD));
};
this.toAPY = () => {
const calc1 = new DefiUtils(this).dividedBy(365);
const calc2 = new DefiUtils(1).plus(calc1);
const calc3 = new DefiUtils(calc2).pow(365);
return new DefiUtils(calc3.minus(1));
};
this.toAPR = () => {
const onePlusVal = new DefiUtils(this).plus(1);
const exponent = new DefiUtils(1).dividedBy(1);
const powResult = onePlusVal.pow(exponent);
const result = powResult.minus(1).times(1).dividedBy(100);
return new DefiUtils(result);
};
this.removeScientificNotation = () => {
return new DefiUtils(this).toFixed(new DefiUtils(this).decimalPlaces(), DefiUtils.ROUND_DOWN);
};
this.toSafeFixed = (decimalPlaces, roundingMode) => {
const value = this.toFixed(decimalPlaces, roundingMode);
return new DefiUtils(value).toFixed(new DefiUtils(value).decimalPlaces(), roundingMode);
};
this.toSafeString = () => {
const value = this.isNaN() || !this.isFinite() ? "0" : this.toString();
return value;
};
this.toSafeNumber = () => {
const value = this.isNaN() || !this.isFinite() ? 0 : this.toNumber();
return value;
};
}
static min(...n) {
return new DefiUtils(super.min(...n));
}
static max(...n) {
return new DefiUtils(super.max(...n));
}
}
exports.DefiUtils = DefiUtils;
DefiUtils.WAD = new DefiUtils(1e18).toString();
DefiUtils.WAD_WAD = new DefiUtils(1e36).toString();
DefiUtils.SECONDS_PER_DAY = new DefiUtils(86400).toString();
exports.default = DefiUtils;
//# sourceMappingURL=index.js.map