Skip to content

Commit 9a303d2

Browse files
committed
added wallets example
1 parent 959b888 commit 9a303d2

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

examples/rest2/wallets.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict'
2+
3+
const _uniq = require('lodash/uniq')
4+
const _capitalize = require('lodash/capitalize')
5+
const _isFinite = require('lodash/isFinite')
6+
const _isEmpty = require('lodash/isEmpty')
7+
const { prepareAmount } = require('bfx-api-node-util')
8+
const runExample = require('../util/run_example')
9+
10+
module.exports = runExample({
11+
name: 'rest-wallets',
12+
rest: {
13+
env: true,
14+
transform: true
15+
},
16+
17+
params: {
18+
hideZeroBalances: true,
19+
filterByType: false,
20+
filterByCurrency: false,
21+
valueCCY: 'USD'
22+
}
23+
}, async ({ debug, debugTable, rest, params }) => {
24+
const {
25+
valueCCY, hideZeroBalances, filterByType, filterByCurrency
26+
} = params
27+
28+
const symbolForWallet = w => `t${w.currency}${valueCCY}`
29+
30+
debug('fetching balances')
31+
32+
const allWallets = await rest.wallets() // actual balance fetch
33+
const balances = allWallets.filter(w => !( // filter as requested
34+
(hideZeroBalances && +w.balance === 0) ||
35+
(!_isEmpty(filterByType) && (w.type.toLowerCase() !== filterByType.toLowerCase())) ||
36+
(!_isEmpty(filterByCurrency) && (w.currency.toLowerCase() !== filterByCurrency.toLowerCase()))
37+
)).map(w => ({
38+
...w,
39+
currency: w.currency.toUpperCase(),
40+
inValueCurrency: w.currency.toUpperCase() === valueCCY
41+
}))
42+
43+
if (balances.length === 0) {
44+
return debug('no wallets match provided filters')
45+
}
46+
47+
debug('found %d balances', balances.length)
48+
49+
// Pull in ticker data for balances which are not in the requested value ccy
50+
// Balance in BTC, value in USD -> We need to fetch tBTCUSD (last price)
51+
const lastPrices = {}
52+
const balancesToConvert = balances.filter(w => w.currency !== valueCCY)
53+
const symbols = _uniq(balancesToConvert.map(symbolForWallet))
54+
55+
if (symbols.length > 0) {
56+
debug('fetching tickers for: %s', symbols.join(', '))
57+
const tickers = await rest.tickers(symbols)
58+
tickers.forEach(({ symbol, lastPrice }) => (lastPrices[symbol] = +lastPrice))
59+
}
60+
61+
let totalValue = 0
62+
const rows = balances.map(({ currency, type, balance, balanceAvailable }) => {
63+
const value = currency !== valueCCY
64+
? (lastPrices[symbolForWallet({ currency })] * +balance) || 0
65+
: +balance
66+
67+
totalValue += value
68+
69+
return [
70+
_capitalize(type),
71+
currency,
72+
prepareAmount(balance),
73+
prepareAmount(balanceAvailable),
74+
75+
...(_isFinite(value) ? [
76+
prepareAmount(value),
77+
currency !== valueCCY
78+
? prepareAmount(lastPrices[symbolForWallet({ currency })])
79+
: 1
80+
] : [
81+
'-',
82+
'-'
83+
])
84+
]
85+
})
86+
87+
debugTable({
88+
rows,
89+
headers: [
90+
'Type', 'Symbol', 'Total', 'Available', `Value (${valueCCY})`,
91+
`Unit Price (${valueCCY})`
92+
]
93+
})
94+
95+
debug('total value: %d %s', prepareAmount(totalValue), valueCCY)
96+
})

0 commit comments

Comments
 (0)