@volare.finance/volare.js
Version:
The SDK for Volare Protocol
147 lines • 7.22 kB
JavaScript
"use strict";
/**
* @file MarginCalculator.ts
* @author astra <astra@volare.finance>
* @date 2022
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarginCalculator = void 0;
const tslib_1 = require("tslib");
const utils_js_1 = require("@volare.finance/utils.js");
const ethers_1 = require("ethers");
const MarginCalculator_json_1 = require("../artifacts/MarginCalculator.json");
const protocols_1 = require("./protocols");
class MarginCalculator extends utils_js_1.Provider {
static ABI() {
return MarginCalculator_json_1.abi;
}
constructor(address, endpoint) {
super(endpoint);
this.contract = new ethers_1.Contract(address, MarginCalculator.ABI(), this.provider);
}
/**
* @notice set spot shock value, scaled to 1e27
* @dev can only be called by owner
* @param owner
* @param vToken
* @param shockValue spot shock value
*/
setSpotShock(owner, vToken, shockValue) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const scaledShockValue = (0, utils_js_1.$)(shockValue, protocols_1.BASE_DECIMALS);
return (_a = this.contract) === null || _a === void 0 ? void 0 : _a.connect(owner).setSpotShock(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut, scaledShockValue.toString(10));
});
}
/***
* @description calculate required collateral margin for a vault
* @param vToken
* @return shockValue spot shock value (1e27)
*/
getSpotShock(vToken) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const shockValue = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getSpotShock(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut));
return (0, utils_js_1.$float)(shockValue, protocols_1.BASE_DECIMALS);
});
}
/**
* @notice set product upper bound values
* @dev can only be called by owner
* @param owner
* @param vToken
* @param timesToExpiry array of times to expiry timestamp
* @param values upper bound values array
*
*/
setUpperBoundValues(owner, vToken, timesToExpiry, values) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (timesToExpiry.length === values.length) {
throw 'MarginCalculator: invalid values array';
}
const scaledValues = [];
for (let i = 0; i < values.length; i++) {
scaledValues[i] = (0, utils_js_1.$)(values[i], protocols_1.BASE_DECIMALS);
}
return (_a = this.contract) === null || _a === void 0 ? void 0 : _a.connect(owner).setUpperBoundValues(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut, timesToExpiry, scaledValues.map(value => value.toString(10)));
});
}
/**
* @notice set option upper bound value for specific time to expiry (1e27)
* @dev can only be called by owner
* @param owner
* @param vToken
* @param timeToExpiry option time to expiry timestamp
* @param value upper bound value
*/
updateUpperBoundValue(owner, vToken, timeToExpiry, value) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const scaledValue = (0, utils_js_1.$)(value, protocols_1.BASE_DECIMALS);
return (_a = this.contract) === null || _a === void 0 ? void 0 : _a.connect(owner).updateUpperBoundValue(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut, timeToExpiry, scaledValue.toString(10));
});
}
/**
* @notice get times to expiry for a specific product
* @param vToken
* @return array of times to expiry
*/
getTimesToExpiry(vToken) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const timesToExpiryForProduct = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getTimesToExpiry(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut));
const timesToExpiryForProduct2 = [];
for (let i = 0; i < timesToExpiryForProduct.length; i++) {
timesToExpiryForProduct2[i] = timesToExpiryForProduct[i].toNumber();
}
return timesToExpiryForProduct2;
});
}
/**
* @notice get option upper bound value for specific time to expiry
* @param vToken
* @param timeToExpiry option time to expiry timestamp
* @return option upper bound value (1e27)
*/
getMaxPrice(vToken, timeToExpiry) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const maxPriceAtTimeToExpiry = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getMaxPrice(vToken.underlyingAsset, vToken.strikeAsset, vToken.collateralAsset, vToken.isPut, timeToExpiry));
return (0, utils_js_1.$float)(maxPriceAtTimeToExpiry, protocols_1.BASE_DECIMALS);
});
}
/***
* @description calculate required collateral margin for a vault
* @param vault
* @param vaultType
* @return the vault collateral amount,
* and marginRequired the minimal amount of collateral needed in a vault, scaled to 1e27
*/
getMarginRequired(vault, vaultType) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const [collateralAmount, collateralRequired] = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getMarginRequired(vault, Number(vaultType)));
return [(0, utils_js_1.$float)(collateralAmount, protocols_1.BASE_DECIMALS), (0, utils_js_1.$float)(collateralRequired, protocols_1.BASE_DECIMALS)];
});
}
/**
* @notice returns the amount of collateral that can be removed from an actual or a theoretical vault
* @dev return amount is denominated in the collateral asset for the vToken in the vault, or the collateral asset in the vault
* @param vault theoretical vault that needs to be checked
* @param vaultType vault type (0 for spread/max loss, 1 for naked margin)
* @return excessCollateral the amount by which the margin is above or below the required amount
* @return isExcess True if there is excess margin in the vault, False if there is a deficit of margin in the vault
* if True, collateral can be taken out from the vault, if False, additional collateral needs to be added to vault
*/
getExcessCollateral(vault, vaultType) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const [excessCollateral, isExcess] = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getExcessCollateral(vault, Number(vaultType)));
const decimals = yield utils_js_1.ERC20.Decimals(vault.collateralAssets[0]);
return [(0, utils_js_1.$float)(excessCollateral, decimals), isExcess];
});
}
}
exports.MarginCalculator = MarginCalculator;
//# sourceMappingURL=MarginCalculator.js.map