Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
snwolak committed Nov 5, 2018
1 parent 3da19c9 commit 4a79864
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -4,7 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

delegations.json*
# Runtime data
pids
*.pid
Expand Down
90 changes: 90 additions & 0 deletions index.js
@@ -0,0 +1,90 @@
const steem = require('steem')
const fs = require('fs')
let store = [];
async function start() {
setInterval(async () => {
await getData().catch(err => console.log(err))
console.log("Delegations downloaded.", Date.now())
return null;
}, 300000)
return null;
}
start()
async function getData() {
let data;
//Number of the operation, it has to be the same or higher than limit
let from = store === undefined || store.length === 0 ? 10000000 : store[store.length - 1 ][0];
await steem.api.getAccountHistoryAsync('knacksteem.org', from, from < 1000 ? from : 1000).then(res => {
const sorted = res.sort(function(a, b){
const x = a[1].block;
const y = b[1].block;
if (x < y) {return 1;}
if (x > y) {return -1;}
return 0;
});
data = sorted;

return res;
}).catch(err => {
return err;
});

if(store.length === 0) {
store = data
await getData();
return null;

//if pulled data is the same function stops here
} else if(data[data.length - 1][1].block === store[store.length - 1][1].block) {
let arr = [];
//filtering to get only delegations
const filtered = store.filter(op => {
return op[1].op[0] === 'delegate_vesting_shares';
});
filtered.map(sponsor => {
let obj = {
delegator: sponsor[1].op[1].delegator,
vesting_shares: Number(sponsor[1].op[1].vesting_shares.split(' ')[0]),
block: sponsor[1].block,
trx_id: sponsor[1].trx_id
};
//checking if delegator already exists in array
const check = arr.find(x => x.delegator === obj.delegator);
if(check !== undefined) {

if(check.block < sponsor[1].block) {
//if delegation is newer it will replace the old one
const filtered = arr.filter(item => {
return item.trx_id !== check.trx_id;
});
arr = filtered;
arr.push(obj);
}
} else {
arr.push(obj);
}

});
//deleting delegations with zero vests
const filterZeroVesting = arr.filter(item => {
return item.vesting_shares !== 0;
});

const sorted = filterZeroVesting.sort(function(a, b){
const x = a.vesting_shares;
const y = b.vesting_shares;
if (x < y) {return 1;}
if (x > y) {return -1;}
return 0;
});


fs.writeFile("delegations.json", JSON.stringify(sorted), err => console.log(err))
return null;
} else {
store = store.concat(data)
await getData();
return null;
}

}

0 comments on commit 4a79864

Please sign in to comment.