-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBalance.js
48 lines (35 loc) · 1.68 KB
/
getBalance.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
var Env = require('./config/config.js');
var Gdax = require('gdax');
var authedClient = new Gdax.AuthenticatedClient(Env.ACCESS_KEY, Env.SECRET_KEY, Env.PASSPHRASE_KEY);
var winston = require('winston');
// Un-comment line below if you want to write to file
// winston.add(winston.transports.File, { filename: 'balancer.log' });
winston.log('info', "Calculating balance.");
// Get the account
authedClient.getAccounts(function(error, response, accounts){
// Get the ticker for the current BTC price
authedClient.getProductTicker(function(error, response, data){
if (error || response.statusCode != 200) {
winston.log('info', "Error getting ticker: " + error);
winston.log('info', "Response: " + JSON.stringify(response));
return;
}
//winston.log('info', JSON.stringify(data));
var currentPrice = Number(data.price);
winston.log('info', "Current market price: $" + currentPrice);
var balance = 0;
for(var i = 0; i < accounts.length; i++){
if( accounts[i].currency === 'BTC'){
var bitcoinBalance = Number(accounts[i].balance) * Number(currentPrice);
balance += bitcoinBalance;
winston.log('info', "BTC Balance: " + bitcoinBalance.toFixed(2));
}
if( accounts[i].currency === 'USD'){
var usdBalance = Number(accounts[i].balance);
balance += usdBalance;
winston.log('info', "USD: " + usdBalance.toFixed(2));
}
}
winston.log('info', "Total: " + balance.toFixed(2));
});
});