ccxt 4.2.11 → 4.2.13

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 (392) hide show
  1. package/README.md +5 -5
  2. package/build.sh +2 -2
  3. package/dist/ccxt.browser.js +1347 -490
  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 +2209 -0
  125. package/dist/cjs/js/src/binance.js +9736 -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 +3807 -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 +8291 -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 +3424 -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 +7072 -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 +9049 -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 +761 -0
  185. package/dist/cjs/js/src/indodax.js +1069 -0
  186. package/dist/cjs/js/src/kraken.js +2857 -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 +2851 -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 +7330 -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 +4722 -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 +1608 -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 +1867 -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 +2765 -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/bingx.d.ts +2 -0
  336. package/js/src/abstract/bybit.d.ts +1 -0
  337. package/js/src/abstract/gate.d.ts +11 -0
  338. package/js/src/abstract/gateio.d.ts +11 -0
  339. package/js/src/abstract/okx.d.ts +1 -0
  340. package/js/src/alpaca.js +18 -18
  341. package/js/src/base/Exchange.d.ts +5 -1
  342. package/js/src/base/Exchange.js +101 -12
  343. package/js/src/bigone.d.ts +3 -2
  344. package/js/src/bigone.js +429 -167
  345. package/js/src/binance.js +48 -34
  346. package/js/src/bingx.js +115 -38
  347. package/js/src/bitfinex.d.ts +2 -2
  348. package/js/src/bitfinex.js +2 -3
  349. package/js/src/bitget.js +33 -13
  350. package/js/src/bitmart.d.ts +2 -2
  351. package/js/src/bitmart.js +5 -5
  352. package/js/src/bitmex.js +1 -0
  353. package/js/src/bybit.js +2 -0
  354. package/js/src/coinbase.d.ts +26 -3
  355. package/js/src/coinbase.js +176 -26
  356. package/js/src/coinlist.js +3 -4
  357. package/js/src/coinone.js +1 -1
  358. package/js/src/coinsph.js +2 -3
  359. package/js/src/deribit.js +1 -0
  360. package/js/src/gate.d.ts +4 -4
  361. package/js/src/gate.js +96 -59
  362. package/js/src/gemini.js +1 -1
  363. package/js/src/hitbtc.d.ts +4 -4
  364. package/js/src/hitbtc.js +2 -3
  365. package/js/src/htx.d.ts +1 -0
  366. package/js/src/htx.js +29 -7
  367. package/js/src/huobijp.js +2 -3
  368. package/js/src/independentreserve.js +7 -5
  369. package/js/src/kraken.js +3 -6
  370. package/js/src/lbank.js +59 -33
  371. package/js/src/mexc.js +2 -1
  372. package/js/src/oceanex.js +1 -1
  373. package/js/src/okx.js +14 -3
  374. package/js/src/phemex.js +9 -2
  375. package/js/src/pro/binance.d.ts +2 -23
  376. package/js/src/pro/binance.js +58 -22
  377. package/js/src/pro/coinbase.d.ts +2 -2
  378. package/js/src/pro/coinbase.js +4 -1
  379. package/js/src/pro/coinbasepro.d.ts +2 -2
  380. package/js/src/pro/hitbtc.d.ts +2 -2
  381. package/js/src/pro/kraken.js +1 -1
  382. package/js/src/pro/okx.d.ts +1 -0
  383. package/js/src/pro/okx.js +52 -2
  384. package/js/src/pro/poloniex.d.ts +2 -2
  385. package/js/src/probit.js +4 -2
  386. package/js/src/upbit.d.ts +3 -101
  387. package/js/src/upbit.js +12 -12
  388. package/js/src/wavesexchange.js +1 -1
  389. package/js/src/woo.d.ts +2 -0
  390. package/js/src/woo.js +52 -0
  391. package/package.json +1 -1
  392. package/skip-tests.json +5 -0
@@ -0,0 +1,3807 @@
1
+ 'use strict';
2
+
3
+ var bingx$1 = require('./abstract/bingx.js');
4
+ var errors = require('./base/errors.js');
5
+ var Precise = require('./base/Precise.js');
6
+ var sha256 = require('./static_dependencies/noble-hashes/sha256.js');
7
+ var number = require('./base/functions/number.js');
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // ---------------------------------------------------------------------------
11
+ class bingx extends bingx$1 {
12
+ describe() {
13
+ return this.deepExtend(super.describe(), {
14
+ 'id': 'bingx',
15
+ 'name': 'BingX',
16
+ 'countries': ['US'],
17
+ // cheapest is 60 requests a minute = 1 requests per second on average => ( 1000ms / 1) = 1000 ms between requests on average
18
+ 'rateLimit': 1000,
19
+ 'version': 'v1',
20
+ 'certified': true,
21
+ 'pro': true,
22
+ 'has': {
23
+ 'CORS': undefined,
24
+ 'spot': true,
25
+ 'margin': true,
26
+ 'swap': true,
27
+ 'future': false,
28
+ 'option': false,
29
+ 'cancelAllOrders': true,
30
+ 'cancelOrder': true,
31
+ 'cancelOrders': true,
32
+ 'closeAllPositions': true,
33
+ 'closePosition': true,
34
+ 'createMarketBuyOrderWithCost': true,
35
+ 'createMarketOrderWithCost': true,
36
+ 'createMarketSellOrderWithCost': true,
37
+ 'createOrder': true,
38
+ 'createOrders': true,
39
+ 'createTrailingAmountOrder': true,
40
+ 'createTrailingPercentOrder': true,
41
+ 'fetchBalance': true,
42
+ 'fetchClosedOrders': true,
43
+ 'fetchCurrencies': true,
44
+ 'fetchDepositAddress': true,
45
+ 'fetchDeposits': true,
46
+ 'fetchDepositWithdrawFee': 'emulated',
47
+ 'fetchDepositWithdrawFees': true,
48
+ 'fetchFundingRate': true,
49
+ 'fetchFundingRateHistory': true,
50
+ 'fetchLeverage': true,
51
+ 'fetchLiquidations': false,
52
+ 'fetchMarkets': true,
53
+ 'fetchMyLiquidations': true,
54
+ 'fetchOHLCV': true,
55
+ 'fetchOpenInterest': true,
56
+ 'fetchOpenOrders': true,
57
+ 'fetchOrder': true,
58
+ 'fetchOrderBook': true,
59
+ 'fetchPositions': true,
60
+ 'fetchTicker': true,
61
+ 'fetchTickers': true,
62
+ 'fetchTime': true,
63
+ 'fetchTrades': true,
64
+ 'fetchTransfers': true,
65
+ 'fetchWithdrawals': true,
66
+ 'setLeverage': true,
67
+ 'setMargin': true,
68
+ 'setMarginMode': true,
69
+ 'transfer': true,
70
+ },
71
+ 'hostname': 'bingx.com',
72
+ 'urls': {
73
+ 'logo': 'https://github-production-user-asset-6210df.s3.amazonaws.com/1294454/253675376-6983b72e-4999-4549-b177-33b374c195e3.jpg',
74
+ 'api': {
75
+ 'spot': 'https://open-api.{hostname}/openApi',
76
+ 'swap': 'https://open-api.{hostname}/openApi',
77
+ 'contract': 'https://open-api.{hostname}/openApi',
78
+ 'wallets': 'https://open-api.{hostname}/openApi',
79
+ 'user': 'https://open-api.{hostname}/openApi',
80
+ 'subAccount': 'https://open-api.{hostname}/openApi',
81
+ 'account': 'https://open-api.{hostname}/openApi',
82
+ 'copyTrading': 'https://open-api.{hostname}/openApi',
83
+ },
84
+ 'www': 'https://bingx.com/',
85
+ 'doc': 'https://bingx-api.github.io/docs/',
86
+ 'referral': 'https://bingx.com/invite/OHETOM',
87
+ },
88
+ 'fees': {
89
+ 'tierBased': true,
90
+ 'spot': {
91
+ 'feeSide': 'get',
92
+ 'maker': this.parseNumber('0.001'),
93
+ 'taker': this.parseNumber('0.001'),
94
+ },
95
+ 'swap': {
96
+ 'feeSide': 'quote',
97
+ 'maker': this.parseNumber('0.0002'),
98
+ 'taker': this.parseNumber('0.0005'),
99
+ },
100
+ },
101
+ 'requiredCredentials': {
102
+ 'apiKey': true,
103
+ 'secret': true,
104
+ },
105
+ 'api': {
106
+ 'spot': {
107
+ 'v1': {
108
+ 'public': {
109
+ 'get': {
110
+ 'common/symbols': 3,
111
+ 'market/trades': 3,
112
+ 'market/depth': 3,
113
+ 'market/kline': 3,
114
+ 'ticker/24hr': 1,
115
+ },
116
+ },
117
+ 'private': {
118
+ 'get': {
119
+ 'trade/query': 3,
120
+ 'trade/openOrders': 3,
121
+ 'trade/historyOrders': 3,
122
+ 'trade/myTrades': 3,
123
+ 'user/commissionRate': 3,
124
+ 'account/balance': 3,
125
+ },
126
+ 'post': {
127
+ 'trade/order': 3,
128
+ 'trade/cancel': 3,
129
+ 'trade/batchOrders': 3,
130
+ 'trade/cancelOrders': 3,
131
+ 'trade/cancelOpenOrders': 3,
132
+ },
133
+ },
134
+ },
135
+ 'v3': {
136
+ 'private': {
137
+ 'get': {
138
+ 'get/asset/transfer': 3,
139
+ 'asset/transfer': 3,
140
+ 'capital/deposit/hisrec': 3,
141
+ 'capital/withdraw/history': 3,
142
+ },
143
+ 'post': {
144
+ 'post/asset/transfer': 3,
145
+ },
146
+ },
147
+ },
148
+ },
149
+ 'swap': {
150
+ 'v1': {
151
+ 'private': {
152
+ 'get': {
153
+ 'positionSide/dual': 1,
154
+ },
155
+ 'post': {
156
+ 'positionSide/dual': 1,
157
+ },
158
+ },
159
+ },
160
+ 'v2': {
161
+ 'public': {
162
+ 'get': {
163
+ 'server/time': 3,
164
+ 'quote/contracts': 1,
165
+ 'quote/price': 1,
166
+ 'quote/depth': 1,
167
+ 'quote/trades': 1,
168
+ 'quote/premiumIndex': 1,
169
+ 'quote/fundingRate': 1,
170
+ 'quote/klines': 1,
171
+ 'quote/openInterest': 1,
172
+ 'quote/ticker': 1,
173
+ 'quote/bookTicker': 1,
174
+ },
175
+ },
176
+ 'private': {
177
+ 'get': {
178
+ 'user/balance': 3,
179
+ 'user/positions': 3,
180
+ 'user/income': 3,
181
+ 'trade/openOrders': 3,
182
+ 'trade/openOrder': 3,
183
+ 'trade/order': 3,
184
+ 'trade/marginType': 3,
185
+ 'trade/leverage': 3,
186
+ 'trade/forceOrders': 3,
187
+ 'trade/allOrders': 3,
188
+ 'trade/allFillOrders': 3,
189
+ 'user/income/export': 3,
190
+ 'user/commissionRate': 3,
191
+ 'quote/bookTicker': 3,
192
+ },
193
+ 'post': {
194
+ 'trade/order': 3,
195
+ 'trade/batchOrders': 3,
196
+ 'trade/closeAllPositions': 3,
197
+ 'trade/marginType': 3,
198
+ 'trade/leverage': 3,
199
+ 'trade/positionMargin': 3,
200
+ 'trade/order/test': 3,
201
+ },
202
+ 'delete': {
203
+ 'trade/order': 3,
204
+ 'trade/batchOrders': 3,
205
+ 'trade/allOpenOrders': 3,
206
+ },
207
+ },
208
+ },
209
+ 'v3': {
210
+ 'public': {
211
+ 'get': {
212
+ 'quote/klines': 1,
213
+ },
214
+ },
215
+ },
216
+ },
217
+ 'contract': {
218
+ 'v1': {
219
+ 'private': {
220
+ 'get': {
221
+ 'allPosition': 3,
222
+ 'allOrders': 3,
223
+ 'balance': 3,
224
+ },
225
+ },
226
+ },
227
+ },
228
+ 'wallets': {
229
+ 'v1': {
230
+ 'private': {
231
+ 'get': {
232
+ 'capital/config/getall': 3,
233
+ 'capital/deposit/address': 1,
234
+ 'capital/innerTransfer/records': 1,
235
+ 'capital/subAccount/deposit/address': 1,
236
+ 'capital/deposit/subHisrec': 1,
237
+ 'capital/subAccount/innerTransfer/records': 1,
238
+ },
239
+ 'post': {
240
+ 'capital/withdraw/apply': 3,
241
+ 'capital/innerTransfer/apply': 3,
242
+ 'capital/subAccountInnerTransfer/apply': 3,
243
+ 'capital/deposit/createSubAddress': 1,
244
+ },
245
+ },
246
+ },
247
+ },
248
+ 'subAccount': {
249
+ 'v1': {
250
+ 'private': {
251
+ 'get': {
252
+ 'list': 3,
253
+ 'assets': 3,
254
+ 'apiKey/query': 1,
255
+ },
256
+ 'post': {
257
+ 'create': 3,
258
+ 'apiKey/create': 3,
259
+ 'apiKey/edit': 3,
260
+ 'apiKey/del': 3,
261
+ 'updateStatus': 3,
262
+ },
263
+ },
264
+ },
265
+ },
266
+ 'account': {
267
+ 'v1': {
268
+ 'private': {
269
+ 'get': {
270
+ 'uid': 1,
271
+ },
272
+ 'post': {
273
+ 'innerTransfer/authorizeSubAccount': 3,
274
+ },
275
+ },
276
+ },
277
+ },
278
+ 'user': {
279
+ 'auth': {
280
+ 'private': {
281
+ 'post': {
282
+ 'userDataStream': 1,
283
+ },
284
+ 'put': {
285
+ 'userDataStream': 1,
286
+ },
287
+ },
288
+ },
289
+ },
290
+ 'copyTrading': {
291
+ 'v1': {
292
+ 'private': {
293
+ 'get': {
294
+ 'swap/trace/currentTrack': 1,
295
+ },
296
+ 'post': {
297
+ 'swap/trace/closeTrackOrder': 1,
298
+ 'swap/trace/setTPSL': 1,
299
+ },
300
+ },
301
+ },
302
+ },
303
+ 'api': {
304
+ 'v3': {
305
+ 'private': {
306
+ 'get': {
307
+ 'asset/transfer': 1,
308
+ 'capital/deposit/hisrec': 1,
309
+ 'capital/withdraw/history': 1,
310
+ },
311
+ 'post': {
312
+ 'post/asset/transfer': 1,
313
+ },
314
+ },
315
+ },
316
+ },
317
+ },
318
+ 'timeframes': {
319
+ '1m': '1m',
320
+ '3m': '3m',
321
+ '5m': '5m',
322
+ '15m': '15m',
323
+ '30m': '30m',
324
+ '1h': '1h',
325
+ '2h': '2h',
326
+ '4h': '4h',
327
+ '6h': '6h',
328
+ '12h': '12h',
329
+ '1d': '1d',
330
+ '3d': '3d',
331
+ '1w': '1w',
332
+ '1M': '1M',
333
+ },
334
+ 'precisionMode': number.DECIMAL_PLACES,
335
+ 'exceptions': {
336
+ 'exact': {
337
+ '400': errors.BadRequest,
338
+ '401': errors.AuthenticationError,
339
+ '403': errors.PermissionDenied,
340
+ '404': errors.BadRequest,
341
+ '429': errors.DDoSProtection,
342
+ '418': errors.PermissionDenied,
343
+ '500': errors.ExchangeError,
344
+ '504': errors.ExchangeError,
345
+ '100001': errors.AuthenticationError,
346
+ '100412': errors.AuthenticationError,
347
+ '100202': errors.InsufficientFunds,
348
+ '100204': errors.BadRequest,
349
+ '100400': errors.BadRequest,
350
+ '100421': errors.BadSymbol,
351
+ '100440': errors.ExchangeError,
352
+ '100500': errors.ExchangeError,
353
+ '100503': errors.ExchangeError,
354
+ '80001': errors.BadRequest,
355
+ '80012': errors.ExchangeNotAvailable,
356
+ '80014': errors.BadRequest,
357
+ '80016': errors.OrderNotFound,
358
+ '80017': errors.OrderNotFound,
359
+ '100414': errors.AccountSuspended,
360
+ '100437': errors.BadRequest, // {"code":100437,"msg":"The withdrawal amount is lower than the minimum limit, please re-enter.","timestamp":1689258588845}
361
+ },
362
+ 'broad': {},
363
+ },
364
+ 'commonCurrencies': {},
365
+ 'options': {
366
+ 'defaultType': 'spot',
367
+ 'accountsByType': {
368
+ 'spot': 'FUND',
369
+ 'swap': 'PFUTURES',
370
+ 'future': 'SFUTURES',
371
+ },
372
+ 'accountsById': {
373
+ 'FUND': 'spot',
374
+ 'PFUTURES': 'swap',
375
+ 'SFUTURES': 'future',
376
+ },
377
+ 'recvWindow': 5 * 1000,
378
+ 'broker': 'CCXT',
379
+ },
380
+ });
381
+ }
382
+ async fetchTime(params = {}) {
383
+ /**
384
+ * @method
385
+ * @name bingx#fetchTime
386
+ * @description fetches the current integer timestamp in milliseconds from the bingx server
387
+ * @see https://bingx-api.github.io/docs/#/swapV2/base-info.html#Get%20Server%20Time
388
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
389
+ * @returns {int} the current integer timestamp in milliseconds from the bingx server
390
+ */
391
+ const response = await this.swapV2PublicGetServerTime(params);
392
+ //
393
+ // {
394
+ // "code": 0,
395
+ // "msg": "",
396
+ // "data": {
397
+ // "serverTime": 1675319535362
398
+ // }
399
+ // }
400
+ //
401
+ const data = this.safeValue(response, 'data');
402
+ return this.safeInteger(data, 'serverTime');
403
+ }
404
+ async fetchCurrencies(params = {}) {
405
+ /**
406
+ * @method
407
+ * @name bingx#fetchCurrencies
408
+ * @description fetches all available currencies on an exchange
409
+ * @see https://bingx-api.github.io/docs/#/common/account-api.html#All%20Coins
410
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
411
+ * @returns {object} an associative dictionary of currencies
412
+ */
413
+ if (!this.checkRequiredCredentials(false)) {
414
+ return undefined;
415
+ }
416
+ const response = await this.walletsV1PrivateGetCapitalConfigGetall(params);
417
+ //
418
+ // {
419
+ // "code": 0,
420
+ // "timestamp": 1688045966616,
421
+ // "data": [
422
+ // {
423
+ // "coin": "BTC",
424
+ // "name": "BTC",
425
+ // "networkList": [
426
+ // {
427
+ // "name": "BTC",
428
+ // "network": "BTC",
429
+ // "isDefault": true,
430
+ // "minConfirm": "2",
431
+ // "withdrawEnable": true,
432
+ // "withdrawFee": "0.00035",
433
+ // "withdrawMax": "1.62842",
434
+ // "withdrawMin": "0.0005"
435
+ // },
436
+ // {
437
+ // "name": "BTC",
438
+ // "network": "BEP20",
439
+ // "isDefault": false,
440
+ // "minConfirm": "15",
441
+ // "withdrawEnable": true,
442
+ // "withdrawFee": "0.00001",
443
+ // "withdrawMax": "1.62734",
444
+ // "withdrawMin": "0.0001"
445
+ // }
446
+ // ]
447
+ // },
448
+ // ...
449
+ // ],
450
+ // }
451
+ //
452
+ const data = this.safeValue(response, 'data', []);
453
+ const result = {};
454
+ for (let i = 0; i < data.length; i++) {
455
+ const entry = data[i];
456
+ const currencyId = this.safeString(entry, 'coin');
457
+ const code = this.safeCurrencyCode(currencyId);
458
+ const name = this.safeString(entry, 'name');
459
+ const networkList = this.safeValue(entry, 'networkList');
460
+ const networks = {};
461
+ let fee = undefined;
462
+ let active = undefined;
463
+ let withdrawEnabled = undefined;
464
+ let defaultLimits = {};
465
+ for (let j = 0; j < networkList.length; j++) {
466
+ const rawNetwork = networkList[j];
467
+ const network = this.safeString(rawNetwork, 'network');
468
+ const networkCode = this.networkIdToCode(network);
469
+ const isDefault = this.safeValue(rawNetwork, 'isDefault');
470
+ withdrawEnabled = this.safeValue(rawNetwork, 'withdrawEnable');
471
+ const limits = {
472
+ 'amounts': { 'min': this.safeNumber(rawNetwork, 'withdrawMin'), 'max': this.safeNumber(rawNetwork, 'withdrawMax') },
473
+ };
474
+ if (isDefault) {
475
+ fee = this.safeNumber(rawNetwork, 'withdrawFee');
476
+ active = withdrawEnabled;
477
+ defaultLimits = limits;
478
+ }
479
+ networks[networkCode] = {
480
+ 'info': rawNetwork,
481
+ 'id': network,
482
+ 'network': networkCode,
483
+ 'fee': fee,
484
+ 'active': active,
485
+ 'deposit': undefined,
486
+ 'withdraw': withdrawEnabled,
487
+ 'precision': undefined,
488
+ 'limits': limits,
489
+ };
490
+ }
491
+ result[code] = {
492
+ 'info': entry,
493
+ 'code': code,
494
+ 'id': currencyId,
495
+ 'precision': undefined,
496
+ 'name': name,
497
+ 'active': active,
498
+ 'deposit': undefined,
499
+ 'withdraw': withdrawEnabled,
500
+ 'networks': networks,
501
+ 'fee': fee,
502
+ 'limits': defaultLimits,
503
+ };
504
+ }
505
+ return result;
506
+ }
507
+ async fetchSpotMarkets(params) {
508
+ const response = await this.spotV1PublicGetCommonSymbols(params);
509
+ //
510
+ // {
511
+ // "code": 0,
512
+ // "msg": "",
513
+ // "debugMsg": "",
514
+ // "data": {
515
+ // "symbols": [
516
+ // {
517
+ // "symbol": "GEAR-USDT",
518
+ // "minQty": 735,
519
+ // "maxQty": 2941177,
520
+ // "minNotional": 5,
521
+ // "maxNotional": 20000,
522
+ // "status": 1,
523
+ // "tickSize": 0.000001,
524
+ // "stepSize": 1
525
+ // },
526
+ // ...
527
+ // ]
528
+ // }
529
+ // }
530
+ //
531
+ const data = this.safeValue(response, 'data');
532
+ const markets = this.safeValue(data, 'symbols', []);
533
+ return this.parseMarkets(markets);
534
+ }
535
+ async fetchSwapMarkets(params) {
536
+ const response = await this.swapV2PublicGetQuoteContracts(params);
537
+ //
538
+ // {
539
+ // "code": 0,
540
+ // "msg": "",
541
+ // "data": [
542
+ // {
543
+ // "contractId": "100",
544
+ // "symbol": "BTC-USDT",
545
+ // "size": "0.0001",
546
+ // "quantityPrecision": 4,
547
+ // "pricePrecision": 1,
548
+ // "feeRate": 0.0005,
549
+ // "tradeMinLimit": 1,
550
+ // "maxLongLeverage": 150,
551
+ // "maxShortLeverage": 150,
552
+ // "currency": "USDT",
553
+ // "asset": "BTC",
554
+ // "status": 1
555
+ // },
556
+ // ...
557
+ // ]
558
+ // }
559
+ //
560
+ const markets = this.safeValue(response, 'data', []);
561
+ return this.parseMarkets(markets);
562
+ }
563
+ parseMarket(market) {
564
+ const id = this.safeString(market, 'symbol');
565
+ const symbolParts = id.split('-');
566
+ const baseId = symbolParts[0];
567
+ const quoteId = symbolParts[1];
568
+ const base = this.safeCurrencyCode(baseId);
569
+ const quote = this.safeCurrencyCode(quoteId);
570
+ const currency = this.safeString(market, 'currency');
571
+ const settle = this.safeCurrencyCode(currency);
572
+ let pricePrecision = this.safeInteger(market, 'pricePrecision');
573
+ if (pricePrecision === undefined) {
574
+ pricePrecision = this.precisionFromString(this.safeString(market, 'tickSize'));
575
+ }
576
+ let quantityPrecision = this.safeInteger(market, 'quantityPrecision');
577
+ if (quantityPrecision === undefined) {
578
+ quantityPrecision = this.precisionFromString(this.safeString(market, 'stepSize'));
579
+ }
580
+ const type = (settle !== undefined) ? 'swap' : 'spot';
581
+ const spot = type === 'spot';
582
+ const swap = type === 'swap';
583
+ let symbol = base + '/' + quote;
584
+ if (settle !== undefined) {
585
+ symbol += ':' + settle;
586
+ }
587
+ const fees = this.safeValue(this.fees, type, {});
588
+ const contractSize = this.safeNumber(market, 'size');
589
+ const isActive = this.safeString(market, 'status') === '1';
590
+ const isInverse = (spot) ? undefined : false;
591
+ const isLinear = (spot) ? undefined : swap;
592
+ return this.safeMarketStructure({
593
+ 'id': id,
594
+ 'symbol': symbol,
595
+ 'base': base,
596
+ 'quote': quote,
597
+ 'settle': settle,
598
+ 'baseId': baseId,
599
+ 'quoteId': quoteId,
600
+ 'settleId': currency,
601
+ 'type': type,
602
+ 'spot': spot,
603
+ 'margin': false,
604
+ 'swap': swap,
605
+ 'future': false,
606
+ 'option': false,
607
+ 'active': isActive,
608
+ 'contract': swap,
609
+ 'linear': isLinear,
610
+ 'inverse': isInverse,
611
+ 'taker': this.safeNumber(fees, 'taker'),
612
+ 'maker': this.safeNumber(fees, 'maker'),
613
+ 'feeSide': this.safeString(fees, 'feeSide'),
614
+ 'contractSize': contractSize,
615
+ 'expiry': undefined,
616
+ 'expiryDatetime': undefined,
617
+ 'strike': undefined,
618
+ 'optionType': undefined,
619
+ 'precision': {
620
+ 'amount': quantityPrecision,
621
+ 'price': pricePrecision,
622
+ },
623
+ 'limits': {
624
+ 'leverage': {
625
+ 'min': undefined,
626
+ 'max': this.safeInteger(market, 'maxLongLeverage'),
627
+ },
628
+ 'amount': {
629
+ 'min': this.safeNumber(market, 'minQty'),
630
+ 'max': this.safeNumber(market, 'maxQty'),
631
+ },
632
+ 'price': {
633
+ 'min': undefined,
634
+ 'max': undefined,
635
+ },
636
+ 'cost': {
637
+ 'min': this.safeNumber(market, 'minNotional'),
638
+ 'max': this.safeNumber(market, 'maxNotional'),
639
+ },
640
+ },
641
+ 'created': undefined,
642
+ 'info': market,
643
+ });
644
+ }
645
+ async fetchMarkets(params = {}) {
646
+ /**
647
+ * @method
648
+ * @name bingx#fetchMarkets
649
+ * @description retrieves data on all markets for bingx
650
+ * @see https://bingx-api.github.io/docs/#/spot/market-api.html#Query%20Symbols
651
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Contract%20Information
652
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
653
+ * @returns {object[]} an array of objects representing market data
654
+ */
655
+ const requests = [this.fetchSpotMarkets(params), this.fetchSwapMarkets(params)];
656
+ const promises = await Promise.all(requests);
657
+ const spotMarkets = this.safeValue(promises, 0, []);
658
+ const swapMarkets = this.safeValue(promises, 1, []);
659
+ return this.arrayConcat(spotMarkets, swapMarkets);
660
+ }
661
+ async fetchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) {
662
+ /**
663
+ * @method
664
+ * @name bingx#fetchOHLCV
665
+ * @description fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
666
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#K-Line%20Data
667
+ * @see https://bingx-api.github.io/docs/#/spot/market-api.html#Candlestick%20chart%20data
668
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#%20K-Line%20Data
669
+ * @param {string} symbol unified symbol of the market to fetch OHLCV data for
670
+ * @param {string} timeframe the length of time each candle represents
671
+ * @param {int} [since] timestamp in ms of the earliest candle to fetch
672
+ * @param {int} [limit] the maximum amount of candles to fetch
673
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
674
+ * @param {int} [params.until] timestamp in ms of the latest candle to fetch
675
+ * @param {boolean} [params.paginate] default false, when true will automatically paginate by calling this endpoint multiple times. See in the docs all the [availble parameters](https://github.com/ccxt/ccxt/wiki/Manual#pagination-params)
676
+ * @returns {int[][]} A list of candles ordered as timestamp, open, high, low, close, volume
677
+ */
678
+ await this.loadMarkets();
679
+ let paginate = false;
680
+ [paginate, params] = this.handleOptionAndParams(params, 'fetchOHLCV', 'paginate', false);
681
+ if (paginate) {
682
+ return await this.fetchPaginatedCallDeterministic('fetchOHLCV', symbol, since, limit, timeframe, params, 1440);
683
+ }
684
+ const market = this.market(symbol);
685
+ const request = {
686
+ 'symbol': market['id'],
687
+ };
688
+ request['interval'] = this.safeString(this.timeframes, timeframe, timeframe);
689
+ if (since !== undefined) {
690
+ request['startTime'] = since;
691
+ }
692
+ if (limit !== undefined) {
693
+ request['limit'] = limit;
694
+ }
695
+ const until = this.safeInteger2(params, 'until', 'endTime');
696
+ if (until !== undefined) {
697
+ params = this.omit(params, ['until']);
698
+ request['endTime'] = until;
699
+ }
700
+ let response = undefined;
701
+ if (market['spot']) {
702
+ response = await this.spotV1PublicGetMarketKline(this.extend(request, params));
703
+ }
704
+ else {
705
+ response = await this.swapV3PublicGetQuoteKlines(this.extend(request, params));
706
+ }
707
+ //
708
+ // {
709
+ // "code": 0,
710
+ // "msg": "",
711
+ // "data": [
712
+ // {
713
+ // "open": "19396.8",
714
+ // "close": "19394.4",
715
+ // "high": "19397.5",
716
+ // "low": "19385.7",
717
+ // "volume": "110.05",
718
+ // "time": 1666583700000
719
+ // },
720
+ // ...
721
+ // ]
722
+ // }
723
+ //
724
+ let ohlcvs = this.safeValue(response, 'data', []);
725
+ if (!Array.isArray(ohlcvs)) {
726
+ ohlcvs = [ohlcvs];
727
+ }
728
+ return this.parseOHLCVs(ohlcvs, market, timeframe, since, limit);
729
+ }
730
+ parseOHLCV(ohlcv, market = undefined) {
731
+ //
732
+ // {
733
+ // "open": "19394.4",
734
+ // "close": "19379.0",
735
+ // "high": "19394.4",
736
+ // "low": "19368.3",
737
+ // "volume": "167.44",
738
+ // "time": 1666584000000
739
+ // }
740
+ // spot
741
+ // [
742
+ // 1691402580000,
743
+ // 29093.61,
744
+ // 29093.93,
745
+ // 29087.73,
746
+ // 29093.24,
747
+ // 0.59,
748
+ // 1691402639999,
749
+ // 17221.07
750
+ // ]
751
+ //
752
+ if (Array.isArray(ohlcv)) {
753
+ return [
754
+ this.safeInteger(ohlcv, 0),
755
+ this.safeNumber(ohlcv, 1),
756
+ this.safeNumber(ohlcv, 2),
757
+ this.safeNumber(ohlcv, 3),
758
+ this.safeNumber(ohlcv, 4),
759
+ this.safeNumber(ohlcv, 5),
760
+ ];
761
+ }
762
+ return [
763
+ this.safeInteger(ohlcv, 'time'),
764
+ this.safeNumber(ohlcv, 'open'),
765
+ this.safeNumber(ohlcv, 'high'),
766
+ this.safeNumber(ohlcv, 'low'),
767
+ this.safeNumber(ohlcv, 'close'),
768
+ this.safeNumber(ohlcv, 'volume'),
769
+ ];
770
+ }
771
+ async fetchTrades(symbol, since = undefined, limit = undefined, params = {}) {
772
+ /**
773
+ * @method
774
+ * @name bingx#fetchTrades
775
+ * @description get the list of most recent trades for a particular symbol
776
+ * @see https://bingx-api.github.io/docs/#/spot/market-api.html#Query%20transaction%20records
777
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#The%20latest%20Trade%20of%20a%20Trading%20Pair
778
+ * @param {string} symbol unified symbol of the market to fetch trades for
779
+ * @param {int} [since] timestamp in ms of the earliest trade to fetch
780
+ * @param {int} [limit] the maximum amount of trades to fetch
781
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
782
+ * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
783
+ */
784
+ await this.loadMarkets();
785
+ const market = this.market(symbol);
786
+ const request = {
787
+ 'symbol': market['id'],
788
+ };
789
+ if (limit !== undefined) {
790
+ request['limit'] = Math.min(limit, 100); // avoid API exception "limit should less than 100"
791
+ }
792
+ let response = undefined;
793
+ let marketType = undefined;
794
+ [marketType, params] = this.handleMarketTypeAndParams('fetchTrades', market, params);
795
+ if (marketType === 'spot') {
796
+ response = await this.spotV1PublicGetMarketTrades(this.extend(request, params));
797
+ }
798
+ else {
799
+ response = await this.swapV2PublicGetQuoteTrades(this.extend(request, params));
800
+ }
801
+ //
802
+ // spot
803
+ //
804
+ // {
805
+ // "code": 0,
806
+ // "data": [
807
+ // {
808
+ // "id": 43148253,
809
+ // "price": 25714.71,
810
+ // "qty": 1.674571,
811
+ // "time": 1655085975589,
812
+ // "buyerMaker": false
813
+ // }
814
+ // ]
815
+ // }
816
+ //
817
+ // swap
818
+ //
819
+ // {
820
+ // "code":0,
821
+ // "msg":"",
822
+ // "data":[
823
+ // {
824
+ // "time": 1672025549368,
825
+ // "isBuyerMaker": true,
826
+ // "price": "16885.0",
827
+ // "qty": "3.3002",
828
+ // "quoteQty": "55723.87"
829
+ // },
830
+ // ...
831
+ // ]
832
+ // }
833
+ //
834
+ const trades = this.safeValue(response, 'data', []);
835
+ return this.parseTrades(trades, market, since, limit);
836
+ }
837
+ parseTrade(trade, market = undefined) {
838
+ //
839
+ // spot
840
+ // fetchTrades
841
+ //
842
+ // {
843
+ // "id": 43148253,
844
+ // "price": 25714.71,
845
+ // "qty": 1.674571,
846
+ // "time": 1655085975589,
847
+ // "buyerMaker": false
848
+ // }
849
+ //
850
+ // spot
851
+ // fetchMyTrades
852
+ // {
853
+ // "symbol": "LTC-USDT",
854
+ // "id": 36237072,
855
+ // "orderId": 1674069326895775744,
856
+ // "price": "85.891",
857
+ // "qty": "0.0582",
858
+ // "quoteQty": "4.9988562000000005",
859
+ // "commission": -0.00005820000000000001,
860
+ // "commissionAsset": "LTC",
861
+ // "time": 1687964205000,
862
+ // "isBuyer": true,
863
+ // "isMaker": false
864
+ // }
865
+ //
866
+ // swap
867
+ // fetchTrades
868
+ //
869
+ // {
870
+ // "time": 1672025549368,
871
+ // "isBuyerMaker": true,
872
+ // "price": "16885.0",
873
+ // "qty": "3.3002",
874
+ // "quoteQty": "55723.87"
875
+ // }
876
+ //
877
+ // swap
878
+ // fetchMyTrades
879
+ //
880
+ // {
881
+ // "volume": "0.1",
882
+ // "price": "106.75",
883
+ // "amount": "10.6750",
884
+ // "commission": "-0.0053",
885
+ // "currency": "USDT",
886
+ // "orderId": "1676213270274379776",
887
+ // "liquidatedPrice": "0.00",
888
+ // "liquidatedMarginRatio": "0.00",
889
+ // "filledTime": "2023-07-04T20:56:01.000+0800"
890
+ // }
891
+ //
892
+ //
893
+ // ws
894
+ //
895
+ // spot
896
+ //
897
+ // {
898
+ // "E": 1690214529432,
899
+ // "T": 1690214529386,
900
+ // "e": "trade",
901
+ // "m": true,
902
+ // "p": "29110.19",
903
+ // "q": "0.1868",
904
+ // "s": "BTC-USDT",
905
+ // "t": "57903921"
906
+ // }
907
+ //
908
+ // swap
909
+ //
910
+ // {
911
+ // "q": "0.0421",
912
+ // "p": "29023.5",
913
+ // "T": 1690221401344,
914
+ // "m": false,
915
+ // "s": "BTC-USDT"
916
+ // }
917
+ //
918
+ let time = this.safeIntegerN(trade, ['time', 'filledTm', 'T']);
919
+ const datetimeId = this.safeString(trade, 'filledTm');
920
+ if (datetimeId !== undefined) {
921
+ time = this.parse8601(datetimeId);
922
+ }
923
+ if (time === 0) {
924
+ time = undefined;
925
+ }
926
+ const cost = this.safeString(trade, 'quoteQty');
927
+ const type = (cost === undefined) ? 'spot' : 'swap';
928
+ const currencyId = this.safeStringN(trade, ['currency', 'N', 'commissionAsset']);
929
+ const currencyCode = this.safeCurrencyCode(currencyId);
930
+ const m = this.safeValue(trade, 'm');
931
+ const marketId = this.safeString(trade, 's');
932
+ const isBuyerMaker = this.safeValue2(trade, 'buyerMaker', 'isBuyerMaker');
933
+ let takeOrMaker = undefined;
934
+ if ((isBuyerMaker !== undefined) || (m !== undefined)) {
935
+ takeOrMaker = (isBuyerMaker || m) ? 'maker' : 'taker';
936
+ }
937
+ let side = this.safeStringLower2(trade, 'side', 'S');
938
+ if (side === undefined) {
939
+ if ((isBuyerMaker !== undefined) || (m !== undefined)) {
940
+ side = (isBuyerMaker || m) ? 'sell' : 'buy';
941
+ takeOrMaker = 'taker';
942
+ }
943
+ }
944
+ const isBuyer = this.safeValue(trade, 'isBuyer');
945
+ if (isBuyer !== undefined) {
946
+ side = isBuyer ? 'buy' : 'sell';
947
+ }
948
+ const isMaker = this.safeValue(trade, 'isMaker');
949
+ if (isMaker !== undefined) {
950
+ takeOrMaker = isMaker ? 'maker' : 'taker';
951
+ }
952
+ return this.safeTrade({
953
+ 'id': this.safeStringN(trade, ['id', 't']),
954
+ 'info': trade,
955
+ 'timestamp': time,
956
+ 'datetime': this.iso8601(time),
957
+ 'symbol': this.safeSymbol(marketId, market, '-', type),
958
+ 'order': this.safeString2(trade, 'orderId', 'i'),
959
+ 'type': this.safeStringLower(trade, 'o'),
960
+ 'side': this.parseOrderSide(side),
961
+ 'takerOrMaker': takeOrMaker,
962
+ 'price': this.safeString2(trade, 'price', 'p'),
963
+ 'amount': this.safeStringN(trade, ['qty', 'volume', 'amount', 'q']),
964
+ 'cost': cost,
965
+ 'fee': {
966
+ 'cost': this.parseNumber(Precise["default"].stringAbs(this.safeString2(trade, 'commission', 'n'))),
967
+ 'currency': currencyCode,
968
+ 'rate': undefined,
969
+ },
970
+ }, market);
971
+ }
972
+ async fetchOrderBook(symbol, limit = undefined, params = {}) {
973
+ /**
974
+ * @method
975
+ * @name bingx#fetchOrderBook
976
+ * @description fetches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
977
+ * @see https://bingx-api.github.io/docs/#/spot/market-api.html#Query%20depth%20information
978
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Get%20Market%20Depth
979
+ * @param {string} symbol unified symbol of the market to fetch the order book for
980
+ * @param {int} [limit] the maximum amount of order book entries to return
981
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
982
+ * @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
983
+ */
984
+ await this.loadMarkets();
985
+ const market = this.market(symbol);
986
+ const request = {
987
+ 'symbol': market['id'],
988
+ };
989
+ if (limit !== undefined) {
990
+ request['limit'] = limit;
991
+ }
992
+ let response = undefined;
993
+ let marketType = undefined;
994
+ [marketType, params] = this.handleMarketTypeAndParams('fetchOrderBook', market, params);
995
+ if (marketType === 'spot') {
996
+ response = await this.spotV1PublicGetMarketDepth(this.extend(request, params));
997
+ }
998
+ else {
999
+ response = await this.swapV2PublicGetQuoteDepth(this.extend(request, params));
1000
+ }
1001
+ //
1002
+ // spot
1003
+ //
1004
+ // {
1005
+ // "code": 0,
1006
+ // "data": {
1007
+ // "bids": [
1008
+ // [
1009
+ // "26324.73",
1010
+ // "0.37655"
1011
+ // ],
1012
+ // [
1013
+ // "26324.71",
1014
+ // "0.31888"
1015
+ // ],
1016
+ // ],
1017
+ // "asks": [
1018
+ // [
1019
+ // "26340.30",
1020
+ // "6.45221"
1021
+ // ],
1022
+ // [
1023
+ // "26340.15",
1024
+ // "6.73261"
1025
+ // ],
1026
+ // ]}
1027
+ // }
1028
+ //
1029
+ // swap
1030
+ //
1031
+ // {
1032
+ // "code": 0,
1033
+ // "msg": "",
1034
+ // "data": {
1035
+ // "T": 1683914263304,
1036
+ // "bids": [
1037
+ // [
1038
+ // "26300.90000000",
1039
+ // "30408.00000000"
1040
+ // ],
1041
+ // [
1042
+ // "26300.80000000",
1043
+ // "50906.00000000"
1044
+ // ],
1045
+ // ],
1046
+ // "asks": [
1047
+ // [
1048
+ // "26301.00000000",
1049
+ // "43616.00000000"
1050
+ // ],
1051
+ // [
1052
+ // "26301.10000000",
1053
+ // "49402.00000000"
1054
+ // ],
1055
+ // ]}
1056
+ // }
1057
+ //
1058
+ const orderbook = this.safeValue(response, 'data', {});
1059
+ const timestamp = this.safeInteger2(orderbook, 'T', 'ts');
1060
+ return this.parseOrderBook(orderbook, market['symbol'], timestamp, 'bids', 'asks', 0, 1);
1061
+ }
1062
+ async fetchFundingRate(symbol, params = {}) {
1063
+ /**
1064
+ * @method
1065
+ * @name bingx#fetchFundingRate
1066
+ * @description fetch the current funding rate
1067
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Current%20Funding%20Rate
1068
+ * @param {string} symbol unified market symbol
1069
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1070
+ * @returns {object} a [funding rate structure]{@link https://docs.ccxt.com/#/?id=funding-rate-structure}
1071
+ */
1072
+ await this.loadMarkets();
1073
+ const market = this.market(symbol);
1074
+ const request = {
1075
+ 'symbol': market['id'],
1076
+ };
1077
+ const response = await this.swapV2PublicGetQuotePremiumIndex(this.extend(request, params));
1078
+ //
1079
+ // {
1080
+ // "code":0,
1081
+ // "msg":"",
1082
+ // "data":[
1083
+ // {
1084
+ // "symbol": "BTC-USDT",
1085
+ // "markPrice": "16884.5",
1086
+ // "indexPrice": "16886.9",
1087
+ // "lastFundingRate": "0.0001",
1088
+ // "nextFundingTime": 1672041600000
1089
+ // },
1090
+ // ...
1091
+ // ]
1092
+ // }
1093
+ //
1094
+ const data = this.safeValue(response, 'data', {});
1095
+ return this.parseFundingRate(data, market);
1096
+ }
1097
+ parseFundingRate(contract, market = undefined) {
1098
+ //
1099
+ // {
1100
+ // "symbol": "BTC-USDT",
1101
+ // "markPrice": "16884.5",
1102
+ // "indexPrice": "16886.9",
1103
+ // "lastFundingRate": "0.0001",
1104
+ // "nextFundingTime": 1672041600000
1105
+ // }
1106
+ //
1107
+ const marketId = this.safeString(contract, 'symbol');
1108
+ const nextFundingTimestamp = this.safeInteger(contract, 'nextFundingTime');
1109
+ return {
1110
+ 'info': contract,
1111
+ 'symbol': this.safeSymbol(marketId, market, '-', 'swap'),
1112
+ 'markPrice': this.safeNumber(contract, 'markPrice'),
1113
+ 'indexPrice': this.safeNumber(contract, 'indexPrice'),
1114
+ 'interestRate': undefined,
1115
+ 'estimatedSettlePrice': undefined,
1116
+ 'timestamp': undefined,
1117
+ 'datetime': undefined,
1118
+ 'fundingRate': this.safeNumber(contract, 'lastFundingRate'),
1119
+ 'fundingTimestamp': undefined,
1120
+ 'fundingDatetime': undefined,
1121
+ 'nextFundingRate': undefined,
1122
+ 'nextFundingTimestamp': nextFundingTimestamp,
1123
+ 'nextFundingDatetime': this.iso8601(nextFundingTimestamp),
1124
+ 'previousFundingRate': undefined,
1125
+ 'previousFundingTimestamp': undefined,
1126
+ 'previousFundingDatetime': undefined,
1127
+ };
1128
+ }
1129
+ async fetchFundingRateHistory(symbol = undefined, since = undefined, limit = undefined, params = {}) {
1130
+ /**
1131
+ * @method
1132
+ * @name bingx#fetchFundingRateHistory
1133
+ * @description fetches historical funding rate prices
1134
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Funding%20Rate%20History
1135
+ * @param {string} symbol unified symbol of the market to fetch the funding rate history for
1136
+ * @param {int} [since] timestamp in ms of the earliest funding rate to fetch
1137
+ * @param {int} [limit] the maximum amount of [funding rate structures]{@link https://docs.ccxt.com/#/?id=funding-rate-history-structure} to fetch
1138
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1139
+ * @param {int} [params.until] timestamp in ms of the latest funding rate to fetch
1140
+ * @param {boolean} [params.paginate] default false, when true will automatically paginate by calling this endpoint multiple times. See in the docs all the [availble parameters](https://github.com/ccxt/ccxt/wiki/Manual#pagination-params)
1141
+ * @returns {object[]} a list of [funding rate structures]{@link https://docs.ccxt.com/#/?id=funding-rate-history-structure}
1142
+ */
1143
+ if (symbol === undefined) {
1144
+ throw new errors.ArgumentsRequired(this.id + ' fetchFundingRateHistory() requires a symbol argument');
1145
+ }
1146
+ await this.loadMarkets();
1147
+ let paginate = false;
1148
+ [paginate, params] = this.handleOptionAndParams(params, 'fetchFundingRateHistory', 'paginate');
1149
+ if (paginate) {
1150
+ return await this.fetchPaginatedCallDeterministic('fetchFundingRateHistory', symbol, since, limit, '8h', params);
1151
+ }
1152
+ const market = this.market(symbol);
1153
+ const request = {
1154
+ 'symbol': market['id'],
1155
+ };
1156
+ if (since !== undefined) {
1157
+ request['startTime'] = since;
1158
+ }
1159
+ if (limit !== undefined) {
1160
+ request['limit'] = limit;
1161
+ }
1162
+ const until = this.safeInteger2(params, 'until', 'startTime');
1163
+ if (until !== undefined) {
1164
+ params = this.omit(params, ['until']);
1165
+ request['startTime'] = until;
1166
+ }
1167
+ const response = await this.swapV2PublicGetQuoteFundingRate(this.extend(request, params));
1168
+ //
1169
+ // {
1170
+ // "code":0,
1171
+ // "msg":"",
1172
+ // "data":[
1173
+ // {
1174
+ // "symbol": "BTC-USDT",
1175
+ // "fundingRate": "0.0001",
1176
+ // "fundingTime": 1585684800000
1177
+ // },
1178
+ // ...
1179
+ // ]
1180
+ // }
1181
+ //
1182
+ const data = this.safeValue(response, 'data', []);
1183
+ const rates = [];
1184
+ for (let i = 0; i < data.length; i++) {
1185
+ const entry = data[i];
1186
+ const marketId = this.safeString(entry, 'symbol');
1187
+ const symbolInner = this.safeSymbol(marketId, market, '-', 'swap');
1188
+ const timestamp = this.safeInteger(entry, 'fundingTime');
1189
+ rates.push({
1190
+ 'info': entry,
1191
+ 'symbol': symbolInner,
1192
+ 'fundingRate': this.safeNumber(entry, 'fundingRate'),
1193
+ 'timestamp': timestamp,
1194
+ 'datetime': this.iso8601(timestamp),
1195
+ });
1196
+ }
1197
+ const sorted = this.sortBy(rates, 'timestamp');
1198
+ return this.filterBySymbolSinceLimit(sorted, market['symbol'], since, limit);
1199
+ }
1200
+ async fetchOpenInterest(symbol, params = {}) {
1201
+ /**
1202
+ * @method
1203
+ * @name bingx#fetchOpenInterest
1204
+ * @description Retrieves the open interest of a currency
1205
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Get%20Swap%20Open%20Positions
1206
+ * @param {string} symbol Unified CCXT market symbol
1207
+ * @param {object} [params] exchange specific parameters
1208
+ * @returns {object} an open interest structure{@link https://docs.ccxt.com/#/?id=open-interest-structure}
1209
+ */
1210
+ await this.loadMarkets();
1211
+ const market = this.market(symbol);
1212
+ const request = {
1213
+ 'symbol': market['id'],
1214
+ };
1215
+ const response = await this.swapV2PublicGetQuoteOpenInterest(this.extend(request, params));
1216
+ //
1217
+ // {
1218
+ // "code": 0,
1219
+ // "msg": "",
1220
+ // "data": {
1221
+ // "openInterest": "3289641547.10",
1222
+ // "symbol": "BTC-USDT",
1223
+ // "time": 1672026617364
1224
+ // }
1225
+ // }
1226
+ //
1227
+ const data = this.safeValue(response, 'data', {});
1228
+ return this.parseOpenInterest(data, market);
1229
+ }
1230
+ parseOpenInterest(interest, market = undefined) {
1231
+ //
1232
+ // {
1233
+ // "openInterest": "3289641547.10",
1234
+ // "symbol": "BTC-USDT",
1235
+ // "time": 1672026617364
1236
+ // }
1237
+ //
1238
+ const timestamp = this.safeInteger(interest, 'time');
1239
+ const id = this.safeString(interest, 'symbol');
1240
+ const symbol = this.safeSymbol(id, market, '-', 'swap');
1241
+ const openInterest = this.safeNumber(interest, 'openInterest');
1242
+ return this.safeOpenInterest({
1243
+ 'symbol': symbol,
1244
+ 'baseVolume': undefined,
1245
+ 'quoteVolume': undefined,
1246
+ 'openInterestAmount': undefined,
1247
+ 'openInterestValue': openInterest,
1248
+ 'timestamp': timestamp,
1249
+ 'datetime': this.iso8601(timestamp),
1250
+ 'info': interest,
1251
+ }, market);
1252
+ }
1253
+ async fetchTicker(symbol, params = {}) {
1254
+ /**
1255
+ * @method
1256
+ * @name bingx#fetchTicker
1257
+ * @description fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
1258
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Get%20Ticker
1259
+ * @see https://bingx-api.github.io/docs/#/spot/market-api.html#24%E5%B0%8F%E6%97%B6%E4%BB%B7%E6%A0%BC%E5%8F%98%E5%8A%A8%E6%83%85%E5%86%B5
1260
+ * @param {string} symbol unified symbol of the market to fetch the ticker for
1261
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1262
+ * @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure}
1263
+ */
1264
+ await this.loadMarkets();
1265
+ const market = this.market(symbol);
1266
+ const request = {
1267
+ 'symbol': market['id'],
1268
+ };
1269
+ let response = undefined;
1270
+ if (market['spot']) {
1271
+ response = await this.spotV1PublicGetTicker24hr(this.extend(request, params));
1272
+ }
1273
+ else {
1274
+ response = await this.swapV2PublicGetQuoteTicker(this.extend(request, params));
1275
+ }
1276
+ const data = this.safeValue(response, 'data');
1277
+ const ticker = this.safeValue(data, 0, data);
1278
+ return this.parseTicker(ticker, market);
1279
+ }
1280
+ async fetchTickers(symbols = undefined, params = {}) {
1281
+ /**
1282
+ * @method
1283
+ * @name bingx#fetchTickers
1284
+ * @description fetches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market
1285
+ * @see https://bingx-api.github.io/docs/#/swapV2/market-api.html#Get%20Ticker
1286
+ * @param {string[]|undefined} symbols unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
1287
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1288
+ * @returns {object} a dictionary of [ticker structures]{@link https://docs.ccxt.com/#/?id=ticker-structure}
1289
+ */
1290
+ await this.loadMarkets();
1291
+ let market = undefined;
1292
+ if (symbols !== undefined) {
1293
+ symbols = this.marketSymbols(symbols);
1294
+ const firstSymbol = this.safeString(symbols, 0);
1295
+ market = this.market(firstSymbol);
1296
+ }
1297
+ let type = undefined;
1298
+ [type, params] = this.handleMarketTypeAndParams('fetchTickers', market, params);
1299
+ let response = undefined;
1300
+ if (type === 'spot') {
1301
+ response = await this.spotV1PublicGetTicker24hr(params);
1302
+ }
1303
+ else {
1304
+ response = await this.swapV2PublicGetQuoteTicker(params);
1305
+ }
1306
+ const tickers = this.safeValue(response, 'data');
1307
+ return this.parseTickers(tickers, symbols);
1308
+ }
1309
+ parseTicker(ticker, market = undefined) {
1310
+ //
1311
+ // spot
1312
+ // {
1313
+ // "symbol": "BTC-USDT",
1314
+ // "openPrice": "26032.08",
1315
+ // "highPrice": "26178.86",
1316
+ // "lowPrice": "25968.18",
1317
+ // "lastPrice": "26113.60",
1318
+ // "volume": "1161.79",
1319
+ // "quoteVolume": "30288466.44",
1320
+ // "openTime": "1693081020762",
1321
+ // "closeTime": "1693167420762",
1322
+ // added 2023-11-10:
1323
+ // "bidPrice": 16726.0,
1324
+ // "bidQty": 0.05,
1325
+ // "askPrice": 16726.0,
1326
+ // "askQty": 0.05,
1327
+ // }
1328
+ // swap
1329
+ //
1330
+ // {
1331
+ // "symbol": "BTC-USDT",
1332
+ // "priceChange": "52.5",
1333
+ // "priceChangePercent": "0.31%", // they started to add the percent sign in value
1334
+ // "lastPrice": "16880.5",
1335
+ // "lastQty": "2.2238", // only present in swap!
1336
+ // "highPrice": "16897.5",
1337
+ // "lowPrice": "16726.0",
1338
+ // "volume": "245870.1692",
1339
+ // "quoteVolume": "4151395117.73",
1340
+ // "openPrice": "16832.0",
1341
+ // "openTime": 1672026667803,
1342
+ // "closeTime": 1672026648425,
1343
+ // added 2023-11-10:
1344
+ // "bidPrice": 16726.0,
1345
+ // "bidQty": 0.05,
1346
+ // "askPrice": 16726.0,
1347
+ // "askQty": 0.05,
1348
+ // }
1349
+ //
1350
+ const marketId = this.safeString(ticker, 'symbol');
1351
+ const lastQty = this.safeString(ticker, 'lastQty');
1352
+ // in spot markets, lastQty is not present
1353
+ // it's (bad, but) the only way we can check the tickers origin
1354
+ const type = (lastQty === undefined) ? 'spot' : 'swap';
1355
+ market = this.safeMarket(marketId, market, undefined, type);
1356
+ const symbol = market['symbol'];
1357
+ const open = this.safeString(ticker, 'openPrice');
1358
+ const high = this.safeString(ticker, 'highPrice');
1359
+ const low = this.safeString(ticker, 'lowPrice');
1360
+ const close = this.safeString(ticker, 'lastPrice');
1361
+ const quoteVolume = this.safeString(ticker, 'quoteVolume');
1362
+ const baseVolume = this.safeString(ticker, 'volume');
1363
+ let percentage = this.safeString(ticker, 'priceChangePercent');
1364
+ if (percentage !== undefined) {
1365
+ percentage = percentage.replace('%', '');
1366
+ }
1367
+ const change = this.safeString(ticker, 'priceChange');
1368
+ const ts = this.safeInteger(ticker, 'closeTime');
1369
+ const datetime = this.iso8601(ts);
1370
+ const bid = this.safeString(ticker, 'bidPrice');
1371
+ const bidVolume = this.safeString(ticker, 'bidQty');
1372
+ const ask = this.safeString(ticker, 'askPrice');
1373
+ const askVolume = this.safeString(ticker, 'askQty');
1374
+ return this.safeTicker({
1375
+ 'symbol': symbol,
1376
+ 'timestamp': ts,
1377
+ 'datetime': datetime,
1378
+ 'high': high,
1379
+ 'low': low,
1380
+ 'bid': bid,
1381
+ 'bidVolume': bidVolume,
1382
+ 'ask': ask,
1383
+ 'askVolume': askVolume,
1384
+ 'vwap': undefined,
1385
+ 'open': open,
1386
+ 'close': close,
1387
+ 'last': undefined,
1388
+ 'previousClose': undefined,
1389
+ 'change': change,
1390
+ 'percentage': percentage,
1391
+ 'average': undefined,
1392
+ 'baseVolume': baseVolume,
1393
+ 'quoteVolume': quoteVolume,
1394
+ 'info': ticker,
1395
+ }, market);
1396
+ }
1397
+ async fetchBalance(params = {}) {
1398
+ /**
1399
+ * @method
1400
+ * @name bingx#fetchBalance
1401
+ * @description query for balance and get the amount of funds available for trading or funds locked in orders
1402
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Query%20Assets
1403
+ * @see https://bingx-api.github.io/docs/#/swapV2/account-api.html#Get%20Perpetual%20Swap%20Account%20Asset%20Information
1404
+ * @see https://bingx-api.github.io/docs/#/standard/contract-interface.html#Query%20standard%20contract%20balance
1405
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1406
+ * @param {boolean} [params.standard] whether to fetch standard contract balances
1407
+ * @returns {object} a [balance structure]{@link https://docs.ccxt.com/#/?id=balance-structure}
1408
+ */
1409
+ await this.loadMarkets();
1410
+ let response = undefined;
1411
+ let standard = undefined;
1412
+ [standard, params] = this.handleOptionAndParams(params, 'fetchBalance', 'standard', false);
1413
+ const [marketType, marketTypeQuery] = this.handleMarketTypeAndParams('fetchBalance', undefined, params);
1414
+ if (standard) {
1415
+ response = await this.contractV1PrivateGetBalance(marketTypeQuery);
1416
+ }
1417
+ else if (marketType === 'spot') {
1418
+ response = await this.spotV1PrivateGetAccountBalance(marketTypeQuery);
1419
+ }
1420
+ else {
1421
+ response = await this.swapV2PrivateGetUserBalance(marketTypeQuery);
1422
+ }
1423
+ //
1424
+ // spot
1425
+ //
1426
+ // {
1427
+ // "code": 0,
1428
+ // "msg": "",
1429
+ // "ttl": 1,
1430
+ // "data": {
1431
+ // "balances": [
1432
+ // {
1433
+ // "asset": "USDT",
1434
+ // "free": "16.73971130673954",
1435
+ // "locked": "0"
1436
+ // }
1437
+ // ]
1438
+ // }
1439
+ // }
1440
+ //
1441
+ // swap
1442
+ //
1443
+ // {
1444
+ // "code": 0,
1445
+ // "msg": "",
1446
+ // "data": {
1447
+ // "balance": {
1448
+ // "asset": "USDT",
1449
+ // "balance": "15.6128",
1450
+ // "equity": "15.6128",
1451
+ // "unrealizedProfit": "0.0000",
1452
+ // "realisedProfit": "0.0000",
1453
+ // "availableMargin": "15.6128",
1454
+ // "usedMargin": "0.0000",
1455
+ // "freezedMargin": "0.0000"
1456
+ // }
1457
+ // }
1458
+ // }
1459
+ // standard futures
1460
+ // {
1461
+ // "code":"0",
1462
+ // "timestamp":"1691148990942",
1463
+ // "data":[
1464
+ // {
1465
+ // "asset":"VST",
1466
+ // "balance":"100000.00000000000000000000",
1467
+ // "crossWalletBalance":"100000.00000000000000000000",
1468
+ // "crossUnPnl":"0",
1469
+ // "availableBalance":"100000.00000000000000000000",
1470
+ // "maxWithdrawAmount":"100000.00000000000000000000",
1471
+ // "marginAvailable":false,
1472
+ // "updateTime":"1691148990902"
1473
+ // },
1474
+ // {
1475
+ // "asset":"USDT",
1476
+ // "balance":"0",
1477
+ // "crossWalletBalance":"0",
1478
+ // "crossUnPnl":"0",
1479
+ // "availableBalance":"0",
1480
+ // "maxWithdrawAmount":"0",
1481
+ // "marginAvailable":false,
1482
+ // "updateTime":"1691148990902"
1483
+ // },
1484
+ // ]
1485
+ // }
1486
+ //
1487
+ return this.parseBalance(response);
1488
+ }
1489
+ parseBalance(response) {
1490
+ const data = this.safeValue(response, 'data');
1491
+ const balances = this.safeValue2(data, 'balance', 'balances', data);
1492
+ const result = { 'info': response };
1493
+ if (Array.isArray(balances)) {
1494
+ for (let i = 0; i < balances.length; i++) {
1495
+ const balance = balances[i];
1496
+ const currencyId = this.safeString(balance, 'asset');
1497
+ const code = this.safeCurrencyCode(currencyId);
1498
+ const account = this.account();
1499
+ account['free'] = this.safeString2(balance, 'free', 'availableBalance');
1500
+ account['used'] = this.safeString(balance, 'locked');
1501
+ account['total'] = this.safeString(balance, 'balance');
1502
+ result[code] = account;
1503
+ }
1504
+ }
1505
+ else {
1506
+ const currencyId = this.safeString(balances, 'asset');
1507
+ const code = this.safeCurrencyCode(currencyId);
1508
+ const account = this.account();
1509
+ account['free'] = this.safeString(balances, 'availableMargin');
1510
+ account['used'] = this.safeString(balances, 'usedMargin');
1511
+ result[code] = account;
1512
+ }
1513
+ return this.safeBalance(result);
1514
+ }
1515
+ async fetchPositions(symbols = undefined, params = {}) {
1516
+ /**
1517
+ * @method
1518
+ * @name bingx#fetchPositions
1519
+ * @description fetch all open positions
1520
+ * @see https://bingx-api.github.io/docs/#/swapV2/account-api.html#Perpetual%20Swap%20Positions
1521
+ * @see https://bingx-api.github.io/docs/#/standard/contract-interface.html#Query%20standard%20contract%20balance
1522
+ * @param {string[]|undefined} symbols list of unified market symbols
1523
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1524
+ * @param {boolean} [params.standard] whether to fetch standard contract positions
1525
+ * @returns {object[]} a list of [position structure]{@link https://docs.ccxt.com/#/?id=position-structure}
1526
+ */
1527
+ await this.loadMarkets();
1528
+ symbols = this.marketSymbols(symbols);
1529
+ let standard = undefined;
1530
+ [standard, params] = this.handleOptionAndParams(params, 'fetchPositions', 'standard', false);
1531
+ let response = undefined;
1532
+ if (standard) {
1533
+ response = await this.contractV1PrivateGetAllPosition(params);
1534
+ }
1535
+ else {
1536
+ response = await this.swapV2PrivateGetUserPositions(params);
1537
+ }
1538
+ //
1539
+ // {
1540
+ // "code": 0,
1541
+ // "msg": "",
1542
+ // "data": [
1543
+ // {
1544
+ // "symbol": "BTC-USDT",
1545
+ // "positionId": "12345678",
1546
+ // "positionSide": "LONG",
1547
+ // "isolated": true,
1548
+ // "positionAmt": "123.33",
1549
+ // "availableAmt": "128.99",
1550
+ // "unrealizedProfit": "1.22",
1551
+ // "realisedProfit": "8.1",
1552
+ // "initialMargin": "123.33",
1553
+ // "avgPrice": "2.2",
1554
+ // "leverage": 10,
1555
+ // }
1556
+ // ]
1557
+ // }
1558
+ //
1559
+ const positions = this.safeValue(response, 'data', []);
1560
+ return this.parsePositions(positions, symbols);
1561
+ }
1562
+ parsePosition(position, market = undefined) {
1563
+ //
1564
+ // {
1565
+ // "symbol": "BTC-USDT",
1566
+ // "positionId": "12345678",
1567
+ // "positionSide": "LONG",
1568
+ // "isolated": true,
1569
+ // "positionAmt": "123.33",
1570
+ // "availableAmt": "128.99",
1571
+ // "unrealizedProfit": "1.22",
1572
+ // "realisedProfit": "8.1",
1573
+ // "initialMargin": "123.33",
1574
+ // "avgPrice": "2.2",
1575
+ // "leverage": 10,
1576
+ // }
1577
+ //
1578
+ // standard position
1579
+ //
1580
+ // {
1581
+ // "currentPrice": "82.91",
1582
+ // "symbol": "LTC/USDT",
1583
+ // "initialMargin": "5.00000000000000000000",
1584
+ // "unrealizedProfit": "-0.26464500",
1585
+ // "leverage": "20.000000000",
1586
+ // "isolated": true,
1587
+ // "entryPrice": "83.13",
1588
+ // "positionSide": "LONG",
1589
+ // "positionAmt": "1.20365912",
1590
+ // }
1591
+ //
1592
+ let marketId = this.safeString(position, 'symbol', '');
1593
+ marketId = marketId.replace('/', '-'); // standard return different format
1594
+ const isolated = this.safeValue(position, 'isolated');
1595
+ let marginMode = undefined;
1596
+ if (isolated !== undefined) {
1597
+ marginMode = isolated ? 'isolated' : 'cross';
1598
+ }
1599
+ return this.safePosition({
1600
+ 'info': position,
1601
+ 'id': this.safeString(position, 'positionId'),
1602
+ 'symbol': this.safeSymbol(marketId, market, '-', 'swap'),
1603
+ 'notional': this.safeNumber(position, 'positionAmt'),
1604
+ 'marginMode': marginMode,
1605
+ 'liquidationPrice': undefined,
1606
+ 'entryPrice': this.safeNumber2(position, 'avgPrice', 'entryPrice'),
1607
+ 'unrealizedPnl': this.safeNumber(position, 'unrealizedProfit'),
1608
+ 'realizedPnl': this.safeNumber(position, 'realisedProfit'),
1609
+ 'percentage': undefined,
1610
+ 'contracts': this.safeNumber(position, 'positionAmt'),
1611
+ 'contractSize': undefined,
1612
+ 'markPrice': undefined,
1613
+ 'lastPrice': undefined,
1614
+ 'side': this.safeStringLower(position, 'positionSide'),
1615
+ 'hedged': undefined,
1616
+ 'timestamp': undefined,
1617
+ 'datetime': undefined,
1618
+ 'lastUpdateTimestamp': undefined,
1619
+ 'maintenanceMargin': undefined,
1620
+ 'maintenanceMarginPercentage': undefined,
1621
+ 'collateral': this.safeNumber(position, 'positionAmt'),
1622
+ 'initialMargin': this.safeNumber(position, 'initialMargin'),
1623
+ 'initialMarginPercentage': undefined,
1624
+ 'leverage': this.safeNumber(position, 'leverage'),
1625
+ 'marginRatio': undefined,
1626
+ 'stopLossPrice': undefined,
1627
+ 'takeProfitPrice': undefined,
1628
+ });
1629
+ }
1630
+ async createMarketOrderWithCost(symbol, side, cost, params = {}) {
1631
+ /**
1632
+ * @method
1633
+ * @name bingx#createMarketOrderWithCost
1634
+ * @description create a market order by providing the symbol, side and cost
1635
+ * @param {string} symbol unified symbol of the market to create an order in
1636
+ * @param {string} side 'buy' or 'sell'
1637
+ * @param {float} cost how much you want to trade in units of the quote currency
1638
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1639
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1640
+ */
1641
+ params['quoteOrderQty'] = cost;
1642
+ return await this.createOrder(symbol, 'market', side, cost, undefined, params);
1643
+ }
1644
+ async createMarketBuyOrderWithCost(symbol, cost, params = {}) {
1645
+ /**
1646
+ * @method
1647
+ * @name bingx#createMarketBuyOrderWithCost
1648
+ * @description create a market buy order by providing the symbol and cost
1649
+ * @param {string} symbol unified symbol of the market to create an order in
1650
+ * @param {float} cost how much you want to trade in units of the quote currency
1651
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1652
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1653
+ */
1654
+ params['quoteOrderQty'] = cost;
1655
+ return await this.createOrder(symbol, 'market', 'buy', cost, undefined, params);
1656
+ }
1657
+ async createMarketSellOrderWithCost(symbol, cost, params = {}) {
1658
+ /**
1659
+ * @method
1660
+ * @name bingx#createMarketSellOrderWithCost
1661
+ * @description create a market sell order by providing the symbol and cost
1662
+ * @param {string} symbol unified symbol of the market to create an order in
1663
+ * @param {float} cost how much you want to trade in units of the quote currency
1664
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1665
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1666
+ */
1667
+ params['quoteOrderQty'] = cost;
1668
+ return await this.createOrder(symbol, 'market', 'sell', cost, undefined, params);
1669
+ }
1670
+ createOrderRequest(symbol, type, side, amount, price = undefined, params = {}) {
1671
+ /**
1672
+ * @method
1673
+ * @ignore
1674
+ * @name bingx#createOrderRequest
1675
+ * @description helper function to build request
1676
+ * @param {string} symbol unified symbol of the market to create an order in
1677
+ * @param {string} type 'market' or 'limit'
1678
+ * @param {string} side 'buy' or 'sell'
1679
+ * @param {float} amount how much you want to trade in units of the base currency
1680
+ * @param {float} [price] the price at which the order is to be fullfilled, in units of the quote currency, ignored in market orders
1681
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1682
+ * @returns {object} request to be sent to the exchange
1683
+ */
1684
+ const market = this.market(symbol);
1685
+ let postOnly = undefined;
1686
+ let marketType = undefined;
1687
+ [marketType, params] = this.handleMarketTypeAndParams('createOrder', market, params);
1688
+ type = type.toUpperCase();
1689
+ const request = {
1690
+ 'symbol': market['id'],
1691
+ 'type': type,
1692
+ 'side': side.toUpperCase(),
1693
+ };
1694
+ const isMarketOrder = type === 'MARKET';
1695
+ const isSpot = marketType === 'spot';
1696
+ const exchangeClientOrderId = isSpot ? 'newClientOrderId' : 'clientOrderID';
1697
+ const clientOrderId = this.safeString2(params, exchangeClientOrderId, 'clientOrderId');
1698
+ if (clientOrderId !== undefined) {
1699
+ request[exchangeClientOrderId] = clientOrderId;
1700
+ }
1701
+ const timeInForce = this.safeStringUpper(params, 'timeInForce');
1702
+ if (timeInForce === 'IOC') {
1703
+ request['timeInForce'] = 'IOC';
1704
+ }
1705
+ if (isSpot) {
1706
+ [postOnly, params] = this.handlePostOnly(isMarketOrder, timeInForce === 'POC', params);
1707
+ if (postOnly || (timeInForce === 'POC')) {
1708
+ request['timeInForce'] = 'POC';
1709
+ }
1710
+ const cost = this.safeNumber2(params, 'cost', 'quoteOrderQty');
1711
+ params = this.omit(params, 'cost');
1712
+ if (cost !== undefined) {
1713
+ request['quoteOrderQty'] = this.parseToNumeric(this.costToPrecision(symbol, cost));
1714
+ }
1715
+ else {
1716
+ if (market['spot'] && isMarketOrder && (price !== undefined)) {
1717
+ // keep the legacy behavior, to avoid breaking the old spot-market-buying code
1718
+ const calculatedCost = Precise["default"].stringMul(this.numberToString(amount), this.numberToString(price));
1719
+ request['quoteOrderQty'] = this.parseToNumeric(calculatedCost);
1720
+ }
1721
+ else {
1722
+ request['quantity'] = this.parseToNumeric(this.amountToPrecision(symbol, amount));
1723
+ }
1724
+ }
1725
+ if (!isMarketOrder) {
1726
+ request['price'] = this.parseToNumeric(this.priceToPrecision(symbol, price));
1727
+ }
1728
+ }
1729
+ else {
1730
+ [postOnly, params] = this.handlePostOnly(isMarketOrder, timeInForce === 'PostOnly', params);
1731
+ if (postOnly || (timeInForce === 'PostOnly')) {
1732
+ request['timeInForce'] = 'PostOnly';
1733
+ }
1734
+ else if (timeInForce === 'GTC') {
1735
+ request['timeInForce'] = 'GTC';
1736
+ }
1737
+ else if (timeInForce === 'FOK') {
1738
+ request['timeInForce'] = 'FOK';
1739
+ }
1740
+ const triggerPrice = this.safeString2(params, 'stopPrice', 'triggerPrice');
1741
+ const stopLossPrice = this.safeString(params, 'stopLossPrice');
1742
+ const takeProfitPrice = this.safeString(params, 'takeProfitPrice');
1743
+ const trailingAmount = this.safeString(params, 'trailingAmount');
1744
+ const trailingPercent = this.safeString2(params, 'trailingPercent', 'priceRate');
1745
+ const isTriggerOrder = triggerPrice !== undefined;
1746
+ const isStopLossPriceOrder = stopLossPrice !== undefined;
1747
+ const isTakeProfitPriceOrder = takeProfitPrice !== undefined;
1748
+ const isTrailingAmountOrder = trailingAmount !== undefined;
1749
+ const isTrailingPercentOrder = trailingPercent !== undefined;
1750
+ const isTrailing = isTrailingAmountOrder || isTrailingPercentOrder;
1751
+ const stopLoss = this.safeValue(params, 'stopLoss');
1752
+ const takeProfit = this.safeValue(params, 'takeProfit');
1753
+ const isStopLoss = stopLoss !== undefined;
1754
+ const isTakeProfit = takeProfit !== undefined;
1755
+ if (((type === 'LIMIT') || (type === 'TRIGGER_LIMIT') || (type === 'STOP') || (type === 'TAKE_PROFIT')) && !isTrailing) {
1756
+ request['price'] = this.parseToNumeric(this.priceToPrecision(symbol, price));
1757
+ }
1758
+ let reduceOnly = this.safeValue(params, 'reduceOnly', false);
1759
+ if (isTriggerOrder) {
1760
+ request['stopPrice'] = this.parseToNumeric(this.priceToPrecision(symbol, triggerPrice));
1761
+ if (isMarketOrder || (type === 'TRIGGER_MARKET')) {
1762
+ request['type'] = 'TRIGGER_MARKET';
1763
+ }
1764
+ else if ((type === 'LIMIT') || (type === 'TRIGGER_LIMIT')) {
1765
+ request['type'] = 'TRIGGER_LIMIT';
1766
+ }
1767
+ }
1768
+ else if (isStopLossPriceOrder || isTakeProfitPriceOrder) {
1769
+ // This can be used to set the stop loss and take profit, but the position needs to be opened first
1770
+ reduceOnly = true;
1771
+ if (isStopLossPriceOrder) {
1772
+ request['stopPrice'] = this.parseToNumeric(this.priceToPrecision(symbol, stopLossPrice));
1773
+ if (isMarketOrder || (type === 'STOP_MARKET')) {
1774
+ request['type'] = 'STOP_MARKET';
1775
+ }
1776
+ else if ((type === 'LIMIT') || (type === 'STOP')) {
1777
+ request['type'] = 'STOP';
1778
+ }
1779
+ }
1780
+ else if (isTakeProfitPriceOrder) {
1781
+ request['stopPrice'] = this.parseToNumeric(this.priceToPrecision(symbol, takeProfitPrice));
1782
+ if (isMarketOrder || (type === 'TAKE_PROFIT_MARKET')) {
1783
+ request['type'] = 'TAKE_PROFIT_MARKET';
1784
+ }
1785
+ else if ((type === 'LIMIT') || (type === 'TAKE_PROFIT')) {
1786
+ request['type'] = 'TAKE_PROFIT';
1787
+ }
1788
+ }
1789
+ }
1790
+ else if (isTrailing) {
1791
+ request['type'] = 'TRAILING_STOP_MARKET';
1792
+ if (isTrailingAmountOrder) {
1793
+ request['price'] = this.parseToNumeric(trailingAmount);
1794
+ }
1795
+ else if (isTrailingPercentOrder) {
1796
+ const requestTrailingPercent = Precise["default"].stringDiv(trailingPercent, '100');
1797
+ request['priceRate'] = this.parseToNumeric(requestTrailingPercent);
1798
+ }
1799
+ }
1800
+ if (isStopLoss || isTakeProfit) {
1801
+ if (isStopLoss) {
1802
+ const slTriggerPrice = this.safeString2(stopLoss, 'triggerPrice', 'stopPrice', stopLoss);
1803
+ const slWorkingType = this.safeString(stopLoss, 'workingType', 'MARK_PRICE');
1804
+ const slType = this.safeString(stopLoss, 'type', 'STOP_MARKET');
1805
+ const slRequest = {
1806
+ 'stopPrice': this.parseToNumeric(this.priceToPrecision(symbol, slTriggerPrice)),
1807
+ 'workingType': slWorkingType,
1808
+ 'type': slType,
1809
+ };
1810
+ const slPrice = this.safeString(stopLoss, 'price');
1811
+ if (slPrice !== undefined) {
1812
+ slRequest['price'] = this.parseToNumeric(this.priceToPrecision(symbol, slPrice));
1813
+ }
1814
+ const slQuantity = this.safeString(stopLoss, 'quantity', amount);
1815
+ slRequest['quantity'] = this.parseToNumeric(this.amountToPrecision(symbol, slQuantity));
1816
+ request['stopLoss'] = this.json(slRequest);
1817
+ }
1818
+ if (isTakeProfit) {
1819
+ const tkTriggerPrice = this.safeString2(takeProfit, 'triggerPrice', 'stopPrice', takeProfit);
1820
+ const tkWorkingType = this.safeString(takeProfit, 'workingType', 'MARK_PRICE');
1821
+ const tpType = this.safeString(takeProfit, 'type', 'TAKE_PROFIT_MARKET');
1822
+ const tpRequest = {
1823
+ 'stopPrice': this.parseToNumeric(this.priceToPrecision(symbol, tkTriggerPrice)),
1824
+ 'workingType': tkWorkingType,
1825
+ 'type': tpType,
1826
+ };
1827
+ const slPrice = this.safeString(takeProfit, 'price');
1828
+ if (slPrice !== undefined) {
1829
+ tpRequest['price'] = this.parseToNumeric(this.priceToPrecision(symbol, slPrice));
1830
+ }
1831
+ const tkQuantity = this.safeString(takeProfit, 'quantity', amount);
1832
+ tpRequest['quantity'] = this.parseToNumeric(this.amountToPrecision(symbol, tkQuantity));
1833
+ request['takeProfit'] = this.json(tpRequest);
1834
+ }
1835
+ }
1836
+ let positionSide = undefined;
1837
+ if (reduceOnly) {
1838
+ positionSide = (side === 'buy') ? 'SHORT' : 'LONG';
1839
+ }
1840
+ else {
1841
+ positionSide = (side === 'buy') ? 'LONG' : 'SHORT';
1842
+ }
1843
+ request['positionSide'] = positionSide;
1844
+ request['quantity'] = this.parseToNumeric(this.amountToPrecision(symbol, amount));
1845
+ params = this.omit(params, ['reduceOnly', 'triggerPrice', 'stopLossPrice', 'takeProfitPrice', 'trailingAmount', 'trailingPercent', 'takeProfit', 'stopLoss', 'clientOrderId']);
1846
+ }
1847
+ return this.extend(request, params);
1848
+ }
1849
+ async createOrder(symbol, type, side, amount, price = undefined, params = {}) {
1850
+ /**
1851
+ * @method
1852
+ * @name bingx#createOrder
1853
+ * @description create a trade order
1854
+ * @see https://bingx-api.github.io/docs/#/en-us/swapV2/trade-api.html#Trade%20order
1855
+ * @see https://bingx-api.github.io/docs/#/en-us/spot/trade-api.html#Create%20an%20Order
1856
+ * @param {string} symbol unified symbol of the market to create an order in
1857
+ * @param {string} type 'market' or 'limit'
1858
+ * @param {string} side 'buy' or 'sell'
1859
+ * @param {float} amount how much you want to trade in units of the base currency
1860
+ * @param {float} [price] the price at which the order is to be fullfilled, in units of the quote currency, ignored in market orders
1861
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1862
+ * @param {string} [params.clientOrderId] a unique id for the order
1863
+ * @param {bool} [params.postOnly] true to place a post only order
1864
+ * @param {string} [params.timeInForce] spot supports 'PO' and 'IOC', swap supports 'PO', 'GTC', 'IOC' and 'FOK'
1865
+ * @param {bool} [params.reduceOnly] *swap only* true or false whether the order is reduce only
1866
+ * @param {float} [params.triggerPrice] *swap only* triggerPrice at which the attached take profit / stop loss order will be triggered
1867
+ * @param {float} [params.stopLossPrice] *swap only* stop loss trigger price
1868
+ * @param {float} [params.takeProfitPrice] *swap only* take profit trigger price
1869
+ * @param {float} [params.cost] the quote quantity that can be used as an alternative for the amount
1870
+ * @param {float} [params.trailingAmount] *swap only* the quote amount to trail away from the current market price
1871
+ * @param {float} [params.trailingPercent] *swap only* the percent to trail away from the current market price
1872
+ * @param {object} [params.takeProfit] *takeProfit object in params* containing the triggerPrice at which the attached take profit order will be triggered
1873
+ * @param {float} [params.takeProfit.triggerPrice] take profit trigger price
1874
+ * @param {object} [params.stopLoss] *stopLoss object in params* containing the triggerPrice at which the attached stop loss order will be triggered
1875
+ * @param {float} [params.stopLoss.triggerPrice] stop loss trigger price
1876
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1877
+ */
1878
+ await this.loadMarkets();
1879
+ const market = this.market(symbol);
1880
+ const request = this.createOrderRequest(symbol, type, side, amount, price, params);
1881
+ let response = undefined;
1882
+ if (market['swap']) {
1883
+ response = await this.swapV2PrivatePostTradeOrder(request);
1884
+ }
1885
+ else {
1886
+ response = await this.spotV1PrivatePostTradeOrder(request);
1887
+ }
1888
+ //
1889
+ // spot
1890
+ //
1891
+ // {
1892
+ // "code": 0,
1893
+ // "msg": "",
1894
+ // "data": {
1895
+ // "symbol": "XRP-USDT",
1896
+ // "orderId": 1514090846268424192,
1897
+ // "transactTime": 1649822362855,
1898
+ // "price": "0.5",
1899
+ // "origQty": "10",
1900
+ // "executedQty": "0",
1901
+ // "cummulativeQuoteQty": "0",
1902
+ // "status": "PENDING",
1903
+ // "type": "LIMIT",
1904
+ // "side": "BUY"
1905
+ // }
1906
+ // }
1907
+ //
1908
+ // swap
1909
+ //
1910
+ // {
1911
+ // "code": 0,
1912
+ // "msg": "",
1913
+ // "data": {
1914
+ // "order": {
1915
+ // "symbol": "BTC-USDT",
1916
+ // "orderId": 1709036527545438208,
1917
+ // "side": "BUY",
1918
+ // "positionSide": "LONG",
1919
+ // "type": "TRIGGER_LIMIT",
1920
+ // "clientOrderID": "",
1921
+ // "workingType": ""
1922
+ // }
1923
+ // }
1924
+ // }
1925
+ //
1926
+ if (typeof response === 'string') {
1927
+ // broken api engine : order-ids are too long numbers (i.e. 1742930526912864656)
1928
+ // and JSON.parse can not handle them in JS, so we have to use .parseJson
1929
+ // however, when order has an attached SL/TP, their value types need extra parsing
1930
+ response = this.fixStringifiedJsonMembers(response);
1931
+ response = this.parseJson(response);
1932
+ }
1933
+ const data = this.safeValue(response, 'data', {});
1934
+ const order = this.safeValue(data, 'order', data);
1935
+ return this.parseOrder(order, market);
1936
+ }
1937
+ async createOrders(orders, params = {}) {
1938
+ /**
1939
+ * @method
1940
+ * @name bingx#createOrders
1941
+ * @description create a list of trade orders
1942
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Batch%20Placing%20Orders
1943
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Bulk%20order
1944
+ * @param {Array} orders list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
1945
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
1946
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
1947
+ */
1948
+ await this.loadMarkets();
1949
+ const ordersRequests = [];
1950
+ let symbol = undefined;
1951
+ for (let i = 0; i < orders.length; i++) {
1952
+ const rawOrder = orders[i];
1953
+ const marketId = this.safeString(rawOrder, 'symbol');
1954
+ if (symbol === undefined) {
1955
+ symbol = marketId;
1956
+ }
1957
+ else {
1958
+ if (symbol !== marketId) {
1959
+ throw new errors.BadRequest(this.id + ' createOrders() requires all orders to have the same symbol');
1960
+ }
1961
+ }
1962
+ const type = this.safeString(rawOrder, 'type');
1963
+ const side = this.safeString(rawOrder, 'side');
1964
+ const amount = this.safeNumber(rawOrder, 'amount');
1965
+ const price = this.safeNumber(rawOrder, 'price');
1966
+ const orderParams = this.safeValue(rawOrder, 'params', {});
1967
+ const orderRequest = this.createOrderRequest(marketId, type, side, amount, price, orderParams);
1968
+ ordersRequests.push(orderRequest);
1969
+ }
1970
+ const market = this.market(symbol);
1971
+ const request = {};
1972
+ let response = undefined;
1973
+ if (market['swap']) {
1974
+ request['batchOrders'] = this.json(ordersRequests);
1975
+ response = await this.swapV2PrivatePostTradeBatchOrders(request);
1976
+ }
1977
+ else {
1978
+ request['data'] = this.json(ordersRequests);
1979
+ response = await this.spotV1PrivatePostTradeBatchOrders(request);
1980
+ }
1981
+ //
1982
+ // spot
1983
+ //
1984
+ // {
1985
+ // "code": 0,
1986
+ // "msg": "",
1987
+ // "debugMsg": "",
1988
+ // "data": {
1989
+ // "orders": [
1990
+ // {
1991
+ // "symbol": "BTC-USDT",
1992
+ // "orderId": 1720661389564968960,
1993
+ // "transactTime": 1699072618272,
1994
+ // "price": "25000",
1995
+ // "origQty": "0.0002",
1996
+ // "executedQty": "0",
1997
+ // "cummulativeQuoteQty": "0",
1998
+ // "status": "PENDING",
1999
+ // "type": "LIMIT",
2000
+ // "side": "BUY"
2001
+ // },
2002
+ // ]
2003
+ // }
2004
+ // }
2005
+ //
2006
+ // swap
2007
+ //
2008
+ // {
2009
+ // "code": 0,
2010
+ // "msg": "",
2011
+ // "data": {
2012
+ // "orders": [
2013
+ // {
2014
+ // "symbol": "BTC-USDT",
2015
+ // "orderId": 1720657081994006528,
2016
+ // "side": "BUY",
2017
+ // "positionSide": "LONG",
2018
+ // "type": "LIMIT",
2019
+ // "clientOrderID": "",
2020
+ // "workingType": ""
2021
+ // },
2022
+ // ]
2023
+ // }
2024
+ // }
2025
+ //
2026
+ const data = this.safeValue(response, 'data', {});
2027
+ const result = this.safeValue(data, 'orders', []);
2028
+ return this.parseOrders(result, market);
2029
+ }
2030
+ parseOrderSide(side) {
2031
+ const sides = {
2032
+ 'BUY': 'buy',
2033
+ 'SELL': 'sell',
2034
+ 'SHORT': 'sell',
2035
+ 'LONG': 'buy',
2036
+ };
2037
+ return this.safeString(sides, side, side);
2038
+ }
2039
+ parseOrder(order, market = undefined) {
2040
+ //
2041
+ // spot
2042
+ // createOrder, createOrders, cancelOrder
2043
+ //
2044
+ // {
2045
+ // "symbol": "XRP-USDT",
2046
+ // "orderId": 1514090846268424192,
2047
+ // "transactTime": 1649822362855,
2048
+ // "price": "0.5",
2049
+ // "origQty": "10",
2050
+ // "executedQty": "0",
2051
+ // "cummulativeQuoteQty": "0",
2052
+ // "status": "PENDING",
2053
+ // "type": "LIMIT",
2054
+ // "side": "BUY"
2055
+ // }
2056
+ //
2057
+ // fetchOrder
2058
+ //
2059
+ // {
2060
+ // "symbol": "ETH-USDT",
2061
+ // "orderId": "1660602123001266176",
2062
+ // "price": "1700",
2063
+ // "origQty": "0.003",
2064
+ // "executedQty": "0",
2065
+ // "cummulativeQuoteQty": "0",
2066
+ // "status": "PENDING",
2067
+ // "type": "LIMIT",
2068
+ // "side": "BUY",
2069
+ // "time": "1684753373276",
2070
+ // "updateTime": "1684753373276",
2071
+ // "origQuoteOrderQty": "0",
2072
+ // "fee": "0",
2073
+ // "feeAsset": "ETH"
2074
+ // }
2075
+ //
2076
+ // fetchOpenOrders, fetchClosedOrders
2077
+ //
2078
+ // {
2079
+ // "symbol": "XRP-USDT",
2080
+ // "orderId": 1514073325788200960,
2081
+ // "price": "0.5",
2082
+ // "StopPrice": "0",
2083
+ // "origQty": "20",
2084
+ // "executedQty": "10",
2085
+ // "cummulativeQuoteQty": "5",
2086
+ // "status": "PENDING",
2087
+ // "type": "LIMIT",
2088
+ // "side": "BUY",
2089
+ // "time": 1649818185647,
2090
+ // "updateTime": 1649818185647,
2091
+ // "origQuoteOrderQty": "0"
2092
+ // "fee": "-0.01"
2093
+ // }
2094
+ //
2095
+ //
2096
+ // swap
2097
+ // createOrder, createOrders
2098
+ //
2099
+ // {
2100
+ // "symbol": "BTC-USDT",
2101
+ // "orderId": 1590973236294713344,
2102
+ // "side": "BUY",
2103
+ // "positionSide": "LONG",
2104
+ // "type": "LIMIT"
2105
+ // }
2106
+ //
2107
+ // fetchOrder, fetchOpenOrders, fetchClosedOrders
2108
+ //
2109
+ // {
2110
+ // "symbol": "BTC-USDT",
2111
+ // "orderId": 1709036527545438208,
2112
+ // "side": "BUY",
2113
+ // "positionSide": "LONG",
2114
+ // "type": "TRIGGER_LIMIT",
2115
+ // "origQty": "0.0010",
2116
+ // "price": "22000.0",
2117
+ // "executedQty": "0.0000",
2118
+ // "avgPrice": "0.0",
2119
+ // "cumQuote": "",
2120
+ // "stopPrice": "23000.0",
2121
+ // "profit": "",
2122
+ // "commission": "",
2123
+ // "status": "NEW",
2124
+ // "time": 1696301035187,
2125
+ // "updateTime": 1696301035187,
2126
+ // "clientOrderId": "",
2127
+ // "leverage": "",
2128
+ // "takeProfit": "",
2129
+ // "stopLoss": "",
2130
+ // "advanceAttr": 0,
2131
+ // "positionID": 0,
2132
+ // "takeProfitEntrustPrice": 0,
2133
+ // "stopLossEntrustPrice": 0,
2134
+ // "orderType": "",
2135
+ // "workingType": "MARK_PRICE"
2136
+ // }
2137
+ // with tp and sl
2138
+ // {
2139
+ // orderId: 1741440894764281900,
2140
+ // symbol: 'LTC-USDT',
2141
+ // positionSide: 'LONG',
2142
+ // side: 'BUY',
2143
+ // type: 'MARKET',
2144
+ // price: 0,
2145
+ // quantity: 1,
2146
+ // stopPrice: 0,
2147
+ // workingType: 'MARK_PRICE',
2148
+ // clientOrderID: '',
2149
+ // timeInForce: 'GTC',
2150
+ // priceRate: 0,
2151
+ // stopLoss: '{"stopPrice":50,"workingType":"MARK_PRICE","type":"STOP_MARKET","quantity":1}',
2152
+ // takeProfit: '{"stopPrice":150,"workingType":"MARK_PRICE","type":"TAKE_PROFIT_MARKET","quantity":1}',
2153
+ // reduceOnly: false
2154
+ // }
2155
+ //
2156
+ const positionSide = this.safeString2(order, 'positionSide', 'ps');
2157
+ const marketType = (positionSide === undefined) ? 'spot' : 'swap';
2158
+ const marketId = this.safeString2(order, 'symbol', 's');
2159
+ if (market === undefined) {
2160
+ market = this.safeMarket(marketId, undefined, undefined, marketType);
2161
+ }
2162
+ const symbol = this.safeSymbol(marketId, market, '-', marketType);
2163
+ const orderId = this.safeString2(order, 'orderId', 'i');
2164
+ const side = this.safeStringLower2(order, 'side', 'S');
2165
+ const type = this.safeStringLower2(order, 'type', 'o');
2166
+ const timestamp = this.safeIntegerN(order, ['time', 'transactTime', 'E']);
2167
+ const lastTradeTimestamp = this.safeInteger2(order, 'updateTime', 'T');
2168
+ const price = this.safeString2(order, 'price', 'p');
2169
+ const average = this.safeString2(order, 'avgPrice', 'ap');
2170
+ const amount = this.safeString2(order, 'origQty', 'q');
2171
+ const filled = this.safeString2(order, 'executedQty', 'z');
2172
+ const statusId = this.safeString2(order, 'status', 'X');
2173
+ let feeCurrencyCode = this.safeString2(order, 'feeAsset', 'N');
2174
+ const feeCost = this.safeStringN(order, ['fee', 'commission', 'n']);
2175
+ if ((feeCurrencyCode === undefined)) {
2176
+ if (market['spot']) {
2177
+ if (side === 'buy') {
2178
+ feeCurrencyCode = market['base'];
2179
+ }
2180
+ else {
2181
+ feeCurrencyCode = market['quote'];
2182
+ }
2183
+ }
2184
+ else {
2185
+ feeCurrencyCode = market['quote'];
2186
+ }
2187
+ }
2188
+ const fee = {
2189
+ 'currency': feeCurrencyCode,
2190
+ 'cost': Precise["default"].stringAbs(feeCost),
2191
+ };
2192
+ const clientOrderId = this.safeStringN(order, ['clientOrderID', 'origClientOrderId', 'c']);
2193
+ let stopLoss = this.safeValue(order, 'stopLoss');
2194
+ let stopLossPrice = undefined;
2195
+ if (stopLoss !== undefined) {
2196
+ stopLossPrice = this.safeNumber(stopLoss, 'stopLoss');
2197
+ }
2198
+ if ((stopLoss !== undefined) && (typeof stopLoss !== 'number')) {
2199
+ // stopLoss: '{"stopPrice":50,"workingType":"MARK_PRICE","type":"STOP_MARKET","quantity":1}',
2200
+ if (typeof stopLoss === 'string') {
2201
+ stopLoss = this.parseJson(stopLoss);
2202
+ }
2203
+ stopLossPrice = this.safeNumber(stopLoss, 'stopPrice');
2204
+ }
2205
+ let takeProfit = this.safeValue(order, 'takeProfit');
2206
+ let takeProfitPrice = undefined;
2207
+ if (takeProfit !== undefined) {
2208
+ takeProfitPrice = this.safeNumber(takeProfit, 'takeProfit');
2209
+ }
2210
+ if ((takeProfit !== undefined) && (typeof takeProfit !== 'number')) {
2211
+ // takeProfit: '{"stopPrice":150,"workingType":"MARK_PRICE","type":"TAKE_PROFIT_MARKET","quantity":1}',
2212
+ if (typeof takeProfit === 'string') {
2213
+ takeProfit = this.parseJson(takeProfit);
2214
+ }
2215
+ takeProfitPrice = this.safeNumber(takeProfit, 'stopPrice');
2216
+ }
2217
+ return this.safeOrder({
2218
+ 'info': order,
2219
+ 'id': orderId,
2220
+ 'clientOrderId': clientOrderId,
2221
+ 'timestamp': timestamp,
2222
+ 'datetime': this.iso8601(timestamp),
2223
+ 'lastTradeTimestamp': lastTradeTimestamp,
2224
+ 'lastUpdateTimestamp': this.safeInteger(order, 'updateTime'),
2225
+ 'symbol': symbol,
2226
+ 'type': type,
2227
+ 'timeInForce': undefined,
2228
+ 'postOnly': undefined,
2229
+ 'side': this.parseOrderSide(side),
2230
+ 'price': price,
2231
+ 'stopPrice': this.safeNumber(order, 'stopPrice'),
2232
+ 'triggerPrice': this.safeNumber(order, 'stopPrice'),
2233
+ 'stopLossPrice': stopLossPrice,
2234
+ 'takeProfitPrice': takeProfitPrice,
2235
+ 'average': average,
2236
+ 'cost': undefined,
2237
+ 'amount': amount,
2238
+ 'filled': filled,
2239
+ 'remaining': undefined,
2240
+ 'status': this.parseOrderStatus(statusId),
2241
+ 'fee': fee,
2242
+ 'trades': undefined,
2243
+ }, market);
2244
+ }
2245
+ parseOrderStatus(status) {
2246
+ const statuses = {
2247
+ 'NEW': 'open',
2248
+ 'PENDING': 'open',
2249
+ 'PARTIALLY_FILLED': 'open',
2250
+ 'FILLED': 'closed',
2251
+ 'CANCELED': 'canceled',
2252
+ 'CANCELLED': 'canceled',
2253
+ 'FAILED': 'failed',
2254
+ };
2255
+ return this.safeString(statuses, status, status);
2256
+ }
2257
+ async cancelOrder(id, symbol = undefined, params = {}) {
2258
+ /**
2259
+ * @method
2260
+ * @name bingx#cancelOrder
2261
+ * @description cancels an open order
2262
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Cancel%20an%20Order
2263
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Cancel%20an%20Order
2264
+ * @param {string} id order id
2265
+ * @param {string} symbol unified symbol of the market the order was made in
2266
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2267
+ * @param {string} [params.clientOrderId] a unique id for the order
2268
+ * @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
2269
+ */
2270
+ if (symbol === undefined) {
2271
+ throw new errors.ArgumentsRequired(this.id + ' cancelOrder() requires a symbol argument');
2272
+ }
2273
+ await this.loadMarkets();
2274
+ const market = this.market(symbol);
2275
+ const request = {
2276
+ 'symbol': market['id'],
2277
+ };
2278
+ const clientOrderId = this.safeString2(params, 'clientOrderId', 'clientOrderID');
2279
+ params = this.omit(params, ['clientOrderId']);
2280
+ if (clientOrderId !== undefined) {
2281
+ request['clientOrderID'] = clientOrderId;
2282
+ }
2283
+ else {
2284
+ request['orderId'] = id;
2285
+ }
2286
+ let response = undefined;
2287
+ const [marketType, query] = this.handleMarketTypeAndParams('cancelOrder', market, params);
2288
+ if (marketType === 'spot') {
2289
+ response = await this.spotV1PrivatePostTradeCancel(this.extend(request, query));
2290
+ }
2291
+ else {
2292
+ response = await this.swapV2PrivateDeleteTradeOrder(this.extend(request, query));
2293
+ }
2294
+ //
2295
+ // spot
2296
+ //
2297
+ // {
2298
+ // "code": 0,
2299
+ // "msg": "",
2300
+ // "data": {
2301
+ // "symbol": "XRP-USDT",
2302
+ // "orderId": 1514090846268424192,
2303
+ // "price": "0.5",
2304
+ // "origQty": "10",
2305
+ // "executedQty": "0",
2306
+ // "cummulativeQuoteQty": "0",
2307
+ // "status": "CANCELED",
2308
+ // "type": "LIMIT",
2309
+ // "side": "BUY"
2310
+ // }
2311
+ // }
2312
+ //
2313
+ // swap
2314
+ //
2315
+ // {
2316
+ // "code": 0,
2317
+ // "msg": "",
2318
+ // "data": {
2319
+ // "order": {
2320
+ // "symbol": "LINK-USDT",
2321
+ // "orderId": 1597783850786750464,
2322
+ // "side": "BUY",
2323
+ // "positionSide": "LONG",
2324
+ // "type": "TRIGGER_MARKET",
2325
+ // "origQty": "5.0",
2326
+ // "price": "5.0000",
2327
+ // "executedQty": "0.0",
2328
+ // "avgPrice": "0.0000",
2329
+ // "cumQuote": "0",
2330
+ // "stopPrice": "5.0000",
2331
+ // "profit": "",
2332
+ // "commission": "",
2333
+ // "status": "CANCELLED",
2334
+ // "time": 1669776330000,
2335
+ // "updateTime": 1669776330000
2336
+ // }
2337
+ // }
2338
+ // }
2339
+ //
2340
+ const data = this.safeValue(response, 'data');
2341
+ const first = this.safeValue(data, 'order', data);
2342
+ return this.parseOrder(first, market);
2343
+ }
2344
+ async cancelAllOrders(symbol = undefined, params = {}) {
2345
+ /**
2346
+ * @method
2347
+ * @name bingx#cancelAllOrders
2348
+ * @description cancel all open orders
2349
+ * @see https://bingx-api.github.io/docs/#/en-us/spot/trade-api.html#Cancel%20orders%20by%20symbol
2350
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Cancel%20All%20Orders
2351
+ * @param {string} [symbol] unified market symbol, only orders in the market of this symbol are cancelled when symbol is not undefined
2352
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2353
+ * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2354
+ */
2355
+ if (symbol === undefined) {
2356
+ throw new errors.ArgumentsRequired(this.id + ' cancelAllOrders() requires a symbol argument');
2357
+ }
2358
+ await this.loadMarkets();
2359
+ const market = this.market(symbol);
2360
+ const request = {
2361
+ 'symbol': market['id'],
2362
+ };
2363
+ let response = undefined;
2364
+ if (market['spot']) {
2365
+ response = await this.spotV1PrivatePostTradeCancelOpenOrders(this.extend(request, params));
2366
+ //
2367
+ // {
2368
+ // "code": 0,
2369
+ // "msg": "",
2370
+ // "debugMsg": "",
2371
+ // "data": {
2372
+ // "orders": [{
2373
+ // "symbol": "ADA-USDT",
2374
+ // "orderId": 1740659971369992192,
2375
+ // "transactTime": 1703840651730,
2376
+ // "price": 5,
2377
+ // "stopPrice": 0,
2378
+ // "origQty": 10,
2379
+ // "executedQty": 0,
2380
+ // "cummulativeQuoteQty": 0,
2381
+ // "status": "CANCELED",
2382
+ // "type": "LIMIT",
2383
+ // "side": "SELL"
2384
+ // }]
2385
+ // }
2386
+ // }
2387
+ //
2388
+ }
2389
+ else if (market['swap']) {
2390
+ response = await this.swapV2PrivateDeleteTradeAllOpenOrders(this.extend(request, params));
2391
+ //
2392
+ // {
2393
+ // "code": 0,
2394
+ // "msg": "",
2395
+ // "data": {
2396
+ // "success": [
2397
+ // {
2398
+ // "symbol": "LINK-USDT",
2399
+ // "orderId": 1597783835095859200,
2400
+ // "side": "BUY",
2401
+ // "positionSide": "LONG",
2402
+ // "type": "TRIGGER_LIMIT",
2403
+ // "origQty": "5.0",
2404
+ // "price": "9.0000",
2405
+ // "executedQty": "0.0",
2406
+ // "avgPrice": "0.0000",
2407
+ // "cumQuote": "0",
2408
+ // "stopPrice": "9.5000",
2409
+ // "profit": "",
2410
+ // "commission": "",
2411
+ // "status": "NEW",
2412
+ // "time": 1669776326000,
2413
+ // "updateTime": 1669776326000
2414
+ // }
2415
+ // ],
2416
+ // "failed": null
2417
+ // }
2418
+ // }
2419
+ //
2420
+ }
2421
+ else {
2422
+ throw new errors.BadRequest(this.id + ' cancelAllOrders is only supported for spot and swap markets.');
2423
+ }
2424
+ return response;
2425
+ }
2426
+ async cancelOrders(ids, symbol = undefined, params = {}) {
2427
+ /**
2428
+ * @method
2429
+ * @name bingx#cancelOrders
2430
+ * @description cancel multiple orders
2431
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Cancel%20a%20Batch%20of%20Orders
2432
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Cancel%20a%20Batch%20of%20Orders
2433
+ * @param {string[]} ids order ids
2434
+ * @param {string} symbol unified market symbol, default is undefined
2435
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2436
+ * @param {string[]} [params.clientOrderIds] client order ids
2437
+ * @returns {object} an list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2438
+ */
2439
+ if (symbol === undefined) {
2440
+ throw new errors.ArgumentsRequired(this.id + ' cancelOrders() requires a symbol argument');
2441
+ }
2442
+ await this.loadMarkets();
2443
+ const market = this.market(symbol);
2444
+ const request = {
2445
+ 'symbol': market['id'],
2446
+ };
2447
+ const clientOrderIds = this.safeValue(params, 'clientOrderIds');
2448
+ params = this.omit(params, 'clientOrderIds');
2449
+ let idsToParse = ids;
2450
+ const areClientOrderIds = (clientOrderIds !== undefined);
2451
+ if (areClientOrderIds) {
2452
+ idsToParse = clientOrderIds;
2453
+ }
2454
+ const parsedIds = [];
2455
+ for (let i = 0; i < idsToParse.length; i++) {
2456
+ const id = idsToParse[i];
2457
+ const stringId = id.toString();
2458
+ parsedIds.push(stringId);
2459
+ }
2460
+ let response = undefined;
2461
+ if (market['spot']) {
2462
+ const spotReqKey = areClientOrderIds ? 'clientOrderIDs' : 'orderIds';
2463
+ request[spotReqKey] = parsedIds.join(',');
2464
+ response = await this.spotV1PrivatePostTradeCancelOrders(this.extend(request, params));
2465
+ }
2466
+ else {
2467
+ if (areClientOrderIds) {
2468
+ request['clientOrderIDList'] = this.json(parsedIds);
2469
+ }
2470
+ else {
2471
+ request['orderIdList'] = parsedIds;
2472
+ }
2473
+ response = await this.swapV2PrivateDeleteTradeBatchOrders(this.extend(request, params));
2474
+ }
2475
+ //
2476
+ // {
2477
+ // "code": 0,
2478
+ // "msg": "",
2479
+ // "data": {
2480
+ // "success": [
2481
+ // {
2482
+ // "symbol": "LINK-USDT",
2483
+ // "orderId": 1597783850786750464,
2484
+ // "side": "BUY",
2485
+ // "positionSide": "LONG",
2486
+ // "type": "TRIGGER_MARKET",
2487
+ // "origQty": "5.0",
2488
+ // "price": "5.5710",
2489
+ // "executedQty": "0.0",
2490
+ // "avgPrice": "0.0000",
2491
+ // "cumQuote": "0",
2492
+ // "stopPrice": "5.0000",
2493
+ // "profit": "0.0000",
2494
+ // "commission": "0.000000",
2495
+ // "status": "CANCELLED",
2496
+ // "time": 1669776330000,
2497
+ // "updateTime": 1672370837000
2498
+ // }
2499
+ // ],
2500
+ // "failed": null
2501
+ // }
2502
+ // }
2503
+ //
2504
+ return response;
2505
+ }
2506
+ async fetchOrder(id, symbol = undefined, params = {}) {
2507
+ /**
2508
+ * @method
2509
+ * @name bingx#fetchOrder
2510
+ * @description fetches information on an order made by the user
2511
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Query%20Orders
2512
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Query%20Order
2513
+ * @param {string} symbol unified symbol of the market the order was made in
2514
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2515
+ * @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
2516
+ */
2517
+ if (symbol === undefined) {
2518
+ throw new errors.ArgumentsRequired(this.id + ' fetchOrders() requires a symbol argument');
2519
+ }
2520
+ await this.loadMarkets();
2521
+ const market = this.market(symbol);
2522
+ const request = {
2523
+ 'symbol': market['id'],
2524
+ 'orderId': id,
2525
+ };
2526
+ let response = undefined;
2527
+ const [marketType, query] = this.handleMarketTypeAndParams('fetchOrder', market, params);
2528
+ if (marketType === 'spot') {
2529
+ response = await this.spotV1PrivateGetTradeQuery(this.extend(request, query));
2530
+ }
2531
+ else {
2532
+ response = await this.swapV2PrivateGetTradeOrder(this.extend(request, query));
2533
+ }
2534
+ //
2535
+ // spot
2536
+ //
2537
+ // {
2538
+ // "code": 0,
2539
+ // "msg": "",
2540
+ // "data": {
2541
+ // "symbol": "XRP-USDT",
2542
+ // "orderId": 1514087361158316032,
2543
+ // "price": "0.5",
2544
+ // "origQty": "10",
2545
+ // "executedQty": "0",
2546
+ // "cummulativeQuoteQty": "0",
2547
+ // "status": "CANCELED",
2548
+ // "type": "LIMIT",
2549
+ // "side": "BUY",
2550
+ // "time": 1649821532000,
2551
+ // "updateTime": 1649821543000,
2552
+ // "origQuoteOrderQty": "0",
2553
+ // "fee": "0",
2554
+ // "feeAsset": "XRP"
2555
+ // }
2556
+ // }
2557
+ //
2558
+ // swap
2559
+ //
2560
+ // {
2561
+ // "code": 0,
2562
+ // "msg": "",
2563
+ // "data": {
2564
+ // "order": {
2565
+ // "symbol": "BTC-USDT",
2566
+ // "orderId": 1597597642269917184,
2567
+ // "side": "SELL",
2568
+ // "positionSide": "LONG",
2569
+ // "type": "TAKE_PROFIT_MARKET",
2570
+ // "origQty": "1.0000",
2571
+ // "price": "0.0",
2572
+ // "executedQty": "0.0000",
2573
+ // "avgPrice": "0.0",
2574
+ // "cumQuote": "",
2575
+ // "stopPrice": "16494.0",
2576
+ // "profit": "",
2577
+ // "commission": "",
2578
+ // "status": "FILLED",
2579
+ // "time": 1669731935000,
2580
+ // "updateTime": 1669752524000
2581
+ // }
2582
+ // }
2583
+ // }
2584
+ //
2585
+ const data = this.safeValue(response, 'data');
2586
+ const first = this.safeValue(data, 'order', data);
2587
+ return this.parseOrder(first, market);
2588
+ }
2589
+ async fetchOpenOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
2590
+ /**
2591
+ * @method
2592
+ * @name bingx#fetchOpenOrders
2593
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Query%20Open%20Orders
2594
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Query%20all%20current%20pending%20orders
2595
+ * @description fetch all unfilled currently open orders
2596
+ * @param {string} symbol unified market symbol
2597
+ * @param {int} [since] the earliest time in ms to fetch open orders for
2598
+ * @param {int} [limit] the maximum number of open order structures to retrieve
2599
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2600
+ * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2601
+ */
2602
+ await this.loadMarkets();
2603
+ let market = undefined;
2604
+ const request = {};
2605
+ if (symbol !== undefined) {
2606
+ market = this.market(symbol);
2607
+ request['symbol'] = market['id'];
2608
+ }
2609
+ let response = undefined;
2610
+ const [marketType, query] = this.handleMarketTypeAndParams('fetchOpenOrders', market, params);
2611
+ if (marketType === 'spot') {
2612
+ response = await this.spotV1PrivateGetTradeOpenOrders(this.extend(request, query));
2613
+ }
2614
+ else {
2615
+ response = await this.swapV2PrivateGetTradeOpenOrders(this.extend(request, query));
2616
+ }
2617
+ //
2618
+ // spot
2619
+ //
2620
+ // {
2621
+ // "code": 0,
2622
+ // "msg": "",
2623
+ // "data": {
2624
+ // "orders": [
2625
+ // {
2626
+ // "symbol": "XRP-USDT",
2627
+ // "orderId": 1514073325788200960,
2628
+ // "price": "0.5",
2629
+ // "origQty": "20",
2630
+ // "executedQty": "0",
2631
+ // "cummulativeQuoteQty": "0",
2632
+ // "status": "PENDING",
2633
+ // "type": "LIMIT",
2634
+ // "side": "BUY",
2635
+ // "time": 1649818185647,
2636
+ // "updateTime": 1649818185647,
2637
+ // "origQuoteOrderQty": "0"
2638
+ // }
2639
+ // ]
2640
+ // }
2641
+ // }
2642
+ //
2643
+ // swap
2644
+ //
2645
+ // {
2646
+ // "code": 0,
2647
+ // "msg": "",
2648
+ // "data": {
2649
+ // "orders": [
2650
+ // {
2651
+ // "symbol": "LINK-USDT",
2652
+ // "orderId": 1585839271162413056,
2653
+ // "side": "BUY",
2654
+ // "positionSide": "LONG",
2655
+ // "type": "TRIGGER_MARKET",
2656
+ // "origQty": "5.0",
2657
+ // "price": "9",
2658
+ // "executedQty": "0.0",
2659
+ // "avgPrice": "0",
2660
+ // "cumQuote": "0",
2661
+ // "stopPrice": "5",
2662
+ // "profit": "0.0000",
2663
+ // "commission": "0.000000",
2664
+ // "status": "CANCELLED",
2665
+ // "time": 1667631605000,
2666
+ // "updateTime": 1667631605000
2667
+ // },
2668
+ // ]
2669
+ // }
2670
+ // }
2671
+ //
2672
+ const data = this.safeValue(response, 'data', []);
2673
+ const orders = this.safeValue(data, 'orders', []);
2674
+ return this.parseOrders(orders, market, since, limit);
2675
+ }
2676
+ async fetchClosedOrders(symbol = undefined, since = undefined, limit = undefined, params = {}) {
2677
+ /**
2678
+ * @method
2679
+ * @name bingx#fetchClosedOrders
2680
+ * @description fetches information on multiple closed orders made by the user
2681
+ * @see https://bingx-api.github.io/docs/#/spot/trade-api.html#Query%20Order%20History
2682
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#User's%20Force%20Orders
2683
+ * @see https://bingx-api.github.io/docs/#/standard/contract-interface.html#Historical%20order
2684
+ * @param {string} [symbol] unified market symbol of the market orders were made in
2685
+ * @param {int} [since] the earliest time in ms to fetch orders for
2686
+ * @param {int} [limit] the maximum number of order structures to retrieve
2687
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2688
+ * @param {int} [params.until] the latest time in ms to fetch orders for
2689
+ * @param {boolean} [params.standard] whether to fetch standard contract orders
2690
+ * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/#/?id=order-structure}
2691
+ */
2692
+ if (symbol === undefined) {
2693
+ throw new errors.ArgumentsRequired(this.id + ' fetchClosedOrders() requires a symbol argument');
2694
+ }
2695
+ await this.loadMarkets();
2696
+ const market = this.market(symbol);
2697
+ const request = {
2698
+ 'symbol': market['id'],
2699
+ };
2700
+ let response = undefined;
2701
+ let standard = undefined;
2702
+ [standard, params] = this.handleOptionAndParams(params, 'fetchClosedOrders', 'standard', false);
2703
+ const [marketType, query] = this.handleMarketTypeAndParams('fetchClosedOrders', market, params);
2704
+ if (standard) {
2705
+ response = await this.contractV1PrivateGetAllOrders(this.extend(request, query));
2706
+ }
2707
+ else if (marketType === 'spot') {
2708
+ response = await this.spotV1PrivateGetTradeHistoryOrders(this.extend(request, query));
2709
+ }
2710
+ else {
2711
+ response = await this.swapV2PrivateGetTradeAllOrders(this.extend(request, query));
2712
+ }
2713
+ //
2714
+ // spot
2715
+ //
2716
+ // {
2717
+ // "code": 0,
2718
+ // "msg": "",
2719
+ // "data": {
2720
+ // "orders": [
2721
+ // {
2722
+ // "symbol": "XRP-USDT",
2723
+ // "orderId": 1514073325788200960,
2724
+ // "price": "0.5",
2725
+ // "origQty": "20",
2726
+ // "executedQty": "0",
2727
+ // "cummulativeQuoteQty": "0",
2728
+ // "status": "PENDING",
2729
+ // "type": "LIMIT",
2730
+ // "side": "BUY",
2731
+ // "time": 1649818185647,
2732
+ // "updateTime": 1649818185647,
2733
+ // "origQuoteOrderQty": "0"
2734
+ // }
2735
+ // ]
2736
+ // }
2737
+ // }
2738
+ //
2739
+ // swap
2740
+ //
2741
+ // {
2742
+ // "code": 0,
2743
+ // "msg": "",
2744
+ // "data": {
2745
+ // "orders": [
2746
+ // {
2747
+ // "symbol": "LINK-USDT",
2748
+ // "orderId": 1585839271162413056,
2749
+ // "side": "BUY",
2750
+ // "positionSide": "LONG",
2751
+ // "type": "TRIGGER_MARKET",
2752
+ // "origQty": "5.0",
2753
+ // "price": "9",
2754
+ // "executedQty": "0.0",
2755
+ // "avgPrice": "0",
2756
+ // "cumQuote": "0",
2757
+ // "stopPrice": "5",
2758
+ // "profit": "0.0000",
2759
+ // "commission": "0.000000",
2760
+ // "status": "CANCELLED",
2761
+ // "time": 1667631605000,
2762
+ // "updateTime": 1667631605000
2763
+ // },
2764
+ // ]
2765
+ // }
2766
+ // }
2767
+ //
2768
+ const data = this.safeValue(response, 'data', []);
2769
+ const orders = this.safeValue(data, 'orders', []);
2770
+ return this.parseOrders(orders, market, since, limit);
2771
+ }
2772
+ async transfer(code, amount, fromAccount, toAccount, params = {}) {
2773
+ /**
2774
+ * @method
2775
+ * @name bingx#transfer
2776
+ * @description transfer currency internally between wallets on the same account
2777
+ * @see https://bingx-api.github.io/docs/#/spot/account-api.html#User%20Universal%20Transfer
2778
+ * @param {string} code unified currency code
2779
+ * @param {float} amount amount to transfer
2780
+ * @param {string} fromAccount account to transfer from
2781
+ * @param {string} toAccount account to transfer to
2782
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2783
+ * @returns {object} a [transfer structure]{@link https://docs.ccxt.com/#/?id=transfer-structure}
2784
+ */
2785
+ await this.loadMarkets();
2786
+ const currency = this.currency(code);
2787
+ const accountsByType = this.safeValue(this.options, 'accountsByType', {});
2788
+ const fromId = this.safeString(accountsByType, fromAccount, fromAccount);
2789
+ const toId = this.safeString(accountsByType, toAccount, toAccount);
2790
+ const request = {
2791
+ 'asset': currency['id'],
2792
+ 'amount': this.currencyToPrecision(code, amount),
2793
+ 'type': fromId + '_' + toId,
2794
+ };
2795
+ const response = await this.spotV3PrivateGetGetAssetTransfer(this.extend(request, params));
2796
+ //
2797
+ // {
2798
+ // "tranId":13526853623
2799
+ // }
2800
+ //
2801
+ return {
2802
+ 'info': response,
2803
+ 'id': this.safeString(response, 'tranId'),
2804
+ 'timestamp': undefined,
2805
+ 'datetime': undefined,
2806
+ 'currency': code,
2807
+ 'amount': amount,
2808
+ 'fromAccount': fromAccount,
2809
+ 'toAccount': toAccount,
2810
+ 'status': undefined,
2811
+ };
2812
+ }
2813
+ async fetchTransfers(code = undefined, since = undefined, limit = undefined, params = {}) {
2814
+ /**
2815
+ * @method
2816
+ * @name bingx#fetchTransfers
2817
+ * @description fetch a history of internal transfers made on an account
2818
+ * @see https://bingx-api.github.io/docs/#/spot/account-api.html#Query%20User%20Universal%20Transfer%20History%20(USER_DATA)
2819
+ * @param {string} [code] unified currency code of the currency transferred
2820
+ * @param {int} [since] the earliest time in ms to fetch transfers for
2821
+ * @param {int} [limit] the maximum number of transfers structures to retrieve
2822
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2823
+ * @returns {object[]} a list of [transfer structures]{@link https://docs.ccxt.com/#/?id=transfer-structure}
2824
+ */
2825
+ await this.loadMarkets();
2826
+ let currency = undefined;
2827
+ if (code !== undefined) {
2828
+ currency = this.currency(code);
2829
+ }
2830
+ const accountsByType = this.safeValue(this.options, 'accountsByType', {});
2831
+ const fromAccount = this.safeString(params, 'fromAccount');
2832
+ const toAccount = this.safeString(params, 'toAccount');
2833
+ const fromId = this.safeString(accountsByType, fromAccount, fromAccount);
2834
+ const toId = this.safeString(accountsByType, toAccount, toAccount);
2835
+ if (fromId === undefined || toId === undefined) {
2836
+ throw new errors.ExchangeError(this.id + ' fromAccount & toAccount parameter are required');
2837
+ }
2838
+ const request = {
2839
+ 'type': fromId + '_' + toId,
2840
+ };
2841
+ if (since !== undefined) {
2842
+ request['startTime'] = since;
2843
+ }
2844
+ if (limit !== undefined) {
2845
+ request['size'] = limit;
2846
+ }
2847
+ const response = await this.spotV3PrivateGetAssetTransfer(this.extend(request, params));
2848
+ //
2849
+ // {
2850
+ // "total": 3,
2851
+ // "rows": [
2852
+ // {
2853
+ // "asset":"USDT",
2854
+ // "amount":"-100.00000000000000000000",
2855
+ // "type":"FUND_SFUTURES",
2856
+ // "status":"CONFIRMED",
2857
+ // "tranId":1067594500957016069,
2858
+ // "timestamp":1658388859000
2859
+ // },
2860
+ // ]
2861
+ // }
2862
+ //
2863
+ const rows = this.safeValue(response, 'rows', []);
2864
+ return this.parseTransfers(rows, currency, since, limit);
2865
+ }
2866
+ parseTransfer(transfer, currency = undefined) {
2867
+ const tranId = this.safeString(transfer, 'tranId');
2868
+ const timestamp = this.safeInteger(transfer, 'timestamp');
2869
+ const currencyCode = this.safeCurrencyCode(undefined, currency);
2870
+ const status = this.safeString(transfer, 'status');
2871
+ const accountsById = this.safeValue(this.options, 'accountsById', {});
2872
+ const typeId = this.safeString(transfer, 'type');
2873
+ const typeIdSplit = typeId.split('_');
2874
+ const fromId = this.safeString(typeIdSplit, 0);
2875
+ const toId = this.safeString(typeId, 1);
2876
+ const fromAccount = this.safeString(accountsById, fromId, fromId);
2877
+ const toAccount = this.safeString(accountsById, toId, toId);
2878
+ return {
2879
+ 'info': transfer,
2880
+ 'id': tranId,
2881
+ 'timestamp': timestamp,
2882
+ 'datetime': this.iso8601(timestamp),
2883
+ 'currency': currencyCode,
2884
+ 'amount': this.safeNumber(transfer, 'amount'),
2885
+ 'fromAccount': fromAccount,
2886
+ 'toAccount': toAccount,
2887
+ 'status': status,
2888
+ };
2889
+ }
2890
+ async fetchDepositAddress(code, params = {}) {
2891
+ /**
2892
+ * @method
2893
+ * @name bingx#fetchDepositAddress
2894
+ * @description fetch the deposit address for a currency associated with this account
2895
+ * @see https://bingx-api.github.io/docs/#/common/sub-account#Query%20Main%20Account%20Deposit%20Address
2896
+ * @param {string} code unified currency code
2897
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2898
+ * @returns {object} an [address structure]{@link https://docs.ccxt.com/#/?id=address-structure}
2899
+ */
2900
+ await this.loadMarkets();
2901
+ const currency = this.currency(code);
2902
+ const defaultRecvWindow = this.safeInteger(this.options, 'recvWindow');
2903
+ const recvWindow = this.safeInteger(this.parseParams, 'recvWindow', defaultRecvWindow);
2904
+ const request = {
2905
+ 'coin': currency['id'],
2906
+ 'offset': 0,
2907
+ 'limit': 1000,
2908
+ 'recvWindow': recvWindow,
2909
+ };
2910
+ const response = await this.walletsV1PrivateGetCapitalDepositAddress(this.extend(request, params));
2911
+ //
2912
+ // {
2913
+ // "code": "0",
2914
+ // "timestamp": "1695200226859",
2915
+ // "data": {
2916
+ // "data": [
2917
+ // {
2918
+ // "coinId": "799",
2919
+ // "coin": "USDT",
2920
+ // "network": "BEP20",
2921
+ // "address": "6a7eda2817462dabb6493277a2cfe0f5c3f2550b",
2922
+ // "tag": ''
2923
+ // }
2924
+ // ],
2925
+ // "total": "1"
2926
+ // }
2927
+ // }
2928
+ //
2929
+ const data = this.safeValue(this.safeValue(response, 'data'), 'data');
2930
+ const parsed = this.parseDepositAddresses(data, [currency['code']], false);
2931
+ return this.indexBy(parsed, 'network');
2932
+ }
2933
+ parseDepositAddress(depositAddress, currency = undefined) {
2934
+ //
2935
+ // {
2936
+ // "coinId": "799",
2937
+ // "coin": "USDT",
2938
+ // "network": "BEP20",
2939
+ // "address": "6a7eda2817462dabb6493277a2cfe0f5c3f2550b",
2940
+ // "tag": ''
2941
+ // }
2942
+ //
2943
+ const address = this.safeString(depositAddress, 'address');
2944
+ const tag = this.safeString(depositAddress, 'tag');
2945
+ const currencyId = this.safeString(depositAddress, 'coin');
2946
+ currency = this.safeCurrency(currencyId, currency);
2947
+ const code = currency['code'];
2948
+ const network = this.safeString(depositAddress, 'network');
2949
+ this.checkAddress(address);
2950
+ return {
2951
+ 'currency': code,
2952
+ 'address': address,
2953
+ 'tag': tag,
2954
+ 'network': network,
2955
+ 'info': depositAddress,
2956
+ };
2957
+ }
2958
+ async fetchDeposits(code = undefined, since = undefined, limit = undefined, params = {}) {
2959
+ /**
2960
+ * @method
2961
+ * @name bingx#fetchDeposits
2962
+ * @description fetch all deposits made to an account
2963
+ * @see https://bingx-api.github.io/docs/#/spot/account-api.html#Deposit%20History(supporting%20network)
2964
+ * @param {string} [code] unified currency code
2965
+ * @param {int} [since] the earliest time in ms to fetch deposits for
2966
+ * @param {int} [limit] the maximum number of deposits structures to retrieve
2967
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
2968
+ * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/#/?id=transaction-structure}
2969
+ */
2970
+ await this.loadMarkets();
2971
+ const request = {};
2972
+ let currency = undefined;
2973
+ if (code !== undefined) {
2974
+ currency = this.currency(code);
2975
+ request['coin'] = currency['id'];
2976
+ }
2977
+ if (since !== undefined) {
2978
+ request['startTime'] = since;
2979
+ }
2980
+ if (limit !== undefined) {
2981
+ request['limit'] = limit; // default 1000
2982
+ }
2983
+ const response = await this.spotV3PrivateGetCapitalDepositHisrec(this.extend(request, params));
2984
+ //
2985
+ // [
2986
+ // {
2987
+ // "amount":"0.00999800",
2988
+ // "coin":"PAXG",
2989
+ // "network":"ETH",
2990
+ // "status":1,
2991
+ // "address":"0x788cabe9236ce061e5a892e1a59395a81fc8d62c",
2992
+ // "addressTag":"",
2993
+ // "txId":"0xaad4654a3234aa6118af9b4b335f5ae81c360b2394721c019b5d1e75328b09f3",
2994
+ // "insertTime":1599621997000,
2995
+ // "transferType":0,
2996
+ // "unlockConfirm":"12/12", // confirm times for unlocking
2997
+ // "confirmTimes":"12/12"
2998
+ // },
2999
+ // ]
3000
+ //
3001
+ return this.parseTransactions(response, currency, since, limit);
3002
+ }
3003
+ async fetchWithdrawals(code = undefined, since = undefined, limit = undefined, params = {}) {
3004
+ /**
3005
+ * @method
3006
+ * @name bingx#fetchWithdrawals
3007
+ * @description fetch all withdrawals made from an account
3008
+ * @see https://bingx-api.github.io/docs/#/spot/account-api.html#Withdraw%20History%20(supporting%20network)
3009
+ * @param {string} [code] unified currency code
3010
+ * @param {int} [since] the earliest time in ms to fetch withdrawals for
3011
+ * @param {int} [limit] the maximum number of withdrawals structures to retrieve
3012
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3013
+ * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/#/?id=transaction-structure}
3014
+ */
3015
+ await this.loadMarkets();
3016
+ const request = {};
3017
+ let currency = undefined;
3018
+ if (code !== undefined) {
3019
+ currency = this.currency(code);
3020
+ request['coin'] = currency['id'];
3021
+ }
3022
+ if (since !== undefined) {
3023
+ request['startTime'] = since;
3024
+ }
3025
+ if (limit !== undefined) {
3026
+ request['limit'] = limit; // default 1000
3027
+ }
3028
+ const response = await this.spotV3PrivateGetCapitalWithdrawHistory(this.extend(request, params));
3029
+ //
3030
+ // [
3031
+ // {
3032
+ // "address": "0x94df8b352de7f46f64b01d3666bf6e936e44ce60",
3033
+ // "amount": "8.91000000",
3034
+ // "applyTime": "2019-10-12 11:12:02",
3035
+ // "coin": "USDT",
3036
+ // "id": "b6ae22b3aa844210a7041aee7589627c",
3037
+ // "withdrawOrderId": "WITHDRAWtest123",
3038
+ // "network": "ETH",
3039
+ // "transferType": 0
3040
+ // "status": 6,
3041
+ // "transactionFee": "0.004",
3042
+ // "confirmNo":3,
3043
+ // "info": "The address is not valid. Please confirm with the recipient",
3044
+ // "txId": "0xb5ef8c13b968a406cc62a93a8bd80f9e9a906ef1b3fcf20a2e48573c17659268"
3045
+ // },
3046
+ // ]
3047
+ //
3048
+ return this.parseTransactions(response, currency, since, limit);
3049
+ }
3050
+ parseTransaction(transaction, currency = undefined) {
3051
+ //
3052
+ // fetchDeposits
3053
+ //
3054
+ // {
3055
+ // "amount":"0.00999800",
3056
+ // "coin":"PAXG",
3057
+ // "network":"ETH",
3058
+ // "status":1,
3059
+ // "address":"0x788cabe9236ce061e5a892e1a59395a81fc8d62c",
3060
+ // "addressTag":"",
3061
+ // "txId":"0xaad4654a3234aa6118af9b4b335f5ae81c360b2394721c019b5d1e75328b09f3",
3062
+ // "insertTime":1599621997000,
3063
+ // "transferType":0,
3064
+ // "unlockConfirm":"12/12", // confirm times for unlocking
3065
+ // "confirmTimes":"12/12"
3066
+ // }
3067
+ //
3068
+ // fetchWithdrawals
3069
+ //
3070
+ // {
3071
+ // "address": "0x94df8b352de7f46f64b01d3666bf6e936e44ce60",
3072
+ // "amount": "8.91000000",
3073
+ // "applyTime": "2019-10-12 11:12:02",
3074
+ // "coin": "USDT",
3075
+ // "id": "b6ae22b3aa844210a7041aee7589627c",
3076
+ // "withdrawOrderId": "WITHDRAWtest123",
3077
+ // "network": "ETH",
3078
+ // "transferType": 0
3079
+ // "status": 6,
3080
+ // "transactionFee": "0.004",
3081
+ // "confirmNo":3,
3082
+ // "info": "The address is not valid. Please confirm with the recipient",
3083
+ // "txId": "0xb5ef8c13b968a406cc62a93a8bd80f9e9a906ef1b3fcf20a2e48573c17659268"
3084
+ // }
3085
+ //
3086
+ const address = this.safeString(transaction, 'address');
3087
+ const tag = this.safeString(transaction, 'addressTag');
3088
+ let timestamp = this.safeInteger(transaction, 'insertTime');
3089
+ let datetime = this.iso8601(timestamp);
3090
+ if (timestamp === undefined) {
3091
+ datetime = this.safeString(transaction, 'applyTime');
3092
+ timestamp = this.parse8601(datetime);
3093
+ }
3094
+ const network = this.safeString(transaction, 'network');
3095
+ const currencyId = this.safeString(transaction, 'coin');
3096
+ let code = this.safeCurrencyCode(currencyId, currency);
3097
+ if ((code !== undefined) && (code !== network) && code.indexOf(network) >= 0) {
3098
+ if (network !== undefined) {
3099
+ code = code.replace(network, '');
3100
+ }
3101
+ }
3102
+ const rawType = this.safeString(transaction, 'transferType');
3103
+ const type = (rawType === '0') ? 'deposit' : 'withdrawal';
3104
+ return {
3105
+ 'info': transaction,
3106
+ 'id': this.safeString(transaction, 'id'),
3107
+ 'txid': this.safeString(transaction, 'txId'),
3108
+ 'type': type,
3109
+ 'currency': code,
3110
+ 'network': this.networkIdToCode(network),
3111
+ 'amount': this.safeNumber(transaction, 'amount'),
3112
+ 'status': this.parseTransactionStatus(this.safeString(transaction, 'status')),
3113
+ 'timestamp': timestamp,
3114
+ 'datetime': datetime,
3115
+ 'address': address,
3116
+ 'addressFrom': undefined,
3117
+ 'addressTo': address,
3118
+ 'tag': tag,
3119
+ 'tagFrom': tag,
3120
+ 'tagTo': undefined,
3121
+ 'updated': undefined,
3122
+ 'comment': this.safeString(transaction, 'info'),
3123
+ 'fee': {
3124
+ 'currency': code,
3125
+ 'cost': this.safeNumber(transaction, 'transactionFee'),
3126
+ 'rate': undefined,
3127
+ },
3128
+ 'internal': undefined,
3129
+ };
3130
+ }
3131
+ parseTransactionStatus(status) {
3132
+ const statuses = {
3133
+ '0': 'pending',
3134
+ '1': 'ok',
3135
+ '10': 'pending',
3136
+ '20': 'rejected',
3137
+ '30': 'ok',
3138
+ '40': 'rejected',
3139
+ '50': 'ok',
3140
+ '60': 'pending',
3141
+ '70': 'rejected',
3142
+ '2': 'pending',
3143
+ '3': 'rejected',
3144
+ '4': 'pending',
3145
+ '5': 'rejected',
3146
+ '6': 'ok',
3147
+ };
3148
+ return this.safeString(statuses, status, status);
3149
+ }
3150
+ async setMarginMode(marginMode, symbol = undefined, params = {}) {
3151
+ /**
3152
+ * @method
3153
+ * @name bingx#setMarginMode
3154
+ * @description set margin mode to 'cross' or 'isolated'
3155
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Switch%20Margin%20Mode
3156
+ * @param {string} marginMode 'cross' or 'isolated'
3157
+ * @param {string} symbol unified market symbol
3158
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3159
+ * @returns {object} response from the exchange
3160
+ */
3161
+ if (symbol === undefined) {
3162
+ throw new errors.ArgumentsRequired(this.id + ' setMarginMode() requires a symbol argument');
3163
+ }
3164
+ await this.loadMarkets();
3165
+ const market = this.market(symbol);
3166
+ if (market['type'] !== 'swap') {
3167
+ throw new errors.BadSymbol(this.id + ' setMarginMode() supports swap contracts only');
3168
+ }
3169
+ marginMode = marginMode.toUpperCase();
3170
+ if (marginMode === 'CROSS') {
3171
+ marginMode = 'CROSSED';
3172
+ }
3173
+ if (marginMode !== 'ISOLATED' && marginMode !== 'CROSSED') {
3174
+ throw new errors.BadRequest(this.id + ' setMarginMode() marginMode argument should be isolated or cross');
3175
+ }
3176
+ const request = {
3177
+ 'symbol': market['id'],
3178
+ 'marginType': marginMode,
3179
+ };
3180
+ return await this.swapV2PrivatePostTradeMarginType(this.extend(request, params));
3181
+ }
3182
+ async setMargin(symbol, amount, params = {}) {
3183
+ /**
3184
+ * @method
3185
+ * @name bingx#setMargin
3186
+ * @description Either adds or reduces margin in an isolated position in order to set the margin to a specific value
3187
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Adjust%20isolated%20margin
3188
+ * @param {string} symbol unified market symbol of the market to set margin in
3189
+ * @param {float} amount the amount to set the margin to
3190
+ * @param {object} [params] parameters specific to the bingx api endpoint
3191
+ * @returns {object} A [margin structure]{@link https://docs.ccxt.com/#/?id=add-margin-structure}
3192
+ */
3193
+ const type = this.safeInteger(params, 'type'); // 1 increase margin 2 decrease margin
3194
+ if (type === undefined) {
3195
+ throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a type parameter either 1 (increase margin) or 2 (decrease margin)');
3196
+ }
3197
+ if (!this.inArray(type, [1, 2])) {
3198
+ throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a type parameter either 1 (increase margin) or 2 (decrease margin)');
3199
+ }
3200
+ await this.loadMarkets();
3201
+ const market = this.market(symbol);
3202
+ const request = {
3203
+ 'symbol': market['id'],
3204
+ 'amount': this.amountToPrecision(market['symbol'], amount),
3205
+ 'type': type,
3206
+ };
3207
+ const response = await this.swapV2PrivatePostTradePositionMargin(this.extend(request, params));
3208
+ //
3209
+ // {
3210
+ // "code": 0,
3211
+ // "msg": "",
3212
+ // "amount": 1,
3213
+ // "type": 1
3214
+ // }
3215
+ //
3216
+ return response;
3217
+ }
3218
+ async fetchLeverage(symbol, params = {}) {
3219
+ /**
3220
+ * @method
3221
+ * @name bingx#fetchLeverage
3222
+ * @description fetch the set leverage for a market
3223
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Query%20Leverage
3224
+ * @param {string} symbol unified market symbol
3225
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3226
+ * @returns {object} a [leverage structure]{@link https://docs.ccxt.com/#/?id=leverage-structure}
3227
+ */
3228
+ await this.loadMarkets();
3229
+ const market = this.market(symbol);
3230
+ const request = {
3231
+ 'symbol': market['id'],
3232
+ };
3233
+ const response = await this.swapV2PrivateGetTradeLeverage(this.extend(request, params));
3234
+ //
3235
+ // {
3236
+ // "code": 0,
3237
+ // "msg": "",
3238
+ // "data": {
3239
+ // "longLeverage": 6,
3240
+ // "shortLeverage": 6
3241
+ // }
3242
+ // }
3243
+ //
3244
+ return response;
3245
+ }
3246
+ async setLeverage(leverage, symbol = undefined, params = {}) {
3247
+ /**
3248
+ * @method
3249
+ * @name bingx#setLeverage
3250
+ * @description set the level of leverage for a market
3251
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Switch%20Leverage
3252
+ * @param {float} leverage the rate of leverage
3253
+ * @param {string} symbol unified market symbol
3254
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3255
+ * @returns {object} response from the exchange
3256
+ */
3257
+ if (symbol === undefined) {
3258
+ throw new errors.ArgumentsRequired(this.id + ' setLeverage() requires a symbol argument');
3259
+ }
3260
+ const side = this.safeStringUpper(params, 'side');
3261
+ this.checkRequiredArgument('setLeverage', side, 'side', ['LONG', 'SHORT']);
3262
+ await this.loadMarkets();
3263
+ const market = this.market(symbol);
3264
+ const request = {
3265
+ 'symbol': market['id'],
3266
+ 'side': side,
3267
+ 'leverage': leverage,
3268
+ };
3269
+ //
3270
+ // {
3271
+ // "code": 0,
3272
+ // "msg": "",
3273
+ // "data": {
3274
+ // "leverage": 6,
3275
+ // "symbol": "BTC-USDT"
3276
+ // }
3277
+ // }
3278
+ //
3279
+ return await this.swapV2PrivatePostTradeLeverage(this.extend(request, params));
3280
+ }
3281
+ async fetchMyTrades(symbol = undefined, since = undefined, limit = undefined, params = {}) {
3282
+ /**
3283
+ * @method
3284
+ * @name bingx#fetchMyTrades
3285
+ * @description fetch all trades made by the user
3286
+ * @see https://bingx-api.github.io/docs/#/en-us/spot/trade-api.html#Query%20Order%20History
3287
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#Query%20historical%20transaction%20orders
3288
+ * @param {string} [symbol] unified market symbol
3289
+ * @param {int} [since] the earliest time in ms to fetch trades for
3290
+ * @param {int} [limit] the maximum number of trades structures to retrieve
3291
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3292
+ * @param {int} [params.until] timestamp in ms for the ending date filter, default is undefined
3293
+ * @param {string} params.trandingUnit COIN (directly represent assets such as BTC and ETH) or CONT (represents the number of contract sheets)
3294
+ * @returns {object[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=trade-structure}
3295
+ */
3296
+ if (symbol === undefined) {
3297
+ throw new errors.ArgumentsRequired(this.id + ' fetchMyTrades() requires a symbol argument');
3298
+ }
3299
+ await this.loadMarkets();
3300
+ const market = this.market(symbol);
3301
+ const now = this.milliseconds();
3302
+ let response = undefined;
3303
+ const request = {
3304
+ 'symbol': market['id'],
3305
+ };
3306
+ if (since !== undefined) {
3307
+ const startTimeReq = market['spot'] ? 'startTime' : 'startTs';
3308
+ request[startTimeReq] = since;
3309
+ }
3310
+ else if (market['swap']) {
3311
+ request['startTs'] = now - 7776000000; // 90 days
3312
+ }
3313
+ const until = this.safeInteger(params, 'until');
3314
+ params = this.omit(params, 'until');
3315
+ if (until !== undefined) {
3316
+ const endTimeReq = market['spot'] ? 'endTime' : 'endTs';
3317
+ request[endTimeReq] = until;
3318
+ }
3319
+ else if (market['swap']) {
3320
+ request['endTs'] = now;
3321
+ }
3322
+ let fills = undefined;
3323
+ if (market['spot']) {
3324
+ response = await this.spotV1PrivateGetTradeMyTrades(this.extend(request, params));
3325
+ const data = this.safeValue(response, 'data', []);
3326
+ fills = this.safeValue(data, 'fills', []);
3327
+ //
3328
+ // {
3329
+ // "code": 0,
3330
+ // "msg": "",
3331
+ // "debugMsg": "",
3332
+ // "data": {
3333
+ // "fills": [
3334
+ // {
3335
+ // "symbol": "LTC-USDT",
3336
+ // "id": 36237072,
3337
+ // "orderId": 1674069326895775744,
3338
+ // "price": "85.891",
3339
+ // "qty": "0.0582",
3340
+ // "quoteQty": "4.9988562000000005",
3341
+ // "commission": -0.00005820000000000001,
3342
+ // "commissionAsset": "LTC",
3343
+ // "time": 1687964205000,
3344
+ // "isBuyer": true,
3345
+ // "isMaker": false
3346
+ // }
3347
+ // ]
3348
+ // }
3349
+ // }
3350
+ //
3351
+ }
3352
+ else {
3353
+ const tradingUnit = this.safeStringUpper(params, 'tradingUnit', 'CONT');
3354
+ params = this.omit(params, 'tradingUnit');
3355
+ request['tradingUnit'] = tradingUnit;
3356
+ response = await this.swapV2PrivateGetTradeAllFillOrders(this.extend(request, params));
3357
+ const data = this.safeValue(response, 'data', []);
3358
+ fills = this.safeValue(data, 'fill_orders', []);
3359
+ //
3360
+ // {
3361
+ // "code": "0",
3362
+ // "msg": '',
3363
+ // "data": { fill_orders: [
3364
+ // {
3365
+ // "volume": "0.1",
3366
+ // "price": "106.75",
3367
+ // "amount": "10.6750",
3368
+ // "commission": "-0.0053",
3369
+ // "currency": "USDT",
3370
+ // "orderId": "1676213270274379776",
3371
+ // "liquidatedPrice": "0.00",
3372
+ // "liquidatedMarginRatio": "0.00",
3373
+ // "filledTime": "2023-07-04T20:56:01.000+0800"
3374
+ // }
3375
+ // ]
3376
+ // }
3377
+ // }
3378
+ //
3379
+ }
3380
+ return this.parseTrades(fills, market, since, limit, params);
3381
+ }
3382
+ parseDepositWithdrawFee(fee, currency = undefined) {
3383
+ //
3384
+ // {
3385
+ // "coin": "BTC",
3386
+ // "name": "BTC",
3387
+ // "networkList": [
3388
+ // {
3389
+ // "name": "BTC",
3390
+ // "network": "BTC",
3391
+ // "isDefault": true,
3392
+ // "minConfirm": "2",
3393
+ // "withdrawEnable": true,
3394
+ // "withdrawFee": "0.00035",
3395
+ // "withdrawMax": "1.62842",
3396
+ // "withdrawMin": "0.0005"
3397
+ // },
3398
+ // {
3399
+ // "name": "BTC",
3400
+ // "network": "BEP20",
3401
+ // "isDefault": false,
3402
+ // "minConfirm": "15",
3403
+ // "withdrawEnable": true,
3404
+ // "withdrawFee": "0.00001",
3405
+ // "withdrawMax": "1.62734",
3406
+ // "withdrawMin": "0.0001"
3407
+ // }
3408
+ // ]
3409
+ // }
3410
+ //
3411
+ const networkList = this.safeValue(fee, 'networkList', []);
3412
+ const networkListLength = networkList.length;
3413
+ const result = {
3414
+ 'info': fee,
3415
+ 'withdraw': {
3416
+ 'fee': undefined,
3417
+ 'percentage': undefined,
3418
+ },
3419
+ 'deposit': {
3420
+ 'fee': undefined,
3421
+ 'percentage': undefined,
3422
+ },
3423
+ 'networks': {},
3424
+ };
3425
+ if (networkListLength !== 0) {
3426
+ for (let i = 0; i < networkListLength; i++) {
3427
+ const network = networkList[i];
3428
+ const networkId = this.safeString(network, 'network');
3429
+ const isDefault = this.safeValue(network, 'isDefault');
3430
+ const currencyCode = this.safeString(currency, 'code');
3431
+ const networkCode = this.networkIdToCode(networkId, currencyCode);
3432
+ result['networks'][networkCode] = {
3433
+ 'deposit': { 'fee': undefined, 'percentage': undefined },
3434
+ 'withdraw': { 'fee': this.safeNumber(network, 'withdrawFee'), 'percentage': false },
3435
+ };
3436
+ if (isDefault) {
3437
+ result['withdraw']['fee'] = this.safeNumber(network, 'withdrawFee');
3438
+ result['withdraw']['percentage'] = false;
3439
+ }
3440
+ }
3441
+ }
3442
+ return result;
3443
+ }
3444
+ async fetchDepositWithdrawFees(codes = undefined, params = {}) {
3445
+ /**
3446
+ * @method
3447
+ * @name bingx#fetchDepositWithdrawFees
3448
+ * @description fetch deposit and withdraw fees
3449
+ * @see https://bingx-api.github.io/docs/#/common/account-api.html#All%20Coins'%20Information
3450
+ * @param {string[]|undefined} codes list of unified currency codes
3451
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3452
+ * @returns {object} a list of [fee structures]{@link https://docs.ccxt.com/#/?id=fee-structure}
3453
+ */
3454
+ await this.loadMarkets();
3455
+ const response = await this.walletsV1PrivateGetCapitalConfigGetall(params);
3456
+ const coins = this.safeValue(response, 'data');
3457
+ return this.parseDepositWithdrawFees(coins, codes, 'coin');
3458
+ }
3459
+ async withdraw(code, amount, address, tag = undefined, params = {}) {
3460
+ /**
3461
+ * @method
3462
+ * @name bingx#withdraw
3463
+ * @description make a withdrawal
3464
+ * @see https://bingx-api.github.io/docs/#/common/account-api.html#Withdraw
3465
+ * @param {string} code unified currency code
3466
+ * @param {float} amount the amount to withdraw
3467
+ * @param {string} address the address to withdraw to
3468
+ * @param {string} [tag]
3469
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3470
+ * @param {int} [params.walletType] 1 fund account, 2 standard account, 3 perpetual account
3471
+ * @returns {object} a [transaction structure]{@link https://docs.ccxt.com/#/?id=transaction-structure}
3472
+ */
3473
+ await this.loadMarkets();
3474
+ const currency = this.currency(code);
3475
+ let walletType = this.safeInteger(params, 'walletType');
3476
+ if (walletType === undefined) {
3477
+ walletType = 1;
3478
+ }
3479
+ if (!this.inArray(walletType, [1, 2, 3])) {
3480
+ throw new errors.BadRequest(this.id + ' withdraw() requires either 1 fund account, 2 standard futures account, 3 perpetual account for walletType');
3481
+ }
3482
+ const request = {
3483
+ 'coin': currency['id'],
3484
+ 'address': address,
3485
+ 'amount': this.numberToString(amount),
3486
+ 'walletType': walletType,
3487
+ };
3488
+ const network = this.safeStringUpper(params, 'network');
3489
+ if (network !== undefined) {
3490
+ request['network'] = this.networkCodeToId(network);
3491
+ }
3492
+ params = this.omit(params, ['walletType', 'network']);
3493
+ const response = await this.walletsV1PrivatePostCapitalWithdrawApply(this.extend(request, params));
3494
+ const data = this.safeValue(response, 'data');
3495
+ // {
3496
+ // "code":0,
3497
+ // "timestamp":1689258953651,
3498
+ // "data":{
3499
+ // "id":"1197073063359000577"
3500
+ // }
3501
+ // }
3502
+ this.parseTransaction(data);
3503
+ }
3504
+ parseParams(params) {
3505
+ const sortedParams = this.keysort(params);
3506
+ const keys = Object.keys(sortedParams);
3507
+ for (let i = 0; i < keys.length; i++) {
3508
+ const key = keys[i];
3509
+ const value = sortedParams[key];
3510
+ if (Array.isArray(value)) {
3511
+ let arrStr = '[';
3512
+ for (let j = 0; j < value.length; j++) {
3513
+ const arrayElement = value[j];
3514
+ if (j > 0) {
3515
+ arrStr += ',';
3516
+ }
3517
+ arrStr += arrayElement.toString();
3518
+ }
3519
+ arrStr += ']';
3520
+ sortedParams[key] = arrStr;
3521
+ }
3522
+ }
3523
+ return sortedParams;
3524
+ }
3525
+ async fetchMyLiquidations(symbol = undefined, since = undefined, limit = undefined, params = {}) {
3526
+ /**
3527
+ * @method
3528
+ * @name bingx#fetchMyLiquidations
3529
+ * @description retrieves the users liquidated positions
3530
+ * @see https://bingx-api.github.io/docs/#/swapV2/trade-api.html#User's%20Force%20Orders
3531
+ * @param {string} [symbol] unified CCXT market symbol
3532
+ * @param {int} [since] the earliest time in ms to fetch liquidations for
3533
+ * @param {int} [limit] the maximum number of liquidation structures to retrieve
3534
+ * @param {object} [params] exchange specific parameters for the bingx api endpoint
3535
+ * @param {int} [params.until] timestamp in ms of the latest liquidation
3536
+ * @returns {object} an array of [liquidation structures]{@link https://docs.ccxt.com/#/?id=liquidation-structure}
3537
+ */
3538
+ await this.loadMarkets();
3539
+ let request = {
3540
+ 'autoCloseType': 'LIQUIDATION',
3541
+ };
3542
+ [request, params] = this.handleUntilOption('endTime', request, params);
3543
+ let market = undefined;
3544
+ if (symbol !== undefined) {
3545
+ market = this.market(symbol);
3546
+ request['symbol'] = symbol;
3547
+ }
3548
+ if (since !== undefined) {
3549
+ request['startTime'] = since;
3550
+ }
3551
+ if (limit !== undefined) {
3552
+ request['limit'] = limit;
3553
+ }
3554
+ const response = await this.swapV2PrivateGetTradeForceOrders(this.extend(request, params));
3555
+ //
3556
+ // {
3557
+ // "code": 0,
3558
+ // "msg": "",
3559
+ // "data": {
3560
+ // "orders": [
3561
+ // {
3562
+ // "time": "int64",
3563
+ // "symbol": "string",
3564
+ // "side": "string",
3565
+ // "type": "string",
3566
+ // "positionSide": "string",
3567
+ // "cumQuote": "string",
3568
+ // "status": "string",
3569
+ // "stopPrice": "string",
3570
+ // "price": "string",
3571
+ // "origQty": "string",
3572
+ // "avgPrice": "string",
3573
+ // "executedQty": "string",
3574
+ // "orderId": "int64",
3575
+ // "profit": "string",
3576
+ // "commission": "string",
3577
+ // "workingType": "string",
3578
+ // "updateTime": "int64"
3579
+ // },
3580
+ // ]
3581
+ // }
3582
+ // }
3583
+ //
3584
+ const data = this.safeValue(response, 'data', {});
3585
+ const liquidations = this.safeValue(data, 'orders', []);
3586
+ return this.parseLiquidations(liquidations, market, since, limit);
3587
+ }
3588
+ parseLiquidation(liquidation, market = undefined) {
3589
+ //
3590
+ // {
3591
+ // "time": "int64",
3592
+ // "symbol": "string",
3593
+ // "side": "string",
3594
+ // "type": "string",
3595
+ // "positionSide": "string",
3596
+ // "cumQuote": "string",
3597
+ // "status": "string",
3598
+ // "stopPrice": "string",
3599
+ // "price": "string",
3600
+ // "origQty": "string",
3601
+ // "avgPrice": "string",
3602
+ // "executedQty": "string",
3603
+ // "orderId": "int64",
3604
+ // "profit": "string",
3605
+ // "commission": "string",
3606
+ // "workingType": "string",
3607
+ // "updateTime": "int64"
3608
+ // }
3609
+ //
3610
+ const marketId = this.safeString(liquidation, 'symbol');
3611
+ const timestamp = this.safeInteger(liquidation, 'time');
3612
+ const contractsString = this.safeString(liquidation, 'executedQty');
3613
+ const contractSizeString = this.safeString(market, 'contractSize');
3614
+ const priceString = this.safeString(liquidation, 'avgPrice');
3615
+ const baseValueString = Precise["default"].stringMul(contractsString, contractSizeString);
3616
+ const quoteValueString = Precise["default"].stringMul(baseValueString, priceString);
3617
+ return this.safeLiquidation({
3618
+ 'info': liquidation,
3619
+ 'symbol': this.safeSymbol(marketId, market),
3620
+ 'contracts': this.parseNumber(contractsString),
3621
+ 'contractSize': this.parseNumber(contractSizeString),
3622
+ 'price': this.parseNumber(priceString),
3623
+ 'baseValue': this.parseNumber(baseValueString),
3624
+ 'quoteValue': this.parseNumber(quoteValueString),
3625
+ 'timestamp': timestamp,
3626
+ 'datetime': this.iso8601(timestamp),
3627
+ });
3628
+ }
3629
+ async closePosition(symbol, side = undefined, params = {}) {
3630
+ /**
3631
+ * @method
3632
+ * @name bingx#closePosition
3633
+ * @description closes open positions for a market
3634
+ * @see https://bingx-api.github.io/docs/#/en-us/swapV2/trade-api.html#One-Click%20Close%20All%20Positions
3635
+ * @param {string} symbol Unified CCXT market symbol
3636
+ * @param {string} [side] not used by bingx
3637
+ * @param {object} [params] extra parameters specific to the bingx api endpoint
3638
+ * @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
3639
+ */
3640
+ await this.loadMarkets();
3641
+ const market = this.market(symbol);
3642
+ const request = {
3643
+ 'symbol': market['id'],
3644
+ };
3645
+ const response = await this.swapV2PrivatePostTradeCloseAllPositions(this.extend(request, params));
3646
+ //
3647
+ // {
3648
+ // "code": 0,
3649
+ // "msg": "",
3650
+ // "data": {
3651
+ // "success": [
3652
+ // 1727686766700486656,
3653
+ // ],
3654
+ // "failed": null
3655
+ // }
3656
+ // }
3657
+ //
3658
+ const data = this.safeValue(response, 'data');
3659
+ return this.parseOrder(data);
3660
+ }
3661
+ async closeAllPositions(params = {}) {
3662
+ /**
3663
+ * @method
3664
+ * @name bitget#closePositions
3665
+ * @description closes open positions for a market
3666
+ * @see https://bingx-api.github.io/docs/#/en-us/swapV2/trade-api.html#One-Click%20Close%20All%20Positions
3667
+ * @param {object} [params] extra parameters specific to the okx api endpoint
3668
+ * @param {string} [params.recvWindow] request valid time window value
3669
+ * @returns {object[]} [A list of position structures]{@link https://docs.ccxt.com/#/?id=position-structure}
3670
+ */
3671
+ await this.loadMarkets();
3672
+ const defaultRecvWindow = this.safeInteger(this.options, 'recvWindow');
3673
+ const recvWindow = this.safeInteger(this.parseParams, 'recvWindow', defaultRecvWindow);
3674
+ let marketType = undefined;
3675
+ [marketType, params] = this.handleMarketTypeAndParams('closeAllPositions', undefined, params);
3676
+ if (marketType === 'margin') {
3677
+ throw new errors.BadRequest(this.id + ' closePositions () cannot be used for ' + marketType + ' markets');
3678
+ }
3679
+ const request = {
3680
+ 'recvWindow': recvWindow,
3681
+ };
3682
+ const response = await this.swapV2PrivatePostTradeCloseAllPositions(this.extend(request, params));
3683
+ //
3684
+ // {
3685
+ // "code": 0,
3686
+ // "msg": "",
3687
+ // "data": {
3688
+ // "success": [
3689
+ // 1727686766700486656,
3690
+ // 1727686767048613888
3691
+ // ],
3692
+ // "failed": null
3693
+ // }
3694
+ // }
3695
+ //
3696
+ const data = this.safeValue(response, 'data', {});
3697
+ const success = this.safeValue(data, 'success', []);
3698
+ const positions = [];
3699
+ for (let i = 0; i < success.length; i++) {
3700
+ const position = this.parsePosition({ 'positionId': success[i] });
3701
+ positions.push(position);
3702
+ }
3703
+ return positions;
3704
+ }
3705
+ async setPositionMode(hedged, symbol = undefined, params = {}) {
3706
+ /**
3707
+ * @method
3708
+ * @name bingx#setPositionMode
3709
+ * @description set hedged to true or false for a market
3710
+ * @see https://bingx-api.github.io/docs/#/en-us/swapV2/trade-api.html#Set%20Position%20Mode
3711
+ * @param {bool} hedged set to true to use dualSidePosition
3712
+ * @param {string} symbol not used by bingx setPositionMode ()
3713
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
3714
+ * @returns {object} response from the exchange
3715
+ */
3716
+ let dualSidePosition = undefined;
3717
+ if (hedged) {
3718
+ dualSidePosition = 'true';
3719
+ }
3720
+ else {
3721
+ dualSidePosition = 'false';
3722
+ }
3723
+ const request = {
3724
+ 'dualSidePosition': dualSidePosition,
3725
+ };
3726
+ //
3727
+ // {
3728
+ // code: '0',
3729
+ // msg: '',
3730
+ // timeStamp: '1703327432734',
3731
+ // data: { dualSidePosition: 'false' }
3732
+ // }
3733
+ //
3734
+ return await this.swapV1PrivatePostPositionSideDual(this.extend(request, params));
3735
+ }
3736
+ sign(path, section = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
3737
+ const type = section[0];
3738
+ const version = section[1];
3739
+ const access = section[2];
3740
+ let url = this.implodeHostname(this.urls['api'][type]);
3741
+ if (type === 'spot' && version === 'v3') {
3742
+ url += '/api';
3743
+ }
3744
+ else {
3745
+ url += '/' + type;
3746
+ }
3747
+ url += '/' + version + '/';
3748
+ path = this.implodeParams(path, params);
3749
+ url += path;
3750
+ params = this.omit(params, this.extractParams(path));
3751
+ params = this.keysort(params);
3752
+ if (access === 'public') {
3753
+ params['timestamp'] = this.nonce();
3754
+ if (Object.keys(params).length) {
3755
+ url += '?' + this.urlencode(params);
3756
+ }
3757
+ }
3758
+ else if (access === 'private') {
3759
+ this.checkRequiredCredentials();
3760
+ params['timestamp'] = this.nonce();
3761
+ const parsedParams = this.parseParams(params);
3762
+ let query = this.urlencode(parsedParams);
3763
+ const signature = this.hmac(this.encode(this.rawencode(parsedParams)), this.encode(this.secret), sha256.sha256);
3764
+ if (Object.keys(params).length) {
3765
+ query = '?' + query + '&';
3766
+ }
3767
+ else {
3768
+ query += '?';
3769
+ }
3770
+ query += 'signature=' + signature;
3771
+ headers = {
3772
+ 'X-BX-APIKEY': this.apiKey,
3773
+ 'X-SOURCE-KEY': this.safeString(this.options, 'broker', 'CCXT'),
3774
+ };
3775
+ url += query;
3776
+ }
3777
+ return { 'url': url, 'method': method, 'body': body, 'headers': headers };
3778
+ }
3779
+ nonce() {
3780
+ return this.milliseconds();
3781
+ }
3782
+ handleErrors(httpCode, reason, url, method, headers, body, response, requestHeaders, requestBody) {
3783
+ if (response === undefined) {
3784
+ return undefined; // fallback to default error handler
3785
+ }
3786
+ //
3787
+ // {
3788
+ // "code": 80014,
3789
+ // "msg": "Invalid parameters, err:Key: 'GetTickerRequest.Symbol' Error:Field validation for "Symbol" failed on the "len=0|endswith=-USDT" tag",
3790
+ // "data": {
3791
+ // }
3792
+ // }
3793
+ //
3794
+ const code = this.safeString(response, 'code');
3795
+ const message = this.safeString(response, 'msg');
3796
+ if (code !== undefined && code !== '0') {
3797
+ const feedback = this.id + ' ' + body;
3798
+ this.throwExactlyMatchedException(this.exceptions['exact'], message, feedback);
3799
+ this.throwExactlyMatchedException(this.exceptions['exact'], code, feedback);
3800
+ this.throwBroadlyMatchedException(this.exceptions['broad'], message, feedback);
3801
+ throw new errors.ExchangeError(feedback); // unknown message
3802
+ }
3803
+ return undefined;
3804
+ }
3805
+ }
3806
+
3807
+ module.exports = bingx;