UNPKG

@volare.finance/volare.js

Version:
85 lines 3.52 kB
"use strict"; /** * @file collateral.ts * @author astra <astra@volare.finance> * @date 2022 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Collateral = void 0; const tslib_1 = require("tslib"); const utils_js_1 = require("@volare.finance/utils.js"); const volare_1 = require("../volare"); const url_1 = require("./url"); const LINECHART_X_SIZE = 30; const DAY_IN_S = 24 * 3600; const DAY_IN_MS = DAY_IN_S * 1000; class Collateral { constructor(instance) { this.instance = instance; } collaterals(owner, filter) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield this.instance.post((0, url_1.CollateralsUrl)(), filter, { params: { owner, }, }); const { collaterals, prices } = response.data; const prices2 = {}; for (const symbol in prices) { prices2[symbol] = new utils_js_1.BigNumber(prices[symbol]); } for (let i = 0; i < collaterals.length; i++) { if (collaterals[i].assets.length !== collaterals[i].amounts.length) throw 'C1'; collaterals[i].symbols = new Array(collaterals[i].assets.length); collaterals[i].value = utils_js_1.ZERO; for (let j = 0; j < collaterals[i].assets.length; j++) { const symbol = yield (0, volare_1.cSymbol)(collaterals[i].assets[j]); collaterals[i].symbols[j] = symbol; collaterals[i].amounts[j] = new utils_js_1.BigNumber(collaterals[i].amounts[j]); if (symbol in prices2 && prices2[symbol].gt(0)) { collaterals[i].value = collaterals[i].value.plus(collaterals[i].amounts[j].multipliedBy(prices2[symbol])); } } } return { collaterals: collaterals, prices: prices2, }; }); } lineChart(owner, symbol, interval = '24h', // only support 24h right now filter) { return tslib_1.__awaiter(this, void 0, void 0, function* () { filter = Object.assign(Object.assign({}, filter), { limit: LINECHART_X_SIZE, sort: { timestamp: -1 } }); const response = yield this.instance.post((0, url_1.CollateralsSamplesUrl)(), filter, { params: { owner, symbol, interval, }, }); const data = response.data; data[data.length] = { timestamp: 0, amount: 0 }; const timestamps = new Array(LINECHART_X_SIZE); const samples = new Array(LINECHART_X_SIZE); const timestamp = Math.floor(Date.now() / DAY_IN_MS) * DAY_IN_S; timestamps[0] = 0; samples[0] = 0; // for (let k = 0; k < data.length; k++) { // console.log(`${new Date(data[k].timestamp * 1000)}: ${data[k].amount}`) // } for (let i = 1, j = 0; i < LINECHART_X_SIZE; i++) { timestamps[i] = timestamp - DAY_IN_S * (i - 1) - 1; while (timestamps[i] < data[j].timestamp) { j++; } samples[i] = data[j].amount; } return { timestamps, samples }; }); } } exports.Collateral = Collateral; //# sourceMappingURL=collateral.js.map