@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
88 lines (83 loc) • 3.6 kB
JavaScript
;
/*
Copyright 2019 dYdX Trading Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Interest = void 0;
const bignumber_js_1 = require("bignumber.js");
const Helpers_1 = require("./Helpers");
const interest_constants_json_1 = __importDefault(require("./interest-constants.json"));
const Constants_1 = require("./Constants");
class Interest {
constructor(networkId) {
this.setNetworkId(networkId);
}
setNetworkId(networkId) {
this.networkId = networkId;
}
getEarningsRate() {
const networkConstants = this.getNetworkConstants();
const earningsRate = new bignumber_js_1.BigNumber(networkConstants.earningsRate);
if (!earningsRate) {
throw new Error(`No earnings rate for network: ${this.networkId}`);
}
return new bignumber_js_1.BigNumber(earningsRate);
}
getInterestPerSecondByMarket(marketId, totals, extraData) {
const earningsRate = this.getEarningsRate();
const constants = extraData ?? this.getMarketConstants(marketId);
// determine the borrow interest rate (capped at 18 decimal places)
let borrowInterestRate;
if (constants.interestRateModel === 'DoubleExponent') {
borrowInterestRate = Helpers_1.getInterestPerSecondForDoubleExponent(constants.maxAPR, constants.coefficients, totals);
}
else if (constants.interestRateModel === 'AAVECopyCat') {
borrowInterestRate = Helpers_1.getInterestPerSecondForAAVECopyCat(constants.isStableCoin, totals);
}
else if (constants.interestRateModel === 'AlwaysZero') {
borrowInterestRate = Constants_1.INTEGERS.ZERO;
}
// determine the supply interest rate (uncapped decimal places)
let supplyInterestRate = borrowInterestRate.times(earningsRate);
if (totals.totalBorrowed.lt(totals.totalSupply)) {
supplyInterestRate = supplyInterestRate
.times(totals.totalBorrowed)
.div(totals.totalSupply);
}
return {
borrowInterestRate,
supplyInterestRate,
};
}
// ============ Private Helper Functions ============
getNetworkConstants() {
const networkConstants = interest_constants_json_1.default[this.networkId];
if (!networkConstants) {
throw new Error(`No interest constants for network: ${this.networkId}`);
}
return networkConstants;
}
getMarketConstants(marketId) {
const networkConstants = this.getNetworkConstants();
const constants = networkConstants[marketId.toFixed(0)];
if (!constants) {
// '0' is always defined and is the fallback
return networkConstants['0'];
}
return constants;
}
}
exports.Interest = Interest;
//# sourceMappingURL=Interest.js.map