-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply utility, Rename methods, Reduce dependencies
- 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
1 parent
72cc787
commit ce60461
Showing
14 changed files
with
178 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.