@cetusprotocol/aggregator-sdk 0.0.8 → 0.1.1

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 -161
  2. package/dist/index.d.ts +98 -161
  3. package/dist/index.js +1128 -1550
  4. package/dist/index.mjs +1116 -1488
  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 -68
  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 -113
  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 -24
  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 +123 -73
  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 -169
  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 -339
  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,6 +4979,402 @@ LN10 = new Decimal(LN10);
4980
4979
  PI = new Decimal(PI);
4981
4980
  var decimal_default = Decimal;
4982
4981
 
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
+ );
5003
+ }
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
+ });
5037
+ }
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
+ }
5047
+ }
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
+ }
5074
+ }
5075
+ return null;
5076
+ });
5077
+ }
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
+ });
5097
+ }
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);
5120
+ }
5121
+ return res;
5122
+ });
5123
+ }
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
+ }
5132
+ }
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
+ });
5145
+ }
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");
5153
+ }
5154
+ this.container = "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511";
5155
+ }
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
+ });
5168
+ }
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";
5178
+ }
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
+ });
5203
+ }
5204
+ };
5205
+
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
+ };
5234
+ }
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
+ var EQUAL = 0;
5273
+ var LESS_THAN = 1;
5274
+ var GREATER_THAN = 2;
5275
+ function cmp(a, b) {
5276
+ if (a === b) {
5277
+ return EQUAL;
5278
+ }
5279
+ if (a < b) {
5280
+ return LESS_THAN;
5281
+ }
5282
+ return GREATER_THAN;
5283
+ }
5284
+ function compare(symbolX, symbolY) {
5285
+ let i = 0;
5286
+ const len = symbolX.length <= symbolY.length ? symbolX.length : symbolY.length;
5287
+ const lenCmp = cmp(symbolX.length, symbolY.length);
5288
+ while (i < len) {
5289
+ const elemCmp = cmp(symbolX.charCodeAt(i), symbolY.charCodeAt(i));
5290
+ i += 1;
5291
+ if (elemCmp !== 0) {
5292
+ return elemCmp;
5293
+ }
5294
+ }
5295
+ return lenCmp;
5296
+ }
5297
+ function isSortedSymbols(symbolX, symbolY) {
5298
+ return compare(symbolX, symbolY) === LESS_THAN;
5299
+ }
5300
+ function composeType(address, ...args) {
5301
+ const generics = Array.isArray(args[args.length - 1]) ? args.pop() : [];
5302
+ const chains = [address, ...args].filter(Boolean);
5303
+ let result = chains.join("::");
5304
+ if (generics && generics.length) {
5305
+ result += `<${generics.join(", ")}>`;
5306
+ }
5307
+ return result;
5308
+ }
5309
+ function extractAddressFromType(type) {
5310
+ return type.split("::")[0];
5311
+ }
5312
+ function extractStructTagFromType(type) {
5313
+ var _a;
5314
+ try {
5315
+ let _type = type.replace(/\s/g, "");
5316
+ const genericsString = _type.match(/(<.+>)$/);
5317
+ const generics = (_a = genericsString == null ? void 0 : genericsString[0]) == null ? void 0 : _a.match(/(\w+::\w+::\w+)(?:<.*?>(?!>))?/g);
5318
+ if (generics) {
5319
+ _type = _type.slice(0, _type.indexOf("<"));
5320
+ const tag2 = extractStructTagFromType(_type);
5321
+ const structTag2 = __spreadProps(__spreadValues({}, tag2), {
5322
+ type_arguments: generics.map((item) => extractStructTagFromType(item).source_address)
5323
+ });
5324
+ structTag2.type_arguments = structTag2.type_arguments.map((item) => {
5325
+ return CoinUtils.isSuiCoin(item) ? item : extractStructTagFromType(item).source_address;
5326
+ });
5327
+ structTag2.source_address = composeType(structTag2.full_address, structTag2.type_arguments);
5328
+ return structTag2;
5329
+ }
5330
+ const parts = _type.split("::");
5331
+ const isSuiCoin = _type === GAS_TYPE_ARG || _type === GAS_TYPE_ARG_LONG;
5332
+ const structTag = {
5333
+ full_address: _type,
5334
+ address: isSuiCoin ? "0x2" : utils.normalizeSuiObjectId(parts[0]),
5335
+ module: parts[1],
5336
+ name: parts[2],
5337
+ type_arguments: [],
5338
+ source_address: ""
5339
+ };
5340
+ structTag.full_address = `${structTag.address}::${structTag.module}::${structTag.name}`;
5341
+ structTag.source_address = composeType(structTag.full_address, structTag.type_arguments);
5342
+ return structTag;
5343
+ } catch (error) {
5344
+ return {
5345
+ full_address: type,
5346
+ address: "",
5347
+ module: "",
5348
+ name: "",
5349
+ type_arguments: [],
5350
+ source_address: type
5351
+ };
5352
+ }
5353
+ }
5354
+ function normalizeCoinType(coinType) {
5355
+ return extractStructTagFromType(coinType).source_address;
5356
+ }
5357
+ function fixSuiObjectId(value) {
5358
+ if (value.toLowerCase().startsWith("0x")) {
5359
+ return utils.normalizeSuiObjectId(value);
5360
+ }
5361
+ return value;
5362
+ }
5363
+ function patchFixSuiObjectId(data) {
5364
+ for (const key in data) {
5365
+ const type = typeof data[key];
5366
+ if (type === "object") {
5367
+ patchFixSuiObjectId(data[key]);
5368
+ } else if (type === "string") {
5369
+ const value = data[key];
5370
+ data[key] = fixSuiObjectId(value);
5371
+ }
5372
+ }
5373
+ }
5374
+ function createTarget(packageName, moduleName, functionName) {
5375
+ return `${packageName}::${moduleName}::${functionName}`;
5376
+ }
5377
+
4983
5378
  // src/types/CoinAssist.ts
4984
5379
  var COIN_TYPE = "0x2::coin::Coin";
4985
5380
  var COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;
@@ -5157,182 +5552,6 @@ var CoinUtils = class _CoinUtils {
5157
5552
  }
5158
5553
  };
5159
5554
 
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;
5167
- }
5168
- if (a < b) {
5169
- return LESS_THAN;
5170
- }
5171
- return GREATER_THAN;
5172
- }
5173
- function compare(symbolX, symbolY) {
5174
- let i = 0;
5175
- const len = symbolX.length <= symbolY.length ? symbolX.length : symbolY.length;
5176
- const lenCmp = cmp(symbolX.length, symbolY.length);
5177
- while (i < len) {
5178
- const elemCmp = cmp(symbolX.charCodeAt(i), symbolY.charCodeAt(i));
5179
- i += 1;
5180
- if (elemCmp !== 0) {
5181
- return elemCmp;
5182
- }
5183
- }
5184
- return lenCmp;
5185
- }
5186
- function isSortedSymbols(symbolX, symbolY) {
5187
- return compare(symbolX, symbolY) === LESS_THAN;
5188
- }
5189
- function composeType(address, ...args) {
5190
- const generics = Array.isArray(args[args.length - 1]) ? args.pop() : [];
5191
- const chains = [address, ...args].filter(Boolean);
5192
- let result = chains.join("::");
5193
- if (generics && generics.length) {
5194
- result += `<${generics.join(", ")}>`;
5195
- }
5196
- return result;
5197
- }
5198
- function extractAddressFromType(type) {
5199
- return type.split("::")[0];
5200
- }
5201
- function extractStructTagFromType(type) {
5202
- var _a;
5203
- try {
5204
- let _type = type.replace(/\s/g, "");
5205
- const genericsString = _type.match(/(<.+>)$/);
5206
- const generics = (_a = genericsString == null ? void 0 : genericsString[0]) == null ? void 0 : _a.match(/(\w+::\w+::\w+)(?:<.*?>(?!>))?/g);
5207
- if (generics) {
5208
- _type = _type.slice(0, _type.indexOf("<"));
5209
- const tag2 = extractStructTagFromType(_type);
5210
- const structTag2 = __spreadProps(__spreadValues({}, tag2), {
5211
- type_arguments: generics.map((item) => extractStructTagFromType(item).source_address)
5212
- });
5213
- structTag2.type_arguments = structTag2.type_arguments.map((item) => {
5214
- return CoinUtils.isSuiCoin(item) ? item : extractStructTagFromType(item).source_address;
5215
- });
5216
- structTag2.source_address = composeType(structTag2.full_address, structTag2.type_arguments);
5217
- return structTag2;
5218
- }
5219
- const parts = _type.split("::");
5220
- const isSuiCoin = _type === GAS_TYPE_ARG || _type === GAS_TYPE_ARG_LONG;
5221
- const structTag = {
5222
- full_address: _type,
5223
- address: isSuiCoin ? "0x2" : utils.normalizeSuiObjectId(parts[0]),
5224
- module: parts[1],
5225
- name: parts[2],
5226
- type_arguments: [],
5227
- source_address: ""
5228
- };
5229
- structTag.full_address = `${structTag.address}::${structTag.module}::${structTag.name}`;
5230
- structTag.source_address = composeType(structTag.full_address, structTag.type_arguments);
5231
- return structTag;
5232
- } catch (error) {
5233
- return {
5234
- full_address: type,
5235
- address: "",
5236
- module: "",
5237
- name: "",
5238
- type_arguments: [],
5239
- source_address: type
5240
- };
5241
- }
5242
- }
5243
- function normalizeCoinType(coinType) {
5244
- return extractStructTagFromType(coinType).source_address;
5245
- }
5246
- function fixSuiObjectId(value) {
5247
- if (value.toLowerCase().startsWith("0x")) {
5248
- return utils.normalizeSuiObjectId(value);
5249
- }
5250
- return value;
5251
- }
5252
- function patchFixSuiObjectId(data) {
5253
- for (const key in data) {
5254
- const type = typeof data[key];
5255
- if (type === "object") {
5256
- patchFixSuiObjectId(data[key]);
5257
- } else if (type === "string") {
5258
- const value = data[key];
5259
- data[key] = fixSuiObjectId(value);
5260
- }
5261
- }
5262
- }
5263
- function createTarget(packageName, moduleName, functionName) {
5264
- return `${packageName}::${moduleName}::${functionName}`;
5265
- }
5266
-
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
5555
  // src/errors.ts
5337
5556
  function getAggregatorServerErrorMessage(code) {
5338
5557
  switch (code) {
@@ -5344,980 +5563,11 @@ function getAggregatorServerErrorMessage(code) {
5344
5563
  return "No router";
5345
5564
  case 10003 /* InsufficientLiquidity */:
5346
5565
  return "Insufficient Liquidity";
5347
- default:
5348
- return "Unknown error";
5349
- }
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
- };
5366
- }
5367
- let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5368
- if (totalCoinBalance < amount) {
5369
- throw new AggregateError(
5370
- "Insufficient balance when build merge coin",
5371
- "InsufficientBalance" /* InsufficientBalance */
5372
- );
5373
- }
5374
- if (CoinUtils.isSuiCoin(coinType)) {
5375
- const resultCoin = txb.splitCoins(txb.gas, [
5376
- txb.pure.u64(amount.toString())
5377
- ]);
5378
- return {
5379
- targetCoin: resultCoin,
5380
- isMintZeroCoin: true,
5381
- targetCoinAmount: Number(amount.toString())
5382
- };
5383
- }
5384
- let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
5385
- let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
5386
- if (totalThreeCoinBalance < BigInt(amount)) {
5387
- sortCoinAssets = CoinUtils.sortByBalanceDes(usedCoinAsests);
5388
- }
5389
- let selectedCoinResult = CoinUtils.selectCoinObjectIdGreaterThanOrEqual(
5390
- sortCoinAssets,
5391
- amount
5392
- );
5393
- const [masterCoin, ...mergedCoin] = selectedCoinResult.objectArray;
5394
- if (mergedCoin.length > 0) {
5395
- txb.mergeCoins(
5396
- masterCoin,
5397
- mergedCoin.map((coin) => txb.object(coin))
5398
- );
5399
- }
5400
- return {
5401
- targetCoin: txb.object(masterCoin),
5402
- isMintZeroCoin: false,
5403
- targetCoinAmount: Number(amount.toString())
5404
- };
5405
- }
5406
- function transferOrDestoryCoin(txb, coinObject, coinType, config2) {
5407
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5408
- if (aggregatorPackage == null) {
5409
- throw new AggregateError(
5410
- "Aggregator package not set",
5411
- "MissAggregatorPackage" /* MissAggregatorPackage */
5412
- );
5413
- }
5414
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5415
- txb.moveCall({
5416
- target: createTarget(
5417
- aggregatorPublishedAt,
5418
- UTILS_MODULE,
5419
- TRANSFER_OR_DESTORY_COIN_FUNC
5420
- ),
5421
- typeArguments: [coinType],
5422
- arguments: [coinObject]
5423
- });
5424
- }
5425
- function checkCoinThresholdAndMergeCoin(txb, coins, coinType, amountLimit, config2) {
5426
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5427
- if (aggregatorPackage == null) {
5428
- throw new AggregateError(
5429
- "Aggregator package not set",
5430
- "MissAggregatorPackage" /* MissAggregatorPackage */
5431
- );
5432
- }
5433
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5434
- const vec = txb.makeMoveVec({
5435
- elements: coins
5436
- });
5437
- txb.moveCall({
5438
- target: createTarget(
5439
- aggregatorPublishedAt,
5440
- UTILS_MODULE,
5441
- CHECK_COINS_THRESHOLD_FUNC
5442
- ),
5443
- typeArguments: [coinType],
5444
- arguments: [vec, txb.pure.u64(amountLimit)]
5445
- });
5446
- const zeroCoin = mintZeroCoin(txb, coinType);
5447
- txb.moveCall({
5448
- target: createTarget(utils.SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
5449
- typeArguments: [coinType],
5450
- arguments: [zeroCoin, vec]
5451
- });
5452
- return zeroCoin;
5453
- }
5454
-
5455
- // src/utils/account_cap.ts
5456
- function getOrCreateAccountCap(txb, client, owner) {
5457
- return __async(this, null, function* () {
5458
- let accountCapStr = yield getAccountCap(client, owner);
5459
- if (accountCapStr !== null) {
5460
- return {
5461
- accountCap: txb.object(accountCapStr),
5462
- isCreate: false
5463
- };
5464
- }
5465
- const accountCap = txb.moveCall({
5466
- target: `${DEEPBOOK_PUBLISHED_AT}::${DEEPBOOK_CLOB_V2_MODULE}::create_account`,
5467
- typeArguments: [],
5468
- arguments: []
5469
- });
5470
- return {
5471
- accountCap,
5472
- isCreate: true
5473
- };
5474
- });
5475
- }
5476
- function getAccountCap(client, owner) {
5477
- return __async(this, null, function* () {
5478
- let limit = 50;
5479
- let cursor = null;
5480
- while (true) {
5481
- const ownedObjects = client.getOwnedObjects({
5482
- owner,
5483
- cursor,
5484
- limit,
5485
- filter: {
5486
- MoveModule: {
5487
- package: DEEPBOOK_PACKAGE_ID,
5488
- module: DEEPBOOK_CUSTODIAN_V2_MODULE
5489
- }
5490
- }
5491
- });
5492
- if (ownedObjects != null && ownedObjects.data != null) {
5493
- if (ownedObjects.data.length !== 0) {
5494
- return ownedObjects.data[0].data.objectId;
5495
- }
5496
- if (ownedObjects.data.length < 50) {
5497
- break;
5498
- }
5499
- } else {
5500
- break;
5501
- }
5502
- }
5503
- return null;
5504
- });
5505
- }
5506
-
5507
- // src/transaction/deepbook.ts
5508
- function deepbookSwapMovecall(swapParams, client, txb, config2) {
5509
- return __async(this, null, function* () {
5510
- const accountCapRes = yield getOrCreateAccountCap(
5511
- txb,
5512
- client,
5513
- config2.getWallet()
5514
- );
5515
- const accountCap = accountCapRes.accountCap;
5516
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5517
- if (aggregatorPackage == null) {
5518
- throw new AggregateError(
5519
- "Aggregator package not set",
5520
- "MissAggregatorPackage" /* MissAggregatorPackage */
5521
- );
5522
- }
5523
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5524
- if (swapParams.a2b) {
5525
- if (swapParams.coinA == null) {
5526
- throw new AggregateError(
5527
- "coinA is required",
5528
- "MissCoinA" /* MissCoinA */
5529
- );
5530
- }
5531
- } else {
5532
- if (swapParams.coinB == null) {
5533
- throw new AggregateError(
5534
- "coinB is required",
5535
- "MissCoinB" /* MissCoinB */
5536
- );
5537
- }
5538
- }
5539
- const args = swapParams.a2b ? [
5540
- txb.object(swapParams.poolId),
5541
- swapParams.amount,
5542
- txb.pure.u64(swapParams.amountLimit),
5543
- swapParams.coinA,
5544
- accountCap,
5545
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5546
- txb.object(CLOCK_ADDRESS)
5547
- ] : [
5548
- txb.object(swapParams.poolId),
5549
- swapParams.amount,
5550
- txb.pure.u64(swapParams.amountLimit),
5551
- swapParams.coinB,
5552
- accountCap,
5553
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5554
- txb.object(CLOCK_ADDRESS)
5555
- ];
5556
- let func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5557
- const target = createTarget(aggregatorPublishedAt, DEEPBOOK_MODULE, func);
5558
- const res = txb.moveCall({
5559
- target,
5560
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5561
- arguments: args
5562
- });
5563
- if (accountCapRes.isCreate) {
5564
- const target2 = createTarget(
5565
- aggregatorPublishedAt,
5566
- DEEPBOOK_MODULE,
5567
- TRANSFER_ACCOUNT_CAP
5568
- );
5569
- txb.moveCall({
5570
- target: target2,
5571
- typeArguments: [],
5572
- arguments: [accountCap]
5573
- });
5574
- }
5575
- return {
5576
- targetCoin: res[0],
5577
- amountIn: res[1],
5578
- amountOut: res[2],
5579
- txb
5580
- };
5581
- });
5582
- }
5583
-
5584
- // src/transaction/kriya.ts
5585
- function kriyaSwapMovecall(swapParams, txb, config2) {
5586
- return __async(this, null, function* () {
5587
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5588
- if (aggregatorPackage == null) {
5589
- throw new AggregateError("Aggregator package not set", "MissAggregatorPackage" /* MissAggregatorPackage */);
5590
- }
5591
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5592
- if (swapParams.a2b) {
5593
- if (swapParams.coinA == null) {
5594
- throw new AggregateError("coinA is required", "MissCoinA" /* MissCoinA */);
5595
- }
5596
- } else {
5597
- if (swapParams.coinB == null) {
5598
- throw new AggregateError("coinB is required", "MissCoinB" /* MissCoinB */);
5599
- }
5600
- }
5601
- const args = swapParams.a2b ? [
5602
- txb.object(swapParams.poolId),
5603
- swapParams.amount,
5604
- txb.pure.u64(swapParams.amountLimit),
5605
- swapParams.coinA,
5606
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5607
- ] : [
5608
- txb.object(swapParams.poolId),
5609
- swapParams.amount,
5610
- txb.pure.u64(swapParams.amountLimit),
5611
- swapParams.coinB,
5612
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5613
- ];
5614
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5615
- const target = createTarget(aggregatorPublishedAt, KRIYA_MODULE, func);
5616
- const res = txb.moveCall({
5617
- target,
5618
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5619
- arguments: args
5620
- });
5621
- return {
5622
- targetCoin: res[0],
5623
- amountIn: res[1],
5624
- amountOut: res[2],
5625
- txb
5626
- };
5627
- });
5628
- }
5629
-
5630
- // src/config.ts
5631
- var ENV = /* @__PURE__ */ ((ENV2) => {
5632
- ENV2[ENV2["MAINNET"] = 0] = "MAINNET";
5633
- ENV2[ENV2["TESTNET"] = 1] = "TESTNET";
5634
- return ENV2;
5635
- })(ENV || {});
5636
- var AggregatorConfig = class {
5637
- constructor(aggregatorUrl, fullNodeUrl, wallet, packages, env) {
5638
- this.aggregatorUrl = aggregatorUrl;
5639
- this.fullNodeUrl = fullNodeUrl;
5640
- this.wallet = wallet;
5641
- this.packages = packages;
5642
- this.env = env;
5643
- }
5644
- getAggregatorUrl() {
5645
- return this.aggregatorUrl;
5646
- }
5647
- getFullNodeUrl() {
5648
- return this.fullNodeUrl;
5649
- }
5650
- getWallet() {
5651
- return this.wallet;
5652
- }
5653
- getENV() {
5654
- return this.env;
5655
- }
5656
- getPackages() {
5657
- return this.packages;
5658
- }
5659
- getPackage(packageName) {
5660
- return this.packages.find((pkg) => pkg.packageName === packageName);
5661
- }
5662
- addPackage(newPackage) {
5663
- if (!newPackage.packageName || !newPackage.packageId || !newPackage.publishedAt) {
5664
- throw new Error("Invalid package data");
5665
- }
5666
- this.packages.push(newPackage);
5667
- }
5668
- removePackageById(packageId) {
5669
- this.packages = this.packages.filter((pkg) => pkg.packageId !== packageId);
5670
- }
5671
- findPackageById(packageId) {
5672
- return this.packages.find((pkg) => pkg.packageId === packageId);
5673
- }
5674
- };
5675
-
5676
- // src/transaction/cetus.ts
5677
- function cetusFlashSwapMovecall(swapParams, txb, config2) {
5678
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5679
- if (aggregatorPackage == null) {
5680
- throw new AggregateError(
5681
- "Aggregator package not set",
5682
- "MissAggregatorPackage" /* MissAggregatorPackage */
5683
- );
5684
- }
5685
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5686
- const globalConfigId = config2.getENV() === 0 /* MAINNET */ ? MAINNET_CETUS_GLOBAL_CONFIG_ID : TESTNET_CETUS_GLOBAL_CONFIG_ID;
5687
- const hasPartner = swapParams.partner != null && swapParams.partner.length > 0;
5688
- const args = hasPartner ? [
5689
- txb.object(globalConfigId),
5690
- txb.object(swapParams.poolId),
5691
- swapParams.amount,
5692
- txb.pure.u64(swapParams.amountLimit),
5693
- txb.pure.bool(swapParams.byAmountIn),
5694
- txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
5695
- txb.object(swapParams.partner.toString()),
5696
- txb.object(CLOCK_ADDRESS)
5697
- ] : [
5698
- txb.object(globalConfigId),
5699
- txb.object(swapParams.poolId),
5700
- swapParams.amount,
5701
- txb.pure.u64(swapParams.amountLimit),
5702
- txb.pure.bool(swapParams.byAmountIn),
5703
- txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
5704
- txb.object(CLOCK_ADDRESS)
5705
- ];
5706
- let func;
5707
- if (hasPartner) {
5708
- if (swapParams.a2b) {
5709
- func = FlashSwapWithPartnerA2BFunc;
5710
- } else {
5711
- func = FlashSwapWithPartnerB2AFunc;
5712
- }
5713
- } else {
5714
- if (swapParams.a2b) {
5715
- func = FlashSwapA2BFunc;
5716
- } else {
5717
- func = FlashSwapB2AFunc;
5718
- }
5719
- }
5720
- const target = createTarget(aggregatorPublishedAt, CETUS_MODULE, func);
5721
- const moveCallRes = txb.moveCall({
5722
- target,
5723
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5724
- arguments: args
5725
- });
5726
- return {
5727
- targetCoin: moveCallRes[0],
5728
- flashReceipt: moveCallRes[1],
5729
- payAmount: moveCallRes[2],
5730
- swapedAmount: moveCallRes[3],
5731
- txb
5732
- };
5733
- }
5734
- function cetusRepayFlashSwapMovecall(repayParams, txb, config2) {
5735
- return __async(this, null, function* () {
5736
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5737
- if (aggregatorPackage == null) {
5738
- throw new AggregateError(
5739
- "Aggregator package not set",
5740
- "MissAggregatorPackage" /* MissAggregatorPackage */
5741
- );
5742
- }
5743
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5744
- const globalConfigId = config2.getENV() === 0 /* MAINNET */ ? MAINNET_CETUS_GLOBAL_CONFIG_ID : TESTNET_CETUS_GLOBAL_CONFIG_ID;
5745
- const hasPartner = repayParams.partner != null && repayParams.partner.length > 0;
5746
- if (repayParams.a2b) {
5747
- if (repayParams.coinA == null) {
5748
- throw new AggregateError(
5749
- "coinA is required",
5750
- "MissCoinA" /* MissCoinA */
5751
- );
5752
- }
5753
- } else {
5754
- if (repayParams.coinB == null) {
5755
- throw new AggregateError(
5756
- "coinB is required",
5757
- "MissCoinB" /* MissCoinB */
5758
- );
5759
- }
5760
- }
5761
- let args;
5762
- if (hasPartner) {
5763
- if (repayParams.a2b) {
5764
- args = [
5765
- txb.object(globalConfigId),
5766
- txb.object(repayParams.poolId),
5767
- repayParams.coinA,
5768
- repayParams.receipt,
5769
- txb.object(repayParams.partner)
5770
- ];
5771
- } else {
5772
- args = [
5773
- txb.object(globalConfigId),
5774
- txb.object(repayParams.poolId),
5775
- repayParams.coinB,
5776
- repayParams.receipt,
5777
- txb.object(repayParams.partner)
5778
- ];
5779
- }
5780
- } else {
5781
- if (repayParams.a2b) {
5782
- args = [
5783
- txb.object(globalConfigId),
5784
- txb.object(repayParams.poolId),
5785
- repayParams.coinA,
5786
- repayParams.receipt
5787
- ];
5788
- } else {
5789
- args = [
5790
- txb.object(globalConfigId),
5791
- txb.object(repayParams.poolId),
5792
- repayParams.coinB,
5793
- repayParams.receipt
5794
- ];
5795
- }
5796
- }
5797
- let func;
5798
- if (hasPartner) {
5799
- if (repayParams.a2b) {
5800
- func = REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC;
5801
- } else {
5802
- func = REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC;
5803
- }
5804
- } else {
5805
- if (repayParams.a2b) {
5806
- func = REPAY_FLASH_SWAP_A2B_FUNC;
5807
- } else {
5808
- func = REPAY_FLASH_SWAP_B2A_FUNC;
5809
- }
5810
- }
5811
- const target = createTarget(aggregatorPublishedAt, CETUS_MODULE, func);
5812
- const res = txb.moveCall({
5813
- target,
5814
- typeArguments: [repayParams.coinAType, repayParams.coinBType],
5815
- arguments: args
5816
- });
5817
- return {
5818
- repayTargetCoin: res[0]
5819
- };
5820
- });
5821
- }
5822
- function cetusSwapWithOutLimit(swapParams, fromCoin, txb, config2) {
5823
- return __async(this, null, function* () {
5824
- const flashResult = cetusFlashSwapMovecall(swapParams, txb, config2);
5825
- const repayCoinA = swapParams.a2b ? fromCoin : void 0;
5826
- const repayCoinB = swapParams.a2b ? void 0 : fromCoin;
5827
- const repayParams = {
5828
- poolId: swapParams.poolId,
5829
- a2b: swapParams.a2b,
5830
- coinA: repayCoinA,
5831
- coinB: repayCoinB,
5832
- receipt: flashResult.flashReceipt,
5833
- coinAType: swapParams.coinAType,
5834
- coinBType: swapParams.coinBType,
5835
- partner: swapParams.partner
5836
- };
5837
- let nextInputAmount;
5838
- if (swapParams.byAmountIn) {
5839
- nextInputAmount = flashResult.swapedAmount;
5840
- } else {
5841
- nextInputAmount = flashResult.payAmount;
5842
- }
5843
- const repayResult = yield cetusRepayFlashSwapMovecall(
5844
- repayParams,
5845
- txb,
5846
- config2
5847
- );
5848
- return {
5849
- flashTargetCoin: flashResult.targetCoin,
5850
- repayTargetCoin: repayResult.repayTargetCoin,
5851
- nextInputAmount
5852
- };
5853
- });
5854
- }
5855
-
5856
- // src/math.ts
5857
- var import_bn2 = __toESM(require_bn());
5858
- function CalculateAmountLimit(expectAmount, byAmountIn, slippage) {
5859
- let amountLimit = ZERO;
5860
- if (byAmountIn) {
5861
- amountLimit = expectAmount.mul(new import_bn2.default(TEN_POW_NINE - slippage * TEN_POW_NINE)).div(new import_bn2.default(TEN_POW_NINE));
5862
- } else {
5863
- amountLimit = expectAmount.mul(new import_bn2.default(TEN_POW_NINE + slippage * TEN_POW_NINE)).div(new import_bn2.default(TEN_POW_NINE));
5864
- }
5865
- return Number(amountLimit.toString());
5866
- }
5867
- var MAX_SQER_PRICE_X64 = "79226673515401279992447579055";
5868
- var MIN_SQER_PRICE_X64 = "4295048016";
5869
- function GetDefaultSqrtPriceLimit(a2b) {
5870
- if (a2b) {
5871
- return new import_bn2.default(MIN_SQER_PRICE_X64);
5872
- } else {
5873
- return new import_bn2.default(MAX_SQER_PRICE_X64);
5874
- }
5875
- }
5876
- function sqrtPriceX64ToPrice(sqrtPriceStr, decimalsA, decimalsB) {
5877
- const sqrtPriceX64 = new decimal_default(sqrtPriceStr).mul(decimal_default.pow(2, -64));
5878
- return sqrtPriceX64.pow(2).mul(decimal_default.pow(10, decimalsA - decimalsB));
5879
- }
5880
-
5881
- // src/transaction/flowx.ts
5882
- function flowxAmmSwapMovecall(swapParams, txb, config2) {
5883
- return __async(this, null, function* () {
5884
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5885
- if (aggregatorPackage == null) {
5886
- throw new AggregateError(
5887
- "Aggregator package not set",
5888
- "MissAggregatorPackage" /* MissAggregatorPackage */
5889
- );
5890
- }
5891
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5892
- if (swapParams.a2b) {
5893
- if (swapParams.coinA == null) {
5894
- throw new AggregateError(
5895
- "coinA is required",
5896
- "MissCoinA" /* MissCoinA */
5897
- );
5898
- }
5899
- } else {
5900
- if (swapParams.coinB == null) {
5901
- throw new AggregateError(
5902
- "coinB is required",
5903
- "MissCoinB" /* MissCoinB */
5904
- );
5905
- }
5906
- }
5907
- const containerID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_FLOWX_AMM_CONTAINER_ID : TESTNET_FLOWX_AMM_CONTAINER_ID;
5908
- const swapCoin = swapParams.a2b ? swapParams.coinA : swapParams.coinB;
5909
- const args = [
5910
- txb.object(containerID),
5911
- swapParams.amount,
5912
- txb.pure.u64(swapParams.amountLimit),
5913
- swapCoin,
5914
- txb.pure.bool(swapParams.useFullInputCoinAmount)
5915
- ];
5916
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5917
- const target = createTarget(aggregatorPublishedAt, FLOWX_AMM_MODULE, func);
5918
- const res = txb.moveCall({
5919
- target,
5920
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
5921
- arguments: args
5922
- });
5923
- return {
5924
- targetCoin: res[0],
5925
- amountIn: res[1],
5926
- amountOut: res[2],
5927
- txb
5928
- };
5929
- });
5930
- }
5931
-
5932
- // src/transaction/turbos.ts
5933
- function turbosClmmSwapMovecall(swapParams, txb, config2) {
5934
- return __async(this, null, function* () {
5935
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
5936
- if (aggregatorPackage == null) {
5937
- throw new AggregateError(
5938
- "Aggregator package not set",
5939
- "MissAggregatorPackage" /* MissAggregatorPackage */
5940
- );
5941
- }
5942
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
5943
- if (swapParams.a2b) {
5944
- if (swapParams.coinA == null) {
5945
- throw new AggregateError(
5946
- "coinA is required",
5947
- "MissCoinA" /* MissCoinA */
5948
- );
5949
- }
5950
- } else {
5951
- if (swapParams.coinB == null) {
5952
- throw new AggregateError(
5953
- "coinB is required",
5954
- "MissCoinB" /* MissCoinB */
5955
- );
5956
- }
5957
- }
5958
- const sqrtPriceLimit = swapParams.a2b ? "4295048016" : "79226673515401279992447579055";
5959
- const args = swapParams.a2b ? [
5960
- txb.object(swapParams.poolId),
5961
- swapParams.amount,
5962
- txb.pure.u64(swapParams.amountLimit),
5963
- swapParams.coinA,
5964
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5965
- txb.pure.u128(sqrtPriceLimit),
5966
- txb.object(CLOCK_ADDRESS),
5967
- txb.object(TURBOS_VERSIONED)
5968
- ] : [
5969
- txb.object(swapParams.poolId),
5970
- swapParams.amount,
5971
- txb.pure.u64(swapParams.amountLimit),
5972
- swapParams.coinB,
5973
- txb.pure.bool(swapParams.useFullInputCoinAmount),
5974
- txb.pure.u128(sqrtPriceLimit),
5975
- txb.object(CLOCK_ADDRESS),
5976
- txb.object(TURBOS_VERSIONED)
5977
- ];
5978
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
5979
- const target = createTarget(aggregatorPublishedAt, TURBOS_MODULE, func);
5980
- const res = txb.moveCall({
5981
- target,
5982
- typeArguments: [
5983
- swapParams.coinAType,
5984
- swapParams.coinBType,
5985
- swapParams.feeType
5986
- ],
5987
- arguments: args
5988
- });
5989
- return {
5990
- targetCoin: res[0],
5991
- amountIn: res[1],
5992
- amountOut: res[2],
5993
- txb
5994
- };
5995
- });
5996
- }
5997
-
5998
- // src/transaction/aftermath.ts
5999
- function AftermathAmmSwapMovecall(swapParams, txb, config2) {
6000
- return __async(this, null, function* () {
6001
- const aggregatorPackage = config2.getPackage(AGGREGATOR);
6002
- if (aggregatorPackage == null) {
6003
- throw new AggregateError(
6004
- "Aggregator package not set",
6005
- "MissAggregatorPackage" /* MissAggregatorPackage */
6006
- );
6007
- }
6008
- const aggregatorPublishedAt = aggregatorPackage.publishedAt;
6009
- if (swapParams.a2b) {
6010
- if (swapParams.coinA == null) {
6011
- throw new AggregateError(
6012
- "coinA is required",
6013
- "MissCoinA" /* MissCoinA */
6014
- );
6015
- }
6016
- } else {
6017
- if (swapParams.coinB == null) {
6018
- throw new AggregateError(
6019
- "coinB is required",
6020
- "MissCoinB" /* MissCoinB */
6021
- );
6022
- }
6023
- }
6024
- const poolRegistryID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_REGISTRY_ID : TESTNET_AFTERMATH_REGISTRY_ID;
6025
- const vaultID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID : TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID;
6026
- const treasuryID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_TREASURY_ID : TESTNET_AFTERMATH_TREASURY_ID;
6027
- const insuranceFundID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_INSURANCE_FUND_ID : TESTNET_AFTERMATH_INSURANCE_FUND_ID;
6028
- const referrealVaultID = config2.getENV() === 0 /* MAINNET */ ? MAINNET_AFTERMATH_REFERRAL_VAULT_ID : TESTNET_AFTERMATH_REFERRAL_VAULT_ID;
6029
- const swapCoin = swapParams.a2b ? swapParams.coinA : swapParams.coinB;
6030
- const slippageArg = (1 - swapParams.slippage) * 1e18;
6031
- const args = [
6032
- txb.object(swapParams.poolId),
6033
- txb.object(poolRegistryID),
6034
- txb.object(vaultID),
6035
- txb.object(treasuryID),
6036
- txb.object(insuranceFundID),
6037
- txb.object(referrealVaultID),
6038
- swapParams.amount,
6039
- txb.pure.u64(swapParams.amountLimit),
6040
- txb.pure.u64(swapParams.amountOut),
6041
- txb.pure.u64(slippageArg),
6042
- swapCoin,
6043
- txb.pure.bool(swapParams.useFullInputCoinAmount)
6044
- ];
6045
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC;
6046
- const target = createTarget(aggregatorPublishedAt, AFTERMATH_MODULE, func);
6047
- const res = txb.moveCall({
6048
- target,
6049
- typeArguments: [
6050
- swapParams.coinAType,
6051
- swapParams.coinBType,
6052
- swapParams.lpSupplyType
6053
- ],
6054
- arguments: args
6055
- });
6056
- return {
6057
- targetCoin: res[0],
6058
- amountIn: res[1],
6059
- amountOut: res[2],
6060
- txb
6061
- };
6062
- });
6063
- }
6064
-
6065
- // src/transaction/router.ts
6066
- function expectInputRouterSwap(client, params, txb, fromCoin, config2, partner) {
6067
- return __async(this, null, function* () {
6068
- const splitAmounts = params.routers.map(
6069
- (router) => router.amountIn.toString()
6070
- );
6071
- const targetCoins = [];
6072
- const fromCoins = txb.splitCoins(fromCoin, splitAmounts);
6073
- for (let i = 0; i < params.routers.length; i++) {
6074
- const router = params.routers[i];
6075
- let intermediateTargetCoin;
6076
- let nextFromCoin = fromCoins[i];
6077
- let nextFlashAmount = txb.pure.u64(splitAmounts[i]);
6078
- for (let j = 0; j < router.path.length; j++) {
6079
- const firstPathPool = j === 0;
6080
- const path = router.path[j];
6081
- if (path.provider === CETUS_DEX) {
6082
- const swapParams = {
6083
- poolId: path.id,
6084
- amount: nextFlashAmount,
6085
- amountLimit: "0",
6086
- a2b: path.a2b,
6087
- byAmountIn: true,
6088
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b),
6089
- partner,
6090
- coinAType: path.a2b ? path.from : path.target,
6091
- coinBType: path.a2b ? path.target : path.from
6092
- };
6093
- const swapResult = yield cetusSwapWithOutLimit(
6094
- swapParams,
6095
- nextFromCoin,
6096
- txb,
6097
- config2
6098
- );
6099
- transferOrDestoryCoin(
6100
- txb,
6101
- swapResult.repayTargetCoin,
6102
- path.from,
6103
- config2
6104
- );
6105
- intermediateTargetCoin = swapResult.flashTargetCoin;
6106
- nextFromCoin = intermediateTargetCoin;
6107
- nextFlashAmount = swapResult.nextInputAmount;
6108
- }
6109
- if (path.provider === DEEPBOOK_DEX) {
6110
- const coinA = path.a2b ? nextFromCoin : void 0;
6111
- const coinB = path.a2b ? void 0 : nextFromCoin;
6112
- const swapParams = {
6113
- poolId: path.id,
6114
- a2b: path.a2b,
6115
- amount: nextFlashAmount,
6116
- amountLimit: 0,
6117
- coinA,
6118
- coinB,
6119
- useFullInputCoinAmount: !firstPathPool,
6120
- coinAType: path.a2b ? path.from : path.target,
6121
- coinBType: path.a2b ? params.targetCoinType : path.from
6122
- };
6123
- const deepbookSwapResult = yield deepbookSwapMovecall(
6124
- swapParams,
6125
- client,
6126
- txb,
6127
- config2
6128
- );
6129
- intermediateTargetCoin = deepbookSwapResult.targetCoin;
6130
- nextFromCoin = intermediateTargetCoin;
6131
- nextFlashAmount = deepbookSwapResult.amountOut;
6132
- }
6133
- if (path.provider === KRIYA_DEX) {
6134
- const coinA = path.a2b ? nextFromCoin : void 0;
6135
- const coinB = path.a2b ? void 0 : nextFromCoin;
6136
- const swapParams = {
6137
- poolId: path.id,
6138
- amount: nextFlashAmount,
6139
- amountLimit: 0,
6140
- a2b: path.a2b,
6141
- byAmountIn: true,
6142
- coinA,
6143
- coinB,
6144
- useFullInputCoinAmount: !firstPathPool,
6145
- coinAType: path.a2b ? path.from : path.target,
6146
- coinBType: path.a2b ? path.target : path.from
6147
- };
6148
- const swapResult = yield kriyaSwapMovecall(swapParams, txb, config2);
6149
- intermediateTargetCoin = swapResult.targetCoin;
6150
- nextFromCoin = intermediateTargetCoin;
6151
- nextFlashAmount = swapResult.amountOut;
6152
- }
6153
- if (path.provider === FLOWX_AMM) {
6154
- const coinA = path.a2b ? nextFromCoin : void 0;
6155
- const coinB = path.a2b ? void 0 : nextFromCoin;
6156
- const swapParams = {
6157
- amount: nextFlashAmount,
6158
- amountLimit: 0,
6159
- a2b: path.a2b,
6160
- byAmountIn: true,
6161
- coinA,
6162
- coinB,
6163
- useFullInputCoinAmount: !firstPathPool,
6164
- coinAType: path.a2b ? path.from : path.target,
6165
- coinBType: path.a2b ? path.target : path.from
6166
- };
6167
- const swapResult = yield flowxAmmSwapMovecall(swapParams, txb, config2);
6168
- intermediateTargetCoin = swapResult.targetCoin;
6169
- nextFromCoin = intermediateTargetCoin;
6170
- nextFlashAmount = swapResult.amountOut;
6171
- }
6172
- if (path.provider === TURBOS_DEX) {
6173
- const coinA = path.a2b ? nextFromCoin : void 0;
6174
- const coinB = path.a2b ? void 0 : nextFromCoin;
6175
- let feeType = "";
6176
- if (path.extendedDetails != null && path.extendedDetails.turbosFeeType != null) {
6177
- feeType = path.extendedDetails.turbosFeeType;
6178
- } else {
6179
- throw new AggregateError(
6180
- "Build turbos swap movecall error: ",
6181
- "MissTurbosFeeType" /* MissTurbosFeeType */
6182
- );
6183
- }
6184
- const swapParams = {
6185
- poolId: path.id,
6186
- amount: nextFlashAmount,
6187
- amountLimit: 0,
6188
- a2b: path.a2b,
6189
- byAmountIn: true,
6190
- coinA,
6191
- coinB,
6192
- useFullInputCoinAmount: !firstPathPool,
6193
- coinAType: path.a2b ? path.from : path.target,
6194
- coinBType: path.a2b ? path.target : path.from,
6195
- feeType
6196
- };
6197
- const swapResult = yield turbosClmmSwapMovecall(swapParams, txb, config2);
6198
- intermediateTargetCoin = swapResult.targetCoin;
6199
- nextFromCoin = intermediateTargetCoin;
6200
- nextFlashAmount = swapResult.amountOut;
6201
- }
6202
- if (path.provider === AFTERMATH_AMM) {
6203
- const coinA = path.a2b ? nextFromCoin : void 0;
6204
- const coinB = path.a2b ? void 0 : nextFromCoin;
6205
- let lpSupplyType = "";
6206
- if (path.extendedDetails != null && path.extendedDetails.aftermathLpSupplyType != null) {
6207
- lpSupplyType = path.extendedDetails.aftermathLpSupplyType;
6208
- } else {
6209
- throw new AggregateError(
6210
- "Build aftermath swap movecall error: ",
6211
- "MissAftermathLpSupplyType" /* MissAftermathLpSupplyType */
6212
- );
6213
- }
6214
- const swapParams = {
6215
- poolId: path.id,
6216
- amount: nextFlashAmount,
6217
- amountOut: path.amountOut,
6218
- amountLimit: 0,
6219
- a2b: path.a2b,
6220
- byAmountIn: true,
6221
- coinA,
6222
- coinB,
6223
- useFullInputCoinAmount: !firstPathPool,
6224
- coinAType: path.a2b ? path.from : path.target,
6225
- coinBType: path.a2b ? path.target : path.from,
6226
- slippage: params.slippage,
6227
- lpSupplyType
6228
- };
6229
- const swapResult = yield AftermathAmmSwapMovecall(
6230
- swapParams,
6231
- txb,
6232
- config2
6233
- );
6234
- intermediateTargetCoin = swapResult.targetCoin;
6235
- nextFromCoin = intermediateTargetCoin;
6236
- nextFlashAmount = swapResult.amountOut;
6237
- }
6238
- }
6239
- targetCoins.push(nextFromCoin);
6240
- }
6241
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config2);
6242
- return targetCoins;
6243
- });
6244
- }
6245
- function expectOutputRouterSwap(params, txb, fromCoin, config2, partner) {
6246
- return __async(this, null, function* () {
6247
- const splitAmounts = params.routers.map(
6248
- (router) => router.amountOut.toString()
6249
- );
6250
- const returnCoins = [];
6251
- const receipts = [];
6252
- const targetCoins = [];
6253
- for (let i = 0; i < params.routers.length; i++) {
6254
- const router = params.routers[i];
6255
- let nextFlashAmount = txb.pure.u64(splitAmounts[i]);
6256
- for (let j = router.path.length - 1; j >= 0; j--) {
6257
- const path = router.path[j];
6258
- const coinAType = path.a2b ? path.from : path.target;
6259
- const coinBType = path.a2b ? path.target : path.from;
6260
- const swapParams = {
6261
- poolId: path.id,
6262
- amount: nextFlashAmount,
6263
- amountLimit: U64_MAX,
6264
- a2b: path.a2b,
6265
- byAmountIn: false,
6266
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b),
6267
- //
6268
- partner,
6269
- coinAType,
6270
- coinBType
6271
- };
6272
- const flashSwapResult = cetusFlashSwapMovecall(swapParams, txb, config2);
6273
- nextFlashAmount = flashSwapResult.payAmount;
6274
- returnCoins.unshift(flashSwapResult.targetCoin);
6275
- receipts.unshift(flashSwapResult.flashReceipt);
6276
- }
6277
- let nextRepayCoin = fromCoin;
6278
- for (let j = 0; j < router.path.length; j++) {
6279
- const path = router.path[j];
6280
- const coinAType = path.a2b ? path.from : path.target;
6281
- const coinBType = path.a2b ? path.target : path.from;
6282
- const repayParams = {
6283
- poolId: path.id,
6284
- a2b: path.a2b,
6285
- partner,
6286
- coinA: path.a2b ? nextRepayCoin : void 0,
6287
- coinB: path.a2b ? void 0 : nextRepayCoin,
6288
- receipt: receipts[j],
6289
- coinAType,
6290
- coinBType
6291
- };
6292
- const repayResult = yield cetusRepayFlashSwapMovecall(
6293
- repayParams,
6294
- txb,
6295
- config2
6296
- );
6297
- nextRepayCoin = returnCoins[j];
6298
- if (j === 0) {
6299
- fromCoin = repayResult.repayTargetCoin;
6300
- } else {
6301
- transferOrDestoryCoin(
6302
- txb,
6303
- repayResult.repayTargetCoin,
6304
- path.from,
6305
- config2
6306
- );
6307
- }
6308
- if (j === router.path.length - 1) {
6309
- targetCoins.push(nextRepayCoin);
6310
- }
6311
- }
6312
- }
6313
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config2);
6314
- return targetCoins;
6315
- });
5566
+ default:
5567
+ return "Unknown error";
5568
+ }
6316
5569
  }
6317
5570
 
6318
- // src/client.ts
6319
- var import_bn4 = __toESM(require_bn());
6320
-
6321
5571
  // src/utils/coin.ts
6322
5572
  function completionCoin(s) {
6323
5573
  const index = s.indexOf("::");
@@ -6349,6 +5599,61 @@ function compareCoins(coinA, coinB) {
6349
5599
  }
6350
5600
  return true;
6351
5601
  }
5602
+ function mintZeroCoin(txb, coinType) {
5603
+ return txb.moveCall({
5604
+ target: "0x2::coin::zero",
5605
+ typeArguments: [coinType]
5606
+ });
5607
+ }
5608
+ function buildInputCoin(txb, allCoins, amount, coinType) {
5609
+ const usedCoinAsests = CoinUtils.getCoinAssets(coinType, allCoins);
5610
+ if (amount === BigInt(0) && usedCoinAsests.length === 0) {
5611
+ const zeroCoin = mintZeroCoin(txb, coinType);
5612
+ return {
5613
+ targetCoin: zeroCoin,
5614
+ isMintZeroCoin: true,
5615
+ targetCoinAmount: 0
5616
+ };
5617
+ }
5618
+ let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
5619
+ if (totalCoinBalance < amount) {
5620
+ throw new AggregateError(
5621
+ "Insufficient balance when build merge coin",
5622
+ "InsufficientBalance" /* InsufficientBalance */
5623
+ );
5624
+ }
5625
+ if (CoinUtils.isSuiCoin(coinType)) {
5626
+ const resultCoin = txb.splitCoins(txb.gas, [
5627
+ txb.pure.u64(amount.toString())
5628
+ ]);
5629
+ return {
5630
+ targetCoin: resultCoin,
5631
+ isMintZeroCoin: true,
5632
+ targetCoinAmount: Number(amount.toString())
5633
+ };
5634
+ }
5635
+ let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
5636
+ let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
5637
+ if (totalThreeCoinBalance < BigInt(amount)) {
5638
+ sortCoinAssets = CoinUtils.sortByBalanceDes(usedCoinAsests);
5639
+ }
5640
+ let selectedCoinResult = CoinUtils.selectCoinObjectIdGreaterThanOrEqual(
5641
+ sortCoinAssets,
5642
+ amount
5643
+ );
5644
+ const [masterCoin, ...mergedCoin] = selectedCoinResult.objectArray;
5645
+ if (mergedCoin.length > 0) {
5646
+ txb.mergeCoins(
5647
+ masterCoin,
5648
+ mergedCoin.map((coin) => txb.object(coin))
5649
+ );
5650
+ }
5651
+ return {
5652
+ targetCoin: txb.object(masterCoin),
5653
+ isMintZeroCoin: false,
5654
+ targetCoinAmount: Number(amount.toString())
5655
+ };
5656
+ }
6352
5657
 
6353
5658
  // src/utils/transaction.ts
6354
5659
  function checkInvalidSuiAddress(address) {
@@ -6359,30 +5664,53 @@ function checkInvalidSuiAddress(address) {
6359
5664
  }
6360
5665
 
6361
5666
  // src/transaction/swap.ts
5667
+ var import_bn4 = __toESM(require_bn());
5668
+
5669
+ // src/math.ts
6362
5670
  var import_bn3 = __toESM(require_bn());
6363
- function swapInPools(client, params, config2) {
5671
+
5672
+ // src/const.ts
5673
+ var import_bn2 = __toESM(require_bn());
5674
+ var ZERO = new import_bn2.default(0);
5675
+ var ONE = new import_bn2.default(1);
5676
+ var TWO = new import_bn2.default(2);
5677
+ var U128 = TWO.pow(new import_bn2.default(128));
5678
+ var U64_MAX_BN = new import_bn2.default("18446744073709551615");
5679
+ var U64_MAX = "18446744073709551615";
5680
+ var TEN_POW_NINE = 1e9;
5681
+
5682
+ // src/math.ts
5683
+ function CalculateAmountLimit(expectAmount, byAmountIn, slippage) {
5684
+ let amountLimit = ZERO;
5685
+ if (byAmountIn) {
5686
+ amountLimit = expectAmount.mul(new import_bn3.default(TEN_POW_NINE - slippage * TEN_POW_NINE)).div(new import_bn3.default(TEN_POW_NINE));
5687
+ } else {
5688
+ amountLimit = expectAmount.mul(new import_bn3.default(TEN_POW_NINE + slippage * TEN_POW_NINE)).div(new import_bn3.default(TEN_POW_NINE));
5689
+ }
5690
+ return Number(amountLimit.toString());
5691
+ }
5692
+ function sqrtPriceX64ToPrice(sqrtPriceStr, decimalsA, decimalsB) {
5693
+ const sqrtPriceX64 = new decimal_default(sqrtPriceStr).mul(decimal_default.pow(2, -64));
5694
+ return sqrtPriceX64.pow(2).mul(decimal_default.pow(10, decimalsA - decimalsB));
5695
+ }
5696
+
5697
+ // src/transaction/swap.ts
5698
+ function swapInPools(client, params, sender) {
6364
5699
  return __async(this, null, function* () {
6365
5700
  var _a, _b, _c, _d, _e;
6366
5701
  const { from, target, amount, byAmountIn, pools } = params;
6367
5702
  const fromCoin = completionCoin(from);
6368
5703
  const targetCoin = completionCoin(target);
6369
5704
  const tx = new transactions.Transaction();
6370
- const a2b = compareCoins(fromCoin, targetCoin);
6371
- const integratePackage = config2.getPackage(INTEGRATE);
6372
- if (integratePackage == null) {
6373
- throw new AggregateError(
6374
- "Aggregator package not set",
6375
- "MissAggregatorPackage" /* MissAggregatorPackage */
6376
- );
6377
- }
6378
- const integratePublishedAt = integratePackage.publishedAt;
6379
- const coinA = a2b ? fromCoin : targetCoin;
6380
- const coinB = a2b ? targetCoin : fromCoin;
5705
+ const direction = compareCoins(fromCoin, targetCoin);
5706
+ const integratePublishedAt = "0x8faab90228e4c4df91c41626bbaefa19fc25c514405ac64de54578dec9e6f5ee";
5707
+ const coinA = direction ? fromCoin : targetCoin;
5708
+ const coinB = direction ? targetCoin : fromCoin;
6381
5709
  const typeArguments = [coinA, coinB];
6382
5710
  for (let i = 0; i < pools.length; i++) {
6383
5711
  const args = [
6384
5712
  tx.object(pools[i]),
6385
- tx.pure.bool(a2b),
5713
+ tx.pure.bool(direction),
6386
5714
  tx.pure.bool(byAmountIn),
6387
5715
  tx.pure.u64(amount.toString())
6388
5716
  ];
@@ -6392,7 +5720,7 @@ function swapInPools(client, params, config2) {
6392
5720
  typeArguments
6393
5721
  });
6394
5722
  }
6395
- if (!checkInvalidSuiAddress(config2.getWallet())) {
5723
+ if (!checkInvalidSuiAddress(sender)) {
6396
5724
  throw new AggregateError(
6397
5725
  "Aggregator package not set",
6398
5726
  "InvalidWallet" /* InvalidWallet */
@@ -6400,7 +5728,7 @@ function swapInPools(client, params, config2) {
6400
5728
  }
6401
5729
  const simulateRes = yield client.devInspectTransactionBlock({
6402
5730
  transactionBlock: tx,
6403
- sender: config2.getWallet()
5731
+ sender
6404
5732
  });
6405
5733
  if (simulateRes.error != null) {
6406
5734
  throw new AggregateError(
@@ -6424,13 +5752,13 @@ function swapInPools(client, params, config2) {
6424
5752
  continue;
6425
5753
  }
6426
5754
  if (params.byAmountIn) {
6427
- const amount2 = new import_bn3.BN(valueData[i].parsedJson.data.amount_out);
5755
+ const amount2 = new import_bn4.BN(valueData[i].parsedJson.data.amount_out);
6428
5756
  if (amount2.gt(tempMaxAmount)) {
6429
5757
  tempIndex = i;
6430
5758
  tempMaxAmount = amount2;
6431
5759
  }
6432
5760
  } else {
6433
- const amount2 = new import_bn3.BN(valueData[i].parsedJson.data.amount_out);
5761
+ const amount2 = new import_bn4.BN(valueData[i].parsedJson.data.amount_out);
6434
5762
  if (amount2.lt(tempMaxAmount)) {
6435
5763
  tempIndex = i;
6436
5764
  tempMaxAmount = amount2;
@@ -6438,6 +5766,7 @@ function swapInPools(client, params, config2) {
6438
5766
  }
6439
5767
  }
6440
5768
  const event = valueData[tempIndex].parsedJson.data;
5769
+ console.log("event", JSON.stringify(event, null, 2));
6441
5770
  const currentSqrtPrice = event.step_results[0].current_sqrt_price;
6442
5771
  const [decimalA, decimalB] = yield Promise.all([
6443
5772
  client.getCoinMetadata({ coinType: coinA }).then((metadata) => metadata == null ? void 0 : metadata.decimals),
@@ -6455,24 +5784,24 @@ function swapInPools(client, params, config2) {
6455
5784
  decimalB
6456
5785
  );
6457
5786
  const routeData = {
6458
- amountIn: new import_bn3.BN((_b = event.amount_in) != null ? _b : 0),
6459
- amountOut: new import_bn3.BN((_c = event.amount_out) != null ? _c : 0),
5787
+ amountIn: new import_bn4.BN((_b = event.amount_in) != null ? _b : 0),
5788
+ amountOut: new import_bn4.BN((_c = event.amount_out) != null ? _c : 0),
6460
5789
  routes: [
6461
5790
  {
6462
5791
  path: [
6463
5792
  {
6464
5793
  id: pools[tempIndex],
6465
- a2b,
6466
- provider: CETUS_DEX,
5794
+ direction,
5795
+ provider: "CETUS",
6467
5796
  from: fromCoin,
6468
5797
  target: targetCoin,
6469
- feeRate: 0,
6470
- amountIn: 0,
6471
- amountOut: 0
5798
+ feeRate: event.fee_rate,
5799
+ amountIn: event.amount_in,
5800
+ amountOut: event.amount_out
6472
5801
  }
6473
5802
  ],
6474
- amountIn: new import_bn3.BN((_d = event.amount_in) != null ? _d : 0),
6475
- amountOut: new import_bn3.BN((_e = event.amount_out) != null ? _e : 0),
5803
+ amountIn: new import_bn4.BN((_d = event.amount_in) != null ? _d : 0),
5804
+ amountOut: new import_bn4.BN((_e = event.amount_out) != null ? _e : 0),
6476
5805
  initialPrice
6477
5806
  }
6478
5807
  ],
@@ -6485,11 +5814,146 @@ function swapInPools(client, params, config2) {
6485
5814
  return result;
6486
5815
  });
6487
5816
  }
6488
- var AggregatorClient = class {
6489
- constructor(config2) {
6490
- this.config = config2;
6491
- this.client = new client.SuiClient({ url: config2.getFullNodeUrl() });
6492
- this.wallet = config2.getWallet();
5817
+
5818
+ // src/transaction/kriya_v3.ts
5819
+ var KriyaV3 = class {
5820
+ constructor(env) {
5821
+ if (env !== 0 /* Mainnet */) {
5822
+ throw new Error("Kriya clmm only supported on mainnet");
5823
+ }
5824
+ this.version = "0xf5145a7ac345ca8736cf8c76047d00d6d378f30e81be6f6eb557184d9de93c78";
5825
+ }
5826
+ swap(client, txb, path, inputCoin) {
5827
+ return __async(this, null, function* () {
5828
+ const { direction, from, target } = path;
5829
+ const [func, coinAType, coinBType] = direction ? ["swap_a2b", from, target] : ["swap_b2a", target, from];
5830
+ const args = [
5831
+ txb.object(path.id),
5832
+ inputCoin,
5833
+ txb.object(this.version),
5834
+ txb.object(CLOCK_ADDRESS)
5835
+ ];
5836
+ const res = txb.moveCall({
5837
+ target: `${client.publishedAt()}::kriya_clmm::${func}`,
5838
+ typeArguments: [coinAType, coinBType],
5839
+ arguments: args
5840
+ });
5841
+ return res;
5842
+ });
5843
+ }
5844
+ };
5845
+
5846
+ // src/transaction/haedal.ts
5847
+ var Haedal = class {
5848
+ constructor(env) {
5849
+ if (env !== 0 /* Mainnet */) {
5850
+ throw new Error("Haedal only supported on mainnet");
5851
+ }
5852
+ }
5853
+ swap(client, txb, path, inputCoin) {
5854
+ return __async(this, null, function* () {
5855
+ const { direction } = path;
5856
+ if (!direction) {
5857
+ throw new Error("Haedal not support b2a swap");
5858
+ }
5859
+ const func = "swap_a2b";
5860
+ console.log("haedal path id", path.id);
5861
+ const args = [txb.object(path.id), txb.object("0x5"), inputCoin];
5862
+ const res = txb.moveCall({
5863
+ target: `${client.publishedAt()}::haedal::${func}`,
5864
+ typeArguments: [],
5865
+ arguments: args
5866
+ });
5867
+ return res;
5868
+ });
5869
+ }
5870
+ };
5871
+
5872
+ // src/transaction/afsui.ts
5873
+ var Afsui = class {
5874
+ constructor(env) {
5875
+ if (env !== 0 /* Mainnet */) {
5876
+ throw new Error("Afsui only supported on mainnet");
5877
+ }
5878
+ this.stakedSuiVault = "0x2f8f6d5da7f13ea37daa397724280483ed062769813b6f31e9788e59cc88994d";
5879
+ this.safe = "0xeb685899830dd5837b47007809c76d91a098d52aabbf61e8ac467c59e5cc4610";
5880
+ this.referVault = "0x4ce9a19b594599536c53edb25d22532f82f18038dc8ef618afd00fbbfb9845ef";
5881
+ this.validator = "0xd30018ec3f5ff1a3c75656abf927a87d7f0529e6dc89c7ddd1bd27ecb05e3db2";
5882
+ }
5883
+ swap(client, txb, path, inputCoin) {
5884
+ return __async(this, null, function* () {
5885
+ const { direction } = path;
5886
+ if (!direction) {
5887
+ throw new Error("Afsui not support b2a swap");
5888
+ }
5889
+ const func = "swap_a2b";
5890
+ const args = [
5891
+ txb.object(this.stakedSuiVault),
5892
+ txb.object(this.safe),
5893
+ txb.object("0x5"),
5894
+ txb.object(this.referVault),
5895
+ txb.object(this.validator),
5896
+ inputCoin
5897
+ ];
5898
+ const res = txb.moveCall({
5899
+ target: `${client.publishedAt()}::afsui::${func}`,
5900
+ typeArguments: [],
5901
+ arguments: args
5902
+ });
5903
+ return res;
5904
+ });
5905
+ }
5906
+ };
5907
+
5908
+ // src/transaction/volo.ts
5909
+ var Volo = class {
5910
+ constructor(env) {
5911
+ if (env !== 0 /* Mainnet */) {
5912
+ throw new Error("Volo only supported on mainnet");
5913
+ }
5914
+ this.nativePool = "0x7fa2faa111b8c65bea48a23049bfd81ca8f971a262d981dcd9a17c3825cb5baf";
5915
+ this.metadata = "0x680cd26af32b2bde8d3361e804c53ec1d1cfe24c7f039eb7f549e8dfde389a60";
5916
+ }
5917
+ swap(client, txb, path, inputCoin) {
5918
+ return __async(this, null, function* () {
5919
+ const { direction } = path;
5920
+ if (!direction) {
5921
+ throw new Error("Volo not support b2a swap");
5922
+ }
5923
+ const func = "swap_a2b";
5924
+ const args = [
5925
+ txb.object(this.nativePool),
5926
+ txb.object(this.metadata),
5927
+ txb.object("0x5"),
5928
+ inputCoin
5929
+ ];
5930
+ const res = txb.moveCall({
5931
+ target: `${client.publishedAt()}::volo::${func}`,
5932
+ typeArguments: [],
5933
+ arguments: args
5934
+ });
5935
+ return res;
5936
+ });
5937
+ }
5938
+ };
5939
+
5940
+ // src/client.ts
5941
+ var CETUS = "CETUS";
5942
+ var DEEPBOOKV2 = "DEEPBOOK";
5943
+ var KRIYA = "KRIYA";
5944
+ var FLOWXV2 = "FLOWX";
5945
+ var KRIYAV3 = "KRIYAV3";
5946
+ var TURBOS = "TURBOS";
5947
+ var AFTERMATH = "AFTERMATH";
5948
+ var HAEDAL = "HAEDAL";
5949
+ var VOLO = "VOLO";
5950
+ var AFSUI = "AFSUI";
5951
+ var AggregatorClient5 = class {
5952
+ constructor(endpoint, signer, client, env) {
5953
+ this.endpoint = endpoint;
5954
+ this.client = client;
5955
+ this.signer = signer;
5956
+ this.env = env;
6493
5957
  this.allCoins = [];
6494
5958
  }
6495
5959
  getAllCoins() {
@@ -6499,7 +5963,7 @@ var AggregatorClient = class {
6499
5963
  const allCoins = [];
6500
5964
  while (true) {
6501
5965
  const gotAllCoins = yield this.client.getAllCoins({
6502
- owner: this.wallet,
5966
+ owner: this.signer,
6503
5967
  cursor,
6504
5968
  limit
6505
5969
  });
@@ -6518,79 +5982,95 @@ var AggregatorClient = class {
6518
5982
  return allCoins;
6519
5983
  });
6520
5984
  }
6521
- findRouter(fromRouterParams) {
5985
+ findRouters(params) {
6522
5986
  return __async(this, null, function* () {
6523
- const {
6524
- from,
6525
- target,
6526
- amount,
6527
- byAmountIn,
6528
- depth,
6529
- splitAlgorithm,
6530
- splitFactor,
6531
- splitCount,
6532
- providers
6533
- } = fromRouterParams;
6534
- const fromCoin = completionCoin(from);
6535
- const targetCoin = completionCoin(target);
6536
- let url = this.config.getAggregatorUrl() + `/find_routes?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`;
6537
- if (depth) {
6538
- url += `&depth=${depth}`;
6539
- }
6540
- if (splitAlgorithm) {
6541
- url += `&split_algorithm=${splitAlgorithm}`;
6542
- }
6543
- if (splitFactor) {
6544
- url += `&split_factor=${splitFactor}`;
6545
- }
6546
- if (splitCount) {
6547
- url += `&split_count=${splitCount}`;
5987
+ return getRouterResult(this.endpoint, params);
5988
+ });
5989
+ }
5990
+ expectInputSwap(txb, inputCoin, routers, amountOutLimit, partner) {
5991
+ return __async(this, null, function* () {
5992
+ if (routers.length === 0) {
5993
+ throw new Error("No router found");
6548
5994
  }
6549
- if (providers) {
6550
- if (providers.length > 0) {
6551
- url += `&providers=${providers.join(",")}`;
6552
- }
5995
+ const splitAmounts = routers.map((router) => router.amountIn.toString());
5996
+ const inputCoinType = routers[0].path[0].from;
5997
+ const outputCoinType = routers[0].path[routers[0].path.length - 1].target;
5998
+ const inputCoins = txb.splitCoins(inputCoin, splitAmounts);
5999
+ const outputCoins = [];
6000
+ for (let i = 0; i < routers.length; i++) {
6001
+ if (routers[i].path.length === 0) {
6002
+ throw new Error("Empty path");
6003
+ }
6004
+ let nextCoin = inputCoins[i];
6005
+ for (const path of routers[i].path) {
6006
+ const dex = this.newDex(path.provider, partner);
6007
+ nextCoin = yield dex.swap(this, txb, path, nextCoin);
6008
+ }
6009
+ outputCoins.push(nextCoin);
6553
6010
  }
6554
- const response = yield fetch(url);
6555
- if (!response.ok) {
6556
- return {
6557
- amountIn: ZERO,
6558
- amountOut: ZERO,
6559
- routes: [],
6560
- insufficientLiquidity: false,
6561
- error: {
6562
- code: 10001 /* NumberTooLarge */,
6563
- msg: getAggregatorServerErrorMessage(
6564
- 10001 /* NumberTooLarge */
6565
- )
6011
+ this.transferOrDestoryCoin(txb, inputCoin, inputCoinType);
6012
+ const mergedTargetCointhis = this.checkCoinThresholdAndMergeCoin(
6013
+ txb,
6014
+ outputCoins,
6015
+ outputCoinType,
6016
+ amountOutLimit
6017
+ );
6018
+ return mergedTargetCointhis;
6019
+ });
6020
+ }
6021
+ expectOutputSwap(txb, inputCoin, routers, partner) {
6022
+ return __async(this, null, function* () {
6023
+ const returnCoins = [];
6024
+ const receipts = [];
6025
+ const targetCoins = [];
6026
+ const dex = new Cetus(this.env, partner);
6027
+ for (let i = 0; i < routers.length; i++) {
6028
+ const router = routers[i];
6029
+ for (let j = router.path.length - 1; j >= 0; j--) {
6030
+ const path = router.path[j];
6031
+ const flashSwapResult = dex.flash_swap(this, txb, path, false);
6032
+ returnCoins.unshift(flashSwapResult.targetCoin);
6033
+ receipts.unshift(flashSwapResult.flashReceipt);
6034
+ }
6035
+ let nextRepayCoin = inputCoin;
6036
+ for (let j = 0; j < router.path.length; j++) {
6037
+ const path = router.path[j];
6038
+ const repayResult = dex.repay_flash_swap(
6039
+ this,
6040
+ txb,
6041
+ path,
6042
+ nextRepayCoin,
6043
+ receipts[j]
6044
+ );
6045
+ nextRepayCoin = returnCoins[j];
6046
+ if (j === 0) {
6047
+ inputCoin = repayResult;
6048
+ } else {
6049
+ this.transferOrDestoryCoin(txb, repayResult, path.from);
6566
6050
  }
6567
- };
6051
+ if (j === router.path.length - 1) {
6052
+ targetCoins.push(nextRepayCoin);
6053
+ }
6054
+ }
6568
6055
  }
6569
- const data = yield response.json();
6570
- if (data.data != null) {
6571
- const res = parseRouterResponse(data.data);
6572
- return res;
6056
+ const inputCoinType = routers[0].path[0].from;
6057
+ this.transferOrDestoryCoin(txb, inputCoin, inputCoinType);
6058
+ if (targetCoins.length > 1) {
6059
+ const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) });
6060
+ txb.moveCall({
6061
+ target: `${utils.SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
6062
+ typeArguments: [routers[0].path[routers[0].path.length - 1].target],
6063
+ arguments: [targetCoins[0], vec]
6064
+ });
6573
6065
  }
6574
- const insufficientLiquidity = data.msg === "liquidity is not enough";
6575
- return {
6576
- amountIn: ZERO,
6577
- amountOut: ZERO,
6578
- routes: [],
6579
- insufficientLiquidity,
6580
- error: {
6581
- code: 10003 /* InsufficientLiquidity */,
6582
- msg: getAggregatorServerErrorMessage(
6583
- 10003 /* InsufficientLiquidity */
6584
- )
6585
- }
6586
- };
6066
+ return targetCoins[0];
6587
6067
  });
6588
6068
  }
6589
6069
  swapInPools(params) {
6590
6070
  return __async(this, null, function* () {
6591
6071
  let result;
6592
6072
  try {
6593
- result = yield swapInPools(this.client, params, this.config);
6073
+ result = yield swapInPools(this.client, params, this.signer);
6594
6074
  } catch (e) {
6595
6075
  console.error("swapInPools error:", e);
6596
6076
  return null;
@@ -6599,100 +6079,168 @@ var AggregatorClient = class {
6599
6079
  });
6600
6080
  }
6601
6081
  routerSwap(params) {
6082
+ return __async(this, null, function* () {
6083
+ const { routers, inputCoin, slippage, byAmountIn, txb, partner } = params;
6084
+ const amountIn = routers.reduce(
6085
+ (acc, router) => acc.add(router.amountIn),
6086
+ new import_bn5.default(0)
6087
+ );
6088
+ const amountOut = routers.reduce(
6089
+ (acc, router) => acc.add(router.amountOut),
6090
+ new import_bn5.default(0)
6091
+ );
6092
+ const amountLimit = CalculateAmountLimit(
6093
+ byAmountIn ? amountOut : amountIn,
6094
+ byAmountIn,
6095
+ slippage
6096
+ );
6097
+ if (byAmountIn) {
6098
+ const targetCoin2 = yield this.expectInputSwap(
6099
+ txb,
6100
+ inputCoin,
6101
+ routers,
6102
+ new import_bn5.default(amountLimit),
6103
+ partner
6104
+ );
6105
+ return targetCoin2;
6106
+ }
6107
+ const splitedInputCoins = txb.splitCoins(inputCoin, [
6108
+ amountLimit.toString()
6109
+ ]);
6110
+ this.transferOrDestoryCoin(txb, inputCoin, routers[0].path[0].from);
6111
+ const targetCoin = yield this.expectOutputSwap(
6112
+ txb,
6113
+ splitedInputCoins[0],
6114
+ routers,
6115
+ partner
6116
+ );
6117
+ return targetCoin;
6118
+ });
6119
+ }
6120
+ // auto build input coin
6121
+ // auto merge, transfer or destory target coin.
6122
+ fastRouterSwap(params) {
6602
6123
  return __async(this, null, function* () {
6603
6124
  const {
6604
- routers: _,
6605
- amountIn,
6606
- amountOut,
6125
+ routers,
6607
6126
  byAmountIn,
6608
6127
  slippage,
6609
- fromCoinType,
6610
- targetCoinType,
6128
+ txb,
6611
6129
  partner,
6612
6130
  isMergeTragetCoin,
6613
6131
  refreshAllCoins
6614
6132
  } = params;
6133
+ if (refreshAllCoins || this.allCoins.length === 0) {
6134
+ this.allCoins = yield this.getAllCoins();
6135
+ }
6136
+ const fromCoinType = routers[0].path[0].from;
6137
+ const targetCoinType = routers[0].path[routers[0].path.length - 1].target;
6138
+ const amountIn = routers.reduce(
6139
+ (acc, router) => acc.add(router.amountIn),
6140
+ new import_bn5.default(0)
6141
+ );
6142
+ const amountOut = routers.reduce(
6143
+ (acc, router) => acc.add(router.amountOut),
6144
+ new import_bn5.default(0)
6145
+ );
6615
6146
  const amountLimit = CalculateAmountLimit(
6616
6147
  byAmountIn ? amountOut : amountIn,
6617
6148
  byAmountIn,
6618
6149
  slippage
6619
6150
  );
6620
- const txb = new transactions.Transaction();
6621
- if (refreshAllCoins || this.allCoins.length === 0) {
6622
- this.allCoins = yield this.getAllCoins();
6623
- }
6624
- const allCoins = this.allCoins;
6625
- let targetCoins = [];
6626
- if (byAmountIn) {
6627
- const buildFromCoinRes = buildInputCoin(
6628
- txb,
6629
- allCoins,
6630
- BigInt(amountIn.toString()),
6631
- fromCoinType
6632
- );
6633
- const fromCoin = buildFromCoinRes.targetCoin;
6634
- const swapedCoins = yield expectInputRouterSwap(
6635
- this.client,
6636
- params,
6637
- txb,
6638
- fromCoin,
6639
- this.config,
6640
- partner
6641
- );
6642
- const mergedCoin = checkCoinThresholdAndMergeCoin(
6643
- txb,
6644
- swapedCoins,
6645
- targetCoinType,
6646
- amountLimit,
6647
- this.config
6648
- );
6649
- targetCoins.push(mergedCoin);
6650
- } else {
6651
- const fromCoin = buildInputCoin(
6652
- txb,
6653
- allCoins,
6654
- BigInt(amountLimit),
6655
- fromCoinType
6656
- ).targetCoin;
6657
- const swapedCoins = yield expectOutputRouterSwap(
6658
- params,
6659
- txb,
6660
- fromCoin,
6661
- this.config,
6662
- partner
6663
- );
6664
- targetCoins.push(...swapedCoins);
6665
- }
6151
+ const amount = byAmountIn ? amountIn : amountLimit;
6152
+ const buildFromCoinRes = buildInputCoin(
6153
+ txb,
6154
+ this.allCoins,
6155
+ BigInt(amount.toString()),
6156
+ fromCoinType
6157
+ );
6158
+ const targetCoin = yield this.routerSwap({
6159
+ routers,
6160
+ inputCoin: buildFromCoinRes.targetCoin,
6161
+ slippage,
6162
+ byAmountIn,
6163
+ txb,
6164
+ partner
6165
+ });
6666
6166
  if (isMergeTragetCoin) {
6667
6167
  const targetCoinRes = buildInputCoin(
6668
6168
  txb,
6669
- allCoins,
6169
+ this.allCoins,
6670
6170
  BigInt(0),
6671
6171
  targetCoinType
6672
6172
  );
6673
- txb.mergeCoins(targetCoinRes.targetCoin, targetCoins);
6173
+ txb.mergeCoins(targetCoinRes.targetCoin, [targetCoin]);
6674
6174
  if (targetCoinRes.isMintZeroCoin) {
6675
- transferOrDestoryCoin(
6175
+ this.transferOrDestoryCoin(
6676
6176
  txb,
6677
6177
  targetCoinRes.targetCoin,
6678
- targetCoinType,
6679
- this.config
6178
+ targetCoinType
6680
6179
  );
6681
6180
  }
6682
6181
  } else {
6683
- if (targetCoins.length > 1) {
6684
- const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) });
6685
- txb.moveCall({
6686
- target: createTarget(utils.SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
6687
- typeArguments: [targetCoinType],
6688
- arguments: [targetCoins[0], vec]
6689
- });
6690
- }
6691
- transferOrDestoryCoin(txb, targetCoins[0], targetCoinType, this.config);
6182
+ this.transferOrDestoryCoin(txb, targetCoin, targetCoinType);
6692
6183
  }
6693
- return txb;
6694
6184
  });
6695
6185
  }
6186
+ publishedAt() {
6187
+ if (this.env === 0 /* Mainnet */) {
6188
+ return "0x9fd702cf1a8db5a2e99be20be6750d22ec75c30d57e3d9ccbc37c9b4ad7bef21";
6189
+ } else {
6190
+ return "0x0";
6191
+ }
6192
+ }
6193
+ transferOrDestoryCoin(txb, coin, coinType) {
6194
+ txb.moveCall({
6195
+ target: `${this.publishedAt()}::utils::transfer_or_destroy_coin`,
6196
+ typeArguments: [coinType],
6197
+ arguments: [coin]
6198
+ });
6199
+ }
6200
+ checkCoinThresholdAndMergeCoin(txb, coins, coinType, amountLimit) {
6201
+ let targetCoin = coins[0];
6202
+ if (coins.length > 1) {
6203
+ let vec = txb.makeMoveVec({ elements: coins.slice(1) });
6204
+ txb.moveCall({
6205
+ target: `${utils.SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
6206
+ typeArguments: [coinType],
6207
+ arguments: [coins[0], vec]
6208
+ });
6209
+ targetCoin = coins[0];
6210
+ }
6211
+ txb.moveCall({
6212
+ target: `${this.publishedAt()}::utils::check_coin_threshold`,
6213
+ typeArguments: [coinType],
6214
+ arguments: [targetCoin, txb.pure.u64(amountLimit.toString())]
6215
+ });
6216
+ return targetCoin;
6217
+ }
6218
+ newDex(provider, partner) {
6219
+ switch (provider) {
6220
+ case CETUS:
6221
+ return new Cetus(this.env, partner);
6222
+ case DEEPBOOKV2:
6223
+ return new DeepbookV2(this.env);
6224
+ case KRIYA:
6225
+ return new KriyaV2(this.env);
6226
+ case KRIYAV3:
6227
+ return new KriyaV3(this.env);
6228
+ case FLOWXV2:
6229
+ return new FlowxV2(this.env);
6230
+ case TURBOS:
6231
+ return new Turbos(this.env);
6232
+ case AFTERMATH:
6233
+ return new Aftermath(this.env);
6234
+ case HAEDAL:
6235
+ return new Haedal(this.env);
6236
+ case AFSUI:
6237
+ return new Afsui(this.env);
6238
+ case VOLO:
6239
+ return new Volo(this.env);
6240
+ default:
6241
+ throw new Error(`Unsupported dex ${provider}`);
6242
+ }
6243
+ }
6696
6244
  signAndExecuteTransaction(txb, signer) {
6697
6245
  return __async(this, null, function* () {
6698
6246
  const res = yield this.client.signAndExecuteTransaction({
@@ -6712,7 +6260,7 @@ var AggregatorClient = class {
6712
6260
  return __async(this, null, function* () {
6713
6261
  const res = yield this.client.devInspectTransactionBlock({
6714
6262
  transactionBlock: txb,
6715
- sender: this.wallet
6263
+ sender: this.signer
6716
6264
  });
6717
6265
  return res;
6718
6266
  });
@@ -6729,19 +6277,19 @@ var AggregatorClient = class {
6729
6277
  };
6730
6278
  function parseRouterResponse(data) {
6731
6279
  return {
6732
- amountIn: new import_bn4.default(data.amount_in.toString()),
6733
- amountOut: new import_bn4.default(data.amount_out.toString()),
6280
+ amountIn: new import_bn5.default(data.amount_in.toString()),
6281
+ amountOut: new import_bn5.default(data.amount_out.toString()),
6734
6282
  insufficientLiquidity: false,
6735
6283
  routes: data.routes.map((route) => {
6736
6284
  return {
6737
6285
  path: route.path.map((path) => {
6738
6286
  var _a, _b;
6739
6287
  let version;
6740
- if (path.provider === AFTERMATH_AMM) {
6288
+ if (path.provider === AFTERMATH) {
6741
6289
  version = path.extended_details.aftermath_pool_flatness === 0 ? "v2" : "v3";
6742
6290
  }
6743
6291
  let extendedDetails;
6744
- if (path.provider === TURBOS_DEX || path.provider === AFTERMATH_AMM) {
6292
+ if (path.provider === TURBOS || path.provider === AFTERMATH) {
6745
6293
  extendedDetails = {
6746
6294
  aftermathLpSupplyType: (_a = path.extended_details) == null ? void 0 : _a.aftermath_lp_supply_type,
6747
6295
  turbosFeeType: (_b = path.extended_details) == null ? void 0 : _b.turbos_fee_type
@@ -6749,7 +6297,7 @@ function parseRouterResponse(data) {
6749
6297
  }
6750
6298
  return {
6751
6299
  id: path.id,
6752
- a2b: path.direction,
6300
+ direction: path.direction,
6753
6301
  provider: path.provider,
6754
6302
  from: path.from,
6755
6303
  target: path.target,
@@ -6760,13 +6308,93 @@ function parseRouterResponse(data) {
6760
6308
  version
6761
6309
  };
6762
6310
  }),
6763
- amountIn: new import_bn4.default(route.amount_in.toString()),
6764
- amountOut: new import_bn4.default(route.amount_out.toString()),
6311
+ amountIn: new import_bn5.default(route.amount_in.toString()),
6312
+ amountOut: new import_bn5.default(route.amount_out.toString()),
6765
6313
  initialPrice: new decimal_default(route.initial_price.toString())
6766
6314
  };
6767
6315
  })
6768
6316
  };
6769
6317
  }
6318
+
6319
+ // src/transaction/index.ts
6320
+ var CLOCK_ADDRESS = "0x0000000000000000000000000000000000000000000000000000000000000006";
6321
+
6322
+ // src/api.ts
6323
+ function getRouterResult(endpoint, params) {
6324
+ return __async(this, null, function* () {
6325
+ const {
6326
+ from,
6327
+ target,
6328
+ amount,
6329
+ byAmountIn,
6330
+ depth,
6331
+ splitAlgorithm,
6332
+ splitFactor,
6333
+ splitCount,
6334
+ providers
6335
+ } = params;
6336
+ const fromCoin = completionCoin(from);
6337
+ const targetCoin = completionCoin(target);
6338
+ let url = `${endpoint}?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`;
6339
+ if (depth) {
6340
+ url += `&depth=${depth}`;
6341
+ }
6342
+ if (splitAlgorithm) {
6343
+ url += `&split_algorithm=${splitAlgorithm}`;
6344
+ }
6345
+ if (splitFactor) {
6346
+ url += `&split_factor=${splitFactor}`;
6347
+ }
6348
+ if (splitCount) {
6349
+ url += `&split_count=${splitCount}`;
6350
+ }
6351
+ if (providers) {
6352
+ if (providers.length > 0) {
6353
+ url += `&providers=${providers.join(",")}`;
6354
+ }
6355
+ }
6356
+ const response = yield fetch(url);
6357
+ if (!response.ok) {
6358
+ return {
6359
+ amountIn: ZERO,
6360
+ amountOut: ZERO,
6361
+ routes: [],
6362
+ insufficientLiquidity: false,
6363
+ error: {
6364
+ code: 10001 /* NumberTooLarge */,
6365
+ msg: getAggregatorServerErrorMessage(
6366
+ 10001 /* NumberTooLarge */
6367
+ )
6368
+ }
6369
+ };
6370
+ }
6371
+ const data = yield response.json();
6372
+ if (data.data != null) {
6373
+ const res = parseRouterResponse(data.data);
6374
+ return res;
6375
+ }
6376
+ const insufficientLiquidity = data.msg === "liquidity is not enough";
6377
+ return {
6378
+ amountIn: ZERO,
6379
+ amountOut: ZERO,
6380
+ routes: [],
6381
+ insufficientLiquidity,
6382
+ error: {
6383
+ code: 10003 /* InsufficientLiquidity */,
6384
+ msg: getAggregatorServerErrorMessage(
6385
+ 10003 /* InsufficientLiquidity */
6386
+ )
6387
+ }
6388
+ };
6389
+ });
6390
+ }
6391
+
6392
+ // src/index.ts
6393
+ var Env = /* @__PURE__ */ ((Env2) => {
6394
+ Env2[Env2["Mainnet"] = 0] = "Mainnet";
6395
+ Env2[Env2["Testnet"] = 1] = "Testnet";
6396
+ return Env2;
6397
+ })(Env || {});
6770
6398
  /*! Bundled license information:
6771
6399
 
6772
6400
  decimal.js/decimal.mjs:
@@ -6779,82 +6407,32 @@ decimal.js/decimal.mjs:
6779
6407
  *)
6780
6408
  */
6781
6409
 
6782
- exports.AFTERMATH_AMM = AFTERMATH_AMM;
6783
- exports.AFTERMATH_MODULE = AFTERMATH_MODULE;
6784
- exports.AGGREGATOR = AGGREGATOR;
6785
- exports.AggregatorClient = AggregatorClient;
6786
- exports.AggregatorConfig = AggregatorConfig;
6787
- exports.CETUS_DEX = CETUS_DEX;
6788
- exports.CETUS_MODULE = CETUS_MODULE;
6789
- exports.CETUS_PUBLISHED_AT = CETUS_PUBLISHED_AT;
6790
- exports.CHECK_COINS_THRESHOLD_FUNC = CHECK_COINS_THRESHOLD_FUNC;
6410
+ exports.AFSUI = AFSUI;
6411
+ exports.AFTERMATH = AFTERMATH;
6412
+ exports.AggregatorClient = AggregatorClient5;
6413
+ exports.CETUS = CETUS;
6791
6414
  exports.CLOCK_ADDRESS = CLOCK_ADDRESS;
6792
- exports.CoinInfoAddress = CoinInfoAddress;
6793
- exports.CoinStoreAddress = CoinStoreAddress;
6794
- exports.DEEPBOOK_CLOB_V2_MODULE = DEEPBOOK_CLOB_V2_MODULE;
6795
- exports.DEEPBOOK_CUSTODIAN_V2_MODULE = DEEPBOOK_CUSTODIAN_V2_MODULE;
6796
- exports.DEEPBOOK_DEX = DEEPBOOK_DEX;
6797
- exports.DEEPBOOK_MODULE = DEEPBOOK_MODULE;
6798
- exports.DEEPBOOK_PACKAGE_ID = DEEPBOOK_PACKAGE_ID;
6799
- exports.DEEPBOOK_PUBLISHED_AT = DEEPBOOK_PUBLISHED_AT;
6800
- exports.ENV = ENV;
6801
- exports.FLOWX_AMM = FLOWX_AMM;
6802
- exports.FLOWX_AMM_MODULE = FLOWX_AMM_MODULE;
6803
- exports.FlashSwapA2BFunc = FlashSwapA2BFunc;
6804
- exports.FlashSwapB2AFunc = FlashSwapB2AFunc;
6805
- exports.FlashSwapFunc = FlashSwapFunc;
6806
- exports.FlashSwapWithPartnerA2BFunc = FlashSwapWithPartnerA2BFunc;
6807
- exports.FlashSwapWithPartnerB2AFunc = FlashSwapWithPartnerB2AFunc;
6808
- exports.FlashSwapWithPartnerFunc = FlashSwapWithPartnerFunc;
6809
- exports.INTEGRATE = INTEGRATE;
6810
- exports.JOIN_FUNC = JOIN_FUNC;
6811
- exports.KRIYA_DEX = KRIYA_DEX;
6812
- exports.KRIYA_MODULE = KRIYA_MODULE;
6813
- exports.MAINNET_AFTERMATH_INSURANCE_FUND_ID = MAINNET_AFTERMATH_INSURANCE_FUND_ID;
6814
- exports.MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID;
6815
- exports.MAINNET_AFTERMATH_REFERRAL_VAULT_ID = MAINNET_AFTERMATH_REFERRAL_VAULT_ID;
6816
- exports.MAINNET_AFTERMATH_REGISTRY_ID = MAINNET_AFTERMATH_REGISTRY_ID;
6817
- exports.MAINNET_AFTERMATH_TREASURY_ID = MAINNET_AFTERMATH_TREASURY_ID;
6818
- exports.MAINNET_CETUS_GLOBAL_CONFIG_ID = MAINNET_CETUS_GLOBAL_CONFIG_ID;
6819
- exports.MAINNET_FLOWX_AMM_CONTAINER_ID = MAINNET_FLOWX_AMM_CONTAINER_ID;
6415
+ exports.DEEPBOOKV2 = DEEPBOOKV2;
6416
+ exports.Env = Env;
6417
+ exports.FLOWXV2 = FLOWXV2;
6418
+ exports.HAEDAL = HAEDAL;
6419
+ exports.KRIYA = KRIYA;
6420
+ exports.KRIYAV3 = KRIYAV3;
6820
6421
  exports.ONE = ONE;
6821
- exports.PAY_MODULE = PAY_MODULE;
6822
- exports.POOL_MODULT = POOL_MODULT;
6823
- exports.REPAY_FLASH_SWAP_A2B_FUNC = REPAY_FLASH_SWAP_A2B_FUNC;
6824
- exports.REPAY_FLASH_SWAP_B2A_FUNC = REPAY_FLASH_SWAP_B2A_FUNC;
6825
- exports.REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC = REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC;
6826
- exports.REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC = REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC;
6827
- exports.RepayFalshSwapFunc = RepayFalshSwapFunc;
6828
- exports.RepayFlashSwapWithPartnerFunc = RepayFlashSwapWithPartnerFunc;
6829
- exports.SWAP_A2B_FUNC = SWAP_A2B_FUNC;
6830
- exports.SWAP_B2A_FUNC = SWAP_B2A_FUNC;
6831
- exports.SuiZeroCoinFn = SuiZeroCoinFn;
6832
6422
  exports.TEN_POW_NINE = TEN_POW_NINE;
6833
- exports.TESTNET_AFTERMATH_INSURANCE_FUND_ID = TESTNET_AFTERMATH_INSURANCE_FUND_ID;
6834
- exports.TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID;
6835
- exports.TESTNET_AFTERMATH_REFERRAL_VAULT_ID = TESTNET_AFTERMATH_REFERRAL_VAULT_ID;
6836
- exports.TESTNET_AFTERMATH_REGISTRY_ID = TESTNET_AFTERMATH_REGISTRY_ID;
6837
- exports.TESTNET_AFTERMATH_TREASURY_ID = TESTNET_AFTERMATH_TREASURY_ID;
6838
- exports.TESTNET_CETUS_GLOBAL_CONFIG_ID = TESTNET_CETUS_GLOBAL_CONFIG_ID;
6839
- exports.TESTNET_FLOWX_AMM_CONTAINER_ID = TESTNET_FLOWX_AMM_CONTAINER_ID;
6840
- exports.TRANSFER_ACCOUNT_CAP = TRANSFER_ACCOUNT_CAP;
6841
- exports.TRANSFER_OR_DESTORY_COIN_FUNC = TRANSFER_OR_DESTORY_COIN_FUNC;
6842
- exports.TURBOS_DEX = TURBOS_DEX;
6843
- exports.TURBOS_MODULE = TURBOS_MODULE;
6844
- exports.TURBOS_VERSIONED = TURBOS_VERSIONED;
6423
+ exports.TURBOS = TURBOS;
6845
6424
  exports.TWO = TWO;
6846
6425
  exports.U128 = U128;
6847
6426
  exports.U64_MAX = U64_MAX;
6848
6427
  exports.U64_MAX_BN = U64_MAX_BN;
6849
- exports.UTILS_MODULE = UTILS_MODULE;
6428
+ exports.VOLO = VOLO;
6850
6429
  exports.ZERO = ZERO;
6851
6430
  exports.composeType = composeType;
6852
6431
  exports.createTarget = createTarget;
6853
- exports.expectInputRouterSwap = expectInputRouterSwap;
6854
- exports.expectOutputRouterSwap = expectOutputRouterSwap;
6855
6432
  exports.extractAddressFromType = extractAddressFromType;
6856
6433
  exports.extractStructTagFromType = extractStructTagFromType;
6857
6434
  exports.fixSuiObjectId = fixSuiObjectId;
6435
+ exports.getRouterResult = getRouterResult;
6858
6436
  exports.isSortedSymbols = isSortedSymbols;
6859
6437
  exports.normalizeCoinType = normalizeCoinType;
6860
6438
  exports.parseRouterResponse = parseRouterResponse;