ccxt 4.5.48 → 4.5.50

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 (60) hide show
  1. package/README.md +7 -5
  2. package/dist/ccxt.browser.min.js +10 -10
  3. package/dist/cjs/ccxt.js +6 -1
  4. package/dist/cjs/src/abstract/weex.js +11 -0
  5. package/dist/cjs/src/base/Exchange.js +48 -13
  6. package/dist/cjs/src/bitget.js +1 -0
  7. package/dist/cjs/src/btcbox.js +1 -1
  8. package/dist/cjs/src/bullish.js +2 -1
  9. package/dist/cjs/src/gate.js +0 -1
  10. package/dist/cjs/src/hibachi.js +1 -1
  11. package/dist/cjs/src/kraken.js +1 -0
  12. package/dist/cjs/src/krakenfutures.js +10 -1
  13. package/dist/cjs/src/kucoin.js +5 -1
  14. package/dist/cjs/src/lighter.js +312 -144
  15. package/dist/cjs/src/mexc.js +7 -12
  16. package/dist/cjs/src/okx.js +46 -1
  17. package/dist/cjs/src/paradex.js +5 -2
  18. package/dist/cjs/src/pro/binance.js +1 -1
  19. package/dist/cjs/src/pro/cex.js +1 -1
  20. package/dist/cjs/src/pro/coinbase.js +1 -1
  21. package/dist/cjs/src/pro/hyperliquid.js +361 -1
  22. package/dist/cjs/src/pro/lighter.js +339 -18
  23. package/dist/cjs/src/pro/weex.js +1906 -0
  24. package/dist/cjs/src/weex.js +3823 -0
  25. package/js/ccxt.d.ts +8 -2
  26. package/js/ccxt.js +6 -2
  27. package/js/src/abstract/myokx.d.ts +34 -0
  28. package/js/src/abstract/okx.d.ts +34 -0
  29. package/js/src/abstract/okxus.d.ts +34 -0
  30. package/js/src/abstract/weex.d.ts +83 -0
  31. package/js/src/abstract/weex.js +11 -0
  32. package/js/src/base/Exchange.d.ts +5 -1
  33. package/js/src/base/Exchange.js +48 -13
  34. package/js/src/bitget.js +1 -0
  35. package/js/src/btcbox.js +1 -1
  36. package/js/src/bullish.js +2 -1
  37. package/js/src/gate.js +0 -1
  38. package/js/src/hibachi.js +1 -1
  39. package/js/src/kraken.js +1 -0
  40. package/js/src/krakenfutures.js +10 -1
  41. package/js/src/kucoin.d.ts +4 -0
  42. package/js/src/kucoin.js +5 -1
  43. package/js/src/lighter.d.ts +12 -2
  44. package/js/src/lighter.js +313 -145
  45. package/js/src/mexc.d.ts +2 -0
  46. package/js/src/mexc.js +7 -12
  47. package/js/src/okx.js +46 -1
  48. package/js/src/paradex.js +5 -2
  49. package/js/src/pro/binance.js +1 -1
  50. package/js/src/pro/cex.js +1 -1
  51. package/js/src/pro/coinbase.js +1 -1
  52. package/js/src/pro/hyperliquid.d.ts +48 -1
  53. package/js/src/pro/hyperliquid.js +362 -2
  54. package/js/src/pro/lighter.d.ts +37 -2
  55. package/js/src/pro/lighter.js +339 -18
  56. package/js/src/pro/weex.d.ts +330 -0
  57. package/js/src/pro/weex.js +1905 -0
  58. package/js/src/weex.d.ts +675 -0
  59. package/js/src/weex.js +3822 -0
  60. package/package.json +1 -1
@@ -6,6 +6,9 @@ var lighter$1 = require('./abstract/lighter.js');
6
6
  var errors = require('./base/errors.js');
7
7
  var number = require('./base/functions/number.js');
8
8
  var Precise = require('./base/Precise.js');
9
+ var crypto = require('./base/functions/crypto.js');
10
+ var sha3 = require('./static_dependencies/noble-hashes/sha3.js');
11
+ var secp256k1 = require('./static_dependencies/noble-curves/secp256k1.js');
9
12
 
10
13
  // ----------------------------------------------------------------------------
11
14
  // ---------------------------------------------------------------------------
@@ -24,6 +27,7 @@ class lighter extends lighter$1["default"] {
24
27
  'certified': false,
25
28
  'pro': true,
26
29
  'dex': true,
30
+ 'quoteJsonNumbers': false,
27
31
  'has': {
28
32
  'CORS': undefined,
29
33
  'spot': false,
@@ -246,6 +250,7 @@ class lighter extends lighter$1["default"] {
246
250
  'httpExceptions': {},
247
251
  'exceptions': {
248
252
  'exact': {
253
+ '21146': errors.ExchangeError,
249
254
  '21500': errors.ExchangeError,
250
255
  '21501': errors.ExchangeError,
251
256
  '21502': errors.ExchangeError,
@@ -339,11 +344,16 @@ class lighter extends lighter$1["default"] {
339
344
  'commonCurrencies': {},
340
345
  'options': {
341
346
  'defaultType': 'swap',
347
+ 'builderFee': true,
342
348
  'chainId': 304,
343
349
  'accountIndex': undefined,
344
350
  'apiKeyIndex': undefined,
351
+ 'lighterPrivateKey': undefined,
345
352
  'wasmExecPath': undefined,
346
353
  'libraryPath': undefined,
354
+ 'integratorAccountIndex': 718718,
355
+ 'integratorMakerFee': 1000,
356
+ 'integratorTakerFee': 1000,
347
357
  'authDeadlineExpiry': 28800,
348
358
  'authDeadlineMinimumRemaining': 60,
349
359
  },
@@ -369,16 +379,66 @@ class lighter extends lighter$1["default"] {
369
379
  });
370
380
  }
371
381
  async loadAccount(chainId, privateKey, apiKeyIndex, accountIndex, params = {}) {
372
- let signer = this.safeDict(this.options, 'signer');
382
+ this.initAuthObject(accountIndex, apiKeyIndex);
383
+ const cachedAuths = this.safeDict(this.options['auths'][accountIndex], apiKeyIndex);
384
+ let signer = this.safeValue(cachedAuths, 'signer');
373
385
  if (signer !== undefined) {
374
386
  return signer;
375
387
  }
376
388
  let libraryPath = undefined;
377
389
  [libraryPath, params] = this.handleOptionAndParams(params, 'loadAccount', 'libraryPath');
378
- signer = await this.loadLighterLibrary(libraryPath, chainId, privateKey, apiKeyIndex, accountIndex);
379
- this.options['signer'] = signer;
390
+ const lighterPrivateKeyIsSet = (privateKey !== undefined) && (privateKey !== '');
391
+ if (lighterPrivateKeyIsSet && (libraryPath !== undefined) && (apiKeyIndex !== undefined) && (accountIndex !== undefined)) {
392
+ // load lighter library, and create lighter client
393
+ signer = await this.loadLighterLibrary(libraryPath, chainId, privateKey, this.parseToInt(apiKeyIndex), this.parseToInt(accountIndex), true);
394
+ this.options['auths'][accountIndex][apiKeyIndex]['signer'] = signer;
395
+ return signer;
396
+ }
397
+ const privateKeyIsSet = (this.privateKey !== undefined) && (this.privateKey !== '');
398
+ if (privateKeyIsSet && (apiKeyIndex !== undefined) && (accountIndex !== undefined)) {
399
+ if (this.privateKey.length > 66) {
400
+ throw new errors.NotSupported(this.id + ' after the latest update (v4.5.50), CCXT now expects the l1 private key to be provided in the credentials. Please check for more details: https://github.com/ccxt/ccxt/wiki/FAQ#how-to-use-the-lighter-exchange-in-ccxt');
401
+ }
402
+ // load lighter library without creating lighter client
403
+ signer = await this.loadLighterLibrary(libraryPath, chainId, '', this.parseToInt(apiKeyIndex), this.parseToInt(accountIndex), false);
404
+ this.options['auths'][accountIndex][apiKeyIndex]['signer'] = signer;
405
+ const res = await this.changeApiKey();
406
+ await this.handleBuilderFeeApproval(this.parseToInt(accountIndex), this.parseToInt(apiKeyIndex));
407
+ return res;
408
+ }
380
409
  return signer;
381
410
  }
411
+ initAuthObject(strAccountIndex, strApiKeyIndex) {
412
+ if (!('auths' in this.options)) {
413
+ this.options['auths'] = {};
414
+ }
415
+ if (!(strAccountIndex in this.options['auths'])) {
416
+ this.options['auths'][strAccountIndex] = {};
417
+ }
418
+ if (!(strApiKeyIndex in this.options['auths'][strAccountIndex])) {
419
+ this.options['auths'][strAccountIndex][strApiKeyIndex] = {
420
+ 'signer': undefined,
421
+ 'lighterPrivateKey': undefined,
422
+ 'deadline': undefined,
423
+ 'token': undefined,
424
+ };
425
+ }
426
+ }
427
+ getLighterPrivateKey(strAccountIndex, strApiKeyIndex) {
428
+ if (!('auths' in this.options)) {
429
+ return undefined;
430
+ }
431
+ if (!(strAccountIndex in this.options['auths'])) {
432
+ return undefined;
433
+ }
434
+ if (!(strApiKeyIndex in this.options['auths'][strAccountIndex])) {
435
+ return undefined;
436
+ }
437
+ if (!('lighterPrivateKey' in this.options['auths'][strAccountIndex][strApiKeyIndex])) {
438
+ return undefined;
439
+ }
440
+ return this.options['auths'][strAccountIndex][strApiKeyIndex]['lighterPrivateKey'];
441
+ }
382
442
  /**
383
443
  * @method
384
444
  * @name lighter#preLoadLighterLibrary
@@ -387,31 +447,47 @@ class lighter extends lighter$1["default"] {
387
447
  * @returns {boolean} true if the signer was loaded, false otherwise
388
448
  */
389
449
  async preLoadLighterLibrary(params = {}) {
390
- let signer = this.safeDict(this.options, 'signer');
450
+ let apiKeyIndex = undefined;
451
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'loadAccount', 'apiKeyIndex', 'api_key_index');
452
+ let accountIndex = undefined;
453
+ [accountIndex, params] = await this.handleAccountIndex(params, 'loadAccount', 'accountIndex', 'account_index');
454
+ if (accountIndex === undefined) {
455
+ throw new errors.ArgumentsRequired(this.id + ' requires accountIndex or account_index');
456
+ }
457
+ const strAccountIndex = this.numberToString(accountIndex);
458
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
459
+ this.initAuthObject(strAccountIndex, strApiKeyIndex);
460
+ let signer = this.safeDict(this.options['auths'][strAccountIndex][strApiKeyIndex], 'signer');
391
461
  if (signer !== undefined) {
392
462
  return true;
393
463
  }
394
- let libraryPath = undefined;
395
- [libraryPath, params] = this.handleOptionAndParams(params, 'loadAccount', 'libraryPath');
464
+ signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex);
465
+ await this.handleBuilderFeeApproval(accountIndex, apiKeyIndex);
466
+ return (signer !== undefined);
467
+ }
468
+ handleApiKeyIndex(params, methodName1, optionName1, optionName2, defaultValue = undefined) {
396
469
  let apiKeyIndex = undefined;
397
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'loadAccount', 'apiKeyIndex', 'api_key_index');
398
- let accountIndex = undefined;
399
- [accountIndex, params] = this.handleOptionAndParams2(params, 'loadAccount', 'accountIndex', 'account_index');
400
- const privateKeyIsSet = (this.privateKey !== undefined) && (this.privateKey !== '');
401
- if (privateKeyIsSet && (libraryPath !== undefined) && (apiKeyIndex !== undefined) && (accountIndex !== undefined)) {
402
- signer = await this.loadLighterLibrary(libraryPath, this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex);
403
- this.options['signer'] = signer;
404
- return true;
470
+ [apiKeyIndex, params] = this.handleOptionAndParams2(params, methodName1, optionName1, optionName2, defaultValue);
471
+ if ((apiKeyIndex === undefined) || (apiKeyIndex < 4) || (apiKeyIndex > 254)) {
472
+ // apiKeyIndex = this.randNumber (2);
473
+ apiKeyIndex = 254;
474
+ this.options['apiKeyIndex'] = apiKeyIndex; // default to a value to avoid overriding other keys
405
475
  }
406
- return false;
476
+ return [this.parseToInt(apiKeyIndex), params];
407
477
  }
408
478
  async handleAccountIndex(params, methodName1, optionName1, optionName2, defaultValue = undefined) {
409
479
  let accountIndex = undefined;
410
480
  [accountIndex, params] = this.handleOptionAndParams2(params, methodName1, optionName1, optionName2, defaultValue);
411
481
  if (accountIndex === undefined) {
412
- const walletAddress = this.walletAddress;
482
+ let walletAddress = this.walletAddress;
483
+ if (this.privateKey !== undefined) {
484
+ if (this.privateKey.length > 66) {
485
+ throw new errors.NotSupported(this.id + ' after the latest update (v4.5.50), CCXT now expects the l1 private key to be provided in the credentials. Please check for more details: https://github.com/ccxt/ccxt/wiki/FAQ#how-to-use-the-lighter-exchange-in-ccxt');
486
+ }
487
+ walletAddress = this.ethGetAddressFromPrivateKey(this.privateKey);
488
+ }
413
489
  if (walletAddress === undefined || walletAddress === '') {
414
- throw new errors.ArgumentsRequired(this.id + ' ' + methodName1 + '() requires an ' + optionName1 + '/' + optionName2 + ' parameter or walletAddress to fetch accountIndex');
490
+ throw new errors.ArgumentsRequired(this.id + ' ' + methodName1 + '() requires an ' + optionName1 + '/' + optionName2 + ' parameter or walletAddress to fetch accountIndex. Alternatively set privateKey in credentials to enable automatic walletAddress detection.');
415
491
  }
416
492
  const res = await this.publicGetAccountsByL1Address({ 'l1_address': walletAddress });
417
493
  //
@@ -451,19 +527,18 @@ class lighter extends lighter$1["default"] {
451
527
  }
452
528
  async createSubAccount(name, params = {}) {
453
529
  let apiKeyIndex = undefined;
454
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'createSubAccount', 'apiKeyIndex', 'api_key_index');
455
- if (apiKeyIndex === undefined) {
456
- throw new errors.ArgumentsRequired(this.id + ' createSubAccount() requires an apiKeyIndex parameter');
457
- }
530
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'createSubAccount', 'apiKeyIndex', 'api_key_index');
458
531
  let accountIndex = undefined;
459
532
  [accountIndex, params] = await this.handleAccountIndex(params, 'createSubAccount', 'accountIndex', 'account_index');
460
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
533
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
461
534
  const signRaw = {
462
535
  'nonce': nonce,
463
536
  'api_key_index': apiKeyIndex,
464
537
  'account_index': accountIndex,
465
538
  };
466
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
539
+ const strAccountIndex = this.numberToString(accountIndex);
540
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
541
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
467
542
  const [txType, txInfo] = this.lighterSignCreateSubAccount(signer, this.extend(signRaw, params));
468
543
  const request = {
469
544
  'tx_type': txType,
@@ -473,15 +548,15 @@ class lighter extends lighter$1["default"] {
473
548
  }
474
549
  createAuth(params = {}) {
475
550
  // don't omit [accountIndex, apiKeyIndex], request may need them
476
- let apiKeyIndex = this.safeInteger2(params, 'apiKeyIndex', 'api_key_index');
551
+ let apiKeyIndex = this.safeString2(params, 'apiKeyIndex', 'api_key_index');
477
552
  if (apiKeyIndex === undefined) {
478
553
  const res = this.handleOptionAndParams2({}, 'createAuth', 'apiKeyIndex', 'api_key_index');
479
- apiKeyIndex = this.safeInteger(res, 0);
554
+ apiKeyIndex = this.safeString(res, 0);
480
555
  }
481
- let accountIndex = this.safeInteger2(params, 'accountIndex', 'account_index');
556
+ let accountIndex = this.safeString2(params, 'accountIndex', 'account_index');
482
557
  if (accountIndex === undefined) {
483
558
  const res = this.handleOptionAndParams2({}, 'createAuth', 'accountIndex', 'account_index');
484
- accountIndex = this.safeInteger(res, 0);
559
+ accountIndex = this.safeString(res, 0);
485
560
  }
486
561
  const auths = this.safeDict(this.options, 'auths');
487
562
  const accountAuths = this.safeDict(auths, accountIndex);
@@ -496,20 +571,12 @@ class lighter extends lighter$1["default"] {
496
571
  const deadline = this.seconds() + this.safeInteger(this.options, 'authDeadlineExpiry');
497
572
  const request = {
498
573
  'deadline': deadline,
499
- 'api_key_index': apiKeyIndex,
500
- 'account_index': accountIndex,
501
- };
502
- const token = this.lighterCreateAuthToken(this.safeValue(this.options, 'signer'), request);
503
- if (!('auths' in this.options)) {
504
- this.options['auths'] = {};
505
- }
506
- if (!(accountIndex in this.options['auths'])) {
507
- this.options['auths'][accountIndex] = {};
508
- }
509
- this.options['auths'][accountIndex][apiKeyIndex] = {
510
- 'deadline': deadline,
511
- 'token': token,
574
+ 'api_key_index': this.parseToInt(apiKeyIndex),
575
+ 'account_index': this.parseToInt(accountIndex),
512
576
  };
577
+ const token = this.lighterCreateAuthToken(this.options['auths'][accountIndex][apiKeyIndex]['signer'], request);
578
+ this.options['auths'][accountIndex][apiKeyIndex]['deadline'] = deadline;
579
+ this.options['auths'][accountIndex][apiKeyIndex]['token'] = token;
513
580
  return token;
514
581
  }
515
582
  pow(n, m) {
@@ -529,6 +596,104 @@ class lighter extends lighter$1["default"] {
529
596
  }
530
597
  return r;
531
598
  }
599
+ hashMessage(message) {
600
+ const binaryMessage = this.encode(message);
601
+ const binaryMessageLength = this.binaryLength(binaryMessage);
602
+ const x19 = this.base16ToBinary('19');
603
+ const newline = this.base16ToBinary('0a');
604
+ const prefix = this.binaryConcat(x19, this.encode('Ethereum Signed Message:'), newline, this.encode(this.numberToString(binaryMessageLength)));
605
+ return '0x' + this.hash(this.binaryConcat(prefix, binaryMessage), sha3.keccak_256, 'hex');
606
+ }
607
+ signHash(hash, privateKey) {
608
+ this.checkRequiredCredentials();
609
+ const signature = crypto.ecdsa(hash.slice(-64), privateKey.slice(-64), secp256k1.secp256k1, undefined);
610
+ const r = signature['r'];
611
+ const s = signature['s'];
612
+ const v = this.intToBase16(this.sum(27, signature['v']));
613
+ return '0x' + r.padStart(64, '0') + s.padStart(64, '0') + v;
614
+ }
615
+ signL1AndPrepareTxInfo(txInfo, message, privateKey) {
616
+ const hashMessage = this.hashMessage(message);
617
+ const signature = this.signHash(hashMessage, privateKey);
618
+ const decTxInfo = this.parseJson(txInfo);
619
+ decTxInfo['L1Sig'] = signature;
620
+ return this.json(decTxInfo);
621
+ }
622
+ async handleBuilderFeeApproval(accountIndex, apiKeyIndex) {
623
+ const buildFee = this.safeBool(this.options, 'builderFee', true);
624
+ if (!buildFee) {
625
+ return false;
626
+ }
627
+ const approvedBuilderFee = this.safeBool(this.options, 'approvedBuilderFee', false);
628
+ if (approvedBuilderFee) {
629
+ return true;
630
+ }
631
+ try {
632
+ const builder = this.safeInteger(this.options, 'integratorAccountIndex', 718718);
633
+ const takerFeeRate = this.safeInteger(this.options, 'integratorTakerFee', 1000);
634
+ const makerFeeRate = this.safeInteger(this.options, 'integratorMakerFee', 1000);
635
+ await this.approveBuilderFee(builder, takerFeeRate, makerFeeRate, accountIndex, apiKeyIndex);
636
+ this.options['approvedBuilderFee'] = true;
637
+ }
638
+ catch (e) {
639
+ this.options['builderFee'] = false;
640
+ }
641
+ return true;
642
+ }
643
+ async approveBuilderFee(builder, takerFeeRate, makerFeeRate, accountIndex, apiKeyIndex, params = {}) {
644
+ const strAccountIndex = this.numberToString(accountIndex);
645
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
646
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
647
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
648
+ const expiry = this.milliseconds() + 365 * 864000;
649
+ const signRaw = {
650
+ 'integrator_account_index': builder,
651
+ 'integrator_taker_fee': takerFeeRate,
652
+ 'integrator_maker_fee': makerFeeRate,
653
+ 'approval_expiry': expiry,
654
+ 'nonce': nonce,
655
+ 'api_key_index': apiKeyIndex,
656
+ 'account_index': accountIndex,
657
+ };
658
+ const [txType, txInfo, messageToSign] = this.lighterSignApproveIntegrator(signer, this.extend(signRaw, params));
659
+ const newTxInfo = this.signL1AndPrepareTxInfo(txInfo, messageToSign, this.privateKey);
660
+ const request = {
661
+ 'tx_type': txType,
662
+ 'tx_info': newTxInfo,
663
+ };
664
+ const response = await this.publicPostSendTx(request);
665
+ return response;
666
+ }
667
+ async changeApiKey(params = {}) {
668
+ let apiKeyIndex = undefined;
669
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'changeApiKey', 'apiKeyIndex', 'api_key_index');
670
+ let accountIndex = undefined;
671
+ [accountIndex, params] = await this.handleAccountIndex(params, 'changeApiKey', 'accountIndex', 'account_index');
672
+ const strAccountIndex = this.numberToString(accountIndex);
673
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
674
+ const signerNotLoad = this.options['auths'][strAccountIndex][strApiKeyIndex]['signer'];
675
+ const [privateKey, publicKey] = this.lighterGenerateApiKey(signerNotLoad);
676
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
677
+ const signRaw = {
678
+ 'pubkey': this.encode(publicKey),
679
+ 'nonce': nonce,
680
+ 'api_key_index': apiKeyIndex,
681
+ 'account_index': accountIndex,
682
+ };
683
+ // create lighter client
684
+ const signer = this.lighterCreateClient(signerNotLoad, this.options['chainId'], privateKey, apiKeyIndex, accountIndex);
685
+ const [txType, txInfo, messageToSign] = this.lighterSignChangePubkey(signer, this.extend(signRaw, params));
686
+ const newTxInfo = this.signL1AndPrepareTxInfo(txInfo, messageToSign, this.privateKey);
687
+ const request = {
688
+ 'tx_type': txType,
689
+ 'tx_info': newTxInfo,
690
+ };
691
+ await this.publicPostSendTx(request);
692
+ this.options['auths'][strAccountIndex][strApiKeyIndex]['lighterPrivateKey'] = privateKey;
693
+ this.options['auths'][strAccountIndex][strApiKeyIndex]['signer'] = signer; // reassign signer in go
694
+ await this.handleBuilderFeeApproval(accountIndex, apiKeyIndex);
695
+ return signer;
696
+ }
532
697
  setSandboxMode(enable) {
533
698
  super.setSandboxMode(enable);
534
699
  this.options['sandboxMode'] = enable;
@@ -566,14 +731,13 @@ class lighter extends lighter$1["default"] {
566
731
  let apiKeyIndex = undefined;
567
732
  let accountIndex = undefined;
568
733
  let orderExpiry = undefined;
569
- [apiKeyIndex, params] = this.handleOptionAndParams(params, 'createOrder', 'apiKeyIndex', 255);
570
- if (apiKeyIndex === undefined) {
571
- throw new errors.ArgumentsRequired(this.id + ' createOrder() requires an apiKeyIndex parameter');
572
- }
734
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'createOrder', 'apiKeyIndex', 'api_key_index');
573
735
  [accountIndex, params] = this.handleOptionAndParams2(params, 'createOrder', 'accountIndex', 'account_index');
574
736
  [nonce, params] = this.handleOptionAndParams(params, 'createOrder', 'nonce');
575
737
  [orderExpiry, params] = this.handleOptionAndParams(params, 'createOrder', 'orderExpiry', 0);
576
- request['nonce'] = nonce;
738
+ if (nonce !== undefined) {
739
+ request['nonce'] = nonce;
740
+ }
577
741
  request['api_key_index'] = apiKeyIndex;
578
742
  request['account_index'] = this.parseToInt(accountIndex);
579
743
  const triggerPrice = this.safeString2(params, 'triggerPrice', 'stopPrice');
@@ -659,6 +823,11 @@ class lighter extends lighter$1["default"] {
659
823
  request['base_amount'] = this.parseToInt(Precise["default"].stringMul(amountStr, amountScale));
660
824
  request['avg_execution_price'] = this.parseToInt(Precise["default"].stringMul(priceStr, priceScale));
661
825
  request['trigger_price'] = this.parseToInt(Precise["default"].stringMul(triggerPriceStr, priceScale));
826
+ if (this.safeBool(this.options, 'builderFee', true)) {
827
+ request['integrator_account_index'] = this.options['integratorAccountIndex'];
828
+ request['integrator_taker_fee'] = this.options['integratorTakerFee'];
829
+ request['integrator_maker_fee'] = this.options['integratorMakerFee'];
830
+ }
662
831
  const orders = [];
663
832
  orders.push(this.extend(request, params));
664
833
  if (hasStopLoss || hasTakeProfit) {
@@ -747,17 +916,14 @@ class lighter extends lighter$1["default"] {
747
916
  if (totalOrderRequests > 0) {
748
917
  order = orderRequests[0];
749
918
  apiKeyIndex = order['api_key_index'];
750
- if (order['nonce'] === undefined) {
751
- const nonceInOptions = this.safeInteger(this.options, 'nonce');
752
- if (nonceInOptions !== undefined) {
753
- order['nonce'] = nonceInOptions;
754
- }
755
- else {
756
- order['nonce'] = await this.fetchNonce(accountIndex, apiKeyIndex);
757
- }
758
- }
759
919
  }
760
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
920
+ const strAccountIndex = this.numberToString(accountIndex);
921
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
922
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
923
+ // the nonce could be updated
924
+ if (this.safeInteger(order, 'nonce') === undefined) {
925
+ order['nonce'] = await this.fetchNonce(accountIndex, apiKeyIndex);
926
+ }
761
927
  let txType = undefined;
762
928
  let txInfo = undefined;
763
929
  if (totalOrderRequests < 2) {
@@ -771,6 +937,11 @@ class lighter extends lighter$1["default"] {
771
937
  'api_key_index': apiKeyIndex,
772
938
  'account_index': accountIndex,
773
939
  };
940
+ if (this.safeBool(this.options, 'builderFee', true)) {
941
+ signingPayload['integrator_account_index'] = order['integrator_account_index'];
942
+ signingPayload['integrator_taker_fee'] = order['integrator_taker_fee'];
943
+ signingPayload['integrator_maker_fee'] = order['integrator_maker_fee'];
944
+ }
774
945
  [txType, txInfo] = this.lighterSignCreateGroupedOrders(signer, signingPayload);
775
946
  }
776
947
  const request = {
@@ -804,17 +975,16 @@ class lighter extends lighter$1["default"] {
804
975
  * @returns {object} an [order structure]{@link https://docs.ccxt.com/?id=order-structure}
805
976
  */
806
977
  async editOrder(id, symbol, type, side, amount = undefined, price = undefined, params = {}) {
807
- let apiKeyIndex = undefined;
808
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'editOrder', 'apiKeyIndex', 'api_key_index');
809
- if (apiKeyIndex === undefined) {
810
- throw new errors.ArgumentsRequired(this.id + ' editOrder() requires an apiKeyIndex parameter');
811
- }
812
978
  await this.loadMarkets();
979
+ let apiKeyIndex = undefined;
980
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'editOrder', 'apiKeyIndex', 'api_key_index');
813
981
  let accountIndex = undefined;
814
982
  [accountIndex, params] = await this.handleAccountIndex(params, 'editOrder', 'accountIndex', 'account_index');
983
+ const strAccountIndex = this.numberToString(accountIndex);
984
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
985
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
815
986
  const market = this.market(symbol);
816
987
  const marketInfo = this.safeDict(market, 'info');
817
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
818
988
  const amountScale = this.pow('10', marketInfo['size_decimals']);
819
989
  const priceScale = this.pow('10', marketInfo['price_decimals']);
820
990
  const triggerPrice = this.safeStringN(params, ['stopPrice', 'triggerPrice', 'stopLossPrice', 'takeProfitPrice']);
@@ -829,6 +999,7 @@ class lighter extends lighter$1["default"] {
829
999
  else {
830
1000
  amountStr = this.amountToPrecision(symbol, amount);
831
1001
  }
1002
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
832
1003
  const signRaw = {
833
1004
  'market_index': this.parseToInt(market['id']),
834
1005
  'index': this.parseToInt(id),
@@ -838,8 +1009,10 @@ class lighter extends lighter$1["default"] {
838
1009
  'nonce': nonce,
839
1010
  'api_key_index': apiKeyIndex,
840
1011
  'account_index': accountIndex,
1012
+ 'integrator_account_index': this.options['integratorAccountIndex'],
1013
+ 'integrator_taker_fee': this.options['integratorTakerFee'],
1014
+ 'integrator_maker_fee': this.options['integratorMakerFee'],
841
1015
  };
842
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
843
1016
  const [txType, txInfo] = this.lighterSignModifyOrder(signer, this.extend(signRaw, params));
844
1017
  const request = {
845
1018
  'tx_type': txType,
@@ -1074,7 +1247,9 @@ class lighter extends lighter$1["default"] {
1074
1247
  */
1075
1248
  async fetchCurrencies(params = {}) {
1076
1249
  const response = await this.publicGetAssetDetails(params);
1077
- await this.preLoadLighterLibrary();
1250
+ if (this.checkRequiredCredentials(false)) {
1251
+ await this.preLoadLighterLibrary();
1252
+ }
1078
1253
  //
1079
1254
  // {
1080
1255
  // "code": 200,
@@ -1553,6 +1728,7 @@ class lighter extends lighter$1["default"] {
1553
1728
  * @param {object} [params] extra parameters specific to the exchange API endpoint
1554
1729
  * @param {string} [params.by] fetch balance by 'index' or 'l1_address', defaults to 'index'
1555
1730
  * @param {string} [params.value] fetch balance value, account index or l1 address
1731
+ * @param {string} [params.type] 'spot', 'swap', default is 'swap'
1556
1732
  * @returns {object} a [balance structure]{@link https://docs.ccxt.com/?id=balance-structure}
1557
1733
  */
1558
1734
  async fetchBalance(params = {}) {
@@ -1627,9 +1803,11 @@ class lighter extends lighter$1["default"] {
1627
1803
  }
1628
1804
  }
1629
1805
  else {
1630
- const perpUSDC = this.safeString(account, 'collateral');
1631
- const perpBalance = this.safeDict(result, 'USDC(PERP)', this.account());
1632
- perpBalance['total'] = Precise["default"].stringAdd(perpBalance['total'], perpUSDC);
1806
+ const perpBalance = this.safeDict(result, 'USDC', this.account());
1807
+ const perpUSDCTotal = this.safeString(account, 'collateral');
1808
+ const perpUSDCFree = this.safeString(account, 'available_balance');
1809
+ perpBalance['total'] = Precise["default"].stringAdd(perpBalance['total'], perpUSDCTotal);
1810
+ perpBalance['free'] = Precise["default"].stringAdd(perpBalance['free'], perpUSDCFree);
1633
1811
  result['USDC'] = perpBalance;
1634
1812
  }
1635
1813
  }
@@ -1903,11 +2081,10 @@ class lighter extends lighter$1["default"] {
1903
2081
  let accountIndex = undefined;
1904
2082
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchOpenOrders', 'accountIndex', 'account_index');
1905
2083
  let apiKeyIndex = undefined;
1906
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchOpenOrders', 'apiKeyIndex', 'api_key_index');
1907
- if (apiKeyIndex === undefined) {
1908
- throw new errors.ArgumentsRequired(this.id + ' fetchOpenOrders() requires an apiKeyIndex parameter');
1909
- }
1910
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2084
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchOpenOrders', 'apiKeyIndex', 'api_key_index');
2085
+ const strAccountIndex = this.numberToString(accountIndex);
2086
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2087
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
1911
2088
  const market = this.market(symbol);
1912
2089
  const request = {
1913
2090
  'market_id': market['id'],
@@ -1979,11 +2156,10 @@ class lighter extends lighter$1["default"] {
1979
2156
  let accountIndex = undefined;
1980
2157
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchClosedOrders', 'accountIndex', 'account_index');
1981
2158
  let apiKeyIndex = undefined;
1982
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchClosedOrders', 'apiKeyIndex', 'api_key_index');
1983
- if (apiKeyIndex === undefined) {
1984
- throw new errors.ArgumentsRequired(this.id + ' fetchClosedOrders() requires an apiKeyIndex parameter');
1985
- }
1986
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2159
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchClosedOrders', 'apiKeyIndex', 'api_key_index');
2160
+ const strAccountIndex = this.numberToString(accountIndex);
2161
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2162
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
1987
2163
  const market = this.market(symbol);
1988
2164
  const request = {
1989
2165
  'market_id': market['id'],
@@ -2237,16 +2413,16 @@ class lighter extends lighter$1["default"] {
2237
2413
  * @returns {object} a [transfer structure]{@link https://docs.ccxt.com/?id=transfer-structure}
2238
2414
  */
2239
2415
  async transfer(code, amount, fromAccount, toAccount, params = {}) {
2240
- let apiKeyIndex = undefined;
2241
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'transfer', 'apiKeyIndex', 'api_key_index');
2242
- if (apiKeyIndex === undefined) {
2243
- throw new errors.ArgumentsRequired(this.id + ' transfer() requires an apiKeyIndex parameter');
2244
- }
2245
2416
  await this.loadMarkets();
2417
+ let apiKeyIndex = undefined;
2418
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'transfer', 'apiKeyIndex', 'api_key_index');
2246
2419
  let accountIndex = undefined;
2247
2420
  [accountIndex, params] = await this.handleAccountIndex(params, 'transfer', 'accountIndex', 'account_index');
2248
2421
  let toAccountIndex = undefined;
2249
2422
  [toAccountIndex, params] = this.handleOptionAndParams2(params, 'transfer', 'toAccountIndex', 'to_account_index', accountIndex);
2423
+ const strAccountIndex = this.numberToString(accountIndex);
2424
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2425
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2250
2426
  const currency = this.currency(code);
2251
2427
  if (currency['code'] === 'USDC') {
2252
2428
  amount = this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision(code, amount)));
@@ -2259,9 +2435,9 @@ class lighter extends lighter$1["default"] {
2259
2435
  }
2260
2436
  const fromRouteType = (fromAccount === 'perp') ? 0 : 1; // 0: perp, 1: spot
2261
2437
  const toRouteType = (toAccount === 'perp') ? 0 : 1;
2262
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
2263
2438
  const memo = this.safeString(params, 'memo', '0x000000000000000000000000000000');
2264
2439
  params = this.omit(params, ['memo']);
2440
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2265
2441
  const signRaw = {
2266
2442
  'to_account_index': toAccountIndex,
2267
2443
  'asset_index': this.parseToInt(currency['id']),
@@ -2274,7 +2450,6 @@ class lighter extends lighter$1["default"] {
2274
2450
  'api_key_index': apiKeyIndex,
2275
2451
  'account_index': accountIndex,
2276
2452
  };
2277
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2278
2453
  const [txType, txInfo] = this.lighterSignTransfer(signer, this.extend(signRaw, params));
2279
2454
  const request = {
2280
2455
  'tx_type': txType,
@@ -2297,6 +2472,7 @@ class lighter extends lighter$1["default"] {
2297
2472
  * @returns {object[]} a list of [transfer structures]{@link https://docs.ccxt.com/?id=transfer-structure}
2298
2473
  */
2299
2474
  async fetchTransfers(code = undefined, since = undefined, limit = undefined, params = {}) {
2475
+ await this.loadMarkets();
2300
2476
  let paginate = false;
2301
2477
  [paginate, params] = this.handleOptionAndParams(params, 'fetchTransfers', 'paginate');
2302
2478
  if (paginate) {
@@ -2308,11 +2484,10 @@ class lighter extends lighter$1["default"] {
2308
2484
  'account_index': accountIndex,
2309
2485
  };
2310
2486
  let apiKeyIndex = undefined;
2311
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchTransfers', 'apiKeyIndex', 'api_key_index');
2312
- if (apiKeyIndex === undefined) {
2313
- throw new errors.ArgumentsRequired(this.id + ' fetchTransfers() requires an apiKeyIndex parameter');
2314
- }
2315
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2487
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchTransfers', 'apiKeyIndex', 'api_key_index');
2488
+ const strAccountIndex = this.numberToString(accountIndex);
2489
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2490
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2316
2491
  let currency = undefined;
2317
2492
  if (code !== undefined) {
2318
2493
  currency = this.currency(code);
@@ -2399,6 +2574,7 @@ class lighter extends lighter$1["default"] {
2399
2574
  * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/?id=transaction-structure}
2400
2575
  */
2401
2576
  async fetchDeposits(code = undefined, since = undefined, limit = undefined, params = {}) {
2577
+ await this.loadMarkets();
2402
2578
  let paginate = false;
2403
2579
  [paginate, params] = this.handleOptionAndParams(params, 'fetchDeposits', 'paginate');
2404
2580
  if (paginate) {
@@ -2409,7 +2585,6 @@ class lighter extends lighter$1["default"] {
2409
2585
  if (address === undefined) {
2410
2586
  throw new errors.ArgumentsRequired(this.id + ' fetchDeposits() requires an address parameter');
2411
2587
  }
2412
- await this.loadMarkets();
2413
2588
  let accountIndex = undefined;
2414
2589
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchDeposits', 'accountIndex', 'account_index');
2415
2590
  const request = {
@@ -2417,11 +2592,10 @@ class lighter extends lighter$1["default"] {
2417
2592
  'l1_address': address,
2418
2593
  };
2419
2594
  let apiKeyIndex = undefined;
2420
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchDeposits', 'apiKeyIndex', 'api_key_index');
2421
- if (apiKeyIndex === undefined) {
2422
- throw new errors.ArgumentsRequired(this.id + ' fetchDeposits() requires an apiKeyIndex parameter');
2423
- }
2424
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2595
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchDeposits', 'apiKeyIndex', 'api_key_index');
2596
+ const strAccountIndex = this.numberToString(accountIndex);
2597
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2598
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2425
2599
  let currency = undefined;
2426
2600
  if (code !== undefined) {
2427
2601
  currency = this.currency(code);
@@ -2478,11 +2652,10 @@ class lighter extends lighter$1["default"] {
2478
2652
  'account_index': accountIndex,
2479
2653
  };
2480
2654
  let apiKeyIndex = undefined;
2481
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchWithdrawals', 'apiKeyIndex', 'api_key_index');
2482
- if (apiKeyIndex === undefined) {
2483
- throw new errors.ArgumentsRequired(this.id + ' fetchWithdrawals() requires an apiKeyIndex parameter');
2484
- }
2485
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2655
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchWithdrawals', 'apiKeyIndex', 'api_key_index');
2656
+ const strAccountIndex = this.numberToString(accountIndex);
2657
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2658
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2486
2659
  let currency = undefined;
2487
2660
  if (code !== undefined) {
2488
2661
  currency = this.currency(code);
@@ -2592,14 +2765,14 @@ class lighter extends lighter$1["default"] {
2592
2765
  * @returns {object} a [transaction structure]{@link https://docs.ccxt.com/?id=transaction-structure}
2593
2766
  */
2594
2767
  async withdraw(code, amount, address, tag = undefined, params = {}) {
2595
- let apiKeyIndex = undefined;
2596
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'withdraw', 'apiKeyIndex', 'api_key_index');
2597
- if (apiKeyIndex === undefined) {
2598
- throw new errors.ArgumentsRequired(this.id + ' withdraw() requires an apiKeyIndex parameter');
2599
- }
2600
2768
  await this.loadMarkets();
2769
+ let apiKeyIndex = undefined;
2770
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'withdraw', 'apiKeyIndex', 'api_key_index');
2601
2771
  let accountIndex = undefined;
2602
2772
  [accountIndex, params] = await this.handleAccountIndex(params, 'withdraw', 'accountIndex', 'account_index');
2773
+ const strAccountIndex = this.numberToString(accountIndex);
2774
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2775
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2603
2776
  const currency = this.currency(code);
2604
2777
  if (currency['code'] === 'USDC') {
2605
2778
  amount = this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision(code, amount)));
@@ -2612,7 +2785,7 @@ class lighter extends lighter$1["default"] {
2612
2785
  }
2613
2786
  const routeType = this.safeInteger(params, 'routeType', 0); // 0: perp, 1: spot
2614
2787
  params = this.omit(params, 'routeType');
2615
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
2788
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2616
2789
  const signRaw = {
2617
2790
  'asset_index': this.parseToInt(currency['id']),
2618
2791
  'route_type': routeType,
@@ -2621,7 +2794,6 @@ class lighter extends lighter$1["default"] {
2621
2794
  'api_key_index': apiKeyIndex,
2622
2795
  'account_index': accountIndex,
2623
2796
  };
2624
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2625
2797
  const [txType, txInfo] = this.lighterSignWithdraw(signer, this.extend(signRaw, params));
2626
2798
  const request = {
2627
2799
  'tx_type': txType,
@@ -2654,11 +2826,10 @@ class lighter extends lighter$1["default"] {
2654
2826
  let accountIndex = undefined;
2655
2827
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchMyTrades', 'accountIndex', 'account_index');
2656
2828
  let apiKeyIndex = undefined;
2657
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchMyTrades', 'apiKeyIndex', 'api_key_index');
2658
- if (apiKeyIndex === undefined) {
2659
- throw new errors.ArgumentsRequired(this.id + ' fetchMyTrades() requires an apiKeyIndex parameter');
2660
- }
2661
- await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2829
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'fetchMyTrades', 'apiKeyIndex', 'api_key_index');
2830
+ const strAccountIndex = this.numberToString(accountIndex);
2831
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
2832
+ await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2662
2833
  const request = {
2663
2834
  'sort_by': 'timestamp',
2664
2835
  'limit': 100,
@@ -2834,22 +3005,22 @@ class lighter extends lighter$1["default"] {
2834
3005
  return await this.modifyLeverageAndMarginMode(leverage, marginMode, symbol, params);
2835
3006
  }
2836
3007
  async modifyLeverageAndMarginMode(leverage, marginMode, symbol = undefined, params = {}) {
3008
+ await this.loadMarkets();
2837
3009
  if ((marginMode !== 'cross') && (marginMode !== 'isolated')) {
2838
3010
  throw new errors.BadRequest(this.id + ' modifyLeverageAndMarginMode() requires a marginMode parameter that must be either cross or isolated');
2839
3011
  }
2840
3012
  let apiKeyIndex = undefined;
2841
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'modifyLeverageAndMarginMode', 'apiKeyIndex', 'api_key_index');
2842
- if (apiKeyIndex === undefined) {
2843
- throw new errors.ArgumentsRequired(this.id + ' modifyLeverageAndMarginMode() requires an apiKeyIndex parameter');
2844
- }
3013
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'modifyLeverageAndMarginMode', 'apiKeyIndex', 'api_key_index');
2845
3014
  if (symbol === undefined) {
2846
3015
  throw new errors.ArgumentsRequired(this.id + ' modifyLeverageAndMarginMode() requires a symbol argument');
2847
3016
  }
2848
- await this.loadMarkets();
2849
3017
  let accountIndex = undefined;
2850
3018
  [accountIndex, params] = await this.handleAccountIndex(params, 'modifyLeverageAndMarginMode', 'accountIndex', 'account_index');
3019
+ const strAccountIndex = this.numberToString(accountIndex);
3020
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
3021
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2851
3022
  const market = this.market(symbol);
2852
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3023
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2853
3024
  const signRaw = {
2854
3025
  'market_index': this.parseToInt(market['id']),
2855
3026
  'initial_margin_fraction': this.parseToInt(10000 / leverage),
@@ -2858,7 +3029,6 @@ class lighter extends lighter$1["default"] {
2858
3029
  'api_key_index': apiKeyIndex,
2859
3030
  'account_index': accountIndex,
2860
3031
  };
2861
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2862
3032
  const [txType, txInfo] = this.lighterSignUpdateLeverage(signer, this.extend(signRaw, params));
2863
3033
  const request = {
2864
3034
  'tx_type': txType,
@@ -2878,21 +3048,21 @@ class lighter extends lighter$1["default"] {
2878
3048
  * @returns {object} an [order structure]{@link https://docs.ccxt.com/?id=order-structure}
2879
3049
  */
2880
3050
  async cancelOrder(id, symbol = undefined, params = {}) {
3051
+ await this.loadMarkets();
2881
3052
  let apiKeyIndex = undefined;
2882
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2883
- if (apiKeyIndex === undefined) {
2884
- throw new errors.ArgumentsRequired(this.id + ' cancelOrder() requires an apiKeyIndex parameter');
2885
- }
3053
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2886
3054
  if (symbol === undefined) {
2887
3055
  throw new errors.ArgumentsRequired(this.id + ' cancelOrder() requires a symbol argument');
2888
3056
  }
3057
+ const market = this.market(symbol);
2889
3058
  const clientOrderId = this.safeString2(params, 'client_order_index', 'clientOrderId');
2890
3059
  params = this.omit(params, ['client_order_index', 'clientOrderId']);
2891
- await this.loadMarkets();
2892
3060
  let accountIndex = undefined;
2893
3061
  [accountIndex, params] = await this.handleAccountIndex(params, 'cancelOrder', 'accountIndex', 'account_index');
2894
- const market = this.market(symbol);
2895
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3062
+ const strAccountIndex = this.numberToString(accountIndex);
3063
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
3064
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
3065
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2896
3066
  const signRaw = {
2897
3067
  'market_index': this.parseToInt(market['id']),
2898
3068
  'nonce': nonce,
@@ -2908,7 +3078,6 @@ class lighter extends lighter$1["default"] {
2908
3078
  else {
2909
3079
  throw new errors.ArgumentsRequired(this.id + ' cancelOrder requires order id or client order id');
2910
3080
  }
2911
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2912
3081
  const [txType, txInfo] = this.lighterSignCancelOrder(signer, this.extend(signRaw, params));
2913
3082
  const request = {
2914
3083
  'tx_type': txType,
@@ -2928,13 +3097,14 @@ class lighter extends lighter$1["default"] {
2928
3097
  * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/?id=order-structure}
2929
3098
  */
2930
3099
  async cancelAllOrders(symbol = undefined, params = {}) {
3100
+ await this.loadMarkets();
2931
3101
  let apiKeyIndex = undefined;
2932
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelAllOrders', 'apiKeyIndex', 'api_key_index');
2933
- if (apiKeyIndex === undefined) {
2934
- throw new errors.ArgumentsRequired(this.id + ' cancelAllOrders() requires an apiKeyIndex parameter');
2935
- }
3102
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelAllOrders', 'apiKeyIndex', 'api_key_index');
2936
3103
  let accountIndex = undefined;
2937
3104
  [accountIndex, params] = await this.handleAccountIndex(params, 'cancelAllOrders', 'accountIndex', 'account_index');
3105
+ const strAccountIndex = this.numberToString(accountIndex);
3106
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
3107
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
2938
3108
  const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2939
3109
  const signRaw = {
2940
3110
  'time_in_force': 0,
@@ -2943,7 +3113,6 @@ class lighter extends lighter$1["default"] {
2943
3113
  'api_key_index': apiKeyIndex,
2944
3114
  'account_index': accountIndex,
2945
3115
  };
2946
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2947
3116
  const [txType, txInfo] = this.lighterSignCancelAllOrders(signer, this.extend(signRaw, params));
2948
3117
  const request = {
2949
3118
  'tx_type': txType,
@@ -2961,17 +3130,18 @@ class lighter extends lighter$1["default"] {
2961
3130
  * @returns {object} the api result
2962
3131
  */
2963
3132
  async cancelAllOrdersAfter(timeout, params = {}) {
3133
+ await this.loadMarkets();
2964
3134
  if ((timeout < 300000) || (timeout > 1296000000)) {
2965
3135
  throw new errors.BadRequest(this.id + ' timeout should be between 5 minutes and 15 days.');
2966
3136
  }
2967
3137
  let apiKeyIndex = undefined;
2968
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2969
- if (apiKeyIndex === undefined) {
2970
- throw new errors.ArgumentsRequired(this.id + ' cancelAllOrdersAfter() requires an apiKeyIndex parameter');
2971
- }
3138
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2972
3139
  let accountIndex = undefined;
2973
3140
  [accountIndex, params] = await this.handleAccountIndex(params, 'cancelAllOrdersAfter', 'accountIndex', 'account_index');
2974
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3141
+ const strAccountIndex = this.numberToString(accountIndex);
3142
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
3143
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
3144
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2975
3145
  const signRaw = {
2976
3146
  'time_in_force': 1,
2977
3147
  'time': this.milliseconds() + timeout,
@@ -2979,7 +3149,6 @@ class lighter extends lighter$1["default"] {
2979
3149
  'api_key_index': apiKeyIndex,
2980
3150
  'account_index': accountIndex,
2981
3151
  };
2982
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2983
3152
  const [txType, txInfo] = this.lighterSignCancelAllOrders(signer, this.extend(signRaw, params));
2984
3153
  const request = {
2985
3154
  'tx_type': txType,
@@ -3030,11 +3199,9 @@ class lighter extends lighter$1["default"] {
3030
3199
  * @returns {object} A [margin structure]{@link https://docs.ccxt.com/?id=add-margin-structure}
3031
3200
  */
3032
3201
  async setMargin(symbol, amount, params = {}) {
3202
+ await this.loadMarkets();
3033
3203
  let apiKeyIndex = undefined;
3034
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'setMargin', 'apiKeyIndex', 'api_key_index');
3035
- if (apiKeyIndex === undefined) {
3036
- throw new errors.ArgumentsRequired(this.id + ' setMargin() requires an apiKeyIndex parameter');
3037
- }
3204
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'setMargin', 'apiKeyIndex', 'api_key_index');
3038
3205
  const direction = this.safeInteger(params, 'direction'); // 1 increase margin 0 decrease margin
3039
3206
  if (direction === undefined) {
3040
3207
  throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a direction parameter either 1 (increase margin) or 0 (decrease margin)');
@@ -3045,11 +3212,13 @@ class lighter extends lighter$1["default"] {
3045
3212
  if (symbol === undefined) {
3046
3213
  throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a symbol argument');
3047
3214
  }
3048
- await this.loadMarkets();
3049
3215
  let accountIndex = undefined;
3050
3216
  [accountIndex, params] = await this.handleAccountIndex(params, 'setMargin', 'accountIndex', 'account_index');
3217
+ const strAccountIndex = this.numberToString(accountIndex);
3218
+ const strApiKeyIndex = this.numberToString(apiKeyIndex);
3219
+ const signer = await this.loadAccount(this.options['chainId'], this.getLighterPrivateKey(strAccountIndex, strApiKeyIndex), strApiKeyIndex, strAccountIndex, params);
3051
3220
  const market = this.market(symbol);
3052
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3221
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
3053
3222
  const signRaw = {
3054
3223
  'market_index': this.parseToInt(market['id']),
3055
3224
  'usdc_amount': this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision('USDC', amount))),
@@ -3058,7 +3227,6 @@ class lighter extends lighter$1["default"] {
3058
3227
  'api_key_index': apiKeyIndex,
3059
3228
  'account_index': accountIndex,
3060
3229
  };
3061
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
3062
3230
  const [txType, txInfo] = this.lighterSignUpdateMargin(signer, this.extend(signRaw, params));
3063
3231
  const request = {
3064
3232
  'tx_type': txType,