UNPKG

@walletpack/core

Version:

> TODO: description

240 lines (217 loc) 10.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _IdGenerator = _interopRequireDefault(require("../util/IdGenerator")); var _PluginRepository = _interopRequireDefault(require("../plugins/PluginRepository")); var _Blockchains = require("./Blockchains"); var _StoreService = _interopRequireDefault(require("../services/utility/StoreService")); var _BalanceService = _interopRequireDefault(require("../services/blockchain/BalanceService")); var Token = /*#__PURE__*/ function () { function Token() { var blockchain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; var contract = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var symbol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; var name = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; var decimals = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; var chainId = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ''; (0, _classCallCheck2["default"])(this, Token); this.id = _IdGenerator["default"].text(24); this.blockchain = blockchain; this.contract = contract; this.symbol = symbol; this.name = name ? name : symbol; this.decimals = decimals ? decimals : 2; this.amount = 0; this.chainId = chainId; this.unusable = null; this.fromOrigin = ''; this.createdAt = +new Date(); } (0, _createClass2["default"])(Token, [{ key: "isValid", value: function isValid() { if (Object.keys(this).length !== 11) return false; return _Blockchains.BlockchainsArray.map(function (x) { return x.value; }).includes(this.blockchain) && this.contract.length && this.symbol.length && this.name.length && this.decimals.toString().length && this.chainId.length; } }, { key: "clone", value: function clone() { return Token.fromJson(JSON.parse(JSON.stringify(this))); } }, { key: "unique", value: function unique() { return "".concat(this.blockchain, ":").concat(this.contract.toLowerCase(), ":").concat(this.symbol.toLowerCase()); } }, { key: "uniqueWithChain", value: function uniqueWithChain() { var includeUnusable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; return "".concat(this.blockchain, ":").concat(this.contract.toLowerCase(), ":").concat(this.symbol.toLowerCase(), ":").concat(this.chainId).concat(includeUnusable && this.unusable ? ":".concat(this.unusable) : ''); } }, { key: "identifiable", value: function identifiable() { return "".concat(this.blockchain, ":").concat(this.contract.toLowerCase()); } }, { key: "add", value: function add(quantity) { this.amount = (parseFloat(this.amount) + parseFloat(quantity)).toFixed(this.decimals); } }, { key: "network", value: function network() { var _this = this; var networks = _StoreService["default"].get().state.scatter.settings.networks; var endorsed = networks.find(function (x) { return x.unique() === _PluginRepository["default"].plugin(_this.blockchain).getEndorsedNetwork().unique(); }); if (!this.chainId || !this.chainId.length) return endorsed; return networks.find(function (x) { return x.blockchain === _this.blockchain && x.chainId === _this.chainId; }) || endorsed; } }, { key: "formatted", value: function formatted() { return "".concat(this.amount, " ").concat(this.symbol); } }, { key: "fiatBalance", value: function fiatBalance() { var withSymbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var price = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var unusableReplacement = this.uniqueWithChain().replace(":".concat(this.unusable), ''); if (_StoreService["default"].get().state.prices.hasOwnProperty(this.uniqueWithChain())) { price = price ? price : parseFloat(_StoreService["default"].get().state.prices[this.uniqueWithChain()][_StoreService["default"].get().state.scatter.settings.displayCurrency]); return "".concat(parseFloat(price * parseFloat(this.amount)).toFixed(4), " ").concat(withSymbol ? _StoreService["default"].get().state.scatter.settings.displayCurrency : ''); } else if (this.unusable && _StoreService["default"].get().state.prices.hasOwnProperty(unusableReplacement)) { price = price ? price : parseFloat(_StoreService["default"].get().state.prices[unusableReplacement][_StoreService["default"].get().state.scatter.settings.displayCurrency]); return "".concat(parseFloat(price * parseFloat(this.amount)).toFixed(4), " ").concat(withSymbol ? _StoreService["default"].get().state.scatter.settings.displayCurrency : ''); } else { return null; } } }, { key: "fiatPrice", value: function fiatPrice() { var withSymbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (_StoreService["default"].get().state.prices.hasOwnProperty(this.uniqueWithChain())) { var price = parseFloat(_StoreService["default"].get().state.prices[this.uniqueWithChain()][_StoreService["default"].get().state.scatter.settings.displayCurrency]); return "".concat(parseFloat(price).toFixed(4), " ").concat(withSymbol ? _StoreService["default"].get().state.scatter.settings.displayCurrency : ''); } else { return null; } } }, { key: "baseTokenPrice", value: function baseTokenPrice() { var withSymbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (_StoreService["default"].get().state.prices.hasOwnProperty(this.uniqueWithChain())) { var systemToken = this.network().systemToken(); if (this.uniqueWithChain(false) === systemToken.uniqueWithChain(false)) return null; var baseTokenPrice = parseFloat(_StoreService["default"].get().state.prices[systemToken.uniqueWithChain()][_StoreService["default"].get().state.scatter.settings.displayCurrency]); var price = parseFloat(_StoreService["default"].get().state.prices[this.uniqueWithChain()][_StoreService["default"].get().state.scatter.settings.displayCurrency]); return "".concat(parseFloat(price / baseTokenPrice).toFixed(10), " ").concat(withSymbol ? systemToken.symbol : ''); } else { return null; } } }, { key: "totalBalance", value: function totalBalance() { if (_BalanceService["default"].totalBalances().totals.hasOwnProperty(this.uniqueWithChain())) { return _BalanceService["default"].totalBalances().totals[this.uniqueWithChain()]; } else { return null; } } }, { key: "symbolClass", value: function symbolClass() { var iconSearch = "".concat(this.blockchain, "-").concat(this.symbol).toLowerCase(); var icons = ['eth-tusd', 'btc-btc', 'eos-eos', 'eth-dai', 'trx-trx', 'eth-eth', 'fio-fio']; return icons.includes(iconSearch) ? "token-icon token-".concat(iconSearch) : null; } }, { key: "truncatedSymbol", value: function truncatedSymbol() { return this.symbol.length > 4 ? this.symbol.substr(0, 4) : this.symbol; } }, { key: "accounts", value: function accounts() { var _this2 = this; return _StoreService["default"].get().state.scatter.keychain.accounts.filter(function (x) { return x.blockchain() === _this2.blockchain && x.network().chainId === _this2.chainId; }).reduce(function (acc, x) { if (!acc.find(function (y) { return y.sendable() === x.sendable(); })) acc.push(x); return acc; }, []); // Problem with doing this is that if the balance checks fail then accounts never show up. // const state = StoreService.get().state; // if(!state.balances) return []; // return Object.keys(state.balances).reduce((acc,accountUnique) => { // if(state.balances[accountUnique].find(token => token.uniqueWithChain() === this.uniqueWithChain())){ // if(!acc.find(x => x.identifiable() === accountUnique)){ // acc.push(state.scatter.keychain.accounts.find(x => x.identifiable() === accountUnique)); // } // } // return acc; // }, []); } }], [{ key: "placeholder", value: function placeholder() { return new Token(); } }, { key: "fromJson", value: function fromJson(json) { var p = Object.assign(this.placeholder(), json); if (!json.hasOwnProperty('name') || !json.name.length) p.name = json.symbol; return p; } }, { key: "fromUnique", value: function fromUnique(unique) { var p = this.placeholder(); var _unique$split = unique.split(':'), _unique$split2 = (0, _slicedToArray2["default"])(_unique$split, 4), blockchain = _unique$split2[0], contract = _unique$split2[1], symbol = _unique$split2[2], chainId = _unique$split2[3]; p.blockchain = blockchain; p.contract = contract; p.symbol = symbol ? symbol.toUpperCase() : 'INVALID_TOKEN'; p.chainId = chainId; p.decimals = _PluginRepository["default"].plugin(blockchain).defaultDecimals(); return p; } }, { key: "sorter", value: function sorter(a, b) { var untouchable = !!b.unusable ? 1 : !!a.unusable ? -1 : 0; var systemTokenUniques = _StoreService["default"].get().state.scatter.networkTokens().map(function (x) { return x.uniqueWithChain(false); }); var isSelfSystem = systemTokenUniques.includes(b.uniqueWithChain(false)) ? 1 : systemTokenUniques.includes(a.uniqueWithChain(false)) ? -1 : 0; return isSelfSystem || untouchable || (b.fiatBalance(false) || 0) - (a.fiatBalance(false) || 0); } }]); return Token; }(); exports["default"] = Token;