質問するログイン新規登録

質問編集履歴

4

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

2019/11/08 05:46

投稿

masayoshi555
masayoshi555

スコア9

title CHANGED
File without changes
body CHANGED
@@ -197,4 +197,59 @@
197
197
  });
198
198
  console.log("------------------------\n");
199
199
  }
200
- ```
200
+ ```
201
+ 追記3
202
+ ---
203
+ "authorizations:{エラー"は以下の変更でなくなりましたが、次のエラーが出ました。引き続き検証します。
204
+ (参考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))
205
+ ```swaggerClient
206
+ 'use strict';
207
+ var SwaggerClient = require("swagger-client");
208
+ var _ = require('lodash');
209
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
210
+
211
+ new SwaggerClient({
212
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
213
+ // Don't forget the `www`!
214
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
215
+ usePromise: true,
216
+ authorizations: {
217
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
218
+ }
219
+ })
220
+ .then(client => {
221
+ (以下変更なし)
222
+ ```
223
+ ```swaggerClient
224
+ 'use strict';
225
+ var SwaggerClient = require("swagger-client");
226
+ var _ = require('lodash');
227
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
228
+
229
+ var client = new SwaggerClient('http://petstore.swagger.io/v2/swagger.json',{
230
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
231
+ // Don't forget the `www`!
232
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
233
+ usePromise: true,
234
+ authorizations: {
235
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
236
+ }
237
+ })
238
+ .then(client => {
239
+ (以下省略)
240
+ ```
241
+ ```terminalerror
242
+ :bitmex admin$ swagger project start
243
+ Starting: /Users/admin/swagger/bitmex/swaggerClient.js...
244
+ project started here: http://undefined/api/v1
245
+ project will restart on changes.
246
+ to restart at any time, enter `rs`
247
+ Inspecting BitMEX API...
248
+ ------------------------
249
+
250
+ Unable to connect: TypeError: Cannot read property 'Trade_get' of undefined
251
+ at /Users/admin/swagger/bitmex/swaggerClient.js:23:16
252
+ at processTicksAndRejections (internal/process/task_queues.js:93:5)
253
+ ```
254
+
255
+ js:23:16は "client.Trade.Trade_get({symbol: 'XBTUSD', count: 40})"です。

3

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

2019/11/08 05:46

投稿

masayoshi555
masayoshi555

スコア9

title CHANGED
File without changes
body CHANGED
@@ -118,4 +118,83 @@
118
118
  at Function.Module._load (internal/modules/cjs/loader.js:730:14)
119
119
  at Function.Module.runMain (internal/modules/cjs/loader.js:1051:12)
120
120
  at internal/main/run_main_module.js:16:11
121
+ ```
122
+ ```swaggerClient
123
+ 'use strict';
124
+ var SwaggerClient = require("swagger-client");
125
+ var _ = require('lodash');
126
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
127
+
128
+ new SwaggerClient({
129
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
130
+ // Don't forget the `www`!
131
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
132
+ usePromise: true,
133
+ authorizations: {
134
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
135
+ }
136
+ })
137
+ .then(client => {
138
+ // Comment out if you're not requesting any user data.
139
+ // client.clientAuthorizations.add("apiKey", new BitMEXAPIKeyAuthorization('api-key', 'api-secret'));
140
+
141
+ // Print client capabilities
142
+ inspect(client.apis);
143
+
144
+ // Get a trade
145
+ client.Trade.Trade_get({symbol: 'XBTUSD', count: 40})
146
+ .then(function(response) {
147
+ var trades = JSON.parse(response.data.toString());
148
+ // Print the max price traded in the last `count` trades.
149
+ console.log('\nMax Trade:\n----\n', JSON.stringify(_.max(trades, 'price'), undefined, 2));
150
+ })
151
+ .catch(function(e) {
152
+ // Error handling...
153
+ console.log('Error:', e.statusText);
154
+ })
155
+
156
+ client.User.User_getMargin()
157
+ .then(function(response) {
158
+ var margin = JSON.parse(response.data.toString());
159
+ var marginBalance = (margin.marginBalance / 1e8).toFixed(4);
160
+ console.log('\nMargin Balance:', marginBalance, 'XBT');
161
+ })
162
+ .catch(function(e) {
163
+ // Error handling...
164
+ console.log('Error:', e.statusText);
165
+ })
166
+
167
+ // Example: Placing an order - commented for your safety
168
+ // .then(function() {
169
+ // return client.Order.Order_new({symbol: 'XBTUSD', price: 1000, orderQty: 1})
170
+ // })
171
+ // .then(function (response) {
172
+ // console.log(response.data.toString());
173
+ // });
174
+
175
+ // Example: sending a bulk order
176
+ // Note: due to a bug in the Swagger client, you must stringify the Array, otherwise
177
+ // we will be sent `["[object Object]","[object Object]"]`
178
+ // client.Order.Order_newBulk({
179
+ // "orders": JSON.stringify([
180
+ // {"symbol":"XBTUSD","price":2433.5,"orderQty":147,"side":"Sell"},
181
+ // {"symbol":"XBTUSD","price":2431.1,"orderQty":190,"side":"Sell"}
182
+ // ])
183
+ // })
184
+ // .then(function (response) {
185
+ // console.log(response.data.toString());
186
+ // });
187
+ })
188
+ .catch(function(e) {
189
+ console.error("Unable to connect:", e);
190
+ })
191
+
192
+ function inspect(client) {
193
+ console.log("Inspecting BitMEX API...");
194
+ Object.keys(client).forEach(function(model) {
195
+ if (!client[model].operations) return;
196
+ console.log("Available methods for %s: %s", model, Object.keys(client[model].operations).join(', '));
197
+ });
198
+ console.log("------------------------\n");
199
+ }
121
200
  ```

2

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

2019/11/08 05:25

投稿

masayoshi555
masayoshi555

スコア9

title CHANGED
File without changes
body CHANGED
@@ -37,4 +37,85 @@
37
37
  その後、BitMEX側にある程度の文書があることがわかりました。(一般的だと後ほどわかりました)
38
38
  [https://www.bitmex.com/app/apiKeysUsage](https://www.bitmex.com/app/apiKeysUsage)
39
39
  その中にAPI keyとAPI secret(secret keyとは言わないんですね)の入力場所があるのですが、それがどのファイルかはわかりませんでした。
40
- ![イメージ説明](88830bc25047faca0c8da80a201424e1.png)
40
+ ![イメージ説明](88830bc25047faca0c8da80a201424e1.png)
41
+
42
+ ### 追記2
43
+ 正しいかどうかはわかっていないのですが、その後のアクションを追記します。
44
+ swaggerのsampleコードとbitmexのサンプルコードで対応するものを探しました。
45
+ これに対して不足や指摘があればお願いしたいです。
46
+
47
+ 0. app.jsに該当するものを見つけ、ルート直下へ
48
+ ![イメージ説明](2aa30969610e312476902017e599777d.png)
49
+ 0. apiファイルを作成し、定義ファイルを移動&node_modulesフォルダのコピー
50
+ ![イメージ説明](30c62ad463eaddeacf21f95f72c23cfe.png)
51
+ 0. swaggerClient.jsの「接続できません:TypeError:未定義のプロパティ ‘add’を読み取れません」
52
+  web上に解決策あったので、これに基づき解決([https://codeday.me/jp/qa/20190426/709259.html](https://codeday.me/jp/qa/20190426/709259.html))
53
+ ```before
54
+ 'use strict';
55
+ var SwaggerClient = require("swagger-client");
56
+ var _ = require('lodash');
57
+ var BitMEXAPIKeyAuthorization = require('./lib/BitMEXAPIKeyAuthorization');
58
+
59
+ require('dotenv').config();
60
+
61
+ new SwaggerClient({
62
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
63
+ // Don't forget the `www`!
64
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
65
+ usePromise: true
66
+ })
67
+ .then(function(client) {
68
+ //console.log(client);
69
+ // Comment out if you're not requesting any user data.
70
+ client.clientAuthorizations.add("apiKey", new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET));
71
+
72
+ // Print client capabilities
73
+ //
74
+
75
+ })
76
+ .catch(function(e) {
77
+ console.error("Unable to connect:", e);
78
+ })
79
+ ```
80
+ ```after
81
+ new SwaggerClient({
82
+ // Switch this to `www.bitmex.com` when you're ready to try it out for real.
83
+ // Don't forget the `www`!
84
+ url: 'https://testnet.bitmex.com/api/explorer/swagger.json',
85
+ usePromise: true,
86
+ authorizations: {
87
+ apiKey: new BitMEXAPIKeyAuthorization(process.env.BITMEX_API_KEY, process.env.BITMEX_API_SECRET)
88
+ }
89
+ })
90
+ .then(client => {
91
+ // Do whatever with client
92
+ })
93
+ .catch(function(e) {
94
+ console.error("Unable to connect:", e);
95
+ })
96
+ ```
97
+
98
+ 結果(現在)
99
+ ---
100
+ フォルダ構成は以下の通り、
101
+ ![イメージ説明](5f9ce4b009ca6f7b76d217d6534ba3a9.png)
102
+ この状態でのターミナルからの実行結果
103
+ ```terminal
104
+ **:bitmex admin$ swagger project start
105
+ Starting: /Users/admin/swagger/bitmex/swaggerClient.js...
106
+ project started here: http://undefined/api/v1
107
+ project will restart on changes.
108
+ to restart at any time, enter `rs`
109
+ /Users/admin/swagger/bitmex/swaggerClient.js:11
110
+ authorizations: {
111
+ ^^^^^^^^^^^^^^
112
+
113
+ SyntaxError: Unexpected identifier
114
+ at wrapSafe (internal/modules/cjs/loader.js:891:16)
115
+ at Module._compile (internal/modules/cjs/loader.js:941:27)
116
+ at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
117
+ at Module.load (internal/modules/cjs/loader.js:822:32)
118
+ at Function.Module._load (internal/modules/cjs/loader.js:730:14)
119
+ at Function.Module.runMain (internal/modules/cjs/loader.js:1051:12)
120
+ at internal/main/run_main_module.js:16:11
121
+ ```

1

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

2019/11/08 05:22

投稿

masayoshi555
masayoshi555

スコア9

title CHANGED
File without changes
body CHANGED
@@ -23,7 +23,7 @@
23
23
   - それぞれのファイルの役割
24
24
   - BitMEXでやる場合、どのファイルがどう置き換わるのか
25
25
  ![イメージ説明](174dbe957ed2c12471968fb7669acd26.png)
26
-  - API keyやsecret kyeはどこに入力するのか
26
+  - API keyやAPI secret~~ kye~~はどこに入力するのか
27
27
   ・・・などなど恐らく知らなければいけないことに対して、全て質問が出来ていないと思われます。
28
28
   「まずはここを見なさい」でも構いませんので、情報頂けると大変助かります。
29
29
 
@@ -31,4 +31,10 @@
31
31
   - 本屋さんで参考になりそうな本を探した → ない。orもしくわわからない
32
32
   - ネットで参考になりそうなファイル構成を説明してそうなサイトを探した → 断片的
33
33
    参考サイト:[https://qiita.com/ta1nakamura/items/9ce151835d5b131fddf4](https://qiita.com/ta1nakamura/items/9ce151835d5b131fddf4)
34
-  API関連、特にswaggerは情報が少なく苦労しています。(そもそもAPI関連も少ない)
34
+  API関連、特にswaggerは情報が少なく苦労しています。(そもそもAPI関連も少ない)
35
+
36
+ ### 追記
37
+ その後、BitMEX側にある程度の文書があることがわかりました。(一般的だと後ほどわかりました)
38
+ [https://www.bitmex.com/app/apiKeysUsage](https://www.bitmex.com/app/apiKeysUsage)
39
+ その中にAPI keyとAPI secret(secret keyとは言わないんですね)の入力場所があるのですが、それがどのファイルかはわかりませんでした。
40
+ ![イメージ説明](88830bc25047faca0c8da80a201424e1.png)