@volare.finance/volare.js
Version:
The SDK for Volare Protocol
104 lines • 3.52 kB
JavaScript
;
/**
* @file vToken.ts
* @author astra <astra@volare.finance>
* @date 2022
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.VToken = void 0;
const tslib_1 = require("tslib");
const utils_js_1 = require("@volare.finance/utils.js");
const MattL922_1 = require("../3rd-party/MattL922");
const url_1 = require("./url");
const MONTH = {
JAN: 0,
FEB: 1,
MAR: 2,
APR: 3,
MAY: 4,
JUN: 5,
JUL: 6,
AUG: 7,
SEP: 8,
OCT: 9,
NOV: 10,
DEC: 11,
};
function params(vToken, collateralInUSD, strikeInUSD) {
const { expiry, strikePrice, isPut } = VToken.SplitSymbol(vToken.symbol);
const s = collateralInUSD.toNumber();
const k = new utils_js_1.BigNumber(strikePrice).dividedBy(strikeInUSD).toNumber();
const now = Date.now() / 1000;
const t = now < expiry ? (expiry - now) / 31536000 : 0;
const r = 0.001;
return [s, k, t, r, isPut];
}
class VToken {
static SplitSymbol(symbol) {
const [underlyingSymbol, expiry, strikePrice, isPut] = symbol.split('-');
return {
underlyingSymbol,
expiry: Date.UTC(Number(expiry.substring(5, 7)) + 2000, MONTH[expiry.substring(2, 5)], Number(expiry.substring(0, 2)), 8) / 1000,
strikePrice: Number(strikePrice),
isPut: isPut === 'P',
};
}
static Greek(vToken, collateralInUSD, strikeInUSD, costInUSD, estimate = 0.1) {
const [s, k, t, r, isPut] = params(vToken, collateralInUSD, strikeInUSD);
const iv = (0, MattL922_1.getImpliedVolatility)(costInUSD, s, k, t, r, isPut, estimate);
const delta = (0, MattL922_1.getDelta)(s, k, t, iv, r, isPut);
const rho = (0, MattL922_1.getRho)(s, k, t, iv, r, isPut);
const vega = (0, MattL922_1.getVega)(s, k, t, iv, r);
const theta = (0, MattL922_1.getTheta)(s, k, t, iv, r, isPut);
const gamma = (0, MattL922_1.getGamma)(s, k, t, iv, r);
return {
iv,
delta,
rho,
vega,
theta,
gamma,
};
}
constructor(instance) {
this.instance = instance;
}
v(vToken) {
vToken.strikePrice = new utils_js_1.BigNumber(vToken.strikePrice);
return vToken;
}
vTokens(product, underlying, isExpired = false, expiry, filter) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield this.instance.post((0, url_1.VTokensUrl)(), filter, {
params: {
product,
underlying,
isExpired,
expiry,
isWhitelisted: true,
},
});
return response.data;
});
}
vToken(symbol) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield this.instance.get((0, url_1.VTokensBySymbolUrl)(symbol));
return this.v(response.data);
});
}
expires(product, isExpired = false, filter) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const response = yield this.instance.post((0, url_1.VTokensExpiresUrl)(), filter, {
params: {
product,
isExpired,
isWhitelisted: true,
},
});
return response.data;
});
}
}
exports.VToken = VToken;
//# sourceMappingURL=vToken.js.map