@magmaprotocol/magma-clmm-sdk 0.5.39 → 0.5.40

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.
package/dist/index.mjs CHANGED
@@ -37,7 +37,7 @@ var CachedContent = class {
37
37
  };
38
38
 
39
39
  // src/utils/common.ts
40
- import BN11 from "bn.js";
40
+ import BN13 from "bn.js";
41
41
  import { fromB64, fromHEX } from "@mysten/bcs";
42
42
  import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
43
43
  import { Secp256k1Keypair } from "@mysten/sui/keypairs/secp256k1";
@@ -1592,8 +1592,660 @@ function collectFeesQuote(param) {
1592
1592
  return updateFees(param.position, fee_growth_inside_a, fee_growth_inside_b);
1593
1593
  }
1594
1594
 
1595
- // src/math/percentage.ts
1595
+ // src/math/dlmmStrategy.ts
1596
+ import BN10 from "bn.js";
1597
+
1598
+ // src/math/dlmmWeightToAmounts.ts
1596
1599
  import BN9 from "bn.js";
1600
+ import Decimal3 from "decimal.js";
1601
+ import { get_price_from_real_id } from "@magmaprotocol/calc_dlmm";
1602
+ function getPriceOfBinByBinId(binId, binStep) {
1603
+ const twoDec = new Decimal3(2);
1604
+ const price = new Decimal3(get_price_from_real_id(binId, binStep));
1605
+ return price.div(twoDec.pow(128));
1606
+ }
1607
+ function autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, distributions) {
1608
+ const activeBins = distributions.filter((element) => {
1609
+ return element.binId === activeId;
1610
+ });
1611
+ if (activeBins.length === 1) {
1612
+ const p0 = getPriceOfBinByBinId(activeId, binStep);
1613
+ let wx0 = new Decimal3(0);
1614
+ let wy0 = new Decimal3(0);
1615
+ const activeBin = activeBins[0];
1616
+ if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
1617
+ wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
1618
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
1619
+ } else {
1620
+ const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
1621
+ const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
1622
+ if (!amountXInActiveBin.isZero()) {
1623
+ wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
1624
+ }
1625
+ if (!amountYInActiveBin.isZero()) {
1626
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
1627
+ }
1628
+ }
1629
+ let totalWeightX2 = wx0;
1630
+ let totalWeightY2 = wy0;
1631
+ distributions.forEach((element) => {
1632
+ if (element.binId < activeId) {
1633
+ totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
1634
+ }
1635
+ if (element.binId > activeId) {
1636
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1637
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1638
+ totalWeightX2 = totalWeightX2.add(weighPerPrice);
1639
+ }
1640
+ });
1641
+ const kx2 = totalWeightX2.isZero() ? new Decimal3(1) : new Decimal3(amountX.toString()).div(totalWeightX2);
1642
+ const amountY2 = kx2.mul(totalWeightY2);
1643
+ return new BN9(amountY2.floor().toString());
1644
+ }
1645
+ let totalWeightX = new Decimal3(0);
1646
+ let totalWeightY = new Decimal3(0);
1647
+ distributions.forEach((element) => {
1648
+ if (element.binId < activeId) {
1649
+ totalWeightY = totalWeightY.add(new Decimal3(element.weight));
1650
+ } else {
1651
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1652
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1653
+ totalWeightX = totalWeightX.add(weighPerPrice);
1654
+ }
1655
+ });
1656
+ const kx = totalWeightX.isZero() ? new Decimal3(1) : new Decimal3(amountX.toString()).div(totalWeightX);
1657
+ const amountY = kx.mul(totalWeightY);
1658
+ return new BN9(amountY.floor().toString());
1659
+ }
1660
+ function autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, distributions) {
1661
+ const activeBins = distributions.filter((element) => {
1662
+ return element.binId === activeId;
1663
+ });
1664
+ if (activeBins.length === 1) {
1665
+ const p0 = getPriceOfBinByBinId(activeId, binStep);
1666
+ let wx0 = new Decimal3(0);
1667
+ let wy0 = new Decimal3(0);
1668
+ const activeBin = activeBins[0];
1669
+ if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
1670
+ wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
1671
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
1672
+ } else {
1673
+ const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
1674
+ const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
1675
+ if (!amountXInActiveBin.isZero()) {
1676
+ wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
1677
+ }
1678
+ if (!amountYInActiveBin.isZero()) {
1679
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
1680
+ }
1681
+ }
1682
+ let totalWeightX2 = wx0;
1683
+ let totalWeightY2 = wy0;
1684
+ distributions.forEach((element) => {
1685
+ if (element.binId < activeId) {
1686
+ totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
1687
+ }
1688
+ if (element.binId > activeId) {
1689
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1690
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1691
+ totalWeightX2 = totalWeightX2.add(weighPerPrice);
1692
+ }
1693
+ });
1694
+ const ky2 = totalWeightY2.isZero() ? new Decimal3(1) : new Decimal3(amountY.toString()).div(totalWeightY2);
1695
+ const amountX2 = ky2.mul(totalWeightX2);
1696
+ return new BN9(amountX2.floor().toString());
1697
+ }
1698
+ let totalWeightX = new Decimal3(0);
1699
+ let totalWeightY = new Decimal3(0);
1700
+ distributions.forEach((element) => {
1701
+ if (element.binId < activeId) {
1702
+ totalWeightY = totalWeightY.add(new Decimal3(element.weight));
1703
+ } else {
1704
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1705
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1706
+ totalWeightX = totalWeightX.add(weighPerPrice);
1707
+ }
1708
+ });
1709
+ const ky = totalWeightY.isZero() ? new Decimal3(1) : new Decimal3(amountY.toString()).div(totalWeightY);
1710
+ const amountX = ky.mul(totalWeightX);
1711
+ return new BN9(amountX.floor().toString());
1712
+ }
1713
+ function toAmountBidSide(activeId, totalAmount, distributions) {
1714
+ const totalWeight = distributions.reduce((sum, el) => {
1715
+ return el.binId > activeId ? sum : sum.add(el.weight);
1716
+ }, new Decimal3(0));
1717
+ if (totalWeight.cmp(new Decimal3(0)) !== 1) {
1718
+ throw Error("Invalid parameteres");
1719
+ }
1720
+ return distributions.map((bin) => {
1721
+ if (bin.binId > activeId) {
1722
+ return {
1723
+ binId: bin.binId,
1724
+ amount: new BN9(0)
1725
+ };
1726
+ }
1727
+ return {
1728
+ binId: bin.binId,
1729
+ amount: new BN9(new Decimal3(totalAmount.toString()).mul(new Decimal3(bin.weight).div(totalWeight)).floor().toString())
1730
+ };
1731
+ });
1732
+ }
1733
+ function toAmountAskSide(activeId, binStep, totalAmount, distributions) {
1734
+ const totalWeight = distributions.reduce((sum, el) => {
1735
+ if (el.binId < activeId) {
1736
+ return sum;
1737
+ }
1738
+ const price = getPriceOfBinByBinId(el.binId, binStep);
1739
+ const weightPerPrice = new Decimal3(el.weight).div(price);
1740
+ return sum.add(weightPerPrice);
1741
+ }, new Decimal3(0));
1742
+ if (totalWeight.cmp(new Decimal3(0)) !== 1) {
1743
+ throw Error("Invalid parameteres");
1744
+ }
1745
+ return distributions.map((bin) => {
1746
+ if (bin.binId < activeId) {
1747
+ return {
1748
+ binId: bin.binId,
1749
+ amount: new BN9(0)
1750
+ };
1751
+ }
1752
+ const price = getPriceOfBinByBinId(bin.binId, binStep);
1753
+ const weightPerPrice = new Decimal3(bin.weight).div(price);
1754
+ return {
1755
+ binId: bin.binId,
1756
+ amount: new BN9(new Decimal3(totalAmount.toString()).mul(weightPerPrice).div(totalWeight).floor().toString())
1757
+ };
1758
+ });
1759
+ }
1760
+ function toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, distributions) {
1761
+ if (activeId > distributions[distributions.length - 1].binId) {
1762
+ const amounts = toAmountBidSide(activeId, amountY, distributions);
1763
+ return amounts.map((bin) => {
1764
+ return {
1765
+ binId: bin.binId,
1766
+ amountX: new BN9(0),
1767
+ amountY: bin.amount
1768
+ };
1769
+ });
1770
+ }
1771
+ if (activeId < distributions[0].binId) {
1772
+ const amounts = toAmountAskSide(activeId, binStep, amountX, distributions);
1773
+ return amounts.map((bin) => {
1774
+ return {
1775
+ binId: bin.binId,
1776
+ amountX: bin.amount,
1777
+ amountY: new BN9(0)
1778
+ };
1779
+ });
1780
+ }
1781
+ const activeBins = distributions.filter((element) => {
1782
+ return element.binId === activeId;
1783
+ });
1784
+ if (activeBins.length === 1) {
1785
+ const p0 = getPriceOfBinByBinId(activeId, binStep);
1786
+ let wx0 = new Decimal3(0);
1787
+ let wy0 = new Decimal3(0);
1788
+ const activeBin = activeBins[0];
1789
+ if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
1790
+ wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
1791
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
1792
+ } else {
1793
+ const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
1794
+ const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
1795
+ if (!amountXInActiveBin.isZero()) {
1796
+ wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
1797
+ }
1798
+ if (!amountYInActiveBin.isZero()) {
1799
+ wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
1800
+ }
1801
+ }
1802
+ let totalWeightX2 = wx0;
1803
+ let totalWeightY2 = wy0;
1804
+ distributions.forEach((element) => {
1805
+ if (element.binId < activeId) {
1806
+ totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
1807
+ }
1808
+ if (element.binId > activeId) {
1809
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1810
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1811
+ totalWeightX2 = totalWeightX2.add(weighPerPrice);
1812
+ }
1813
+ });
1814
+ const kx2 = new Decimal3(amountX.toString()).div(totalWeightX2);
1815
+ const ky2 = new Decimal3(amountY.toString()).div(totalWeightY2);
1816
+ const k2 = kx2.lessThan(ky2) ? kx2 : ky2;
1817
+ return distributions.map((bin) => {
1818
+ if (bin.binId < activeId) {
1819
+ const amount = k2.mul(new Decimal3(bin.weight));
1820
+ return {
1821
+ binId: bin.binId,
1822
+ amountX: new BN9(0),
1823
+ amountY: new BN9(amount.floor().toString())
1824
+ };
1825
+ }
1826
+ if (bin.binId > activeId) {
1827
+ const price = getPriceOfBinByBinId(bin.binId, binStep);
1828
+ const weighPerPrice = new Decimal3(bin.weight).div(price);
1829
+ const amount = k2.mul(weighPerPrice);
1830
+ return {
1831
+ binId: bin.binId,
1832
+ amountX: new BN9(amount.floor().toString()),
1833
+ amountY: new BN9(0)
1834
+ };
1835
+ }
1836
+ const amountXActiveBin = k2.mul(wx0);
1837
+ const amountYActiveBin = k2.mul(wy0);
1838
+ return {
1839
+ binId: bin.binId,
1840
+ amountX: new BN9(amountXActiveBin.floor().toString()),
1841
+ amountY: new BN9(amountYActiveBin.floor().toString())
1842
+ };
1843
+ });
1844
+ }
1845
+ let totalWeightX = new Decimal3(0);
1846
+ let totalWeightY = new Decimal3(0);
1847
+ distributions.forEach((element) => {
1848
+ if (element.binId < activeId) {
1849
+ totalWeightY = totalWeightY.add(new Decimal3(element.weight));
1850
+ } else {
1851
+ const price = getPriceOfBinByBinId(element.binId, binStep);
1852
+ const weighPerPrice = new Decimal3(element.weight).div(price);
1853
+ totalWeightX = totalWeightX.add(weighPerPrice);
1854
+ }
1855
+ });
1856
+ const kx = new Decimal3(amountX.toString()).div(totalWeightX);
1857
+ const ky = new Decimal3(amountY.toString()).div(totalWeightY);
1858
+ const k = kx.lessThan(ky) ? kx : ky;
1859
+ return distributions.map((bin) => {
1860
+ if (bin.binId < activeId) {
1861
+ const amount2 = k.mul(new Decimal3(bin.weight));
1862
+ return {
1863
+ binId: bin.binId,
1864
+ amountX: new BN9(0),
1865
+ amountY: new BN9(amount2.floor().toString())
1866
+ };
1867
+ }
1868
+ const price = getPriceOfBinByBinId(bin.binId, binStep);
1869
+ const weighPerPrice = new Decimal3(bin.weight).div(price);
1870
+ const amount = k.mul(weighPerPrice);
1871
+ return {
1872
+ binId: bin.binId,
1873
+ amountX: new BN9(amount.floor().toString()),
1874
+ amountY: new BN9(0)
1875
+ };
1876
+ });
1877
+ }
1878
+
1879
+ // src/math/dlmmStrategy.ts
1880
+ var StrategyType = /* @__PURE__ */ ((StrategyType2) => {
1881
+ StrategyType2[StrategyType2["Spot"] = 0] = "Spot";
1882
+ StrategyType2[StrategyType2["Curve"] = 1] = "Curve";
1883
+ StrategyType2[StrategyType2["BidAsk"] = 2] = "BidAsk";
1884
+ return StrategyType2;
1885
+ })(StrategyType || {});
1886
+ function toWeightDecendingOrder(minBinId, maxBinId) {
1887
+ const distributions = [];
1888
+ for (let i = minBinId; i <= maxBinId; i++) {
1889
+ distributions.push({
1890
+ binId: i,
1891
+ weight: maxBinId - i + 1
1892
+ });
1893
+ }
1894
+ return distributions;
1895
+ }
1896
+ function toWeightAscendingOrder(minBinId, maxBinId) {
1897
+ const distributions = [];
1898
+ for (let i = minBinId; i <= maxBinId; i++) {
1899
+ distributions.push({
1900
+ binId: i,
1901
+ weight: i - minBinId + 1
1902
+ });
1903
+ }
1904
+ return distributions;
1905
+ }
1906
+ function toWeightSpotBalanced(minBinId, maxBinId) {
1907
+ const distributions = [];
1908
+ for (let i = minBinId; i <= maxBinId; i++) {
1909
+ distributions.push({
1910
+ binId: i,
1911
+ weight: 1
1912
+ });
1913
+ }
1914
+ return distributions;
1915
+ }
1916
+ var DEFAULT_MAX_WEIGHT = 2e3;
1917
+ var DEFAULT_MIN_WEIGHT = 200;
1918
+ function toWeightCurve(minBinId, maxBinId, activeId) {
1919
+ if (activeId < minBinId || activeId > maxBinId) {
1920
+ throw new ClmmpoolsError("Invalid strategy params", "InvalidParams" /* InvalidParams */);
1921
+ }
1922
+ const maxWeight = DEFAULT_MAX_WEIGHT;
1923
+ const minWeight = DEFAULT_MIN_WEIGHT;
1924
+ const diffWeight = maxWeight - minWeight;
1925
+ const diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
1926
+ const diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
1927
+ const distributions = [];
1928
+ for (let i = minBinId; i <= maxBinId; i++) {
1929
+ if (i < activeId) {
1930
+ distributions.push({
1931
+ binId: i,
1932
+ weight: maxWeight - (activeId - i) * diffMinWeight
1933
+ });
1934
+ } else if (i > activeId) {
1935
+ distributions.push({
1936
+ binId: i,
1937
+ weight: maxWeight - (i - activeId) * diffMaxWeight
1938
+ });
1939
+ } else {
1940
+ distributions.push({
1941
+ binId: i,
1942
+ weight: maxWeight
1943
+ });
1944
+ }
1945
+ }
1946
+ return distributions;
1947
+ }
1948
+ function toWeightBidAsk(minBinId, maxBinId, activeId) {
1949
+ if (activeId < minBinId || activeId > maxBinId) {
1950
+ throw new ClmmpoolsError("Invalid strategy params", "InvalidParams" /* InvalidParams */);
1951
+ }
1952
+ const maxWeight = DEFAULT_MAX_WEIGHT;
1953
+ const minWeight = DEFAULT_MIN_WEIGHT;
1954
+ const diffWeight = maxWeight - minWeight;
1955
+ const diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
1956
+ const diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
1957
+ const distributions = [];
1958
+ for (let i = minBinId; i <= maxBinId; i++) {
1959
+ if (i < activeId) {
1960
+ distributions.push({
1961
+ binId: i,
1962
+ weight: minWeight + (activeId - i) * diffMinWeight
1963
+ });
1964
+ } else if (i > activeId) {
1965
+ distributions.push({
1966
+ binId: i,
1967
+ weight: minWeight + (i - activeId) * diffMaxWeight
1968
+ });
1969
+ } else {
1970
+ distributions.push({
1971
+ binId: i,
1972
+ weight: minWeight
1973
+ });
1974
+ }
1975
+ }
1976
+ return distributions;
1977
+ }
1978
+ function autoFillYByStrategy(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
1979
+ switch (strategyType) {
1980
+ case 0 /* Spot */: {
1981
+ const weights = toWeightSpotBalanced(minBinId, maxBinId);
1982
+ return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
1983
+ }
1984
+ case 1 /* Curve */: {
1985
+ const weights = toWeightCurve(minBinId, maxBinId, activeId);
1986
+ return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
1987
+ }
1988
+ case 2 /* BidAsk */: {
1989
+ const weights = toWeightBidAsk(minBinId, maxBinId, activeId);
1990
+ return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
1991
+ }
1992
+ default:
1993
+ throw new Error(`Unsupported strategy type: ${strategyType}`);
1994
+ }
1995
+ }
1996
+ function autoFillXByStrategy(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
1997
+ switch (strategyType) {
1998
+ case 0 /* Spot */: {
1999
+ const weights = toWeightSpotBalanced(minBinId, maxBinId);
2000
+ return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2001
+ }
2002
+ case 1 /* Curve */: {
2003
+ const weights = toWeightCurve(minBinId, maxBinId, activeId);
2004
+ return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2005
+ }
2006
+ case 2 /* BidAsk */: {
2007
+ const weights = toWeightBidAsk(minBinId, maxBinId, activeId);
2008
+ return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2009
+ }
2010
+ default:
2011
+ throw new Error(`Unsupported strategy type: ${strategyType}`);
2012
+ }
2013
+ }
2014
+ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amountX, amountY, amountXInActiveBin, amountYInActiveBin, strategyType) {
2015
+ const isSingleSideX = amountY.isZero();
2016
+ switch (strategyType) {
2017
+ case 0 /* Spot */: {
2018
+ if (activeId < minBinId || activeId > maxBinId) {
2019
+ const weights = toWeightSpotBalanced(minBinId, maxBinId);
2020
+ return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2021
+ }
2022
+ const amountsInBin = [];
2023
+ if (!isSingleSideX) {
2024
+ if (minBinId <= activeId) {
2025
+ const weights = toWeightSpotBalanced(minBinId, activeId);
2026
+ const amounts = toAmountBidSide(activeId, amountY, weights);
2027
+ for (const bin of amounts) {
2028
+ amountsInBin.push({
2029
+ binId: bin.binId,
2030
+ amountX: new BN10(0),
2031
+ amountY: bin.amount
2032
+ });
2033
+ }
2034
+ }
2035
+ if (activeId < maxBinId) {
2036
+ const weights = toWeightSpotBalanced(activeId + 1, maxBinId);
2037
+ const amounts = toAmountAskSide(activeId, binStep, amountX, weights);
2038
+ for (const bin of amounts) {
2039
+ amountsInBin.push({
2040
+ binId: bin.binId,
2041
+ amountX: bin.amount,
2042
+ amountY: new BN10(0)
2043
+ });
2044
+ }
2045
+ }
2046
+ } else {
2047
+ if (minBinId < activeId) {
2048
+ const weights = toWeightSpotBalanced(minBinId, activeId - 1);
2049
+ const amountsIntoBidSide = toAmountBidSide(activeId, amountY, weights);
2050
+ for (const bin of amountsIntoBidSide) {
2051
+ amountsInBin.push({
2052
+ binId: bin.binId,
2053
+ amountX: new BN10(0),
2054
+ amountY: bin.amount
2055
+ });
2056
+ }
2057
+ }
2058
+ if (activeId <= maxBinId) {
2059
+ const weights = toWeightSpotBalanced(activeId, maxBinId);
2060
+ const amountsIntoAskSide = toAmountAskSide(activeId, binStep, amountX, weights);
2061
+ for (const bin of amountsIntoAskSide) {
2062
+ amountsInBin.push({
2063
+ binId: bin.binId,
2064
+ amountX: bin.amount,
2065
+ amountY: new BN10(0)
2066
+ });
2067
+ }
2068
+ }
2069
+ }
2070
+ return amountsInBin;
2071
+ }
2072
+ case 1 /* Curve */: {
2073
+ if (activeId < minBinId) {
2074
+ const weights = toWeightDecendingOrder(minBinId, maxBinId);
2075
+ return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2076
+ }
2077
+ if (activeId > maxBinId) {
2078
+ const weights = toWeightAscendingOrder(minBinId, maxBinId);
2079
+ return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2080
+ }
2081
+ const amountsInBin = [];
2082
+ if (!isSingleSideX) {
2083
+ if (minBinId <= activeId) {
2084
+ const weights = toWeightAscendingOrder(minBinId, activeId);
2085
+ const amounts = toAmountBidSide(activeId, amountY, weights);
2086
+ for (const bin of amounts) {
2087
+ amountsInBin.push({
2088
+ binId: bin.binId,
2089
+ amountX: new BN10(0),
2090
+ amountY: bin.amount
2091
+ });
2092
+ }
2093
+ }
2094
+ if (activeId < maxBinId) {
2095
+ const weights = toWeightDecendingOrder(activeId + 1, maxBinId);
2096
+ const amounts = toAmountAskSide(activeId, binStep, amountX, weights);
2097
+ for (const bin of amounts) {
2098
+ amountsInBin.push({
2099
+ binId: bin.binId,
2100
+ amountX: bin.amount,
2101
+ amountY: new BN10(0)
2102
+ });
2103
+ }
2104
+ }
2105
+ } else {
2106
+ if (minBinId < activeId) {
2107
+ const weights = toWeightAscendingOrder(minBinId, activeId - 1);
2108
+ const amountsIntoBidSide = toAmountBidSide(activeId, amountY, weights);
2109
+ for (const bin of amountsIntoBidSide) {
2110
+ amountsInBin.push({
2111
+ binId: bin.binId,
2112
+ amountX: new BN10(0),
2113
+ amountY: bin.amount
2114
+ });
2115
+ }
2116
+ }
2117
+ if (activeId <= maxBinId) {
2118
+ const weights = toWeightDecendingOrder(activeId, maxBinId);
2119
+ const amountsIntoAskSide = toAmountAskSide(activeId, binStep, amountX, weights);
2120
+ for (const bin of amountsIntoAskSide) {
2121
+ amountsInBin.push({
2122
+ binId: bin.binId,
2123
+ amountX: bin.amount,
2124
+ amountY: new BN10(0)
2125
+ });
2126
+ }
2127
+ }
2128
+ }
2129
+ return amountsInBin;
2130
+ }
2131
+ case 2 /* BidAsk */: {
2132
+ if (activeId < minBinId) {
2133
+ const weights = toWeightAscendingOrder(minBinId, maxBinId);
2134
+ return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2135
+ }
2136
+ if (activeId > maxBinId) {
2137
+ const weights = toWeightDecendingOrder(minBinId, maxBinId);
2138
+ return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
2139
+ }
2140
+ const amountsInBin = [];
2141
+ if (!isSingleSideX) {
2142
+ if (minBinId <= activeId) {
2143
+ const weights = toWeightDecendingOrder(minBinId, activeId);
2144
+ const amounts = toAmountBidSide(activeId, amountY, weights);
2145
+ for (const bin of amounts) {
2146
+ amountsInBin.push({
2147
+ binId: bin.binId,
2148
+ amountX: new BN10(0),
2149
+ amountY: bin.amount
2150
+ });
2151
+ }
2152
+ }
2153
+ if (activeId < maxBinId) {
2154
+ const weights = toWeightAscendingOrder(activeId + 1, maxBinId);
2155
+ const amounts = toAmountAskSide(activeId, binStep, amountX, weights);
2156
+ for (const bin of amounts) {
2157
+ amountsInBin.push({
2158
+ binId: bin.binId,
2159
+ amountX: bin.amount,
2160
+ amountY: new BN10(0)
2161
+ });
2162
+ }
2163
+ }
2164
+ } else {
2165
+ if (minBinId < activeId) {
2166
+ const weights = toWeightDecendingOrder(minBinId, activeId - 1);
2167
+ const amountsIntoBidSide = toAmountBidSide(activeId, amountY, weights);
2168
+ for (const bin of amountsIntoBidSide) {
2169
+ amountsInBin.push({
2170
+ binId: bin.binId,
2171
+ amountX: new BN10(0),
2172
+ amountY: bin.amount
2173
+ });
2174
+ }
2175
+ }
2176
+ if (activeId <= maxBinId) {
2177
+ const weights = toWeightAscendingOrder(activeId, maxBinId);
2178
+ const amountsIntoAskSide = toAmountAskSide(activeId, binStep, amountX, weights);
2179
+ for (const bin of amountsIntoAskSide) {
2180
+ amountsInBin.push({
2181
+ binId: bin.binId,
2182
+ amountX: bin.amount,
2183
+ amountY: new BN10(0)
2184
+ });
2185
+ }
2186
+ }
2187
+ }
2188
+ return amountsInBin;
2189
+ }
2190
+ default:
2191
+ throw new Error(`Unsupported strategy type: ${strategyType}`);
2192
+ }
2193
+ }
2194
+
2195
+ // src/math/LiquidityHelper.ts
2196
+ import Decimal5 from "decimal.js";
2197
+
2198
+ // src/utils/numbers.ts
2199
+ import Decimal4 from "decimal.js";
2200
+ function d(value) {
2201
+ if (Decimal4.isDecimal(value)) {
2202
+ return value;
2203
+ }
2204
+ return new Decimal4(value === void 0 ? 0 : value);
2205
+ }
2206
+ function decimalsMultiplier(decimals) {
2207
+ return d(10).pow(d(decimals).abs());
2208
+ }
2209
+
2210
+ // src/math/LiquidityHelper.ts
2211
+ function withLiquiditySlippage(value, slippage, mode) {
2212
+ return d(value)[mode](d(value).mul(slippage)).toDP(0);
2213
+ }
2214
+ function getLiquidityAndCoinYByCoinX(coinInVal, reserveInSize, reserveOutSize, lpSupply) {
2215
+ if (coinInVal.lessThanOrEqualTo(0)) {
2216
+ throw new ClmmpoolsError("coinInVal is less than zero", "InvalidCoinAmount" /* InvalidCoinAmount */);
2217
+ }
2218
+ if (reserveInSize.lessThanOrEqualTo(0) || reserveOutSize.lessThanOrEqualTo(0)) {
2219
+ return -1;
2220
+ }
2221
+ const coinYAmount = coinInVal.mul(reserveOutSize).div(reserveInSize);
2222
+ const sqrtSupply = lpSupply;
2223
+ const lpX = coinInVal.div(reserveInSize).mul(sqrtSupply);
2224
+ const lpY = coinYAmount.div(reserveOutSize).mul(sqrtSupply);
2225
+ const lpAmount = Decimal5.min(lpX, lpY);
2226
+ return {
2227
+ coinYAmount,
2228
+ lpAmount
2229
+ };
2230
+ }
2231
+ function getCoinXYForLiquidity(liquidity, reserveInSize, reserveOutSize, lpSuply) {
2232
+ if (liquidity.lessThanOrEqualTo(0)) {
2233
+ throw new ClmmpoolsError("liquidity can't be equal or less than zero", "InvalidLiquidityAmount" /* InvalidLiquidityAmount */);
2234
+ }
2235
+ if (reserveInSize.lessThanOrEqualTo(0) || reserveOutSize.lessThanOrEqualTo(0)) {
2236
+ throw new ClmmpoolsError("reserveInSize or reserveOutSize can not be equal or less than zero", "InvalidReserveAmount" /* InvalidReserveAmount */);
2237
+ }
2238
+ const sqrtSupply = lpSuply;
2239
+ const coinXAmount = liquidity.div(sqrtSupply).mul(reserveInSize);
2240
+ const coinYAmount = liquidity.div(sqrtSupply).mul(reserveOutSize);
2241
+ return {
2242
+ coinXAmount,
2243
+ coinYAmount
2244
+ };
2245
+ }
2246
+
2247
+ // src/math/percentage.ts
2248
+ import BN11 from "bn.js";
1597
2249
  var Percentage = class {
1598
2250
  numerator;
1599
2251
  denominator;
@@ -1621,8 +2273,8 @@ var Percentage = class {
1621
2273
  * @returns
1622
2274
  */
1623
2275
  static fromFraction(numerator, denominator) {
1624
- const num = typeof numerator === "number" ? new BN9(numerator.toString()) : numerator;
1625
- const denom = typeof denominator === "number" ? new BN9(denominator.toString()) : denominator;
2276
+ const num = typeof numerator === "number" ? new BN11(numerator.toString()) : numerator;
2277
+ const denom = typeof denominator === "number" ? new BN11(denominator.toString()) : denominator;
1626
2278
  return new Percentage(num, denom);
1627
2279
  }
1628
2280
  };
@@ -1722,7 +2374,7 @@ function adjustForCoinSlippage(tokenAmount, slippage, adjustUp) {
1722
2374
  }
1723
2375
 
1724
2376
  // src/math/SplitSwap.ts
1725
- import BN10 from "bn.js";
2377
+ import BN12 from "bn.js";
1726
2378
  var SplitUnit = /* @__PURE__ */ ((SplitUnit2) => {
1727
2379
  SplitUnit2[SplitUnit2["FIVE"] = 5] = "FIVE";
1728
2380
  SplitUnit2[SplitUnit2["TEN"] = 10] = "TEN";
@@ -1780,7 +2432,7 @@ function updateSplitSwapResult(maxIndex, currentIndex, splitSwapResult, stepResu
1780
2432
  return splitSwapResult;
1781
2433
  }
1782
2434
  function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
1783
- let currentLiquidity = new BN10(poolData.liquidity);
2435
+ let currentLiquidity = new BN12(poolData.liquidity);
1784
2436
  let { currentSqrtPrice } = poolData;
1785
2437
  let splitSwapResult = {
1786
2438
  amountInArray: [],
@@ -1843,7 +2495,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
1843
2495
  targetSqrtPrice,
1844
2496
  currentLiquidity,
1845
2497
  remainerAmount,
1846
- new BN10(poolData.feeRate),
2498
+ new BN12(poolData.feeRate),
1847
2499
  byAmountIn
1848
2500
  );
1849
2501
  tempStepResult = stepResult;
@@ -1855,7 +2507,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
1855
2507
  splitSwapResult.amountOutArray[i] = splitSwapResult.amountOutArray[i].add(stepResult.amountOut);
1856
2508
  splitSwapResult.feeAmountArray[i] = splitSwapResult.feeAmountArray[i].add(stepResult.feeAmount);
1857
2509
  if (stepResult.nextSqrtPrice.eq(tick.sqrtPrice)) {
1858
- signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN10(-1)) : tick.liquidityNet;
2510
+ signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN12(-1)) : tick.liquidityNet;
1859
2511
  currentLiquidity = signedLiquidityChange.gt(ZERO) ? currentLiquidity.add(signedLiquidityChange) : currentLiquidity.sub(signedLiquidityChange.abs());
1860
2512
  currentSqrtPrice = tick.sqrtPrice;
1861
2513
  } else {
@@ -1880,7 +2532,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
1880
2532
  break;
1881
2533
  }
1882
2534
  if (tempStepResult.nextSqrtPrice.eq(tick.sqrtPrice)) {
1883
- signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN10(-1)) : tick.liquidityNet;
2535
+ signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN12(-1)) : tick.liquidityNet;
1884
2536
  currentLiquidity = signedLiquidityChange.gt(ZERO) ? currentLiquidity.add(signedLiquidityChange) : currentLiquidity.sub(signedLiquidityChange.abs());
1885
2537
  currentSqrtPrice = tick.sqrtPrice;
1886
2538
  } else {
@@ -1935,18 +2587,6 @@ var SplitSwap = class {
1935
2587
  }
1936
2588
  };
1937
2589
 
1938
- // src/utils/numbers.ts
1939
- import Decimal3 from "decimal.js";
1940
- function d(value) {
1941
- if (Decimal3.isDecimal(value)) {
1942
- return value;
1943
- }
1944
- return new Decimal3(value === void 0 ? 0 : value);
1945
- }
1946
- function decimalsMultiplier(decimals) {
1947
- return d(10).pow(d(decimals).abs());
1948
- }
1949
-
1950
2590
  // src/utils/objects.ts
1951
2591
  function getSuiObjectData(resp) {
1952
2592
  return resp.data;
@@ -2100,7 +2740,7 @@ function buildPool(objects) {
2100
2740
  const rewarders = [];
2101
2741
  fields.rewarder_manager.fields.rewarders.forEach((item) => {
2102
2742
  const { emissions_per_second } = item.fields;
2103
- const emissionSeconds = MathUtil.fromX64(new BN11(emissions_per_second));
2743
+ const emissionSeconds = MathUtil.fromX64(new BN13(emissions_per_second));
2104
2744
  const emissionsEveryDay = Math.floor(emissionSeconds.toNumber() * 60 * 60 * 24);
2105
2745
  rewarders.push({
2106
2746
  emissions_per_second,
@@ -2292,11 +2932,11 @@ function buildTickData(objects) {
2292
2932
  const possition = {
2293
2933
  objectId: getObjectId(objects),
2294
2934
  index: asIntN(BigInt(valueItem.index.fields.bits)),
2295
- sqrtPrice: new BN11(valueItem.sqrt_price),
2296
- liquidityNet: new BN11(valueItem.liquidity_net.fields.bits),
2297
- liquidityGross: new BN11(valueItem.liquidity_gross),
2298
- feeGrowthOutsideA: new BN11(valueItem.fee_growth_outside_a),
2299
- feeGrowthOutsideB: new BN11(valueItem.fee_growth_outside_b),
2935
+ sqrtPrice: new BN13(valueItem.sqrt_price),
2936
+ liquidityNet: new BN13(valueItem.liquidity_net.fields.bits),
2937
+ liquidityGross: new BN13(valueItem.liquidity_gross),
2938
+ feeGrowthOutsideA: new BN13(valueItem.fee_growth_outside_a),
2939
+ feeGrowthOutsideB: new BN13(valueItem.fee_growth_outside_b),
2300
2940
  rewardersGrowthOutside: valueItem.rewards_growth_outside
2301
2941
  };
2302
2942
  return possition;
@@ -2306,11 +2946,11 @@ function buildTickDataByEvent(fields) {
2306
2946
  throw new ClmmpoolsError(`Invalid tick fields.`, "InvalidTickFields" /* InvalidTickFields */);
2307
2947
  }
2308
2948
  const index = asIntN(BigInt(fields.index.bits));
2309
- const sqrtPrice = new BN11(fields.sqrt_price);
2310
- const liquidityNet = new BN11(fields.liquidity_net.bits);
2311
- const liquidityGross = new BN11(fields.liquidity_gross);
2312
- const feeGrowthOutsideA = new BN11(fields.fee_growth_outside_a);
2313
- const feeGrowthOutsideB = new BN11(fields.fee_growth_outside_b);
2949
+ const sqrtPrice = new BN13(fields.sqrt_price);
2950
+ const liquidityNet = new BN13(fields.liquidity_net.bits);
2951
+ const liquidityGross = new BN13(fields.liquidity_gross);
2952
+ const feeGrowthOutsideA = new BN13(fields.fee_growth_outside_a);
2953
+ const feeGrowthOutsideB = new BN13(fields.fee_growth_outside_b);
2314
2954
  const rewardersGrowthOutside = fields.rewards_growth_outside || [];
2315
2955
  const tick = {
2316
2956
  objectId: "",
@@ -2329,7 +2969,7 @@ function buildClmmPositionName(pool_index, position_index) {
2329
2969
  }
2330
2970
 
2331
2971
  // src/utils/tick.ts
2332
- import BN12 from "bn.js";
2972
+ import BN14 from "bn.js";
2333
2973
  var TickUtil = class {
2334
2974
  /**
2335
2975
  * Get min tick index.
@@ -2369,22 +3009,22 @@ function getRewardInTickRange(pool, tickLower, tickUpper, tickLowerIndex, tickUp
2369
3009
  let rewarder_growth_below = growthGlobal[i];
2370
3010
  if (tickLower !== null) {
2371
3011
  if (pool.current_tick_index < tickLowerIndex) {
2372
- rewarder_growth_below = growthGlobal[i].sub(new BN12(tickLower.rewardersGrowthOutside[i]));
3012
+ rewarder_growth_below = growthGlobal[i].sub(new BN14(tickLower.rewardersGrowthOutside[i]));
2373
3013
  } else {
2374
3014
  rewarder_growth_below = tickLower.rewardersGrowthOutside[i];
2375
3015
  }
2376
3016
  }
2377
- let rewarder_growth_above = new BN12(0);
3017
+ let rewarder_growth_above = new BN14(0);
2378
3018
  if (tickUpper !== null) {
2379
3019
  if (pool.current_tick_index >= tickUpperIndex) {
2380
- rewarder_growth_above = growthGlobal[i].sub(new BN12(tickUpper.rewardersGrowthOutside[i]));
3020
+ rewarder_growth_above = growthGlobal[i].sub(new BN14(tickUpper.rewardersGrowthOutside[i]));
2381
3021
  } else {
2382
3022
  rewarder_growth_above = tickUpper.rewardersGrowthOutside[i];
2383
3023
  }
2384
3024
  }
2385
3025
  const rewGrowthInside = MathUtil.subUnderflowU128(
2386
- MathUtil.subUnderflowU128(new BN12(growthGlobal[i]), new BN12(rewarder_growth_below)),
2387
- new BN12(rewarder_growth_above)
3026
+ MathUtil.subUnderflowU128(new BN14(growthGlobal[i]), new BN14(rewarder_growth_below)),
3027
+ new BN14(rewarder_growth_above)
2388
3028
  );
2389
3029
  rewarderGrowthInside.push(rewGrowthInside);
2390
3030
  }
@@ -2392,8 +3032,8 @@ function getRewardInTickRange(pool, tickLower, tickUpper, tickLowerIndex, tickUp
2392
3032
  }
2393
3033
 
2394
3034
  // src/utils/transaction-util.ts
2395
- import BN13 from "bn.js";
2396
- import Decimal4 from "decimal.js";
3035
+ import BN15 from "bn.js";
3036
+ import Decimal6 from "decimal.js";
2397
3037
  import { Transaction } from "@mysten/sui/transactions";
2398
3038
  function findAdjustCoin(coinPair) {
2399
3039
  const isAdjustCoinA = CoinAssist.isSuiCoin(coinPair.coinTypeA);
@@ -2401,7 +3041,7 @@ function findAdjustCoin(coinPair) {
2401
3041
  return { isAdjustCoinA, isAdjustCoinB };
2402
3042
  }
2403
3043
  function reverSlippageAmount(slippageAmount, slippage) {
2404
- return Decimal4.ceil(d(slippageAmount).div(1 + slippage)).toString();
3044
+ return Decimal6.ceil(d(slippageAmount).div(1 + slippage)).toString();
2405
3045
  }
2406
3046
  async function printTransaction(tx, isPrint = true) {
2407
3047
  console.log(`inputs`, tx.blockData.inputs);
@@ -2701,7 +3341,7 @@ var _TransactionUtil = class {
2701
3341
  const liquidityInput = ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts(
2702
3342
  Number(params.tick_lower),
2703
3343
  Number(params.tick_upper),
2704
- new BN13(coinAmount),
3344
+ new BN15(coinAmount),
2705
3345
  params.fix_amount_a,
2706
3346
  true,
2707
3347
  slippage,
@@ -2775,12 +3415,12 @@ var _TransactionUtil = class {
2775
3415
  max_amount_a = params.amount_a;
2776
3416
  min_amount_a = params.amount_a;
2777
3417
  max_amount_b = params.amount_b;
2778
- min_amount_b = new Decimal4(params.amount_b).div(new Decimal4(1).plus(new Decimal4(params.slippage))).mul(new Decimal4(1).minus(new Decimal4(params.slippage))).toDecimalPlaces(0).toNumber();
3418
+ min_amount_b = new Decimal6(params.amount_b).div(new Decimal6(1).plus(new Decimal6(params.slippage))).mul(new Decimal6(1).minus(new Decimal6(params.slippage))).toDecimalPlaces(0).toNumber();
2779
3419
  } else {
2780
3420
  max_amount_b = params.amount_b;
2781
3421
  min_amount_b = params.amount_b;
2782
3422
  max_amount_a = params.amount_a;
2783
- min_amount_a = new Decimal4(params.amount_a).div(new Decimal4(1).plus(new Decimal4(params.slippage))).mul(new Decimal4(1).minus(new Decimal4(params.slippage))).toDecimalPlaces(0).toNumber();
3423
+ min_amount_a = new Decimal6(params.amount_a).div(new Decimal6(1).plus(new Decimal6(params.slippage))).mul(new Decimal6(1).minus(new Decimal6(params.slippage))).toDecimalPlaces(0).toNumber();
2784
3424
  }
2785
3425
  const args = params.is_open ? [
2786
3426
  tx.object(clmmConfig.global_config_id),
@@ -3769,7 +4409,7 @@ var _TransactionUtil = class {
3769
4409
  const basePath = splitPath.basePaths[i];
3770
4410
  a2b.push(basePath.direction);
3771
4411
  poolAddress.push(basePath.poolAddress);
3772
- rawAmountLimit.push(new BN13(basePath.inputAmount.toString()));
4412
+ rawAmountLimit.push(new BN15(basePath.inputAmount.toString()));
3773
4413
  if (i === 0) {
3774
4414
  coinType.push(basePath.fromCoin, basePath.toCoin);
3775
4415
  } else {
@@ -3777,8 +4417,8 @@ var _TransactionUtil = class {
3777
4417
  }
3778
4418
  }
3779
4419
  const onePath = {
3780
- amountIn: new BN13(splitPath.inputAmount.toString()),
3781
- amountOut: new BN13(splitPath.outputAmount.toString()),
4420
+ amountIn: new BN15(splitPath.inputAmount.toString()),
4421
+ amountOut: new BN15(splitPath.outputAmount.toString()),
3782
4422
  poolAddress,
3783
4423
  a2b,
3784
4424
  rawAmountLimit,
@@ -4135,9 +4775,9 @@ var TxBlock = class {
4135
4775
  };
4136
4776
 
4137
4777
  // src/utils/deepbook-utils.ts
4138
- import BN14 from "bn.js";
4778
+ import BN16 from "bn.js";
4139
4779
  import { Transaction as Transaction3 } from "@mysten/sui/transactions";
4140
- var FLOAT_SCALING = new BN14(1e9);
4780
+ var FLOAT_SCALING = new BN16(1e9);
4141
4781
  var DeepbookUtils = class {
4142
4782
  static createAccountCap(senderAddress, sdkOptions, tx, isTransfer = false) {
4143
4783
  if (senderAddress.length === 0) {
@@ -4283,9 +4923,9 @@ var DeepbookUtils = class {
4283
4923
  static async preSwap(sdk, pool, a2b, amountIn) {
4284
4924
  let isExceed = false;
4285
4925
  let amountOut = ZERO;
4286
- let remainAmount = new BN14(amountIn);
4926
+ let remainAmount = new BN16(amountIn);
4287
4927
  let feeAmount = ZERO;
4288
- const initAmountIn = new BN14(amountIn);
4928
+ const initAmountIn = new BN16(amountIn);
4289
4929
  if (a2b) {
4290
4930
  let bids = await this.getPoolBids(sdk, pool.poolID, pool.baseAsset, pool.quoteAsset);
4291
4931
  if (bids === null) {
@@ -4295,16 +4935,16 @@ var DeepbookUtils = class {
4295
4935
  return b.price - a.price;
4296
4936
  });
4297
4937
  for (let i = 0; i < bids.length; i += 1) {
4298
- const curBidAmount = new BN14(bids[i].quantity);
4299
- const curBidPrice = new BN14(bids[i].price);
4300
- const fee = curBidAmount.mul(new BN14(curBidPrice)).mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING).div(FLOAT_SCALING);
4938
+ const curBidAmount = new BN16(bids[i].quantity);
4939
+ const curBidPrice = new BN16(bids[i].price);
4940
+ const fee = curBidAmount.mul(new BN16(curBidPrice)).mul(new BN16(pool.takerFeeRate)).div(FLOAT_SCALING).div(FLOAT_SCALING);
4301
4941
  if (remainAmount.gt(curBidAmount)) {
4302
4942
  remainAmount = remainAmount.sub(curBidAmount);
4303
4943
  amountOut = amountOut.add(curBidAmount.mul(curBidPrice).div(FLOAT_SCALING).sub(fee));
4304
4944
  feeAmount = feeAmount.add(fee);
4305
4945
  } else {
4306
- const curOut = remainAmount.mul(new BN14(bids[i].price)).div(FLOAT_SCALING);
4307
- const curFee = curOut.mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING);
4946
+ const curOut = remainAmount.mul(new BN16(bids[i].price)).div(FLOAT_SCALING);
4947
+ const curFee = curOut.mul(new BN16(pool.takerFeeRate)).div(FLOAT_SCALING);
4308
4948
  amountOut = amountOut.add(curOut.sub(curFee));
4309
4949
  remainAmount = remainAmount.sub(remainAmount);
4310
4950
  feeAmount = feeAmount.add(curFee);
@@ -4319,15 +4959,15 @@ var DeepbookUtils = class {
4319
4959
  isExceed = true;
4320
4960
  }
4321
4961
  for (let i = 0; i < asks.length; i += 1) {
4322
- const curAskAmount = new BN14(asks[i].price).mul(new BN14(asks[i].quantity)).div(new BN14(1e9));
4323
- const fee = curAskAmount.mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING);
4962
+ const curAskAmount = new BN16(asks[i].price).mul(new BN16(asks[i].quantity)).div(new BN16(1e9));
4963
+ const fee = curAskAmount.mul(new BN16(pool.takerFeeRate)).div(FLOAT_SCALING);
4324
4964
  const curAskAmountWithFee = curAskAmount.add(fee);
4325
4965
  if (remainAmount.gt(curAskAmount)) {
4326
- amountOut = amountOut.add(new BN14(asks[i].quantity));
4966
+ amountOut = amountOut.add(new BN16(asks[i].quantity));
4327
4967
  remainAmount = remainAmount.sub(curAskAmountWithFee);
4328
4968
  feeAmount = feeAmount.add(fee);
4329
4969
  } else {
4330
- const splitNums = new BN14(asks[i].quantity).div(new BN14(pool.lotSize));
4970
+ const splitNums = new BN16(asks[i].quantity).div(new BN16(pool.lotSize));
4331
4971
  const splitAmount = curAskAmountWithFee.div(splitNums);
4332
4972
  const swapSplitNum = remainAmount.div(splitAmount);
4333
4973
  amountOut = amountOut.add(swapSplitNum.muln(pool.lotSize));
@@ -5188,7 +5828,7 @@ var PoolModule = class {
5188
5828
  };
5189
5829
 
5190
5830
  // src/modules/positionModule.ts
5191
- import BN15 from "bn.js";
5831
+ import BN17 from "bn.js";
5192
5832
  import { Transaction as Transaction5 } from "@mysten/sui/transactions";
5193
5833
  import { isValidSuiObjectId } from "@mysten/sui/utils";
5194
5834
  var PositionModule = class {
@@ -5410,8 +6050,8 @@ var PositionModule = class {
5410
6050
  for (let i = 0; i < valueData.length; i += 1) {
5411
6051
  const { parsedJson } = valueData[i];
5412
6052
  const posRrewarderResult = {
5413
- feeOwedA: new BN15(parsedJson.fee_owned_a),
5414
- feeOwedB: new BN15(parsedJson.fee_owned_b),
6053
+ feeOwedA: new BN17(parsedJson.fee_owned_a),
6054
+ feeOwedB: new BN17(parsedJson.fee_owned_b),
5415
6055
  position_id: parsedJson.position_id
5416
6056
  };
5417
6057
  result.push(posRrewarderResult);
@@ -5787,7 +6427,7 @@ var PositionModule = class {
5787
6427
  };
5788
6428
 
5789
6429
  // src/modules/rewarderModule.ts
5790
- import BN16 from "bn.js";
6430
+ import BN18 from "bn.js";
5791
6431
  import { Transaction as Transaction6 } from "@mysten/sui/transactions";
5792
6432
  var RewarderModule = class {
5793
6433
  _sdk;
@@ -5813,7 +6453,7 @@ var RewarderModule = class {
5813
6453
  }
5814
6454
  const emissionsEveryDay = [];
5815
6455
  for (const rewarderInfo of rewarderInfos) {
5816
- const emissionSeconds = MathUtil.fromX64(new BN16(rewarderInfo.emissions_per_second));
6456
+ const emissionSeconds = MathUtil.fromX64(new BN18(rewarderInfo.emissions_per_second));
5817
6457
  emissionsEveryDay.push({
5818
6458
  emissions: Math.floor(emissionSeconds.toNumber() * 60 * 60 * 24),
5819
6459
  coin_address: rewarderInfo.coinAddress
@@ -5832,20 +6472,20 @@ var RewarderModule = class {
5832
6472
  const currentPool = await this.sdk.Pool.getPool(poolID);
5833
6473
  const lastTime = currentPool.rewarder_last_updated_time;
5834
6474
  currentPool.rewarder_last_updated_time = currentTime.toString();
5835
- if (Number(currentPool.liquidity) === 0 || currentTime.eq(new BN16(lastTime))) {
6475
+ if (Number(currentPool.liquidity) === 0 || currentTime.eq(new BN18(lastTime))) {
5836
6476
  return currentPool;
5837
6477
  }
5838
- const timeDelta = currentTime.div(new BN16(1e3)).sub(new BN16(lastTime)).add(new BN16(15));
6478
+ const timeDelta = currentTime.div(new BN18(1e3)).sub(new BN18(lastTime)).add(new BN18(15));
5839
6479
  const rewarderInfos = currentPool.rewarder_infos;
5840
6480
  for (let i = 0; i < rewarderInfos.length; i += 1) {
5841
6481
  const rewarderInfo = rewarderInfos[i];
5842
6482
  const rewarderGrowthDelta = MathUtil.checkMulDivFloor(
5843
6483
  timeDelta,
5844
- new BN16(rewarderInfo.emissions_per_second),
5845
- new BN16(currentPool.liquidity),
6484
+ new BN18(rewarderInfo.emissions_per_second),
6485
+ new BN18(currentPool.liquidity),
5846
6486
  128
5847
6487
  );
5848
- this.growthGlobal[i] = new BN16(rewarderInfo.growth_global).add(new BN16(rewarderGrowthDelta));
6488
+ this.growthGlobal[i] = new BN18(rewarderInfo.growth_global).add(new BN18(rewarderGrowthDelta));
5849
6489
  }
5850
6490
  return currentPool;
5851
6491
  }
@@ -5860,7 +6500,7 @@ var RewarderModule = class {
5860
6500
  */
5861
6501
  async posRewardersAmount(poolID, positionHandle, positionID) {
5862
6502
  const currentTime = Date.parse((/* @__PURE__ */ new Date()).toString());
5863
- const pool = await this.updatePoolRewarder(poolID, new BN16(currentTime));
6503
+ const pool = await this.updatePoolRewarder(poolID, new BN18(currentTime));
5864
6504
  const position = await this.sdk.Position.getPositionRewarders(positionHandle, positionID);
5865
6505
  if (position === void 0) {
5866
6506
  return [];
@@ -5881,7 +6521,7 @@ var RewarderModule = class {
5881
6521
  */
5882
6522
  async poolRewardersAmount(accountAddress, poolID) {
5883
6523
  const currentTime = Date.parse((/* @__PURE__ */ new Date()).toString());
5884
- const pool = await this.updatePoolRewarder(poolID, new BN16(currentTime));
6524
+ const pool = await this.updatePoolRewarder(poolID, new BN18(currentTime));
5885
6525
  const positions = await this.sdk.Position.getPositionList(accountAddress, [poolID]);
5886
6526
  const tickDatas = await this.getPoolLowerAndUpperTicks(pool.ticks_handle, positions);
5887
6527
  const rewarderAmount = [ZERO, ZERO, ZERO];
@@ -5908,38 +6548,38 @@ var RewarderModule = class {
5908
6548
  const growthInside = [];
5909
6549
  const AmountOwed = [];
5910
6550
  if (rewardersInside.length > 0) {
5911
- let growthDelta0 = MathUtil.subUnderflowU128(rewardersInside[0], new BN16(position.reward_growth_inside_0));
5912
- if (growthDelta0.gt(new BN16("3402823669209384634633745948738404"))) {
6551
+ let growthDelta0 = MathUtil.subUnderflowU128(rewardersInside[0], new BN18(position.reward_growth_inside_0));
6552
+ if (growthDelta0.gt(new BN18("3402823669209384634633745948738404"))) {
5913
6553
  growthDelta0 = ONE;
5914
6554
  }
5915
- const amountOwed_0 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta0, 64, 128);
6555
+ const amountOwed_0 = MathUtil.checkMulShiftRight(new BN18(position.liquidity), growthDelta0, 64, 128);
5916
6556
  growthInside.push(rewardersInside[0]);
5917
6557
  AmountOwed.push({
5918
- amount_owed: new BN16(position.reward_amount_owed_0).add(amountOwed_0),
6558
+ amount_owed: new BN18(position.reward_amount_owed_0).add(amountOwed_0),
5919
6559
  coin_address: pool.rewarder_infos[0].coinAddress
5920
6560
  });
5921
6561
  }
5922
6562
  if (rewardersInside.length > 1) {
5923
- let growthDelta_1 = MathUtil.subUnderflowU128(rewardersInside[1], new BN16(position.reward_growth_inside_1));
5924
- if (growthDelta_1.gt(new BN16("3402823669209384634633745948738404"))) {
6563
+ let growthDelta_1 = MathUtil.subUnderflowU128(rewardersInside[1], new BN18(position.reward_growth_inside_1));
6564
+ if (growthDelta_1.gt(new BN18("3402823669209384634633745948738404"))) {
5925
6565
  growthDelta_1 = ONE;
5926
6566
  }
5927
- const amountOwed_1 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta_1, 64, 128);
6567
+ const amountOwed_1 = MathUtil.checkMulShiftRight(new BN18(position.liquidity), growthDelta_1, 64, 128);
5928
6568
  growthInside.push(rewardersInside[1]);
5929
6569
  AmountOwed.push({
5930
- amount_owed: new BN16(position.reward_amount_owed_1).add(amountOwed_1),
6570
+ amount_owed: new BN18(position.reward_amount_owed_1).add(amountOwed_1),
5931
6571
  coin_address: pool.rewarder_infos[1].coinAddress
5932
6572
  });
5933
6573
  }
5934
6574
  if (rewardersInside.length > 2) {
5935
- let growthDelta_2 = MathUtil.subUnderflowU128(rewardersInside[2], new BN16(position.reward_growth_inside_2));
5936
- if (growthDelta_2.gt(new BN16("3402823669209384634633745948738404"))) {
6575
+ let growthDelta_2 = MathUtil.subUnderflowU128(rewardersInside[2], new BN18(position.reward_growth_inside_2));
6576
+ if (growthDelta_2.gt(new BN18("3402823669209384634633745948738404"))) {
5937
6577
  growthDelta_2 = ONE;
5938
6578
  }
5939
- const amountOwed_2 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta_2, 64, 128);
6579
+ const amountOwed_2 = MathUtil.checkMulShiftRight(new BN18(position.liquidity), growthDelta_2, 64, 128);
5940
6580
  growthInside.push(rewardersInside[2]);
5941
6581
  AmountOwed.push({
5942
- amount_owed: new BN16(position.reward_amount_owed_2).add(amountOwed_2),
6582
+ amount_owed: new BN18(position.reward_amount_owed_2).add(amountOwed_2),
5943
6583
  coin_address: pool.rewarder_infos[2].coinAddress
5944
6584
  });
5945
6585
  }
@@ -6053,8 +6693,8 @@ var RewarderModule = class {
6053
6693
  for (let i = 0; i < valueData.length; i += 1) {
6054
6694
  const { parsedJson } = valueData[i];
6055
6695
  const posRrewarderResult = {
6056
- feeOwedA: new BN16(parsedJson.fee_owned_a),
6057
- feeOwedB: new BN16(parsedJson.fee_owned_b),
6696
+ feeOwedA: new BN18(parsedJson.fee_owned_a),
6697
+ feeOwedB: new BN18(parsedJson.fee_owned_b),
6058
6698
  position_id: parsedJson.position_id
6059
6699
  };
6060
6700
  result.push(posRrewarderResult);
@@ -6117,7 +6757,7 @@ var RewarderModule = class {
6117
6757
  };
6118
6758
  for (let j = 0; j < params[i].rewarderInfo.length; j += 1) {
6119
6759
  posRrewarderResult.rewarderAmountOwed.push({
6120
- amount_owed: new BN16(valueData[i].parsedJson.data[j]),
6760
+ amount_owed: new BN18(valueData[i].parsedJson.data[j]),
6121
6761
  coin_address: params[i].rewarderInfo[j].coinAddress
6122
6762
  });
6123
6763
  }
@@ -6306,7 +6946,7 @@ var RewarderModule = class {
6306
6946
  };
6307
6947
 
6308
6948
  // src/modules/routerModule.ts
6309
- import BN17 from "bn.js";
6949
+ import BN19 from "bn.js";
6310
6950
  import { Graph, GraphEdge, GraphVertex } from "@syntsugar/cc-graph";
6311
6951
  import { Transaction as Transaction7 } from "@mysten/sui/transactions";
6312
6952
  function _pairSymbol(base, quote) {
@@ -6588,8 +7228,8 @@ var RouterModule = class {
6588
7228
  if (swapWithMultiPoolParams != null) {
6589
7229
  const preSwapResult2 = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
6590
7230
  const onePath2 = {
6591
- amountIn: new BN17(preSwapResult2.estimatedAmountIn),
6592
- amountOut: new BN17(preSwapResult2.estimatedAmountOut),
7231
+ amountIn: new BN19(preSwapResult2.estimatedAmountIn),
7232
+ amountOut: new BN19(preSwapResult2.estimatedAmountOut),
6593
7233
  poolAddress: [preSwapResult2.poolAddress],
6594
7234
  a2b: [preSwapResult2.aToB],
6595
7235
  rawAmountLimit: byAmountIn ? [preSwapResult2.estimatedAmountOut] : [preSwapResult2.estimatedAmountIn],
@@ -6602,8 +7242,8 @@ var RouterModule = class {
6602
7242
  priceSlippagePoint
6603
7243
  };
6604
7244
  const result2 = {
6605
- amountIn: new BN17(preSwapResult2.estimatedAmountIn),
6606
- amountOut: new BN17(preSwapResult2.estimatedAmountOut),
7245
+ amountIn: new BN19(preSwapResult2.estimatedAmountIn),
7246
+ amountOut: new BN19(preSwapResult2.estimatedAmountOut),
6607
7247
  paths: [onePath2],
6608
7248
  a2b: preSwapResult2.aToB,
6609
7249
  b2c: void 0,
@@ -6625,8 +7265,8 @@ var RouterModule = class {
6625
7265
  if (swapWithMultiPoolParams != null) {
6626
7266
  const preSwapResult2 = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
6627
7267
  const onePath2 = {
6628
- amountIn: new BN17(preSwapResult2.estimatedAmountIn),
6629
- amountOut: new BN17(preSwapResult2.estimatedAmountOut),
7268
+ amountIn: new BN19(preSwapResult2.estimatedAmountIn),
7269
+ amountOut: new BN19(preSwapResult2.estimatedAmountOut),
6630
7270
  poolAddress: [preSwapResult2.poolAddress],
6631
7271
  a2b: [preSwapResult2.aToB],
6632
7272
  rawAmountLimit: byAmountIn ? [preSwapResult2.estimatedAmountOut] : [preSwapResult2.estimatedAmountIn],
@@ -6639,8 +7279,8 @@ var RouterModule = class {
6639
7279
  priceSlippagePoint
6640
7280
  };
6641
7281
  const result3 = {
6642
- amountIn: new BN17(preSwapResult2.estimatedAmountIn),
6643
- amountOut: new BN17(preSwapResult2.estimatedAmountOut),
7282
+ amountIn: new BN19(preSwapResult2.estimatedAmountIn),
7283
+ amountOut: new BN19(preSwapResult2.estimatedAmountOut),
6644
7284
  paths: [onePath2],
6645
7285
  a2b: preSwapResult2.aToB,
6646
7286
  b2c: void 0,
@@ -6721,8 +7361,8 @@ var RouterModule = class {
6721
7361
  if (swapWithMultiPoolParams != null) {
6722
7362
  const preSwapResult = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
6723
7363
  const onePath = {
6724
- amountIn: new BN17(preSwapResult.estimatedAmountIn),
6725
- amountOut: new BN17(preSwapResult.estimatedAmountOut),
7364
+ amountIn: new BN19(preSwapResult.estimatedAmountIn),
7365
+ amountOut: new BN19(preSwapResult.estimatedAmountOut),
6726
7366
  poolAddress: [preSwapResult.poolAddress],
6727
7367
  a2b: [preSwapResult.aToB],
6728
7368
  rawAmountLimit: byAmountIn ? [preSwapResult.estimatedAmountOut] : [preSwapResult.estimatedAmountIn],
@@ -6735,8 +7375,8 @@ var RouterModule = class {
6735
7375
  priceSlippagePoint
6736
7376
  };
6737
7377
  const result = {
6738
- amountIn: new BN17(preSwapResult.estimatedAmountIn),
6739
- amountOut: new BN17(preSwapResult.estimatedAmountOut),
7378
+ amountIn: new BN19(preSwapResult.estimatedAmountIn),
7379
+ amountOut: new BN19(preSwapResult.estimatedAmountOut),
6740
7380
  paths: [onePath],
6741
7381
  a2b: preSwapResult.aToB,
6742
7382
  b2c: void 0,
@@ -6820,13 +7460,13 @@ var RouterModule = class {
6820
7460
  continue;
6821
7461
  }
6822
7462
  if (params[0].byAmountIn) {
6823
- const amount = new BN17(valueData[i].parsedJson.data.amount_out);
7463
+ const amount = new BN19(valueData[i].parsedJson.data.amount_out);
6824
7464
  if (amount.gt(tempMaxAmount)) {
6825
7465
  tempIndex = i;
6826
7466
  tempMaxAmount = amount;
6827
7467
  }
6828
7468
  } else {
6829
- const amount = params[i].stepNums > 1 ? new BN17(valueData[i].parsedJson.data.amount_in) : new BN17(valueData[i].parsedJson.data.amount_in).add(new BN17(valueData[i].parsedJson.data.fee_amount));
7469
+ const amount = params[i].stepNums > 1 ? new BN19(valueData[i].parsedJson.data.amount_in) : new BN19(valueData[i].parsedJson.data.amount_in).add(new BN19(valueData[i].parsedJson.data.fee_amount));
6830
7470
  if (amount.lt(tempMaxAmount)) {
6831
7471
  tempIndex = i;
6832
7472
  tempMaxAmount = amount;
@@ -6896,8 +7536,8 @@ var RouterModule = class {
6896
7536
  };
6897
7537
 
6898
7538
  // src/modules/swapModule.ts
6899
- import BN18 from "bn.js";
6900
- import Decimal5 from "decimal.js";
7539
+ import BN20 from "bn.js";
7540
+ import Decimal7 from "decimal.js";
6901
7541
  import { Transaction as Transaction8 } from "@mysten/sui/transactions";
6902
7542
  var AMM_SWAP_MODULE = "amm_swap";
6903
7543
  var POOL_STRUCT = "Pool";
@@ -6915,14 +7555,14 @@ var SwapModule = class {
6915
7555
  const pathCount = item.basePaths.length;
6916
7556
  if (pathCount > 0) {
6917
7557
  const path = item.basePaths[0];
6918
- const feeRate = path.label === "Magma" ? new Decimal5(path.feeRate).div(10 ** 6) : new Decimal5(path.feeRate).div(10 ** 9);
7558
+ const feeRate = path.label === "Magma" ? new Decimal7(path.feeRate).div(10 ** 6) : new Decimal7(path.feeRate).div(10 ** 9);
6919
7559
  const feeAmount = d(path.inputAmount).div(10 ** path.fromDecimal).mul(feeRate);
6920
7560
  fee = fee.add(feeAmount);
6921
7561
  if (pathCount > 1) {
6922
7562
  const path2 = item.basePaths[1];
6923
- const price1 = path.direction ? path.currentPrice : new Decimal5(1).div(path.currentPrice);
6924
- const price2 = path2.direction ? path2.currentPrice : new Decimal5(1).div(path2.currentPrice);
6925
- const feeRate2 = path2.label === "Magma" ? new Decimal5(path2.feeRate).div(10 ** 6) : new Decimal5(path2.feeRate).div(10 ** 9);
7563
+ const price1 = path.direction ? path.currentPrice : new Decimal7(1).div(path.currentPrice);
7564
+ const price2 = path2.direction ? path2.currentPrice : new Decimal7(1).div(path2.currentPrice);
7565
+ const feeRate2 = path2.label === "Magma" ? new Decimal7(path2.feeRate).div(10 ** 6) : new Decimal7(path2.feeRate).div(10 ** 9);
6926
7566
  const feeAmount2 = d(path2.outputAmount).div(10 ** path2.toDecimal).mul(feeRate2);
6927
7567
  const fee2 = feeAmount2.div(price1.mul(price2));
6928
7568
  fee = fee.add(fee2);
@@ -6940,17 +7580,17 @@ var SwapModule = class {
6940
7580
  const outputAmount = d(path.outputAmount).div(10 ** path.toDecimal);
6941
7581
  const inputAmount = d(path.inputAmount).div(10 ** path.fromDecimal);
6942
7582
  const rate = outputAmount.div(inputAmount);
6943
- const cprice = path.direction ? new Decimal5(path.currentPrice) : new Decimal5(1).div(path.currentPrice);
7583
+ const cprice = path.direction ? new Decimal7(path.currentPrice) : new Decimal7(1).div(path.currentPrice);
6944
7584
  impactValue = impactValue.add(this.calculateSingleImpact(rate, cprice));
6945
7585
  }
6946
7586
  if (pathCount === 2) {
6947
7587
  const path = item.basePaths[0];
6948
7588
  const path2 = item.basePaths[1];
6949
- const cprice1 = path.direction ? new Decimal5(path.currentPrice) : new Decimal5(1).div(path.currentPrice);
6950
- const cprice2 = path2.direction ? new Decimal5(path2.currentPrice) : new Decimal5(1).div(path2.currentPrice);
7589
+ const cprice1 = path.direction ? new Decimal7(path.currentPrice) : new Decimal7(1).div(path.currentPrice);
7590
+ const cprice2 = path2.direction ? new Decimal7(path2.currentPrice) : new Decimal7(1).div(path2.currentPrice);
6951
7591
  const cprice = cprice1.mul(cprice2);
6952
- const outputAmount = new Decimal5(path2.outputAmount).div(10 ** path2.toDecimal);
6953
- const inputAmount = new Decimal5(path.inputAmount).div(10 ** path.fromDecimal);
7592
+ const outputAmount = new Decimal7(path2.outputAmount).div(10 ** path2.toDecimal);
7593
+ const inputAmount = new Decimal7(path.inputAmount).div(10 ** path.fromDecimal);
6954
7594
  const rate = outputAmount.div(inputAmount);
6955
7595
  impactValue = impactValue.add(this.calculateSingleImpact(rate, cprice));
6956
7596
  }
@@ -7012,13 +7652,13 @@ var SwapModule = class {
7012
7652
  continue;
7013
7653
  }
7014
7654
  if (params.byAmountIn) {
7015
- const amount = new BN18(valueData[i].parsedJson.data.amount_out);
7655
+ const amount = new BN20(valueData[i].parsedJson.data.amount_out);
7016
7656
  if (amount.gt(tempMaxAmount)) {
7017
7657
  tempIndex = i;
7018
7658
  tempMaxAmount = amount;
7019
7659
  }
7020
7660
  } else {
7021
- const amount = new BN18(valueData[i].parsedJson.data.amount_out);
7661
+ const amount = new BN20(valueData[i].parsedJson.data.amount_out);
7022
7662
  if (amount.lt(tempMaxAmount)) {
7023
7663
  tempIndex = i;
7024
7664
  tempMaxAmount = amount;
@@ -7082,7 +7722,7 @@ var SwapModule = class {
7082
7722
  return this.transformSwapData(params, valueData[0].parsedJson.data);
7083
7723
  }
7084
7724
  transformSwapData(params, data) {
7085
- const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN18(data.amount_in).add(new BN18(data.fee_amount)).toString() : "";
7725
+ const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN20(data.amount_in).add(new BN20(data.fee_amount)).toString() : "";
7086
7726
  return {
7087
7727
  poolAddress: params.pool.poolAddress,
7088
7728
  currentSqrtPrice: params.currentSqrtPrice,
@@ -7099,7 +7739,7 @@ var SwapModule = class {
7099
7739
  transformSwapWithMultiPoolData(params, jsonData) {
7100
7740
  const { data } = jsonData;
7101
7741
  console.log("json data. ", data);
7102
- const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN18(data.amount_in).add(new BN18(data.fee_amount)).toString() : "";
7742
+ const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN20(data.amount_in).add(new BN20(data.fee_amount)).toString() : "";
7103
7743
  return {
7104
7744
  poolAddress: params.poolAddress,
7105
7745
  estimatedAmountIn,
@@ -8612,8 +9252,8 @@ var TokenModule = class {
8612
9252
  };
8613
9253
 
8614
9254
  // src/modules/routerModuleV2.ts
8615
- import BN19 from "bn.js";
8616
- import Decimal6 from "decimal.js";
9255
+ import BN21 from "bn.js";
9256
+ import Decimal8 from "decimal.js";
8617
9257
  import { v4 as uuidv4 } from "uuid";
8618
9258
  import axios from "axios";
8619
9259
  var RouterModuleV2 = class {
@@ -8630,7 +9270,7 @@ var RouterModuleV2 = class {
8630
9270
  if (label === "Magma") {
8631
9271
  return TickMath.sqrtPriceX64ToPrice(currentSqrtPrice, decimalA, decimalB);
8632
9272
  }
8633
- return new Decimal6(currentSqrtPrice.toString()).div(new Decimal6(10).pow(new Decimal6(decimalB + 9 - decimalA)));
9273
+ return new Decimal8(currentSqrtPrice.toString()).div(new Decimal8(10).pow(new Decimal8(decimalB + 9 - decimalA)));
8634
9274
  }
8635
9275
  parseJsonResult(data) {
8636
9276
  const result = {
@@ -8656,12 +9296,12 @@ var RouterModuleV2 = class {
8656
9296
  outputAmount: basePath.output_amount,
8657
9297
  inputAmount: basePath.input_amount,
8658
9298
  feeRate: basePath.fee_rate,
8659
- currentSqrtPrice: new BN19(basePath.current_sqrt_price.toString()),
8660
- afterSqrtPrice: basePath.label === "Magma" ? new BN19(basePath.after_sqrt_price.toString()) : ZERO,
9299
+ currentSqrtPrice: new BN21(basePath.current_sqrt_price.toString()),
9300
+ afterSqrtPrice: basePath.label === "Magma" ? new BN21(basePath.after_sqrt_price.toString()) : ZERO,
8661
9301
  fromDecimal: basePath.from_decimal,
8662
9302
  toDecimal: basePath.to_decimal,
8663
9303
  currentPrice: this.calculatePrice(
8664
- new BN19(basePath.current_sqrt_price.toString()),
9304
+ new BN21(basePath.current_sqrt_price.toString()),
8665
9305
  basePath.from_decimal,
8666
9306
  basePath.to_decimal,
8667
9307
  basePath.direction,
@@ -8744,7 +9384,7 @@ var RouterModuleV2 = class {
8744
9384
  const priceResult = await this.sdk.Router.priceUseV1(
8745
9385
  from,
8746
9386
  to,
8747
- new BN19(amount),
9387
+ new BN21(amount),
8748
9388
  byAmountIn,
8749
9389
  priceSplitPoint,
8750
9390
  partner,
@@ -8756,7 +9396,7 @@ var RouterModuleV2 = class {
8756
9396
  if (path.poolAddress.length > 1) {
8757
9397
  const fromDecimal0 = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
8758
9398
  const toDecimal0 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8759
- const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
9399
+ const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
8760
9400
  const path0 = {
8761
9401
  direction: path.a2b[0],
8762
9402
  label: "Magma",
@@ -8773,7 +9413,7 @@ var RouterModuleV2 = class {
8773
9413
  };
8774
9414
  const fromDecimal1 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8775
9415
  const toDecimal1 = this.sdk.Router.tokenInfo(path.coinType[2]).decimals;
8776
- const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
9416
+ const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
8777
9417
  const path1 = {
8778
9418
  direction: path.a2b[1],
8779
9419
  label: "Magma",
@@ -8792,7 +9432,7 @@ var RouterModuleV2 = class {
8792
9432
  } else {
8793
9433
  const fromDecimal = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
8794
9434
  const toDecimal = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8795
- const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
9435
+ const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
8796
9436
  const path0 = {
8797
9437
  direction: path.a2b[0],
8798
9438
  label: "Magma",
@@ -8877,7 +9517,7 @@ var RouterModuleV2 = class {
8877
9517
  const priceResult = await this.sdk.Router.priceUseV1(
8878
9518
  from,
8879
9519
  to,
8880
- new BN19(amount),
9520
+ new BN21(amount),
8881
9521
  byAmountIn,
8882
9522
  priceSplitPoint,
8883
9523
  partner,
@@ -8889,7 +9529,7 @@ var RouterModuleV2 = class {
8889
9529
  if (path.poolAddress.length > 1) {
8890
9530
  const fromDecimal0 = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
8891
9531
  const toDecimal0 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8892
- const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
9532
+ const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
8893
9533
  const path0 = {
8894
9534
  direction: path.a2b[0],
8895
9535
  label: "Magma",
@@ -8906,7 +9546,7 @@ var RouterModuleV2 = class {
8906
9546
  };
8907
9547
  const fromDecimal1 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8908
9548
  const toDecimal1 = this.sdk.Router.tokenInfo(path.coinType[2]).decimals;
8909
- const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
9549
+ const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
8910
9550
  const path1 = {
8911
9551
  direction: path.a2b[1],
8912
9552
  label: "Magma",
@@ -8925,7 +9565,7 @@ var RouterModuleV2 = class {
8925
9565
  } else {
8926
9566
  const fromDecimal = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
8927
9567
  const toDecimal = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
8928
- const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
9568
+ const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN21(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
8929
9569
  const path0 = {
8930
9570
  direction: path.a2b[0],
8931
9571
  label: "Magma",
@@ -9849,7 +10489,7 @@ var GaugeModule = class {
9849
10489
 
9850
10490
  // src/modules/dlmm.ts
9851
10491
  import { Transaction as Transaction12 } from "@mysten/sui/transactions";
9852
- import Decimal7 from "decimal.js";
10492
+ import Decimal9 from "decimal.js";
9853
10493
  import { get_real_id_from_price, get_storage_id_from_real_id } from "@magmaprotocol/calc_dlmm";
9854
10494
  var DlmmModule = class {
9855
10495
  _sdk;
@@ -9922,9 +10562,9 @@ var DlmmModule = class {
9922
10562
  }
9923
10563
  // params price: input (b/(10^b_decimal))/(a/(10^a_decimal))
9924
10564
  price_to_storage_id(price, bin_step, tokenADecimal, tokenBDecimal) {
9925
- const priceDec = new Decimal7(price);
9926
- const tenDec = new Decimal7(10);
9927
- const twoDec = new Decimal7(2);
10565
+ const priceDec = new Decimal9(price);
10566
+ const tenDec = new Decimal9(10);
10567
+ const twoDec = new Decimal9(2);
9928
10568
  const price_x128 = priceDec.mul(tenDec.pow(tokenBDecimal)).div(tenDec.pow(tokenADecimal)).mul(twoDec.pow(128));
9929
10569
  const active_id = get_real_id_from_price(price_x128.toFixed(0).toString(), bin_step);
9930
10570
  return get_storage_id_from_real_id(active_id);
@@ -10637,6 +11277,7 @@ export {
10637
11277
  SUI_SYSTEM_STATE_OBJECT_ID,
10638
11278
  SplitSwap,
10639
11279
  SplitUnit,
11280
+ StrategyType,
10640
11281
  SwapDirection,
10641
11282
  SwapModule,
10642
11283
  SwapUtils,
@@ -10658,6 +11299,10 @@ export {
10658
11299
  adjustForSlippage,
10659
11300
  asIntN,
10660
11301
  asUintN,
11302
+ autoFillXByStrategy,
11303
+ autoFillXByWeight,
11304
+ autoFillYByStrategy,
11305
+ autoFillYByWeight,
10661
11306
  bufferToHex,
10662
11307
  buildClmmPositionName,
10663
11308
  buildNFT,
@@ -10696,12 +11341,14 @@ export {
10696
11341
  getAmountUnfixedDelta,
10697
11342
  getCoinAFromLiquidity,
10698
11343
  getCoinBFromLiquidity,
11344
+ getCoinXYForLiquidity,
10699
11345
  getDefaultSuiInputType,
10700
11346
  getDeltaA,
10701
11347
  getDeltaB,
10702
11348
  getDeltaDownFromOutput,
10703
11349
  getDeltaUpFromInput,
10704
11350
  getFutureTime,
11351
+ getLiquidityAndCoinYByCoinX,
10705
11352
  getLiquidityFromCoinA,
10706
11353
  getLiquidityFromCoinB,
10707
11354
  getLowerSqrtPriceFromCoinA,
@@ -10725,6 +11372,7 @@ export {
10725
11372
  getObjectType,
10726
11373
  getObjectVersion,
10727
11374
  getPackagerConfigs,
11375
+ getPriceOfBinByBinId,
10728
11376
  getRewardInTickRange,
10729
11377
  getSuiObjectData,
10730
11378
  getTickDataFromUrlData,
@@ -10748,10 +11396,15 @@ export {
10748
11396
  shortAddress,
10749
11397
  shortString,
10750
11398
  tickScore,
11399
+ toAmountAskSide,
11400
+ toAmountBidSide,
11401
+ toAmountBothSide,
11402
+ toAmountsBothSideByStrategy,
10751
11403
  toBuffer,
10752
11404
  toCoinAmount,
10753
11405
  toDecimalsAmount,
10754
11406
  transClmmpoolDataWithoutTicks,
10755
- utf8to16
11407
+ utf8to16,
11408
+ withLiquiditySlippage
10756
11409
  };
10757
11410
  //# sourceMappingURL=index.mjs.map