UNPKG

@swipewallet/venus-js

Version:

A JavaScript SDK for Ethereum and the Venus Protocol.

193 lines 10.9 kB
"use strict"; /** * @file Price Feed * @desc These methods facilitate interactions with the Open Price Feed smart * contracts. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; exports.__esModule = true; exports.getPrice = void 0; var eth = require("./eth"); var helpers_1 = require("./helpers"); var constants_1 = require("./constants"); var bignumber_1 = require("@ethersproject/bignumber/lib/bignumber"); function validateAsset(asset, argument, errorPrefix) { if (typeof asset !== 'string' || asset.length < 1) { throw Error(errorPrefix + 'Argument `' + argument + '` must be a non-empty string.'); } var assetIsVToken = asset[0] === 'v'; var vTokenName = assetIsVToken ? asset : 'v' + asset; var vTokenAddress = constants_1.address[this._network.name][vTokenName]; var underlyingName = assetIsVToken ? asset.slice(1, asset.length) : asset; var underlyingAddress = constants_1.address[this._network.name][underlyingName]; if ((!constants_1.cTokens.includes(vTokenName) || !constants_1.underlyings.includes(underlyingName)) && !constants_1.opfAssets.includes(underlyingName)) { throw Error(errorPrefix + 'Argument `' + argument + '` is not supported.'); } var underlyingDecimals = constants_1.decimals[underlyingName]; // The open price feed reveals BTC, not WBTC. underlyingName = underlyingName === 'WBTC' ? 'BTC' : underlyingName; return [assetIsVToken, vTokenName, vTokenAddress, underlyingName, underlyingAddress, underlyingDecimals]; } function vTokenExchangeRate(vTokenAddress, vTokenName, underlyingDecimals) { return __awaiter(this, void 0, void 0, function () { var address, method, options, exchangeRateCurrent, mantissa, oneVTokenInUnderlying; return __generator(this, function (_a) { switch (_a.label) { case 0: address = vTokenAddress; method = 'exchangeRateCurrent'; options = { _compoundProvider: this._provider, abi: vTokenName === constants_1.constants.vBNB ? constants_1.abi.vBNB : constants_1.abi.vBep20 }; return [4 /*yield*/, eth.read(address, method, [], options)]; case 1: exchangeRateCurrent = _a.sent(); mantissa = 18 + underlyingDecimals - 8; oneVTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa); return [2 /*return*/, oneVTokenInUnderlying]; } }); }); } /** * Gets an asset's price from the Venus Protocol open price feed. The price * of the asset can be returned in any other supported asset value, including * all vTokens and underlyings. * * @param {string} asset A string of a supported asset in which to find the * current price. * @param {string} [inAsset] A string of a supported asset in which to express * the `asset` parameter's price. This defaults to USD. * * @returns {string} Returns a string of the numeric value of the asset. * * @example * ``` * const venus = new Venus(window.ethereum); * let price; * * (async function () { * * price = await venus.getPrice(Venus.BNB); * console.log('BNB in USD', price); * * price = await venus.getPrice(Venus.SXP, Venus.USDC); // supports vTokens too * console.log('SXP in USDC', price); * * })().catch(console.error); * ``` */ function getPrice(asset, inAsset) { if (inAsset === void 0) { inAsset = constants_1.constants.USDC; } return __awaiter(this, void 0, void 0, function () { var errorPrefix, _a, // eslint-disable-next-line @typescript-eslint/no-unused-vars assetIsVToken, vTokenName, vTokenAddress, underlyingName, underlyingAddress, underlyingDecimals, _b, // eslint-disable-next-line @typescript-eslint/no-unused-vars inAssetIsVToken, inAssetVTokenName, inAssetVTokenAddress, inAssetUnderlyingName, inAssetUnderlyingAddress, inAssetUnderlyingDecimals, comptrollerAddress, oracleTrxOptions, priceOracleAddress, trxOptions, assetUnderlyingPrice, inAssetUnderlyingPrice, assetDecimal, inAssetDecimal, assetVTokensInUnderlying, inAssetVTokensInUnderlying, result, assetInOther, assetInOther, assetInOther, vTokensInUnderlying; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, helpers_1.netId(this)]; case 1: _c.sent(); errorPrefix = 'Venus [getPrice] | '; _a = validateAsset.bind(this)(asset, 'asset', errorPrefix), assetIsVToken = _a[0], vTokenName = _a[1], vTokenAddress = _a[2], underlyingName = _a[3], underlyingAddress = _a[4], underlyingDecimals = _a[5]; _b = validateAsset.bind(this)(inAsset, 'inAsset', errorPrefix), inAssetIsVToken = _b[0], inAssetVTokenName = _b[1], inAssetVTokenAddress = _b[2], inAssetUnderlyingName = _b[3], inAssetUnderlyingAddress = _b[4], inAssetUnderlyingDecimals = _b[5]; comptrollerAddress = constants_1.address[this._network.name].Comptroller; oracleTrxOptions = { _compoundProvider: this._provider, abi: constants_1.abi.Comptroller }; return [4 /*yield*/, eth.read(comptrollerAddress, 'oracle', [], oracleTrxOptions)]; case 2: priceOracleAddress = _c.sent(); trxOptions = { _compoundProvider: this._provider, abi: constants_1.abi.PriceOracle }; return [4 /*yield*/, eth.read(priceOracleAddress, 'getUnderlyingPrice', [vTokenAddress], trxOptions)]; case 3: assetUnderlyingPrice = _c.sent(); return [4 /*yield*/, eth.read(priceOracleAddress, 'getUnderlyingPrice', [inAssetVTokenAddress], trxOptions)]; case 4: inAssetUnderlyingPrice = _c.sent(); assetDecimal = constants_1.decimals[asset]; inAssetDecimal = constants_1.decimals[inAsset]; if ((assetDecimal - inAssetDecimal) > 0) { assetUnderlyingPrice = assetUnderlyingPrice.mul(bignumber_1.BigNumber.from("10").pow(assetDecimal - inAssetDecimal)); } else { assetUnderlyingPrice = assetUnderlyingPrice.div(bignumber_1.BigNumber.from("10").pow(inAssetDecimal - assetDecimal)); } if (!assetIsVToken) return [3 /*break*/, 6]; return [4 /*yield*/, vTokenExchangeRate.bind(this)(vTokenAddress, vTokenName, underlyingDecimals)]; case 5: assetVTokensInUnderlying = _c.sent(); _c.label = 6; case 6: if (!inAssetIsVToken) return [3 /*break*/, 8]; return [4 /*yield*/, vTokenExchangeRate.bind(this)(inAssetVTokenAddress, inAssetVTokenName, inAssetUnderlyingDecimals)]; case 7: inAssetVTokensInUnderlying = _c.sent(); _c.label = 8; case 8: if (!assetIsVToken && !inAssetIsVToken) { result = assetUnderlyingPrice / inAssetUnderlyingPrice; } else if (assetIsVToken && !inAssetIsVToken) { assetInOther = assetUnderlyingPrice / inAssetUnderlyingPrice; result = assetInOther * assetVTokensInUnderlying; } else if (!assetIsVToken && inAssetIsVToken) { assetInOther = assetUnderlyingPrice / inAssetUnderlyingPrice; result = assetInOther / inAssetVTokensInUnderlying; } else { assetInOther = assetUnderlyingPrice / inAssetUnderlyingPrice; vTokensInUnderlying = assetInOther / assetVTokensInUnderlying; result = inAssetVTokensInUnderlying * vTokensInUnderlying; } return [2 /*return*/, result]; } }); }); } exports.getPrice = getPrice; //# sourceMappingURL=priceFeed.js.map