Skip to content

Commit

Permalink
Merge pull request #175 from steemit/suggestPassword-formatter
Browse files Browse the repository at this point in the history
Added Suggest password formatter
  • Loading branch information
Fabien committed Jun 21, 2017
2 parents 3afec8a + de3e490 commit ef4db1d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 91 deletions.
37 changes: 37 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Broadcast API](#broadcast-api)
- [Broadcast](#broadcast)
- [Auth](#auth)
- [Formatter](#formatter)

# Install
```
Expand Down Expand Up @@ -884,3 +885,39 @@ steem.auth.wifToPublic(privWif);
```
steem.auth.signTransaction(trx, keys);
```

# Formatter

### Create Suggested Password
```
var password = steem.formatter.createSuggestedPassword();
console.log(password);
// => 'GAz3GYFvvQvgm7t2fQmwMDuXEzDqTzn9'
```

### Comment Permlink
```
var parentAuthor = 'ned';
var parentPermlink = 'a-selfie';
var commentPermlink = steem.formatter.commentPermlink(parentAuthor, parentPermlink);
console.log(commentPermlink);
// => 're-ned-a-selfie-20170621t080403765z'
```

### Estimate Account Value
```
var steemPower = steem.formatter.estimateAccountValue(account);
```

### Reputation
```
var reputation = steem.formatter.reputation(3512485230915);
console.log(reputation);
// => 56
```

### Vest To Steem
```
var steemPower = steem.formatter.vestToSteem(vestingShares, totalVestingShares, totalVestingFundSteem);
console.log(steemPower);
```
190 changes: 99 additions & 91 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from "lodash/get";
import { key_utils } from "./auth/ecc";

module.exports = steemAPI => {
function numberWithCommas(x) {
Expand Down Expand Up @@ -50,6 +51,102 @@ module.exports = steemAPI => {
return { savings_pending, savings_sbd_pending };
}

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 = undefined;
const { base, quote } = feed_price;
if (/ SBD$/.test(base) && / STEEM$/.test(quote))
price_per_steem = parseFloat(base.split(" ")[0]);
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 (get(item, [1, "op", 0], "") !== "convert") return out;

const timestamp = new Date(get(item, [1, "timestamp"])).getTime();
const finishTime = timestamp + 86400000 * 3.5; // add 3.5day conversion delay
if (finishTime < currentTime) return out;

const amount = parseFloat(
get(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 = key_utils.get_random_key();
return privateKey.toWif().substring(3, 3 + PASSWORD_LENGTH);
}

return {
reputation: function(reputation) {
if (reputation == null) return reputation;
Expand Down Expand Up @@ -95,96 +192,7 @@ module.exports = steemAPI => {
},
numberWithCommas,
vestingSteem,
estimateAccountValue: 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 = undefined;
const { base, quote } = feed_price;
if (/ SBD$/.test(base) && / STEEM$/.test(quote))
price_per_steem = parseFloat(base.split(" ")[0]);
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 (get(item, [1, "op", 0], "") !== "convert") return out;

const timestamp = new Date(get(item, [1, "timestamp"])).getTime();
const finishTime = timestamp + 86400000 * 3.5; // add 3.5day conversion delay
if (finishTime < currentTime) return out;

const amount = parseFloat(
get(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);
});
}
estimateAccountValue,
createSuggestedPassword
};
};

0 comments on commit ef4db1d

Please sign in to comment.