Skip to content

Instantly share code, notes, and snippets.

@reazuliqbal
Created September 16, 2018 13:34
Show Gist options
  • Save reazuliqbal/6e93bdcf8309b6a04c1d484d00e41a47 to your computer and use it in GitHub Desktop.
Save reazuliqbal/6e93bdcf8309b6a04c1d484d00e41a47 to your computer and use it in GitHub Desktop.
Save your Curation Rewards sorted by date to a readable JSON file using Node JS
const { Client } = require('dsteem');
const fs = require('fs');
const client = new Client('https://api.steemit.com');
const curationReward = [];
let steemPerMvests = 0;
const getCuration = async (account, start, callback) => {
let lastTrans = start;
await client.database.call('get_account_history', [account, start, (start < 0) ? 5000 : Math.min(start, 5000)])
.then((result) => {
result.reverse();
for (let i = 0; i < result.length; i += 1) {
const trans = result[i];
const { op } = trans[1];
// Checking on curation rewards and pushing them to an array by their date
if (op[0] === 'curation_reward') {
const rdate = new Date(`${trans[1].timestamp}Z`);
const ndate = `${rdate.getMonth() + 1}/${rdate.getDate()}/${rdate.getFullYear()}`;
curationReward.push({ time: ndate, reward: op[1].reward });
}
// Save the ID of the last transaction that was processed.
[lastTrans] = trans;
}
if (lastTrans > 0 && lastTrans !== start) {
// Looping through all the account history
getCuration(account, lastTrans, callback);
} else {
// Grouping the rewards by date and making a new object
callback(curationReward.reduce((acc, obj) => {
const key = obj.time;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj.reward.replace(' VESTS', ''));
return acc;
}, {}));
}
});
};
(async () => {
// Getting STEEM per VEST number to calculate rewards in SP
await client.database.getDynamicGlobalProperties()
.then((r) => {
const totalVestingFund = parseFloat(r.total_vesting_fund_steem.replace(' STEEM', ''));
const totalVestingShares = parseFloat(r.total_vesting_shares.replace(' VESTS', ''));
steemPerMvests = ((totalVestingFund / totalVestingShares) * 1000000);
})
.catch(e => console.log(`Error loading global properties: ${e}`));
const vestToSP = vests => parseFloat(vests / 1000000 * steemPerMvests).toFixed(3);
// Replace `reazuliqbal` with your username
await getCuration('reazuliqbal', -1, (data) => {
const rewards = [];
// Adding all rewards for each day and converting them to SP
Object.keys(data).forEach((d) => {
const reward = data[d].reduce((a, b) => a + parseFloat(b), 0);
rewards.push({ date: d, reward: `${vestToSP(reward)} SP` });
});
// Writing the result to a readable JSON file
fs.writeFileSync('rewards.json', JSON.stringify(rewards, null, 4));
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment