-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
88 lines (76 loc) · 2.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const Client = require('rippled-ws-client')
const {parseBalanceChanges} = require('ripple-lib-transactionparser')
const app = async (account, cb, returnTx) => {
const display = result => {
if (result?.transactions) {
result?.transactions.forEach(r => {
const {tx, meta} = r
let direction = 'other'
if (tx?.Account === account) direction = 'sent'
if (tx?.Destination === account) direction = 'received'
const moment = (new Date((tx.date + 946684800) * 1000)).toISOString()
const balanceChanges = parseBalanceChanges(meta)
if (Object.keys(balanceChanges).indexOf(account) > -1) {
const mutations = balanceChanges[account]
mutations.forEach(mutation => {
const currency = mutation.counterparty === ''
? 'XRP'
: `${mutation.counterparty}.${mutation.currency}`
const isFee = direction === 'sent' && Number(mutation.value) * -1 * 1000000 === Number(tx?.Fee)
? 1
: 0
const fee = direction === 'sent'
? Number(tx?.Fee) / 1000000 * -1
: 0
cb({
ledger: tx.ledger_index,
direction: direction,
txtype: tx.TransactionType,
date: moment,
currency: currency,
amount: mutation.value,
is_fee: isFee,
fee: fee,
hash: tx.hash,
_tx: returnTx ? tx : undefined,
_meta: returnTx ? meta : undefined
})
})
}
})
}
}
const client = await new Client('wss://xrplcluster.com', {
NoUserAgent: true
})
const getMore = async marker => {
const result = await client.send({
command: 'account_tx',
account,
limit: 10,
marker
})
display(result)
return result?.marker
}
let proceed = await getMore()
while (proceed) {
proceed = await getMore(proceed)
}
client.close()
}
const fields = [
'ledger',
'direction',
'txtype',
'date',
'currency',
'amount',
'is_fee',
'fee',
'hash'
]
module.exports = {
app,
fields
}