-
Notifications
You must be signed in to change notification settings - Fork 6
/
balanceDataset.js
304 lines (265 loc) · 8.97 KB
/
balanceDataset.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// DACP Library
// Follow the instructions on the Github Page to generate the Dataset
const express = require('express');
const morgan = require('morgan');
const app = express();
const bodyParser = require('body-parser');
const ccxt = require('ccxt');
require('dotenv').config();
// -- Setting Morgan and Body Parser --
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// List of the User Exchanges
var myExchanges = [];
// Calls all Functions
(async () => {
console.log("----------");
console.log('Path of Coins to USD: ')
var dataset = await setDataset();
console.log("----------");
console.log(dataset)
console.log("----------");
console.log(dataset.exchanges[0])
console.log(dataset.exchanges[1])
console.log("----------");
}) ();
// Creates the dataset of balances with all exchanges
async function setDataset(){
var balanceDataset = {};
balanceDataset.exchanges = []
for (var i = 0; i < myExchanges.length; i++){
balanceDataset.exchanges.push(await setExchangeData(myExchanges[i]));
}
return balanceDataset;
}
// Takes balances and prices from an exchange
async function setExchangeData(exchangeName){
// Set up Exchange
let exchange = new ccxt [exchangeName] ({
apiKey: eval(`process.env.` + exchangeName + `_apiKey`),
secret: eval(`process.env.` + exchangeName + `_secret`)
});
// Set up JSON
var dataset = {};
dataset.name = exchangeName;
dataset.coins = [];
var errorCounter = 0;
var exchangeHoldings = await findHoldings(exchange);
var rateLimit = exchange.rateLimit;
await sleep (rateLimit);
//Set Balance for each coin in exchange
for(var i = 0; i < exchangeHoldings.length; i++){
try {
await sleep (rateLimit);
var balanceOfCoin = await findBalance(exchange, exchangeHoldings[i]);
var balanceInUSD = 0;
if(exchangeHoldings[i] === 'USD')
{
console.log(['USD']);
balanceInUSD = balanceOfCoin;
}
else {
balanceInUSD = balanceOfCoin * await findPriceUSD(exchange, exchangeHoldings[i]);
}
var coin = {};
coin.name = exchangeHoldings[i];
coin.balanceInCurrency = balanceOfCoin;
coin.balanceInUSD = balanceInUSD;
dataset.coins.push(coin);
}
catch (e) {
errorCounter++;
console.log("--------------------");
console.log(`Error ${errorCounter}`);
console.log(e);
console.log("--------------------");
console.log('Please Wait...')
let errorSleepTime = 20000; // in ms
await sleep (errorSleepTime);
i--; //Restart
}
}
return dataset;
}
// Finds the holdings in that single exchange
async function findBalance(exchange,coinName){
var balance = await exchange.fetchBalance()
var totalBalance = balance.total;
return totalBalance[coinName]; // Delete coinName to get all
}
// Finds the balances in that single exchange
async function findHoldings(exchange){
var balance = await exchange.fetchBalance()
var coinHoldings = Object.keys(balance.total)
return coinHoldings;
}
// Gets the last price it was sold at on specific Symbol.
async function findPriceAtSymbol(exchange, market){
var ticker = await exchange.fetchTicker(market);
return ticker.last;
}
// Finds the symbols offered by certain exchange
async function findSymbols(exchange){
var market = await exchange.fetchMarkets();
symbolsOffered = [];
for (var i = 0; i < market.length; i++){
symbolsOffered.push(market[i].symbol);
}
return symbolsOffered;
}
// Finds path to get the price of the coin in USD on exchange
async function findPriceUSD(exchange, coinName){
//Preset the Bitfinex access
let bitfinexExchange = new ccxt ['bitfinex'] ({
apiKey: eval(`process.env.` + 'bitfinex' + `_apiKey`),
secret: eval(`process.env.` + 'bitfinex' + `_secret`)
});
path = [];
symbol = coinName + '/USD';
allSymbols = await findSymbols(exchange);
found = false;
// If the asset is USD (Should check if in other currency as well)
if(coinName === 'USD'){ // Should not be necessary
path.push('USD');
return await findBalance(exchange, 'USD');
}
//Checks if there is a connection straight to USD
for(var i = 0; i < allSymbols.length; i++){
if(symbol === allSymbols[i])
{
found = true;
path.push(symbol);
console.log(path)
return await findPriceAtSymbol(exchange, symbol);
}
}
// if Not, checks for BTC or ETH connection
if(!found){
// Check for BTC connection
if(coinName != 'BTC'){
symbol = coinName + '/BTC';
}
else {
found = true; // If BTC, goes to next step to be tested on Bitfinex
}
for(var i = 0; i < allSymbols.length; i++){
if(symbol === allSymbols[i]){
found = true;
path.push(symbol);
}
}
// Find if there is a USD connection from BTC
foundBTCPath = false;
if(found){
// Look for BTC path to USD in exchange
for(var i = 0; i < allSymbols.length; i++){
if('BTC/USD' === allSymbols[i])
{
foundBTCPath = true;
path.push(symbol);
var price = await findPriceAtSymbol(exchange, symbol) *
await findPriceAtSymbol(exchange, 'BTC/USD');
console.log(path)
return price;
}
}
// Look for BTC path to USD in Bitfinex
if(!foundBTCPath){
BTCUSDprice = await findPriceAtSymbol(bitfinexExchange, 'BTC/USD'); //Check, May need to change the 'bitfinex thing'
path.push('BTC/USD @ Bitfinex');
foundBTCPath = true;
var price = 0;
// if BTC, only compare to other exchange, if anything else, take into account the coin/BTC.
if(coinName === 'BTC')
price = await findPriceAtSymbol(bitfinexExchange, 'BTC/USD'); //Check, May need to change the 'bitfinex thing'
else{
price = await findPriceAtSymbol(exchange, symbol) *
await findPriceAtSymbol(bitfinexExchange, 'BTC/USD'); //Check, May need to change the 'bitfinex thing'
}
console.log(path)
return price;
}
}
else{ // Meaning if no connection to BTC, check for ETH connection
if(coinName != 'ETH')
symbol = coinName + '/ETH';
else
found = true; // If ETH, goes to next step to be tested on Bitfinex
for(var i = 0; i < allSymbols.length; i++){
if(symbol === allSymbols[i]){
found = true;
path.push(symbol);
}
}
foundETHPath = false;
if(found){
// Look for BTC path to USD in exchange
for(var i = 0; i < allSymbols.length; i++){
if('ETH/USD' === allSymbols[i])
{
foundETHPath = true;
path.push(symbol);
var price = await findPriceAtSymbol(exchange, symbol) *
await findPriceAtSymbol(exchange, 'ETH/USD');
console.log(path)
return price;
}
}
// Look for BTC path to USD in Bitfinex
if(!foundETHPath){
ETHUSDprice = await findPriceAtSymbol(bitfinexExchange, 'ETH/USD');
path.push('ETH/USD @ Bitfinex');
foundETHPath = true;
if(symbol === 'ETH')
price = await findPriceAtSymbol(bitfinexExchange, 'ETH/USD')
else{
price = await findPriceAtSymbol(exchange, symbol) *
await findPriceAtSymbol(bitfinexExchange, 'ETH/USD');
}
console.log(path)
return price;
}
}
if(!foundETHPath){
console.log("Connection could not be found")
return -1;
}
}
}
console.log(path)
}
// Sleeps for certain duration of time set in milliseconds
async function sleep (duration) {
return new Promise (resolve => setTimeout (resolve, duration));
}
// Goes through all exchanges and finds holdings (coin types)
async function getTotalHoldings() {
allHoldings.exchanges = []
for (var i = 0; i < myExchanges.length; i++){
let exchange = new ccxt [myExchanges[i]] ({
apiKey: eval(`process.env.` + exchangeName + `_apiKey`),
secret: eval(`process.env.` + exchangeName + `_secret`)
});
var exchangeBalance = await findHoldings(exchange);
exchangeInfo = {};
exchangeInfo.name = myExchanges[i]
exchangeInfo.holdings = exchangeBalance;
allHoldings.exchanges.push(exchangeInfo);
}
}; // For this to work, fix input of methods
// Goes through all exchanges and finds balance (coin and total)
async function getTotalBalances() {
allBalances.exchanges = [];
for (var i = 0; i < myExchanges.length; i++){
let exchange = new ccxt [myExchanges[i]] ({
apiKey: eval(`process.env.` + exchangeName + `_apiKey`),
secret: eval(`process.env.` + exchangeName + `_secret`)
});
var exchangeBalance = await findBalance(exchange);
exchangeInfo = {}
exchangeInfo.name = myExchanges[i];
exchangeInfo.balances = exchangeBalance;
allBalances.exchanges.push(exchangeInfo)
}
};