@steemit/steem-js
Version:
JavaScript library for the Steem blockchain
163 lines (162 loc) • 6.38 kB
JavaScript
;
var _get = _interopRequireDefault(require("lodash/get"));
var _ecc = require("./auth/ecc");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
module.exports = steemAPI => {
function numberWithCommas(x) {
return x.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function vestingSteem(account, gprops) {
const vests = parseFloat(account.vesting_shares.split(" ")[0]);
const total_vests = parseFloat(gprops.total_vesting_shares.split(" ")[0]);
const total_vest_steem = parseFloat(gprops.total_vesting_fund_steem.split(" ")[0]);
const vesting_steemf = total_vest_steem * (vests / total_vests);
return vesting_steemf;
}
function processOrders(open_orders, assetPrecision) {
const sbdOrders = !open_orders ? 0 : open_orders.reduce((o, order) => {
if (order.sell_price.base.indexOf("SBD") !== -1) {
o += order.for_sale;
}
return o;
}, 0) / assetPrecision;
const steemOrders = !open_orders ? 0 : open_orders.reduce((o, order) => {
if (order.sell_price.base.indexOf("STEEM") !== -1) {
o += order.for_sale;
}
return o;
}, 0) / assetPrecision;
return {
steemOrders,
sbdOrders
};
}
function calculateSaving(savings_withdraws) {
let savings_pending = 0;
let savings_sbd_pending = 0;
savings_withdraws.forEach(withdraw => {
const [amount, asset] = withdraw.amount.split(" ");
if (asset === "STEEM") savings_pending += parseFloat(amount);else {
if (asset === "SBD") savings_sbd_pending += parseFloat(amount);
}
});
return {
savings_pending,
savings_sbd_pending
};
}
function pricePerSteem(feed_price) {
let price_per_steem = undefined;
const {
base,
quote
} = feed_price;
if (/ SBD$/.test(base) && / STEEM$/.test(quote)) {
price_per_steem = parseFloat(base.split(" ")[0]) / parseFloat(quote.split(" ")[0]);
}
return price_per_steem;
}
function estimateAccountValue(account, {
gprops,
feed_price,
open_orders,
savings_withdraws,
vesting_steem
} = {}) {
const promises = [];
const username = account.name;
const assetPrecision = 1000;
let orders, savings;
if (!vesting_steem || !feed_price) {
if (!gprops || !feed_price) {
promises.push(steemAPI.getStateAsync(`/@${username}`).then(data => {
gprops = data.props;
feed_price = data.feed_price;
vesting_steem = vestingSteem(account, gprops);
}));
} else {
vesting_steem = vestingSteem(account, gprops);
}
}
if (!open_orders) {
promises.push(steemAPI.getOpenOrdersAsync(username).then(open_orders => {
orders = processOrders(open_orders, assetPrecision);
}));
} else {
orders = processOrders(open_orders, assetPrecision);
}
if (!savings_withdraws) {
promises.push(steemAPI.getSavingsWithdrawFromAsync(username).then(savings_withdraws => {
savings = calculateSaving(savings_withdraws);
}));
} else {
savings = calculateSaving(savings_withdraws);
}
return Promise.all(promises).then(() => {
let price_per_steem = pricePerSteem(feed_price);
const savings_balance = account.savings_balance;
const savings_sbd_balance = account.savings_sbd_balance;
const balance_steem = parseFloat(account.balance.split(" ")[0]);
const saving_balance_steem = parseFloat(savings_balance.split(" ")[0]);
const sbd_balance = parseFloat(account.sbd_balance);
const sbd_balance_savings = parseFloat(savings_sbd_balance.split(" ")[0]);
let conversionValue = 0;
const currentTime = new Date().getTime();
(account.other_history || []).reduce((out, item) => {
if ((0, _get.default)(item, [1, "op", 0], "") !== "convert") return out;
const timestamp = new Date((0, _get.default)(item, [1, "timestamp"])).getTime();
const finishTime = timestamp + 86400000 * 3.5; // add 3.5day conversion delay
if (finishTime < currentTime) return out;
const amount = parseFloat((0, _get.default)(item, [1, "op", 1, "amount"]).replace(" SBD", ""));
conversionValue += amount;
}, []);
const total_sbd = sbd_balance + sbd_balance_savings + savings.savings_sbd_pending + orders.sbdOrders + conversionValue;
const total_steem = vesting_steem + balance_steem + saving_balance_steem + savings.savings_pending + orders.steemOrders;
return (total_steem * price_per_steem + total_sbd).toFixed(2);
});
}
function createSuggestedPassword() {
const PASSWORD_LENGTH = 32;
const privateKey = _ecc.key_utils.get_random_key();
return privateKey.toWif().substring(3, 3 + PASSWORD_LENGTH);
}
return {
reputation: function (reputation, decimal_places = 0) {
if (reputation == 0) return 25;
if (!reputation) return reputation;
let neg = reputation < 0;
let rep = String(reputation);
rep = neg ? rep.substring(1) : rep;
let v = Math.log10((rep > 0 ? rep : -rep) - 10) - 9;
v = neg ? -v : v;
v = v * 9 + 25;
if (decimal_places > 0) {
return +(Math.round(v + "e+" + decimal_places) + "e-" + decimal_places);
}
return parseInt(v);
},
vestToSteem: function (vestingShares, totalVestingShares, totalVestingFundSteem) {
return parseFloat(totalVestingFundSteem) * (parseFloat(vestingShares) / parseFloat(totalVestingShares));
},
commentPermlink: function (parentAuthor, parentPermlink) {
const timeStr = new Date().toISOString().replace(/[^a-zA-Z0-9]+/g, "").toLowerCase();
parentPermlink = parentPermlink.replace(/(-\d{8}t\d{9}z)/g, "");
let permLink = "re-" + parentAuthor + "-" + parentPermlink + "-" + timeStr;
if (permLink.length > 255) {
// pay respect to STEEMIT_MAX_PERMLINK_LENGTH
permLink.substr(permLink.length - 255, permLink.length);
}
// permlinks must be lower case and not contain anything but
// alphanumeric characters plus dashes
return permLink.toLowerCase().replace(/[^a-z0-9-]+/g, "");
},
amount: function (amount, asset) {
return amount.toFixed(3) + " " + asset;
},
numberWithCommas,
vestingSteem,
estimateAccountValue,
createSuggestedPassword,
pricePerSteem
};
};