Skip to content

Commit

Permalink
Fix & Improve memo detecting
Browse files Browse the repository at this point in the history
  • Loading branch information
gigatoride committed Sep 24, 2018
1 parent 962f46d commit 71686b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ inquirer.prompt(questions).then(answers => {
break;
case 'memos received': // In case of `memos received`
detector.memo.detect(answers.wif, answers.username, (msg) => { // Start monitoring blockchain by user inputs
if (Array.isArray(msg)) {
let [from, amount, memo] = msg; // Convert object into local variables.
if (amount.includes('0.001')) { // Amount of transaction 0.001 sbd or detector
log(chalk.cyanBright(from) + ' sent you a memo: ' + chalk.magentaBright(memo)); // Log it & dont view that tiny amount
} else {
log(chalk.cyanBright(from) + ' sent you a memo: ' + chalk.magentaBright(memo) + ' with an amount of ' + chalk.yellowBright(amount)); // Log it
if (amount.includes('0.001')) { // Amount of transaction 0.001 sbd or detector
log(chalk.cyanBright(from) + ' sent you a memo: ' + chalk.magentaBright(memo)); // Log it & dont view that tiny amount
} else {
log(chalk.cyanBright(from) + ' sent you a memo: ' + chalk.magentaBright(memo) + ' with an amount of ' + chalk.yellowBright(amount)); // Log it
}
}
})
break;
Expand Down
26 changes: 15 additions & 11 deletions src/lib/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,32 @@ module.exports = {
utils.isMemoKey(memoKey, username).then((isMemo) => {
if (!isMemo) throw new Error('Memo key is not valid.') // Check username
})
let latest_timestamp;
this.run = true; // Start updating
const update = () => {
if (!this.run) return; // If run is false stop updating.
steem.api.getAccountHistoryAsync(username, -1, 100).then(res => {
let transfer;
// Reserve for the first activity becomes the last, and the last activity becomes the first.
const received = res.reverse().find(obj => obj[1].op[0] === 'transfer' && obj[1].op[1].to === username)[1].op[1]
const received = res.reverse().find(obj => obj[1].op[0] === 'transfer' && obj[1].op[1].to === username)
const {
from, // Sender
to, // Receiver
amount, // Amount of SBD/STEEM
memo // Transaction MEMO
} = received; // Convert the matches object into local variables
if (memo !== '' && memoKey !== null) { // Check if private memo
if (memo.charAt(0) === '#') {
let privateMemo = utils.isMemo(memoKey, memo);
transfer = (privateMemo !== false) ? [from, amount, privateMemo] : [from, amount, 'Wrong Memo Key.'];
} else
transfer = [from, amount, '[Private Memo]']; // It is private memo in case of (no wif key)
} else
transfer = [from, amount, memo]; // It is public memo
callback(transfer);
} = received[1].op[1]; // Convert the matches object into local variables
if (latest_timestamp !== received[1].timestamp) {
latest_timestamp = received[1].timestamp;
if (memo !== '') // Check if private memo
if (memo.charAt(0) === '#' && memoKey !== null) {
let privateMemo = utils.isMemo(memoKey, memo);
transfer = (privateMemo !== false) ? [from, amount, privateMemo] : [from, amount, 'Wrong Memo Key.'];
} else
transfer = [from, amount, memo]; // It is private memo in case of (no wif key)
else
transfer = [from, amount, memo]; // It is public memo
callback(transfer);
}
Promise.delay(300).then(update).catch(callback); // Promise for updating callbacks and error handler.
}).catch(callback);
}
Expand Down

0 comments on commit 71686b1

Please sign in to comment.