Skip to content

Commit

Permalink
Apply utility, Rename methods, Reduce dependencies
Browse files Browse the repository at this point in the history
- Add isValidAccountNames to utils for shorter code
- Add get to method names
- Apply built-in readable streams utility
- Describe if/else statments with variables
- Improve code readability
- Merge exchanges to config with the ability to customize it
- Add optional/null signs for jsdoc
- Remove most of dependencies
  • Loading branch information
gigatoride committed Jun 13, 2019
1 parent 72cc787 commit ce60461
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 209 deletions.
18 changes: 14 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"url": "https://api.steemit.com",
"dapps": [
{
"account": "utopian-io"
}
"global_blacklist_api": "blacklist.usesteem.com",
"exchanges": [
"blocktrades",
"deepcrypto8",
"huobi-pro",
"bittrex",
"bithumb.live",
"openledger-dex",
"changelly",
"onepagex",
"poloniex",
"shapeshiftio",
"hitbtc-exchange",
"upbituserwallet"
]
}
14 changes: 0 additions & 14 deletions exchanges.json

This file was deleted.

12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,18 @@
"monitor",
"alerts"
],
"bin": {
"steemradar": "./bin"
},
"author": "gigatoride",
"license": "MIT",
"bugs": {
"url": "https://github.com/gigatoride/steemradar.js/issues"
},
"homepage": "https://github.com/gigatoride/steemradar.js#readme",
"dependencies": {
"async-iterator-to-stream": "^1.1.0",
"axios": "^0.18.0",
"bad-words": "^1.6.3",
"chalk": "^2.4.1",
"inquirer": "^6.2.0",
"node-utopian-rocks": "^0.1.7",
"profanities": "^2.11.0",
"steem": "^0.7.5"
},
"devDependencies": {
"documentation": "^11.0.0",
"documentation": "^11.0.1",
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
Expand Down
17 changes: 7 additions & 10 deletions src/blockchain/accountActivity.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
const iteratorStream = require('async-iterator-to-stream');
const steem = require('steem');
const { validateAccountName } = require('steem').utils;
const { sleep } = require('../utils');
const { sleep, isValidAccountNames, readableStream } = require('../utils');

/**
* Scan account database recent activity
* @param {Array} accounts - steem account names
* @param {?Array=} names - steem account names
* @returns {Stream.<Object>} - transaction
* @memberof Scan.blockchain
*/
function accountActivity(accounts) {
if (accounts && !accounts.every(account => validateAccountName(account) === null))
throw new Error('accounts are not valid.');
function getAccountActivity(names) {
if (names && isValidAccountNames(names)) throw new Error('An account name is not valid.');

let latestCatch = [];

const iterator = async function * (ms = 4 * 1000) {
while (true) {
const history = await steem.api.getAccountsAsync(accounts);
const history = await steem.api.getAccountAsync(names);
const changes = history.every((account, i) => {
return !Object.is(account, latestCatch[i]);
});
Expand All @@ -29,7 +26,7 @@ function accountActivity(accounts) {
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = accountActivity;
module.exports = getAccountActivity;
9 changes: 4 additions & 5 deletions src/blockchain/accountCounter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const iteratorStream = require('async-iterator-to-stream');
const api = require('../helper');
const { sleep } = require('../utils');
const { sleep, readableStream } = require('../utils');

/**
* Stream account counter
* @returns {Stream.<Int>} - account number
* @memberof Scan.blockchain
*/
function accountCounter() {
function getAccountCounter() {
let latestCatch;
const iterator = async function * (ms = 700) {
while (true) {
Expand All @@ -21,7 +20,7 @@ function accountCounter() {
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = accountCounter;
module.exports = getAccountCounter;
43 changes: 20 additions & 23 deletions src/blockchain/accountMentions.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
const iteratorStream = require('async-iterator-to-stream');
const { validateAccountName } = require('steem').utils;
const blockchain = require('../helper');
const { sleep } = require('../utils');
const api = require('../helper');
const { sleep, isValidAccountNames, readableStream } = require('../utils');

/**
* Scan for mentions
* @param {Array} accounts - steem account names
* @param {?Array=} accounts - steem account names
* @returns {Stream.<Object>} - transaction
* @memberof Scan.blockchain
*/
function accountMentions(accounts) {
if (accounts && !accounts.every(account => validateAccountName(account) === null))
throw new Error('An account name is not valid or exist.');
function getAccountMentions(accounts) {
if (accounts && isValidAccountNames(accounts)) throw new Error('An account name is not valid or exist.');

let latestCatch;
const iterator = async function * (ms = 700) {
while (true) {
const transactions = await blockchain.getTransactions();
const transactions = await api.getTransactions();
for (const trx of transactions) {
const [txType, txData] = trx.operations[0];
if (txType === 'comment') {
const mentionAccounts = txData.body.match(/\B@[a-z0-9-.]+/gm);
const mentionTargets = accounts
? accounts.map(name => {
return `@${name}`;
})
: [];
const setMentions = accounts ? mentionAccounts.some(name => mentionTargets.includes(name)) : true;
if (mentionAccounts && setMentions && trx.transaction_id !== latestCatch) {
latestCatch = trx.transaction_id;
yield trx;
}
const isContent = txType === 'comment';
const isUnique = trx.transaction_id !== latestCatch;
const mentionAccounts = txData.body.match(/\B@[a-z0-9-.]+/gm);
const mentionTargets = accounts
? accounts.map(name => {
return `@${name}`;
})
: [];
const setMentions = accounts ? mentionAccounts.some(name => mentionTargets.includes(name)) : true;
if (isUnique && isContent && mentionAccounts && setMentions) {
latestCatch = trx.transaction_id;
yield trx;
}
}
await sleep(ms);
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = accountMentions;
module.exports = getAccountMentions;
19 changes: 8 additions & 11 deletions src/blockchain/accountSecurity.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
const iteratorStream = require('async-iterator-to-stream');
const blockchain = require('../helper');
const { validateAccountName } = require('steem').utils;
const { sleep } = require('../utils');
const api = require('../helper');
const { sleep, isValidAccountNames, readableStream } = require('../utils');

/**
* Scan for security threats or account changes/transfers
* @param {Array} accounts - steem account names
* @param {?Array=} accounts - steem account names
* @returns {Stream.<Object>} - transaction
* @access public
* @memberof Scan.blockchain
*/
function accountSecurity(accounts) {
if (!accounts.every(account => validateAccountName(account) === null))
function getAccountSecurity(accounts) {
if (isValidAccountNames(accounts))
throw new Error('An account name is not valid or exist.');

let latestCatch;
const iterator = async function * (ms = 700) {
while (true) {
const operationTypes = ['account_update', 'change_recovery_account', 'transfer_from_savings'];
const transactions = await blockchain.getTransactions();
const transactions = await api.getTransactions();

for (const trx of transactions) {
const [txType, txData] = trx.operations[0];
Expand All @@ -34,7 +31,7 @@ function accountSecurity(accounts) {
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = accountSecurity;
module.exports = getAccountSecurity;
13 changes: 6 additions & 7 deletions src/blockchain/blacklist.js → src/blockchain/blacklisted.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const iteratorStream = require('async-iterator-to-stream');
const blockchain = require('./../helper');
const { sleep } = require('./../utils');
const { isBlacklisted } = require('../utils');
const blockchain = require('../helper');
const { isBlacklisted, readableStream, sleep } = require('../utils');

/**
* Scan for any blacklisted user on blockchain latest blocks.
* @returns {Stream.<Object>} - transaction
* @memberof Scan.blockchain
*/
function blacklist() {
function getBlacklisted() {
const iterator = async function * (ms = 700) {
let latestCatch;
while (true) {
Expand All @@ -28,6 +26,7 @@ function blacklist() {
break;
}
if (account && trx.transaction_id !== latestCatch) {
latestCatch = trx.transaction_id;
const res = await isBlacklisted(account);
if (res.blacklisted.length) yield trx;
}
Expand All @@ -36,7 +35,7 @@ function blacklist() {
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = blacklist;
module.exports = getBlacklisted;
16 changes: 8 additions & 8 deletions src/blockchain/feedPublish.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const iteratorStream = require('async-iterator-to-stream');
const blockchain = require('../helper');
const { sleep } = require('../utils');
const api = require('../helper');
const { sleep, readableStream } = require('../utils');

/**
* Stream price feed
* @returns {Stream.<Object>} - transaction
* @memberof Scan.blockchain
*/
function feedPublish() {
function getFeedPublish() {
let latestCatch;
const iterator = async function * (ms = 700) {
while (true) {
const transactions = await blockchain.getTransactions();
const transactions = await api.getTransactions();
for (const trx of transactions) {
const [txType] = trx.operations[0];
if (txType === 'feed_publish' && trx.transaction_id !== latestCatch) {
const isUnique = trx.transaction_id !== latestCatch;
if (isUnique & (txType === 'feed_publish')) {
latestCatch = trx.transaction_id;
yield trx;
}
Expand All @@ -23,7 +23,7 @@ function feedPublish() {
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = feedPublish;
module.exports = getFeedPublish;
34 changes: 17 additions & 17 deletions src/blockchain/fundsTracker.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
const iteratorStream = require('async-iterator-to-stream');
const api = require('./../helper');
const { percentage } = require('../utils');
const exchanges = require('../../exchanges.json');
const { percentage, readableStream } = require('../utils');
const config = require('../../config.json');
/**
* Tracking funds until it goes out of blockchain by 3rd party exchange
* @param {String} username - steem account to be tracked.
* @param {String} trxId - transaction Id for the transfer
* @param {Object} opts - options
* @param {Boolean} opts.multi - multi tracking
* @param {!String} name - steem account to be tracked.
* @param {?String} trxId - transaction Id for the transfer
* @param {?Object} opts - options
* @param {!Boolean} opts.multi - multi tracking
* @returns {Stream.<Object>} - transaction
* @memberof Scan.blockchain
*/
function fundsTracker(username, trxId, opts = {}) {
function getFundsTracker(name, trxId, opts = {}) {
const multiTracking = opts.multi;
const exchanges = opts.exchanges || config.exchanges;

const getTransfer = transactions => {
return transactions.reverse().find(trx => {
if (trxId) return trx[1].op[0] === 'transfer' && trx[1].trx_id === trxId;
else return trx[1].op[0] === 'transfer' && trx[1].op[1].from === username;
else return trx[1].op[0] === 'transfer' && trx[1].op[1].from === name;
});
};

let latestCatch;
const iterator = async function * () {
let out = false;
if (!multiTracking) {
const history = await api.getRecentAccountTransactions(username);
const history = await api.getRecentAccountTransactions(name);
const follow = getTransfer(history);
let target = {
trxId: follow[1].trx_id,
username: follow[1].op[1].to,
name: follow[1].op[1].to,
amount: follow[1].op[1].amount,
timestamp: follow[1].timestamp,
percentage: 100
};
const fullAmount = target.amount;
if (exchanges.includes(target.username)) yield target;
if (exchanges.includes(target.name)) yield target;
else {
yield target;
while (!out) {
const transactions = api.streamTransactions();
for (const trx of transactions) {
const [txType, txData] = trx[0];
if (txType === 'transfer' && txData.from === target.username && latestCatch !== trx.transaction_id) {
if (txType === 'transfer' && txData.from === target.name && latestCatch !== trx.transaction_id) {
latestCatch = trx.transaction_id;
target = {
trxId: trx.transaction_id,
username: txData.to,
name: txData.to,
amount: txData.amount,
timestamp: trx.timestamp,
percentage: percentage(parseFloat(txData.amount), parseFloat(fullAmount))
};
yield trx;
if (target && !exchanges.includes(target.username)) out = true;
if (target && !exchanges.includes(target.name)) out = true;
}
}
}
}
}
};

return iteratorStream.obj(iterator());
return readableStream(iterator());
}

module.exports = fundsTracker;
module.exports = getFundsTracker;
Loading

0 comments on commit ce60461

Please sign in to comment.