質問編集履歴

4

追記:3 authorizationsエラー解決。別エラー発生。

2019/11/08 05:46

投稿

masayoshi555
masayoshi555

スコア9

test CHANGED
File without changes
test CHANGED
@@ -397,3 +397,113 @@
397
397
  }
398
398
 
399
399
  ```
400
+
401
+ 追記3
402
+
403
+ ---
404
+
405
+ "authorizations:{エラー"は以下の変更でなくなりましたが、次のエラーが出ました。引き続き検証します。
406
+
407
+ (参考URL:[https://github.com/swagger-api/swagger-js/blob/903569948d5a5c718d7b87d6832a672de4e76afc/docs/MIGRATION_2_X.md#authorizations](https://github.com/swagger-api/swagger-js/blob/903569948d5a5c718d7b87d6832a672de4e76afc/docs/MIGRATION_2_X.md#authorizations))
408
+
409
+ ```swaggerClient
410
+
411
+ 'use strict';
412
+
413
+ var SwaggerClient = require("swagger-client");
414
+
415
+ var _ = require('lodash');
416
+
417
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
418
+
419
+
420
+
421
+ new SwaggerClient({
422
+
423
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
424
+
425
+ // Don't forget the `www`!
426
+
427
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
428
+
429
+ usePromise: true,
430
+
431
+ authorizations: {
432
+
433
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
434
+
435
+ }
436
+
437
+ })
438
+
439
+ .then(client => {
440
+
441
+ (以下変更なし)
442
+
443
+ ```
444
+
445
+ ```swaggerClient
446
+
447
+ 'use strict';
448
+
449
+ var SwaggerClient = require("swagger-client");
450
+
451
+ var _ = require('lodash');
452
+
453
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
454
+
455
+
456
+
457
+ var client = new SwaggerClient('http://petstore.swagger.io/v2/swagger.json',{
458
+
459
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
460
+
461
+ // Don't forget the `www`!
462
+
463
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
464
+
465
+ usePromise: true,
466
+
467
+ authorizations: {
468
+
469
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
470
+
471
+ }
472
+
473
+ })
474
+
475
+ .then(client => {
476
+
477
+ (以下省略)
478
+
479
+ ```
480
+
481
+ ```terminalerror
482
+
483
+ :bitmex admin$ swagger project start
484
+
485
+ Starting: /Users/admin/swagger/bitmex/swaggerClient.js...
486
+
487
+ project started here: http://undefined/api/v1
488
+
489
+ project will restart on changes.
490
+
491
+ to restart at any time, enter `rs`
492
+
493
+ Inspecting BitMEX API...
494
+
495
+ ------------------------
496
+
497
+
498
+
499
+ Unable to connect: TypeError: Cannot read property 'Trade_get' of undefined
500
+
501
+ at /Users/admin/swagger/bitmex/swaggerClient.js:23:16
502
+
503
+ at processTicksAndRejections (internal/process/task_queues.js:93:5)
504
+
505
+ ```
506
+
507
+
508
+
509
+ js:23:16は "client.Trade.Trade_get({symbol: 'XBTUSD', count: 40})"です。

3

現状のエラーとエラーになっているファイルのコードを追記しました

2019/11/08 05:46

投稿

masayoshi555
masayoshi555

スコア9

test CHANGED
File without changes
test CHANGED
@@ -239,3 +239,161 @@
239
239
  at internal/main/run_main_module.js:16:11
240
240
 
241
241
  ```
242
+
243
+ ```swaggerClient
244
+
245
+ 'use strict';
246
+
247
+ var SwaggerClient = require("swagger-client");
248
+
249
+ var _ = require('lodash');
250
+
251
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
252
+
253
+
254
+
255
+ new SwaggerClient({
256
+
257
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
258
+
259
+ // Don't forget the `www`!
260
+
261
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
262
+
263
+ usePromise: true,
264
+
265
+ authorizations: {
266
+
267
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
268
+
269
+ }
270
+
271
+ })
272
+
273
+ .then(client => {
274
+
275
+ // Comment out if you're not requesting any user data.
276
+
277
+ // client.clientAuthorizations.add("apiKey", new BitMEXAPIKeyAuthorization('api-key', 'api-secret'));
278
+
279
+
280
+
281
+ // Print client capabilities
282
+
283
+ inspect(client.apis);
284
+
285
+
286
+
287
+ // Get a trade
288
+
289
+ client.Trade.Trade_get({symbol: 'XBTUSD', count: 40})
290
+
291
+ .then(function(response) {
292
+
293
+ var trades = JSON.parse(response.data.toString());
294
+
295
+ // Print the max price traded in the last `count` trades.
296
+
297
+ console.log('\nMax Trade:\n----\n', JSON.stringify(_.max(trades, 'price'), undefined, 2));
298
+
299
+ })
300
+
301
+ .catch(function(e) {
302
+
303
+ // Error handling...
304
+
305
+ console.log('Error:', e.statusText);
306
+
307
+ })
308
+
309
+
310
+
311
+ client.User.User_getMargin()
312
+
313
+ .then(function(response) {
314
+
315
+ var margin = JSON.parse(response.data.toString());
316
+
317
+ var marginBalance = (margin.marginBalance / 1e8).toFixed(4);
318
+
319
+ console.log('\nMargin Balance:', marginBalance, 'XBT');
320
+
321
+ })
322
+
323
+ .catch(function(e) {
324
+
325
+ // Error handling...
326
+
327
+ console.log('Error:', e.statusText);
328
+
329
+ })
330
+
331
+
332
+
333
+ // Example: Placing an order - commented for your safety
334
+
335
+ // .then(function() {
336
+
337
+ // return client.Order.Order_new({symbol: 'XBTUSD', price: 1000, orderQty: 1})
338
+
339
+ // })
340
+
341
+ // .then(function (response) {
342
+
343
+ // console.log(response.data.toString());
344
+
345
+ // });
346
+
347
+
348
+
349
+ // Example: sending a bulk order
350
+
351
+ // Note: due to a bug in the Swagger client, you must stringify the Array, otherwise
352
+
353
+ // we will be sent `["[object Object]","[object Object]"]`
354
+
355
+ // client.Order.Order_newBulk({
356
+
357
+ // "orders": JSON.stringify([
358
+
359
+ // {"symbol":"XBTUSD","price":2433.5,"orderQty":147,"side":"Sell"},
360
+
361
+ // {"symbol":"XBTUSD","price":2431.1,"orderQty":190,"side":"Sell"}
362
+
363
+ // ])
364
+
365
+ // })
366
+
367
+ // .then(function (response) {
368
+
369
+ // console.log(response.data.toString());
370
+
371
+ // });
372
+
373
+ })
374
+
375
+ .catch(function(e) {
376
+
377
+ console.error("Unable to connect:", e);
378
+
379
+ })
380
+
381
+
382
+
383
+ function inspect(client) {
384
+
385
+ console.log("Inspecting BitMEX API...");
386
+
387
+ Object.keys(client).forEach(function(model) {
388
+
389
+ if (!client[model].operations) return;
390
+
391
+ console.log("Available methods for %s: %s", model, Object.keys(client[model].operations).join(', '));
392
+
393
+ });
394
+
395
+ console.log("------------------------\n");
396
+
397
+ }
398
+
399
+ ```

2

追記2:さらにやったことを追記。いまだエラーにより進まず。

2019/11/08 05:25

投稿

masayoshi555
masayoshi555

スコア9

test CHANGED
File without changes
test CHANGED
@@ -77,3 +77,165 @@
77
77
  その中にAPI keyとAPI secret(secret keyとは言わないんですね)の入力場所があるのですが、それがどのファイルかはわかりませんでした。
78
78
 
79
79
  ![イメージ説明](88830bc25047faca0c8da80a201424e1.png)
80
+
81
+
82
+
83
+ ### 追記2
84
+
85
+ 正しいかどうかはわかっていないのですが、その後のアクションを追記します。
86
+
87
+ swaggerのsampleコードとbitmexのサンプルコードで対応するものを探しました。
88
+
89
+ これに対して不足や指摘があればお願いしたいです。
90
+
91
+
92
+
93
+ 0. app.jsに該当するものを見つけ、ルート直下へ
94
+
95
+ ![イメージ説明](2aa30969610e312476902017e599777d.png)
96
+
97
+ 0. apiファイルを作成し、定義ファイルを移動&node_modulesフォルダのコピー
98
+
99
+ ![イメージ説明](30c62ad463eaddeacf21f95f72c23cfe.png)
100
+
101
+ 0. swaggerClient.jsの「接続できません:TypeError:未定義のプロパティ ‘add’を読み取れません」
102
+
103
+  web上に解決策あったので、これに基づき解決([https://codeday.me/jp/qa/20190426/709259.html](https://codeday.me/jp/qa/20190426/709259.html))
104
+
105
+ ```before
106
+
107
+ 'use strict';
108
+
109
+ var SwaggerClient = require("swagger-client");
110
+
111
+ var _ = require('lodash');
112
+
113
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
114
+
115
+
116
+
117
+ require('dotenv').config();
118
+
119
+
120
+
121
+ new SwaggerClient({
122
+
123
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
124
+
125
+ // Don't forget the `www`!
126
+
127
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
128
+
129
+ usePromise: true
130
+
131
+ })
132
+
133
+ .then(function(client) {
134
+
135
+ //console.log(client);
136
+
137
+ // Comment out if you're not requesting any user data.
138
+
139
+ client.clientAuthorizations.add("apiKey", new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET));
140
+
141
+
142
+
143
+ // Print client capabilities
144
+
145
+ //
146
+
147
+
148
+
149
+ })
150
+
151
+ .catch(function(e) {
152
+
153
+ console.error("Unable to connect:", e);
154
+
155
+ })
156
+
157
+ ```
158
+
159
+ ```after
160
+
161
+ new SwaggerClient({
162
+
163
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
164
+
165
+ // Don't forget the `www`!
166
+
167
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
168
+
169
+ usePromise: true,
170
+
171
+ authorizations: {
172
+
173
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
174
+
175
+ }
176
+
177
+ })
178
+
179
+ .then(client => {
180
+
181
+ // Do whatever with client
182
+
183
+ })
184
+
185
+ .catch(function(e) {
186
+
187
+ console.error("Unable to connect:", e);
188
+
189
+ })
190
+
191
+ ```
192
+
193
+
194
+
195
+ 結果(現在)
196
+
197
+ ---
198
+
199
+ フォルダ構成は以下の通り、
200
+
201
+ ![イメージ説明](5f9ce4b009ca6f7b76d217d6534ba3a9.png)
202
+
203
+ この状態でのターミナルからの実行結果
204
+
205
+ ```terminal
206
+
207
+ **:bitmex admin$ swagger project start
208
+
209
+ Starting: /Users/admin/swagger/bitmex/swaggerClient.js...
210
+
211
+ project started here: http://undefined/api/v1
212
+
213
+ project will restart on changes.
214
+
215
+ to restart at any time, enter `rs`
216
+
217
+ /Users/admin/swagger/bitmex/swaggerClient.js:11
218
+
219
+ authorizations: {
220
+
221
+ ^^^^^^^^^^^^^^
222
+
223
+
224
+
225
+ SyntaxError: Unexpected identifier
226
+
227
+ at wrapSafe (internal/modules/cjs/loader.js:891:16)
228
+
229
+ at Module._compile (internal/modules/cjs/loader.js:941:27)
230
+
231
+ at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
232
+
233
+ at Module.load (internal/modules/cjs/loader.js:822:32)
234
+
235
+ at Function.Module._load (internal/modules/cjs/loader.js:730:14)
236
+
237
+ at Function.Module.runMain (internal/modules/cjs/loader.js:1051:12)
238
+
239
+ at internal/main/run_main_module.js:16:11
240
+
241
+ ```

1

追記に投稿後わかったことを記載しました。丸投げとの指摘を頂きましたが、わからない者にとっては丸投げかどうかもわからないので、何かヒントをもらえるだけでも大変助かります。

2019/11/08 05:22

投稿

masayoshi555
masayoshi555

スコア9

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  ![イメージ説明](174dbe957ed2c12471968fb7669acd26.png)
50
50
 
51
-  - API keyやsecret kyeはどこに入力するのか
51
+  - API keyやAPI secret~~ kye~~はどこに入力するのか
52
52
 
53
53
   ・・・などなど恐らく知らなければいけないことに対して、全て質問が出来ていないと思われます。
54
54
 
@@ -64,4 +64,16 @@
64
64
 
65
65
    参考サイト:[https://qiita.com/ta1nakamura/items/9ce151835d5b131fddf4](https://qiita.com/ta1nakamura/items/9ce151835d5b131fddf4)
66
66
 
67
-  API関連、特にswaggerは情報が少なく苦労しています。(そもそもAPI関連も少ない)
67
+  API関連、特にswaggerは情報が少なく苦労しています。(そもそもAPI関連も少ない)
68
+
69
+
70
+
71
+ ### 追記
72
+
73
+ その後、BitMEX側にある程度の文書があることがわかりました。(一般的だと後ほどわかりました)
74
+
75
+ [https://www.bitmex.com/app/apiKeysUsage](https://www.bitmex.com/app/apiKeysUsage)
76
+
77
+ その中にAPI keyとAPI secret(secret keyとは言わないんですね)の入力場所があるのですが、それがどのファイルかはわかりませんでした。
78
+
79
+ ![イメージ説明](88830bc25047faca0c8da80a201424e1.png)