Created
April 13, 2018 19:22
-
-
Save mattbailey/8295b76295443130fc8e0894c20e8eda to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Binance = require('binance') | |
| const fetch = require('node-fetch') | |
| const binance = new Binance.BinanceRest({ | |
| key: '[REDACTED]', | |
| secret: '[REDACTED]' | |
| }) | |
| function precisionRound(number, precision) { | |
| const factor = Math.pow(10, precision) | |
| return Math.round(number * factor) / factor | |
| } | |
| async function getvalue() { | |
| let account | |
| let prices | |
| try { | |
| account = await binance.account() | |
| prices = await binance.tickerPrice() | |
| } catch (e) { | |
| console.error(e) | |
| } | |
| const pricemap = prices.reduce( | |
| (state, next) => | |
| Object.assign(state, { [next.symbol]: parseFloat(next.price) }), | |
| {} | |
| ) | |
| const wallet = account.balances | |
| .map(asset => | |
| Object.assign({ asset: asset.asset }, { free: parseFloat(asset.free) }) | |
| ) | |
| .filter(asset => asset.free > 0) | |
| .map(asset => { | |
| const price = pricemap[`${asset.asset}BTC`] || 1 | |
| return { ...asset, ...{ btc: price * asset.free } } | |
| }) | |
| const btc = wallet.reduce((acc, asset) => asset.btc + acc, 0) | |
| const value = pricemap.BTCUSDT | |
| const usd = Math.round(value * btc) | |
| return { btc, usd } | |
| } | |
| function slack(value) { | |
| const body = `${precisionRound(value.btc, 5)} BTC / ${value.usd} USD` | |
| fetch( | |
| 'https://hooks.slack.com/services/[REDACTED]', | |
| { method: 'POST', body: JSON.stringify({ text: body }) } | |
| ) | |
| } | |
| async function main() { | |
| const value = await getvalue() | |
| slack(value) | |
| } | |
| try { | |
| main() | |
| } catch (e) { | |
| console.error(e) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment