1 | 'use strict';
|
2 | var assert = require('assert');
|
3 | var les = require("local-echoserver");
|
4 | var verify = require("@you21979/simple-verify");
|
5 | var qstring = require("querystring");
|
6 | var Promise = require("bluebird");
|
7 | var zaif = require('..');
|
8 |
|
9 | describe('test', function () {
|
10 | describe('stream api test', function (done) {
|
11 | var constant = zaif.Constant;
|
12 | it('stream', function () {
|
13 | return les(function(url){
|
14 | return new Promise(function(resolve,reject){
|
15 | constant.OPT_WEBSOCKET_URL = url.replace("http://","ws://");
|
16 | var w = zaif.createStreamApi('btc_jpy', function(data){
|
17 | w.close()
|
18 | resolve(data)
|
19 | })
|
20 | }).then(function(res){
|
21 | assert(res.currency_pair === 'mona_jpy')
|
22 | })
|
23 | },
|
24 | function(res, headers, method, url, body){},
|
25 | 3000,
|
26 | function(ws){
|
27 | ws.on("connection", function(conn) {
|
28 | conn.send(JSON.stringify({ asks:
|
29 | [ [ 17.1, 1576 ],
|
30 | [ 17.2, 3446 ],
|
31 | [ 17.3, 4226 ],
|
32 | [ 19.3, 22232 ] ],
|
33 | last_price: { action: 'ask', price: 17 },
|
34 | trades:
|
35 | [ { currenty_pair: 'mona_jpy',
|
36 | trade_type: 'ask',
|
37 | price: 17,
|
38 | tid: 80618,
|
39 | amount: 64,
|
40 | date: 1428130395 },
|
41 | { currenty_pair: 'mona_jpy',
|
42 | trade_type: 'ask',
|
43 | price: 16.9,
|
44 | tid: 80505,
|
45 | amount: 16,
|
46 | date: 1428117904 } ],
|
47 | bids:
|
48 | [ [ 17, 1133 ],
|
49 | [ 16.9, 2390 ],
|
50 | [ 15.1, 3862 ] ],
|
51 | currency_pair: 'mona_jpy',
|
52 | timestamp: '2015-04-04 16:04:14.000419' })
|
53 | );
|
54 | })
|
55 | })
|
56 | })
|
57 | })
|
58 | describe('private api test', function () {
|
59 | var constant = zaif.Constant;
|
60 | var config = {apikey : "",secretkey : "", useragent : "tradebot"}
|
61 | before(function() {
|
62 | return verify.createKeyPair().then(function(res){
|
63 | config.apikey = res.key
|
64 | config.secretkey = res.secret
|
65 | })
|
66 | })
|
67 | it('getInfo and auth', function () {
|
68 | return les(function(url){
|
69 | constant.OPT_TAPI_URL = url + '/tapi'
|
70 | var papi = zaif.createPrivateApi(config.apikey, config.secretkey, config.useragent);
|
71 | return papi.getInfo().then(function(res){
|
72 | assert(res.funds.jpy === 15320)
|
73 | assert(res.deposit.jpy === 20440)
|
74 | })
|
75 | }, function(res, headers, method, url, body){
|
76 | assert(method === 'POST');
|
77 | assert(url === '/tapi');
|
78 | assert(headers['key'] === config.apikey)
|
79 | assert(headers['user-agent'] === config.useragent)
|
80 | assert(headers['sign'])
|
81 | var param = qstring.parse(body)
|
82 | assert(param.method === 'get_info');
|
83 | assert(param.nonce);
|
84 | var result = JSON.stringify({
|
85 | success:1,
|
86 | return:{
|
87 | funds:{
|
88 | jpy:15320,
|
89 | btc:1.389,
|
90 | xem:100.2,
|
91 | mona:2600
|
92 | },
|
93 | deposit:{
|
94 | jpy:20440,
|
95 | btc:1.479,
|
96 | xem:100.2,
|
97 | mona:3200
|
98 | },
|
99 | rights:{
|
100 | info:1,
|
101 | trade:1,
|
102 | withdraw:1
|
103 | },
|
104 | trade_count:18,
|
105 | open_orders:3,
|
106 | server_time:1401950833,
|
107 | },
|
108 | });
|
109 | res.end(result);
|
110 | })
|
111 | })
|
112 | })
|
113 | describe('public api test', function () {
|
114 | var constant = zaif.Constant;
|
115 | var apiv1 = zaif.PublicApi;
|
116 | it('depth', function () {
|
117 | return les(function(url){
|
118 | constant.OPT_APIV1_URL = url + '/api/1'
|
119 | return apiv1.depth('btc_jpy').then(function(v){
|
120 | assert(v.asks[0][0] === 1000);
|
121 | assert(v.asks[0][1] == 1.0);
|
122 | assert(v.asks[1][0] === 1001);
|
123 | assert(v.asks[1][1] == 0.9);
|
124 | assert(v.bids[0][0] === 999);
|
125 | assert(v.bids[0][1] == 1.5);
|
126 | assert(v.bids[1][0] === 998);
|
127 | assert(v.bids[1][1] == 2.2);
|
128 | })
|
129 | }, function(res, headers, method, url, body){
|
130 | assert(method === 'GET');
|
131 | assert(url === '/api/1/depth/btc_jpy');
|
132 | var result = JSON.stringify({
|
133 | asks:[[1000, 1.0],[1001,0.9]], bids:[[999,1.5],[998,2.2]]
|
134 | });
|
135 | res.end(result);
|
136 | })
|
137 | })
|
138 | it('lastPrice', function () {
|
139 | return les(function(url){
|
140 | constant.OPT_APIV1_URL = url + '/api/1'
|
141 | return apiv1.lastPrice('btc_jpy').then(function(v){
|
142 | assert(v.last_price === 75555.0);
|
143 | })
|
144 | }, function(res, headers, method, url, body){
|
145 | assert(method === 'GET');
|
146 | assert(url === '/api/1/last_price/btc_jpy');
|
147 | var result = JSON.stringify({"last_price": 75555.0});
|
148 | res.end(result);
|
149 | })
|
150 | })
|
151 | it('ticker', function () {
|
152 | return les(function(url){
|
153 | constant.OPT_APIV1_URL = url + '/api/1'
|
154 | return apiv1.ticker('btc_jpy').then(function(v){
|
155 | assert(v.last === 75630.0);
|
156 | assert(v.high === 75900.0);
|
157 | assert(v.low === 73930.0);
|
158 | assert(v.vwap === 75048.3677);
|
159 | assert(v.volume === 11725.0985);
|
160 | assert(v.bid === 75630.0);
|
161 | assert(v.ask === 75635.0);
|
162 | })
|
163 | }, function(res, headers, method, url, body){
|
164 | assert(method === 'GET');
|
165 | assert(url === '/api/1/ticker/btc_jpy');
|
166 | var result = JSON.stringify({
|
167 | "last": 75630.0, "high": 75900.0, "low": 73930.0, "vwap": 75048.3677, "volume": 11725.0985, "bid": 75630.0, "ask": 75635.0
|
168 | });
|
169 | res.end(result);
|
170 | })
|
171 | })
|
172 | it('trades', function () {
|
173 | return les(function(url){
|
174 | constant.OPT_APIV1_URL = url + '/api/1'
|
175 | return apiv1.trades('btc_jpy').then(function(v){
|
176 | assert(v instanceof Array)
|
177 | assert(v.length === 3)
|
178 | assert(v[0].currency_pair === 'btc_jpy')
|
179 | })
|
180 | }, function(res, headers, method, url, body){
|
181 | assert(method === 'GET');
|
182 | assert(url === '/api/1/trades/btc_jpy');
|
183 | var result = JSON.stringify([
|
184 | { date: 1477977267,
|
185 | price: 75695,
|
186 | amount: 0.1,
|
187 | tid: 16301201,
|
188 | currency_pair: 'btc_jpy',
|
189 | trade_type: 'ask' },
|
190 | { date: 1477977264,
|
191 | price: 75695,
|
192 | amount: 0.1,
|
193 | tid: 16301200,
|
194 | currency_pair: 'btc_jpy',
|
195 | trade_type: 'ask' },
|
196 | { date: 1477977262,
|
197 | price: 75690,
|
198 | amount: 0.6561,
|
199 | tid: 16301199,
|
200 | currency_pair: 'btc_jpy',
|
201 | trade_type: 'ask' }
|
202 | ]);
|
203 | res.end(result);
|
204 | })
|
205 | })
|
206 | })
|
207 | })
|