ccxt 4.2.11 → 4.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (375) hide show
  1. package/README.md +5 -5
  2. package/build.sh +2 -2
  3. package/dist/ccxt.browser.js +640 -261
  4. package/dist/ccxt.browser.min.js +3 -3
  5. package/dist/cjs/_virtual/agent.js +7 -0
  6. package/dist/cjs/_virtual/parse-proxy-response.js +7 -0
  7. package/dist/cjs/_virtual/promisify.js +7 -0
  8. package/dist/cjs/ccxt.js +1 -1
  9. package/dist/cjs/js/ccxt.js +474 -0
  10. package/dist/cjs/js/src/abstract/ace.js +9 -0
  11. package/dist/cjs/js/src/abstract/alpaca.js +9 -0
  12. package/dist/cjs/js/src/abstract/ascendex.js +9 -0
  13. package/dist/cjs/js/src/abstract/bigone.js +9 -0
  14. package/dist/cjs/js/src/abstract/binance.js +9 -0
  15. package/dist/cjs/js/src/abstract/bingx.js +9 -0
  16. package/dist/cjs/js/src/abstract/bit2c.js +9 -0
  17. package/dist/cjs/js/src/abstract/bitbank.js +9 -0
  18. package/dist/cjs/js/src/abstract/bitbns.js +9 -0
  19. package/dist/cjs/js/src/abstract/bitfinex.js +9 -0
  20. package/dist/cjs/js/src/abstract/bitfinex2.js +9 -0
  21. package/dist/cjs/js/src/abstract/bitflyer.js +9 -0
  22. package/dist/cjs/js/src/abstract/bitforex.js +9 -0
  23. package/dist/cjs/js/src/abstract/bitget.js +9 -0
  24. package/dist/cjs/js/src/abstract/bithumb.js +9 -0
  25. package/dist/cjs/js/src/abstract/bitmart.js +9 -0
  26. package/dist/cjs/js/src/abstract/bitmex.js +9 -0
  27. package/dist/cjs/js/src/abstract/bitopro.js +9 -0
  28. package/dist/cjs/js/src/abstract/bitpanda.js +9 -0
  29. package/dist/cjs/js/src/abstract/bitrue.js +9 -0
  30. package/dist/cjs/js/src/abstract/bitso.js +9 -0
  31. package/dist/cjs/js/src/abstract/bitstamp.js +9 -0
  32. package/dist/cjs/js/src/abstract/bitteam.js +9 -0
  33. package/dist/cjs/js/src/abstract/bitvavo.js +9 -0
  34. package/dist/cjs/js/src/abstract/bl3p.js +9 -0
  35. package/dist/cjs/js/src/abstract/blockchaincom.js +9 -0
  36. package/dist/cjs/js/src/abstract/btcalpha.js +9 -0
  37. package/dist/cjs/js/src/abstract/btcbox.js +9 -0
  38. package/dist/cjs/js/src/abstract/btcmarkets.js +9 -0
  39. package/dist/cjs/js/src/abstract/btcturk.js +9 -0
  40. package/dist/cjs/js/src/abstract/bybit.js +9 -0
  41. package/dist/cjs/js/src/abstract/cex.js +9 -0
  42. package/dist/cjs/js/src/abstract/coinbase.js +9 -0
  43. package/dist/cjs/js/src/abstract/coinbasepro.js +9 -0
  44. package/dist/cjs/js/src/abstract/coincheck.js +9 -0
  45. package/dist/cjs/js/src/abstract/coinex.js +9 -0
  46. package/dist/cjs/js/src/abstract/coinlist.js +9 -0
  47. package/dist/cjs/js/src/abstract/coinmate.js +9 -0
  48. package/dist/cjs/js/src/abstract/coinone.js +9 -0
  49. package/dist/cjs/js/src/abstract/coinsph.js +9 -0
  50. package/dist/cjs/js/src/abstract/coinspot.js +9 -0
  51. package/dist/cjs/js/src/abstract/cryptocom.js +9 -0
  52. package/dist/cjs/js/src/abstract/currencycom.js +9 -0
  53. package/dist/cjs/js/src/abstract/delta.js +9 -0
  54. package/dist/cjs/js/src/abstract/deribit.js +9 -0
  55. package/dist/cjs/js/src/abstract/digifinex.js +9 -0
  56. package/dist/cjs/js/src/abstract/exmo.js +9 -0
  57. package/dist/cjs/js/src/abstract/gate.js +9 -0
  58. package/dist/cjs/js/src/abstract/gemini.js +9 -0
  59. package/dist/cjs/js/src/abstract/hitbtc.js +9 -0
  60. package/dist/cjs/js/src/abstract/hollaex.js +9 -0
  61. package/dist/cjs/js/src/abstract/htx.js +9 -0
  62. package/dist/cjs/js/src/abstract/huobijp.js +9 -0
  63. package/dist/cjs/js/src/abstract/idex.js +9 -0
  64. package/dist/cjs/js/src/abstract/independentreserve.js +9 -0
  65. package/dist/cjs/js/src/abstract/indodax.js +9 -0
  66. package/dist/cjs/js/src/abstract/kraken.js +9 -0
  67. package/dist/cjs/js/src/abstract/krakenfutures.js +9 -0
  68. package/dist/cjs/js/src/abstract/kucoin.js +9 -0
  69. package/dist/cjs/js/src/abstract/kucoinfutures.js +9 -0
  70. package/dist/cjs/js/src/abstract/kuna.js +9 -0
  71. package/dist/cjs/js/src/abstract/latoken.js +9 -0
  72. package/dist/cjs/js/src/abstract/lbank.js +9 -0
  73. package/dist/cjs/js/src/abstract/luno.js +9 -0
  74. package/dist/cjs/js/src/abstract/lykke.js +9 -0
  75. package/dist/cjs/js/src/abstract/mercado.js +9 -0
  76. package/dist/cjs/js/src/abstract/mexc.js +9 -0
  77. package/dist/cjs/js/src/abstract/ndax.js +9 -0
  78. package/dist/cjs/js/src/abstract/novadax.js +9 -0
  79. package/dist/cjs/js/src/abstract/oceanex.js +9 -0
  80. package/dist/cjs/js/src/abstract/okcoin.js +9 -0
  81. package/dist/cjs/js/src/abstract/okx.js +9 -0
  82. package/dist/cjs/js/src/abstract/p2b.js +9 -0
  83. package/dist/cjs/js/src/abstract/paymium.js +9 -0
  84. package/dist/cjs/js/src/abstract/phemex.js +9 -0
  85. package/dist/cjs/js/src/abstract/poloniex.js +9 -0
  86. package/dist/cjs/js/src/abstract/poloniexfutures.js +9 -0
  87. package/dist/cjs/js/src/abstract/probit.js +9 -0
  88. package/dist/cjs/js/src/abstract/timex.js +9 -0
  89. package/dist/cjs/js/src/abstract/tokocrypto.js +9 -0
  90. package/dist/cjs/js/src/abstract/upbit.js +9 -0
  91. package/dist/cjs/js/src/abstract/wavesexchange.js +9 -0
  92. package/dist/cjs/js/src/abstract/wazirx.js +9 -0
  93. package/dist/cjs/js/src/abstract/whitebit.js +9 -0
  94. package/dist/cjs/js/src/abstract/woo.js +9 -0
  95. package/dist/cjs/js/src/abstract/yobit.js +9 -0
  96. package/dist/cjs/js/src/abstract/zaif.js +9 -0
  97. package/dist/cjs/js/src/abstract/zonda.js +9 -0
  98. package/dist/cjs/js/src/ace.js +1058 -0
  99. package/dist/cjs/js/src/alpaca.js +1125 -0
  100. package/dist/cjs/js/src/ascendex.js +3360 -0
  101. package/dist/cjs/js/src/base/Exchange.js +5110 -0
  102. package/dist/cjs/js/src/base/Precise.js +263 -0
  103. package/dist/cjs/js/src/base/errors.js +299 -0
  104. package/dist/cjs/js/src/base/functions/crypto.js +78 -0
  105. package/dist/cjs/js/src/base/functions/encode.js +44 -0
  106. package/dist/cjs/js/src/base/functions/generic.js +193 -0
  107. package/dist/cjs/js/src/base/functions/misc.js +96 -0
  108. package/dist/cjs/js/src/base/functions/number.js +297 -0
  109. package/dist/cjs/js/src/base/functions/platform.js +28 -0
  110. package/dist/cjs/js/src/base/functions/rsa.js +34 -0
  111. package/dist/cjs/js/src/base/functions/string.js +48 -0
  112. package/dist/cjs/js/src/base/functions/throttle.js +66 -0
  113. package/dist/cjs/js/src/base/functions/time.js +187 -0
  114. package/dist/cjs/js/src/base/functions/totp.js +24 -0
  115. package/dist/cjs/js/src/base/functions/type.js +162 -0
  116. package/dist/cjs/js/src/base/functions.js +157 -0
  117. package/dist/cjs/js/src/base/ws/Cache.js +254 -0
  118. package/dist/cjs/js/src/base/ws/Client.js +299 -0
  119. package/dist/cjs/js/src/base/ws/Future.js +34 -0
  120. package/dist/cjs/js/src/base/ws/OrderBook.js +107 -0
  121. package/dist/cjs/js/src/base/ws/OrderBookSide.js +281 -0
  122. package/dist/cjs/js/src/base/ws/WsClient.js +69 -0
  123. package/dist/cjs/js/src/bequant.js +33 -0
  124. package/dist/cjs/js/src/bigone.js +2142 -0
  125. package/dist/cjs/js/src/binance.js +9729 -0
  126. package/dist/cjs/js/src/binancecoinm.js +45 -0
  127. package/dist/cjs/js/src/binanceus.js +84 -0
  128. package/dist/cjs/js/src/binanceusdm.js +58 -0
  129. package/dist/cjs/js/src/bingx.js +3737 -0
  130. package/dist/cjs/js/src/bit2c.js +916 -0
  131. package/dist/cjs/js/src/bitbank.js +1000 -0
  132. package/dist/cjs/js/src/bitbay.js +17 -0
  133. package/dist/cjs/js/src/bitbns.js +1220 -0
  134. package/dist/cjs/js/src/bitcoincom.js +17 -0
  135. package/dist/cjs/js/src/bitfinex.js +1670 -0
  136. package/dist/cjs/js/src/bitfinex2.js +2990 -0
  137. package/dist/cjs/js/src/bitflyer.js +1045 -0
  138. package/dist/cjs/js/src/bitforex.js +852 -0
  139. package/dist/cjs/js/src/bitget.js +8284 -0
  140. package/dist/cjs/js/src/bithumb.js +1090 -0
  141. package/dist/cjs/js/src/bitmart.js +4454 -0
  142. package/dist/cjs/js/src/bitmex.js +2884 -0
  143. package/dist/cjs/js/src/bitopro.js +1724 -0
  144. package/dist/cjs/js/src/bitpanda.js +2002 -0
  145. package/dist/cjs/js/src/bitrue.js +3253 -0
  146. package/dist/cjs/js/src/bitso.js +1753 -0
  147. package/dist/cjs/js/src/bitstamp.js +2188 -0
  148. package/dist/cjs/js/src/bitteam.js +2309 -0
  149. package/dist/cjs/js/src/bitvavo.js +1968 -0
  150. package/dist/cjs/js/src/bl3p.js +447 -0
  151. package/dist/cjs/js/src/blockchaincom.js +1160 -0
  152. package/dist/cjs/js/src/btcalpha.js +929 -0
  153. package/dist/cjs/js/src/btcbox.js +565 -0
  154. package/dist/cjs/js/src/btcmarkets.js +1237 -0
  155. package/dist/cjs/js/src/btcturk.js +929 -0
  156. package/dist/cjs/js/src/bybit.js +7646 -0
  157. package/dist/cjs/js/src/cex.js +1693 -0
  158. package/dist/cjs/js/src/coinbase.js +3274 -0
  159. package/dist/cjs/js/src/coinbasepro.js +1866 -0
  160. package/dist/cjs/js/src/coincheck.js +843 -0
  161. package/dist/cjs/js/src/coinex.js +5414 -0
  162. package/dist/cjs/js/src/coinlist.js +2329 -0
  163. package/dist/cjs/js/src/coinmate.js +989 -0
  164. package/dist/cjs/js/src/coinone.js +1185 -0
  165. package/dist/cjs/js/src/coinsph.js +1933 -0
  166. package/dist/cjs/js/src/coinspot.js +548 -0
  167. package/dist/cjs/js/src/cryptocom.js +3007 -0
  168. package/dist/cjs/js/src/currencycom.js +2015 -0
  169. package/dist/cjs/js/src/delta.js +3256 -0
  170. package/dist/cjs/js/src/deribit.js +3306 -0
  171. package/dist/cjs/js/src/digifinex.js +4307 -0
  172. package/dist/cjs/js/src/exmo.js +2645 -0
  173. package/dist/cjs/js/src/fmfwio.js +34 -0
  174. package/dist/cjs/js/src/gate.js +7054 -0
  175. package/dist/cjs/js/src/gateio.js +16 -0
  176. package/dist/cjs/js/src/gemini.js +1801 -0
  177. package/dist/cjs/js/src/hitbtc.js +3660 -0
  178. package/dist/cjs/js/src/hitbtc3.js +19 -0
  179. package/dist/cjs/js/src/hollaex.js +1882 -0
  180. package/dist/cjs/js/src/htx.js +9024 -0
  181. package/dist/cjs/js/src/huobi.js +16 -0
  182. package/dist/cjs/js/src/huobijp.js +1918 -0
  183. package/dist/cjs/js/src/idex.js +1770 -0
  184. package/dist/cjs/js/src/independentreserve.js +759 -0
  185. package/dist/cjs/js/src/indodax.js +1069 -0
  186. package/dist/cjs/js/src/kraken.js +2861 -0
  187. package/dist/cjs/js/src/krakenfutures.js +2407 -0
  188. package/dist/cjs/js/src/kucoin.js +4489 -0
  189. package/dist/cjs/js/src/kucoinfutures.js +2475 -0
  190. package/dist/cjs/js/src/kuna.js +1949 -0
  191. package/dist/cjs/js/src/latoken.js +1729 -0
  192. package/dist/cjs/js/src/lbank.js +2825 -0
  193. package/dist/cjs/js/src/luno.js +1044 -0
  194. package/dist/cjs/js/src/lykke.js +1303 -0
  195. package/dist/cjs/js/src/mercado.js +897 -0
  196. package/dist/cjs/js/src/mexc.js +5407 -0
  197. package/dist/cjs/js/src/ndax.js +2450 -0
  198. package/dist/cjs/js/src/novadax.js +1556 -0
  199. package/dist/cjs/js/src/oceanex.js +964 -0
  200. package/dist/cjs/js/src/okcoin.js +3115 -0
  201. package/dist/cjs/js/src/okx.js +7329 -0
  202. package/dist/cjs/js/src/p2b.js +1243 -0
  203. package/dist/cjs/js/src/paymium.js +597 -0
  204. package/dist/cjs/js/src/phemex.js +4715 -0
  205. package/dist/cjs/js/src/poloniex.js +2356 -0
  206. package/dist/cjs/js/src/poloniexfutures.js +1794 -0
  207. package/dist/cjs/js/src/pro/alpaca.js +714 -0
  208. package/dist/cjs/js/src/pro/ascendex.js +957 -0
  209. package/dist/cjs/js/src/pro/bequant.js +33 -0
  210. package/dist/cjs/js/src/pro/binance.js +2796 -0
  211. package/dist/cjs/js/src/pro/binancecoinm.js +23 -0
  212. package/dist/cjs/js/src/pro/binanceus.js +51 -0
  213. package/dist/cjs/js/src/pro/binanceusdm.js +32 -0
  214. package/dist/cjs/js/src/pro/bingx.js +944 -0
  215. package/dist/cjs/js/src/pro/bitcoincom.js +29 -0
  216. package/dist/cjs/js/src/pro/bitfinex.js +672 -0
  217. package/dist/cjs/js/src/pro/bitfinex2.js +1159 -0
  218. package/dist/cjs/js/src/pro/bitget.js +1733 -0
  219. package/dist/cjs/js/src/pro/bitmart.js +1486 -0
  220. package/dist/cjs/js/src/pro/bitmex.js +1576 -0
  221. package/dist/cjs/js/src/pro/bitopro.js +327 -0
  222. package/dist/cjs/js/src/pro/bitpanda.js +1341 -0
  223. package/dist/cjs/js/src/pro/bitrue.js +462 -0
  224. package/dist/cjs/js/src/pro/bitstamp.js +547 -0
  225. package/dist/cjs/js/src/pro/bitvavo.js +704 -0
  226. package/dist/cjs/js/src/pro/blockchaincom.js +794 -0
  227. package/dist/cjs/js/src/pro/bybit.js +1843 -0
  228. package/dist/cjs/js/src/pro/cex.js +1510 -0
  229. package/dist/cjs/js/src/pro/coinbase.js +561 -0
  230. package/dist/cjs/js/src/pro/coinbasepro.js +968 -0
  231. package/dist/cjs/js/src/pro/coinex.js +1095 -0
  232. package/dist/cjs/js/src/pro/cryptocom.js +1020 -0
  233. package/dist/cjs/js/src/pro/currencycom.js +563 -0
  234. package/dist/cjs/js/src/pro/deribit.js +825 -0
  235. package/dist/cjs/js/src/pro/exmo.js +658 -0
  236. package/dist/cjs/js/src/pro/gate.js +1316 -0
  237. package/dist/cjs/js/src/pro/gateio.js +16 -0
  238. package/dist/cjs/js/src/pro/gemini.js +649 -0
  239. package/dist/cjs/js/src/pro/hitbtc.js +1293 -0
  240. package/dist/cjs/js/src/pro/hollaex.js +597 -0
  241. package/dist/cjs/js/src/pro/htx.js +2383 -0
  242. package/dist/cjs/js/src/pro/huobi.js +16 -0
  243. package/dist/cjs/js/src/pro/huobijp.js +606 -0
  244. package/dist/cjs/js/src/pro/idex.js +714 -0
  245. package/dist/cjs/js/src/pro/independentreserve.js +280 -0
  246. package/dist/cjs/js/src/pro/kraken.js +1364 -0
  247. package/dist/cjs/js/src/pro/krakenfutures.js +1500 -0
  248. package/dist/cjs/js/src/pro/kucoin.js +1052 -0
  249. package/dist/cjs/js/src/pro/kucoinfutures.js +981 -0
  250. package/dist/cjs/js/src/pro/luno.js +322 -0
  251. package/dist/cjs/js/src/pro/mexc.js +1170 -0
  252. package/dist/cjs/js/src/pro/ndax.js +545 -0
  253. package/dist/cjs/js/src/pro/okcoin.js +760 -0
  254. package/dist/cjs/js/src/pro/okx.js +1558 -0
  255. package/dist/cjs/js/src/pro/phemex.js +1511 -0
  256. package/dist/cjs/js/src/pro/poloniex.js +1253 -0
  257. package/dist/cjs/js/src/pro/poloniexfutures.js +1014 -0
  258. package/dist/cjs/js/src/pro/probit.js +586 -0
  259. package/dist/cjs/js/src/pro/upbit.js +234 -0
  260. package/dist/cjs/js/src/pro/wazirx.js +776 -0
  261. package/dist/cjs/js/src/pro/whitebit.js +927 -0
  262. package/dist/cjs/js/src/pro/woo.js +769 -0
  263. package/dist/cjs/js/src/probit.js +1865 -0
  264. package/dist/cjs/js/src/static_dependencies/fflake/browser.js +401 -0
  265. package/dist/cjs/js/src/static_dependencies/jsencrypt/JSEncrypt.js +195 -0
  266. package/dist/cjs/js/src/static_dependencies/jsencrypt/JSEncryptRSAKey.js +308 -0
  267. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/asn1.js +554 -0
  268. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/base64.js +94 -0
  269. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/hex.js +70 -0
  270. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/asn1js/int10.js +91 -0
  271. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/base64.js +16 -0
  272. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/jsbn.js +1760 -0
  273. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/prng4.js +52 -0
  274. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/rng.js +81 -0
  275. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/rsa.js +376 -0
  276. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsbn/util.js +70 -0
  277. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsrsasign/asn1-1.0.js +1580 -0
  278. package/dist/cjs/js/src/static_dependencies/jsencrypt/lib/jsrsasign/yahoo.js +74 -0
  279. package/dist/cjs/js/src/static_dependencies/noble-curves/_shortw_utils.js +24 -0
  280. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/curve.js +158 -0
  281. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/edwards.js +429 -0
  282. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/hash-to-curve.js +176 -0
  283. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/modular.js +324 -0
  284. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/montgomery.js +163 -0
  285. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/utils.js +245 -0
  286. package/dist/cjs/js/src/static_dependencies/noble-curves/abstract/weierstrass.js +1018 -0
  287. package/dist/cjs/js/src/static_dependencies/noble-curves/ed25519.js +383 -0
  288. package/dist/cjs/js/src/static_dependencies/noble-curves/secp256k1.js +258 -0
  289. package/dist/cjs/js/src/static_dependencies/noble-hashes/_assert.js +53 -0
  290. package/dist/cjs/js/src/static_dependencies/noble-hashes/_sha2.js +120 -0
  291. package/dist/cjs/js/src/static_dependencies/noble-hashes/_u64.js +69 -0
  292. package/dist/cjs/js/src/static_dependencies/noble-hashes/crypto.js +7 -0
  293. package/dist/cjs/js/src/static_dependencies/noble-hashes/hmac.js +83 -0
  294. package/dist/cjs/js/src/static_dependencies/noble-hashes/md5.js +240 -0
  295. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha1.js +91 -0
  296. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha256.js +130 -0
  297. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha3.js +214 -0
  298. package/dist/cjs/js/src/static_dependencies/noble-hashes/sha512.js +239 -0
  299. package/dist/cjs/js/src/static_dependencies/noble-hashes/utils.js +93 -0
  300. package/dist/cjs/js/src/static_dependencies/node-fetch/body.js +354 -0
  301. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/abort-error.js +16 -0
  302. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/base.js +20 -0
  303. package/dist/cjs/js/src/static_dependencies/node-fetch/errors/fetch-error.js +30 -0
  304. package/dist/cjs/js/src/static_dependencies/node-fetch/headers.js +239 -0
  305. package/dist/cjs/js/src/static_dependencies/node-fetch/index.js +372 -0
  306. package/dist/cjs/js/src/static_dependencies/node-fetch/request.js +273 -0
  307. package/dist/cjs/js/src/static_dependencies/node-fetch/response.js +139 -0
  308. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/get-search.js +14 -0
  309. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/is-redirect.js +16 -0
  310. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/is.js +81 -0
  311. package/dist/cjs/js/src/static_dependencies/node-fetch/utils/referrer.js +292 -0
  312. package/dist/cjs/js/src/static_dependencies/proxies/agent-base/index.js +103 -0
  313. package/dist/cjs/js/src/static_dependencies/proxies/http-proxy-agent/index.js +140 -0
  314. package/dist/cjs/js/src/static_dependencies/proxies/https-proxy-agent/index.js +175 -0
  315. package/dist/cjs/js/src/static_dependencies/proxies/https-proxy-agent/parse-proxy-response.js +95 -0
  316. package/dist/cjs/js/src/static_dependencies/qs/index.cjs.js +7 -0
  317. package/dist/cjs/js/src/static_dependencies/scure-base/index.js +383 -0
  318. package/dist/cjs/js/src/timex.js +1562 -0
  319. package/dist/cjs/js/src/tokocrypto.js +2542 -0
  320. package/dist/cjs/js/src/upbit.js +1844 -0
  321. package/dist/cjs/js/src/wavesexchange.js +2607 -0
  322. package/dist/cjs/js/src/wazirx.js +953 -0
  323. package/dist/cjs/js/src/whitebit.js +2309 -0
  324. package/dist/cjs/js/src/woo.js +2715 -0
  325. package/dist/cjs/js/src/yobit.js +1314 -0
  326. package/dist/cjs/js/src/zaif.js +736 -0
  327. package/dist/cjs/js/src/zonda.js +1883 -0
  328. package/js/ccxt.d.ts +1 -1
  329. package/js/ccxt.js +1 -1
  330. package/js/src/abstract/bigone.d.ts +18 -0
  331. package/js/src/abstract/binance.d.ts +2 -0
  332. package/js/src/abstract/binancecoinm.d.ts +2 -0
  333. package/js/src/abstract/binanceus.d.ts +2 -0
  334. package/js/src/abstract/binanceusdm.d.ts +2 -0
  335. package/js/src/abstract/bybit.d.ts +1 -0
  336. package/js/src/abstract/gate.d.ts +11 -0
  337. package/js/src/abstract/gateio.d.ts +11 -0
  338. package/js/src/alpaca.js +18 -18
  339. package/js/src/base/Exchange.d.ts +5 -1
  340. package/js/src/base/Exchange.js +101 -12
  341. package/js/src/bigone.d.ts +1 -2
  342. package/js/src/bigone.js +340 -145
  343. package/js/src/binance.js +15 -8
  344. package/js/src/bingx.js +9 -2
  345. package/js/src/bitfinex.d.ts +2 -2
  346. package/js/src/bitfinex.js +2 -3
  347. package/js/src/bitget.js +21 -8
  348. package/js/src/bitmart.d.ts +2 -2
  349. package/js/src/bitmart.js +3 -3
  350. package/js/src/bitmex.js +1 -0
  351. package/js/src/bybit.js +2 -0
  352. package/js/src/coinlist.js +2 -3
  353. package/js/src/coinsph.js +2 -3
  354. package/js/src/deribit.js +1 -0
  355. package/js/src/gate.d.ts +4 -4
  356. package/js/src/gate.js +22 -3
  357. package/js/src/hitbtc.d.ts +4 -4
  358. package/js/src/hitbtc.js +2 -3
  359. package/js/src/htx.js +4 -7
  360. package/js/src/huobijp.js +2 -3
  361. package/js/src/kraken.js +1 -0
  362. package/js/src/mexc.js +2 -1
  363. package/js/src/okx.js +13 -3
  364. package/js/src/pro/binance.d.ts +2 -23
  365. package/js/src/pro/binance.js +58 -22
  366. package/js/src/pro/coinbase.d.ts +2 -2
  367. package/js/src/pro/coinbase.js +4 -1
  368. package/js/src/pro/coinbasepro.d.ts +2 -2
  369. package/js/src/pro/hitbtc.d.ts +2 -2
  370. package/js/src/pro/poloniex.d.ts +2 -2
  371. package/js/src/upbit.d.ts +3 -101
  372. package/js/src/upbit.js +12 -12
  373. package/js/src/woo.js +2 -0
  374. package/package.json +1 -1
  375. package/skip-tests.json +5 -0
@@ -0,0 +1,2383 @@
1
+ 'use strict';
2
+
3
+ var htx$1 = require('../htx.js');
4
+ var errors = require('../base/errors.js');
5
+ var Cache = require('../base/ws/Cache.js');
6
+ var sha256 = require('../static_dependencies/noble-hashes/sha256.js');
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // ---------------------------------------------------------------------------
10
+ class htx extends htx$1 {
11
+ describe() {
12
+ return this.deepExtend(super.describe(), {
13
+ 'has': {
14
+ 'ws': true,
15
+ 'createOrderWs': false,
16
+ 'editOrderWs': false,
17
+ 'fetchOpenOrdersWs': false,
18
+ 'fetchOrderWs': false,
19
+ 'cancelOrderWs': false,
20
+ 'cancelOrdersWs': false,
21
+ 'cancelAllOrdersWs': false,
22
+ 'fetchTradesWs': false,
23
+ 'fetchBalanceWs': false,
24
+ 'watchOrderBook': true,
25
+ 'watchOrders': true,
26
+ 'watchTickers': false,
27
+ 'watchTicker': true,
28
+ 'watchTrades': true,
29
+ 'watchMyTrades': true,
30
+ 'watchBalance': true,
31
+ 'watchOHLCV': true,
32
+ },
33
+ 'urls': {
34
+ 'api': {
35
+ 'ws': {
36
+ 'api': {
37
+ 'spot': {
38
+ 'public': 'wss://{hostname}/ws',
39
+ 'private': 'wss://{hostname}/ws/v2',
40
+ 'feed': 'wss://{hostname}/feed',
41
+ },
42
+ 'future': {
43
+ 'linear': {
44
+ 'public': 'wss://api.hbdm.com/linear-swap-ws',
45
+ 'private': 'wss://api.hbdm.com/linear-swap-notification',
46
+ },
47
+ 'inverse': {
48
+ 'public': 'wss://api.hbdm.com/ws',
49
+ 'private': 'wss://api.hbdm.com/notification',
50
+ },
51
+ },
52
+ 'swap': {
53
+ 'inverse': {
54
+ 'public': 'wss://api.hbdm.com/swap-ws',
55
+ 'private': 'wss://api.hbdm.com/swap-notification',
56
+ },
57
+ 'linear': {
58
+ 'public': 'wss://api.hbdm.com/linear-swap-ws',
59
+ 'private': 'wss://api.hbdm.com/linear-swap-notification',
60
+ },
61
+ },
62
+ },
63
+ // these settings work faster for clients hosted on AWS
64
+ 'api-aws': {
65
+ 'spot': {
66
+ 'public': 'wss://api-aws.huobi.pro/ws',
67
+ 'private': 'wss://api-aws.huobi.pro/ws/v2',
68
+ 'feed': 'wss://{hostname}/feed',
69
+ },
70
+ 'future': {
71
+ 'linear': {
72
+ 'public': 'wss://api.hbdm.vn/linear-swap-ws',
73
+ 'private': 'wss://api.hbdm.vn/linear-swap-notification',
74
+ },
75
+ 'inverse': {
76
+ 'public': 'wss://api.hbdm.vn/ws',
77
+ 'private': 'wss://api.hbdm.vn/notification',
78
+ },
79
+ },
80
+ 'swap': {
81
+ 'linear': {
82
+ 'public': 'wss://api.hbdm.vn/linear-swap-ws',
83
+ 'private': 'wss://api.hbdm.vn/linear-swap-notification',
84
+ },
85
+ 'inverse': {
86
+ 'public': 'wss://api.hbdm.vn/swap-ws',
87
+ 'private': 'wss://api.hbdm.vn/swap-notification',
88
+ },
89
+ },
90
+ },
91
+ },
92
+ },
93
+ },
94
+ 'options': {
95
+ 'tradesLimit': 1000,
96
+ 'OHLCVLimit': 1000,
97
+ 'api': 'api',
98
+ 'watchOrderBook': {
99
+ 'maxRetries': 3,
100
+ },
101
+ 'ws': {
102
+ 'gunzip': true,
103
+ },
104
+ 'watchTicker': {
105
+ 'name': 'market.{marketId}.detail', // 'market.{marketId}.bbo' or 'market.{marketId}.ticker'
106
+ },
107
+ },
108
+ 'exceptions': {
109
+ 'ws': {
110
+ 'exact': {
111
+ 'bad-request': errors.BadRequest,
112
+ '2002': errors.AuthenticationError,
113
+ '2021': errors.BadRequest,
114
+ '2001': errors.BadSymbol,
115
+ '2011': errors.BadSymbol,
116
+ '2040': errors.BadRequest,
117
+ '4007': errors.BadRequest, // { op: 'sub', cid: '1', topic: 'accounts_unify.USDT', 'err-code': 4007, 'err-msg': 'Non - single account user is not available, please check through the cross and isolated account asset interface', ts: 1698419318540 }
118
+ },
119
+ },
120
+ },
121
+ });
122
+ }
123
+ requestId() {
124
+ const requestId = this.sum(this.safeInteger(this.options, 'requestId', 0), 1);
125
+ this.options['requestId'] = requestId;
126
+ return requestId.toString();
127
+ }
128
+ async watchTicker(symbol, params = {}) {
129
+ /**
130
+ * @method
131
+ * @name huobi#watchTicker
132
+ * @description watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
133
+ * @param {string} symbol unified symbol of the market to fetch the ticker for
134
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
135
+ * @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure}
136
+ */
137
+ await this.loadMarkets();
138
+ const market = this.market(symbol);
139
+ symbol = market['symbol'];
140
+ const options = this.safeValue(this.options, 'watchTicker', {});
141
+ const topic = this.safeString(options, 'name', 'market.{marketId}.detail');
142
+ if (topic === 'market.{marketId}.ticker' && market['type'] !== 'spot') {
143
+ throw new errors.BadRequest(this.id + ' watchTicker() with name market.{marketId}.ticker is only allowed for spot markets, use market.{marketId}.detail instead');
144
+ }
145
+ const messageHash = this.implodeParams(topic, { 'marketId': market['id'] });
146
+ const url = this.getUrlByMarketType(market['type'], market['linear']);
147
+ return await this.subscribePublic(url, symbol, messageHash, undefined, params);
148
+ }
149
+ handleTicker(client, message) {
150
+ //
151
+ // "market.btcusdt.detail"
152
+ // {
153
+ // "ch": "market.btcusdt.detail",
154
+ // "ts": 1583494163784,
155
+ // "tick": {
156
+ // "id": 209988464418,
157
+ // "low": 8988,
158
+ // "high": 9155.41,
159
+ // "open": 9078.91,
160
+ // "close": 9136.46,
161
+ // "vol": 237813910.5928412,
162
+ // "amount": 26184.202558551195,
163
+ // "version": 209988464418,
164
+ // "count": 265673
165
+ // }
166
+ // }
167
+ // "market.btcusdt.bbo"
168
+ // {
169
+ // "ch": "market.btcusdt.bbo",
170
+ // "ts": 1671941599613,
171
+ // "tick": {
172
+ // "seqId": 161499562790,
173
+ // "ask": 16829.51,
174
+ // "askSize": 0.707776,
175
+ // "bid": 16829.5,
176
+ // "bidSize": 1.685945,
177
+ // "quoteTime": 1671941599612,
178
+ // "symbol": "btcusdt"
179
+ // }
180
+ // }
181
+ //
182
+ const tick = this.safeValue(message, 'tick', {});
183
+ const ch = this.safeString(message, 'ch');
184
+ const parts = ch.split('.');
185
+ const marketId = this.safeString(parts, 1);
186
+ const market = this.safeMarket(marketId);
187
+ const ticker = this.parseTicker(tick, market);
188
+ const timestamp = this.safeValue(message, 'ts');
189
+ ticker['timestamp'] = timestamp;
190
+ ticker['datetime'] = this.iso8601(timestamp);
191
+ const symbol = ticker['symbol'];
192
+ this.tickers[symbol] = ticker;
193
+ client.resolve(ticker, ch);
194
+ return message;
195
+ }
196
+ async watchTrades(symbol, since = undefined, limit = undefined, params = {}) {
197
+ /**
198
+ * @method
199
+ * @name huobi#watchTrades
200
+ * @description get the list of most recent trades for a particular symbol
201
+ * @param {string} symbol unified symbol of the market to fetch trades for
202
+ * @param {int} [since] timestamp in ms of the earliest trade to fetch
203
+ * @param {int} [limit] the maximum amount of trades to fetch
204
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
205
+ * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
206
+ */
207
+ await this.loadMarkets();
208
+ const market = this.market(symbol);
209
+ symbol = market['symbol'];
210
+ const messageHash = 'market.' + market['id'] + '.trade.detail';
211
+ const url = this.getUrlByMarketType(market['type'], market['linear']);
212
+ const trades = await this.subscribePublic(url, symbol, messageHash, undefined, params);
213
+ if (this.newUpdates) {
214
+ limit = trades.getLimit(symbol, limit);
215
+ }
216
+ return this.filterBySinceLimit(trades, since, limit, 'timestamp', true);
217
+ }
218
+ handleTrades(client, message) {
219
+ //
220
+ // {
221
+ // "ch": "market.btcusdt.trade.detail",
222
+ // "ts": 1583495834011,
223
+ // "tick": {
224
+ // "id": 105004645372,
225
+ // "ts": 1583495833751,
226
+ // "data": [
227
+ // {
228
+ // "id": 1.050046453727319e+22,
229
+ // "ts": 1583495833751,
230
+ // "tradeId": 102090727790,
231
+ // "amount": 0.003893,
232
+ // "price": 9150.01,
233
+ // "direction": "sell"
234
+ // }
235
+ // ]
236
+ // }
237
+ // }
238
+ //
239
+ const tick = this.safeValue(message, 'tick', {});
240
+ const data = this.safeValue(tick, 'data', {});
241
+ const ch = this.safeString(message, 'ch');
242
+ const parts = ch.split('.');
243
+ const marketId = this.safeString(parts, 1);
244
+ const market = this.safeMarket(marketId);
245
+ const symbol = market['symbol'];
246
+ let tradesCache = this.safeValue(this.trades, symbol);
247
+ if (tradesCache === undefined) {
248
+ const limit = this.safeInteger(this.options, 'tradesLimit', 1000);
249
+ tradesCache = new Cache.ArrayCache(limit);
250
+ this.trades[symbol] = tradesCache;
251
+ }
252
+ for (let i = 0; i < data.length; i++) {
253
+ const trade = this.parseTrade(data[i], market);
254
+ tradesCache.append(trade);
255
+ }
256
+ client.resolve(tradesCache, ch);
257
+ return message;
258
+ }
259
+ async watchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) {
260
+ /**
261
+ * @method
262
+ * @name huobi#watchOHLCV
263
+ * @description watches historical candlestick data containing the open, high, low, and close price, and the volume of a market
264
+ * @param {string} symbol unified symbol of the market to fetch OHLCV data for
265
+ * @param {string} timeframe the length of time each candle represents
266
+ * @param {int} [since] timestamp in ms of the earliest candle to fetch
267
+ * @param {int} [limit] the maximum amount of candles to fetch
268
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
269
+ * @returns {int[][]} A list of candles ordered as timestamp, open, high, low, close, volume
270
+ */
271
+ await this.loadMarkets();
272
+ const market = this.market(symbol);
273
+ symbol = market['symbol'];
274
+ const interval = this.safeString(this.timeframes, timeframe, timeframe);
275
+ const messageHash = 'market.' + market['id'] + '.kline.' + interval;
276
+ const url = this.getUrlByMarketType(market['type'], market['linear']);
277
+ const ohlcv = await this.subscribePublic(url, symbol, messageHash, undefined, params);
278
+ if (this.newUpdates) {
279
+ limit = ohlcv.getLimit(symbol, limit);
280
+ }
281
+ return this.filterBySinceLimit(ohlcv, since, limit, 0, true);
282
+ }
283
+ handleOHLCV(client, message) {
284
+ //
285
+ // {
286
+ // "ch": "market.btcusdt.kline.1min",
287
+ // "ts": 1583501786794,
288
+ // "tick": {
289
+ // "id": 1583501760,
290
+ // "open": 9094.5,
291
+ // "close": 9094.51,
292
+ // "low": 9094.5,
293
+ // "high": 9094.51,
294
+ // "amount": 0.44639786263800907,
295
+ // "vol": 4059.76919054,
296
+ // "count": 16
297
+ // }
298
+ // }
299
+ //
300
+ const ch = this.safeString(message, 'ch');
301
+ const parts = ch.split('.');
302
+ const marketId = this.safeString(parts, 1);
303
+ const market = this.safeMarket(marketId);
304
+ const symbol = market['symbol'];
305
+ const interval = this.safeString(parts, 3);
306
+ const timeframe = this.findTimeframe(interval);
307
+ this.ohlcvs[symbol] = this.safeValue(this.ohlcvs, symbol, {});
308
+ let stored = this.safeValue(this.ohlcvs[symbol], timeframe);
309
+ if (stored === undefined) {
310
+ const limit = this.safeInteger(this.options, 'OHLCVLimit', 1000);
311
+ stored = new Cache.ArrayCacheByTimestamp(limit);
312
+ this.ohlcvs[symbol][timeframe] = stored;
313
+ }
314
+ const tick = this.safeValue(message, 'tick');
315
+ const parsed = this.parseOHLCV(tick, market);
316
+ stored.append(parsed);
317
+ client.resolve(stored, ch);
318
+ }
319
+ async watchOrderBook(symbol, limit = undefined, params = {}) {
320
+ /**
321
+ * @method
322
+ * @name huobi#watchOrderBook
323
+ * @see https://huobiapi.github.io/docs/dm/v1/en/#subscribe-market-depth-data
324
+ * @see https://huobiapi.github.io/docs/coin_margined_swap/v1/en/#subscribe-incremental-market-depth-data
325
+ * @see https://huobiapi.github.io/docs/usdt_swap/v1/en/#general-subscribe-incremental-market-depth-data
326
+ * @description watches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
327
+ * @param {string} symbol unified symbol of the market to fetch the order book for
328
+ * @param {int} [limit] the maximum amount of order book entries to return
329
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
330
+ * @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
331
+ */
332
+ await this.loadMarkets();
333
+ const market = this.market(symbol);
334
+ symbol = market['symbol'];
335
+ const allowedLimits = [20, 150];
336
+ // 2) 5-level/20-level incremental MBP is a tick by tick feed,
337
+ // which means whenever there is an order book change at that level, it pushes an update;
338
+ // 150-levels/400-level incremental MBP feed is based on the gap
339
+ // between two snapshots at 100ms interval.
340
+ if (limit === undefined) {
341
+ limit = market['spot'] ? 150 : 20;
342
+ }
343
+ if (!this.inArray(limit, allowedLimits)) {
344
+ throw new errors.ExchangeError(this.id + ' watchOrderBook market accepts limits of 20 and 150 only');
345
+ }
346
+ let messageHash = undefined;
347
+ if (market['spot']) {
348
+ messageHash = 'market.' + market['id'] + '.mbp.' + limit.toString();
349
+ }
350
+ else {
351
+ messageHash = 'market.' + market['id'] + '.depth.size_' + limit.toString() + '.high_freq';
352
+ }
353
+ const url = this.getUrlByMarketType(market['type'], market['linear'], false, true);
354
+ let method = this.handleOrderBookSubscription;
355
+ if (!market['spot']) {
356
+ params = this.extend(params);
357
+ params['data_type'] = 'incremental';
358
+ method = undefined;
359
+ }
360
+ const orderbook = await this.subscribePublic(url, symbol, messageHash, method, params);
361
+ return orderbook.limit();
362
+ }
363
+ handleOrderBookSnapshot(client, message, subscription) {
364
+ //
365
+ // {
366
+ // "id": 1583473663565,
367
+ // "rep": "market.btcusdt.mbp.150",
368
+ // "status": "ok",
369
+ // "ts": 1698359289261,
370
+ // "data": {
371
+ // "seqNum": 104999417756,
372
+ // "bids": [
373
+ // [9058.27, 0],
374
+ // [9058.43, 0],
375
+ // [9058.99, 0],
376
+ // ],
377
+ // "asks": [
378
+ // [9084.27, 0.2],
379
+ // [9085.69, 0],
380
+ // [9085.81, 0],
381
+ // ]
382
+ // }
383
+ // }
384
+ //
385
+ const symbol = this.safeString(subscription, 'symbol');
386
+ const messageHash = this.safeString(subscription, 'messageHash');
387
+ const id = this.safeString(message, 'id');
388
+ const lastTimestamp = this.safeInteger(subscription, 'lastTimestamp');
389
+ try {
390
+ const orderbook = this.orderbooks[symbol];
391
+ const data = this.safeValue(message, 'data');
392
+ const messages = orderbook.cache;
393
+ const firstMessage = this.safeValue(messages, 0, {});
394
+ const snapshot = this.parseOrderBook(data, symbol);
395
+ const tick = this.safeValue(firstMessage, 'tick');
396
+ const sequence = this.safeInteger(tick, 'prevSeqNum');
397
+ const nonce = this.safeInteger(data, 'seqNum');
398
+ snapshot['nonce'] = nonce;
399
+ const snapshotTimestamp = this.safeInteger(message, 'ts');
400
+ subscription['lastTimestamp'] = snapshotTimestamp;
401
+ const snapshotLimit = this.safeInteger(subscription, 'limit');
402
+ const snapshotOrderBook = this.orderBook(snapshot, snapshotLimit);
403
+ client.resolve(snapshotOrderBook, id);
404
+ if ((sequence === undefined) || (nonce < sequence)) {
405
+ const maxAttempts = this.handleOption('watchOrderBook', 'maxRetries', 3);
406
+ let numAttempts = this.safeInteger(subscription, 'numAttempts', 0);
407
+ // retry to synchronize if we have not reached maxAttempts yet
408
+ if (numAttempts < maxAttempts) {
409
+ // safety guard
410
+ if (messageHash in client.subscriptions) {
411
+ numAttempts = this.sum(numAttempts, 1);
412
+ const delayTime = this.sum(1000, lastTimestamp - snapshotTimestamp);
413
+ subscription['numAttempts'] = numAttempts;
414
+ client.subscriptions[messageHash] = subscription;
415
+ this.delay(delayTime, this.watchOrderBookSnapshot, client, message, subscription);
416
+ }
417
+ }
418
+ else {
419
+ // throw upon failing to synchronize in maxAttempts
420
+ throw new errors.InvalidNonce(this.id + ' failed to synchronize WebSocket feed with the snapshot for symbol ' + symbol + ' in ' + maxAttempts.toString() + ' attempts');
421
+ }
422
+ }
423
+ else {
424
+ orderbook.reset(snapshot);
425
+ // unroll the accumulated deltas
426
+ for (let i = 0; i < messages.length; i++) {
427
+ this.handleOrderBookMessage(client, messages[i]);
428
+ }
429
+ orderbook.cache = [];
430
+ this.orderbooks[symbol] = orderbook;
431
+ client.resolve(orderbook, messageHash);
432
+ }
433
+ }
434
+ catch (e) {
435
+ client.reject(e, messageHash);
436
+ }
437
+ }
438
+ async watchOrderBookSnapshot(client, message, subscription) {
439
+ const messageHash = this.safeString(subscription, 'messageHash');
440
+ const symbol = this.safeString(subscription, 'symbol');
441
+ const limit = this.safeInteger(subscription, 'limit');
442
+ const timestamp = this.safeInteger(message, 'ts');
443
+ const params = this.safeValue(subscription, 'params');
444
+ const attempts = this.safeInteger(subscription, 'numAttempts', 0);
445
+ const market = this.market(symbol);
446
+ const url = this.getUrlByMarketType(market['type'], market['linear'], false, true);
447
+ const requestId = this.requestId();
448
+ const request = {
449
+ 'req': messageHash,
450
+ 'id': requestId,
451
+ };
452
+ // this is a temporary subscription by a specific requestId
453
+ // it has a very short lifetime until the snapshot is received over ws
454
+ const snapshotSubscription = {
455
+ 'id': requestId,
456
+ 'messageHash': messageHash,
457
+ 'symbol': symbol,
458
+ 'limit': limit,
459
+ 'params': params,
460
+ 'numAttempts': attempts,
461
+ 'lastTimestamp': timestamp,
462
+ 'method': this.handleOrderBookSnapshot,
463
+ };
464
+ try {
465
+ const orderbook = await this.watch(url, requestId, request, requestId, snapshotSubscription);
466
+ return orderbook.limit();
467
+ }
468
+ catch (e) {
469
+ delete client.subscriptions[messageHash];
470
+ client.reject(e, messageHash);
471
+ }
472
+ }
473
+ handleDelta(bookside, delta) {
474
+ const price = this.safeFloat(delta, 0);
475
+ const amount = this.safeFloat(delta, 1);
476
+ bookside.store(price, amount);
477
+ }
478
+ handleDeltas(bookside, deltas) {
479
+ for (let i = 0; i < deltas.length; i++) {
480
+ this.handleDelta(bookside, deltas[i]);
481
+ }
482
+ }
483
+ handleOrderBookMessage(client, message) {
484
+ // spot markets
485
+ //
486
+ // {
487
+ // "ch": "market.btcusdt.mbp.150",
488
+ // "ts": 1583472025885,
489
+ // "tick": {
490
+ // "seqNum": 104998984994,
491
+ // "prevSeqNum": 104998984977,
492
+ // "bids": [
493
+ // [9058.27, 0],
494
+ // [9058.43, 0],
495
+ // [9058.99, 0],
496
+ // ],
497
+ // "asks": [
498
+ // [9084.27, 0.2],
499
+ // [9085.69, 0],
500
+ // [9085.81, 0],
501
+ // ]
502
+ // }
503
+ // }
504
+ //
505
+ // non-spot market update
506
+ //
507
+ // {
508
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
509
+ // "tick":{
510
+ // "asks":[],
511
+ // "bids":[
512
+ // [43445.74,1],
513
+ // [43444.48,0 ],
514
+ // [40593.92,9]
515
+ // ],
516
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
517
+ // "event":"update",
518
+ // "id":152727500274,
519
+ // "mrid":152727500274,
520
+ // "ts":1645023376098,
521
+ // "version":37536690
522
+ // },
523
+ // "ts":1645023376098
524
+ // }
525
+ // non-spot market snapshot
526
+ //
527
+ // {
528
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
529
+ // "tick":{
530
+ // "asks":[
531
+ // [43445.74,1],
532
+ // [43444.48,0 ],
533
+ // [40593.92,9]
534
+ // ],
535
+ // "bids":[
536
+ // [43445.74,1],
537
+ // [43444.48,0 ],
538
+ // [40593.92,9]
539
+ // ],
540
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
541
+ // "event":"snapshot",
542
+ // "id":152727500274,
543
+ // "mrid":152727500274,
544
+ // "ts":1645023376098,
545
+ // "version":37536690
546
+ // },
547
+ // "ts":1645023376098
548
+ // }
549
+ //
550
+ const ch = this.safeValue(message, 'ch');
551
+ const parts = ch.split('.');
552
+ const marketId = this.safeString(parts, 1);
553
+ const market = this.safeMarket(marketId);
554
+ const symbol = market['symbol'];
555
+ const orderbook = this.orderbooks[symbol];
556
+ const tick = this.safeValue(message, 'tick', {});
557
+ const seqNum = this.safeInteger(tick, 'seqNum');
558
+ const prevSeqNum = this.safeInteger(tick, 'prevSeqNum');
559
+ const event = this.safeString(tick, 'event');
560
+ const version = this.safeInteger(tick, 'version');
561
+ const timestamp = this.safeInteger(message, 'ts');
562
+ if (event === 'snapshot') {
563
+ const snapshot = this.parseOrderBook(tick, symbol, timestamp);
564
+ orderbook.reset(snapshot);
565
+ orderbook['nonce'] = version;
566
+ }
567
+ if ((prevSeqNum !== undefined) && prevSeqNum > orderbook['nonce']) {
568
+ throw new errors.InvalidNonce(this.id + ' watchOrderBook() received a mesage out of order');
569
+ }
570
+ const spotConditon = market['spot'] && (prevSeqNum === orderbook['nonce']);
571
+ const nonSpotCondition = market['contract'] && (version - 1 === orderbook['nonce']);
572
+ if (spotConditon || nonSpotCondition) {
573
+ const asks = this.safeValue(tick, 'asks', []);
574
+ const bids = this.safeValue(tick, 'bids', []);
575
+ this.handleDeltas(orderbook['asks'], asks);
576
+ this.handleDeltas(orderbook['bids'], bids);
577
+ orderbook['nonce'] = spotConditon ? seqNum : version;
578
+ orderbook['timestamp'] = timestamp;
579
+ orderbook['datetime'] = this.iso8601(timestamp);
580
+ }
581
+ }
582
+ handleOrderBook(client, message) {
583
+ //
584
+ // deltas
585
+ //
586
+ // spot markets
587
+ //
588
+ // {
589
+ // "ch": "market.btcusdt.mbp.150",
590
+ // "ts": 1583472025885,
591
+ // "tick": {
592
+ // "seqNum": 104998984994,
593
+ // "prevSeqNum": 104998984977,
594
+ // "bids": [
595
+ // [9058.27, 0],
596
+ // [9058.43, 0],
597
+ // [9058.99, 0],
598
+ // ],
599
+ // "asks": [
600
+ // [9084.27, 0.2],
601
+ // [9085.69, 0],
602
+ // [9085.81, 0],
603
+ // ]
604
+ // }
605
+ // }
606
+ //
607
+ // non spot markets
608
+ //
609
+ // {
610
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
611
+ // "tick":{
612
+ // "asks":[],
613
+ // "bids":[
614
+ // [43445.74,1],
615
+ // [43444.48,0 ],
616
+ // [40593.92,9]
617
+ // ],
618
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
619
+ // "event":"update",
620
+ // "id":152727500274,
621
+ // "mrid":152727500274,
622
+ // "ts":1645023376098,
623
+ // "version":37536690
624
+ // },
625
+ // "ts":1645023376098
626
+ // }
627
+ //
628
+ const messageHash = this.safeString(message, 'ch');
629
+ const tick = this.safeValue(message, 'tick');
630
+ const event = this.safeString(tick, 'event');
631
+ const ch = this.safeValue(message, 'ch');
632
+ const parts = ch.split('.');
633
+ const marketId = this.safeString(parts, 1);
634
+ const symbol = this.safeSymbol(marketId);
635
+ let orderbook = this.safeValue(this.orderbooks, symbol);
636
+ if (orderbook === undefined) {
637
+ const size = this.safeString(parts, 3);
638
+ const sizeParts = size.split('_');
639
+ const limit = this.safeInteger(sizeParts, 1);
640
+ orderbook = this.orderBook({}, limit);
641
+ this.orderbooks[symbol] = orderbook;
642
+ }
643
+ if ((event === undefined) && (orderbook['nonce'] === undefined)) {
644
+ orderbook.cache.push(message);
645
+ }
646
+ else {
647
+ this.handleOrderBookMessage(client, message);
648
+ client.resolve(orderbook, messageHash);
649
+ }
650
+ }
651
+ handleOrderBookSubscription(client, message, subscription) {
652
+ const symbol = this.safeString(subscription, 'symbol');
653
+ const market = this.market(symbol);
654
+ const limit = this.safeInteger(subscription, 'limit');
655
+ this.orderbooks[symbol] = this.orderBook({}, limit);
656
+ if (market['spot']) {
657
+ this.spawn(this.watchOrderBookSnapshot, client, message, subscription);
658
+ }
659
+ }
660
+ async watchMyTrades(symbol = undefined, since = undefined, limit = undefined, params = {}) {
661
+ /**
662
+ * @method
663
+ * @name huobi#watchMyTrades
664
+ * @description watches information on multiple trades made by the user
665
+ * @param {string} symbol unified market symbol of the market trades were made in
666
+ * @param {int} [since] the earliest time in ms to fetch trades for
667
+ * @param {int} [limit] the maximum number of trade structures to retrieve
668
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
669
+ * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=trade-structure
670
+ */
671
+ this.checkRequiredCredentials();
672
+ await this.loadMarkets();
673
+ let type = undefined;
674
+ let marketId = '*'; // wildcard
675
+ let market = undefined;
676
+ let messageHash = undefined;
677
+ let channel = undefined;
678
+ let trades = undefined;
679
+ let subType = undefined;
680
+ if (symbol !== undefined) {
681
+ market = this.market(symbol);
682
+ symbol = market['symbol'];
683
+ type = market['type'];
684
+ subType = market['linear'] ? 'linear' : 'inverse';
685
+ marketId = market['lowercaseId'];
686
+ }
687
+ else {
688
+ type = this.safeString(this.options, 'defaultType', 'spot');
689
+ type = this.safeString(params, 'type', type);
690
+ subType = this.safeString2(this.options, 'subType', 'defaultSubType', 'linear');
691
+ subType = this.safeString(params, 'subType', subType);
692
+ params = this.omit(params, ['type', 'subType']);
693
+ }
694
+ if (type === 'spot') {
695
+ let mode = undefined;
696
+ if (mode === undefined) {
697
+ mode = this.safeString2(this.options, 'watchMyTrades', 'mode', '0');
698
+ mode = this.safeString(params, 'mode', mode);
699
+ params = this.omit(params, 'mode');
700
+ }
701
+ messageHash = 'trade.clearing' + '#' + marketId + '#' + mode;
702
+ channel = messageHash;
703
+ }
704
+ else {
705
+ const channelAndMessageHash = this.getOrderChannelAndMessageHash(type, subType, market, params);
706
+ channel = this.safeString(channelAndMessageHash, 0);
707
+ const orderMessageHash = this.safeString(channelAndMessageHash, 1);
708
+ // we will take advantage of the order messageHash because already handles stuff
709
+ // like symbol/margin/subtype/type variations
710
+ messageHash = orderMessageHash + ':' + 'trade';
711
+ }
712
+ trades = await this.subscribePrivate(channel, messageHash, type, subType, params);
713
+ if (this.newUpdates) {
714
+ limit = trades.getLimit(symbol, limit);
715
+ }
716
+ return this.filterBySymbolSinceLimit(trades, symbol, since, limit, true);
717
+ }
718
+ getOrderChannelAndMessageHash(type, subType, market = undefined, params = {}) {
719
+ let messageHash = undefined;
720
+ let channel = undefined;
721
+ let orderType = this.safeString(this.options, 'orderType', 'orders'); // orders or matchOrders
722
+ orderType = this.safeString(params, 'orderType', orderType);
723
+ params = this.omit(params, 'orderType');
724
+ const marketCode = (market !== undefined) ? market['lowercaseId'].toLowerCase() : undefined;
725
+ const baseId = (market !== undefined) ? market['baseId'] : undefined;
726
+ const prefix = orderType;
727
+ messageHash = prefix;
728
+ if (subType === 'linear') {
729
+ // USDT Margined Contracts Example: LTC/USDT:USDT
730
+ const marginMode = this.safeString(params, 'margin', 'cross');
731
+ const marginPrefix = (marginMode === 'cross') ? prefix + '_cross' : prefix;
732
+ messageHash = marginPrefix;
733
+ if (marketCode !== undefined) {
734
+ messageHash += '.' + marketCode;
735
+ channel = messageHash;
736
+ }
737
+ else {
738
+ channel = marginPrefix + '.' + '*';
739
+ }
740
+ }
741
+ else if (type === 'future') {
742
+ // inverse futures Example: BCH/USD:BCH-220408
743
+ if (baseId !== undefined) {
744
+ channel = prefix + '.' + baseId.toLowerCase();
745
+ messageHash = channel;
746
+ }
747
+ else {
748
+ channel = prefix + '.' + '*';
749
+ }
750
+ }
751
+ else {
752
+ // inverse swaps: Example: BTC/USD:BTC
753
+ if (marketCode !== undefined) {
754
+ channel = prefix + '.' + marketCode;
755
+ messageHash = channel;
756
+ }
757
+ else {
758
+ channel = prefix + '.' + '*';
759
+ }
760
+ }
761
+ return [channel, messageHash];
762
+ }
763
+ async watchOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
764
+ /**
765
+ * @method
766
+ * @name huobi#watchOrders
767
+ * @description watches information on multiple orders made by the user
768
+ * @param {string} symbol unified market symbol of the market orders were made in
769
+ * @param {int} [since] the earliest time in ms to fetch orders for
770
+ * @param {int} [limit] the maximum number of order structures to retrieve
771
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
772
+ * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
773
+ */
774
+ await this.loadMarkets();
775
+ let type = undefined;
776
+ let subType = undefined;
777
+ let market = undefined;
778
+ let suffix = '*'; // wildcard
779
+ if (symbol !== undefined) {
780
+ market = this.market(symbol);
781
+ symbol = market['symbol'];
782
+ type = market['type'];
783
+ suffix = market['lowercaseId'];
784
+ subType = market['linear'] ? 'linear' : 'inverse';
785
+ }
786
+ else {
787
+ type = this.safeString(this.options, 'defaultType', 'spot');
788
+ type = this.safeString(params, 'type', type);
789
+ subType = this.safeString2(this.options, 'subType', 'defaultSubType', 'linear');
790
+ subType = this.safeString(params, 'subType', subType);
791
+ params = this.omit(params, ['type', 'subType']);
792
+ }
793
+ let messageHash = undefined;
794
+ let channel = undefined;
795
+ if (type === 'spot') {
796
+ messageHash = 'orders' + '#' + suffix;
797
+ channel = messageHash;
798
+ }
799
+ else {
800
+ const channelAndMessageHash = this.getOrderChannelAndMessageHash(type, subType, market, params);
801
+ channel = this.safeString(channelAndMessageHash, 0);
802
+ messageHash = this.safeString(channelAndMessageHash, 1);
803
+ }
804
+ const orders = await this.subscribePrivate(channel, messageHash, type, subType, params);
805
+ if (this.newUpdates) {
806
+ limit = orders.getLimit(symbol, limit);
807
+ }
808
+ return this.filterBySinceLimit(orders, since, limit, 'timestamp', true);
809
+ }
810
+ handleOrder(client, message) {
811
+ //
812
+ // spot
813
+ //
814
+ // {
815
+ // "action":"push",
816
+ // "ch":"orders#btcusdt", // or "orders#*" for global subscriptions
817
+ // "data": {
818
+ // "orderSource": "spot-web",
819
+ // "orderCreateTime": 1645116048355,
820
+ // "accountId": 44234548,
821
+ // "orderPrice": "100",
822
+ // "orderSize": "0.05",
823
+ // "symbol": "ethusdt",
824
+ // "type": "buy-limit",
825
+ // "orderId": "478861479986886",
826
+ // "eventType": "creation",
827
+ // "clientOrderId": '',
828
+ // "orderStatus": "submitted"
829
+ // }
830
+ // }
831
+ //
832
+ // spot wrapped trade
833
+ //
834
+ // {
835
+ // "action": "push",
836
+ // "ch": "orders#ltcusdt",
837
+ // "data": {
838
+ // "tradePrice": "130.01",
839
+ // "tradeVolume": "0.0385",
840
+ // "tradeTime": 1648714741525,
841
+ // "aggressor": true,
842
+ // "execAmt": "0.0385",
843
+ // "orderSource": "spot-web",
844
+ // "orderSize": "0.0385",
845
+ // "remainAmt": "0",
846
+ // "tradeId": 101541578884,
847
+ // "symbol": "ltcusdt",
848
+ // "type": "sell-market",
849
+ // "eventType": "trade",
850
+ // "clientOrderId": '',
851
+ // "orderStatus": "filled",
852
+ // "orderId": 509835753860328
853
+ // }
854
+ // }
855
+ //
856
+ // non spot order
857
+ //
858
+ // {
859
+ // "contract_type": "swap",
860
+ // "pair": "LTC-USDT",
861
+ // "business_type": "swap",
862
+ // "op": "notify",
863
+ // "topic": "orders_cross.ltc-usdt",
864
+ // "ts": 1650354508696,
865
+ // "symbol": "LTC",
866
+ // "contract_code": "LTC-USDT",
867
+ // "volume": 1,
868
+ // "price": 110.34,
869
+ // "order_price_type": "lightning",
870
+ // "direction": "sell",
871
+ // "offset": "close",
872
+ // "status": 6,
873
+ // "lever_rate": 1,
874
+ // "order_id": "966002354015051776",
875
+ // "order_id_str": "966002354015051776",
876
+ // "client_order_id": null,
877
+ // "order_source": "web",
878
+ // "order_type": 1,
879
+ // "created_at": 1650354508649,
880
+ // "trade_volume": 1,
881
+ // "trade_turnover": 11.072,
882
+ // "fee": -0.005536,
883
+ // "trade_avg_price": 110.72,
884
+ // "margin_frozen": 0,
885
+ // "profit": -0.045,
886
+ // "trade": [
887
+ // {
888
+ // "trade_fee": -0.005536,
889
+ // "fee_asset": "USDT",
890
+ // "real_profit": 0.473,
891
+ // "profit": -0.045,
892
+ // "trade_id": 86678766507,
893
+ // "id": "86678766507-966002354015051776-1",
894
+ // "trade_volume": 1,
895
+ // "trade_price": 110.72,
896
+ // "trade_turnover": 11.072,
897
+ // "created_at": 1650354508656,
898
+ // "role": "taker"
899
+ // }
900
+ // ],
901
+ // "canceled_at": 0,
902
+ // "fee_asset": "USDT",
903
+ // "margin_asset": "USDT",
904
+ // "uid": "359305390",
905
+ // "liquidation_type": "0",
906
+ // "margin_mode": "cross",
907
+ // "margin_account": "USDT",
908
+ // "is_tpsl": 0,
909
+ // "real_profit": 0.473,
910
+ // "trade_partition": "USDT",
911
+ // "reduce_only": 1
912
+ // }
913
+ //
914
+ //
915
+ const messageHash = this.safeString2(message, 'ch', 'topic');
916
+ const data = this.safeValue(message, 'data');
917
+ let marketId = this.safeString(message, 'contract_code');
918
+ if (marketId === undefined) {
919
+ marketId = this.safeString(data, 'symbol');
920
+ }
921
+ const market = this.safeMarket(marketId);
922
+ let parsedOrder = undefined;
923
+ if (data !== undefined) {
924
+ // spot updates
925
+ const eventType = this.safeString(data, 'eventType');
926
+ if (eventType === 'trade') {
927
+ // when a spot order is filled we get an update message
928
+ // with the trade info
929
+ const parsedTrade = this.parseOrderTrade(data, market);
930
+ // inject trade in existing order by faking an order object
931
+ const orderId = this.safeString(parsedTrade, 'order');
932
+ const trades = [parsedTrade];
933
+ const order = {
934
+ 'id': orderId,
935
+ 'trades': trades,
936
+ 'status': 'closed',
937
+ 'symbol': market['symbol'],
938
+ };
939
+ parsedOrder = order;
940
+ }
941
+ else {
942
+ parsedOrder = this.parseWsOrder(data, market);
943
+ }
944
+ }
945
+ else {
946
+ // contract branch
947
+ parsedOrder = this.parseWsOrder(message, market);
948
+ const rawTrades = this.safeValue(message, 'trade', []);
949
+ const tradesLength = rawTrades.length;
950
+ if (tradesLength > 0) {
951
+ const tradesObject = {
952
+ 'trades': rawTrades,
953
+ 'ch': messageHash,
954
+ 'symbol': marketId,
955
+ };
956
+ // inject order params in every trade
957
+ const extendTradeParams = {
958
+ 'order': this.safeString(parsedOrder, 'id'),
959
+ 'type': this.safeString(parsedOrder, 'type'),
960
+ 'side': this.safeString(parsedOrder, 'side'),
961
+ };
962
+ // trades arrive inside an order update
963
+ // we're forwarding them to handleMyTrade
964
+ // so they can be properly resolved
965
+ this.handleMyTrade(client, tradesObject, extendTradeParams);
966
+ }
967
+ }
968
+ if (this.orders === undefined) {
969
+ const limit = this.safeInteger(this.options, 'ordersLimit', 1000);
970
+ this.orders = new Cache.ArrayCacheBySymbolById(limit);
971
+ }
972
+ const cachedOrders = this.orders;
973
+ cachedOrders.append(parsedOrder);
974
+ client.resolve(this.orders, messageHash);
975
+ // when we make a global subscription (for contracts only) our message hash can't have a symbol/currency attached
976
+ // so we're removing it here
977
+ let genericMessageHash = messageHash.replace('.' + market['lowercaseId'], '');
978
+ const lowerCaseBaseId = this.safeStringLower(market, 'baseId');
979
+ genericMessageHash = genericMessageHash.replace('.' + lowerCaseBaseId, '');
980
+ client.resolve(this.orders, genericMessageHash);
981
+ }
982
+ parseWsOrder(order, market = undefined) {
983
+ //
984
+ // spot
985
+ //
986
+ // {
987
+ // "orderSource": "spot-web",
988
+ // "orderCreateTime": 1645116048355, // creating only
989
+ // "accountId": 44234548,
990
+ // "orderPrice": "100",
991
+ // "orderSize": "0.05",
992
+ // "orderValue": "3.71676361", // market-buy only
993
+ // "symbol": "ethusdt",
994
+ // "type": "buy-limit",
995
+ // "orderId": "478861479986886",
996
+ // "eventType": "creation",
997
+ // "clientOrderId": '',
998
+ // "orderStatus": "submitted"
999
+ // "lastActTime":1645118621810 // except creating
1000
+ // "execAmt":"0"
1001
+ // }
1002
+ //
1003
+ // swap order
1004
+ //
1005
+ // {
1006
+ // "contract_type": "swap",
1007
+ // "pair": "LTC-USDT",
1008
+ // "business_type": "swap",
1009
+ // "op": "notify",
1010
+ // "topic": "orders_cross.ltc-usdt",
1011
+ // "ts": 1648717911384,
1012
+ // "symbol": "LTC",
1013
+ // "contract_code": "LTC-USDT",
1014
+ // "volume": 1,
1015
+ // "price": 129.13,
1016
+ // "order_price_type": "lightning",
1017
+ // "direction": "sell",
1018
+ // "offset": "close",
1019
+ // "status": 6,
1020
+ // "lever_rate": 5,
1021
+ // "order_id": "959137967397068800",
1022
+ // "order_id_str": "959137967397068800",
1023
+ // "client_order_id": null,
1024
+ // "order_source": "web",
1025
+ // "order_type": 1,
1026
+ // "created_at": 1648717911344,
1027
+ // "trade_volume": 1,
1028
+ // "trade_turnover": 12.952,
1029
+ // "fee": -0.006476,
1030
+ // "trade_avg_price": 129.52,
1031
+ // "margin_frozen": 0,
1032
+ // "profit": -0.005,
1033
+ // "trade": [
1034
+ // {
1035
+ // "trade_fee": -0.006476,
1036
+ // "fee_asset": "USDT",
1037
+ // "real_profit": -0.005,
1038
+ // "profit": -0.005,
1039
+ // "trade_id": 83619995370,
1040
+ // "id": "83619995370-959137967397068800-1",
1041
+ // "trade_volume": 1,
1042
+ // "trade_price": 129.52,
1043
+ // "trade_turnover": 12.952,
1044
+ // "created_at": 1648717911352,
1045
+ // "role": "taker"
1046
+ // }
1047
+ // ],
1048
+ // "canceled_at": 0,
1049
+ // "fee_asset": "USDT",
1050
+ // "margin_asset": "USDT",
1051
+ // "uid": "359305390",
1052
+ // "liquidation_type": "0",
1053
+ // "margin_mode": "cross",
1054
+ // "margin_account": "USDT",
1055
+ // "is_tpsl": 0,
1056
+ // "real_profit": -0.005,
1057
+ // "trade_partition": "USDT",
1058
+ // "reduce_only": 1
1059
+ // }
1060
+ //
1061
+ // {
1062
+ // "op":"notify",
1063
+ // "topic":"orders.ada",
1064
+ // "ts":1604388667226,
1065
+ // "symbol":"ADA",
1066
+ // "contract_type":"quarter",
1067
+ // "contract_code":"ADA201225",
1068
+ // "volume":1,
1069
+ // "price":0.0905,
1070
+ // "order_price_type":"post_only",
1071
+ // "direction":"sell",
1072
+ // "offset":"open",
1073
+ // "status":6,
1074
+ // "lever_rate":20,
1075
+ // "order_id":773207641127878656,
1076
+ // "order_id_str":"773207641127878656",
1077
+ // "client_order_id":null,
1078
+ // "order_source":"web",
1079
+ // "order_type":1,
1080
+ // "created_at":1604388667146,
1081
+ // "trade_volume":1,
1082
+ // "trade_turnover":10,
1083
+ // "fee":-0.022099447513812154,
1084
+ // "trade_avg_price":0.0905,
1085
+ // "margin_frozen":0,
1086
+ // "profit":0,
1087
+ // "trade":[],
1088
+ // "canceled_at":0,
1089
+ // "fee_asset":"ADA",
1090
+ // "uid":"123456789",
1091
+ // "liquidation_type":"0",
1092
+ // "is_tpsl": 0,
1093
+ // "real_profit": 0
1094
+ // }
1095
+ //
1096
+ const lastTradeTimestamp = this.safeInteger2(order, 'lastActTime', 'ts');
1097
+ const created = this.safeInteger(order, 'orderCreateTime');
1098
+ const marketId = this.safeString2(order, 'contract_code', 'symbol');
1099
+ market = this.safeMarket(marketId, market);
1100
+ const symbol = this.safeSymbol(marketId, market);
1101
+ const amount = this.safeString2(order, 'orderSize', 'volume');
1102
+ const status = this.parseOrderStatus(this.safeString2(order, 'orderStatus', 'status'));
1103
+ const id = this.safeString2(order, 'orderId', 'order_id');
1104
+ const clientOrderId = this.safeString2(order, 'clientOrderId', 'client_order_id');
1105
+ const price = this.safeString2(order, 'orderPrice', 'price');
1106
+ const filled = this.safeString(order, 'execAmt');
1107
+ const typeSide = this.safeString(order, 'type');
1108
+ const feeCost = this.safeString(order, 'fee');
1109
+ let fee = undefined;
1110
+ if (feeCost !== undefined) {
1111
+ const feeCurrencyId = this.safeString(order, 'fee_asset');
1112
+ fee = {
1113
+ 'cost': feeCost,
1114
+ 'currency': this.safeCurrencyCode(feeCurrencyId),
1115
+ };
1116
+ }
1117
+ const avgPrice = this.safeString(order, 'trade_avg_price');
1118
+ const rawTrades = this.safeValue(order, 'trade');
1119
+ let typeSideParts = [];
1120
+ if (typeSide !== undefined) {
1121
+ typeSideParts = typeSide.split('-');
1122
+ }
1123
+ let type = this.safeStringLower(typeSideParts, 1);
1124
+ if (type === undefined) {
1125
+ type = this.safeString(order, 'order_price_type');
1126
+ }
1127
+ let side = this.safeStringLower(typeSideParts, 0);
1128
+ if (side === undefined) {
1129
+ side = this.safeString(order, 'direction');
1130
+ }
1131
+ const cost = this.safeString(order, 'orderValue');
1132
+ return this.safeOrder({
1133
+ 'info': order,
1134
+ 'id': id,
1135
+ 'clientOrderId': clientOrderId,
1136
+ 'timestamp': created,
1137
+ 'datetime': this.iso8601(created),
1138
+ 'lastTradeTimestamp': lastTradeTimestamp,
1139
+ 'status': status,
1140
+ 'symbol': symbol,
1141
+ 'type': type,
1142
+ 'timeInForce': undefined,
1143
+ 'postOnly': undefined,
1144
+ 'side': side,
1145
+ 'price': price,
1146
+ 'amount': amount,
1147
+ 'filled': filled,
1148
+ 'remaining': undefined,
1149
+ 'cost': cost,
1150
+ 'fee': fee,
1151
+ 'average': avgPrice,
1152
+ 'trades': rawTrades,
1153
+ }, market);
1154
+ }
1155
+ parseOrderTrade(trade, market = undefined) {
1156
+ // spot private wrapped trade
1157
+ //
1158
+ // {
1159
+ // "tradePrice": "130.01",
1160
+ // "tradeVolume": "0.0385",
1161
+ // "tradeTime": 1648714741525,
1162
+ // "aggressor": true,
1163
+ // "execAmt": "0.0385",
1164
+ // "orderSource": "spot-web",
1165
+ // "orderSize": "0.0385",
1166
+ // "remainAmt": "0",
1167
+ // "tradeId": 101541578884,
1168
+ // "symbol": "ltcusdt",
1169
+ // "type": "sell-market",
1170
+ // "eventType": "trade",
1171
+ // "clientOrderId": '',
1172
+ // "orderStatus": "filled",
1173
+ // "orderId": 509835753860328
1174
+ // }
1175
+ //
1176
+ market = this.safeMarket(undefined, market);
1177
+ const symbol = market['symbol'];
1178
+ const tradeId = this.safeString(trade, 'tradeId');
1179
+ const price = this.safeString(trade, 'tradePrice');
1180
+ const amount = this.safeString(trade, 'tradeVolume');
1181
+ const order = this.safeString(trade, 'orderId');
1182
+ const timestamp = this.safeInteger(trade, 'tradeTime');
1183
+ let type = this.safeString(trade, 'type');
1184
+ let side = undefined;
1185
+ if (type !== undefined) {
1186
+ const typeParts = type.split('-');
1187
+ side = typeParts[0];
1188
+ type = typeParts[1];
1189
+ }
1190
+ const aggressor = this.safeValue(trade, 'aggressor');
1191
+ let takerOrMaker = undefined;
1192
+ if (aggressor !== undefined) {
1193
+ takerOrMaker = aggressor ? 'taker' : 'maker';
1194
+ }
1195
+ return this.safeTrade({
1196
+ 'info': trade,
1197
+ 'timestamp': timestamp,
1198
+ 'datetime': this.iso8601(timestamp),
1199
+ 'symbol': symbol,
1200
+ 'id': tradeId,
1201
+ 'order': order,
1202
+ 'type': type,
1203
+ 'takerOrMaker': takerOrMaker,
1204
+ 'side': side,
1205
+ 'price': price,
1206
+ 'amount': amount,
1207
+ 'cost': undefined,
1208
+ 'fee': undefined,
1209
+ }, market);
1210
+ }
1211
+ async watchPositions(symbols = undefined, since = undefined, limit = undefined, params = {}) {
1212
+ /**
1213
+ * @method
1214
+ * @name huobi#watchPositions
1215
+ * @see https://www.huobi.com/en-in/opend/newApiPages/?id=8cb7de1c-77b5-11ed-9966-0242ac110003
1216
+ * @see https://www.huobi.com/en-in/opend/newApiPages/?id=8cb7df0f-77b5-11ed-9966-0242ac110003
1217
+ * @see https://www.huobi.com/en-in/opend/newApiPages/?id=28c34a7d-77ae-11ed-9966-0242ac110003
1218
+ * @see https://www.huobi.com/en-in/opend/newApiPages/?id=5d5156b5-77b6-11ed-9966-0242ac110003
1219
+ * @description watch all open positions. Note: huobi has one channel for each marginMode and type
1220
+ * @param {string[]|undefined} symbols list of unified market symbols
1221
+ * @param {object} params extra parameters specific to the exchange API endpoint
1222
+ * @returns {object[]} a list of [position structure]{@link https://docs.ccxt.com/en/latest/manual.html#position-structure}
1223
+ */
1224
+ await this.loadMarkets();
1225
+ let market = undefined;
1226
+ let messageHash = '';
1227
+ if (!this.isEmpty(symbols)) {
1228
+ market = this.getMarketFromSymbols(symbols);
1229
+ messageHash = '::' + symbols.join(',');
1230
+ }
1231
+ let type = undefined;
1232
+ let subType = undefined;
1233
+ if (market !== undefined) {
1234
+ type = market['type'];
1235
+ subType = market['linear'] ? 'linear' : 'inverse';
1236
+ }
1237
+ else {
1238
+ [type, params] = this.handleMarketTypeAndParams('watchPositions', market, params);
1239
+ if (type === 'spot') {
1240
+ type = 'future';
1241
+ }
1242
+ [subType, params] = this.handleOptionAndParams(params, 'watchPositions', 'subType', subType);
1243
+ }
1244
+ symbols = this.marketSymbols(symbols);
1245
+ let marginMode = undefined;
1246
+ [marginMode, params] = this.handleMarginModeAndParams('watchPositions', params, 'cross');
1247
+ const isLinear = (subType === 'linear');
1248
+ const url = this.getUrlByMarketType(type, isLinear, true);
1249
+ messageHash = marginMode + ':positions' + messageHash;
1250
+ const channel = (marginMode === 'cross') ? 'positions_cross.*' : 'positions.*';
1251
+ const newPositions = await this.subscribePrivate(channel, messageHash, type, subType, params);
1252
+ if (this.newUpdates) {
1253
+ return newPositions;
1254
+ }
1255
+ return this.filterBySymbolsSinceLimit(this.positions[url][marginMode], symbols, since, limit, false);
1256
+ }
1257
+ handlePositions(client, message) {
1258
+ //
1259
+ // {
1260
+ // op: 'notify',
1261
+ // topic: 'positions_cross',
1262
+ // ts: 1696767149650,
1263
+ // event: 'snapshot',
1264
+ // data: [
1265
+ // {
1266
+ // contract_type: 'swap',
1267
+ // pair: 'BTC-USDT',
1268
+ // business_type: 'swap',
1269
+ // liquidation_price: null,
1270
+ // symbol: 'BTC',
1271
+ // contract_code: 'BTC-USDT',
1272
+ // volume: 1,
1273
+ // available: 1,
1274
+ // frozen: 0,
1275
+ // cost_open: 27802.2,
1276
+ // cost_hold: 27802.2,
1277
+ // profit_unreal: 0.0175,
1278
+ // profit_rate: 0.000629446590557581,
1279
+ // profit: 0.0175,
1280
+ // margin_asset: 'USDT',
1281
+ // position_margin: 27.8197,
1282
+ // lever_rate: 1,
1283
+ // direction: 'buy',
1284
+ // last_price: 27819.7,
1285
+ // margin_mode: 'cross',
1286
+ // margin_account: 'USDT',
1287
+ // trade_partition: 'USDT',
1288
+ // position_mode: 'dual_side'
1289
+ // },
1290
+ // ]
1291
+ // }
1292
+ //
1293
+ const url = client.url;
1294
+ const topic = this.safeString(message, 'topic', '');
1295
+ const marginMode = (topic === 'positions_cross') ? 'cross' : 'isolated';
1296
+ if (this.positions === undefined) {
1297
+ this.positions = {};
1298
+ }
1299
+ const clientPositions = this.safeValue(this.positions, url);
1300
+ if (clientPositions === undefined) {
1301
+ this.positions[url] = {};
1302
+ }
1303
+ const clientMarginModePositions = this.safeValue(clientPositions, marginMode);
1304
+ if (clientMarginModePositions === undefined) {
1305
+ this.positions[url][marginMode] = new Cache.ArrayCacheBySymbolBySide();
1306
+ }
1307
+ const cache = this.positions[url][marginMode];
1308
+ const rawPositions = this.safeValue(message, 'data', []);
1309
+ const newPositions = [];
1310
+ const timestamp = this.safeInteger(message, 'ts');
1311
+ for (let i = 0; i < rawPositions.length; i++) {
1312
+ const rawPosition = rawPositions[i];
1313
+ const position = this.parsePosition(rawPosition);
1314
+ position['timestamp'] = timestamp;
1315
+ position['datetime'] = this.iso8601(timestamp);
1316
+ newPositions.push(position);
1317
+ cache.append(position);
1318
+ }
1319
+ const messageHashes = this.findMessageHashes(client, marginMode + ':positions::');
1320
+ for (let i = 0; i < messageHashes.length; i++) {
1321
+ const messageHash = messageHashes[i];
1322
+ const parts = messageHash.split('::');
1323
+ const symbolsString = parts[1];
1324
+ const symbols = symbolsString.split(',');
1325
+ const positions = this.filterByArray(newPositions, 'symbol', symbols, false);
1326
+ if (!this.isEmpty(positions)) {
1327
+ client.resolve(positions, messageHash);
1328
+ }
1329
+ }
1330
+ client.resolve(newPositions, marginMode + ':positions');
1331
+ }
1332
+ async watchBalance(params = {}) {
1333
+ /**
1334
+ * @method
1335
+ * @name huobi#watchBalance
1336
+ * @description watch balance and get the amount of funds available for trading or funds locked in orders
1337
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1338
+ * @returns {object} a [balance structure]{@link https://docs.ccxt.com/#/?id=balance-structure}
1339
+ */
1340
+ let type = undefined;
1341
+ [type, params] = this.handleMarketTypeAndParams('watchBalance', undefined, params);
1342
+ let subType = undefined;
1343
+ [subType, params] = this.handleSubTypeAndParams('watchBalance', undefined, params, 'linear');
1344
+ const isUnifiedAccount = this.safeValue2(params, 'isUnifiedAccount', 'unified', false);
1345
+ params = this.omit(params, ['isUnifiedAccount', 'unified']);
1346
+ await this.loadMarkets();
1347
+ let messageHash = undefined;
1348
+ let channel = undefined;
1349
+ let marginMode = undefined;
1350
+ if (type === 'spot') {
1351
+ let mode = this.safeString2(this.options, 'watchBalance', 'mode', '2');
1352
+ mode = this.safeString(params, 'mode', mode);
1353
+ messageHash = 'accounts.update' + '#' + mode;
1354
+ channel = messageHash;
1355
+ }
1356
+ else {
1357
+ const symbol = this.safeString(params, 'symbol');
1358
+ const currency = this.safeString(params, 'currency');
1359
+ const market = (symbol !== undefined) ? this.market(symbol) : undefined;
1360
+ const currencyCode = (currency !== undefined) ? this.currency(currency) : undefined;
1361
+ marginMode = this.safeString(params, 'margin', 'cross');
1362
+ params = this.omit(params, ['currency', 'symbol', 'margin']);
1363
+ let prefix = 'accounts';
1364
+ messageHash = prefix;
1365
+ if (subType === 'linear') {
1366
+ if (isUnifiedAccount) {
1367
+ // usdt contracts account
1368
+ prefix = 'accounts_unify';
1369
+ messageHash = prefix;
1370
+ channel = prefix + '.' + 'usdt';
1371
+ }
1372
+ else {
1373
+ // usdt contracts account
1374
+ prefix = (marginMode === 'cross') ? prefix + '_cross' : prefix;
1375
+ messageHash = prefix;
1376
+ if (marginMode === 'isolated') {
1377
+ // isolated margin only allows filtering by symbol3
1378
+ if (symbol !== undefined) {
1379
+ messageHash += '.' + market['id'];
1380
+ channel = messageHash;
1381
+ }
1382
+ else {
1383
+ // subscribe to all
1384
+ channel = prefix + '.' + '*';
1385
+ }
1386
+ }
1387
+ else {
1388
+ // cross margin
1389
+ if (currencyCode !== undefined) {
1390
+ channel = prefix + '.' + currencyCode['id'];
1391
+ messageHash = channel;
1392
+ }
1393
+ else {
1394
+ // subscribe to all
1395
+ channel = prefix + '.' + '*';
1396
+ }
1397
+ }
1398
+ }
1399
+ }
1400
+ else if (type === 'future') {
1401
+ // inverse futures account
1402
+ if (currencyCode !== undefined) {
1403
+ messageHash += '.' + currencyCode['id'];
1404
+ channel = messageHash;
1405
+ }
1406
+ else {
1407
+ // subscribe to all
1408
+ channel = prefix + '.' + '*';
1409
+ }
1410
+ }
1411
+ else {
1412
+ // inverse swaps account
1413
+ if (market !== undefined) {
1414
+ messageHash += '.' + market['id'];
1415
+ channel = messageHash;
1416
+ }
1417
+ else {
1418
+ // subscribe to all
1419
+ channel = prefix + '.' + '*';
1420
+ }
1421
+ }
1422
+ }
1423
+ const subscriptionParams = {
1424
+ 'type': type,
1425
+ 'subType': subType,
1426
+ 'margin': marginMode,
1427
+ };
1428
+ // we are differentiating the channel from the messageHash for global subscriptions (*)
1429
+ // because huobi returns a different topic than the topic sent. Example: we send
1430
+ // "accounts.*" and "accounts" is returned so we're setting channel = "accounts.*" and
1431
+ // messageHash = "accounts" allowing handleBalance to freely resolve the topic in the message
1432
+ return await this.subscribePrivate(channel, messageHash, type, subType, params, subscriptionParams);
1433
+ }
1434
+ handleBalance(client, message) {
1435
+ // spot
1436
+ //
1437
+ // {
1438
+ // "action": "push",
1439
+ // "ch": "accounts.update#0",
1440
+ // "data": {
1441
+ // "currency": "btc",
1442
+ // "accountId": 123456,
1443
+ // "balance": "23.111",
1444
+ // "available": "2028.699426619837209087",
1445
+ // "changeType": "transfer",
1446
+ // "accountType":"trade",
1447
+ // "seqNum": "86872993928",
1448
+ // "changeTime": 1568601800000
1449
+ // }
1450
+ // }
1451
+ //
1452
+ // inverse future
1453
+ //
1454
+ // {
1455
+ // "op":"notify",
1456
+ // "topic":"accounts.ada",
1457
+ // "ts":1604388667226,
1458
+ // "event":"order.match",
1459
+ // "data":[
1460
+ // {
1461
+ // "symbol":"ADA",
1462
+ // "margin_balance":446.417641681222726716,
1463
+ // "margin_static":445.554085945257745136,
1464
+ // "margin_position":11.049723756906077348,
1465
+ // "margin_frozen":0,
1466
+ // "margin_available":435.367917924316649368,
1467
+ // "profit_real":21.627049781983019459,
1468
+ // "profit_unreal":0.86355573596498158,
1469
+ // "risk_rate":40.000796572150656768,
1470
+ // "liquidation_price":0.018674308027108984,
1471
+ // "withdraw_available":423.927036163274725677,
1472
+ // "lever_rate":20,
1473
+ // "adjust_factor":0.4
1474
+ // }
1475
+ // ],
1476
+ // "uid":"123456789"
1477
+ // }
1478
+ //
1479
+ // usdt / linear future, swap
1480
+ //
1481
+ // {
1482
+ // "op":"notify",
1483
+ // "topic":"accounts.btc-usdt", // or "accounts" for global subscriptions
1484
+ // "ts":1603711370689,
1485
+ // "event":"order.open",
1486
+ // "data":[
1487
+ // {
1488
+ // "margin_mode":"cross",
1489
+ // "margin_account":"USDT",
1490
+ // "margin_asset":"USDT",
1491
+ // "margin_balance":30.959342395,
1492
+ // "margin_static":30.959342395,
1493
+ // "margin_position":0,
1494
+ // "margin_frozen":10,
1495
+ // "profit_real":0,
1496
+ // "profit_unreal":0,
1497
+ // "withdraw_available":20.959342395,
1498
+ // "risk_rate":153.796711975,
1499
+ // "position_mode":"dual_side",
1500
+ // "contract_detail":[
1501
+ // {
1502
+ // "symbol":"LTC",
1503
+ // "contract_code":"LTC-USDT",
1504
+ // "margin_position":0,
1505
+ // "margin_frozen":0,
1506
+ // "margin_available":20.959342395,
1507
+ // "profit_unreal":0,
1508
+ // "liquidation_price":null,
1509
+ // "lever_rate":1,
1510
+ // "adjust_factor":0.01,
1511
+ // "contract_type":"swap",
1512
+ // "pair":"LTC-USDT",
1513
+ // "business_type":"swap",
1514
+ // "trade_partition":"USDT"
1515
+ // },
1516
+ // ],
1517
+ // "futures_contract_detail":[],
1518
+ // }
1519
+ // ]
1520
+ // }
1521
+ //
1522
+ // inverse future
1523
+ //
1524
+ // {
1525
+ // "op":"notify",
1526
+ // "topic":"accounts.ada",
1527
+ // "ts":1604388667226,
1528
+ // "event":"order.match",
1529
+ // "data":[
1530
+ // {
1531
+ // "symbol":"ADA",
1532
+ // "margin_balance":446.417641681222726716,
1533
+ // "margin_static":445.554085945257745136,
1534
+ // "margin_position":11.049723756906077348,
1535
+ // "margin_frozen":0,
1536
+ // "margin_available":435.367917924316649368,
1537
+ // "profit_real":21.627049781983019459,
1538
+ // "profit_unreal":0.86355573596498158,
1539
+ // "risk_rate":40.000796572150656768,
1540
+ // "liquidation_price":0.018674308027108984,
1541
+ // "withdraw_available":423.927036163274725677,
1542
+ // "lever_rate":20,
1543
+ // "adjust_factor":0.4
1544
+ // }
1545
+ // ],
1546
+ // "uid":"123456789"
1547
+ // }
1548
+ //
1549
+ const channel = this.safeString(message, 'ch');
1550
+ const data = this.safeValue(message, 'data', []);
1551
+ const timestamp = this.safeInteger(data, 'changeTime', this.safeInteger(message, 'ts'));
1552
+ this.balance['timestamp'] = timestamp;
1553
+ this.balance['datetime'] = this.iso8601(timestamp);
1554
+ this.balance['info'] = data;
1555
+ if (channel !== undefined) {
1556
+ // spot balance
1557
+ const currencyId = this.safeString(data, 'currency');
1558
+ const code = this.safeCurrencyCode(currencyId);
1559
+ const account = this.account();
1560
+ account['free'] = this.safeString(data, 'available');
1561
+ account['total'] = this.safeString(data, 'balance');
1562
+ this.balance[code] = account;
1563
+ this.balance = this.safeBalance(this.balance);
1564
+ client.resolve(this.balance, channel);
1565
+ }
1566
+ else {
1567
+ // contract balance
1568
+ const dataLength = data.length;
1569
+ if (dataLength === 0) {
1570
+ return;
1571
+ }
1572
+ const first = this.safeValue(data, 0, {});
1573
+ const topic = this.safeString(message, 'topic');
1574
+ const splitTopic = topic.split('.');
1575
+ let messageHash = this.safeString(splitTopic, 0);
1576
+ let subscription = this.safeValue2(client.subscriptions, messageHash, messageHash + '.*');
1577
+ if (subscription === undefined) {
1578
+ // if subscription not found means that we subscribed to a specific currency/symbol
1579
+ // and we use the first data entry to find it
1580
+ // Example: topic = 'accounts'
1581
+ // client.subscription hash = 'accounts.usdt'
1582
+ // we do 'accounts' + '.' + data[0]]['margin_asset'] to get it
1583
+ const currencyId = this.safeString2(first, 'margin_asset', 'symbol');
1584
+ messageHash += '.' + currencyId.toLowerCase();
1585
+ subscription = this.safeValue(client.subscriptions, messageHash);
1586
+ }
1587
+ const type = this.safeString(subscription, 'type');
1588
+ const subType = this.safeString(subscription, 'subType');
1589
+ if (topic === 'accounts_unify') {
1590
+ // {
1591
+ // "margin_asset": "USDT",
1592
+ // "margin_static": 10,
1593
+ // "cross_margin_static": 10,
1594
+ // "margin_balance": 10,
1595
+ // "cross_profit_unreal": 0,
1596
+ // "margin_frozen": 0,
1597
+ // "withdraw_available": 10,
1598
+ // "cross_risk_rate": null,
1599
+ // "cross_swap": [],
1600
+ // "cross_future": [],
1601
+ // "isolated_swap": []
1602
+ // }
1603
+ const marginAsset = this.safeString(first, 'margin_asset');
1604
+ const code = this.safeCurrencyCode(marginAsset);
1605
+ const marginFrozen = this.safeString(first, 'margin_frozen');
1606
+ const unifiedAccount = this.account();
1607
+ unifiedAccount['free'] = this.safeString(first, 'withdraw_available');
1608
+ unifiedAccount['used'] = marginFrozen;
1609
+ this.balance[code] = unifiedAccount;
1610
+ this.balance = this.safeBalance(this.balance);
1611
+ client.resolve(this.balance, 'accounts_unify');
1612
+ }
1613
+ else if (subType === 'linear') {
1614
+ const margin = this.safeString(subscription, 'margin');
1615
+ if (margin === 'cross') {
1616
+ const fieldName = (type === 'future') ? 'futures_contract_detail' : 'contract_detail';
1617
+ const balances = this.safeValue(first, fieldName, []);
1618
+ const balancesLength = balances.length;
1619
+ if (balancesLength > 0) {
1620
+ for (let i = 0; i < balances.length; i++) {
1621
+ const balance = balances[i];
1622
+ const marketId = this.safeString2(balance, 'contract_code', 'margin_account');
1623
+ const market = this.safeMarket(marketId);
1624
+ const currencyId = this.safeString(balance, 'margin_asset');
1625
+ const currency = this.safeCurrency(currencyId);
1626
+ const code = this.safeString(market, 'settle', currency['code']);
1627
+ // the exchange outputs positions for delisted markets
1628
+ // https://www.huobi.com/support/en-us/detail/74882968522337
1629
+ // we skip it if the market was delisted
1630
+ if (code !== undefined) {
1631
+ const account = this.account();
1632
+ account['free'] = this.safeString2(balance, 'margin_balance', 'margin_available');
1633
+ account['used'] = this.safeString(balance, 'margin_frozen');
1634
+ const accountsByCode = {};
1635
+ accountsByCode[code] = account;
1636
+ const symbol = market['symbol'];
1637
+ this.balance[symbol] = this.safeBalance(accountsByCode);
1638
+ }
1639
+ }
1640
+ }
1641
+ }
1642
+ else {
1643
+ // isolated margin
1644
+ for (let i = 0; i < data.length; i++) {
1645
+ const isolatedBalance = data[i];
1646
+ const account = this.account();
1647
+ account['free'] = this.safeString(isolatedBalance, 'margin_balance', 'margin_available');
1648
+ account['used'] = this.safeString(isolatedBalance, 'margin_frozen');
1649
+ const currencyId = this.safeString2(isolatedBalance, 'margin_asset', 'symbol');
1650
+ const code = this.safeCurrencyCode(currencyId);
1651
+ this.balance[code] = account;
1652
+ this.balance = this.safeBalance(this.balance);
1653
+ }
1654
+ }
1655
+ }
1656
+ else {
1657
+ // inverse branch
1658
+ for (let i = 0; i < data.length; i++) {
1659
+ const balance = data[i];
1660
+ const currencyId = this.safeString(balance, 'symbol');
1661
+ const code = this.safeCurrencyCode(currencyId);
1662
+ const account = this.account();
1663
+ account['free'] = this.safeString(balance, 'margin_available');
1664
+ account['used'] = this.safeString(balance, 'margin_frozen');
1665
+ this.balance[code] = account;
1666
+ this.balance = this.safeBalance(this.balance);
1667
+ }
1668
+ }
1669
+ client.resolve(this.balance, messageHash);
1670
+ }
1671
+ }
1672
+ handleSubscriptionStatus(client, message) {
1673
+ //
1674
+ // {
1675
+ // "id": 1583414227,
1676
+ // "status": "ok",
1677
+ // "subbed": "market.btcusdt.mbp.150",
1678
+ // "ts": 1583414229143
1679
+ // }
1680
+ //
1681
+ const id = this.safeString(message, 'id');
1682
+ const subscriptionsById = this.indexBy(client.subscriptions, 'id');
1683
+ const subscription = this.safeValue(subscriptionsById, id);
1684
+ if (subscription !== undefined) {
1685
+ const method = this.safeValue(subscription, 'method');
1686
+ if (method !== undefined) {
1687
+ return method.call(this, client, message, subscription);
1688
+ }
1689
+ // clean up
1690
+ if (id in client.subscriptions) {
1691
+ delete client.subscriptions[id];
1692
+ }
1693
+ }
1694
+ return message;
1695
+ }
1696
+ handleSystemStatus(client, message) {
1697
+ //
1698
+ // todo: answer the question whether handleSystemStatus should be renamed
1699
+ // and unified as handleStatus for any usage pattern that
1700
+ // involves system status and maintenance updates
1701
+ //
1702
+ // {
1703
+ // "id": "1578090234088", // connectId
1704
+ // "type": "welcome",
1705
+ // }
1706
+ //
1707
+ return message;
1708
+ }
1709
+ handleSubject(client, message) {
1710
+ // spot
1711
+ // {
1712
+ // "ch": "market.btcusdt.mbp.150",
1713
+ // "ts": 1583472025885,
1714
+ // "tick": {
1715
+ // "seqNum": 104998984994,
1716
+ // "prevSeqNum": 104998984977,
1717
+ // "bids": [
1718
+ // [9058.27, 0],
1719
+ // [9058.43, 0],
1720
+ // [9058.99, 0],
1721
+ // ],
1722
+ // "asks": [
1723
+ // [9084.27, 0.2],
1724
+ // [9085.69, 0],
1725
+ // [9085.81, 0],
1726
+ // ]
1727
+ // }
1728
+ // }
1729
+ // non spot
1730
+ //
1731
+ // {
1732
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
1733
+ // "tick":{
1734
+ // "asks":[],
1735
+ // "bids":[
1736
+ // [43445.74,1],
1737
+ // [43444.48,0 ],
1738
+ // [40593.92,9]
1739
+ // ],
1740
+ // "ch":"market.BTC220218.depth.size_150.high_freq",
1741
+ // "event":"update",
1742
+ // "id":152727500274,
1743
+ // "mrid":152727500274,
1744
+ // "ts":1645023376098,
1745
+ // "version":37536690
1746
+ // },
1747
+ // "ts":1645023376098
1748
+ // }
1749
+ //
1750
+ // spot private trade
1751
+ //
1752
+ // {
1753
+ // "action":"push",
1754
+ // "ch":"trade.clearing#ltcusdt#1",
1755
+ // "data":{
1756
+ // "eventType":"trade",
1757
+ // "symbol":"ltcusdt",
1758
+ // // ...
1759
+ // },
1760
+ // }
1761
+ //
1762
+ // spot order
1763
+ //
1764
+ // {
1765
+ // "action":"push",
1766
+ // "ch":"orders#btcusdt",
1767
+ // "data": {
1768
+ // "orderSide":"buy",
1769
+ // "lastActTime":1583853365586,
1770
+ // "clientOrderId":"abc123",
1771
+ // "orderStatus":"rejected",
1772
+ // "symbol":"btcusdt",
1773
+ // "eventType":"trigger",
1774
+ // "errCode": 2002,
1775
+ // "errMessage":"invalid.client.order.id (NT)"
1776
+ // }
1777
+ // }
1778
+ //
1779
+ // contract order
1780
+ //
1781
+ // {
1782
+ // "op":"notify",
1783
+ // "topic":"orders.ada",
1784
+ // "ts":1604388667226,
1785
+ // // ?
1786
+ // }
1787
+ //
1788
+ const ch = this.safeValue(message, 'ch', '');
1789
+ const parts = ch.split('.');
1790
+ const type = this.safeString(parts, 0);
1791
+ if (type === 'market') {
1792
+ const methodName = this.safeString(parts, 2);
1793
+ const methods = {
1794
+ 'depth': this.handleOrderBook,
1795
+ 'mbp': this.handleOrderBook,
1796
+ 'detail': this.handleTicker,
1797
+ 'bbo': this.handleTicker,
1798
+ 'ticker': this.handleTicker,
1799
+ 'trade': this.handleTrades,
1800
+ 'kline': this.handleOHLCV,
1801
+ };
1802
+ const method = this.safeValue(methods, methodName);
1803
+ if (method === undefined) {
1804
+ return message;
1805
+ }
1806
+ else {
1807
+ return method.call(this, client, message);
1808
+ }
1809
+ }
1810
+ // private spot subjects
1811
+ const privateParts = ch.split('#');
1812
+ const privateType = this.safeString(privateParts, 0, '');
1813
+ if (privateType === 'trade.clearing') {
1814
+ this.handleMyTrade(client, message);
1815
+ return;
1816
+ }
1817
+ if (privateType.indexOf('accounts.update') >= 0) {
1818
+ this.handleBalance(client, message);
1819
+ return;
1820
+ }
1821
+ if (privateType === 'orders') {
1822
+ this.handleOrder(client, message);
1823
+ return;
1824
+ }
1825
+ // private contract subjects
1826
+ const op = this.safeString(message, 'op');
1827
+ if (op === 'notify') {
1828
+ const topic = this.safeString(message, 'topic', '');
1829
+ if (topic.indexOf('orders') >= 0) {
1830
+ this.handleOrder(client, message);
1831
+ }
1832
+ if (topic.indexOf('account') >= 0) {
1833
+ this.handleBalance(client, message);
1834
+ }
1835
+ if (topic.indexOf('positions') >= 0) {
1836
+ this.handlePositions(client, message);
1837
+ }
1838
+ }
1839
+ }
1840
+ async pong(client, message) {
1841
+ //
1842
+ // { ping: 1583491673714 }
1843
+ // { action: "ping", data: { ts: 1645108204665 } }
1844
+ // { op: "ping", ts: "1645202800015" }
1845
+ //
1846
+ try {
1847
+ const ping = this.safeInteger(message, 'ping');
1848
+ if (ping !== undefined) {
1849
+ await client.send({ 'pong': ping });
1850
+ return;
1851
+ }
1852
+ const action = this.safeString(message, 'action');
1853
+ if (action === 'ping') {
1854
+ const data = this.safeValue(message, 'data');
1855
+ const pingTs = this.safeInteger(data, 'ts');
1856
+ await client.send({ 'action': 'pong', 'data': { 'ts': pingTs } });
1857
+ return;
1858
+ }
1859
+ const op = this.safeString(message, 'op');
1860
+ if (op === 'ping') {
1861
+ const pingTs = this.safeInteger(message, 'ts');
1862
+ await client.send({ 'op': 'pong', 'ts': pingTs });
1863
+ }
1864
+ }
1865
+ catch (e) {
1866
+ const error = new errors.NetworkError(this.id + ' pong failed ' + this.json(e));
1867
+ client.reset(error);
1868
+ }
1869
+ }
1870
+ handlePing(client, message) {
1871
+ this.spawn(this.pong, client, message);
1872
+ }
1873
+ handleAuthenticate(client, message) {
1874
+ //
1875
+ // spot
1876
+ //
1877
+ // {
1878
+ // "action": "req",
1879
+ // "code": 200,
1880
+ // "ch": "auth",
1881
+ // "data": {}
1882
+ // }
1883
+ //
1884
+ // non spot
1885
+ //
1886
+ // {
1887
+ // "op": "auth",
1888
+ // "type": "api",
1889
+ // "err-code": 0,
1890
+ // "ts": 1645200307319,
1891
+ // "data": { "user-id": "35930539" }
1892
+ // }
1893
+ //
1894
+ const promise = client.futures['authenticated'];
1895
+ promise.resolve(message);
1896
+ }
1897
+ handleErrorMessage(client, message) {
1898
+ //
1899
+ // {
1900
+ // "action": "sub",
1901
+ // "code": 2002,
1902
+ // "ch": "accounts.update#2",
1903
+ // "message": "invalid.auth.state"
1904
+ // }
1905
+ //
1906
+ // {
1907
+ // "ts": 1586323747018,
1908
+ // "status": "error",
1909
+ // 'err-code': "bad-request",
1910
+ // 'err-msg': "invalid mbp.150.symbol linkusdt",
1911
+ // "id": "2"
1912
+ // }
1913
+ //
1914
+ // {
1915
+ // "op": "sub",
1916
+ // "cid": "1",
1917
+ // "topic": "accounts_unify.USDT",
1918
+ // "err-code": 4007,
1919
+ // 'err-msg': "Non - single account user is not available, please check through the cross and isolated account asset interface",
1920
+ // "ts": 1698419490189
1921
+ // }
1922
+ //
1923
+ const status = this.safeString(message, 'status');
1924
+ if (status === 'error') {
1925
+ const id = this.safeString(message, 'id');
1926
+ const subscriptionsById = this.indexBy(client.subscriptions, 'id');
1927
+ const subscription = this.safeValue(subscriptionsById, id);
1928
+ if (subscription !== undefined) {
1929
+ const errorCode = this.safeString(message, 'err-code');
1930
+ try {
1931
+ this.throwExactlyMatchedException(this.exceptions['ws']['exact'], errorCode, this.json(message));
1932
+ }
1933
+ catch (e) {
1934
+ const messageHash = this.safeString(subscription, 'messageHash');
1935
+ client.reject(e, messageHash);
1936
+ client.reject(e, id);
1937
+ if (id in client.subscriptions) {
1938
+ delete client.subscriptions[id];
1939
+ }
1940
+ }
1941
+ }
1942
+ return false;
1943
+ }
1944
+ const code = this.safeInteger2(message, 'code', 'err-code');
1945
+ if (code !== undefined && ((code !== 200) && (code !== 0))) {
1946
+ const feedback = this.id + ' ' + this.json(message);
1947
+ try {
1948
+ this.throwExactlyMatchedException(this.exceptions['ws']['exact'], code, feedback);
1949
+ }
1950
+ catch (e) {
1951
+ if (e instanceof errors.AuthenticationError) {
1952
+ client.reject(e, 'auth');
1953
+ const method = 'auth';
1954
+ if (method in client.subscriptions) {
1955
+ delete client.subscriptions[method];
1956
+ }
1957
+ return false;
1958
+ }
1959
+ else {
1960
+ client.reject(e);
1961
+ }
1962
+ }
1963
+ }
1964
+ return message;
1965
+ }
1966
+ handleMessage(client, message) {
1967
+ if (this.handleErrorMessage(client, message)) {
1968
+ //
1969
+ // {"id":1583414227,"status":"ok","subbed":"market.btcusdt.mbp.150","ts":1583414229143}
1970
+ //
1971
+ // first ping format
1972
+ //
1973
+ // {"ping": 1645106821667 }
1974
+ //
1975
+ // second ping format
1976
+ //
1977
+ // {"action":"ping","data":{"ts":1645106821667}}
1978
+ //
1979
+ // third pong format
1980
+ //
1981
+ //
1982
+ // auth spot
1983
+ //
1984
+ // {
1985
+ // "action": "req",
1986
+ // "code": 200,
1987
+ // "ch": "auth",
1988
+ // "data": {}
1989
+ // }
1990
+ //
1991
+ // auth non spot
1992
+ //
1993
+ // {
1994
+ // "op": "auth",
1995
+ // "type": "api",
1996
+ // "err-code": 0,
1997
+ // "ts": 1645200307319,
1998
+ // "data": { "user-id": "35930539" }
1999
+ // }
2000
+ //
2001
+ // trade
2002
+ //
2003
+ // {
2004
+ // "action":"push",
2005
+ // "ch":"trade.clearing#ltcusdt#1",
2006
+ // "data":{
2007
+ // "eventType":"trade",
2008
+ // // ?
2009
+ // }
2010
+ // }
2011
+ //
2012
+ if ('id' in message) {
2013
+ this.handleSubscriptionStatus(client, message);
2014
+ return;
2015
+ }
2016
+ if ('action' in message) {
2017
+ const action = this.safeString(message, 'action');
2018
+ if (action === 'ping') {
2019
+ this.handlePing(client, message);
2020
+ return;
2021
+ }
2022
+ if (action === 'sub') {
2023
+ this.handleSubscriptionStatus(client, message);
2024
+ return;
2025
+ }
2026
+ }
2027
+ if ('ch' in message) {
2028
+ if (message['ch'] === 'auth') {
2029
+ this.handleAuthenticate(client, message);
2030
+ return;
2031
+ }
2032
+ else {
2033
+ // route by channel aka topic aka subject
2034
+ this.handleSubject(client, message);
2035
+ return;
2036
+ }
2037
+ }
2038
+ if ('op' in message) {
2039
+ const op = this.safeString(message, 'op');
2040
+ if (op === 'ping') {
2041
+ this.handlePing(client, message);
2042
+ return;
2043
+ }
2044
+ if (op === 'auth') {
2045
+ this.handleAuthenticate(client, message);
2046
+ return;
2047
+ }
2048
+ if (op === 'sub') {
2049
+ this.handleSubscriptionStatus(client, message);
2050
+ return;
2051
+ }
2052
+ if (op === 'notify') {
2053
+ this.handleSubject(client, message);
2054
+ return;
2055
+ }
2056
+ }
2057
+ if ('ping' in message) {
2058
+ this.handlePing(client, message);
2059
+ }
2060
+ }
2061
+ }
2062
+ handleMyTrade(client, message, extendParams = {}) {
2063
+ //
2064
+ // spot
2065
+ //
2066
+ // {
2067
+ // "action":"push",
2068
+ // "ch":"trade.clearing#ltcusdt#1",
2069
+ // "data":{
2070
+ // "eventType":"trade",
2071
+ // "symbol":"ltcusdt",
2072
+ // "orderId":"478862728954426",
2073
+ // "orderSide":"buy",
2074
+ // "orderType":"buy-market",
2075
+ // "accountId":44234548,
2076
+ // "source":"spot-web",
2077
+ // "orderValue":"5.01724137",
2078
+ // "orderCreateTime":1645124660365,
2079
+ // "orderStatus":"filled",
2080
+ // "feeCurrency":"ltc",
2081
+ // "tradePrice":"118.89",
2082
+ // "tradeVolume":"0.042200701236437042",
2083
+ // "aggressor":true,
2084
+ // "tradeId":101539740584,
2085
+ // "tradeTime":1645124660368,
2086
+ // "transactFee":"0.000041778694224073",
2087
+ // "feeDeduct":"0",
2088
+ // "feeDeductType":""
2089
+ // }
2090
+ // }
2091
+ //
2092
+ // contract
2093
+ //
2094
+ // {
2095
+ // "symbol": "ADA/USDT:USDT"
2096
+ // "ch": "orders_cross.ada-usdt"
2097
+ // "trades": [
2098
+ // {
2099
+ // "trade_fee":-0.022099447513812154,
2100
+ // "fee_asset":"ADA",
2101
+ // "trade_id":113913755890,
2102
+ // "id":"113913755890-773207641127878656-1",
2103
+ // "trade_volume":1,
2104
+ // "trade_price":0.0905,
2105
+ // "trade_turnover":10,
2106
+ // "created_at":1604388667194,
2107
+ // "profit":0,
2108
+ // "real_profit": 0,
2109
+ // "role":"maker"
2110
+ // }
2111
+ // ],
2112
+ // }
2113
+ //
2114
+ if (this.myTrades === undefined) {
2115
+ const limit = this.safeInteger(this.options, 'tradesLimit', 1000);
2116
+ this.myTrades = new Cache.ArrayCacheBySymbolById(limit);
2117
+ }
2118
+ const cachedTrades = this.myTrades;
2119
+ const messageHash = this.safeString(message, 'ch');
2120
+ if (messageHash !== undefined) {
2121
+ const data = this.safeValue(message, 'data');
2122
+ if (data !== undefined) {
2123
+ const parsed = this.parseWsTrade(data);
2124
+ const symbol = this.safeString(parsed, 'symbol');
2125
+ if (symbol !== undefined) {
2126
+ cachedTrades.append(parsed);
2127
+ client.resolve(this.myTrades, messageHash);
2128
+ }
2129
+ }
2130
+ else {
2131
+ // this trades object is artificially created
2132
+ // in handleOrder
2133
+ const rawTrades = this.safeValue(message, 'trades', []);
2134
+ const marketId = this.safeValue(message, 'symbol');
2135
+ const market = this.market(marketId);
2136
+ for (let i = 0; i < rawTrades.length; i++) {
2137
+ const trade = rawTrades[i];
2138
+ let parsedTrade = this.parseTrade(trade, market);
2139
+ // add extra params (side, type, ...) coming from the order
2140
+ parsedTrade = this.extend(parsedTrade, extendParams);
2141
+ cachedTrades.append(parsedTrade);
2142
+ }
2143
+ // messageHash here is the orders one, so
2144
+ // we have to recreate the trades messageHash = orderMessageHash + ':' + 'trade'
2145
+ const tradesHash = messageHash + ':' + 'trade';
2146
+ client.resolve(this.myTrades, tradesHash);
2147
+ // when we make an global order sub we have to send the channel like this
2148
+ // ch = orders_cross.* and we store messageHash = 'orders_cross'
2149
+ // however it is returned with the specific order update symbol: ch = orders_cross.btc-usd
2150
+ // since this is a global sub, our messageHash does not specify any symbol (ex: orders_cross:trade)
2151
+ // so we must remove it
2152
+ let genericOrderHash = messageHash.replace('.' + market['lowercaseId'], '');
2153
+ const lowerCaseBaseId = this.safeStringLower(market, 'baseId');
2154
+ genericOrderHash = genericOrderHash.replace('.' + lowerCaseBaseId, '');
2155
+ const genericTradesHash = genericOrderHash + ':' + 'trade';
2156
+ client.resolve(this.myTrades, genericTradesHash);
2157
+ }
2158
+ }
2159
+ }
2160
+ parseWsTrade(trade, market = undefined) {
2161
+ // spot private
2162
+ //
2163
+ // {
2164
+ // "eventType":"trade",
2165
+ // "symbol":"ltcusdt",
2166
+ // "orderId":"478862728954426",
2167
+ // "orderSide":"buy",
2168
+ // "orderType":"buy-market",
2169
+ // "accountId":44234548,
2170
+ // "source":"spot-web",
2171
+ // "orderValue":"5.01724137",
2172
+ // "orderCreateTime":1645124660365,
2173
+ // "orderStatus":"filled",
2174
+ // "feeCurrency":"ltc",
2175
+ // "tradePrice":"118.89",
2176
+ // "tradeVolume":"0.042200701236437042",
2177
+ // "aggressor":true,
2178
+ // "tradeId":101539740584,
2179
+ // "tradeTime":1645124660368,
2180
+ // "transactFee":"0.000041778694224073",
2181
+ // "feeDeduct":"0",
2182
+ // "feeDeductType":""
2183
+ // }
2184
+ //
2185
+ const symbol = this.safeSymbol(this.safeString(trade, 'symbol'));
2186
+ const side = this.safeString2(trade, 'side', 'orderSide');
2187
+ const tradeId = this.safeString(trade, 'tradeId');
2188
+ const price = this.safeString(trade, 'tradePrice');
2189
+ const amount = this.safeString(trade, 'tradeVolume');
2190
+ const order = this.safeString(trade, 'orderId');
2191
+ const timestamp = this.safeInteger(trade, 'tradeTime');
2192
+ market = this.market(symbol);
2193
+ const orderType = this.safeString(trade, 'orderType');
2194
+ const aggressor = this.safeValue(trade, 'aggressor');
2195
+ let takerOrMaker = undefined;
2196
+ if (aggressor !== undefined) {
2197
+ takerOrMaker = aggressor ? 'taker' : 'maker';
2198
+ }
2199
+ let type = undefined;
2200
+ let orderTypeParts = [];
2201
+ if (orderType !== undefined) {
2202
+ orderTypeParts = orderType.split('-');
2203
+ type = this.safeString(orderTypeParts, 1);
2204
+ }
2205
+ let fee = undefined;
2206
+ const feeCurrency = this.safeCurrencyCode(this.safeString(trade, 'feeCurrency'));
2207
+ if (feeCurrency !== undefined) {
2208
+ fee = {
2209
+ 'cost': this.safeString(trade, 'transactFee'),
2210
+ 'currency': feeCurrency,
2211
+ };
2212
+ }
2213
+ return this.safeTrade({
2214
+ 'info': trade,
2215
+ 'timestamp': timestamp,
2216
+ 'datetime': this.iso8601(timestamp),
2217
+ 'symbol': symbol,
2218
+ 'id': tradeId,
2219
+ 'order': order,
2220
+ 'type': type,
2221
+ 'takerOrMaker': takerOrMaker,
2222
+ 'side': side,
2223
+ 'price': price,
2224
+ 'amount': amount,
2225
+ 'cost': undefined,
2226
+ 'fee': fee,
2227
+ }, market);
2228
+ }
2229
+ getUrlByMarketType(type, isLinear = true, isPrivate = false, isFeed = false) {
2230
+ const api = this.safeString(this.options, 'api', 'api');
2231
+ const hostname = { 'hostname': this.hostname };
2232
+ let hostnameURL = undefined;
2233
+ let url = undefined;
2234
+ if (type === 'spot') {
2235
+ if (isPrivate) {
2236
+ hostnameURL = this.urls['api']['ws'][api]['spot']['private'];
2237
+ }
2238
+ else {
2239
+ if (isFeed) {
2240
+ hostnameURL = this.urls['api']['ws'][api]['spot']['feed'];
2241
+ }
2242
+ else {
2243
+ hostnameURL = this.urls['api']['ws'][api]['spot']['public'];
2244
+ }
2245
+ }
2246
+ url = this.implodeParams(hostnameURL, hostname);
2247
+ }
2248
+ else {
2249
+ const baseUrl = this.urls['api']['ws'][api][type];
2250
+ const subTypeUrl = isLinear ? baseUrl['linear'] : baseUrl['inverse'];
2251
+ url = isPrivate ? subTypeUrl['private'] : subTypeUrl['public'];
2252
+ }
2253
+ return url;
2254
+ }
2255
+ async subscribePublic(url, symbol, messageHash, method = undefined, params = {}) {
2256
+ const requestId = this.requestId();
2257
+ const request = {
2258
+ 'sub': messageHash,
2259
+ 'id': requestId,
2260
+ };
2261
+ const subscription = {
2262
+ 'id': requestId,
2263
+ 'messageHash': messageHash,
2264
+ 'symbol': symbol,
2265
+ 'params': params,
2266
+ };
2267
+ if (method !== undefined) {
2268
+ subscription['method'] = method;
2269
+ }
2270
+ return await this.watch(url, messageHash, this.extend(request, params), messageHash, subscription);
2271
+ }
2272
+ async subscribePrivate(channel, messageHash, type, subtype, params = {}, subscriptionParams = {}) {
2273
+ const requestId = this.requestId();
2274
+ const subscription = {
2275
+ 'id': requestId,
2276
+ 'messageHash': messageHash,
2277
+ 'params': params,
2278
+ };
2279
+ const extendedSubsription = this.extend(subscription, subscriptionParams);
2280
+ let request = undefined;
2281
+ if (type === 'spot') {
2282
+ request = {
2283
+ 'action': 'sub',
2284
+ 'ch': channel,
2285
+ };
2286
+ }
2287
+ else {
2288
+ request = {
2289
+ 'op': 'sub',
2290
+ 'topic': channel,
2291
+ 'cid': requestId,
2292
+ };
2293
+ }
2294
+ const isLinear = subtype === 'linear';
2295
+ const url = this.getUrlByMarketType(type, isLinear, true);
2296
+ const hostname = (type === 'spot') ? this.urls['hostnames']['spot'] : this.urls['hostnames']['contract'];
2297
+ const authParams = {
2298
+ 'type': type,
2299
+ 'url': url,
2300
+ 'hostname': hostname,
2301
+ };
2302
+ if (type === 'spot') {
2303
+ this.options['ws']['gunzip'] = false;
2304
+ }
2305
+ await this.authenticate(authParams);
2306
+ return await this.watch(url, messageHash, this.extend(request, params), channel, extendedSubsription);
2307
+ }
2308
+ async authenticate(params = {}) {
2309
+ const url = this.safeString(params, 'url');
2310
+ const hostname = this.safeString(params, 'hostname');
2311
+ const type = this.safeString(params, 'type');
2312
+ if (url === undefined || hostname === undefined || type === undefined) {
2313
+ throw new errors.ArgumentsRequired(this.id + ' authenticate requires a url, hostname and type argument');
2314
+ }
2315
+ this.checkRequiredCredentials();
2316
+ const messageHash = 'authenticated';
2317
+ const relativePath = url.replace('wss://' + hostname, '');
2318
+ const client = this.client(url);
2319
+ const future = client.future(messageHash);
2320
+ const authenticated = this.safeValue(client.subscriptions, messageHash);
2321
+ if (authenticated === undefined) {
2322
+ const timestamp = this.ymdhms(this.milliseconds(), 'T');
2323
+ let signatureParams = undefined;
2324
+ if (type === 'spot') {
2325
+ signatureParams = {
2326
+ 'accessKey': this.apiKey,
2327
+ 'signatureMethod': 'HmacSHA256',
2328
+ 'signatureVersion': '2.1',
2329
+ 'timestamp': timestamp,
2330
+ };
2331
+ }
2332
+ else {
2333
+ signatureParams = {
2334
+ 'AccessKeyId': this.apiKey,
2335
+ 'SignatureMethod': 'HmacSHA256',
2336
+ 'SignatureVersion': '2',
2337
+ 'Timestamp': timestamp,
2338
+ };
2339
+ }
2340
+ signatureParams = this.keysort(signatureParams);
2341
+ const auth = this.urlencode(signatureParams);
2342
+ const payload = ['GET', hostname, relativePath, auth].join("\n"); // eslint-disable-line quotes
2343
+ const signature = this.hmac(this.encode(payload), this.encode(this.secret), sha256.sha256, 'base64');
2344
+ let request = undefined;
2345
+ if (type === 'spot') {
2346
+ const newParams = {
2347
+ 'authType': 'api',
2348
+ 'accessKey': this.apiKey,
2349
+ 'signatureMethod': 'HmacSHA256',
2350
+ 'signatureVersion': '2.1',
2351
+ 'timestamp': timestamp,
2352
+ 'signature': signature,
2353
+ };
2354
+ request = {
2355
+ 'params': newParams,
2356
+ 'action': 'req',
2357
+ 'ch': 'auth',
2358
+ };
2359
+ }
2360
+ else {
2361
+ request = {
2362
+ 'op': 'auth',
2363
+ 'type': 'api',
2364
+ 'AccessKeyId': this.apiKey,
2365
+ 'SignatureMethod': 'HmacSHA256',
2366
+ 'SignatureVersion': '2',
2367
+ 'Timestamp': timestamp,
2368
+ 'Signature': signature,
2369
+ };
2370
+ }
2371
+ const requestId = this.requestId();
2372
+ const subscription = {
2373
+ 'id': requestId,
2374
+ 'messageHash': messageHash,
2375
+ 'params': params,
2376
+ };
2377
+ this.watch(url, messageHash, request, messageHash, subscription);
2378
+ }
2379
+ return future;
2380
+ }
2381
+ }
2382
+
2383
+ module.exports = htx;