@cetusprotocol/aggregator-sdk 0.0.7 → 0.1.0

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 (57) hide show
  1. package/dist/index.d.mts +98 -102
  2. package/dist/index.d.ts +98 -102
  3. package/dist/index.js +1228 -1556
  4. package/dist/index.mjs +1215 -1543
  5. package/dist/src/api.d.ts +53 -0
  6. package/dist/src/client.d.ts +38 -65
  7. package/dist/src/const.d.ts +0 -9
  8. package/dist/src/index.d.ts +9 -5
  9. package/dist/src/transaction/afsui.d.ts +10 -0
  10. package/dist/src/transaction/aftermath.d.ts +13 -24
  11. package/dist/src/transaction/cetus.d.ts +9 -33
  12. package/dist/src/transaction/deepbook_v2.d.ts +14 -0
  13. package/dist/src/transaction/flowx_v2.d.ts +7 -0
  14. package/dist/src/transaction/haedal.d.ts +6 -0
  15. package/dist/src/transaction/index.d.ts +6 -1
  16. package/dist/src/transaction/kriya_v2.d.ts +6 -0
  17. package/dist/src/transaction/kriya_v3.d.ts +7 -0
  18. package/dist/src/transaction/swap.d.ts +1 -2
  19. package/dist/src/transaction/turbos.d.ts +7 -22
  20. package/dist/src/transaction/volo.d.ts +8 -0
  21. package/dist/src/utils/coin.d.ts +9 -0
  22. package/dist/{src → tests}/test_data.test.d.ts +1 -0
  23. package/package.json +2 -2
  24. package/src/api.ts +144 -0
  25. package/src/client.ts +295 -239
  26. package/src/const.ts +0 -13
  27. package/src/index.ts +10 -5
  28. package/src/transaction/afsui.ts +60 -0
  29. package/src/transaction/aftermath.ts +71 -124
  30. package/src/transaction/cetus.ts +87 -258
  31. package/src/transaction/deepbook_v2.ts +122 -0
  32. package/src/transaction/flowx_v2.ts +42 -0
  33. package/src/transaction/haedal.ts +41 -0
  34. package/src/transaction/index.ts +17 -1
  35. package/src/transaction/kriya_v2.ts +38 -0
  36. package/src/transaction/kriya_v3.ts +46 -0
  37. package/src/transaction/swap.ts +17 -31
  38. package/src/transaction/turbos.ts +40 -99
  39. package/src/transaction/volo.ts +52 -0
  40. package/src/utils/coin.ts +91 -6
  41. package/src/utils/transaction.ts +1 -1
  42. package/tests/router.test.ts +127 -81
  43. package/{src → tests}/test_data.test.ts +2 -0
  44. package/dist/src/config.d.ts +0 -26
  45. package/dist/src/transaction/common.d.ts +0 -12
  46. package/dist/src/transaction/deepbook.d.ts +0 -21
  47. package/dist/src/transaction/flowx.d.ts +0 -20
  48. package/dist/src/transaction/kriya.d.ts +0 -21
  49. package/dist/src/transaction/router.d.ts +0 -6
  50. package/dist/src/utils/account_cap.d.ts +0 -7
  51. package/src/config.ts +0 -65
  52. package/src/transaction/common.ts +0 -174
  53. package/src/transaction/deepbook.ts +0 -126
  54. package/src/transaction/flowx.ts +0 -97
  55. package/src/transaction/kriya.ts +0 -77
  56. package/src/transaction/router.ts +0 -341
  57. package/src/utils/account_cap.ts +0 -62
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var client = require('@mysten/sui/client');
4
3
  var utils = require('@mysten/sui/utils');
5
4
  var transactions = require('@mysten/sui/transactions');
6
5
 
@@ -88,8 +87,8 @@ var require_bn = __commonJS({
88
87
  ctor.prototype = new TempCtor();
89
88
  ctor.prototype.constructor = ctor;
90
89
  }
91
- function BN5(number, base, endian) {
92
- if (BN5.isBN(number)) {
90
+ function BN6(number, base, endian) {
91
+ if (BN6.isBN(number)) {
93
92
  return number;
94
93
  }
95
94
  this.negative = 0;
@@ -105,12 +104,12 @@ var require_bn = __commonJS({
105
104
  }
106
105
  }
107
106
  if (typeof module2 === "object") {
108
- module2.exports = BN5;
107
+ module2.exports = BN6;
109
108
  } else {
110
- exports2.BN = BN5;
109
+ exports2.BN = BN6;
111
110
  }
112
- BN5.BN = BN5;
113
- BN5.wordSize = 26;
111
+ BN6.BN = BN6;
112
+ BN6.wordSize = 26;
114
113
  var Buffer2;
115
114
  try {
116
115
  if (typeof window !== "undefined" && typeof window.Buffer !== "undefined") {
@@ -120,21 +119,21 @@ var require_bn = __commonJS({
120
119
  }
121
120
  } catch (e) {
122
121
  }
123
- BN5.isBN = function isBN(num) {
124
- if (num instanceof BN5) {
122
+ BN6.isBN = function isBN(num) {
123
+ if (num instanceof BN6) {
125
124
  return true;
126
125
  }
127
- return num !== null && typeof num === "object" && num.constructor.wordSize === BN5.wordSize && Array.isArray(num.words);
126
+ return num !== null && typeof num === "object" && num.constructor.wordSize === BN6.wordSize && Array.isArray(num.words);
128
127
  };
129
- BN5.max = function max2(left, right) {
128
+ BN6.max = function max2(left, right) {
130
129
  if (left.cmp(right) > 0) return left;
131
130
  return right;
132
131
  };
133
- BN5.min = function min2(left, right) {
132
+ BN6.min = function min2(left, right) {
134
133
  if (left.cmp(right) < 0) return left;
135
134
  return right;
136
135
  };
137
- BN5.prototype._init = function init(number, base, endian) {
136
+ BN6.prototype._init = function init(number, base, endian) {
138
137
  if (typeof number === "number") {
139
138
  return this._initNumber(number, base, endian);
140
139
  }
@@ -162,7 +161,7 @@ var require_bn = __commonJS({
162
161
  }
163
162
  }
164
163
  };
165
- BN5.prototype._initNumber = function _initNumber(number, base, endian) {
164
+ BN6.prototype._initNumber = function _initNumber(number, base, endian) {
166
165
  if (number < 0) {
167
166
  this.negative = 1;
168
167
  number = -number;
@@ -188,7 +187,7 @@ var require_bn = __commonJS({
188
187
  if (endian !== "le") return;
189
188
  this._initArray(this.toArray(), base, endian);
190
189
  };
191
- BN5.prototype._initArray = function _initArray(number, base, endian) {
190
+ BN6.prototype._initArray = function _initArray(number, base, endian) {
192
191
  assert(typeof number.length === "number");
193
192
  if (number.length <= 0) {
194
193
  this.words = [0];
@@ -246,7 +245,7 @@ var require_bn = __commonJS({
246
245
  }
247
246
  return r;
248
247
  }
249
- BN5.prototype._parseHex = function _parseHex(number, start, endian) {
248
+ BN6.prototype._parseHex = function _parseHex(number, start, endian) {
250
249
  this.length = Math.ceil((number.length - start) / 6);
251
250
  this.words = new Array(this.length);
252
251
  for (var i = 0; i < this.length; i++) {
@@ -302,7 +301,7 @@ var require_bn = __commonJS({
302
301
  }
303
302
  return r;
304
303
  }
305
- BN5.prototype._parseBase = function _parseBase(number, base, start) {
304
+ BN6.prototype._parseBase = function _parseBase(number, base, start) {
306
305
  this.words = [0];
307
306
  this.length = 1;
308
307
  for (var limbLen = 0, limbPow = 1; limbPow <= 67108863; limbPow *= base) {
@@ -338,7 +337,7 @@ var require_bn = __commonJS({
338
337
  }
339
338
  this._strip();
340
339
  };
341
- BN5.prototype.copy = function copy(dest) {
340
+ BN6.prototype.copy = function copy(dest) {
342
341
  dest.words = new Array(this.length);
343
342
  for (var i = 0; i < this.length; i++) {
344
343
  dest.words[i] = this.words[i];
@@ -353,27 +352,27 @@ var require_bn = __commonJS({
353
352
  dest.negative = src.negative;
354
353
  dest.red = src.red;
355
354
  }
356
- BN5.prototype._move = function _move(dest) {
355
+ BN6.prototype._move = function _move(dest) {
357
356
  move(dest, this);
358
357
  };
359
- BN5.prototype.clone = function clone2() {
360
- var r = new BN5(null);
358
+ BN6.prototype.clone = function clone2() {
359
+ var r = new BN6(null);
361
360
  this.copy(r);
362
361
  return r;
363
362
  };
364
- BN5.prototype._expand = function _expand(size) {
363
+ BN6.prototype._expand = function _expand(size) {
365
364
  while (this.length < size) {
366
365
  this.words[this.length++] = 0;
367
366
  }
368
367
  return this;
369
368
  };
370
- BN5.prototype._strip = function strip() {
369
+ BN6.prototype._strip = function strip() {
371
370
  while (this.length > 1 && this.words[this.length - 1] === 0) {
372
371
  this.length--;
373
372
  }
374
373
  return this._normSign();
375
374
  };
376
- BN5.prototype._normSign = function _normSign() {
375
+ BN6.prototype._normSign = function _normSign() {
377
376
  if (this.length === 1 && this.words[0] === 0) {
378
377
  this.negative = 0;
379
378
  }
@@ -381,12 +380,12 @@ var require_bn = __commonJS({
381
380
  };
382
381
  if (typeof Symbol !== "undefined" && typeof Symbol.for === "function") {
383
382
  try {
384
- BN5.prototype[Symbol.for("nodejs.util.inspect.custom")] = inspect;
383
+ BN6.prototype[Symbol.for("nodejs.util.inspect.custom")] = inspect;
385
384
  } catch (e) {
386
- BN5.prototype.inspect = inspect;
385
+ BN6.prototype.inspect = inspect;
387
386
  }
388
387
  } else {
389
- BN5.prototype.inspect = inspect;
388
+ BN6.prototype.inspect = inspect;
390
389
  }
391
390
  function inspect() {
392
391
  return (this.red ? "<BN-R: " : "<BN: ") + this.toString(16) + ">";
@@ -497,7 +496,7 @@ var require_bn = __commonJS({
497
496
  52521875,
498
497
  60466176
499
498
  ];
500
- BN5.prototype.toString = function toString(base, padding) {
499
+ BN6.prototype.toString = function toString(base, padding) {
501
500
  base = base || 10;
502
501
  padding = padding | 0 || 1;
503
502
  var out;
@@ -559,7 +558,7 @@ var require_bn = __commonJS({
559
558
  }
560
559
  assert(false, "Base should be between 2 and 36");
561
560
  };
562
- BN5.prototype.toNumber = function toNumber() {
561
+ BN6.prototype.toNumber = function toNumber() {
563
562
  var ret = this.words[0];
564
563
  if (this.length === 2) {
565
564
  ret += this.words[1] * 67108864;
@@ -570,15 +569,15 @@ var require_bn = __commonJS({
570
569
  }
571
570
  return this.negative !== 0 ? -ret : ret;
572
571
  };
573
- BN5.prototype.toJSON = function toJSON() {
572
+ BN6.prototype.toJSON = function toJSON() {
574
573
  return this.toString(16, 2);
575
574
  };
576
575
  if (Buffer2) {
577
- BN5.prototype.toBuffer = function toBuffer(endian, length) {
576
+ BN6.prototype.toBuffer = function toBuffer(endian, length) {
578
577
  return this.toArrayLike(Buffer2, endian, length);
579
578
  };
580
579
  }
581
- BN5.prototype.toArray = function toArray(endian, length) {
580
+ BN6.prototype.toArray = function toArray(endian, length) {
582
581
  return this.toArrayLike(Array, endian, length);
583
582
  };
584
583
  var allocate = function allocate2(ArrayType, size) {
@@ -587,7 +586,7 @@ var require_bn = __commonJS({
587
586
  }
588
587
  return new ArrayType(size);
589
588
  };
590
- BN5.prototype.toArrayLike = function toArrayLike(ArrayType, endian, length) {
589
+ BN6.prototype.toArrayLike = function toArrayLike(ArrayType, endian, length) {
591
590
  this._strip();
592
591
  var byteLength = this.byteLength();
593
592
  var reqLength = length || Math.max(1, byteLength);
@@ -598,7 +597,7 @@ var require_bn = __commonJS({
598
597
  this["_toArrayLike" + postfix](res, byteLength);
599
598
  return res;
600
599
  };
601
- BN5.prototype._toArrayLikeLE = function _toArrayLikeLE(res, byteLength) {
600
+ BN6.prototype._toArrayLikeLE = function _toArrayLikeLE(res, byteLength) {
602
601
  var position = 0;
603
602
  var carry = 0;
604
603
  for (var i = 0, shift = 0; i < this.length; i++) {
@@ -628,7 +627,7 @@ var require_bn = __commonJS({
628
627
  }
629
628
  }
630
629
  };
631
- BN5.prototype._toArrayLikeBE = function _toArrayLikeBE(res, byteLength) {
630
+ BN6.prototype._toArrayLikeBE = function _toArrayLikeBE(res, byteLength) {
632
631
  var position = res.length - 1;
633
632
  var carry = 0;
634
633
  for (var i = 0, shift = 0; i < this.length; i++) {
@@ -659,11 +658,11 @@ var require_bn = __commonJS({
659
658
  }
660
659
  };
661
660
  if (Math.clz32) {
662
- BN5.prototype._countBits = function _countBits(w) {
661
+ BN6.prototype._countBits = function _countBits(w) {
663
662
  return 32 - Math.clz32(w);
664
663
  };
665
664
  } else {
666
- BN5.prototype._countBits = function _countBits(w) {
665
+ BN6.prototype._countBits = function _countBits(w) {
667
666
  var t = w;
668
667
  var r = 0;
669
668
  if (t >= 4096) {
@@ -685,7 +684,7 @@ var require_bn = __commonJS({
685
684
  return r + t;
686
685
  };
687
686
  }
688
- BN5.prototype._zeroBits = function _zeroBits(w) {
687
+ BN6.prototype._zeroBits = function _zeroBits(w) {
689
688
  if (w === 0) return 26;
690
689
  var t = w;
691
690
  var r = 0;
@@ -710,7 +709,7 @@ var require_bn = __commonJS({
710
709
  }
711
710
  return r;
712
711
  };
713
- BN5.prototype.bitLength = function bitLength() {
712
+ BN6.prototype.bitLength = function bitLength() {
714
713
  var w = this.words[this.length - 1];
715
714
  var hi = this._countBits(w);
716
715
  return (this.length - 1) * 26 + hi;
@@ -724,7 +723,7 @@ var require_bn = __commonJS({
724
723
  }
725
724
  return w;
726
725
  }
727
- BN5.prototype.zeroBits = function zeroBits() {
726
+ BN6.prototype.zeroBits = function zeroBits() {
728
727
  if (this.isZero()) return 0;
729
728
  var r = 0;
730
729
  for (var i = 0; i < this.length; i++) {
@@ -734,34 +733,34 @@ var require_bn = __commonJS({
734
733
  }
735
734
  return r;
736
735
  };
737
- BN5.prototype.byteLength = function byteLength() {
736
+ BN6.prototype.byteLength = function byteLength() {
738
737
  return Math.ceil(this.bitLength() / 8);
739
738
  };
740
- BN5.prototype.toTwos = function toTwos(width) {
739
+ BN6.prototype.toTwos = function toTwos(width) {
741
740
  if (this.negative !== 0) {
742
741
  return this.abs().inotn(width).iaddn(1);
743
742
  }
744
743
  return this.clone();
745
744
  };
746
- BN5.prototype.fromTwos = function fromTwos(width) {
745
+ BN6.prototype.fromTwos = function fromTwos(width) {
747
746
  if (this.testn(width - 1)) {
748
747
  return this.notn(width).iaddn(1).ineg();
749
748
  }
750
749
  return this.clone();
751
750
  };
752
- BN5.prototype.isNeg = function isNeg() {
751
+ BN6.prototype.isNeg = function isNeg() {
753
752
  return this.negative !== 0;
754
753
  };
755
- BN5.prototype.neg = function neg() {
754
+ BN6.prototype.neg = function neg() {
756
755
  return this.clone().ineg();
757
756
  };
758
- BN5.prototype.ineg = function ineg() {
757
+ BN6.prototype.ineg = function ineg() {
759
758
  if (!this.isZero()) {
760
759
  this.negative ^= 1;
761
760
  }
762
761
  return this;
763
762
  };
764
- BN5.prototype.iuor = function iuor(num) {
763
+ BN6.prototype.iuor = function iuor(num) {
765
764
  while (this.length < num.length) {
766
765
  this.words[this.length++] = 0;
767
766
  }
@@ -770,19 +769,19 @@ var require_bn = __commonJS({
770
769
  }
771
770
  return this._strip();
772
771
  };
773
- BN5.prototype.ior = function ior(num) {
772
+ BN6.prototype.ior = function ior(num) {
774
773
  assert((this.negative | num.negative) === 0);
775
774
  return this.iuor(num);
776
775
  };
777
- BN5.prototype.or = function or(num) {
776
+ BN6.prototype.or = function or(num) {
778
777
  if (this.length > num.length) return this.clone().ior(num);
779
778
  return num.clone().ior(this);
780
779
  };
781
- BN5.prototype.uor = function uor(num) {
780
+ BN6.prototype.uor = function uor(num) {
782
781
  if (this.length > num.length) return this.clone().iuor(num);
783
782
  return num.clone().iuor(this);
784
783
  };
785
- BN5.prototype.iuand = function iuand(num) {
784
+ BN6.prototype.iuand = function iuand(num) {
786
785
  var b;
787
786
  if (this.length > num.length) {
788
787
  b = num;
@@ -795,19 +794,19 @@ var require_bn = __commonJS({
795
794
  this.length = b.length;
796
795
  return this._strip();
797
796
  };
798
- BN5.prototype.iand = function iand(num) {
797
+ BN6.prototype.iand = function iand(num) {
799
798
  assert((this.negative | num.negative) === 0);
800
799
  return this.iuand(num);
801
800
  };
802
- BN5.prototype.and = function and(num) {
801
+ BN6.prototype.and = function and(num) {
803
802
  if (this.length > num.length) return this.clone().iand(num);
804
803
  return num.clone().iand(this);
805
804
  };
806
- BN5.prototype.uand = function uand(num) {
805
+ BN6.prototype.uand = function uand(num) {
807
806
  if (this.length > num.length) return this.clone().iuand(num);
808
807
  return num.clone().iuand(this);
809
808
  };
810
- BN5.prototype.iuxor = function iuxor(num) {
809
+ BN6.prototype.iuxor = function iuxor(num) {
811
810
  var a;
812
811
  var b;
813
812
  if (this.length > num.length) {
@@ -828,19 +827,19 @@ var require_bn = __commonJS({
828
827
  this.length = a.length;
829
828
  return this._strip();
830
829
  };
831
- BN5.prototype.ixor = function ixor(num) {
830
+ BN6.prototype.ixor = function ixor(num) {
832
831
  assert((this.negative | num.negative) === 0);
833
832
  return this.iuxor(num);
834
833
  };
835
- BN5.prototype.xor = function xor(num) {
834
+ BN6.prototype.xor = function xor(num) {
836
835
  if (this.length > num.length) return this.clone().ixor(num);
837
836
  return num.clone().ixor(this);
838
837
  };
839
- BN5.prototype.uxor = function uxor(num) {
838
+ BN6.prototype.uxor = function uxor(num) {
840
839
  if (this.length > num.length) return this.clone().iuxor(num);
841
840
  return num.clone().iuxor(this);
842
841
  };
843
- BN5.prototype.inotn = function inotn(width) {
842
+ BN6.prototype.inotn = function inotn(width) {
844
843
  assert(typeof width === "number" && width >= 0);
845
844
  var bytesNeeded = Math.ceil(width / 26) | 0;
846
845
  var bitsLeft = width % 26;
@@ -856,10 +855,10 @@ var require_bn = __commonJS({
856
855
  }
857
856
  return this._strip();
858
857
  };
859
- BN5.prototype.notn = function notn(width) {
858
+ BN6.prototype.notn = function notn(width) {
860
859
  return this.clone().inotn(width);
861
860
  };
862
- BN5.prototype.setn = function setn(bit, val) {
861
+ BN6.prototype.setn = function setn(bit, val) {
863
862
  assert(typeof bit === "number" && bit >= 0);
864
863
  var off = bit / 26 | 0;
865
864
  var wbit = bit % 26;
@@ -871,7 +870,7 @@ var require_bn = __commonJS({
871
870
  }
872
871
  return this._strip();
873
872
  };
874
- BN5.prototype.iadd = function iadd(num) {
873
+ BN6.prototype.iadd = function iadd(num) {
875
874
  var r;
876
875
  if (this.negative !== 0 && num.negative === 0) {
877
876
  this.negative = 0;
@@ -914,7 +913,7 @@ var require_bn = __commonJS({
914
913
  }
915
914
  return this;
916
915
  };
917
- BN5.prototype.add = function add2(num) {
916
+ BN6.prototype.add = function add2(num) {
918
917
  var res;
919
918
  if (num.negative !== 0 && this.negative === 0) {
920
919
  num.negative = 0;
@@ -930,7 +929,7 @@ var require_bn = __commonJS({
930
929
  if (this.length > num.length) return this.clone().iadd(num);
931
930
  return num.clone().iadd(this);
932
931
  };
933
- BN5.prototype.isub = function isub(num) {
932
+ BN6.prototype.isub = function isub(num) {
934
933
  if (num.negative !== 0) {
935
934
  num.negative = 0;
936
935
  var r = this.iadd(num);
@@ -979,7 +978,7 @@ var require_bn = __commonJS({
979
978
  }
980
979
  return this._strip();
981
980
  };
982
- BN5.prototype.sub = function sub2(num) {
981
+ BN6.prototype.sub = function sub2(num) {
983
982
  return this.clone().isub(num);
984
983
  };
985
984
  function smallMulTo(self, num, out) {
@@ -1607,7 +1606,7 @@ var require_bn = __commonJS({
1607
1606
  function jumboMulTo(self, num, out) {
1608
1607
  return bigMulTo(self, num, out);
1609
1608
  }
1610
- BN5.prototype.mulTo = function mulTo(num, out) {
1609
+ BN6.prototype.mulTo = function mulTo(num, out) {
1611
1610
  var res;
1612
1611
  var len = this.length + num.length;
1613
1612
  if (this.length === 10 && num.length === 10) {
@@ -1621,20 +1620,20 @@ var require_bn = __commonJS({
1621
1620
  }
1622
1621
  return res;
1623
1622
  };
1624
- BN5.prototype.mul = function mul2(num) {
1625
- var out = new BN5(null);
1623
+ BN6.prototype.mul = function mul2(num) {
1624
+ var out = new BN6(null);
1626
1625
  out.words = new Array(this.length + num.length);
1627
1626
  return this.mulTo(num, out);
1628
1627
  };
1629
- BN5.prototype.mulf = function mulf(num) {
1630
- var out = new BN5(null);
1628
+ BN6.prototype.mulf = function mulf(num) {
1629
+ var out = new BN6(null);
1631
1630
  out.words = new Array(this.length + num.length);
1632
1631
  return jumboMulTo(this, num, out);
1633
1632
  };
1634
- BN5.prototype.imul = function imul(num) {
1633
+ BN6.prototype.imul = function imul(num) {
1635
1634
  return this.clone().mulTo(num, this);
1636
1635
  };
1637
- BN5.prototype.imuln = function imuln(num) {
1636
+ BN6.prototype.imuln = function imuln(num) {
1638
1637
  var isNegNum = num < 0;
1639
1638
  if (isNegNum) num = -num;
1640
1639
  assert(typeof num === "number");
@@ -1654,18 +1653,18 @@ var require_bn = __commonJS({
1654
1653
  }
1655
1654
  return isNegNum ? this.ineg() : this;
1656
1655
  };
1657
- BN5.prototype.muln = function muln(num) {
1656
+ BN6.prototype.muln = function muln(num) {
1658
1657
  return this.clone().imuln(num);
1659
1658
  };
1660
- BN5.prototype.sqr = function sqr() {
1659
+ BN6.prototype.sqr = function sqr() {
1661
1660
  return this.mul(this);
1662
1661
  };
1663
- BN5.prototype.isqr = function isqr() {
1662
+ BN6.prototype.isqr = function isqr() {
1664
1663
  return this.imul(this.clone());
1665
1664
  };
1666
- BN5.prototype.pow = function pow2(num) {
1665
+ BN6.prototype.pow = function pow2(num) {
1667
1666
  var w = toBitArray(num);
1668
- if (w.length === 0) return new BN5(1);
1667
+ if (w.length === 0) return new BN6(1);
1669
1668
  var res = this;
1670
1669
  for (var i = 0; i < w.length; i++, res = res.sqr()) {
1671
1670
  if (w[i] !== 0) break;
@@ -1678,7 +1677,7 @@ var require_bn = __commonJS({
1678
1677
  }
1679
1678
  return res;
1680
1679
  };
1681
- BN5.prototype.iushln = function iushln(bits) {
1680
+ BN6.prototype.iushln = function iushln(bits) {
1682
1681
  assert(typeof bits === "number" && bits >= 0);
1683
1682
  var r = bits % 26;
1684
1683
  var s = (bits - r) / 26;
@@ -1708,11 +1707,11 @@ var require_bn = __commonJS({
1708
1707
  }
1709
1708
  return this._strip();
1710
1709
  };
1711
- BN5.prototype.ishln = function ishln(bits) {
1710
+ BN6.prototype.ishln = function ishln(bits) {
1712
1711
  assert(this.negative === 0);
1713
1712
  return this.iushln(bits);
1714
1713
  };
1715
- BN5.prototype.iushrn = function iushrn(bits, hint, extended) {
1714
+ BN6.prototype.iushrn = function iushrn(bits, hint, extended) {
1716
1715
  assert(typeof bits === "number" && bits >= 0);
1717
1716
  var h;
1718
1717
  if (hint) {
@@ -1756,23 +1755,23 @@ var require_bn = __commonJS({
1756
1755
  }
1757
1756
  return this._strip();
1758
1757
  };
1759
- BN5.prototype.ishrn = function ishrn(bits, hint, extended) {
1758
+ BN6.prototype.ishrn = function ishrn(bits, hint, extended) {
1760
1759
  assert(this.negative === 0);
1761
1760
  return this.iushrn(bits, hint, extended);
1762
1761
  };
1763
- BN5.prototype.shln = function shln(bits) {
1762
+ BN6.prototype.shln = function shln(bits) {
1764
1763
  return this.clone().ishln(bits);
1765
1764
  };
1766
- BN5.prototype.ushln = function ushln(bits) {
1765
+ BN6.prototype.ushln = function ushln(bits) {
1767
1766
  return this.clone().iushln(bits);
1768
1767
  };
1769
- BN5.prototype.shrn = function shrn(bits) {
1768
+ BN6.prototype.shrn = function shrn(bits) {
1770
1769
  return this.clone().ishrn(bits);
1771
1770
  };
1772
- BN5.prototype.ushrn = function ushrn(bits) {
1771
+ BN6.prototype.ushrn = function ushrn(bits) {
1773
1772
  return this.clone().iushrn(bits);
1774
1773
  };
1775
- BN5.prototype.testn = function testn(bit) {
1774
+ BN6.prototype.testn = function testn(bit) {
1776
1775
  assert(typeof bit === "number" && bit >= 0);
1777
1776
  var r = bit % 26;
1778
1777
  var s = (bit - r) / 26;
@@ -1781,7 +1780,7 @@ var require_bn = __commonJS({
1781
1780
  var w = this.words[s];
1782
1781
  return !!(w & q);
1783
1782
  };
1784
- BN5.prototype.imaskn = function imaskn(bits) {
1783
+ BN6.prototype.imaskn = function imaskn(bits) {
1785
1784
  assert(typeof bits === "number" && bits >= 0);
1786
1785
  var r = bits % 26;
1787
1786
  var s = (bits - r) / 26;
@@ -1799,10 +1798,10 @@ var require_bn = __commonJS({
1799
1798
  }
1800
1799
  return this._strip();
1801
1800
  };
1802
- BN5.prototype.maskn = function maskn(bits) {
1801
+ BN6.prototype.maskn = function maskn(bits) {
1803
1802
  return this.clone().imaskn(bits);
1804
1803
  };
1805
- BN5.prototype.iaddn = function iaddn(num) {
1804
+ BN6.prototype.iaddn = function iaddn(num) {
1806
1805
  assert(typeof num === "number");
1807
1806
  assert(num < 67108864);
1808
1807
  if (num < 0) return this.isubn(-num);
@@ -1819,7 +1818,7 @@ var require_bn = __commonJS({
1819
1818
  }
1820
1819
  return this._iaddn(num);
1821
1820
  };
1822
- BN5.prototype._iaddn = function _iaddn(num) {
1821
+ BN6.prototype._iaddn = function _iaddn(num) {
1823
1822
  this.words[0] += num;
1824
1823
  for (var i = 0; i < this.length && this.words[i] >= 67108864; i++) {
1825
1824
  this.words[i] -= 67108864;
@@ -1832,7 +1831,7 @@ var require_bn = __commonJS({
1832
1831
  this.length = Math.max(this.length, i + 1);
1833
1832
  return this;
1834
1833
  };
1835
- BN5.prototype.isubn = function isubn(num) {
1834
+ BN6.prototype.isubn = function isubn(num) {
1836
1835
  assert(typeof num === "number");
1837
1836
  assert(num < 67108864);
1838
1837
  if (num < 0) return this.iaddn(-num);
@@ -1854,20 +1853,20 @@ var require_bn = __commonJS({
1854
1853
  }
1855
1854
  return this._strip();
1856
1855
  };
1857
- BN5.prototype.addn = function addn(num) {
1856
+ BN6.prototype.addn = function addn(num) {
1858
1857
  return this.clone().iaddn(num);
1859
1858
  };
1860
- BN5.prototype.subn = function subn(num) {
1859
+ BN6.prototype.subn = function subn(num) {
1861
1860
  return this.clone().isubn(num);
1862
1861
  };
1863
- BN5.prototype.iabs = function iabs() {
1862
+ BN6.prototype.iabs = function iabs() {
1864
1863
  this.negative = 0;
1865
1864
  return this;
1866
1865
  };
1867
- BN5.prototype.abs = function abs2() {
1866
+ BN6.prototype.abs = function abs2() {
1868
1867
  return this.clone().iabs();
1869
1868
  };
1870
- BN5.prototype._ishlnsubmul = function _ishlnsubmul(num, mul2, shift) {
1869
+ BN6.prototype._ishlnsubmul = function _ishlnsubmul(num, mul2, shift) {
1871
1870
  var len = num.length + shift;
1872
1871
  var i;
1873
1872
  this._expand(len);
@@ -1896,7 +1895,7 @@ var require_bn = __commonJS({
1896
1895
  this.negative = 1;
1897
1896
  return this._strip();
1898
1897
  };
1899
- BN5.prototype._wordDiv = function _wordDiv(num, mode) {
1898
+ BN6.prototype._wordDiv = function _wordDiv(num, mode) {
1900
1899
  var shift = this.length - num.length;
1901
1900
  var a = this.clone();
1902
1901
  var b = num;
@@ -1911,7 +1910,7 @@ var require_bn = __commonJS({
1911
1910
  var m = a.length - b.length;
1912
1911
  var q;
1913
1912
  if (mode !== "mod") {
1914
- q = new BN5(null);
1913
+ q = new BN6(null);
1915
1914
  q.length = m + 1;
1916
1915
  q.words = new Array(q.length);
1917
1916
  for (var i = 0; i < q.length; i++) {
@@ -1953,12 +1952,12 @@ var require_bn = __commonJS({
1953
1952
  mod: a
1954
1953
  };
1955
1954
  };
1956
- BN5.prototype.divmod = function divmod(num, mode, positive) {
1955
+ BN6.prototype.divmod = function divmod(num, mode, positive) {
1957
1956
  assert(!num.isZero());
1958
1957
  if (this.isZero()) {
1959
1958
  return {
1960
- div: new BN5(0),
1961
- mod: new BN5(0)
1959
+ div: new BN6(0),
1960
+ mod: new BN6(0)
1962
1961
  };
1963
1962
  }
1964
1963
  var div2, mod2, res;
@@ -2003,7 +2002,7 @@ var require_bn = __commonJS({
2003
2002
  }
2004
2003
  if (num.length > this.length || this.cmp(num) < 0) {
2005
2004
  return {
2006
- div: new BN5(0),
2005
+ div: new BN6(0),
2007
2006
  mod: this
2008
2007
  };
2009
2008
  }
@@ -2017,26 +2016,26 @@ var require_bn = __commonJS({
2017
2016
  if (mode === "mod") {
2018
2017
  return {
2019
2018
  div: null,
2020
- mod: new BN5(this.modrn(num.words[0]))
2019
+ mod: new BN6(this.modrn(num.words[0]))
2021
2020
  };
2022
2021
  }
2023
2022
  return {
2024
2023
  div: this.divn(num.words[0]),
2025
- mod: new BN5(this.modrn(num.words[0]))
2024
+ mod: new BN6(this.modrn(num.words[0]))
2026
2025
  };
2027
2026
  }
2028
2027
  return this._wordDiv(num, mode);
2029
2028
  };
2030
- BN5.prototype.div = function div2(num) {
2029
+ BN6.prototype.div = function div2(num) {
2031
2030
  return this.divmod(num, "div", false).div;
2032
2031
  };
2033
- BN5.prototype.mod = function mod2(num) {
2032
+ BN6.prototype.mod = function mod2(num) {
2034
2033
  return this.divmod(num, "mod", false).mod;
2035
2034
  };
2036
- BN5.prototype.umod = function umod(num) {
2035
+ BN6.prototype.umod = function umod(num) {
2037
2036
  return this.divmod(num, "mod", true).mod;
2038
2037
  };
2039
- BN5.prototype.divRound = function divRound(num) {
2038
+ BN6.prototype.divRound = function divRound(num) {
2040
2039
  var dm = this.divmod(num);
2041
2040
  if (dm.mod.isZero()) return dm.div;
2042
2041
  var mod2 = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
@@ -2046,7 +2045,7 @@ var require_bn = __commonJS({
2046
2045
  if (cmp2 < 0 || r2 === 1 && cmp2 === 0) return dm.div;
2047
2046
  return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
2048
2047
  };
2049
- BN5.prototype.modrn = function modrn(num) {
2048
+ BN6.prototype.modrn = function modrn(num) {
2050
2049
  var isNegNum = num < 0;
2051
2050
  if (isNegNum) num = -num;
2052
2051
  assert(num <= 67108863);
@@ -2057,10 +2056,10 @@ var require_bn = __commonJS({
2057
2056
  }
2058
2057
  return isNegNum ? -acc : acc;
2059
2058
  };
2060
- BN5.prototype.modn = function modn(num) {
2059
+ BN6.prototype.modn = function modn(num) {
2061
2060
  return this.modrn(num);
2062
2061
  };
2063
- BN5.prototype.idivn = function idivn(num) {
2062
+ BN6.prototype.idivn = function idivn(num) {
2064
2063
  var isNegNum = num < 0;
2065
2064
  if (isNegNum) num = -num;
2066
2065
  assert(num <= 67108863);
@@ -2073,10 +2072,10 @@ var require_bn = __commonJS({
2073
2072
  this._strip();
2074
2073
  return isNegNum ? this.ineg() : this;
2075
2074
  };
2076
- BN5.prototype.divn = function divn(num) {
2075
+ BN6.prototype.divn = function divn(num) {
2077
2076
  return this.clone().idivn(num);
2078
2077
  };
2079
- BN5.prototype.egcd = function egcd(p) {
2078
+ BN6.prototype.egcd = function egcd(p) {
2080
2079
  assert(p.negative === 0);
2081
2080
  assert(!p.isZero());
2082
2081
  var x = this;
@@ -2086,10 +2085,10 @@ var require_bn = __commonJS({
2086
2085
  } else {
2087
2086
  x = x.clone();
2088
2087
  }
2089
- var A = new BN5(1);
2090
- var B = new BN5(0);
2091
- var C = new BN5(0);
2092
- var D = new BN5(1);
2088
+ var A = new BN6(1);
2089
+ var B = new BN6(0);
2090
+ var C = new BN6(0);
2091
+ var D = new BN6(1);
2093
2092
  var g = 0;
2094
2093
  while (x.isEven() && y.isEven()) {
2095
2094
  x.iushrn(1);
@@ -2139,7 +2138,7 @@ var require_bn = __commonJS({
2139
2138
  gcd: y.iushln(g)
2140
2139
  };
2141
2140
  };
2142
- BN5.prototype._invmp = function _invmp(p) {
2141
+ BN6.prototype._invmp = function _invmp(p) {
2143
2142
  assert(p.negative === 0);
2144
2143
  assert(!p.isZero());
2145
2144
  var a = this;
@@ -2149,8 +2148,8 @@ var require_bn = __commonJS({
2149
2148
  } else {
2150
2149
  a = a.clone();
2151
2150
  }
2152
- var x1 = new BN5(1);
2153
- var x2 = new BN5(0);
2151
+ var x1 = new BN6(1);
2152
+ var x2 = new BN6(0);
2154
2153
  var delta = b.clone();
2155
2154
  while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
2156
2155
  for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1) ;
@@ -2192,7 +2191,7 @@ var require_bn = __commonJS({
2192
2191
  }
2193
2192
  return res;
2194
2193
  };
2195
- BN5.prototype.gcd = function gcd(num) {
2194
+ BN6.prototype.gcd = function gcd(num) {
2196
2195
  if (this.isZero()) return num.abs();
2197
2196
  if (num.isZero()) return this.abs();
2198
2197
  var a = this.clone();
@@ -2222,19 +2221,19 @@ var require_bn = __commonJS({
2222
2221
  } while (true);
2223
2222
  return b.iushln(shift);
2224
2223
  };
2225
- BN5.prototype.invm = function invm(num) {
2224
+ BN6.prototype.invm = function invm(num) {
2226
2225
  return this.egcd(num).a.umod(num);
2227
2226
  };
2228
- BN5.prototype.isEven = function isEven() {
2227
+ BN6.prototype.isEven = function isEven() {
2229
2228
  return (this.words[0] & 1) === 0;
2230
2229
  };
2231
- BN5.prototype.isOdd = function isOdd2() {
2230
+ BN6.prototype.isOdd = function isOdd2() {
2232
2231
  return (this.words[0] & 1) === 1;
2233
2232
  };
2234
- BN5.prototype.andln = function andln(num) {
2233
+ BN6.prototype.andln = function andln(num) {
2235
2234
  return this.words[0] & num;
2236
2235
  };
2237
- BN5.prototype.bincn = function bincn(bit) {
2236
+ BN6.prototype.bincn = function bincn(bit) {
2238
2237
  assert(typeof bit === "number");
2239
2238
  var r = bit % 26;
2240
2239
  var s = (bit - r) / 26;
@@ -2258,10 +2257,10 @@ var require_bn = __commonJS({
2258
2257
  }
2259
2258
  return this;
2260
2259
  };
2261
- BN5.prototype.isZero = function isZero() {
2260
+ BN6.prototype.isZero = function isZero() {
2262
2261
  return this.length === 1 && this.words[0] === 0;
2263
2262
  };
2264
- BN5.prototype.cmpn = function cmpn(num) {
2263
+ BN6.prototype.cmpn = function cmpn(num) {
2265
2264
  var negative = num < 0;
2266
2265
  if (this.negative !== 0 && !negative) return -1;
2267
2266
  if (this.negative === 0 && negative) return 1;
@@ -2280,14 +2279,14 @@ var require_bn = __commonJS({
2280
2279
  if (this.negative !== 0) return -res | 0;
2281
2280
  return res;
2282
2281
  };
2283
- BN5.prototype.cmp = function cmp2(num) {
2282
+ BN6.prototype.cmp = function cmp2(num) {
2284
2283
  if (this.negative !== 0 && num.negative === 0) return -1;
2285
2284
  if (this.negative === 0 && num.negative !== 0) return 1;
2286
2285
  var res = this.ucmp(num);
2287
2286
  if (this.negative !== 0) return -res | 0;
2288
2287
  return res;
2289
2288
  };
2290
- BN5.prototype.ucmp = function ucmp(num) {
2289
+ BN6.prototype.ucmp = function ucmp(num) {
2291
2290
  if (this.length > num.length) return 1;
2292
2291
  if (this.length < num.length) return -1;
2293
2292
  var res = 0;
@@ -2304,112 +2303,112 @@ var require_bn = __commonJS({
2304
2303
  }
2305
2304
  return res;
2306
2305
  };
2307
- BN5.prototype.gtn = function gtn(num) {
2306
+ BN6.prototype.gtn = function gtn(num) {
2308
2307
  return this.cmpn(num) === 1;
2309
2308
  };
2310
- BN5.prototype.gt = function gt(num) {
2309
+ BN6.prototype.gt = function gt(num) {
2311
2310
  return this.cmp(num) === 1;
2312
2311
  };
2313
- BN5.prototype.gten = function gten(num) {
2312
+ BN6.prototype.gten = function gten(num) {
2314
2313
  return this.cmpn(num) >= 0;
2315
2314
  };
2316
- BN5.prototype.gte = function gte(num) {
2315
+ BN6.prototype.gte = function gte(num) {
2317
2316
  return this.cmp(num) >= 0;
2318
2317
  };
2319
- BN5.prototype.ltn = function ltn(num) {
2318
+ BN6.prototype.ltn = function ltn(num) {
2320
2319
  return this.cmpn(num) === -1;
2321
2320
  };
2322
- BN5.prototype.lt = function lt(num) {
2321
+ BN6.prototype.lt = function lt(num) {
2323
2322
  return this.cmp(num) === -1;
2324
2323
  };
2325
- BN5.prototype.lten = function lten(num) {
2324
+ BN6.prototype.lten = function lten(num) {
2326
2325
  return this.cmpn(num) <= 0;
2327
2326
  };
2328
- BN5.prototype.lte = function lte(num) {
2327
+ BN6.prototype.lte = function lte(num) {
2329
2328
  return this.cmp(num) <= 0;
2330
2329
  };
2331
- BN5.prototype.eqn = function eqn(num) {
2330
+ BN6.prototype.eqn = function eqn(num) {
2332
2331
  return this.cmpn(num) === 0;
2333
2332
  };
2334
- BN5.prototype.eq = function eq(num) {
2333
+ BN6.prototype.eq = function eq(num) {
2335
2334
  return this.cmp(num) === 0;
2336
2335
  };
2337
- BN5.red = function red(num) {
2336
+ BN6.red = function red(num) {
2338
2337
  return new Red(num);
2339
2338
  };
2340
- BN5.prototype.toRed = function toRed(ctx) {
2339
+ BN6.prototype.toRed = function toRed(ctx) {
2341
2340
  assert(!this.red, "Already a number in reduction context");
2342
2341
  assert(this.negative === 0, "red works only with positives");
2343
2342
  return ctx.convertTo(this)._forceRed(ctx);
2344
2343
  };
2345
- BN5.prototype.fromRed = function fromRed() {
2344
+ BN6.prototype.fromRed = function fromRed() {
2346
2345
  assert(this.red, "fromRed works only with numbers in reduction context");
2347
2346
  return this.red.convertFrom(this);
2348
2347
  };
2349
- BN5.prototype._forceRed = function _forceRed(ctx) {
2348
+ BN6.prototype._forceRed = function _forceRed(ctx) {
2350
2349
  this.red = ctx;
2351
2350
  return this;
2352
2351
  };
2353
- BN5.prototype.forceRed = function forceRed(ctx) {
2352
+ BN6.prototype.forceRed = function forceRed(ctx) {
2354
2353
  assert(!this.red, "Already a number in reduction context");
2355
2354
  return this._forceRed(ctx);
2356
2355
  };
2357
- BN5.prototype.redAdd = function redAdd(num) {
2356
+ BN6.prototype.redAdd = function redAdd(num) {
2358
2357
  assert(this.red, "redAdd works only with red numbers");
2359
2358
  return this.red.add(this, num);
2360
2359
  };
2361
- BN5.prototype.redIAdd = function redIAdd(num) {
2360
+ BN6.prototype.redIAdd = function redIAdd(num) {
2362
2361
  assert(this.red, "redIAdd works only with red numbers");
2363
2362
  return this.red.iadd(this, num);
2364
2363
  };
2365
- BN5.prototype.redSub = function redSub(num) {
2364
+ BN6.prototype.redSub = function redSub(num) {
2366
2365
  assert(this.red, "redSub works only with red numbers");
2367
2366
  return this.red.sub(this, num);
2368
2367
  };
2369
- BN5.prototype.redISub = function redISub(num) {
2368
+ BN6.prototype.redISub = function redISub(num) {
2370
2369
  assert(this.red, "redISub works only with red numbers");
2371
2370
  return this.red.isub(this, num);
2372
2371
  };
2373
- BN5.prototype.redShl = function redShl(num) {
2372
+ BN6.prototype.redShl = function redShl(num) {
2374
2373
  assert(this.red, "redShl works only with red numbers");
2375
2374
  return this.red.shl(this, num);
2376
2375
  };
2377
- BN5.prototype.redMul = function redMul(num) {
2376
+ BN6.prototype.redMul = function redMul(num) {
2378
2377
  assert(this.red, "redMul works only with red numbers");
2379
2378
  this.red._verify2(this, num);
2380
2379
  return this.red.mul(this, num);
2381
2380
  };
2382
- BN5.prototype.redIMul = function redIMul(num) {
2381
+ BN6.prototype.redIMul = function redIMul(num) {
2383
2382
  assert(this.red, "redMul works only with red numbers");
2384
2383
  this.red._verify2(this, num);
2385
2384
  return this.red.imul(this, num);
2386
2385
  };
2387
- BN5.prototype.redSqr = function redSqr() {
2386
+ BN6.prototype.redSqr = function redSqr() {
2388
2387
  assert(this.red, "redSqr works only with red numbers");
2389
2388
  this.red._verify1(this);
2390
2389
  return this.red.sqr(this);
2391
2390
  };
2392
- BN5.prototype.redISqr = function redISqr() {
2391
+ BN6.prototype.redISqr = function redISqr() {
2393
2392
  assert(this.red, "redISqr works only with red numbers");
2394
2393
  this.red._verify1(this);
2395
2394
  return this.red.isqr(this);
2396
2395
  };
2397
- BN5.prototype.redSqrt = function redSqrt() {
2396
+ BN6.prototype.redSqrt = function redSqrt() {
2398
2397
  assert(this.red, "redSqrt works only with red numbers");
2399
2398
  this.red._verify1(this);
2400
2399
  return this.red.sqrt(this);
2401
2400
  };
2402
- BN5.prototype.redInvm = function redInvm() {
2401
+ BN6.prototype.redInvm = function redInvm() {
2403
2402
  assert(this.red, "redInvm works only with red numbers");
2404
2403
  this.red._verify1(this);
2405
2404
  return this.red.invm(this);
2406
2405
  };
2407
- BN5.prototype.redNeg = function redNeg() {
2406
+ BN6.prototype.redNeg = function redNeg() {
2408
2407
  assert(this.red, "redNeg works only with red numbers");
2409
2408
  this.red._verify1(this);
2410
2409
  return this.red.neg(this);
2411
2410
  };
2412
- BN5.prototype.redPow = function redPow(num) {
2411
+ BN6.prototype.redPow = function redPow(num) {
2413
2412
  assert(this.red && !num.red, "redPow(normalNum)");
2414
2413
  this.red._verify1(this);
2415
2414
  return this.red.pow(this, num);
@@ -2422,13 +2421,13 @@ var require_bn = __commonJS({
2422
2421
  };
2423
2422
  function MPrime(name, p) {
2424
2423
  this.name = name;
2425
- this.p = new BN5(p, 16);
2424
+ this.p = new BN6(p, 16);
2426
2425
  this.n = this.p.bitLength();
2427
- this.k = new BN5(1).iushln(this.n).isub(this.p);
2426
+ this.k = new BN6(1).iushln(this.n).isub(this.p);
2428
2427
  this.tmp = this._tmp();
2429
2428
  }
2430
2429
  MPrime.prototype._tmp = function _tmp() {
2431
- var tmp = new BN5(null);
2430
+ var tmp = new BN6(null);
2432
2431
  tmp.words = new Array(Math.ceil(this.n / 13));
2433
2432
  return tmp;
2434
2433
  };
@@ -2554,7 +2553,7 @@ var require_bn = __commonJS({
2554
2553
  }
2555
2554
  return num;
2556
2555
  };
2557
- BN5._prime = function prime(name) {
2556
+ BN6._prime = function prime(name) {
2558
2557
  if (primes[name]) return primes[name];
2559
2558
  var prime2;
2560
2559
  if (name === "k256") {
@@ -2573,7 +2572,7 @@ var require_bn = __commonJS({
2573
2572
  };
2574
2573
  function Red(m) {
2575
2574
  if (typeof m === "string") {
2576
- var prime = BN5._prime(m);
2575
+ var prime = BN6._prime(m);
2577
2576
  this.m = prime.p;
2578
2577
  this.prime = prime;
2579
2578
  } else {
@@ -2659,7 +2658,7 @@ var require_bn = __commonJS({
2659
2658
  var mod3 = this.m.andln(3);
2660
2659
  assert(mod3 % 2 === 1);
2661
2660
  if (mod3 === 3) {
2662
- var pow2 = this.m.add(new BN5(1)).iushrn(2);
2661
+ var pow2 = this.m.add(new BN6(1)).iushrn(2);
2663
2662
  return this.pow(a, pow2);
2664
2663
  }
2665
2664
  var q = this.m.subn(1);
@@ -2669,11 +2668,11 @@ var require_bn = __commonJS({
2669
2668
  q.iushrn(1);
2670
2669
  }
2671
2670
  assert(!q.isZero());
2672
- var one = new BN5(1).toRed(this);
2671
+ var one = new BN6(1).toRed(this);
2673
2672
  var nOne = one.redNeg();
2674
2673
  var lpow = this.m.subn(1).iushrn(1);
2675
2674
  var z = this.m.bitLength();
2676
- z = new BN5(2 * z * z).toRed(this);
2675
+ z = new BN6(2 * z * z).toRed(this);
2677
2676
  while (this.pow(z, lpow).cmp(nOne) !== 0) {
2678
2677
  z.redIAdd(nOne);
2679
2678
  }
@@ -2687,7 +2686,7 @@ var require_bn = __commonJS({
2687
2686
  tmp = tmp.redSqr();
2688
2687
  }
2689
2688
  assert(i < m);
2690
- var b = this.pow(c, new BN5(1).iushln(m - i - 1));
2689
+ var b = this.pow(c, new BN6(1).iushln(m - i - 1));
2691
2690
  r = r.redMul(b);
2692
2691
  c = b.redSqr();
2693
2692
  t = t.redMul(c);
@@ -2705,11 +2704,11 @@ var require_bn = __commonJS({
2705
2704
  }
2706
2705
  };
2707
2706
  Red.prototype.pow = function pow2(a, num) {
2708
- if (num.isZero()) return new BN5(1).toRed(this);
2707
+ if (num.isZero()) return new BN6(1).toRed(this);
2709
2708
  if (num.cmpn(1) === 0) return a.clone();
2710
2709
  var windowSize = 4;
2711
2710
  var wnd = new Array(1 << windowSize);
2712
- wnd[0] = new BN5(1).toRed(this);
2711
+ wnd[0] = new BN6(1).toRed(this);
2713
2712
  wnd[1] = a;
2714
2713
  for (var i = 2; i < wnd.length; i++) {
2715
2714
  wnd[i] = this.mul(wnd[i - 1], a);
@@ -2753,7 +2752,7 @@ var require_bn = __commonJS({
2753
2752
  res.red = null;
2754
2753
  return res;
2755
2754
  };
2756
- BN5.mont = function mont(num) {
2755
+ BN6.mont = function mont(num) {
2757
2756
  return new Mont(num);
2758
2757
  };
2759
2758
  function Mont(m) {
@@ -2762,7 +2761,7 @@ var require_bn = __commonJS({
2762
2761
  if (this.shift % 26 !== 0) {
2763
2762
  this.shift += 26 - this.shift % 26;
2764
2763
  }
2765
- this.r = new BN5(1).iushln(this.shift);
2764
+ this.r = new BN6(1).iushln(this.shift);
2766
2765
  this.r2 = this.imod(this.r.sqr());
2767
2766
  this.rinv = this.r._invmp(this.m);
2768
2767
  this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
@@ -2796,7 +2795,7 @@ var require_bn = __commonJS({
2796
2795
  return res._forceRed(this);
2797
2796
  };
2798
2797
  Mont.prototype.mul = function mul2(a, b) {
2799
- if (a.isZero() || b.isZero()) return new BN5(0)._forceRed(this);
2798
+ if (a.isZero() || b.isZero()) return new BN6(0)._forceRed(this);
2800
2799
  var t = a.mul(b);
2801
2800
  var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
2802
2801
  var u = t.isub(c).iushrn(this.shift);
@@ -4980,193 +4979,365 @@ LN10 = new Decimal(LN10);
4980
4979
  PI = new Decimal(PI);
4981
4980
  var decimal_default = Decimal;
4982
4981
 
4983
- // src/types/CoinAssist.ts
4984
- var COIN_TYPE = "0x2::coin::Coin";
4985
- var COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;
4986
- var GAS_TYPE_ARG = "0x2::sui::SUI";
4987
- var GAS_TYPE_ARG_LONG = "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
4988
- var CoinUtils = class _CoinUtils {
4989
- /**
4990
- * Get the coin type argument from a SuiMoveObject.
4991
- *
4992
- * @param obj The SuiMoveObject to get the coin type argument from.
4993
- * @returns The coin type argument, or null if it is not found.
4994
- */
4995
- static getCoinTypeArg(obj) {
4996
- const res = obj.type.match(COIN_TYPE_ARG_REGEX);
4997
- return res ? res[1] : null;
4998
- }
4999
- /**
5000
- * Get whether a SuiMoveObject is a SUI coin.
5001
- *
5002
- * @param obj The SuiMoveObject to check.
5003
- * @returns Whether the SuiMoveObject is a SUI coin.
5004
- */
5005
- static isSUI(obj) {
5006
- const arg = _CoinUtils.getCoinTypeArg(obj);
5007
- return arg ? _CoinUtils.getCoinSymbol(arg) === "SUI" : false;
4982
+ // src/client.ts
4983
+ var import_bn5 = __toESM(require_bn());
4984
+
4985
+ // src/transaction/aftermath.ts
4986
+ var import_bn = __toESM(require_bn());
4987
+ var Aftermath = class {
4988
+ constructor(env) {
4989
+ if (env !== 0 /* Mainnet */) {
4990
+ throw new Error("Aftermath only supported on mainnet");
4991
+ }
4992
+ this.slippage = "900000000000000000";
4993
+ this.poolRegistry = "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae";
4994
+ this.protocolFeeVault = "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4";
4995
+ this.treasury = "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce";
4996
+ this.insuranceFund = "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b";
4997
+ this.referrealVault = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
4998
+ }
4999
+ amountLimit(exportAmountOut) {
5000
+ return Number(
5001
+ new import_bn.default(exportAmountOut).mul(new import_bn.default(this.slippage)).div(new import_bn.default("1000000000000000000")).toString()
5002
+ );
5008
5003
  }
5009
- /**
5010
- * Get the coin symbol from a coin type argument.
5011
- *
5012
- * @param coinTypeArg The coin type argument to get the symbol from.
5013
- * @returns The coin symbol.
5014
- */
5015
- static getCoinSymbol(coinTypeArg) {
5016
- return coinTypeArg.substring(coinTypeArg.lastIndexOf(":") + 1);
5004
+ swap(client, txb, path, inputCoin) {
5005
+ return __async(this, null, function* () {
5006
+ const { direction, from, target } = path;
5007
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5008
+ if (path.extendedDetails == null) {
5009
+ throw new Error("Extended details not supported");
5010
+ } else {
5011
+ if (path.extendedDetails.aftermathLpSupplyType == null) {
5012
+ throw new Error("LP supply type not supported");
5013
+ }
5014
+ }
5015
+ const args = [
5016
+ txb.object(path.id),
5017
+ txb.object(this.poolRegistry),
5018
+ txb.object(this.protocolFeeVault),
5019
+ txb.object(this.treasury),
5020
+ txb.object(this.insuranceFund),
5021
+ txb.object(this.referrealVault),
5022
+ txb.pure.u64(this.amountLimit(path.amountOut)),
5023
+ txb.pure.u64(this.slippage),
5024
+ inputCoin
5025
+ ];
5026
+ const res = txb.moveCall({
5027
+ target: `${client.publishedAt()}::aftermath::${func}`,
5028
+ typeArguments: [
5029
+ coinAType,
5030
+ coinBType,
5031
+ path.extendedDetails.aftermathLpSupplyType
5032
+ ],
5033
+ arguments: args
5034
+ });
5035
+ return res;
5036
+ });
5017
5037
  }
5018
- /**
5019
- * Get the balance of a SuiMoveObject.
5020
- *
5021
- * @param obj The SuiMoveObject to get the balance from.
5022
- * @returns The balance of the SuiMoveObject.
5023
- */
5024
- static getBalance(obj) {
5025
- return BigInt(obj.fields.balance);
5038
+ };
5039
+
5040
+ // src/transaction/deepbook_v2.ts
5041
+ var DEEPBOOK_PACKAGE_ID = "0x000000000000000000000000000000000000000000000000000000000000dee9";
5042
+ var DeepbookV2 = class {
5043
+ constructor(env) {
5044
+ if (env !== 0 /* Mainnet */) {
5045
+ throw new Error("Aftermath only supported on mainnet");
5046
+ }
5026
5047
  }
5027
- /**
5028
- * Get the total balance of a list of CoinAsset objects for a given coin address.
5029
- *
5030
- * @param objs The list of CoinAsset objects to get the total balance for.
5031
- * @param coinAddress The coin address to get the total balance for.
5032
- * @returns The total balance of the CoinAsset objects for the given coin address.
5033
- */
5034
- static totalBalance(objs, coinAddress) {
5035
- let balanceTotal = BigInt(0);
5036
- objs.forEach((obj) => {
5037
- if (coinAddress === obj.coinAddress) {
5038
- balanceTotal += BigInt(obj.balance);
5048
+ getAccountCap(client, owner) {
5049
+ return __async(this, null, function* () {
5050
+ let limit = 50;
5051
+ let cursor = null;
5052
+ while (true) {
5053
+ const ownedObjects = client.getOwnedObjects({
5054
+ owner,
5055
+ cursor,
5056
+ limit,
5057
+ filter: {
5058
+ MoveModule: {
5059
+ package: DEEPBOOK_PACKAGE_ID,
5060
+ module: "custodian_v2"
5061
+ }
5062
+ }
5063
+ });
5064
+ if (ownedObjects != null && ownedObjects.data != null) {
5065
+ if (ownedObjects.data.length !== 0) {
5066
+ return ownedObjects.data[0].data.objectId;
5067
+ }
5068
+ if (ownedObjects.data.length < 50) {
5069
+ break;
5070
+ }
5071
+ } else {
5072
+ break;
5073
+ }
5039
5074
  }
5075
+ return null;
5040
5076
  });
5041
- return balanceTotal;
5042
- }
5043
- /**
5044
- * Get the ID of a SuiMoveObject.
5045
- *
5046
- * @param obj The SuiMoveObject to get the ID from.
5047
- * @returns The ID of the SuiMoveObject.
5048
- */
5049
- static getID(obj) {
5050
- return obj.fields.id.id;
5051
5077
  }
5052
- /**
5053
- * Get the coin type from a coin type argument.
5054
- *
5055
- * @param coinTypeArg The coin type argument to get the coin type from.
5056
- * @returns The coin type.
5057
- */
5058
- static getCoinTypeFromArg(coinTypeArg) {
5059
- return `${COIN_TYPE}<${coinTypeArg}>`;
5078
+ getOrCreateAccountCap(txb, client, owner) {
5079
+ return __async(this, null, function* () {
5080
+ let accountCapStr = yield this.getAccountCap(client, owner);
5081
+ if (accountCapStr !== null) {
5082
+ return {
5083
+ accountCap: txb.object(accountCapStr),
5084
+ isCreate: false
5085
+ };
5086
+ }
5087
+ const accountCap = txb.moveCall({
5088
+ target: `${DEEPBOOK_PACKAGE_ID}::clob_v2::create_account`,
5089
+ typeArguments: [],
5090
+ arguments: []
5091
+ });
5092
+ return {
5093
+ accountCap,
5094
+ isCreate: true
5095
+ };
5096
+ });
5060
5097
  }
5061
- /**
5062
- * Get the CoinAsset objects for a given coin type.
5063
- *
5064
- * @param coinType The coin type to get the CoinAsset objects for.
5065
- * @param allSuiObjects The list of all SuiMoveObjects.
5066
- * @returns The CoinAsset objects for the given coin type.
5067
- */
5068
- static getCoinAssets(coinType, allSuiObjects) {
5069
- const coins = [];
5070
- allSuiObjects.forEach((anObj) => {
5071
- if (normalizeCoinType(anObj.coinAddress) === normalizeCoinType(coinType)) {
5072
- coins.push(anObj);
5098
+ swap(client, txb, path, inputCoin) {
5099
+ return __async(this, null, function* () {
5100
+ const { direction, from, target } = path;
5101
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5102
+ const accountCapRes = yield this.getOrCreateAccountCap(
5103
+ txb,
5104
+ client.client,
5105
+ client.signer
5106
+ );
5107
+ const args = [
5108
+ txb.object(path.id),
5109
+ inputCoin,
5110
+ accountCapRes.accountCap,
5111
+ txb.object(CLOCK_ADDRESS)
5112
+ ];
5113
+ const res = txb.moveCall({
5114
+ target: `${client.publishedAt()}::deepbook::${func}`,
5115
+ typeArguments: [coinAType, coinBType],
5116
+ arguments: args
5117
+ });
5118
+ if (accountCapRes.isCreate) {
5119
+ txb.transferObjects([accountCapRes.accountCap], client.signer);
5073
5120
  }
5121
+ return res;
5074
5122
  });
5075
- return coins;
5076
5123
  }
5077
- /**
5078
- * Get whether a coin address is a SUI coin.
5079
- *
5080
- * @param coinAddress The coin address to check.
5081
- * @returns Whether the coin address is a SUI coin.
5082
- */
5083
- static isSuiCoin(coinAddress) {
5084
- return extractStructTagFromType(coinAddress).full_address === GAS_TYPE_ARG;
5124
+ };
5125
+
5126
+ // src/transaction/kriya_v2.ts
5127
+ var KriyaV2 = class {
5128
+ constructor(env) {
5129
+ if (env !== 0 /* Mainnet */) {
5130
+ throw new Error("Kriya amm only supported on mainnet");
5131
+ }
5085
5132
  }
5086
- /**
5087
- * Select the CoinAsset objects from a list of CoinAsset objects that have a balance greater than or equal to a given amount.
5088
- *
5089
- * @param coins The list of CoinAsset objects to select from.
5090
- * @param amount The amount to select CoinAsset objects with a balance greater than or equal to.
5091
- * @param exclude A list of CoinAsset objects to exclude from the selection.
5092
- * @returns The CoinAsset objects that have a balance greater than or equal to the given amount.
5093
- */
5094
- static selectCoinObjectIdGreaterThanOrEqual(coins, amount, exclude = []) {
5095
- const selectedResult = _CoinUtils.selectCoinAssetGreaterThanOrEqual(coins, amount, exclude);
5096
- const objectArray = selectedResult.selectedCoins.map((item) => item.coinObjectId);
5097
- const remainCoins = selectedResult.remainingCoins;
5098
- const amountArray = selectedResult.selectedCoins.map((item) => item.balance.toString());
5099
- return { objectArray, remainCoins, amountArray };
5133
+ swap(client, txb, path, inputCoin) {
5134
+ return __async(this, null, function* () {
5135
+ const { direction, from, target } = path;
5136
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5137
+ const args = [txb.object(path.id), inputCoin];
5138
+ const res = txb.moveCall({
5139
+ target: `${client.publishedAt()}::kriya_amm::${func}`,
5140
+ typeArguments: [coinAType, coinBType],
5141
+ arguments: args
5142
+ });
5143
+ return res;
5144
+ });
5100
5145
  }
5101
- /**
5102
- * Select the CoinAsset objects from a list of CoinAsset objects that have a balance greater than or equal to a given amount.
5103
- *
5104
- * @param coins The list of CoinAsset objects to select from.
5105
- * @param amount The amount to select CoinAsset objects with a balance greater than or equal to.
5106
- * @param exclude A list of CoinAsset objects to exclude from the selection.
5107
- * @returns The CoinAsset objects that have a balance greater than or equal to the given amount.
5108
- */
5109
- static selectCoinAssetGreaterThanOrEqual(coins, amount, exclude = []) {
5110
- const sortedCoins = _CoinUtils.sortByBalance(coins.filter((c) => !exclude.includes(c.coinObjectId)));
5111
- const total = _CoinUtils.calculateTotalBalance(sortedCoins);
5112
- if (total < amount) {
5113
- return { selectedCoins: [], remainingCoins: sortedCoins };
5114
- }
5115
- if (total === amount) {
5116
- return { selectedCoins: sortedCoins, remainingCoins: [] };
5117
- }
5118
- let sum2 = BigInt(0);
5119
- const selectedCoins = [];
5120
- const remainingCoins = [...sortedCoins];
5121
- while (sum2 < total) {
5122
- const target = amount - sum2;
5123
- const coinWithSmallestSufficientBalanceIndex = remainingCoins.findIndex((c) => c.balance >= target);
5124
- if (coinWithSmallestSufficientBalanceIndex !== -1) {
5125
- selectedCoins.push(remainingCoins[coinWithSmallestSufficientBalanceIndex]);
5126
- remainingCoins.splice(coinWithSmallestSufficientBalanceIndex, 1);
5127
- break;
5128
- }
5129
- const coinWithLargestBalance = remainingCoins.pop();
5130
- if (coinWithLargestBalance.balance > 0) {
5131
- selectedCoins.push(coinWithLargestBalance);
5132
- sum2 += coinWithLargestBalance.balance;
5133
- }
5146
+ };
5147
+
5148
+ // src/transaction/flowx_v2.ts
5149
+ var FlowxV2 = class {
5150
+ constructor(env) {
5151
+ if (env !== 0 /* Mainnet */) {
5152
+ throw new Error("Flowx only supported on mainnet");
5134
5153
  }
5135
- return { selectedCoins: _CoinUtils.sortByBalance(selectedCoins), remainingCoins: _CoinUtils.sortByBalance(remainingCoins) };
5154
+ this.container = "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511";
5136
5155
  }
5137
- /**
5138
- * Sort the CoinAsset objects by their balance.
5139
- *
5140
- * @param coins The CoinAsset objects to sort.
5141
- * @returns The sorted CoinAsset objects.
5142
- */
5143
- static sortByBalance(coins) {
5144
- return coins.sort((a, b) => a.balance < b.balance ? -1 : a.balance > b.balance ? 1 : 0);
5156
+ swap(client, txb, path, inputCoin) {
5157
+ return __async(this, null, function* () {
5158
+ const { direction, from, target } = path;
5159
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5160
+ const args = [txb.object(this.container), inputCoin];
5161
+ const res = txb.moveCall({
5162
+ target: `${client.publishedAt()}::flowx_amm::${func}`,
5163
+ typeArguments: [coinAType, coinBType],
5164
+ arguments: args
5165
+ });
5166
+ return res;
5167
+ });
5145
5168
  }
5146
- static sortByBalanceDes(coins) {
5147
- return coins.sort((a, b) => a.balance > b.balance ? -1 : a.balance < b.balance ? 0 : 1);
5169
+ };
5170
+
5171
+ // src/transaction/turbos.ts
5172
+ var Turbos = class {
5173
+ constructor(env) {
5174
+ if (env !== 0 /* Mainnet */) {
5175
+ throw new Error("Turbos only supported on mainnet");
5176
+ }
5177
+ this.versioned = "0xf1cf0e81048df168ebeb1b8030fad24b3e0b53ae827c25053fff0779c1445b6f";
5148
5178
  }
5149
- /**
5150
- * Calculate the total balance of a list of CoinAsset objects.
5151
- *
5152
- * @param coins The list of CoinAsset objects to calculate the total balance for.
5153
- * @returns The total balance of the CoinAsset objects.
5154
- */
5155
- static calculateTotalBalance(coins) {
5156
- return coins.reduce((partialSum, c) => partialSum + c.balance, BigInt(0));
5179
+ swap(client, txb, path, inputCoin) {
5180
+ return __async(this, null, function* () {
5181
+ const { direction, from, target } = path;
5182
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5183
+ if (path.extendedDetails == null) {
5184
+ throw new Error("Extended details not supported");
5185
+ } else {
5186
+ if (path.extendedDetails.turbosFeeType == null) {
5187
+ throw new Error("Turbos fee type not supported");
5188
+ }
5189
+ }
5190
+ const args = [
5191
+ txb.object(path.id),
5192
+ inputCoin,
5193
+ txb.object(CLOCK_ADDRESS),
5194
+ txb.object(this.versioned)
5195
+ ];
5196
+ const res = txb.moveCall({
5197
+ target: `${client.publishedAt()}::turbos::${func}`,
5198
+ typeArguments: [coinAType, coinBType, path.extendedDetails.turbosFeeType],
5199
+ arguments: args
5200
+ });
5201
+ return res;
5202
+ });
5157
5203
  }
5158
5204
  };
5159
5205
 
5160
- // src/utils/contracts.ts
5161
- var EQUAL = 0;
5162
- var LESS_THAN = 1;
5163
- var GREATER_THAN = 2;
5164
- function cmp(a, b) {
5165
- if (a === b) {
5166
- return EQUAL;
5206
+ // src/transaction/cetus.ts
5207
+ var Cetus = class {
5208
+ constructor(env, partner) {
5209
+ this.globalConfig = env === 0 /* Mainnet */ ? "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f" : "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a";
5210
+ this.partner = partner != null ? partner : "0x639b5e433da31739e800cd085f356e64cae222966d0f1b11bd9dc76b322ff58b";
5211
+ }
5212
+ flash_swap(client, txb, path, by_amount_in) {
5213
+ const { direction, from, target } = path;
5214
+ const [func, coinAType, coinBType] = direction ? ["flash_swap_a2b", from, target] : ["flash_swap_b2a", target, from];
5215
+ let amount = by_amount_in ? path.amountIn : path.amountOut;
5216
+ const args = [
5217
+ txb.object(this.globalConfig),
5218
+ txb.object(path.id),
5219
+ txb.object(this.partner),
5220
+ txb.pure.u64(amount),
5221
+ txb.pure.bool(by_amount_in),
5222
+ txb.object(CLOCK_ADDRESS)
5223
+ ];
5224
+ const res = txb.moveCall({
5225
+ target: `${client.publishedAt()}::cetus::${func}`,
5226
+ typeArguments: [coinAType, coinBType],
5227
+ arguments: args
5228
+ });
5229
+ return {
5230
+ targetCoin: res[0],
5231
+ flashReceipt: res[1],
5232
+ payAmount: res[2]
5233
+ };
5167
5234
  }
5168
- if (a < b) {
5169
- return LESS_THAN;
5235
+ repay_flash_swap(client, txb, path, inputCoin, receipt) {
5236
+ const { direction, from, target } = path;
5237
+ const [func, coinAType, coinBType] = direction ? ["repay_flash_swap_a2b", from, target] : ["repay_flash_swap_b2a", target, from];
5238
+ const args = [
5239
+ txb.object(this.globalConfig),
5240
+ txb.object(path.id),
5241
+ txb.object(this.partner),
5242
+ inputCoin,
5243
+ receipt
5244
+ ];
5245
+ const res = txb.moveCall({
5246
+ target: `${client.publishedAt()}::cetus::${func}`,
5247
+ typeArguments: [coinAType, coinBType],
5248
+ arguments: args
5249
+ });
5250
+ return res[0];
5251
+ }
5252
+ swap(client, txb, path, inputCoin) {
5253
+ return __async(this, null, function* () {
5254
+ const { direction, from, target } = path;
5255
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5256
+ const args = [
5257
+ txb.object(this.globalConfig),
5258
+ txb.object(path.id),
5259
+ txb.object(this.partner),
5260
+ inputCoin,
5261
+ txb.object(CLOCK_ADDRESS)
5262
+ ];
5263
+ const res = txb.moveCall({
5264
+ target: `${client.publishedAt()}::cetus::${func}`,
5265
+ typeArguments: [coinAType, coinBType],
5266
+ arguments: args
5267
+ });
5268
+ return res;
5269
+ });
5270
+ }
5271
+ };
5272
+
5273
+ // src/const.ts
5274
+ var import_bn2 = __toESM(require_bn());
5275
+ var CoinInfoAddress = "0x1::coin::CoinInfo";
5276
+ var CoinStoreAddress = "0x1::coin::CoinStore";
5277
+ var SuiZeroCoinFn = "0x2::coin::zero";
5278
+ var CETUS_MODULE = "cetus";
5279
+ var DEEPBOOK_MODULE = "deepbook";
5280
+ var KRIYA_MODULE = "kriya";
5281
+ var UTILS_MODULE = "utils";
5282
+ var POOL_MODULT = "pool";
5283
+ var PAY_MODULE = "pay";
5284
+ var FLOWX_AMM_MODULE = "flowx_amm";
5285
+ var TURBOS_MODULE = "turbos";
5286
+ var AFTERMATH_MODULE = "aftermath";
5287
+ var DEEPBOOK_CUSTODIAN_V2_MODULE = "custodian_v2";
5288
+ var DEEPBOOK_CLOB_V2_MODULE = "clob_v2";
5289
+ var FlashSwapFunc = "flash_swap";
5290
+ var FlashSwapWithPartnerFunc = "flash_swap_with_partner";
5291
+ var RepayFalshSwapFunc = "repay_flash_swap";
5292
+ var RepayFlashSwapWithPartnerFunc = "repay_flash_swap_with_partner";
5293
+ var FlashSwapA2BFunc = "flash_swap_a2b";
5294
+ var FlashSwapB2AFunc = "flash_swap_b2a";
5295
+ var FlashSwapWithPartnerA2BFunc = "flash_swap_with_partner_a2b";
5296
+ var FlashSwapWithPartnerB2AFunc = "flash_swap_with_partner_b2a";
5297
+ var REPAY_FLASH_SWAP_A2B_FUNC = "repay_flash_swap_a2b";
5298
+ var REPAY_FLASH_SWAP_B2A_FUNC = "repay_flash_swap_b2a";
5299
+ var REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC = "repay_flash_swap_with_partner_a2b";
5300
+ var REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC = "repay_flash_swap_with_partner_b2a";
5301
+ var SWAP_A2B_FUNC = "swap_a2b";
5302
+ var SWAP_B2A_FUNC = "swap_b2a";
5303
+ var TRANSFER_OR_DESTORY_COIN_FUNC = "transfer_or_destroy_coin";
5304
+ var CHECK_COINS_THRESHOLD_FUNC = "check_coins_threshold";
5305
+ var JOIN_FUNC = "join_vec";
5306
+ var TRANSFER_ACCOUNT_CAP = "transfer_account_cap";
5307
+ var DEEPBOOK_PACKAGE_ID2 = "0x000000000000000000000000000000000000000000000000000000000000dee9";
5308
+ var DEEPBOOK_PUBLISHED_AT = "0x000000000000000000000000000000000000000000000000000000000000dee9";
5309
+ var CETUS_PUBLISHED_AT = "0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a";
5310
+ var MAINNET_CETUS_GLOBAL_CONFIG_ID = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
5311
+ var TESTNET_CETUS_GLOBAL_CONFIG_ID = "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a";
5312
+ var ZERO = new import_bn2.default(0);
5313
+ var ONE = new import_bn2.default(1);
5314
+ var TWO = new import_bn2.default(2);
5315
+ var U128 = TWO.pow(new import_bn2.default(128));
5316
+ var U64_MAX_BN = new import_bn2.default("18446744073709551615");
5317
+ var U64_MAX = "18446744073709551615";
5318
+ var TEN_POW_NINE = 1e9;
5319
+ var MAINNET_FLOWX_AMM_CONTAINER_ID = "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511";
5320
+ var TESTNET_FLOWX_AMM_CONTAINER_ID = "";
5321
+ var TURBOS_VERSIONED = "0xf1cf0e81048df168ebeb1b8030fad24b3e0b53ae827c25053fff0779c1445b6f";
5322
+ var MAINNET_AFTERMATH_REGISTRY_ID = "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae";
5323
+ var TESTNET_AFTERMATH_REGISTRY_ID = "";
5324
+ var MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4";
5325
+ var TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = "";
5326
+ var MAINNET_AFTERMATH_TREASURY_ID = "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce";
5327
+ var TESTNET_AFTERMATH_TREASURY_ID = "";
5328
+ var MAINNET_AFTERMATH_INSURANCE_FUND_ID = "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b";
5329
+ var TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
5330
+ var MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
5331
+ var TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
5332
+ var EQUAL = 0;
5333
+ var LESS_THAN = 1;
5334
+ var GREATER_THAN = 2;
5335
+ function cmp(a, b) {
5336
+ if (a === b) {
5337
+ return EQUAL;
5338
+ }
5339
+ if (a < b) {
5340
+ return LESS_THAN;
5170
5341
  }
5171
5342
  return GREATER_THAN;
5172
5343
  }
@@ -5264,1064 +5435,199 @@ function createTarget(packageName, moduleName, functionName) {
5264
5435
  return `${packageName}::${moduleName}::${functionName}`;
5265
5436
  }
5266
5437
 
5267
- // src/const.ts
5268
- var import_bn = __toESM(require_bn());
5269
- var CLOCK_ADDRESS = "0x0000000000000000000000000000000000000000000000000000000000000006";
5270
- var CoinInfoAddress = "0x1::coin::CoinInfo";
5271
- var CoinStoreAddress = "0x1::coin::CoinStore";
5272
- var SuiZeroCoinFn = "0x2::coin::zero";
5273
- var AGGREGATOR = "aggregator";
5274
- var CETUS_DEX = "CETUS";
5275
- var DEEPBOOK_DEX = "DEEPBOOK";
5276
- var KRIYA_DEX = "KRIYA";
5277
- var FLOWX_AMM = "FLOWX";
5278
- var TURBOS_DEX = "TURBOS";
5279
- var AFTERMATH_AMM = "AFTERMATH";
5280
- var INTEGRATE = "integrate";
5281
- var CETUS_MODULE = "cetus";
5282
- var DEEPBOOK_MODULE = "deepbook";
5283
- var KRIYA_MODULE = "kriya";
5284
- var UTILS_MODULE = "utils";
5285
- var POOL_MODULT = "pool";
5286
- var PAY_MODULE = "pay";
5287
- var FLOWX_AMM_MODULE = "flowx_amm";
5288
- var TURBOS_MODULE = "turbos";
5289
- var AFTERMATH_MODULE = "aftermath";
5290
- var DEEPBOOK_CUSTODIAN_V2_MODULE = "custodian_v2";
5291
- var DEEPBOOK_CLOB_V2_MODULE = "clob_v2";
5292
- var FlashSwapFunc = "flash_swap";
5293
- var FlashSwapWithPartnerFunc = "flash_swap_with_partner";
5294
- var RepayFalshSwapFunc = "repay_flash_swap";
5295
- var RepayFlashSwapWithPartnerFunc = "repay_flash_swap_with_partner";
5296
- var FlashSwapA2BFunc = "flash_swap_a2b";
5297
- var FlashSwapB2AFunc = "flash_swap_b2a";
5298
- var FlashSwapWithPartnerA2BFunc = "flash_swap_with_partner_a2b";
5299
- var FlashSwapWithPartnerB2AFunc = "flash_swap_with_partner_b2a";
5300
- var REPAY_FLASH_SWAP_A2B_FUNC = "repay_flash_swap_a2b";
5301
- var REPAY_FLASH_SWAP_B2A_FUNC = "repay_flash_swap_b2a";
5302
- var REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC = "repay_flash_swap_with_partner_a2b";
5303
- var REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC = "repay_flash_swap_with_partner_b2a";
5304
- var SWAP_A2B_FUNC = "swap_a2b";
5305
- var SWAP_B2A_FUNC = "swap_b2a";
5306
- var TRANSFER_OR_DESTORY_COIN_FUNC = "transfer_or_destroy_coin";
5307
- var CHECK_COINS_THRESHOLD_FUNC = "check_coins_threshold";
5308
- var JOIN_FUNC = "join_vec";
5309
- var TRANSFER_ACCOUNT_CAP = "transfer_account_cap";
5310
- var DEEPBOOK_PACKAGE_ID = "0x000000000000000000000000000000000000000000000000000000000000dee9";
5311
- var DEEPBOOK_PUBLISHED_AT = "0x000000000000000000000000000000000000000000000000000000000000dee9";
5312
- var CETUS_PUBLISHED_AT = "0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a";
5313
- var MAINNET_CETUS_GLOBAL_CONFIG_ID = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
5314
- var TESTNET_CETUS_GLOBAL_CONFIG_ID = "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a";
5315
- var ZERO = new import_bn.default(0);
5316
- var ONE = new import_bn.default(1);
5317
- var TWO = new import_bn.default(2);
5318
- var U128 = TWO.pow(new import_bn.default(128));
5319
- var U64_MAX_BN = new import_bn.default("18446744073709551615");
5320
- var U64_MAX = "18446744073709551615";
5321
- var TEN_POW_NINE = 1e9;
5322
- var MAINNET_FLOWX_AMM_CONTAINER_ID = "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511";
5323
- var TESTNET_FLOWX_AMM_CONTAINER_ID = "";
5324
- var TURBOS_VERSIONED = "0xf1cf0e81048df168ebeb1b8030fad24b3e0b53ae827c25053fff0779c1445b6f";
5325
- var MAINNET_AFTERMATH_REGISTRY_ID = "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae";
5326
- var TESTNET_AFTERMATH_REGISTRY_ID = "";
5327
- var MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4";
5328
- var TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = "";
5329
- var MAINNET_AFTERMATH_TREASURY_ID = "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce";
5330
- var TESTNET_AFTERMATH_TREASURY_ID = "";
5331
- var MAINNET_AFTERMATH_INSURANCE_FUND_ID = "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b";
5332
- var TESTNET_AFTERMATH_INSURANCE_FUND_ID = "";
5333
- var MAINNET_AFTERMATH_REFERRAL_VAULT_ID = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
5334
- var TESTNET_AFTERMATH_REFERRAL_VAULT_ID = "";
5335
-
5336
- // src/errors.ts
5337
- function getAggregatorServerErrorMessage(code) {
5338
- switch (code) {
5339
- case 1e4 /* CalculateError */:
5340
- return "Calculate error";
5341
- case 10001 /* NumberTooLarge */:
5342
- return "Input number too large can not fit in target type";
5343
- case 10002 /* NoRouter */:
5344
- return "No router";
5345
- case 10003 /* InsufficientLiquidity */:
5346
- return "Insufficient Liquidity";
5347
- default:
5348
- return "Unknown error";
5438
+ // src/types/CoinAssist.ts
5439
+ var COIN_TYPE = "0x2::coin::Coin";
5440
+ var COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;
5441
+ var GAS_TYPE_ARG = "0x2::sui::SUI";
5442
+ var GAS_TYPE_ARG_LONG = "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
5443
+ var CoinUtils = class _CoinUtils {
5444
+ /**
5445
+ * Get the coin type argument from a SuiMoveObject.
5446
+ *
5447
+ * @param obj The SuiMoveObject to get the coin type argument from.
5448
+ * @returns The coin type argument, or null if it is not found.
5449
+ */
5450
+ static getCoinTypeArg(obj) {
5451
+ const res = obj.type.match(COIN_TYPE_ARG_REGEX);
5452
+ return res ? res[1] : null;
5349
5453
  }
5350
- }
5351
- function mintZeroCoin(txb, coinType) {
5352
- return txb.moveCall({
5353
- target: SuiZeroCoinFn,
5354
- typeArguments: [coinType]
5355
- });
5356
- }
5357
- function buildInputCoin(txb, allCoins, amount, coinType) {
5358
- const usedCoinAsests = CoinUtils.getCoinAssets(coinType, allCoins);
5359
- if (amount === BigInt(0) && usedCoinAsests.length === 0) {
5360
- const zeroCoin = mintZeroCoin(txb, coinType);
5361
- return {
5362
- targetCoin: zeroCoin,
5363
- isMintZeroCoin: true,
5364
- targetCoinAmount: 0
5365
- };
5454
+ /**
5455
+ * Get whether a SuiMoveObject is a SUI coin.
5456
+ *
5457
+ * @param obj The SuiMoveObject to check.
5458
+ * @returns Whether the SuiMoveObject is a SUI coin.
5459
+ */
5460
+ static isSUI(obj) {
5461
+ const arg = _CoinUtils.getCoinTypeArg(obj);
5462
+ return arg ? _CoinUtils.getCoinSymbol(arg) === "SUI" : false;
5366
5463
  }
5367
- console.log("used coin asests", usedCoinAsests);
5368
- let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5369
- console.log("totalCoinBalance", totalCoinBalance);
5370
- console.log("amount", amount);
5371
- if (totalCoinBalance < amount) {
5372
- throw new AggregateError(
5373
- "Insufficient balance when build merge coin",
5374
- "InsufficientBalance" /* InsufficientBalance */
5375
- );
5464
+ /**
5465
+ * Get the coin symbol from a coin type argument.
5466
+ *
5467
+ * @param coinTypeArg The coin type argument to get the symbol from.
5468
+ * @returns The coin symbol.
5469
+ */
5470
+ static getCoinSymbol(coinTypeArg) {
5471
+ return coinTypeArg.substring(coinTypeArg.lastIndexOf(":") + 1);
5376
5472
  }
5377
- if (CoinUtils.isSuiCoin(coinType) && amount <= BigInt(95e10)) {
5378
- const resultCoin = txb.splitCoins(txb.gas, [
5379
- txb.pure.u64(amount.toString())
5380
- ]);
5381
- return {
5382
- targetCoin: resultCoin,
5383
- isMintZeroCoin: true,
5384
- targetCoinAmount: Number(amount.toString())
5385
- };
5473
+ /**
5474
+ * Get the balance of a SuiMoveObject.
5475
+ *
5476
+ * @param obj The SuiMoveObject to get the balance from.
5477
+ * @returns The balance of the SuiMoveObject.
5478
+ */
5479
+ static getBalance(obj) {
5480
+ return BigInt(obj.fields.balance);
5386
5481
  }
5387
- let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
5388
- let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
5389
- if (totalThreeCoinBalance < BigInt(amount)) {
5390
- sortCoinAssets = CoinUtils.sortByBalanceDes(usedCoinAsests);
5482
+ /**
5483
+ * Get the total balance of a list of CoinAsset objects for a given coin address.
5484
+ *
5485
+ * @param objs The list of CoinAsset objects to get the total balance for.
5486
+ * @param coinAddress The coin address to get the total balance for.
5487
+ * @returns The total balance of the CoinAsset objects for the given coin address.
5488
+ */
5489
+ static totalBalance(objs, coinAddress) {
5490
+ let balanceTotal = BigInt(0);
5491
+ objs.forEach((obj) => {
5492
+ if (coinAddress === obj.coinAddress) {
5493
+ balanceTotal += BigInt(obj.balance);
5494
+ }
5495
+ });
5496
+ return balanceTotal;
5391
5497
  }
5392
- let selectedCoinResult = CoinUtils.selectCoinObjectIdGreaterThanOrEqual(
5393
- sortCoinAssets,
5394
- amount
5395
- );
5396
- const [masterCoin, ...mergedCoin] = selectedCoinResult.objectArray;
5397
- if (mergedCoin.length > 0) {
5398
- txb.mergeCoins(
5399
- masterCoin,
5400
- mergedCoin.map((coin) => txb.object(coin))
5401
- );
5498
+ /**
5499
+ * Get the ID of a SuiMoveObject.
5500
+ *
5501
+ * @param obj The SuiMoveObject to get the ID from.
5502
+ * @returns The ID of the SuiMoveObject.
5503
+ */
5504
+ static getID(obj) {
5505
+ return obj.fields.id.id;
5402
5506
  }
5403
- return {
5404
- targetCoin: txb.object(masterCoin),
5405
- isMintZeroCoin: false,
5406
- targetCoinAmount: Number(amount.toString())
5407
- };
5408
- }
5409
- function transferOrDestoryCoin(txb, coinObject, coinType, config2) {
5410
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5411
- if (aggregatorPackage == null) {
5412
- throw new AggregateError(
5413
- "Aggregator package not set",
5414
- "MissAggregatorPackage" /* MissAggregatorPackage */
5415
- );
5416
- }
5417
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5418
- txb.moveCall({
5419
- target: createTarget(
5420
- aggregatorPublishedAt,
5421
- UTILS_MODULE,
5422
- TRANSFER_OR_DESTORY_COIN_FUNC
5423
- ),
5424
- typeArguments: [coinType],
5425
- arguments: [coinObject]
5426
- });
5427
- }
5428
- function checkCoinThresholdAndMergeCoin(txb, coins, coinType, amountLimit, config2) {
5429
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5430
- if (aggregatorPackage == null) {
5431
- throw new AggregateError(
5432
- "Aggregator package not set",
5433
- "MissAggregatorPackage" /* MissAggregatorPackage */
5434
- );
5507
+ /**
5508
+ * Get the coin type from a coin type argument.
5509
+ *
5510
+ * @param coinTypeArg The coin type argument to get the coin type from.
5511
+ * @returns The coin type.
5512
+ */
5513
+ static getCoinTypeFromArg(coinTypeArg) {
5514
+ return `${COIN_TYPE}<${coinTypeArg}>`;
5435
5515
  }
5436
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5437
- const vec = txb.makeMoveVec({
5438
- elements: coins
5439
- });
5440
- txb.moveCall({
5441
- target: createTarget(
5442
- aggregatorPublishedAt,
5443
- UTILS_MODULE,
5444
- CHECK_COINS_THRESHOLD_FUNC
5445
- ),
5446
- typeArguments: [coinType],
5447
- arguments: [vec, txb.pure.u64(amountLimit)]
5448
- });
5449
- const zeroCoin = mintZeroCoin(txb, coinType);
5450
- txb.moveCall({
5451
- target: createTarget(utils.SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
5452
- typeArguments: [coinType],
5453
- arguments: [zeroCoin, vec]
5454
- });
5455
- return zeroCoin;
5456
- }
5457
-
5458
- // src/utils/account_cap.ts
5459
- function getOrCreateAccountCap(txb, client, owner) {
5460
- return __async(this, null, function* () {
5461
- let accountCapStr = yield getAccountCap(client, owner);
5462
- if (accountCapStr !== null) {
5463
- return {
5464
- accountCap: txb.object(accountCapStr),
5465
- isCreate: false
5466
- };
5467
- }
5468
- const accountCap = txb.moveCall({
5469
- target: `${DEEPBOOK_PUBLISHED_AT}::${DEEPBOOK_CLOB_V2_MODULE}::create_account`,
5470
- typeArguments: [],
5471
- arguments: []
5472
- });
5473
- return {
5474
- accountCap,
5475
- isCreate: true
5476
- };
5477
- });
5478
- }
5479
- function getAccountCap(client, owner) {
5480
- return __async(this, null, function* () {
5481
- let limit = 50;
5482
- let cursor = null;
5483
- while (true) {
5484
- const ownedObjects = client.getOwnedObjects({
5485
- owner,
5486
- cursor,
5487
- limit,
5488
- filter: {
5489
- MoveModule: {
5490
- package: DEEPBOOK_PACKAGE_ID,
5491
- module: DEEPBOOK_CUSTODIAN_V2_MODULE
5492
- }
5493
- }
5494
- });
5495
- if (ownedObjects != null && ownedObjects.data != null) {
5496
- if (ownedObjects.data.length !== 0) {
5497
- return ownedObjects.data[0].data.objectId;
5498
- }
5499
- if (ownedObjects.data.length < 50) {
5500
- break;
5501
- }
5502
- } else {
5503
- break;
5504
- }
5505
- }
5506
- return null;
5507
- });
5508
- }
5509
-
5510
- // src/transaction/deepbook.ts
5511
- function deepbookSwapMovecall(swapParams, client, txb, config2) {
5512
- return __async(this, null, function* () {
5513
- const accountCapRes = yield getOrCreateAccountCap(
5514
- txb,
5515
- client,
5516
- config2.getWallet()
5517
- );
5518
- const accountCap = accountCapRes.accountCap;
5519
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5520
- if (aggregatorPackage == null) {
5521
- throw new AggregateError(
5522
- "Aggregator package not set",
5523
- "MissAggregatorPackage" /* MissAggregatorPackage */
5524
- );
5525
- }
5526
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5527
- if (swapParams.a2b) {
5528
- if (swapParams.coinA == null) {
5529
- throw new AggregateError(
5530
- "coinA is required",
5531
- "MissCoinA" /* MissCoinA */
5532
- );
5533
- }
5534
- } else {
5535
- if (swapParams.coinB == null) {
5536
- throw new AggregateError(
5537
- "coinB is required",
5538
- "MissCoinB" /* MissCoinB */
5539
- );
5540
- }
5541
- }
5542
- const args = swapParams.a2b ? [
5543
- txb.object(swapParams.poolId),
5544
- swapParams.amount,
5545
- txb.pure.u64(swapParams.amountLimit),
5546
- swapParams.coinA,
5547
- accountCap,
5548
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5549
- txb.object(CLOCK_ADDRESS)
5550
- ] : [
5551
- txb.object(swapParams.poolId),
5552
- swapParams.amount,
5553
- txb.pure.u64(swapParams.amountLimit),
5554
- swapParams.coinB,
5555
- accountCap,
5556
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5557
- txb.object(CLOCK_ADDRESS)
5558
- ];
5559
- let func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5560
- const target = createTarget(aggregatorPublishedAt, DEEPBOOK_MODULE, func);
5561
- const res = txb.moveCall({
5562
- target,
5563
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5564
- arguments: args
5565
- });
5566
- if (accountCapRes.isCreate) {
5567
- const target2 = createTarget(
5568
- aggregatorPublishedAt,
5569
- DEEPBOOK_MODULE,
5570
- TRANSFER_ACCOUNT_CAP
5571
- );
5572
- txb.moveCall({
5573
- target: target2,
5574
- typeArguments: [],
5575
- arguments: [accountCap]
5576
- });
5577
- }
5578
- return {
5579
- targetCoin: res[0],
5580
- amountIn: res[1],
5581
- amountOut: res[2],
5582
- txb
5583
- };
5584
- });
5585
- }
5586
-
5587
- // src/transaction/kriya.ts
5588
- function kriyaSwapMovecall(swapParams, txb, config2) {
5589
- return __async(this, null, function* () {
5590
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5591
- if (aggregatorPackage == null) {
5592
- throw new AggregateError("Aggregator package not set", "MissAggregatorPackage" /* MissAggregatorPackage */);
5593
- }
5594
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5595
- if (swapParams.a2b) {
5596
- if (swapParams.coinA == null) {
5597
- throw new AggregateError("coinA is required", "MissCoinA" /* MissCoinA */);
5598
- }
5599
- } else {
5600
- if (swapParams.coinB == null) {
5601
- throw new AggregateError("coinB is required", "MissCoinB" /* MissCoinB */);
5516
+ /**
5517
+ * Get the CoinAsset objects for a given coin type.
5518
+ *
5519
+ * @param coinType The coin type to get the CoinAsset objects for.
5520
+ * @param allSuiObjects The list of all SuiMoveObjects.
5521
+ * @returns The CoinAsset objects for the given coin type.
5522
+ */
5523
+ static getCoinAssets(coinType, allSuiObjects) {
5524
+ const coins = [];
5525
+ allSuiObjects.forEach((anObj) => {
5526
+ if (normalizeCoinType(anObj.coinAddress) === normalizeCoinType(coinType)) {
5527
+ coins.push(anObj);
5602
5528
  }
5603
- }
5604
- const args = swapParams.a2b ? [
5605
- txb.object(swapParams.poolId),
5606
- swapParams.amount,
5607
- txb.pure.u64(swapParams.amountLimit),
5608
- swapParams.coinA,
5609
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5610
- ] : [
5611
- txb.object(swapParams.poolId),
5612
- swapParams.amount,
5613
- txb.pure.u64(swapParams.amountLimit),
5614
- swapParams.coinB,
5615
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5616
- ];
5617
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5618
- const target = createTarget(aggregatorPublishedAt, KRIYA_MODULE, func);
5619
- const res = txb.moveCall({
5620
- target,
5621
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5622
- arguments: args
5623
5529
  });
5624
- return {
5625
- targetCoin: res[0],
5626
- amountIn: res[1],
5627
- amountOut: res[2],
5628
- txb
5629
- };
5630
- });
5631
- }
5632
-
5633
- // src/config.ts
5634
- var ENV = /* @__PURE__ */ ((ENV2) => {
5635
- ENV2[ENV2["MAINNET"] = 0] = "MAINNET";
5636
- ENV2[ENV2["TESTNET"] = 1] = "TESTNET";
5637
- return ENV2;
5638
- })(ENV || {});
5639
- var AggregatorConfig = class {
5640
- constructor(aggregatorUrl, fullNodeUrl, wallet, packages, env) {
5641
- this.aggregatorUrl = aggregatorUrl;
5642
- this.fullNodeUrl = fullNodeUrl;
5643
- this.wallet = wallet;
5644
- this.packages = packages;
5645
- this.env = env;
5646
- }
5647
- getAggregatorUrl() {
5648
- return this.aggregatorUrl;
5649
- }
5650
- getFullNodeUrl() {
5651
- return this.fullNodeUrl;
5652
- }
5653
- getWallet() {
5654
- return this.wallet;
5655
- }
5656
- getENV() {
5657
- return this.env;
5658
- }
5659
- getPackages() {
5660
- return this.packages;
5661
- }
5662
- getPackage(packageName) {
5663
- return this.packages.find((pkg) => pkg.packageName === packageName);
5664
- }
5665
- addPackage(newPackage) {
5666
- if (!newPackage.packageName || !newPackage.packageId || !newPackage.publishedAt) {
5667
- throw new Error("Invalid package data");
5668
- }
5669
- this.packages.push(newPackage);
5670
- }
5671
- removePackageById(packageId) {
5672
- this.packages = this.packages.filter((pkg) => pkg.packageId !== packageId);
5673
- }
5674
- findPackageById(packageId) {
5675
- return this.packages.find((pkg) => pkg.packageId === packageId);
5530
+ return coins;
5676
5531
  }
5677
- };
5678
-
5679
- // src/transaction/cetus.ts
5680
- function cetusFlashSwapMovecall(swapParams, txb, config2) {
5681
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5682
- if (aggregatorPackage == null) {
5683
- throw new AggregateError(
5684
- "Aggregator package not set",
5685
- "MissAggregatorPackage" /* MissAggregatorPackage */
5686
- );
5532
+ /**
5533
+ * Get whether a coin address is a SUI coin.
5534
+ *
5535
+ * @param coinAddress The coin address to check.
5536
+ * @returns Whether the coin address is a SUI coin.
5537
+ */
5538
+ static isSuiCoin(coinAddress) {
5539
+ return extractStructTagFromType(coinAddress).full_address === GAS_TYPE_ARG;
5687
5540
  }
5688
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5689
- const globalConfigId = config2.getENV() === 0 /* MAINNET */ ? MAINNET_CETUS_GLOBAL_CONFIG_ID : TESTNET_CETUS_GLOBAL_CONFIG_ID;
5690
- const hasPartner = swapParams.partner != null && swapParams.partner.length > 0;
5691
- const args = hasPartner ? [
5692
- txb.object(globalConfigId),
5693
- txb.object(swapParams.poolId),
5694
- swapParams.amount,
5695
- txb.pure.u64(swapParams.amountLimit),
5696
- txb.pure.bool(swapParams.byAmountIn),
5697
- txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
5698
- txb.object(swapParams.partner.toString()),
5699
- txb.object(CLOCK_ADDRESS)
5700
- ] : [
5701
- txb.object(globalConfigId),
5702
- txb.object(swapParams.poolId),
5703
- swapParams.amount,
5704
- txb.pure.u64(swapParams.amountLimit),
5705
- txb.pure.bool(swapParams.byAmountIn),
5706
- txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
5707
- txb.object(CLOCK_ADDRESS)
5708
- ];
5709
- let func;
5710
- if (hasPartner) {
5711
- if (swapParams.a2b) {
5712
- func = FlashSwapWithPartnerA2BFunc;
5713
- } else {
5714
- func = FlashSwapWithPartnerB2AFunc;
5715
- }
5716
- } else {
5717
- if (swapParams.a2b) {
5718
- func = FlashSwapA2BFunc;
5719
- } else {
5720
- func = FlashSwapB2AFunc;
5721
- }
5541
+ /**
5542
+ * Select the CoinAsset objects from a list of CoinAsset objects that have a balance greater than or equal to a given amount.
5543
+ *
5544
+ * @param coins The list of CoinAsset objects to select from.
5545
+ * @param amount The amount to select CoinAsset objects with a balance greater than or equal to.
5546
+ * @param exclude A list of CoinAsset objects to exclude from the selection.
5547
+ * @returns The CoinAsset objects that have a balance greater than or equal to the given amount.
5548
+ */
5549
+ static selectCoinObjectIdGreaterThanOrEqual(coins, amount, exclude = []) {
5550
+ const selectedResult = _CoinUtils.selectCoinAssetGreaterThanOrEqual(coins, amount, exclude);
5551
+ const objectArray = selectedResult.selectedCoins.map((item) => item.coinObjectId);
5552
+ const remainCoins = selectedResult.remainingCoins;
5553
+ const amountArray = selectedResult.selectedCoins.map((item) => item.balance.toString());
5554
+ return { objectArray, remainCoins, amountArray };
5722
5555
  }
5723
- const target = createTarget(aggregatorPublishedAt, CETUS_MODULE, func);
5724
- const moveCallRes = txb.moveCall({
5725
- target,
5726
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5727
- arguments: args
5728
- });
5729
- return {
5730
- targetCoin: moveCallRes[0],
5731
- flashReceipt: moveCallRes[1],
5732
- payAmount: moveCallRes[2],
5733
- swapedAmount: moveCallRes[3],
5734
- txb
5735
- };
5736
- }
5737
- function cetusRepayFlashSwapMovecall(repayParams, txb, config2) {
5738
- return __async(this, null, function* () {
5739
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5740
- if (aggregatorPackage == null) {
5741
- throw new AggregateError(
5742
- "Aggregator package not set",
5743
- "MissAggregatorPackage" /* MissAggregatorPackage */
5744
- );
5745
- }
5746
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5747
- const globalConfigId = config2.getENV() === 0 /* MAINNET */ ? MAINNET_CETUS_GLOBAL_CONFIG_ID : TESTNET_CETUS_GLOBAL_CONFIG_ID;
5748
- const hasPartner = repayParams.partner != null && repayParams.partner.length > 0;
5749
- if (repayParams.a2b) {
5750
- if (repayParams.coinA == null) {
5751
- throw new AggregateError(
5752
- "coinA is required",
5753
- "MissCoinA" /* MissCoinA */
5754
- );
5755
- }
5756
- } else {
5757
- if (repayParams.coinB == null) {
5758
- throw new AggregateError(
5759
- "coinB is required",
5760
- "MissCoinB" /* MissCoinB */
5761
- );
5762
- }
5556
+ /**
5557
+ * Select the CoinAsset objects from a list of CoinAsset objects that have a balance greater than or equal to a given amount.
5558
+ *
5559
+ * @param coins The list of CoinAsset objects to select from.
5560
+ * @param amount The amount to select CoinAsset objects with a balance greater than or equal to.
5561
+ * @param exclude A list of CoinAsset objects to exclude from the selection.
5562
+ * @returns The CoinAsset objects that have a balance greater than or equal to the given amount.
5563
+ */
5564
+ static selectCoinAssetGreaterThanOrEqual(coins, amount, exclude = []) {
5565
+ const sortedCoins = _CoinUtils.sortByBalance(coins.filter((c) => !exclude.includes(c.coinObjectId)));
5566
+ const total = _CoinUtils.calculateTotalBalance(sortedCoins);
5567
+ if (total < amount) {
5568
+ return { selectedCoins: [], remainingCoins: sortedCoins };
5763
5569
  }
5764
- let args;
5765
- if (hasPartner) {
5766
- if (repayParams.a2b) {
5767
- args = [
5768
- txb.object(globalConfigId),
5769
- txb.object(repayParams.poolId),
5770
- repayParams.coinA,
5771
- repayParams.receipt,
5772
- txb.object(repayParams.partner)
5773
- ];
5774
- } else {
5775
- args = [
5776
- txb.object(globalConfigId),
5777
- txb.object(repayParams.poolId),
5778
- repayParams.coinB,
5779
- repayParams.receipt,
5780
- txb.object(repayParams.partner)
5781
- ];
5782
- }
5783
- } else {
5784
- if (repayParams.a2b) {
5785
- args = [
5786
- txb.object(globalConfigId),
5787
- txb.object(repayParams.poolId),
5788
- repayParams.coinA,
5789
- repayParams.receipt
5790
- ];
5791
- } else {
5792
- args = [
5793
- txb.object(globalConfigId),
5794
- txb.object(repayParams.poolId),
5795
- repayParams.coinB,
5796
- repayParams.receipt
5797
- ];
5798
- }
5570
+ if (total === amount) {
5571
+ return { selectedCoins: sortedCoins, remainingCoins: [] };
5799
5572
  }
5800
- let func;
5801
- if (hasPartner) {
5802
- if (repayParams.a2b) {
5803
- func = REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC;
5804
- } else {
5805
- func = REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC;
5573
+ let sum2 = BigInt(0);
5574
+ const selectedCoins = [];
5575
+ const remainingCoins = [...sortedCoins];
5576
+ while (sum2 < total) {
5577
+ const target = amount - sum2;
5578
+ const coinWithSmallestSufficientBalanceIndex = remainingCoins.findIndex((c) => c.balance >= target);
5579
+ if (coinWithSmallestSufficientBalanceIndex !== -1) {
5580
+ selectedCoins.push(remainingCoins[coinWithSmallestSufficientBalanceIndex]);
5581
+ remainingCoins.splice(coinWithSmallestSufficientBalanceIndex, 1);
5582
+ break;
5806
5583
  }
5807
- } else {
5808
- if (repayParams.a2b) {
5809
- func = REPAY_FLASH_SWAP_A2B_FUNC;
5810
- } else {
5811
- func = REPAY_FLASH_SWAP_B2A_FUNC;
5584
+ const coinWithLargestBalance = remainingCoins.pop();
5585
+ if (coinWithLargestBalance.balance > 0) {
5586
+ selectedCoins.push(coinWithLargestBalance);
5587
+ sum2 += coinWithLargestBalance.balance;
5812
5588
  }
5813
5589
  }
5814
- const target = createTarget(aggregatorPublishedAt, CETUS_MODULE, func);
5815
- const res = txb.moveCall({
5816
- target,
5817
- typeArguments: [repayParams.coinAType, repayParams.coinBType],
5818
- arguments: args
5819
- });
5820
- return {
5821
- repayTargetCoin: res[0]
5822
- };
5823
- });
5824
- }
5825
- function cetusSwapWithOutLimit(swapParams, fromCoin, txb, config2) {
5826
- return __async(this, null, function* () {
5827
- const flashResult = cetusFlashSwapMovecall(swapParams, txb, config2);
5828
- const repayCoinA = swapParams.a2b ? fromCoin : void 0;
5829
- const repayCoinB = swapParams.a2b ? void 0 : fromCoin;
5830
- const repayParams = {
5831
- poolId: swapParams.poolId,
5832
- a2b: swapParams.a2b,
5833
- coinA: repayCoinA,
5834
- coinB: repayCoinB,
5835
- receipt: flashResult.flashReceipt,
5836
- coinAType: swapParams.coinAType,
5837
- coinBType: swapParams.coinBType,
5838
- partner: swapParams.partner
5839
- };
5840
- let nextInputAmount;
5841
- if (swapParams.byAmountIn) {
5842
- nextInputAmount = flashResult.swapedAmount;
5843
- } else {
5844
- nextInputAmount = flashResult.payAmount;
5845
- }
5846
- const repayResult = yield cetusRepayFlashSwapMovecall(
5847
- repayParams,
5848
- txb,
5849
- config2
5850
- );
5851
- return {
5852
- flashTargetCoin: flashResult.targetCoin,
5853
- repayTargetCoin: repayResult.repayTargetCoin,
5854
- nextInputAmount
5855
- };
5856
- });
5857
- }
5858
-
5859
- // src/math.ts
5860
- var import_bn2 = __toESM(require_bn());
5861
- function CalculateAmountLimit(expectAmount, byAmountIn, slippage) {
5862
- let amountLimit = ZERO;
5863
- if (byAmountIn) {
5864
- amountLimit = expectAmount.mul(new import_bn2.default(TEN_POW_NINE - slippage * TEN_POW_NINE)).div(new import_bn2.default(TEN_POW_NINE));
5865
- } else {
5866
- amountLimit = expectAmount.mul(new import_bn2.default(TEN_POW_NINE + slippage * TEN_POW_NINE)).div(new import_bn2.default(TEN_POW_NINE));
5590
+ return { selectedCoins: _CoinUtils.sortByBalance(selectedCoins), remainingCoins: _CoinUtils.sortByBalance(remainingCoins) };
5867
5591
  }
5868
- return Number(amountLimit.toString());
5869
- }
5870
- var MAX_SQER_PRICE_X64 = "79226673515401279992447579055";
5871
- var MIN_SQER_PRICE_X64 = "4295048016";
5872
- function GetDefaultSqrtPriceLimit(a2b) {
5873
- if (a2b) {
5874
- return new import_bn2.default(MIN_SQER_PRICE_X64);
5875
- } else {
5876
- return new import_bn2.default(MAX_SQER_PRICE_X64);
5592
+ /**
5593
+ * Sort the CoinAsset objects by their balance.
5594
+ *
5595
+ * @param coins The CoinAsset objects to sort.
5596
+ * @returns The sorted CoinAsset objects.
5597
+ */
5598
+ static sortByBalance(coins) {
5599
+ return coins.sort((a, b) => a.balance < b.balance ? -1 : a.balance > b.balance ? 1 : 0);
5877
5600
  }
5878
- }
5879
- function sqrtPriceX64ToPrice(sqrtPriceStr, decimalsA, decimalsB) {
5880
- const sqrtPriceX64 = new decimal_default(sqrtPriceStr).mul(decimal_default.pow(2, -64));
5881
- return sqrtPriceX64.pow(2).mul(decimal_default.pow(10, decimalsA - decimalsB));
5882
- }
5883
-
5884
- // src/transaction/flowx.ts
5885
- function flowxAmmSwapMovecall(swapParams, txb, config2) {
5886
- return __async(this, null, function* () {
5887
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5888
- if (aggregatorPackage == null) {
5889
- throw new AggregateError(
5890
- "Aggregator package not set",
5891
- "MissAggregatorPackage" /* MissAggregatorPackage */
5892
- );
5893
- }
5894
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5895
- if (swapParams.a2b) {
5896
- if (swapParams.coinA == null) {
5897
- throw new AggregateError(
5898
- "coinA is required",
5899
- "MissCoinA" /* MissCoinA */
5900
- );
5901
- }
5902
- } else {
5903
- if (swapParams.coinB == null) {
5904
- throw new AggregateError(
5905
- "coinB is required",
5906
- "MissCoinB" /* MissCoinB */
5907
- );
5908
- }
5909
- }
5910
- const containerID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_FLOWX_AMM_CONTAINER_ID : TESTNET_FLOWX_AMM_CONTAINER_ID;
5911
- const swapCoin = swapParams.a2b ? swapParams.coinA : swapParams.coinB;
5912
- const args = [
5913
- txb.object(containerID),
5914
- swapParams.amount,
5915
- txb.pure.u64(swapParams.amountLimit),
5916
- swapCoin,
5917
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5918
- ];
5919
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5920
- const target = createTarget(aggregatorPublishedAt, FLOWX_AMM_MODULE, func);
5921
- const res = txb.moveCall({
5922
- target,
5923
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5924
- arguments: args
5925
- });
5926
- return {
5927
- targetCoin: res[0],
5928
- amountIn: res[1],
5929
- amountOut: res[2],
5930
- txb
5931
- };
5932
- });
5933
- }
5934
-
5935
- // src/transaction/turbos.ts
5936
- function turbosSwapMovecall(swapParams, txb, config2) {
5937
- return __async(this, null, function* () {
5938
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5939
- if (aggregatorPackage == null) {
5940
- throw new AggregateError(
5941
- "Aggregator package not set",
5942
- "MissAggregatorPackage" /* MissAggregatorPackage */
5943
- );
5944
- }
5945
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5946
- if (swapParams.a2b) {
5947
- if (swapParams.coinA == null) {
5948
- throw new AggregateError(
5949
- "coinA is required",
5950
- "MissCoinA" /* MissCoinA */
5951
- );
5952
- }
5953
- } else {
5954
- if (swapParams.coinB == null) {
5955
- throw new AggregateError(
5956
- "coinB is required",
5957
- "MissCoinB" /* MissCoinB */
5958
- );
5959
- }
5960
- }
5961
- const sqrtPriceLimit = swapParams.a2b ? "4295048016" : "79226673515401279992447579055";
5962
- const args = swapParams.a2b ? [
5963
- txb.object(swapParams.poolId),
5964
- swapParams.amount,
5965
- txb.pure.u64(swapParams.amountLimit),
5966
- swapParams.coinA,
5967
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5968
- txb.pure.u128(sqrtPriceLimit),
5969
- txb.object(CLOCK_ADDRESS),
5970
- txb.object(TURBOS_VERSIONED)
5971
- ] : [
5972
- txb.object(swapParams.poolId),
5973
- swapParams.amount,
5974
- txb.pure.u64(swapParams.amountLimit),
5975
- swapParams.coinB,
5976
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5977
- txb.pure.u128(sqrtPriceLimit),
5978
- txb.object(CLOCK_ADDRESS),
5979
- txb.object(TURBOS_VERSIONED)
5980
- ];
5981
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5982
- const target = createTarget(aggregatorPublishedAt, TURBOS_MODULE, func);
5983
- const res = txb.moveCall({
5984
- target,
5985
- typeArguments: [
5986
- swapParams.coinAType,
5987
- swapParams.coinBType,
5988
- swapParams.feeType
5989
- ],
5990
- arguments: args
5991
- });
5992
- return {
5993
- targetCoin: res[0],
5994
- amountIn: res[1],
5995
- amountOut: res[2],
5996
- txb
5997
- };
5998
- });
5999
- }
6000
-
6001
- // src/transaction/aftermath.ts
6002
- function AftermathAmmSwapMovecall(swapParams, txb, config2) {
6003
- return __async(this, null, function* () {
6004
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
6005
- if (aggregatorPackage == null) {
6006
- throw new AggregateError(
6007
- "Aggregator package not set",
6008
- "MissAggregatorPackage" /* MissAggregatorPackage */
6009
- );
6010
- }
6011
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
6012
- if (swapParams.a2b) {
6013
- if (swapParams.coinA == null) {
6014
- throw new AggregateError(
6015
- "coinA is required",
6016
- "MissCoinA" /* MissCoinA */
6017
- );
6018
- }
6019
- } else {
6020
- if (swapParams.coinB == null) {
6021
- throw new AggregateError(
6022
- "coinB is required",
6023
- "MissCoinB" /* MissCoinB */
6024
- );
6025
- }
6026
- }
6027
- const poolRegistryID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_REGISTRY_ID : TESTNET_AFTERMATH_REGISTRY_ID;
6028
- const vaultID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID : TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID;
6029
- const treasuryID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_TREASURY_ID : TESTNET_AFTERMATH_TREASURY_ID;
6030
- const insuranceFundID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_INSURANCE_FUND_ID : TESTNET_AFTERMATH_INSURANCE_FUND_ID;
6031
- const referrealVaultID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_REFERRAL_VAULT_ID : TESTNET_AFTERMATH_REFERRAL_VAULT_ID;
6032
- const swapCoin = swapParams.a2b ? swapParams.coinA : swapParams.coinB;
6033
- const slippageArg = (1 - swapParams.slippage) * 1e18;
6034
- const args = [
6035
- txb.object(swapParams.poolId),
6036
- txb.object(poolRegistryID),
6037
- txb.object(vaultID),
6038
- txb.object(treasuryID),
6039
- txb.object(insuranceFundID),
6040
- txb.object(referrealVaultID),
6041
- swapParams.amount,
6042
- txb.pure.u64(swapParams.amountLimit),
6043
- txb.pure.u64(swapParams.amountOut),
6044
- txb.pure.u64(slippageArg),
6045
- swapCoin,
6046
- txb.pure.bool(swapParams.useFullInputCoinAmount)
6047
- ];
6048
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
6049
- const target = createTarget(aggregatorPublishedAt, AFTERMATH_MODULE, func);
6050
- const res = txb.moveCall({
6051
- target,
6052
- typeArguments: [
6053
- swapParams.coinAType,
6054
- swapParams.coinBType,
6055
- swapParams.lpSupplyType
6056
- ],
6057
- arguments: args
6058
- });
6059
- return {
6060
- targetCoin: res[0],
6061
- amountIn: res[1],
6062
- amountOut: res[2],
6063
- txb
6064
- };
6065
- });
6066
- }
5601
+ static sortByBalanceDes(coins) {
5602
+ return coins.sort((a, b) => a.balance > b.balance ? -1 : a.balance < b.balance ? 0 : 1);
5603
+ }
5604
+ /**
5605
+ * Calculate the total balance of a list of CoinAsset objects.
5606
+ *
5607
+ * @param coins The list of CoinAsset objects to calculate the total balance for.
5608
+ * @returns The total balance of the CoinAsset objects.
5609
+ */
5610
+ static calculateTotalBalance(coins) {
5611
+ return coins.reduce((partialSum, c) => partialSum + c.balance, BigInt(0));
5612
+ }
5613
+ };
6067
5614
 
6068
- // src/transaction/router.ts
6069
- function expectInputRouterSwap(client, params, txb, fromCoin, config2, partner) {
6070
- return __async(this, null, function* () {
6071
- const splitAmounts = params.routers.map(
6072
- (router) => router.amountIn.toString()
6073
- );
6074
- const targetCoins = [];
6075
- const fromCoins = txb.splitCoins(fromCoin, splitAmounts);
6076
- for (let i = 0; i < params.routers.length; i++) {
6077
- const router = params.routers[i];
6078
- let intermediateTargetCoin;
6079
- let nextFromCoin = fromCoins[i];
6080
- let nextFlashAmount = txb.pure.u64(splitAmounts[i]);
6081
- for (let j = 0; j < router.path.length; j++) {
6082
- const firstPathPool = j === 0;
6083
- const path = router.path[j];
6084
- if (path.provider === CETUS_DEX) {
6085
- const swapParams = {
6086
- poolId: path.id,
6087
- amount: nextFlashAmount,
6088
- amountLimit: "0",
6089
- a2b: path.a2b,
6090
- byAmountIn: true,
6091
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b),
6092
- partner,
6093
- coinAType: path.a2b ? path.from : path.target,
6094
- coinBType: path.a2b ? path.target : path.from
6095
- };
6096
- firstPathPool && router.path.length > 1;
6097
- const swapResult = yield cetusSwapWithOutLimit(
6098
- swapParams,
6099
- nextFromCoin,
6100
- txb,
6101
- config2
6102
- );
6103
- transferOrDestoryCoin(
6104
- txb,
6105
- swapResult.repayTargetCoin,
6106
- path.from,
6107
- config2
6108
- );
6109
- intermediateTargetCoin = swapResult.flashTargetCoin;
6110
- nextFromCoin = intermediateTargetCoin;
6111
- nextFlashAmount = swapResult.nextInputAmount;
6112
- }
6113
- if (path.provider === DEEPBOOK_DEX) {
6114
- const coinA = path.a2b ? nextFromCoin : void 0;
6115
- const coinB = path.a2b ? void 0 : nextFromCoin;
6116
- const swapParams = {
6117
- poolId: path.id,
6118
- a2b: path.a2b,
6119
- amount: nextFlashAmount,
6120
- amountLimit: 0,
6121
- coinA,
6122
- coinB,
6123
- useFullInputCoinAmount: !firstPathPool,
6124
- coinAType: path.a2b ? path.from : path.target,
6125
- coinBType: path.a2b ? params.targetCoinType : path.from
6126
- };
6127
- const deepbookSwapResult = yield deepbookSwapMovecall(
6128
- swapParams,
6129
- client,
6130
- txb,
6131
- config2
6132
- );
6133
- intermediateTargetCoin = deepbookSwapResult.targetCoin;
6134
- nextFromCoin = intermediateTargetCoin;
6135
- nextFlashAmount = deepbookSwapResult.amountOut;
6136
- }
6137
- if (path.provider === KRIYA_DEX) {
6138
- const coinA = path.a2b ? nextFromCoin : void 0;
6139
- const coinB = path.a2b ? void 0 : nextFromCoin;
6140
- const swapParams = {
6141
- poolId: path.id,
6142
- amount: nextFlashAmount,
6143
- amountLimit: 0,
6144
- a2b: path.a2b,
6145
- byAmountIn: true,
6146
- coinA,
6147
- coinB,
6148
- useFullInputCoinAmount: !firstPathPool,
6149
- coinAType: path.a2b ? path.from : path.target,
6150
- coinBType: path.a2b ? path.target : path.from
6151
- };
6152
- const swapResult = yield kriyaSwapMovecall(swapParams, txb, config2);
6153
- intermediateTargetCoin = swapResult.targetCoin;
6154
- nextFromCoin = intermediateTargetCoin;
6155
- nextFlashAmount = swapResult.amountOut;
6156
- }
6157
- if (path.provider === FLOWX_AMM) {
6158
- const coinA = path.a2b ? nextFromCoin : void 0;
6159
- const coinB = path.a2b ? void 0 : nextFromCoin;
6160
- const swapParams = {
6161
- amount: nextFlashAmount,
6162
- amountLimit: 0,
6163
- a2b: path.a2b,
6164
- byAmountIn: true,
6165
- coinA,
6166
- coinB,
6167
- useFullInputCoinAmount: !firstPathPool,
6168
- coinAType: path.a2b ? path.from : path.target,
6169
- coinBType: path.a2b ? path.target : path.from
6170
- };
6171
- const swapResult = yield flowxAmmSwapMovecall(swapParams, txb, config2);
6172
- intermediateTargetCoin = swapResult.targetCoin;
6173
- nextFromCoin = intermediateTargetCoin;
6174
- nextFlashAmount = swapResult.amountOut;
6175
- }
6176
- if (path.provider === TURBOS_DEX) {
6177
- const coinA = path.a2b ? nextFromCoin : void 0;
6178
- const coinB = path.a2b ? void 0 : nextFromCoin;
6179
- let feeType = "";
6180
- if (path.extendedDetails != null && path.extendedDetails.turbosFeeType != null) {
6181
- feeType = path.extendedDetails.turbosFeeType;
6182
- } else {
6183
- throw new AggregateError(
6184
- "Build turbos swap movecall error: ",
6185
- "MissTurbosFeeType" /* MissTurbosFeeType */
6186
- );
6187
- }
6188
- const swapParams = {
6189
- poolId: path.id,
6190
- amount: nextFlashAmount,
6191
- amountLimit: 0,
6192
- a2b: path.a2b,
6193
- byAmountIn: true,
6194
- coinA,
6195
- coinB,
6196
- useFullInputCoinAmount: !firstPathPool,
6197
- coinAType: path.a2b ? path.from : path.target,
6198
- coinBType: path.a2b ? path.target : path.from,
6199
- feeType
6200
- };
6201
- const swapResult = yield turbosSwapMovecall(swapParams, txb, config2);
6202
- intermediateTargetCoin = swapResult.targetCoin;
6203
- nextFromCoin = intermediateTargetCoin;
6204
- nextFlashAmount = swapResult.amountOut;
6205
- }
6206
- if (path.provider === AFTERMATH_AMM) {
6207
- const coinA = path.a2b ? nextFromCoin : void 0;
6208
- const coinB = path.a2b ? void 0 : nextFromCoin;
6209
- let lpSupplyType = "";
6210
- if (path.extendedDetails != null && path.extendedDetails.aftermathLpSupplyType != null) {
6211
- lpSupplyType = path.extendedDetails.aftermathLpSupplyType;
6212
- } else {
6213
- throw new AggregateError(
6214
- "Build aftermath swap movecall error: ",
6215
- "MissAftermathLpSupplyType" /* MissAftermathLpSupplyType */
6216
- );
6217
- }
6218
- const swapParams = {
6219
- poolId: path.id,
6220
- amount: nextFlashAmount,
6221
- amountOut: path.amountOut,
6222
- amountLimit: 0,
6223
- a2b: path.a2b,
6224
- byAmountIn: true,
6225
- coinA,
6226
- coinB,
6227
- useFullInputCoinAmount: !firstPathPool,
6228
- coinAType: path.a2b ? path.from : path.target,
6229
- coinBType: path.a2b ? path.target : path.from,
6230
- slippage: params.slippage,
6231
- lpSupplyType
6232
- };
6233
- const swapResult = yield AftermathAmmSwapMovecall(
6234
- swapParams,
6235
- txb,
6236
- config2
6237
- );
6238
- intermediateTargetCoin = swapResult.targetCoin;
6239
- nextFromCoin = intermediateTargetCoin;
6240
- nextFlashAmount = swapResult.amountOut;
6241
- }
6242
- }
6243
- targetCoins.push(nextFromCoin);
6244
- }
6245
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config2);
6246
- return targetCoins;
6247
- });
6248
- }
6249
- function expectOutputRouterSwap(params, txb, fromCoin, config2, partner) {
6250
- return __async(this, null, function* () {
6251
- const splitAmounts = params.routers.map(
6252
- (router) => router.amountOut.toString()
6253
- );
6254
- const returnCoins = [];
6255
- const receipts = [];
6256
- const targetCoins = [];
6257
- for (let i = 0; i < params.routers.length; i++) {
6258
- const router = params.routers[i];
6259
- let nextFlashAmount = txb.pure.u64(splitAmounts[i]);
6260
- for (let j = router.path.length - 1; j >= 0; j--) {
6261
- const path = router.path[j];
6262
- const coinAType = path.a2b ? path.from : path.target;
6263
- const coinBType = path.a2b ? path.target : path.from;
6264
- const swapParams = {
6265
- poolId: path.id,
6266
- amount: nextFlashAmount,
6267
- amountLimit: U64_MAX,
6268
- a2b: path.a2b,
6269
- byAmountIn: false,
6270
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b),
6271
- //
6272
- partner,
6273
- coinAType,
6274
- coinBType
6275
- };
6276
- const flashSwapResult = cetusFlashSwapMovecall(swapParams, txb, config2);
6277
- nextFlashAmount = flashSwapResult.payAmount;
6278
- returnCoins.unshift(flashSwapResult.targetCoin);
6279
- receipts.unshift(flashSwapResult.flashReceipt);
6280
- }
6281
- let nextRepayCoin = fromCoin;
6282
- for (let j = 0; j < router.path.length; j++) {
6283
- const path = router.path[j];
6284
- const coinAType = path.a2b ? path.from : path.target;
6285
- const coinBType = path.a2b ? path.target : path.from;
6286
- const repayParams = {
6287
- poolId: path.id,
6288
- a2b: path.a2b,
6289
- partner,
6290
- coinA: path.a2b ? nextRepayCoin : void 0,
6291
- coinB: path.a2b ? void 0 : nextRepayCoin,
6292
- receipt: receipts[j],
6293
- coinAType,
6294
- coinBType
6295
- };
6296
- const repayResult = yield cetusRepayFlashSwapMovecall(
6297
- repayParams,
6298
- txb,
6299
- config2
6300
- );
6301
- nextRepayCoin = returnCoins[j];
6302
- if (j === 0) {
6303
- fromCoin = repayResult.repayTargetCoin;
6304
- } else {
6305
- transferOrDestoryCoin(
6306
- txb,
6307
- repayResult.repayTargetCoin,
6308
- path.from,
6309
- config2
6310
- );
6311
- }
6312
- if (j === router.path.length - 1) {
6313
- targetCoins.push(nextRepayCoin);
6314
- }
6315
- }
6316
- }
6317
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config2);
6318
- return targetCoins;
6319
- });
5615
+ // src/errors.ts
5616
+ function getAggregatorServerErrorMessage(code) {
5617
+ switch (code) {
5618
+ case 1e4 /* CalculateError */:
5619
+ return "Calculate error";
5620
+ case 10001 /* NumberTooLarge */:
5621
+ return "Input number too large can not fit in target type";
5622
+ case 10002 /* NoRouter */:
5623
+ return "No router";
5624
+ case 10003 /* InsufficientLiquidity */:
5625
+ return "Insufficient Liquidity";
5626
+ default:
5627
+ return "Unknown error";
5628
+ }
6320
5629
  }
6321
5630
 
6322
- // src/client.ts
6323
- var import_bn4 = __toESM(require_bn());
6324
-
6325
5631
  // src/utils/coin.ts
6326
5632
  function completionCoin(s) {
6327
5633
  const index = s.indexOf("::");
@@ -6353,6 +5659,61 @@ function compareCoins(coinA, coinB) {
6353
5659
  }
6354
5660
  return true;
6355
5661
  }
5662
+ function mintZeroCoin(txb, coinType) {
5663
+ return txb.moveCall({
5664
+ target: SuiZeroCoinFn,
5665
+ typeArguments: [coinType]
5666
+ });
5667
+ }
5668
+ function buildInputCoin(txb, allCoins, amount, coinType) {
5669
+ const usedCoinAsests = CoinUtils.getCoinAssets(coinType, allCoins);
5670
+ if (amount === BigInt(0) && usedCoinAsests.length === 0) {
5671
+ const zeroCoin = mintZeroCoin(txb, coinType);
5672
+ return {
5673
+ targetCoin: zeroCoin,
5674
+ isMintZeroCoin: true,
5675
+ targetCoinAmount: 0
5676
+ };
5677
+ }
5678
+ let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5679
+ if (totalCoinBalance < amount) {
5680
+ throw new AggregateError(
5681
+ "Insufficient balance when build merge coin",
5682
+ "InsufficientBalance" /* InsufficientBalance */
5683
+ );
5684
+ }
5685
+ if (CoinUtils.isSuiCoin(coinType)) {
5686
+ const resultCoin = txb.splitCoins(txb.gas, [
5687
+ txb.pure.u64(amount.toString())
5688
+ ]);
5689
+ return {
5690
+ targetCoin: resultCoin,
5691
+ isMintZeroCoin: true,
5692
+ targetCoinAmount: Number(amount.toString())
5693
+ };
5694
+ }
5695
+ let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
5696
+ let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
5697
+ if (totalThreeCoinBalance < BigInt(amount)) {
5698
+ sortCoinAssets = CoinUtils.sortByBalanceDes(usedCoinAsests);
5699
+ }
5700
+ let selectedCoinResult = CoinUtils.selectCoinObjectIdGreaterThanOrEqual(
5701
+ sortCoinAssets,
5702
+ amount
5703
+ );
5704
+ const [masterCoin, ...mergedCoin] = selectedCoinResult.objectArray;
5705
+ if (mergedCoin.length > 0) {
5706
+ txb.mergeCoins(
5707
+ masterCoin,
5708
+ mergedCoin.map((coin) => txb.object(coin))
5709
+ );
5710
+ }
5711
+ return {
5712
+ targetCoin: txb.object(masterCoin),
5713
+ isMintZeroCoin: false,
5714
+ targetCoinAmount: Number(amount.toString())
5715
+ };
5716
+ }
6356
5717
 
6357
5718
  // src/utils/transaction.ts
6358
5719
  function checkInvalidSuiAddress(address) {
@@ -6363,30 +5724,41 @@ function checkInvalidSuiAddress(address) {
6363
5724
  }
6364
5725
 
6365
5726
  // src/transaction/swap.ts
5727
+ var import_bn4 = __toESM(require_bn());
5728
+
5729
+ // src/math.ts
6366
5730
  var import_bn3 = __toESM(require_bn());
6367
- function swapInPools(client, params, config2) {
5731
+ function CalculateAmountLimit(expectAmount, byAmountIn, slippage) {
5732
+ let amountLimit = ZERO;
5733
+ if (byAmountIn) {
5734
+ amountLimit = expectAmount.mul(new import_bn3.default(TEN_POW_NINE - slippage * TEN_POW_NINE)).div(new import_bn3.default(TEN_POW_NINE));
5735
+ } else {
5736
+ amountLimit = expectAmount.mul(new import_bn3.default(TEN_POW_NINE + slippage * TEN_POW_NINE)).div(new import_bn3.default(TEN_POW_NINE));
5737
+ }
5738
+ return Number(amountLimit.toString());
5739
+ }
5740
+ function sqrtPriceX64ToPrice(sqrtPriceStr, decimalsA, decimalsB) {
5741
+ const sqrtPriceX64 = new decimal_default(sqrtPriceStr).mul(decimal_default.pow(2, -64));
5742
+ return sqrtPriceX64.pow(2).mul(decimal_default.pow(10, decimalsA - decimalsB));
5743
+ }
5744
+
5745
+ // src/transaction/swap.ts
5746
+ function swapInPools(client, params, sender) {
6368
5747
  return __async(this, null, function* () {
6369
5748
  var _a, _b, _c, _d, _e;
6370
5749
  const { from, target, amount, byAmountIn, pools } = params;
6371
5750
  const fromCoin = completionCoin(from);
6372
5751
  const targetCoin = completionCoin(target);
6373
5752
  const tx = new transactions.Transaction();
6374
- const a2b = compareCoins(fromCoin, targetCoin);
6375
- const integratePackage = config2.getPackage(INTEGRATE);
6376
- if (integratePackage == null) {
6377
- throw new AggregateError(
6378
- "Aggregator package not set",
6379
- "MissAggregatorPackage" /* MissAggregatorPackage */
6380
- );
6381
- }
6382
- const integratePublishedAt = integratePackage.publishedAt;
6383
- const coinA = a2b ? fromCoin : targetCoin;
6384
- const coinB = a2b ? targetCoin : fromCoin;
5753
+ const direction = compareCoins(fromCoin, targetCoin);
5754
+ const integratePublishedAt = "0x8faab90228e4c4df91c41626bbaefa19fc25c514405ac64de54578dec9e6f5ee";
5755
+ const coinA = direction ? fromCoin : targetCoin;
5756
+ const coinB = direction ? targetCoin : fromCoin;
6385
5757
  const typeArguments = [coinA, coinB];
6386
5758
  for (let i = 0; i < pools.length; i++) {
6387
5759
  const args = [
6388
5760
  tx.object(pools[i]),
6389
- tx.pure.bool(a2b),
5761
+ tx.pure.bool(direction),
6390
5762
  tx.pure.bool(byAmountIn),
6391
5763
  tx.pure.u64(amount.toString())
6392
5764
  ];
@@ -6396,7 +5768,7 @@ function swapInPools(client, params, config2) {
6396
5768
  typeArguments
6397
5769
  });
6398
5770
  }
6399
- if (!checkInvalidSuiAddress(config2.getWallet())) {
5771
+ if (!checkInvalidSuiAddress(sender)) {
6400
5772
  throw new AggregateError(
6401
5773
  "Aggregator package not set",
6402
5774
  "InvalidWallet" /* InvalidWallet */
@@ -6404,7 +5776,7 @@ function swapInPools(client, params, config2) {
6404
5776
  }
6405
5777
  const simulateRes = yield client.devInspectTransactionBlock({
6406
5778
  transactionBlock: tx,
6407
- sender: config2.getWallet()
5779
+ sender
6408
5780
  });
6409
5781
  if (simulateRes.error != null) {
6410
5782
  throw new AggregateError(
@@ -6428,13 +5800,13 @@ function swapInPools(client, params, config2) {
6428
5800
  continue;
6429
5801
  }
6430
5802
  if (params.byAmountIn) {
6431
- const amount2 = new import_bn3.BN(valueData[i].parsedJson.data.amount_out);
5803
+ const amount2 = new import_bn4.BN(valueData[i].parsedJson.data.amount_out);
6432
5804
  if (amount2.gt(tempMaxAmount)) {
6433
5805
  tempIndex = i;
6434
5806
  tempMaxAmount = amount2;
6435
5807
  }
6436
5808
  } else {
6437
- const amount2 = new import_bn3.BN(valueData[i].parsedJson.data.amount_out);
5809
+ const amount2 = new import_bn4.BN(valueData[i].parsedJson.data.amount_out);
6438
5810
  if (amount2.lt(tempMaxAmount)) {
6439
5811
  tempIndex = i;
6440
5812
  tempMaxAmount = amount2;
@@ -6442,6 +5814,7 @@ function swapInPools(client, params, config2) {
6442
5814
  }
6443
5815
  }
6444
5816
  const event = valueData[tempIndex].parsedJson.data;
5817
+ console.log("event", JSON.stringify(event, null, 2));
6445
5818
  const currentSqrtPrice = event.step_results[0].current_sqrt_price;
6446
5819
  const [decimalA, decimalB] = yield Promise.all([
6447
5820
  client.getCoinMetadata({ coinType: coinA }).then((metadata) => metadata == null ? void 0 : metadata.decimals),
@@ -6459,24 +5832,24 @@ function swapInPools(client, params, config2) {
6459
5832
  decimalB
6460
5833
  );
6461
5834
  const routeData = {
6462
- amountIn: new import_bn3.BN((_b = event.amount_in) != null ? _b : 0),
6463
- amountOut: new import_bn3.BN((_c = event.amount_out) != null ? _c : 0),
5835
+ amountIn: new import_bn4.BN((_b = event.amount_in) != null ? _b : 0),
5836
+ amountOut: new import_bn4.BN((_c = event.amount_out) != null ? _c : 0),
6464
5837
  routes: [
6465
5838
  {
6466
5839
  path: [
6467
5840
  {
6468
5841
  id: pools[tempIndex],
6469
- a2b,
6470
- provider: CETUS_DEX,
5842
+ direction,
5843
+ provider: "CETUS",
6471
5844
  from: fromCoin,
6472
5845
  target: targetCoin,
6473
- feeRate: 0,
6474
- amountIn: 0,
6475
- amountOut: 0
5846
+ feeRate: event.fee_rate,
5847
+ amountIn: event.amount_in,
5848
+ amountOut: event.amount_out
6476
5849
  }
6477
5850
  ],
6478
- amountIn: new import_bn3.BN((_d = event.amount_in) != null ? _d : 0),
6479
- amountOut: new import_bn3.BN((_e = event.amount_out) != null ? _e : 0),
5851
+ amountIn: new import_bn4.BN((_d = event.amount_in) != null ? _d : 0),
5852
+ amountOut: new import_bn4.BN((_e = event.amount_out) != null ? _e : 0),
6480
5853
  initialPrice
6481
5854
  }
6482
5855
  ],
@@ -6489,11 +5862,146 @@ function swapInPools(client, params, config2) {
6489
5862
  return result;
6490
5863
  });
6491
5864
  }
6492
- var AggregatorClient = class {
6493
- constructor(config2) {
6494
- this.config = config2;
6495
- this.client = new client.SuiClient({ url: config2.getFullNodeUrl() });
6496
- this.wallet = config2.getWallet();
5865
+
5866
+ // src/transaction/kriya_v3.ts
5867
+ var KriyaV3 = class {
5868
+ constructor(env) {
5869
+ if (env !== 0 /* Mainnet */) {
5870
+ throw new Error("Kriya clmm only supported on mainnet");
5871
+ }
5872
+ this.version = "0xf5145a7ac345ca8736cf8c76047d00d6d378f30e81be6f6eb557184d9de93c78";
5873
+ }
5874
+ swap(client, txb, path, inputCoin) {
5875
+ return __async(this, null, function* () {
5876
+ const { direction, from, target } = path;
5877
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5878
+ const args = [
5879
+ txb.object(path.id),
5880
+ inputCoin,
5881
+ txb.object(this.version),
5882
+ txb.object(CLOCK_ADDRESS)
5883
+ ];
5884
+ const res = txb.moveCall({
5885
+ target: `${client.publishedAt()}::kriya_clmm::${func}`,
5886
+ typeArguments: [coinAType, coinBType],
5887
+ arguments: args
5888
+ });
5889
+ return res;
5890
+ });
5891
+ }
5892
+ };
5893
+
5894
+ // src/transaction/haedal.ts
5895
+ var Haedal = class {
5896
+ constructor(env) {
5897
+ if (env !== 0 /* Mainnet */) {
5898
+ throw new Error("Haedal only supported on mainnet");
5899
+ }
5900
+ }
5901
+ swap(client, txb, path, inputCoin) {
5902
+ return __async(this, null, function* () {
5903
+ const { direction } = path;
5904
+ if (!direction) {
5905
+ throw new Error("Haedal not support b2a swap");
5906
+ }
5907
+ const func = "swap_a2b";
5908
+ console.log("haedal path id", path.id);
5909
+ const args = [txb.object(path.id), txb.object("0x5"), inputCoin];
5910
+ const res = txb.moveCall({
5911
+ target: `${client.publishedAt()}::haedal::${func}`,
5912
+ typeArguments: [],
5913
+ arguments: args
5914
+ });
5915
+ return res;
5916
+ });
5917
+ }
5918
+ };
5919
+
5920
+ // src/transaction/afsui.ts
5921
+ var Afsui = class {
5922
+ constructor(env) {
5923
+ if (env !== 0 /* Mainnet */) {
5924
+ throw new Error("Afsui only supported on mainnet");
5925
+ }
5926
+ this.stakedSuiVault = "0x2f8f6d5da7f13ea37daa397724280483ed062769813b6f31e9788e59cc88994d";
5927
+ this.safe = "0xeb685899830dd5837b47007809c76d91a098d52aabbf61e8ac467c59e5cc4610";
5928
+ this.referVault = "0x4ce9a19b594599536c53edb25d22532f82f18038dc8ef618afd00fbbfb9845ef";
5929
+ this.validator = "0xd30018ec3f5ff1a3c75656abf927a87d7f0529e6dc89c7ddd1bd27ecb05e3db2";
5930
+ }
5931
+ swap(client, txb, path, inputCoin) {
5932
+ return __async(this, null, function* () {
5933
+ const { direction } = path;
5934
+ if (!direction) {
5935
+ throw new Error("Afsui not support b2a swap");
5936
+ }
5937
+ const func = "swap_a2b";
5938
+ const args = [
5939
+ txb.object(this.stakedSuiVault),
5940
+ txb.object(this.safe),
5941
+ txb.object("0x5"),
5942
+ txb.object(this.referVault),
5943
+ txb.object(this.validator),
5944
+ inputCoin
5945
+ ];
5946
+ const res = txb.moveCall({
5947
+ target: `${client.publishedAt()}::afsui::${func}`,
5948
+ typeArguments: [],
5949
+ arguments: args
5950
+ });
5951
+ return res;
5952
+ });
5953
+ }
5954
+ };
5955
+
5956
+ // src/transaction/volo.ts
5957
+ var Volo = class {
5958
+ constructor(env) {
5959
+ if (env !== 0 /* Mainnet */) {
5960
+ throw new Error("Volo only supported on mainnet");
5961
+ }
5962
+ this.nativePool = "0x7fa2faa111b8c65bea48a23049bfd81ca8f971a262d981dcd9a17c3825cb5baf";
5963
+ this.metadata = "0x680cd26af32b2bde8d3361e804c53ec1d1cfe24c7f039eb7f549e8dfde389a60";
5964
+ }
5965
+ swap(client, txb, path, inputCoin) {
5966
+ return __async(this, null, function* () {
5967
+ const { direction } = path;
5968
+ if (!direction) {
5969
+ throw new Error("Volo not support b2a swap");
5970
+ }
5971
+ const func = "swap_a2b";
5972
+ const args = [
5973
+ txb.object(this.nativePool),
5974
+ txb.object(this.metadata),
5975
+ txb.object("0x5"),
5976
+ inputCoin
5977
+ ];
5978
+ const res = txb.moveCall({
5979
+ target: `${client.publishedAt()}::volo::${func}`,
5980
+ typeArguments: [],
5981
+ arguments: args
5982
+ });
5983
+ return res;
5984
+ });
5985
+ }
5986
+ };
5987
+
5988
+ // src/client.ts
5989
+ var CETUS = "CETUS";
5990
+ var DEEPBOOKV2 = "DEEPBOOK";
5991
+ var KRIYA = "KRIYA";
5992
+ var FLOWXV2 = "FLOWX";
5993
+ var KRIYAV3 = "KRIYAV3";
5994
+ var TURBOS = "TURBOS";
5995
+ var AFTERMATH = "AFTERMATH";
5996
+ var HAEDAL = "HAEDAL";
5997
+ var VOLO = "VOLO";
5998
+ var AFSUI = "AFSUI";
5999
+ var AggregatorClient5 = class {
6000
+ constructor(endpoint, signer, client, env) {
6001
+ this.endpoint = endpoint;
6002
+ this.client = client;
6003
+ this.signer = signer;
6004
+ this.env = env;
6497
6005
  this.allCoins = [];
6498
6006
  }
6499
6007
  getAllCoins() {
@@ -6503,7 +6011,7 @@ var AggregatorClient = class {
6503
6011
  const allCoins = [];
6504
6012
  while (true) {
6505
6013
  const gotAllCoins = yield this.client.getAllCoins({
6506
- owner: this.wallet,
6014
+ owner: this.signer,
6507
6015
  cursor,
6508
6016
  limit
6509
6017
  });
@@ -6522,79 +6030,95 @@ var AggregatorClient = class {
6522
6030
  return allCoins;
6523
6031
  });
6524
6032
  }
6525
- findRouter(fromRouterParams) {
6033
+ findRouters(params) {
6526
6034
  return __async(this, null, function* () {
6527
- const {
6528
- from,
6529
- target,
6530
- amount,
6531
- byAmountIn,
6532
- depth,
6533
- splitAlgorithm,
6534
- splitFactor,
6535
- splitCount,
6536
- providers
6537
- } = fromRouterParams;
6538
- const fromCoin = completionCoin(from);
6539
- const targetCoin = completionCoin(target);
6540
- let url = this.config.getAggregatorUrl() + `/find_routes?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`;
6541
- if (depth) {
6542
- url += `&depth=${depth}`;
6543
- }
6544
- if (splitAlgorithm) {
6545
- url += `&split_algorithm=${splitAlgorithm}`;
6546
- }
6547
- if (splitFactor) {
6548
- url += `&split_factor=${splitFactor}`;
6549
- }
6550
- if (splitCount) {
6551
- url += `&split_count=${splitCount}`;
6035
+ return getRouterResult(this.endpoint, params);
6036
+ });
6037
+ }
6038
+ expectInputSwap(txb, inputCoin, routers, amountOutLimit, partner) {
6039
+ return __async(this, null, function* () {
6040
+ if (routers.length === 0) {
6041
+ throw new Error("No router found");
6552
6042
  }
6553
- if (providers) {
6554
- if (providers.length > 0) {
6555
- url += `&providers=${providers.join(",")}`;
6556
- }
6043
+ const splitAmounts = routers.map((router) => router.amountIn.toString());
6044
+ const inputCoinType = routers[0].path[0].from;
6045
+ const outputCoinType = routers[0].path[routers[0].path.length - 1].target;
6046
+ const inputCoins = txb.splitCoins(inputCoin, splitAmounts);
6047
+ const outputCoins = [];
6048
+ for (let i = 0; i < routers.length; i++) {
6049
+ if (routers[i].path.length === 0) {
6050
+ throw new Error("Empty path");
6051
+ }
6052
+ let nextCoin = inputCoins[i];
6053
+ for (const path of routers[i].path) {
6054
+ const dex = this.newDex(path.provider, partner);
6055
+ nextCoin = yield dex.swap(this, txb, path, nextCoin);
6056
+ }
6057
+ outputCoins.push(nextCoin);
6557
6058
  }
6558
- const response = yield fetch(url);
6559
- if (!response.ok) {
6560
- return {
6561
- amountIn: ZERO,
6562
- amountOut: ZERO,
6563
- routes: [],
6564
- insufficientLiquidity: false,
6565
- error: {
6566
- code: 10001 /* NumberTooLarge */,
6567
- msg: getAggregatorServerErrorMessage(
6568
- 10001 /* NumberTooLarge */
6569
- )
6059
+ this.transferOrDestoryCoin(txb, inputCoin, inputCoinType);
6060
+ const mergedTargetCointhis = this.checkCoinThresholdAndMergeCoin(
6061
+ txb,
6062
+ outputCoins,
6063
+ outputCoinType,
6064
+ amountOutLimit
6065
+ );
6066
+ return mergedTargetCointhis;
6067
+ });
6068
+ }
6069
+ expectOutputSwap(txb, inputCoin, routers, partner) {
6070
+ return __async(this, null, function* () {
6071
+ const returnCoins = [];
6072
+ const receipts = [];
6073
+ const targetCoins = [];
6074
+ const dex = new Cetus(this.env, partner);
6075
+ for (let i = 0; i < routers.length; i++) {
6076
+ const router = routers[i];
6077
+ for (let j = router.path.length - 1; j >= 0; j--) {
6078
+ const path = router.path[j];
6079
+ const flashSwapResult = dex.flash_swap(this, txb, path, false);
6080
+ returnCoins.unshift(flashSwapResult.targetCoin);
6081
+ receipts.unshift(flashSwapResult.flashReceipt);
6082
+ }
6083
+ let nextRepayCoin = inputCoin;
6084
+ for (let j = 0; j < router.path.length; j++) {
6085
+ const path = router.path[j];
6086
+ const repayResult = dex.repay_flash_swap(
6087
+ this,
6088
+ txb,
6089
+ path,
6090
+ nextRepayCoin,
6091
+ receipts[j]
6092
+ );
6093
+ nextRepayCoin = returnCoins[j];
6094
+ if (j === 0) {
6095
+ inputCoin = repayResult;
6096
+ } else {
6097
+ this.transferOrDestoryCoin(txb, repayResult, path.from);
6570
6098
  }
6571
- };
6099
+ if (j === router.path.length - 1) {
6100
+ targetCoins.push(nextRepayCoin);
6101
+ }
6102
+ }
6572
6103
  }
6573
- const data = yield response.json();
6574
- if (data.data != null) {
6575
- const res = parseRouterResponse(data.data);
6576
- return res;
6104
+ const inputCoinType = routers[0].path[0].from;
6105
+ this.transferOrDestoryCoin(txb, inputCoin, inputCoinType);
6106
+ if (targetCoins.length > 1) {
6107
+ const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) });
6108
+ txb.moveCall({
6109
+ target: `${utils.SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
6110
+ typeArguments: [routers[0].path[routers[0].path.length - 1].target],
6111
+ arguments: [targetCoins[0], vec]
6112
+ });
6577
6113
  }
6578
- const insufficientLiquidity = data.msg === "liquidity is not enough";
6579
- return {
6580
- amountIn: ZERO,
6581
- amountOut: ZERO,
6582
- routes: [],
6583
- insufficientLiquidity,
6584
- error: {
6585
- code: 10003 /* InsufficientLiquidity */,
6586
- msg: getAggregatorServerErrorMessage(
6587
- 10003 /* InsufficientLiquidity */
6588
- )
6589
- }
6590
- };
6114
+ return targetCoins[0];
6591
6115
  });
6592
6116
  }
6593
6117
  swapInPools(params) {
6594
6118
  return __async(this, null, function* () {
6595
6119
  let result;
6596
6120
  try {
6597
- result = yield swapInPools(this.client, params, this.config);
6121
+ result = yield swapInPools(this.client, params, this.signer);
6598
6122
  } catch (e) {
6599
6123
  console.error("swapInPools error:", e);
6600
6124
  return null;
@@ -6603,100 +6127,168 @@ var AggregatorClient = class {
6603
6127
  });
6604
6128
  }
6605
6129
  routerSwap(params) {
6130
+ return __async(this, null, function* () {
6131
+ const { routers, inputCoin, slippage, byAmountIn, txb, partner } = params;
6132
+ const amountIn = routers.reduce(
6133
+ (acc, router) => acc.add(router.amountIn),
6134
+ new import_bn5.default(0)
6135
+ );
6136
+ const amountOut = routers.reduce(
6137
+ (acc, router) => acc.add(router.amountOut),
6138
+ new import_bn5.default(0)
6139
+ );
6140
+ const amountLimit = CalculateAmountLimit(
6141
+ byAmountIn ? amountOut : amountIn,
6142
+ byAmountIn,
6143
+ slippage
6144
+ );
6145
+ if (byAmountIn) {
6146
+ const targetCoin2 = yield this.expectInputSwap(
6147
+ txb,
6148
+ inputCoin,
6149
+ routers,
6150
+ new import_bn5.default(amountLimit),
6151
+ partner
6152
+ );
6153
+ return targetCoin2;
6154
+ }
6155
+ const splitedInputCoins = txb.splitCoins(inputCoin, [
6156
+ amountLimit.toString()
6157
+ ]);
6158
+ this.transferOrDestoryCoin(txb, inputCoin, routers[0].path[0].from);
6159
+ const targetCoin = yield this.expectOutputSwap(
6160
+ txb,
6161
+ splitedInputCoins[0],
6162
+ routers,
6163
+ partner
6164
+ );
6165
+ return targetCoin;
6166
+ });
6167
+ }
6168
+ // auto build input coin
6169
+ // auto merge, transfer or destory target coin.
6170
+ fastRouterSwap(params) {
6606
6171
  return __async(this, null, function* () {
6607
6172
  const {
6608
- routers: _,
6609
- amountIn,
6610
- amountOut,
6173
+ routers,
6611
6174
  byAmountIn,
6612
6175
  slippage,
6613
- fromCoinType,
6614
- targetCoinType,
6176
+ txb,
6615
6177
  partner,
6616
6178
  isMergeTragetCoin,
6617
6179
  refreshAllCoins
6618
6180
  } = params;
6181
+ if (refreshAllCoins || this.allCoins.length === 0) {
6182
+ this.allCoins = yield this.getAllCoins();
6183
+ }
6184
+ const fromCoinType = routers[0].path[0].from;
6185
+ const targetCoinType = routers[0].path[routers[0].path.length - 1].target;
6186
+ const amountIn = routers.reduce(
6187
+ (acc, router) => acc.add(router.amountIn),
6188
+ new import_bn5.default(0)
6189
+ );
6190
+ const amountOut = routers.reduce(
6191
+ (acc, router) => acc.add(router.amountOut),
6192
+ new import_bn5.default(0)
6193
+ );
6619
6194
  const amountLimit = CalculateAmountLimit(
6620
6195
  byAmountIn ? amountOut : amountIn,
6621
6196
  byAmountIn,
6622
6197
  slippage
6623
6198
  );
6624
- const txb = new transactions.Transaction();
6625
- if (refreshAllCoins || this.allCoins.length === 0) {
6626
- this.allCoins = yield this.getAllCoins();
6627
- }
6628
- const allCoins = this.allCoins;
6629
- let targetCoins = [];
6630
- if (byAmountIn) {
6631
- const buildFromCoinRes = buildInputCoin(
6632
- txb,
6633
- allCoins,
6634
- BigInt(amountIn.toString()),
6635
- fromCoinType
6636
- );
6637
- const fromCoin = buildFromCoinRes.targetCoin;
6638
- const swapedCoins = yield expectInputRouterSwap(
6639
- this.client,
6640
- params,
6641
- txb,
6642
- fromCoin,
6643
- this.config,
6644
- partner
6645
- );
6646
- const mergedCoin = checkCoinThresholdAndMergeCoin(
6647
- txb,
6648
- swapedCoins,
6649
- targetCoinType,
6650
- amountLimit,
6651
- this.config
6652
- );
6653
- targetCoins.push(mergedCoin);
6654
- } else {
6655
- const fromCoin = buildInputCoin(
6656
- txb,
6657
- allCoins,
6658
- BigInt(amountLimit),
6659
- fromCoinType
6660
- ).targetCoin;
6661
- const swapedCoins = yield expectOutputRouterSwap(
6662
- params,
6663
- txb,
6664
- fromCoin,
6665
- this.config,
6666
- partner
6667
- );
6668
- targetCoins.push(...swapedCoins);
6669
- }
6199
+ const amount = byAmountIn ? amountIn : amountLimit;
6200
+ const buildFromCoinRes = buildInputCoin(
6201
+ txb,
6202
+ this.allCoins,
6203
+ BigInt(amount.toString()),
6204
+ fromCoinType
6205
+ );
6206
+ const targetCoin = yield this.routerSwap({
6207
+ routers,
6208
+ inputCoin: buildFromCoinRes.targetCoin,
6209
+ slippage,
6210
+ byAmountIn,
6211
+ txb,
6212
+ partner
6213
+ });
6670
6214
  if (isMergeTragetCoin) {
6671
6215
  const targetCoinRes = buildInputCoin(
6672
6216
  txb,
6673
- allCoins,
6217
+ this.allCoins,
6674
6218
  BigInt(0),
6675
6219
  targetCoinType
6676
6220
  );
6677
- txb.mergeCoins(targetCoinRes.targetCoin, targetCoins);
6221
+ txb.mergeCoins(targetCoinRes.targetCoin, [targetCoin]);
6678
6222
  if (targetCoinRes.isMintZeroCoin) {
6679
- transferOrDestoryCoin(
6223
+ this.transferOrDestoryCoin(
6680
6224
  txb,
6681
6225
  targetCoinRes.targetCoin,
6682
- targetCoinType,
6683
- this.config
6226
+ targetCoinType
6684
6227
  );
6685
6228
  }
6686
6229
  } else {
6687
- if (targetCoins.length > 1) {
6688
- const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) });
6689
- txb.moveCall({
6690
- target: createTarget(utils.SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
6691
- typeArguments: [targetCoinType],
6692
- arguments: [targetCoins[0], vec]
6693
- });
6694
- }
6695
- transferOrDestoryCoin(txb, targetCoins[0], targetCoinType, this.config);
6230
+ this.transferOrDestoryCoin(txb, targetCoin, targetCoinType);
6696
6231
  }
6697
- return txb;
6698
6232
  });
6699
6233
  }
6234
+ publishedAt() {
6235
+ if (this.env === 0 /* Mainnet */) {
6236
+ return "0x9fd702cf1a8db5a2e99be20be6750d22ec75c30d57e3d9ccbc37c9b4ad7bef21";
6237
+ } else {
6238
+ return "0x0";
6239
+ }
6240
+ }
6241
+ transferOrDestoryCoin(txb, coin, coinType) {
6242
+ txb.moveCall({
6243
+ target: `${this.publishedAt()}::utils::transfer_or_destroy_coin`,
6244
+ typeArguments: [coinType],
6245
+ arguments: [coin]
6246
+ });
6247
+ }
6248
+ checkCoinThresholdAndMergeCoin(txb, coins, coinType, amountLimit) {
6249
+ let targetCoin = coins[0];
6250
+ if (coins.length > 1) {
6251
+ let vec = txb.makeMoveVec({ elements: coins.slice(1) });
6252
+ txb.moveCall({
6253
+ target: `${utils.SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
6254
+ typeArguments: [coinType],
6255
+ arguments: [coins[0], vec]
6256
+ });
6257
+ targetCoin = coins[0];
6258
+ }
6259
+ txb.moveCall({
6260
+ target: `${this.publishedAt()}::utils::check_coin_threshold`,
6261
+ typeArguments: [coinType],
6262
+ arguments: [targetCoin, txb.pure.u64(amountLimit.toString())]
6263
+ });
6264
+ return targetCoin;
6265
+ }
6266
+ newDex(provider, partner) {
6267
+ switch (provider) {
6268
+ case CETUS:
6269
+ return new Cetus(this.env, partner);
6270
+ case DEEPBOOKV2:
6271
+ return new DeepbookV2(this.env);
6272
+ case KRIYA:
6273
+ return new KriyaV2(this.env);
6274
+ case KRIYAV3:
6275
+ return new KriyaV3(this.env);
6276
+ case FLOWXV2:
6277
+ return new FlowxV2(this.env);
6278
+ case TURBOS:
6279
+ return new Turbos(this.env);
6280
+ case AFTERMATH:
6281
+ return new Aftermath(this.env);
6282
+ case HAEDAL:
6283
+ return new Haedal(this.env);
6284
+ case AFSUI:
6285
+ return new Afsui(this.env);
6286
+ case VOLO:
6287
+ return new Volo(this.env);
6288
+ default:
6289
+ throw new Error(`Unsupported dex ${provider}`);
6290
+ }
6291
+ }
6700
6292
  signAndExecuteTransaction(txb, signer) {
6701
6293
  return __async(this, null, function* () {
6702
6294
  const res = yield this.client.signAndExecuteTransaction({
@@ -6716,7 +6308,7 @@ var AggregatorClient = class {
6716
6308
  return __async(this, null, function* () {
6717
6309
  const res = yield this.client.devInspectTransactionBlock({
6718
6310
  transactionBlock: txb,
6719
- sender: this.wallet
6311
+ sender: this.signer
6720
6312
  });
6721
6313
  return res;
6722
6314
  });
@@ -6733,19 +6325,19 @@ var AggregatorClient = class {
6733
6325
  };
6734
6326
  function parseRouterResponse(data) {
6735
6327
  return {
6736
- amountIn: new import_bn4.default(data.amount_in.toString()),
6737
- amountOut: new import_bn4.default(data.amount_out.toString()),
6328
+ amountIn: new import_bn5.default(data.amount_in.toString()),
6329
+ amountOut: new import_bn5.default(data.amount_out.toString()),
6738
6330
  insufficientLiquidity: false,
6739
6331
  routes: data.routes.map((route) => {
6740
6332
  return {
6741
6333
  path: route.path.map((path) => {
6742
6334
  var _a, _b;
6743
6335
  let version;
6744
- if (path.provider === AFTERMATH_AMM) {
6336
+ if (path.provider === AFTERMATH) {
6745
6337
  version = path.extended_details.aftermath_pool_flatness === 0 ? "v2" : "v3";
6746
6338
  }
6747
6339
  let extendedDetails;
6748
- if (path.provider === TURBOS_DEX || path.provider === AFTERMATH_AMM) {
6340
+ if (path.provider === TURBOS || path.provider === AFTERMATH) {
6749
6341
  extendedDetails = {
6750
6342
  aftermathLpSupplyType: (_a = path.extended_details) == null ? void 0 : _a.aftermath_lp_supply_type,
6751
6343
  turbosFeeType: (_b = path.extended_details) == null ? void 0 : _b.turbos_fee_type
@@ -6753,7 +6345,7 @@ function parseRouterResponse(data) {
6753
6345
  }
6754
6346
  return {
6755
6347
  id: path.id,
6756
- a2b: path.direction,
6348
+ direction: path.direction,
6757
6349
  provider: path.provider,
6758
6350
  from: path.from,
6759
6351
  target: path.target,
@@ -6764,13 +6356,93 @@ function parseRouterResponse(data) {
6764
6356
  version
6765
6357
  };
6766
6358
  }),
6767
- amountIn: new import_bn4.default(route.amount_in.toString()),
6768
- amountOut: new import_bn4.default(route.amount_out.toString()),
6359
+ amountIn: new import_bn5.default(route.amount_in.toString()),
6360
+ amountOut: new import_bn5.default(route.amount_out.toString()),
6769
6361
  initialPrice: new decimal_default(route.initial_price.toString())
6770
6362
  };
6771
6363
  })
6772
6364
  };
6773
6365
  }
6366
+
6367
+ // src/transaction/index.ts
6368
+ var CLOCK_ADDRESS = "0x0000000000000000000000000000000000000000000000000000000000000006";
6369
+
6370
+ // src/api.ts
6371
+ function getRouterResult(endpoint, params) {
6372
+ return __async(this, null, function* () {
6373
+ const {
6374
+ from,
6375
+ target,
6376
+ amount,
6377
+ byAmountIn,
6378
+ depth,
6379
+ splitAlgorithm,
6380
+ splitFactor,
6381
+ splitCount,
6382
+ providers
6383
+ } = params;
6384
+ const fromCoin = completionCoin(from);
6385
+ const targetCoin = completionCoin(target);
6386
+ let url = `${endpoint}?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`;
6387
+ if (depth) {
6388
+ url += `&depth=${depth}`;
6389
+ }
6390
+ if (splitAlgorithm) {
6391
+ url += `&split_algorithm=${splitAlgorithm}`;
6392
+ }
6393
+ if (splitFactor) {
6394
+ url += `&split_factor=${splitFactor}`;
6395
+ }
6396
+ if (splitCount) {
6397
+ url += `&split_count=${splitCount}`;
6398
+ }
6399
+ if (providers) {
6400
+ if (providers.length > 0) {
6401
+ url += `&providers=${providers.join(",")}`;
6402
+ }
6403
+ }
6404
+ const response = yield fetch(url);
6405
+ if (!response.ok) {
6406
+ return {
6407
+ amountIn: ZERO,
6408
+ amountOut: ZERO,
6409
+ routes: [],
6410
+ insufficientLiquidity: false,
6411
+ error: {
6412
+ code: 10001 /* NumberTooLarge */,
6413
+ msg: getAggregatorServerErrorMessage(
6414
+ 10001 /* NumberTooLarge */
6415
+ )
6416
+ }
6417
+ };
6418
+ }
6419
+ const data = yield response.json();
6420
+ if (data.data != null) {
6421
+ const res = parseRouterResponse(data.data);
6422
+ return res;
6423
+ }
6424
+ const insufficientLiquidity = data.msg === "liquidity is not enough";
6425
+ return {
6426
+ amountIn: ZERO,
6427
+ amountOut: ZERO,
6428
+ routes: [],
6429
+ insufficientLiquidity,
6430
+ error: {
6431
+ code: 10003 /* InsufficientLiquidity */,
6432
+ msg: getAggregatorServerErrorMessage(
6433
+ 10003 /* InsufficientLiquidity */
6434
+ )
6435
+ }
6436
+ };
6437
+ });
6438
+ }
6439
+
6440
+ // src/index.ts
6441
+ var Env = /* @__PURE__ */ ((Env2) => {
6442
+ Env2[Env2["Mainnet"] = 0] = "Mainnet";
6443
+ Env2[Env2["Testnet"] = 1] = "Testnet";
6444
+ return Env2;
6445
+ })(Env || {});
6774
6446
  /*! Bundled license information:
6775
6447
 
6776
6448
  decimal.js/decimal.mjs:
@@ -6783,26 +6455,25 @@ decimal.js/decimal.mjs:
6783
6455
  *)
6784
6456
  */
6785
6457
 
6786
- exports.AFTERMATH_AMM = AFTERMATH_AMM;
6458
+ exports.AFSUI = AFSUI;
6459
+ exports.AFTERMATH = AFTERMATH;
6787
6460
  exports.AFTERMATH_MODULE = AFTERMATH_MODULE;
6788
- exports.AGGREGATOR = AGGREGATOR;
6789
- exports.AggregatorClient = AggregatorClient;
6790
- exports.AggregatorConfig = AggregatorConfig;
6791
- exports.CETUS_DEX = CETUS_DEX;
6461
+ exports.AggregatorClient = AggregatorClient5;
6462
+ exports.CETUS = CETUS;
6792
6463
  exports.CETUS_MODULE = CETUS_MODULE;
6793
6464
  exports.CETUS_PUBLISHED_AT = CETUS_PUBLISHED_AT;
6794
6465
  exports.CHECK_COINS_THRESHOLD_FUNC = CHECK_COINS_THRESHOLD_FUNC;
6795
6466
  exports.CLOCK_ADDRESS = CLOCK_ADDRESS;
6796
6467
  exports.CoinInfoAddress = CoinInfoAddress;
6797
6468
  exports.CoinStoreAddress = CoinStoreAddress;
6469
+ exports.DEEPBOOKV2 = DEEPBOOKV2;
6798
6470
  exports.DEEPBOOK_CLOB_V2_MODULE = DEEPBOOK_CLOB_V2_MODULE;
6799
6471
  exports.DEEPBOOK_CUSTODIAN_V2_MODULE = DEEPBOOK_CUSTODIAN_V2_MODULE;
6800
- exports.DEEPBOOK_DEX = DEEPBOOK_DEX;
6801
6472
  exports.DEEPBOOK_MODULE = DEEPBOOK_MODULE;
6802
- exports.DEEPBOOK_PACKAGE_ID = DEEPBOOK_PACKAGE_ID;
6473
+ exports.DEEPBOOK_PACKAGE_ID = DEEPBOOK_PACKAGE_ID2;
6803
6474
  exports.DEEPBOOK_PUBLISHED_AT = DEEPBOOK_PUBLISHED_AT;
6804
- exports.ENV = ENV;
6805
- exports.FLOWX_AMM = FLOWX_AMM;
6475
+ exports.Env = Env;
6476
+ exports.FLOWXV2 = FLOWXV2;
6806
6477
  exports.FLOWX_AMM_MODULE = FLOWX_AMM_MODULE;
6807
6478
  exports.FlashSwapA2BFunc = FlashSwapA2BFunc;
6808
6479
  exports.FlashSwapB2AFunc = FlashSwapB2AFunc;
@@ -6810,9 +6481,10 @@ exports.FlashSwapFunc = FlashSwapFunc;
6810
6481
  exports.FlashSwapWithPartnerA2BFunc = FlashSwapWithPartnerA2BFunc;
6811
6482
  exports.FlashSwapWithPartnerB2AFunc = FlashSwapWithPartnerB2AFunc;
6812
6483
  exports.FlashSwapWithPartnerFunc = FlashSwapWithPartnerFunc;
6813
- exports.INTEGRATE = INTEGRATE;
6484
+ exports.HAEDAL = HAEDAL;
6814
6485
  exports.JOIN_FUNC = JOIN_FUNC;
6815
- exports.KRIYA_DEX = KRIYA_DEX;
6486
+ exports.KRIYA = KRIYA;
6487
+ exports.KRIYAV3 = KRIYAV3;
6816
6488
  exports.KRIYA_MODULE = KRIYA_MODULE;
6817
6489
  exports.MAINNET_AFTERMATH_INSURANCE_FUND_ID = MAINNET_AFTERMATH_INSURANCE_FUND_ID;
6818
6490
  exports.MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID;
@@ -6843,7 +6515,7 @@ exports.TESTNET_CETUS_GLOBAL_CONFIG_ID = TESTNET_CETUS_GLOBAL_CONFIG_ID;
6843
6515
  exports.TESTNET_FLOWX_AMM_CONTAINER_ID = TESTNET_FLOWX_AMM_CONTAINER_ID;
6844
6516
  exports.TRANSFER_ACCOUNT_CAP = TRANSFER_ACCOUNT_CAP;
6845
6517
  exports.TRANSFER_OR_DESTORY_COIN_FUNC = TRANSFER_OR_DESTORY_COIN_FUNC;
6846
- exports.TURBOS_DEX = TURBOS_DEX;
6518
+ exports.TURBOS = TURBOS;
6847
6519
  exports.TURBOS_MODULE = TURBOS_MODULE;
6848
6520
  exports.TURBOS_VERSIONED = TURBOS_VERSIONED;
6849
6521
  exports.TWO = TWO;
@@ -6851,14 +6523,14 @@ exports.U128 = U128;
6851
6523
  exports.U64_MAX = U64_MAX;
6852
6524
  exports.U64_MAX_BN = U64_MAX_BN;
6853
6525
  exports.UTILS_MODULE = UTILS_MODULE;
6526
+ exports.VOLO = VOLO;
6854
6527
  exports.ZERO = ZERO;
6855
6528
  exports.composeType = composeType;
6856
6529
  exports.createTarget = createTarget;
6857
- exports.expectInputRouterSwap = expectInputRouterSwap;
6858
- exports.expectOutputRouterSwap = expectOutputRouterSwap;
6859
6530
  exports.extractAddressFromType = extractAddressFromType;
6860
6531
  exports.extractStructTagFromType = extractStructTagFromType;
6861
6532
  exports.fixSuiObjectId = fixSuiObjectId;
6533
+ exports.getRouterResult = getRouterResult;
6862
6534
  exports.isSortedSymbols = isSortedSymbols;
6863
6535
  exports.normalizeCoinType = normalizeCoinType;
6864
6536
  exports.parseRouterResponse = parseRouterResponse;