ccxt 4.5.49 → 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.
@@ -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,
@@ -1906,11 +2081,10 @@ class lighter extends lighter$1["default"] {
1906
2081
  let accountIndex = undefined;
1907
2082
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchOpenOrders', 'accountIndex', 'account_index');
1908
2083
  let apiKeyIndex = undefined;
1909
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchOpenOrders', 'apiKeyIndex', 'api_key_index');
1910
- if (apiKeyIndex === undefined) {
1911
- throw new errors.ArgumentsRequired(this.id + ' fetchOpenOrders() requires an apiKeyIndex parameter');
1912
- }
1913
- 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);
1914
2088
  const market = this.market(symbol);
1915
2089
  const request = {
1916
2090
  'market_id': market['id'],
@@ -1982,11 +2156,10 @@ class lighter extends lighter$1["default"] {
1982
2156
  let accountIndex = undefined;
1983
2157
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchClosedOrders', 'accountIndex', 'account_index');
1984
2158
  let apiKeyIndex = undefined;
1985
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchClosedOrders', 'apiKeyIndex', 'api_key_index');
1986
- if (apiKeyIndex === undefined) {
1987
- throw new errors.ArgumentsRequired(this.id + ' fetchClosedOrders() requires an apiKeyIndex parameter');
1988
- }
1989
- 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);
1990
2163
  const market = this.market(symbol);
1991
2164
  const request = {
1992
2165
  'market_id': market['id'],
@@ -2240,16 +2413,16 @@ class lighter extends lighter$1["default"] {
2240
2413
  * @returns {object} a [transfer structure]{@link https://docs.ccxt.com/?id=transfer-structure}
2241
2414
  */
2242
2415
  async transfer(code, amount, fromAccount, toAccount, params = {}) {
2243
- let apiKeyIndex = undefined;
2244
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'transfer', 'apiKeyIndex', 'api_key_index');
2245
- if (apiKeyIndex === undefined) {
2246
- throw new errors.ArgumentsRequired(this.id + ' transfer() requires an apiKeyIndex parameter');
2247
- }
2248
2416
  await this.loadMarkets();
2417
+ let apiKeyIndex = undefined;
2418
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'transfer', 'apiKeyIndex', 'api_key_index');
2249
2419
  let accountIndex = undefined;
2250
2420
  [accountIndex, params] = await this.handleAccountIndex(params, 'transfer', 'accountIndex', 'account_index');
2251
2421
  let toAccountIndex = undefined;
2252
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);
2253
2426
  const currency = this.currency(code);
2254
2427
  if (currency['code'] === 'USDC') {
2255
2428
  amount = this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision(code, amount)));
@@ -2262,9 +2435,9 @@ class lighter extends lighter$1["default"] {
2262
2435
  }
2263
2436
  const fromRouteType = (fromAccount === 'perp') ? 0 : 1; // 0: perp, 1: spot
2264
2437
  const toRouteType = (toAccount === 'perp') ? 0 : 1;
2265
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
2266
2438
  const memo = this.safeString(params, 'memo', '0x000000000000000000000000000000');
2267
2439
  params = this.omit(params, ['memo']);
2440
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2268
2441
  const signRaw = {
2269
2442
  'to_account_index': toAccountIndex,
2270
2443
  'asset_index': this.parseToInt(currency['id']),
@@ -2277,7 +2450,6 @@ class lighter extends lighter$1["default"] {
2277
2450
  'api_key_index': apiKeyIndex,
2278
2451
  'account_index': accountIndex,
2279
2452
  };
2280
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2281
2453
  const [txType, txInfo] = this.lighterSignTransfer(signer, this.extend(signRaw, params));
2282
2454
  const request = {
2283
2455
  'tx_type': txType,
@@ -2300,6 +2472,7 @@ class lighter extends lighter$1["default"] {
2300
2472
  * @returns {object[]} a list of [transfer structures]{@link https://docs.ccxt.com/?id=transfer-structure}
2301
2473
  */
2302
2474
  async fetchTransfers(code = undefined, since = undefined, limit = undefined, params = {}) {
2475
+ await this.loadMarkets();
2303
2476
  let paginate = false;
2304
2477
  [paginate, params] = this.handleOptionAndParams(params, 'fetchTransfers', 'paginate');
2305
2478
  if (paginate) {
@@ -2311,11 +2484,10 @@ class lighter extends lighter$1["default"] {
2311
2484
  'account_index': accountIndex,
2312
2485
  };
2313
2486
  let apiKeyIndex = undefined;
2314
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchTransfers', 'apiKeyIndex', 'api_key_index');
2315
- if (apiKeyIndex === undefined) {
2316
- throw new errors.ArgumentsRequired(this.id + ' fetchTransfers() requires an apiKeyIndex parameter');
2317
- }
2318
- 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);
2319
2491
  let currency = undefined;
2320
2492
  if (code !== undefined) {
2321
2493
  currency = this.currency(code);
@@ -2402,6 +2574,7 @@ class lighter extends lighter$1["default"] {
2402
2574
  * @returns {object[]} a list of [transaction structures]{@link https://docs.ccxt.com/?id=transaction-structure}
2403
2575
  */
2404
2576
  async fetchDeposits(code = undefined, since = undefined, limit = undefined, params = {}) {
2577
+ await this.loadMarkets();
2405
2578
  let paginate = false;
2406
2579
  [paginate, params] = this.handleOptionAndParams(params, 'fetchDeposits', 'paginate');
2407
2580
  if (paginate) {
@@ -2412,7 +2585,6 @@ class lighter extends lighter$1["default"] {
2412
2585
  if (address === undefined) {
2413
2586
  throw new errors.ArgumentsRequired(this.id + ' fetchDeposits() requires an address parameter');
2414
2587
  }
2415
- await this.loadMarkets();
2416
2588
  let accountIndex = undefined;
2417
2589
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchDeposits', 'accountIndex', 'account_index');
2418
2590
  const request = {
@@ -2420,11 +2592,10 @@ class lighter extends lighter$1["default"] {
2420
2592
  'l1_address': address,
2421
2593
  };
2422
2594
  let apiKeyIndex = undefined;
2423
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchDeposits', 'apiKeyIndex', 'api_key_index');
2424
- if (apiKeyIndex === undefined) {
2425
- throw new errors.ArgumentsRequired(this.id + ' fetchDeposits() requires an apiKeyIndex parameter');
2426
- }
2427
- 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);
2428
2599
  let currency = undefined;
2429
2600
  if (code !== undefined) {
2430
2601
  currency = this.currency(code);
@@ -2481,11 +2652,10 @@ class lighter extends lighter$1["default"] {
2481
2652
  'account_index': accountIndex,
2482
2653
  };
2483
2654
  let apiKeyIndex = undefined;
2484
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchWithdrawals', 'apiKeyIndex', 'api_key_index');
2485
- if (apiKeyIndex === undefined) {
2486
- throw new errors.ArgumentsRequired(this.id + ' fetchWithdrawals() requires an apiKeyIndex parameter');
2487
- }
2488
- 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);
2489
2659
  let currency = undefined;
2490
2660
  if (code !== undefined) {
2491
2661
  currency = this.currency(code);
@@ -2595,14 +2765,14 @@ class lighter extends lighter$1["default"] {
2595
2765
  * @returns {object} a [transaction structure]{@link https://docs.ccxt.com/?id=transaction-structure}
2596
2766
  */
2597
2767
  async withdraw(code, amount, address, tag = undefined, params = {}) {
2598
- let apiKeyIndex = undefined;
2599
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'withdraw', 'apiKeyIndex', 'api_key_index');
2600
- if (apiKeyIndex === undefined) {
2601
- throw new errors.ArgumentsRequired(this.id + ' withdraw() requires an apiKeyIndex parameter');
2602
- }
2603
2768
  await this.loadMarkets();
2769
+ let apiKeyIndex = undefined;
2770
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'withdraw', 'apiKeyIndex', 'api_key_index');
2604
2771
  let accountIndex = undefined;
2605
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);
2606
2776
  const currency = this.currency(code);
2607
2777
  if (currency['code'] === 'USDC') {
2608
2778
  amount = this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision(code, amount)));
@@ -2615,7 +2785,7 @@ class lighter extends lighter$1["default"] {
2615
2785
  }
2616
2786
  const routeType = this.safeInteger(params, 'routeType', 0); // 0: perp, 1: spot
2617
2787
  params = this.omit(params, 'routeType');
2618
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
2788
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2619
2789
  const signRaw = {
2620
2790
  'asset_index': this.parseToInt(currency['id']),
2621
2791
  'route_type': routeType,
@@ -2624,7 +2794,6 @@ class lighter extends lighter$1["default"] {
2624
2794
  'api_key_index': apiKeyIndex,
2625
2795
  'account_index': accountIndex,
2626
2796
  };
2627
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2628
2797
  const [txType, txInfo] = this.lighterSignWithdraw(signer, this.extend(signRaw, params));
2629
2798
  const request = {
2630
2799
  'tx_type': txType,
@@ -2657,11 +2826,10 @@ class lighter extends lighter$1["default"] {
2657
2826
  let accountIndex = undefined;
2658
2827
  [accountIndex, params] = await this.handleAccountIndex(params, 'fetchMyTrades', 'accountIndex', 'account_index');
2659
2828
  let apiKeyIndex = undefined;
2660
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'fetchMyTrades', 'apiKeyIndex', 'api_key_index');
2661
- if (apiKeyIndex === undefined) {
2662
- throw new errors.ArgumentsRequired(this.id + ' fetchMyTrades() requires an apiKeyIndex parameter');
2663
- }
2664
- 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);
2665
2833
  const request = {
2666
2834
  'sort_by': 'timestamp',
2667
2835
  'limit': 100,
@@ -2837,22 +3005,22 @@ class lighter extends lighter$1["default"] {
2837
3005
  return await this.modifyLeverageAndMarginMode(leverage, marginMode, symbol, params);
2838
3006
  }
2839
3007
  async modifyLeverageAndMarginMode(leverage, marginMode, symbol = undefined, params = {}) {
3008
+ await this.loadMarkets();
2840
3009
  if ((marginMode !== 'cross') && (marginMode !== 'isolated')) {
2841
3010
  throw new errors.BadRequest(this.id + ' modifyLeverageAndMarginMode() requires a marginMode parameter that must be either cross or isolated');
2842
3011
  }
2843
3012
  let apiKeyIndex = undefined;
2844
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'modifyLeverageAndMarginMode', 'apiKeyIndex', 'api_key_index');
2845
- if (apiKeyIndex === undefined) {
2846
- throw new errors.ArgumentsRequired(this.id + ' modifyLeverageAndMarginMode() requires an apiKeyIndex parameter');
2847
- }
3013
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'modifyLeverageAndMarginMode', 'apiKeyIndex', 'api_key_index');
2848
3014
  if (symbol === undefined) {
2849
3015
  throw new errors.ArgumentsRequired(this.id + ' modifyLeverageAndMarginMode() requires a symbol argument');
2850
3016
  }
2851
- await this.loadMarkets();
2852
3017
  let accountIndex = undefined;
2853
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);
2854
3022
  const market = this.market(symbol);
2855
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3023
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2856
3024
  const signRaw = {
2857
3025
  'market_index': this.parseToInt(market['id']),
2858
3026
  'initial_margin_fraction': this.parseToInt(10000 / leverage),
@@ -2861,7 +3029,6 @@ class lighter extends lighter$1["default"] {
2861
3029
  'api_key_index': apiKeyIndex,
2862
3030
  'account_index': accountIndex,
2863
3031
  };
2864
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2865
3032
  const [txType, txInfo] = this.lighterSignUpdateLeverage(signer, this.extend(signRaw, params));
2866
3033
  const request = {
2867
3034
  'tx_type': txType,
@@ -2881,21 +3048,21 @@ class lighter extends lighter$1["default"] {
2881
3048
  * @returns {object} an [order structure]{@link https://docs.ccxt.com/?id=order-structure}
2882
3049
  */
2883
3050
  async cancelOrder(id, symbol = undefined, params = {}) {
3051
+ await this.loadMarkets();
2884
3052
  let apiKeyIndex = undefined;
2885
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2886
- if (apiKeyIndex === undefined) {
2887
- throw new errors.ArgumentsRequired(this.id + ' cancelOrder() requires an apiKeyIndex parameter');
2888
- }
3053
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2889
3054
  if (symbol === undefined) {
2890
3055
  throw new errors.ArgumentsRequired(this.id + ' cancelOrder() requires a symbol argument');
2891
3056
  }
3057
+ const market = this.market(symbol);
2892
3058
  const clientOrderId = this.safeString2(params, 'client_order_index', 'clientOrderId');
2893
3059
  params = this.omit(params, ['client_order_index', 'clientOrderId']);
2894
- await this.loadMarkets();
2895
3060
  let accountIndex = undefined;
2896
3061
  [accountIndex, params] = await this.handleAccountIndex(params, 'cancelOrder', 'accountIndex', 'account_index');
2897
- const market = this.market(symbol);
2898
- 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);
2899
3066
  const signRaw = {
2900
3067
  'market_index': this.parseToInt(market['id']),
2901
3068
  'nonce': nonce,
@@ -2911,7 +3078,6 @@ class lighter extends lighter$1["default"] {
2911
3078
  else {
2912
3079
  throw new errors.ArgumentsRequired(this.id + ' cancelOrder requires order id or client order id');
2913
3080
  }
2914
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2915
3081
  const [txType, txInfo] = this.lighterSignCancelOrder(signer, this.extend(signRaw, params));
2916
3082
  const request = {
2917
3083
  'tx_type': txType,
@@ -2931,13 +3097,14 @@ class lighter extends lighter$1["default"] {
2931
3097
  * @returns {object[]} a list of [order structures]{@link https://docs.ccxt.com/?id=order-structure}
2932
3098
  */
2933
3099
  async cancelAllOrders(symbol = undefined, params = {}) {
3100
+ await this.loadMarkets();
2934
3101
  let apiKeyIndex = undefined;
2935
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelAllOrders', 'apiKeyIndex', 'api_key_index');
2936
- if (apiKeyIndex === undefined) {
2937
- throw new errors.ArgumentsRequired(this.id + ' cancelAllOrders() requires an apiKeyIndex parameter');
2938
- }
3102
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelAllOrders', 'apiKeyIndex', 'api_key_index');
2939
3103
  let accountIndex = undefined;
2940
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);
2941
3108
  const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
2942
3109
  const signRaw = {
2943
3110
  'time_in_force': 0,
@@ -2946,7 +3113,6 @@ class lighter extends lighter$1["default"] {
2946
3113
  'api_key_index': apiKeyIndex,
2947
3114
  'account_index': accountIndex,
2948
3115
  };
2949
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2950
3116
  const [txType, txInfo] = this.lighterSignCancelAllOrders(signer, this.extend(signRaw, params));
2951
3117
  const request = {
2952
3118
  'tx_type': txType,
@@ -2964,17 +3130,18 @@ class lighter extends lighter$1["default"] {
2964
3130
  * @returns {object} the api result
2965
3131
  */
2966
3132
  async cancelAllOrdersAfter(timeout, params = {}) {
3133
+ await this.loadMarkets();
2967
3134
  if ((timeout < 300000) || (timeout > 1296000000)) {
2968
3135
  throw new errors.BadRequest(this.id + ' timeout should be between 5 minutes and 15 days.');
2969
3136
  }
2970
3137
  let apiKeyIndex = undefined;
2971
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2972
- if (apiKeyIndex === undefined) {
2973
- throw new errors.ArgumentsRequired(this.id + ' cancelAllOrdersAfter() requires an apiKeyIndex parameter');
2974
- }
3138
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'cancelOrder', 'apiKeyIndex', 'api_key_index');
2975
3139
  let accountIndex = undefined;
2976
3140
  [accountIndex, params] = await this.handleAccountIndex(params, 'cancelAllOrdersAfter', 'accountIndex', 'account_index');
2977
- 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);
2978
3145
  const signRaw = {
2979
3146
  'time_in_force': 1,
2980
3147
  'time': this.milliseconds() + timeout,
@@ -2982,7 +3149,6 @@ class lighter extends lighter$1["default"] {
2982
3149
  'api_key_index': apiKeyIndex,
2983
3150
  'account_index': accountIndex,
2984
3151
  };
2985
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
2986
3152
  const [txType, txInfo] = this.lighterSignCancelAllOrders(signer, this.extend(signRaw, params));
2987
3153
  const request = {
2988
3154
  'tx_type': txType,
@@ -3033,11 +3199,9 @@ class lighter extends lighter$1["default"] {
3033
3199
  * @returns {object} A [margin structure]{@link https://docs.ccxt.com/?id=add-margin-structure}
3034
3200
  */
3035
3201
  async setMargin(symbol, amount, params = {}) {
3202
+ await this.loadMarkets();
3036
3203
  let apiKeyIndex = undefined;
3037
- [apiKeyIndex, params] = this.handleOptionAndParams2(params, 'setMargin', 'apiKeyIndex', 'api_key_index');
3038
- if (apiKeyIndex === undefined) {
3039
- throw new errors.ArgumentsRequired(this.id + ' setMargin() requires an apiKeyIndex parameter');
3040
- }
3204
+ [apiKeyIndex, params] = this.handleApiKeyIndex(params, 'setMargin', 'apiKeyIndex', 'api_key_index');
3041
3205
  const direction = this.safeInteger(params, 'direction'); // 1 increase margin 0 decrease margin
3042
3206
  if (direction === undefined) {
3043
3207
  throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a direction parameter either 1 (increase margin) or 0 (decrease margin)');
@@ -3048,11 +3212,13 @@ class lighter extends lighter$1["default"] {
3048
3212
  if (symbol === undefined) {
3049
3213
  throw new errors.ArgumentsRequired(this.id + ' setMargin() requires a symbol argument');
3050
3214
  }
3051
- await this.loadMarkets();
3052
3215
  let accountIndex = undefined;
3053
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);
3054
3220
  const market = this.market(symbol);
3055
- const nonce = await this.fetchNonce(accountIndex, apiKeyIndex);
3221
+ const nonce = await this.fetchNonce(accountIndex, apiKeyIndex, params);
3056
3222
  const signRaw = {
3057
3223
  'market_index': this.parseToInt(market['id']),
3058
3224
  'usdc_amount': this.parseToInt(Precise["default"].stringMul(this.pow('10', '6'), this.currencyToPrecision('USDC', amount))),
@@ -3061,7 +3227,6 @@ class lighter extends lighter$1["default"] {
3061
3227
  'api_key_index': apiKeyIndex,
3062
3228
  'account_index': accountIndex,
3063
3229
  };
3064
- const signer = await this.loadAccount(this.options['chainId'], this.privateKey, apiKeyIndex, accountIndex, params);
3065
3230
  const [txType, txInfo] = this.lighterSignUpdateMargin(signer, this.extend(signRaw, params));
3066
3231
  const request = {
3067
3232
  'tx_type': txType,