Created
April 2, 2019 05:30
-
-
Save reazuliqbal/42bd91c7ff644730528188ae812d78cf to your computer and use it in GitHub Desktop.
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
const { Client, Asset } = require('dsteem'); | |
const table = require('markdown-table'); | |
const client = new Client('https://anyx.io', { timeout: 30000 }); | |
const users = []; | |
const refunded = memo => (new RegExp(/Unable to purchase|refund/)).test(memo); | |
const getTransactions = async (account, start, callback) => { | |
let trxNum = start; | |
await client.database.call('get_account_history', [account, start, (start < 0) ? 1000 : Math.min(start, 1000)]) | |
.then((history) => { | |
history.reverse(); | |
const transfers = history.filter(h => h[1].trx_id !== '0000000000000000000000000000000000000000' | |
&& h[1].op[0] === 'transfer'); | |
for (let i = 0; i < transfers.length; i += 1) { | |
const trx = transfers[i]; | |
const { op } = trx[1]; | |
const data = op[1]; | |
if (!data.memo.startsWith('sm_market_sale')) { | |
const user = users.find(u => u.name === data.from || u.name === data.to); | |
const amount = Asset.from(data.amount); | |
if (user) { | |
if (refunded(data.memo)) { | |
if (amount.symbol === 'SBD') { | |
user.sent.sbd.amount -= amount.amount; | |
} else { | |
user.sent.steem.amount -= amount.amount; | |
} | |
} else { | |
if (data.from === account) { | |
if (amount.symbol === 'SBD') { | |
user.received.sbd.amount += amount.amount; | |
} else { | |
user.received.steem.amount += amount.amount; | |
} | |
} | |
if (data.to === account && data.memo.startsWith('sm_market_purchase')) { | |
if (amount.symbol === 'SBD') { | |
user.sent.sbd.amount += amount.amount; | |
} else { | |
user.sent.steem.amount += amount.amount; | |
} | |
} | |
} | |
} else { | |
const newUser = { | |
name: (data.from === account) ? data.to : data.from, | |
received: { | |
sbd: Asset.fromString('0.000 SBD'), | |
steem: Asset.fromString('0.000 STEEM'), | |
}, | |
sent: { | |
sbd: Asset.fromString('0.000 SBD'), | |
steem: Asset.fromString('0.000 STEEM'), | |
}, | |
}; | |
if (data.from === account) { | |
if (amount.symbol === 'SBD') { | |
newUser.received.sbd.amount += amount.amount; | |
} else { | |
newUser.received.steem.amount += amount.amount; | |
} | |
} | |
if (data.to === account && data.memo.startsWith('sm_market_purchase')) { | |
if (amount.symbol === 'SBD') { | |
newUser.sent.sbd.amount += amount.amount; | |
} else { | |
newUser.sent.steem.amount += amount.amount; | |
} | |
} | |
users.push(newUser); | |
} | |
} | |
[trxNum] = trx; | |
} | |
if (trxNum > 0 && trxNum !== start) { | |
getTransactions(account, trxNum, callback); | |
} else { | |
callback(users); | |
} | |
}); | |
}; | |
(async () => { | |
await getTransactions('sm-market', -1, async (data) => { | |
const tableData = data | |
.filter(d => d.received.sbd.amount > 0 || d.received.steem.amount > 0) | |
.map(d => ([ | |
`@${d.name}`, | |
`${d.sent.sbd.toString()}, ${d.sent.steem.toString()}`, | |
`${d.received.sbd.toString()}, ${d.received.steem.toString()}`, | |
])); | |
const heading = [ | |
['User', 'Spent', 'Cashback'], | |
]; | |
const contents = [...heading, ...tableData]; | |
console.log(table(contents)); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment