|
1
|
|
|
/*jslint node: true, nomen: true, regexp: true, plusplus: true */ |
|
2
|
|
|
(function () { |
|
3
|
|
|
|
|
4
|
|
|
'use strict'; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
const chai = require("chai"), |
|
7
|
|
|
should = chai.should(), |
|
8
|
|
|
expect = chai.expect; |
|
9
|
|
|
|
|
10
|
|
|
const Blockfolio = require("../index"); |
|
11
|
|
|
|
|
12
|
|
|
const FAKE_TOKEN = "1915f3d2ef313e86"; |
|
13
|
|
|
|
|
14
|
|
|
describe("Blockfolio API", function () { |
|
15
|
|
|
describe("General", function () { |
|
16
|
|
|
it("Get the API version", function (done) { |
|
17
|
|
|
Blockfolio.getVersion((err, version) => { |
|
18
|
|
|
if (err) { return done(err); } |
|
19
|
|
|
should.exist(version); |
|
20
|
|
|
expect(version).to.be.a("number"); |
|
21
|
|
|
return done(); |
|
22
|
|
|
}); |
|
23
|
|
|
}); |
|
24
|
|
|
it("Get the system status of the API", function (done) { |
|
25
|
|
|
Blockfolio.getStatus((err, statusMsg) => { |
|
26
|
|
|
if (err) { return done(err); } |
|
27
|
|
|
should.exist(statusMsg); |
|
28
|
|
|
expect(statusMsg).to.be.a("string"); |
|
29
|
|
|
return done(); |
|
30
|
|
|
}); |
|
31
|
|
|
}); |
|
32
|
|
|
it("Get the announcements from SIGNAL", function (done) { |
|
33
|
|
|
Blockfolio.getAnnouncements((err, announcements) => { |
|
34
|
|
|
if (err) { return done(err); } |
|
35
|
|
|
should.exist(announcements); |
|
36
|
|
|
expect(announcements).to.be.an("array"); |
|
37
|
|
|
return done(); |
|
38
|
|
|
}); |
|
39
|
|
|
}); |
|
40
|
|
|
it("should fail at registering an already activated DEVICE_TOKEN", function (done) { |
|
41
|
|
|
Blockfolio._register(FAKE_TOKEN, (err, response) => { |
|
42
|
|
|
should.exist(err.message); |
|
43
|
|
|
should.not.exist(response); |
|
44
|
|
|
return done(); |
|
45
|
|
|
}); |
|
46
|
|
|
}); |
|
47
|
|
|
}); |
|
48
|
|
|
describe("Module Instanciation", function () { |
|
49
|
|
|
it("a protected method called without it should return an error", function (done) { |
|
50
|
|
|
Blockfolio.getPositions("BTC/USD", (err, positions) => { |
|
51
|
|
|
should.exist(err.message); |
|
52
|
|
|
expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)"); |
|
53
|
|
|
should.not.exist(positions); |
|
54
|
|
|
return done(); |
|
55
|
|
|
}); |
|
56
|
|
|
}); |
|
57
|
|
|
it("should fail to initialize with a disposable token", function (done) { |
|
58
|
|
|
Blockfolio.init("40f027b891222cdf7fe7d7390a29e4bb5c79ea7adbab660c855b2d6c603de2d710c10aebcc4ee76c6da4402457cbfd50", (err) => { |
|
59
|
|
|
should.exist(err.message); |
|
60
|
|
|
return done(); |
|
61
|
|
|
}); |
|
62
|
|
|
}); |
|
63
|
|
|
// Expand timeout for initialization |
|
64
|
|
|
this.timeout(5000); |
|
65
|
|
|
it("should be ok with a working token", function (done) { |
|
66
|
|
|
try { |
|
67
|
|
|
Blockfolio.init(FAKE_TOKEN, (err) => { |
|
|
|
|
|
|
68
|
|
|
if (err) { return done(err); } |
|
69
|
|
|
|
|
70
|
|
|
return done(); |
|
71
|
|
|
}); |
|
72
|
|
|
} catch (e) { |
|
73
|
|
|
return done(e); |
|
74
|
|
|
} |
|
75
|
|
|
}); |
|
76
|
|
|
}); |
|
77
|
|
|
describe("Tools", function () { |
|
78
|
|
|
it("should return a random token", function (done) { |
|
79
|
|
|
const generatedToken = Blockfolio.utils.generateClientToken(); |
|
80
|
|
|
expect(generatedToken).to.be.a("string"); |
|
81
|
|
|
return done(); |
|
82
|
|
|
}); |
|
83
|
|
|
it("should convert properly XRP/BTC to a pair struct", function (done) { |
|
84
|
|
|
const pair = Blockfolio.utils.parseToken("XRP/BTC"); |
|
85
|
|
|
expect(pair).to.be.deep.equal({ base: "BTC", token: "XRP" }); |
|
86
|
|
|
return done(); |
|
87
|
|
|
}); |
|
88
|
|
|
it("should convert properly BTC/USD to a pair struct", function (done) { |
|
89
|
|
|
const pair = Blockfolio.utils.parseToken("BTC/USD"); |
|
90
|
|
|
expect(pair).to.be.deep.equal({ base: "USD", token: "BTC" }); |
|
91
|
|
|
return done(); |
|
92
|
|
|
}); |
|
93
|
|
|
it("should convert properly AEON to a pair struct", function (done) { |
|
94
|
|
|
const pair = Blockfolio.utils.parseToken("AEON"); |
|
95
|
|
|
expect(pair).to.be.deep.equal({ base: "BTC", token: "AEON" }); |
|
96
|
|
|
return done(); |
|
97
|
|
|
}); |
|
98
|
|
|
it("should convert properly BTC-LTC to a pair struct", function (done) { |
|
99
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-LTC"); |
|
100
|
|
|
expect(pair).to.be.deep.equal({ base: "BTC", token: "LTC" }); |
|
101
|
|
|
return done(); |
|
102
|
|
|
}); |
|
103
|
|
|
it("should convert properly BTC-DASH to a pair struct", function (done) { |
|
104
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-DASH"); |
|
105
|
|
|
expect(pair).to.be.deep.equal({ base: "BTC", token: "DASH" }); |
|
106
|
|
|
return done(); |
|
107
|
|
|
}); |
|
108
|
|
|
it("should convert properly BTC-BCH to a pair struct", function (done) { |
|
109
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-BCH"); |
|
110
|
|
|
expect(pair).to.be.deep.equal({ base: "BTC", token: "BCH" }); |
|
111
|
|
|
return done(); |
|
112
|
|
|
}); |
|
113
|
|
|
}); |
|
114
|
|
|
describe("Endpoints", function () { |
|
115
|
|
|
// Expand timeout for network & API lentency |
|
116
|
|
|
this.timeout(10000); |
|
117
|
|
|
it("Get the currencies list", function (done) { |
|
118
|
|
|
Blockfolio.getCurrencies((err, currencies) => { |
|
119
|
|
|
if (err) { return done(err); } |
|
120
|
|
|
should.exist(currencies); |
|
121
|
|
|
expect(currencies).to.be.an("array"); |
|
122
|
|
|
return done(); |
|
123
|
|
|
}); |
|
124
|
|
|
}); |
|
125
|
|
|
it("Get the coins list", function (done) { |
|
126
|
|
|
Blockfolio.getCoinsList((err, coins) => { |
|
127
|
|
|
if (err) { return done(err); } |
|
128
|
|
|
should.exist(coins); |
|
129
|
|
|
expect(coins).to.be.an("array"); |
|
130
|
|
|
return done(); |
|
131
|
|
|
}); |
|
132
|
|
|
}); |
|
133
|
|
|
it("Get a Disposable Device Token", function (done) { |
|
134
|
|
|
Blockfolio.getDisposableDeviceToken(token => { |
|
135
|
|
|
should.exist(token); |
|
136
|
|
|
return done(); |
|
137
|
|
|
}); |
|
138
|
|
|
}); |
|
139
|
|
|
it("Get market details for an AEON/BTC", function (done) { |
|
140
|
|
|
Blockfolio.getMarketDetails("AEON/BTC", "bittrex", (err, details) => { |
|
141
|
|
|
if (err) { return done(err); } |
|
142
|
|
|
|
|
143
|
|
|
should.exist(details.ask); |
|
144
|
|
|
expect(details.ask).to.be.a("string"); |
|
145
|
|
|
return done(); |
|
146
|
|
|
}); |
|
147
|
|
|
}); |
|
148
|
|
|
it("Get available exchanges for this token", function (done) { |
|
149
|
|
|
Blockfolio.getExchanges("AEON/BTC", (err, exchanges) => { |
|
150
|
|
|
if (err) { return done(err); } |
|
151
|
|
|
|
|
152
|
|
|
expect(exchanges).to.be.an("array"); |
|
153
|
|
|
return done(); |
|
154
|
|
|
}); |
|
155
|
|
|
}); |
|
156
|
|
|
it("Get available exchanges for an incorrect token", function (done) { |
|
157
|
|
|
Blockfolio.getExchanges("ZSKJD/BTC", (err, exchanges) => { |
|
158
|
|
|
should.exist(err.message); |
|
159
|
|
|
expect(err.message).to.equal("ZSKJD is not an available token on Blockfolio!"); |
|
160
|
|
|
should.not.exist(exchanges); |
|
161
|
|
|
return done(); |
|
162
|
|
|
}); |
|
163
|
|
|
}); |
|
164
|
|
|
it("Add a token pair to watch from Bittrex", function (done) { |
|
165
|
|
|
Blockfolio.watchCoin("AEON/BTC", "bittrex", (err, res) => { |
|
166
|
|
|
if (err) { return done(err); } |
|
167
|
|
|
|
|
168
|
|
|
expect(res).to.equal("success"); |
|
169
|
|
|
return done(); |
|
170
|
|
|
}); |
|
171
|
|
|
}); |
|
172
|
|
|
it("Get the last price of an incorrect token on this exchange", function (done) { |
|
173
|
|
|
Blockfolio.getPrice("EAZRREZREZ/BTC", "bittrex", (err, rPrice) => { |
|
174
|
|
|
should.exist(err.message); |
|
175
|
|
|
expect(err.message).to.equal("EAZRREZREZ is not an available token on Blockfolio!"); |
|
176
|
|
|
should.not.exist(rPrice); |
|
177
|
|
|
return done(); |
|
178
|
|
|
}); |
|
179
|
|
|
}); |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
it("... and with a valid token, but an incorrect base", function (done) { |
|
183
|
|
|
Blockfolio.getPrice("BTC/DSQFSDFDSF", "bittrex", (err, nPrice) => { |
|
|
|
|
|
|
184
|
|
|
should.exist(err.message); |
|
185
|
|
|
expect(err.message).to.equal("BTC is not an available in DSQFSDFDSF on Blockfolio!"); |
|
186
|
|
|
return done(); |
|
187
|
|
|
}); |
|
188
|
|
|
}); |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
it("Get the last price of AEON on this exchange", function (done) { |
|
192
|
|
|
Blockfolio.getPrice("AEON/BTC", "bittrex", (err, cPrice) => { |
|
193
|
|
|
if (err) { return done(err); } |
|
194
|
|
|
|
|
195
|
|
|
expect(cPrice).to.be.a("number"); |
|
196
|
|
|
return done(); |
|
197
|
|
|
}); |
|
198
|
|
|
}); |
|
199
|
|
|
it("Add a BTC position on this pair", function (done) { |
|
200
|
|
|
Blockfolio.addPosition(true, "AEON/BTC", "bittrex", 0.00018, 200, null, "AEON FTW", (err, res) => { |
|
201
|
|
|
if (err) { return done(err); } |
|
202
|
|
|
|
|
203
|
|
|
expect(res).to.equal("success"); |
|
204
|
|
|
return done(); |
|
205
|
|
|
}); |
|
206
|
|
|
}); |
|
207
|
|
|
it("Add a position honor the buy parameter", function (done) { |
|
208
|
|
|
Blockfolio.addPosition(true, "BTC-SC", "bittrex", 0.000003, -10500, null, null, (err, res) => { |
|
209
|
|
|
if (err) { return done(err); } |
|
210
|
|
|
|
|
211
|
|
|
expect(res).to.equal("success"); |
|
212
|
|
|
return done(); |
|
213
|
|
|
}); |
|
214
|
|
|
}); |
|
215
|
|
|
it("Add a position can add a sell position", function (done) { |
|
216
|
|
|
Blockfolio.addPosition(false, "BTC-SC", "bittrex", 0.0000007, 20000, new Date(2017, 10, 18), "Buying the dip", (err, res) => { |
|
217
|
|
|
if (err) { return done(err); } |
|
218
|
|
|
|
|
219
|
|
|
expect(res).to.equal("success"); |
|
220
|
|
|
return done(); |
|
221
|
|
|
}); |
|
222
|
|
|
}); |
|
223
|
|
|
it("Get the summary for current position", function (done) { |
|
224
|
|
|
Blockfolio.getHoldings("AEON/BTC", (err, summary) => { |
|
225
|
|
|
if (err) { return done(err); } |
|
226
|
|
|
|
|
227
|
|
|
should.exist(summary.holdingValueString); |
|
228
|
|
|
expect(summary.holdingValueString).to.be.a("string"); |
|
229
|
|
|
return done(); |
|
230
|
|
|
}); |
|
231
|
|
|
}); |
|
232
|
|
|
it("Get orders details for this position", function (done) { |
|
233
|
|
|
Blockfolio.getPositions("AEON/BTC", (err, positions) => { |
|
234
|
|
|
if (err) { return done(err); } |
|
235
|
|
|
|
|
236
|
|
|
should.exist(positions); |
|
237
|
|
|
expect(positions).to.be.an("array"); |
|
238
|
|
|
return done(); |
|
239
|
|
|
}); |
|
240
|
|
|
}); |
|
241
|
|
|
it("And then remove the coin from portfolio", function (done) { |
|
242
|
|
|
Blockfolio.removeCoin("AEON/BTC", (err, res) => { |
|
243
|
|
|
if (err) { return done(err); } |
|
244
|
|
|
|
|
245
|
|
|
expect(res).to.equal("success"); |
|
246
|
|
|
return done(); |
|
247
|
|
|
}); |
|
248
|
|
|
}); |
|
249
|
|
|
it("Get actual positions", function (done) { |
|
250
|
|
|
Blockfolio.getPositions((err, positions) => { |
|
251
|
|
|
if (err) { return done(err); } |
|
252
|
|
|
|
|
253
|
|
|
should.exist(positions); |
|
254
|
|
|
expect(positions).to.be.an("array"); |
|
255
|
|
|
return done(); |
|
256
|
|
|
}); |
|
257
|
|
|
}); |
|
258
|
|
|
}); |
|
259
|
|
|
}); |
|
260
|
|
|
|
|
261
|
|
|
})(); |