@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
148 lines • 6.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assetAmountToContractAssetAmount = exports.getInterestPerSecondForAAVECopyCat = exports.getInterestPerSecondForDoubleExponent = exports.getInterestPerSecondForPolynomial = exports.toNumber = exports.coefficientsToString = exports.valueToInteger = exports.integerToValue = exports.toString = exports.decimalToString = exports.stringToDecimal = void 0;
const bignumber_js_1 = require("bignumber.js");
const Constants_1 = require("./Constants");
const DolomiteMarginMath_1 = __importDefault(require("../modules/DolomiteMarginMath"));
function stringToDecimal(s) {
return new bignumber_js_1.BigNumber(s).div(Constants_1.INTEGERS.INTEREST_RATE_BASE);
}
exports.stringToDecimal = stringToDecimal;
function decimalToString(d) {
return new bignumber_js_1.BigNumber(d).times(Constants_1.INTEGERS.INTEREST_RATE_BASE).toFixed(0);
}
exports.decimalToString = decimalToString;
function toString(input) {
return new bignumber_js_1.BigNumber(input).toFixed(0);
}
exports.toString = toString;
function integerToValue(i) {
return {
sign: i.isGreaterThan(0),
value: i.abs().toFixed(0),
};
}
exports.integerToValue = integerToValue;
function valueToInteger({ value, sign, }) {
let result = new bignumber_js_1.BigNumber(value);
if (!result.isZero() && !sign) {
result = result.times(-1);
}
return result;
}
exports.valueToInteger = valueToInteger;
function coefficientsToString(coefficients) {
let m = new bignumber_js_1.BigNumber(1);
let result = new bignumber_js_1.BigNumber(0);
for (let i = 0; i < coefficients.length; i += 1) {
result = result.plus(m.times(coefficients[i]));
m = m.times(256);
}
return result.toFixed(0);
}
exports.coefficientsToString = coefficientsToString;
function toNumber(input) {
return new bignumber_js_1.BigNumber(input).toNumber();
}
exports.toNumber = toNumber;
function getInterestPerSecondForPolynomial(maxAPR, coefficients, totals) {
if (totals.totalBorrowed.isZero()) {
return new bignumber_js_1.BigNumber(0);
}
const PERCENT = new bignumber_js_1.BigNumber('100');
const BASE = new bignumber_js_1.BigNumber('1e18');
let result = new bignumber_js_1.BigNumber(0);
if (totals.totalBorrowed.gt(totals.totalSupply)) {
result = BASE.times(PERCENT);
}
else {
let polynomial = BASE;
for (let i = 0; i < coefficients.length; i += 1) {
const coefficient = new bignumber_js_1.BigNumber(coefficients[i]);
const term = polynomial.times(coefficient);
result = result.plus(term);
polynomial = partial(polynomial, totals.totalBorrowed, totals.totalSupply);
}
}
return result
.times(maxAPR)
.div(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS)
.div(PERCENT)
.integerValue(bignumber_js_1.BigNumber.ROUND_DOWN)
.div(BASE);
}
exports.getInterestPerSecondForPolynomial = getInterestPerSecondForPolynomial;
function getInterestPerSecondForDoubleExponent(maxAPR, coefficients, totals) {
if (totals.totalBorrowed.isZero()) {
return new bignumber_js_1.BigNumber(0);
}
const PERCENT = new bignumber_js_1.BigNumber('100');
const BASE = new bignumber_js_1.BigNumber('1e18');
let result;
if (totals.totalBorrowed.gt(totals.totalSupply)) {
result = BASE.times(PERCENT);
}
else {
result = BASE.times(coefficients[0]);
let polynomial = partial(BASE, totals.totalBorrowed, totals.totalSupply);
for (let i = 1; i < coefficients.length; i += 1) {
const coefficient = new bignumber_js_1.BigNumber(coefficients[i]);
const term = polynomial.times(coefficient);
result = result.plus(term);
polynomial = partial(polynomial, polynomial, BASE);
}
}
return result
.times(maxAPR)
.div(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS)
.div(PERCENT)
.integerValue(bignumber_js_1.BigNumber.ROUND_DOWN)
.div(BASE);
}
exports.getInterestPerSecondForDoubleExponent = getInterestPerSecondForDoubleExponent;
function getInterestPerSecondForAAVECopyCat(isStableCoin, totals) {
const BASE = new bignumber_js_1.BigNumber(1e18); // 100%
if (totals.totalBorrowed.isZero()) {
return Constants_1.INTEGERS.ZERO;
}
if (totals.totalSupply.isZero()) {
// totalBorrowed > 0
return BASE.dividedToIntegerBy(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS).div(BASE);
}
const utilization = DolomiteMarginMath_1.default.getPartial(BASE, totals.totalBorrowed.abs(), totals.totalSupply);
const NINETY_PERCENT = BASE.times(new bignumber_js_1.BigNumber('0.9'));
const TEN_PERCENT = BASE.times(new bignumber_js_1.BigNumber('0.1'));
const INITIAL_GOAL = BASE.times(isStableCoin ? new bignumber_js_1.BigNumber('0.04') : new bignumber_js_1.BigNumber('0.07'));
if (utilization.gte(BASE)) {
return BASE.dividedToIntegerBy(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS).div(BASE);
}
if (utilization.gt(NINETY_PERCENT)) {
// interest is equal to 4% + linear progress to 100% APR
const deltaToGoal = BASE.minus(INITIAL_GOAL);
const interestToAdd = deltaToGoal.times(utilization.minus(NINETY_PERCENT)).div(TEN_PERCENT);
return interestToAdd.plus(INITIAL_GOAL).dividedToIntegerBy(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS).div(BASE);
}
return DolomiteMarginMath_1.default.getPartial(INITIAL_GOAL, utilization, NINETY_PERCENT)
.dividedToIntegerBy(Constants_1.INTEGERS.ONE_YEAR_IN_SECONDS)
.div(BASE);
}
exports.getInterestPerSecondForAAVECopyCat = getInterestPerSecondForAAVECopyCat;
function partial(target, numerator, denominator) {
return target
.times(numerator)
.div(denominator)
.integerValue(bignumber_js_1.BigNumber.ROUND_DOWN);
}
function assetAmountToContractAssetAmount(assetAmount) {
return {
sign: assetAmount.sign,
denomination: assetAmount.denomination,
ref: assetAmount.ref,
value: assetAmount.value.toFixed(),
};
}
exports.assetAmountToContractAssetAmount = assetAmountToContractAssetAmount;
//# sourceMappingURL=Helpers.js.map