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