@magmaprotocol/magma-clmm-sdk 0.5.75 → 0.5.77
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/README.md +6 -10
- package/dist/index.d.ts +2 -424
- package/dist/index.js +145 -1594
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +144 -1583
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -37,7 +37,7 @@ var CachedContent = class {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
// src/utils/common.ts
|
|
40
|
-
import
|
|
40
|
+
import BN11 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";
|
|
@@ -303,7 +303,6 @@ var Voter = "voter";
|
|
|
303
303
|
var RewardDistributor = "reward_distributor";
|
|
304
304
|
var Gauge = "gauge";
|
|
305
305
|
var Minter = "minter";
|
|
306
|
-
var DlmmScript = "dlmm_script";
|
|
307
306
|
var CoinInfoAddress = "0x1::coin::CoinInfo";
|
|
308
307
|
var CoinStoreAddress = "0x1::coin::CoinStore";
|
|
309
308
|
var DeepbookCustodianV2Moudle = "custodian_v2";
|
|
@@ -1592,476 +1591,8 @@ function collectFeesQuote(param) {
|
|
|
1592
1591
|
return updateFees(param.position, fee_growth_inside_a, fee_growth_inside_b);
|
|
1593
1592
|
}
|
|
1594
1593
|
|
|
1595
|
-
// src/math/dlmmWeightToAmounts.ts
|
|
1596
|
-
import BN9 from "bn.js";
|
|
1597
|
-
import Decimal3 from "decimal.js";
|
|
1598
|
-
import { get_price_x128_from_real_id } from "@magmaprotocol/calc_dlmm";
|
|
1599
|
-
function getPriceOfBinByBinId(binId, binStep) {
|
|
1600
|
-
const twoDec = new Decimal3(2);
|
|
1601
|
-
const price = new Decimal3(get_price_x128_from_real_id(binId, binStep));
|
|
1602
|
-
return price.div(twoDec.pow(128));
|
|
1603
|
-
}
|
|
1604
|
-
function autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, distributions) {
|
|
1605
|
-
const activeBins = distributions.filter((element) => {
|
|
1606
|
-
return element.binId === activeId;
|
|
1607
|
-
});
|
|
1608
|
-
if (activeBins.length === 1) {
|
|
1609
|
-
const p0 = getPriceOfBinByBinId(activeId, binStep);
|
|
1610
|
-
let wx0 = new Decimal3(0);
|
|
1611
|
-
let wy0 = new Decimal3(0);
|
|
1612
|
-
const activeBin = activeBins[0];
|
|
1613
|
-
if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
|
|
1614
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
|
|
1615
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
|
|
1616
|
-
} else {
|
|
1617
|
-
const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
|
|
1618
|
-
const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
|
|
1619
|
-
if (!amountXInActiveBin.isZero()) {
|
|
1620
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
|
|
1621
|
-
}
|
|
1622
|
-
if (!amountYInActiveBin.isZero()) {
|
|
1623
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
|
|
1624
|
-
}
|
|
1625
|
-
}
|
|
1626
|
-
let totalWeightX2 = wx0;
|
|
1627
|
-
let totalWeightY2 = wy0;
|
|
1628
|
-
distributions.forEach((element) => {
|
|
1629
|
-
if (element.binId < activeId) {
|
|
1630
|
-
totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
|
|
1631
|
-
}
|
|
1632
|
-
if (element.binId > activeId) {
|
|
1633
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1634
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1635
|
-
totalWeightX2 = totalWeightX2.add(weighPerPrice);
|
|
1636
|
-
}
|
|
1637
|
-
});
|
|
1638
|
-
const kx2 = totalWeightX2.isZero() ? new Decimal3(1) : new Decimal3(amountX.toString()).div(totalWeightX2);
|
|
1639
|
-
const amountY2 = kx2.mul(totalWeightY2);
|
|
1640
|
-
return new BN9(amountY2.floor().toString());
|
|
1641
|
-
}
|
|
1642
|
-
let totalWeightX = new Decimal3(0);
|
|
1643
|
-
let totalWeightY = new Decimal3(0);
|
|
1644
|
-
distributions.forEach((element) => {
|
|
1645
|
-
if (element.binId < activeId) {
|
|
1646
|
-
totalWeightY = totalWeightY.add(new Decimal3(element.weight));
|
|
1647
|
-
} else {
|
|
1648
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1649
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1650
|
-
totalWeightX = totalWeightX.add(weighPerPrice);
|
|
1651
|
-
}
|
|
1652
|
-
});
|
|
1653
|
-
const kx = totalWeightX.isZero() ? new Decimal3(1) : new Decimal3(amountX.toString()).div(totalWeightX);
|
|
1654
|
-
const amountY = kx.mul(totalWeightY);
|
|
1655
|
-
return new BN9(amountY.floor().toString());
|
|
1656
|
-
}
|
|
1657
|
-
function autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, distributions) {
|
|
1658
|
-
const activeBins = distributions.filter((element) => {
|
|
1659
|
-
return element.binId === activeId;
|
|
1660
|
-
});
|
|
1661
|
-
if (activeBins.length === 1) {
|
|
1662
|
-
const p0 = getPriceOfBinByBinId(activeId, binStep);
|
|
1663
|
-
let wx0 = new Decimal3(0);
|
|
1664
|
-
let wy0 = new Decimal3(0);
|
|
1665
|
-
const activeBin = activeBins[0];
|
|
1666
|
-
if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
|
|
1667
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
|
|
1668
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
|
|
1669
|
-
} else {
|
|
1670
|
-
const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
|
|
1671
|
-
const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
|
|
1672
|
-
if (!amountXInActiveBin.isZero()) {
|
|
1673
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
|
|
1674
|
-
}
|
|
1675
|
-
if (!amountYInActiveBin.isZero()) {
|
|
1676
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
|
|
1677
|
-
}
|
|
1678
|
-
}
|
|
1679
|
-
let totalWeightX2 = wx0;
|
|
1680
|
-
let totalWeightY2 = wy0;
|
|
1681
|
-
distributions.forEach((element) => {
|
|
1682
|
-
if (element.binId < activeId) {
|
|
1683
|
-
totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
|
|
1684
|
-
}
|
|
1685
|
-
if (element.binId > activeId) {
|
|
1686
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1687
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1688
|
-
totalWeightX2 = totalWeightX2.add(weighPerPrice);
|
|
1689
|
-
}
|
|
1690
|
-
});
|
|
1691
|
-
const ky2 = totalWeightY2.isZero() ? new Decimal3(1) : new Decimal3(amountY.toString()).div(totalWeightY2);
|
|
1692
|
-
const amountX2 = ky2.mul(totalWeightX2);
|
|
1693
|
-
return new BN9(amountX2.floor().toString());
|
|
1694
|
-
}
|
|
1695
|
-
let totalWeightX = new Decimal3(0);
|
|
1696
|
-
let totalWeightY = new Decimal3(0);
|
|
1697
|
-
distributions.forEach((element) => {
|
|
1698
|
-
if (element.binId < activeId) {
|
|
1699
|
-
totalWeightY = totalWeightY.add(new Decimal3(element.weight));
|
|
1700
|
-
} else {
|
|
1701
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1702
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1703
|
-
totalWeightX = totalWeightX.add(weighPerPrice);
|
|
1704
|
-
}
|
|
1705
|
-
});
|
|
1706
|
-
const ky = totalWeightY.isZero() ? new Decimal3(1) : new Decimal3(amountY.toString()).div(totalWeightY);
|
|
1707
|
-
const amountX = ky.mul(totalWeightX);
|
|
1708
|
-
return new BN9(amountX.floor().toString());
|
|
1709
|
-
}
|
|
1710
|
-
function toAmountBidSide(activeId, totalAmount, distributions) {
|
|
1711
|
-
const totalWeight = distributions.reduce((sum, el) => {
|
|
1712
|
-
return el.binId > activeId ? sum : sum.add(el.weight);
|
|
1713
|
-
}, new Decimal3(0));
|
|
1714
|
-
if (totalWeight.cmp(new Decimal3(0)) !== 1) {
|
|
1715
|
-
throw Error("Invalid parameteres");
|
|
1716
|
-
}
|
|
1717
|
-
return distributions.map((bin) => {
|
|
1718
|
-
if (bin.binId > activeId) {
|
|
1719
|
-
return {
|
|
1720
|
-
binId: bin.binId,
|
|
1721
|
-
amount: new BN9(0)
|
|
1722
|
-
};
|
|
1723
|
-
}
|
|
1724
|
-
return {
|
|
1725
|
-
binId: bin.binId,
|
|
1726
|
-
amount: new BN9(new Decimal3(totalAmount.toString()).mul(new Decimal3(bin.weight).div(totalWeight)).floor().toString())
|
|
1727
|
-
};
|
|
1728
|
-
});
|
|
1729
|
-
}
|
|
1730
|
-
function toAmountAskSide(activeId, binStep, totalAmount, distributions) {
|
|
1731
|
-
const totalWeight = distributions.reduce((sum, el) => {
|
|
1732
|
-
if (el.binId < activeId) {
|
|
1733
|
-
return sum;
|
|
1734
|
-
}
|
|
1735
|
-
const price = getPriceOfBinByBinId(el.binId, binStep);
|
|
1736
|
-
const weightPerPrice = new Decimal3(el.weight).div(price);
|
|
1737
|
-
return sum.add(weightPerPrice);
|
|
1738
|
-
}, new Decimal3(0));
|
|
1739
|
-
if (totalWeight.cmp(new Decimal3(0)) !== 1) {
|
|
1740
|
-
throw Error("Invalid parameteres");
|
|
1741
|
-
}
|
|
1742
|
-
return distributions.map((bin) => {
|
|
1743
|
-
if (bin.binId < activeId) {
|
|
1744
|
-
return {
|
|
1745
|
-
binId: bin.binId,
|
|
1746
|
-
amount: new BN9(0)
|
|
1747
|
-
};
|
|
1748
|
-
}
|
|
1749
|
-
const price = getPriceOfBinByBinId(bin.binId, binStep);
|
|
1750
|
-
const weightPerPrice = new Decimal3(bin.weight).div(price);
|
|
1751
|
-
return {
|
|
1752
|
-
binId: bin.binId,
|
|
1753
|
-
amount: new BN9(new Decimal3(totalAmount.toString()).mul(weightPerPrice).div(totalWeight).floor().toString())
|
|
1754
|
-
};
|
|
1755
|
-
});
|
|
1756
|
-
}
|
|
1757
|
-
function toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, distributions) {
|
|
1758
|
-
if (activeId > distributions[distributions.length - 1].binId) {
|
|
1759
|
-
const amounts = toAmountBidSide(activeId, amountY, distributions);
|
|
1760
|
-
return amounts.map((bin) => {
|
|
1761
|
-
return {
|
|
1762
|
-
binId: bin.binId,
|
|
1763
|
-
amountX: new BN9(0),
|
|
1764
|
-
amountY: bin.amount
|
|
1765
|
-
};
|
|
1766
|
-
});
|
|
1767
|
-
}
|
|
1768
|
-
if (activeId < distributions[0].binId) {
|
|
1769
|
-
const amounts = toAmountAskSide(activeId, binStep, amountX, distributions);
|
|
1770
|
-
return amounts.map((bin) => {
|
|
1771
|
-
return {
|
|
1772
|
-
binId: bin.binId,
|
|
1773
|
-
amountX: bin.amount,
|
|
1774
|
-
amountY: new BN9(0)
|
|
1775
|
-
};
|
|
1776
|
-
});
|
|
1777
|
-
}
|
|
1778
|
-
const activeBins = distributions.filter((element) => {
|
|
1779
|
-
return element.binId === activeId;
|
|
1780
|
-
});
|
|
1781
|
-
if (activeBins.length === 1) {
|
|
1782
|
-
const p0 = getPriceOfBinByBinId(activeId, binStep);
|
|
1783
|
-
let wx0 = new Decimal3(0);
|
|
1784
|
-
let wy0 = new Decimal3(0);
|
|
1785
|
-
const activeBin = activeBins[0];
|
|
1786
|
-
if (amountXInActiveBin.isZero() && amountYInActiveBin.isZero()) {
|
|
1787
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.mul(new Decimal3(2)));
|
|
1788
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(2));
|
|
1789
|
-
} else {
|
|
1790
|
-
const amountXInActiveBinDec = new Decimal3(amountXInActiveBin.toString());
|
|
1791
|
-
const amountYInActiveBinDec = new Decimal3(amountYInActiveBin.toString());
|
|
1792
|
-
if (!amountXInActiveBin.isZero()) {
|
|
1793
|
-
wx0 = new Decimal3(activeBin.weight).div(p0.add(amountYInActiveBinDec.div(amountXInActiveBinDec)));
|
|
1794
|
-
}
|
|
1795
|
-
if (!amountYInActiveBin.isZero()) {
|
|
1796
|
-
wy0 = new Decimal3(activeBin.weight).div(new Decimal3(1).add(p0.mul(amountXInActiveBinDec).div(amountYInActiveBinDec)));
|
|
1797
|
-
}
|
|
1798
|
-
}
|
|
1799
|
-
let totalWeightX2 = wx0;
|
|
1800
|
-
let totalWeightY2 = wy0;
|
|
1801
|
-
distributions.forEach((element) => {
|
|
1802
|
-
if (element.binId < activeId) {
|
|
1803
|
-
totalWeightY2 = totalWeightY2.add(new Decimal3(element.weight));
|
|
1804
|
-
}
|
|
1805
|
-
if (element.binId > activeId) {
|
|
1806
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1807
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1808
|
-
totalWeightX2 = totalWeightX2.add(weighPerPrice);
|
|
1809
|
-
}
|
|
1810
|
-
});
|
|
1811
|
-
const kx2 = new Decimal3(amountX.toString()).div(totalWeightX2);
|
|
1812
|
-
const ky2 = new Decimal3(amountY.toString()).div(totalWeightY2);
|
|
1813
|
-
const k2 = kx2.lessThan(ky2) ? kx2 : ky2;
|
|
1814
|
-
return distributions.map((bin) => {
|
|
1815
|
-
if (bin.binId < activeId) {
|
|
1816
|
-
const amount = k2.mul(new Decimal3(bin.weight));
|
|
1817
|
-
return {
|
|
1818
|
-
binId: bin.binId,
|
|
1819
|
-
amountX: new BN9(0),
|
|
1820
|
-
amountY: new BN9(amount.floor().toString())
|
|
1821
|
-
};
|
|
1822
|
-
}
|
|
1823
|
-
if (bin.binId > activeId) {
|
|
1824
|
-
const price = getPriceOfBinByBinId(bin.binId, binStep);
|
|
1825
|
-
const weighPerPrice = new Decimal3(bin.weight).div(price);
|
|
1826
|
-
const amount = k2.mul(weighPerPrice);
|
|
1827
|
-
return {
|
|
1828
|
-
binId: bin.binId,
|
|
1829
|
-
amountX: new BN9(amount.floor().toString()),
|
|
1830
|
-
amountY: new BN9(0)
|
|
1831
|
-
};
|
|
1832
|
-
}
|
|
1833
|
-
const amountXActiveBin = k2.mul(wx0);
|
|
1834
|
-
const amountYActiveBin = k2.mul(wy0);
|
|
1835
|
-
return {
|
|
1836
|
-
binId: bin.binId,
|
|
1837
|
-
amountX: new BN9(amountXActiveBin.floor().toString()),
|
|
1838
|
-
amountY: new BN9(amountYActiveBin.floor().toString())
|
|
1839
|
-
};
|
|
1840
|
-
});
|
|
1841
|
-
}
|
|
1842
|
-
let totalWeightX = new Decimal3(0);
|
|
1843
|
-
let totalWeightY = new Decimal3(0);
|
|
1844
|
-
distributions.forEach((element) => {
|
|
1845
|
-
if (element.binId < activeId) {
|
|
1846
|
-
totalWeightY = totalWeightY.add(new Decimal3(element.weight));
|
|
1847
|
-
} else {
|
|
1848
|
-
const price = getPriceOfBinByBinId(element.binId, binStep);
|
|
1849
|
-
const weighPerPrice = new Decimal3(element.weight).div(price);
|
|
1850
|
-
totalWeightX = totalWeightX.add(weighPerPrice);
|
|
1851
|
-
}
|
|
1852
|
-
});
|
|
1853
|
-
const kx = new Decimal3(amountX.toString()).div(totalWeightX);
|
|
1854
|
-
const ky = new Decimal3(amountY.toString()).div(totalWeightY);
|
|
1855
|
-
const k = kx.lessThan(ky) ? kx : ky;
|
|
1856
|
-
return distributions.map((bin) => {
|
|
1857
|
-
if (bin.binId < activeId) {
|
|
1858
|
-
const amount2 = k.mul(new Decimal3(bin.weight));
|
|
1859
|
-
return {
|
|
1860
|
-
binId: bin.binId,
|
|
1861
|
-
amountX: new BN9(0),
|
|
1862
|
-
amountY: new BN9(amount2.floor().toString())
|
|
1863
|
-
};
|
|
1864
|
-
}
|
|
1865
|
-
const price = getPriceOfBinByBinId(bin.binId, binStep);
|
|
1866
|
-
const weighPerPrice = new Decimal3(bin.weight).div(price);
|
|
1867
|
-
const amount = k.mul(weighPerPrice);
|
|
1868
|
-
return {
|
|
1869
|
-
binId: bin.binId,
|
|
1870
|
-
amountX: new BN9(amount.floor().toString()),
|
|
1871
|
-
amountY: new BN9(0)
|
|
1872
|
-
};
|
|
1873
|
-
});
|
|
1874
|
-
}
|
|
1875
|
-
|
|
1876
|
-
// src/math/dlmmStrategy.ts
|
|
1877
|
-
var StrategyType = /* @__PURE__ */ ((StrategyType2) => {
|
|
1878
|
-
StrategyType2[StrategyType2["Spot"] = 1] = "Spot";
|
|
1879
|
-
StrategyType2[StrategyType2["Curve"] = 2] = "Curve";
|
|
1880
|
-
StrategyType2[StrategyType2["BidAsk"] = 3] = "BidAsk";
|
|
1881
|
-
return StrategyType2;
|
|
1882
|
-
})(StrategyType || {});
|
|
1883
|
-
function toWeightSpotBalanced(minBinId, maxBinId) {
|
|
1884
|
-
const distributions = [];
|
|
1885
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
1886
|
-
distributions.push({
|
|
1887
|
-
binId: i,
|
|
1888
|
-
weight: 1
|
|
1889
|
-
});
|
|
1890
|
-
}
|
|
1891
|
-
return distributions;
|
|
1892
|
-
}
|
|
1893
|
-
var DEFAULT_MAX_WEIGHT = 2e3;
|
|
1894
|
-
var DEFAULT_MIN_WEIGHT = 200;
|
|
1895
|
-
function toWeightCurve(minBinId, maxBinId, activeId) {
|
|
1896
|
-
if (activeId < minBinId || activeId > maxBinId) {
|
|
1897
|
-
throw new ClmmpoolsError("Invalid strategy params", "InvalidParams" /* InvalidParams */);
|
|
1898
|
-
}
|
|
1899
|
-
const maxWeight = DEFAULT_MAX_WEIGHT;
|
|
1900
|
-
const minWeight = DEFAULT_MIN_WEIGHT;
|
|
1901
|
-
const diffWeight = maxWeight - minWeight;
|
|
1902
|
-
const diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
|
|
1903
|
-
const diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
|
|
1904
|
-
const distributions = [];
|
|
1905
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
1906
|
-
if (i < activeId) {
|
|
1907
|
-
distributions.push({
|
|
1908
|
-
binId: i,
|
|
1909
|
-
weight: maxWeight - (activeId - i) * diffMinWeight
|
|
1910
|
-
});
|
|
1911
|
-
} else if (i > activeId) {
|
|
1912
|
-
distributions.push({
|
|
1913
|
-
binId: i,
|
|
1914
|
-
weight: maxWeight - (i - activeId) * diffMaxWeight
|
|
1915
|
-
});
|
|
1916
|
-
} else {
|
|
1917
|
-
distributions.push({
|
|
1918
|
-
binId: i,
|
|
1919
|
-
weight: maxWeight
|
|
1920
|
-
});
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
return distributions;
|
|
1924
|
-
}
|
|
1925
|
-
function toWeightBidAsk(minBinId, maxBinId, activeId) {
|
|
1926
|
-
if (activeId < minBinId || activeId > maxBinId) {
|
|
1927
|
-
throw new ClmmpoolsError("Invalid strategy params", "InvalidParams" /* InvalidParams */);
|
|
1928
|
-
}
|
|
1929
|
-
const maxWeight = DEFAULT_MAX_WEIGHT;
|
|
1930
|
-
const minWeight = DEFAULT_MIN_WEIGHT;
|
|
1931
|
-
const diffWeight = maxWeight - minWeight;
|
|
1932
|
-
const diffMinWeight = activeId > minBinId ? Math.floor(diffWeight / (activeId - minBinId)) : 0;
|
|
1933
|
-
const diffMaxWeight = maxBinId > activeId ? Math.floor(diffWeight / (maxBinId - activeId)) : 0;
|
|
1934
|
-
const distributions = [];
|
|
1935
|
-
for (let i = minBinId; i <= maxBinId; i++) {
|
|
1936
|
-
if (i < activeId) {
|
|
1937
|
-
distributions.push({
|
|
1938
|
-
binId: i,
|
|
1939
|
-
weight: minWeight + (activeId - i) * diffMinWeight
|
|
1940
|
-
});
|
|
1941
|
-
} else if (i > activeId) {
|
|
1942
|
-
distributions.push({
|
|
1943
|
-
binId: i,
|
|
1944
|
-
weight: minWeight + (i - activeId) * diffMaxWeight
|
|
1945
|
-
});
|
|
1946
|
-
} else {
|
|
1947
|
-
distributions.push({
|
|
1948
|
-
binId: i,
|
|
1949
|
-
weight: minWeight
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1952
|
-
}
|
|
1953
|
-
return distributions;
|
|
1954
|
-
}
|
|
1955
|
-
function autoFillYByStrategy(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
|
|
1956
|
-
switch (strategyType) {
|
|
1957
|
-
case 1 /* Spot */: {
|
|
1958
|
-
const weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
1959
|
-
return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1960
|
-
}
|
|
1961
|
-
case 2 /* Curve */: {
|
|
1962
|
-
const weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
1963
|
-
return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1964
|
-
}
|
|
1965
|
-
case 3 /* BidAsk */: {
|
|
1966
|
-
const weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
1967
|
-
return autoFillYByWeight(activeId, binStep, amountX, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1968
|
-
}
|
|
1969
|
-
default:
|
|
1970
|
-
throw new Error(`Unsupported strategy type: ${strategyType}`);
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
function autoFillXByStrategy(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, minBinId, maxBinId, strategyType) {
|
|
1974
|
-
switch (strategyType) {
|
|
1975
|
-
case 1 /* Spot */: {
|
|
1976
|
-
const weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
1977
|
-
return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1978
|
-
}
|
|
1979
|
-
case 2 /* Curve */: {
|
|
1980
|
-
const weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
1981
|
-
return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1982
|
-
}
|
|
1983
|
-
case 3 /* BidAsk */: {
|
|
1984
|
-
const weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
1985
|
-
return autoFillXByWeight(activeId, binStep, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1986
|
-
}
|
|
1987
|
-
default:
|
|
1988
|
-
throw new Error(`Unsupported strategy type: ${strategyType}`);
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amountX, amountY, amountXInActiveBin, amountYInActiveBin, strategyType) {
|
|
1992
|
-
const isSingleSideX = amountY.isZero();
|
|
1993
|
-
switch (strategyType) {
|
|
1994
|
-
case 1 /* Spot */: {
|
|
1995
|
-
const weights = toWeightSpotBalanced(minBinId, maxBinId);
|
|
1996
|
-
return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
1997
|
-
}
|
|
1998
|
-
case 2 /* Curve */: {
|
|
1999
|
-
const weights = toWeightCurve(minBinId, maxBinId, activeId);
|
|
2000
|
-
return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
2001
|
-
}
|
|
2002
|
-
case 3 /* BidAsk */: {
|
|
2003
|
-
const weights = toWeightBidAsk(minBinId, maxBinId, activeId);
|
|
2004
|
-
return toAmountBothSide(activeId, binStep, amountX, amountY, amountXInActiveBin, amountYInActiveBin, weights);
|
|
2005
|
-
}
|
|
2006
|
-
default:
|
|
2007
|
-
throw new Error(`Unsupported strategy type: ${strategyType}`);
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
|
|
2011
|
-
// src/math/LiquidityHelper.ts
|
|
2012
|
-
import Decimal5 from "decimal.js";
|
|
2013
|
-
|
|
2014
|
-
// src/utils/numbers.ts
|
|
2015
|
-
import Decimal4 from "decimal.js";
|
|
2016
|
-
function d(value) {
|
|
2017
|
-
if (Decimal4.isDecimal(value)) {
|
|
2018
|
-
return value;
|
|
2019
|
-
}
|
|
2020
|
-
return new Decimal4(value === void 0 ? 0 : value);
|
|
2021
|
-
}
|
|
2022
|
-
function decimalsMultiplier(decimals) {
|
|
2023
|
-
return d(10).pow(d(decimals).abs());
|
|
2024
|
-
}
|
|
2025
|
-
|
|
2026
|
-
// src/math/LiquidityHelper.ts
|
|
2027
|
-
function withLiquiditySlippage(value, slippage, mode) {
|
|
2028
|
-
return d(value)[mode](d(value).mul(slippage)).toDP(0);
|
|
2029
|
-
}
|
|
2030
|
-
function getLiquidityAndCoinYByCoinX(coinInVal, reserveInSize, reserveOutSize, lpSupply) {
|
|
2031
|
-
if (coinInVal.lessThanOrEqualTo(0)) {
|
|
2032
|
-
throw new ClmmpoolsError("coinInVal is less than zero", "InvalidCoinAmount" /* InvalidCoinAmount */);
|
|
2033
|
-
}
|
|
2034
|
-
if (reserveInSize.lessThanOrEqualTo(0) || reserveOutSize.lessThanOrEqualTo(0)) {
|
|
2035
|
-
return -1;
|
|
2036
|
-
}
|
|
2037
|
-
const coinYAmount = coinInVal.mul(reserveOutSize).div(reserveInSize);
|
|
2038
|
-
const sqrtSupply = lpSupply;
|
|
2039
|
-
const lpX = coinInVal.div(reserveInSize).mul(sqrtSupply);
|
|
2040
|
-
const lpY = coinYAmount.div(reserveOutSize).mul(sqrtSupply);
|
|
2041
|
-
const lpAmount = Decimal5.min(lpX, lpY);
|
|
2042
|
-
return {
|
|
2043
|
-
coinYAmount,
|
|
2044
|
-
lpAmount
|
|
2045
|
-
};
|
|
2046
|
-
}
|
|
2047
|
-
function getCoinXYForLiquidity(liquidity, reserveInSize, reserveOutSize, lpSuply) {
|
|
2048
|
-
if (liquidity.lessThanOrEqualTo(0)) {
|
|
2049
|
-
throw new ClmmpoolsError("liquidity can't be equal or less than zero", "InvalidLiquidityAmount" /* InvalidLiquidityAmount */);
|
|
2050
|
-
}
|
|
2051
|
-
if (reserveInSize.lessThanOrEqualTo(0) || reserveOutSize.lessThanOrEqualTo(0)) {
|
|
2052
|
-
throw new ClmmpoolsError("reserveInSize or reserveOutSize can not be equal or less than zero", "InvalidReserveAmount" /* InvalidReserveAmount */);
|
|
2053
|
-
}
|
|
2054
|
-
const sqrtSupply = lpSuply;
|
|
2055
|
-
const coinXAmount = liquidity.div(sqrtSupply).mul(reserveInSize);
|
|
2056
|
-
const coinYAmount = liquidity.div(sqrtSupply).mul(reserveOutSize);
|
|
2057
|
-
return {
|
|
2058
|
-
coinXAmount,
|
|
2059
|
-
coinYAmount
|
|
2060
|
-
};
|
|
2061
|
-
}
|
|
2062
|
-
|
|
2063
1594
|
// src/math/percentage.ts
|
|
2064
|
-
import
|
|
1595
|
+
import BN9 from "bn.js";
|
|
2065
1596
|
var Percentage = class {
|
|
2066
1597
|
numerator;
|
|
2067
1598
|
denominator;
|
|
@@ -2089,8 +1620,8 @@ var Percentage = class {
|
|
|
2089
1620
|
* @returns
|
|
2090
1621
|
*/
|
|
2091
1622
|
static fromFraction(numerator, denominator) {
|
|
2092
|
-
const num = typeof numerator === "number" ? new
|
|
2093
|
-
const denom = typeof denominator === "number" ? new
|
|
1623
|
+
const num = typeof numerator === "number" ? new BN9(numerator.toString()) : numerator;
|
|
1624
|
+
const denom = typeof denominator === "number" ? new BN9(denominator.toString()) : denominator;
|
|
2094
1625
|
return new Percentage(num, denom);
|
|
2095
1626
|
}
|
|
2096
1627
|
};
|
|
@@ -2190,7 +1721,7 @@ function adjustForCoinSlippage(tokenAmount, slippage, adjustUp) {
|
|
|
2190
1721
|
}
|
|
2191
1722
|
|
|
2192
1723
|
// src/math/SplitSwap.ts
|
|
2193
|
-
import
|
|
1724
|
+
import BN10 from "bn.js";
|
|
2194
1725
|
var SplitUnit = /* @__PURE__ */ ((SplitUnit2) => {
|
|
2195
1726
|
SplitUnit2[SplitUnit2["FIVE"] = 5] = "FIVE";
|
|
2196
1727
|
SplitUnit2[SplitUnit2["TEN"] = 10] = "TEN";
|
|
@@ -2248,7 +1779,7 @@ function updateSplitSwapResult(maxIndex, currentIndex, splitSwapResult, stepResu
|
|
|
2248
1779
|
return splitSwapResult;
|
|
2249
1780
|
}
|
|
2250
1781
|
function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
|
|
2251
|
-
let currentLiquidity = new
|
|
1782
|
+
let currentLiquidity = new BN10(poolData.liquidity);
|
|
2252
1783
|
let { currentSqrtPrice } = poolData;
|
|
2253
1784
|
let splitSwapResult = {
|
|
2254
1785
|
amountInArray: [],
|
|
@@ -2311,7 +1842,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
|
|
|
2311
1842
|
targetSqrtPrice,
|
|
2312
1843
|
currentLiquidity,
|
|
2313
1844
|
remainerAmount,
|
|
2314
|
-
new
|
|
1845
|
+
new BN10(poolData.feeRate),
|
|
2315
1846
|
byAmountIn
|
|
2316
1847
|
);
|
|
2317
1848
|
tempStepResult = stepResult;
|
|
@@ -2323,7 +1854,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
|
|
|
2323
1854
|
splitSwapResult.amountOutArray[i] = splitSwapResult.amountOutArray[i].add(stepResult.amountOut);
|
|
2324
1855
|
splitSwapResult.feeAmountArray[i] = splitSwapResult.feeAmountArray[i].add(stepResult.feeAmount);
|
|
2325
1856
|
if (stepResult.nextSqrtPrice.eq(tick.sqrtPrice)) {
|
|
2326
|
-
signedLiquidityChange = a2b ? tick.liquidityNet.mul(new
|
|
1857
|
+
signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN10(-1)) : tick.liquidityNet;
|
|
2327
1858
|
currentLiquidity = signedLiquidityChange.gt(ZERO) ? currentLiquidity.add(signedLiquidityChange) : currentLiquidity.sub(signedLiquidityChange.abs());
|
|
2328
1859
|
currentSqrtPrice = tick.sqrtPrice;
|
|
2329
1860
|
} else {
|
|
@@ -2348,7 +1879,7 @@ function computeSplitSwap(a2b, byAmountIn, amounts, poolData, swapTicks) {
|
|
|
2348
1879
|
break;
|
|
2349
1880
|
}
|
|
2350
1881
|
if (tempStepResult.nextSqrtPrice.eq(tick.sqrtPrice)) {
|
|
2351
|
-
signedLiquidityChange = a2b ? tick.liquidityNet.mul(new
|
|
1882
|
+
signedLiquidityChange = a2b ? tick.liquidityNet.mul(new BN10(-1)) : tick.liquidityNet;
|
|
2352
1883
|
currentLiquidity = signedLiquidityChange.gt(ZERO) ? currentLiquidity.add(signedLiquidityChange) : currentLiquidity.sub(signedLiquidityChange.abs());
|
|
2353
1884
|
currentSqrtPrice = tick.sqrtPrice;
|
|
2354
1885
|
} else {
|
|
@@ -2403,22 +1934,17 @@ var SplitSwap = class {
|
|
|
2403
1934
|
}
|
|
2404
1935
|
};
|
|
2405
1936
|
|
|
2406
|
-
// src/
|
|
2407
|
-
import
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
const twoDec = new Decimal6(2);
|
|
2412
|
-
const price = new Decimal6(get_price_x128_from_real_id2(binId, binStep));
|
|
2413
|
-
return price.div(twoDec.pow(128)).mul(Decimal6.pow(10, decimalsA - decimalsB));
|
|
2414
|
-
}
|
|
2415
|
-
static getBinIdFromPrice(price, binStep, decimalsA, decimalsB) {
|
|
2416
|
-
const twoDec = new Decimal6(2);
|
|
2417
|
-
const tenDec = new Decimal6(10);
|
|
2418
|
-
const realid = get_real_id_from_price_x128(new Decimal6(price).mul(tenDec.pow(decimalsB - decimalsA)).mul(twoDec.pow(128)).toDecimalPlaces(0).toString(), binStep);
|
|
2419
|
-
return realid;
|
|
1937
|
+
// src/utils/numbers.ts
|
|
1938
|
+
import Decimal3 from "decimal.js";
|
|
1939
|
+
function d(value) {
|
|
1940
|
+
if (Decimal3.isDecimal(value)) {
|
|
1941
|
+
return value;
|
|
2420
1942
|
}
|
|
2421
|
-
|
|
1943
|
+
return new Decimal3(value === void 0 ? 0 : value);
|
|
1944
|
+
}
|
|
1945
|
+
function decimalsMultiplier(decimals) {
|
|
1946
|
+
return d(10).pow(d(decimals).abs());
|
|
1947
|
+
}
|
|
2422
1948
|
|
|
2423
1949
|
// src/utils/objects.ts
|
|
2424
1950
|
function getSuiObjectData(resp) {
|
|
@@ -2573,7 +2099,7 @@ function buildPool(objects) {
|
|
|
2573
2099
|
const rewarders = [];
|
|
2574
2100
|
fields.rewarder_manager.fields.rewarders.forEach((item) => {
|
|
2575
2101
|
const { emissions_per_second } = item.fields;
|
|
2576
|
-
const emissionSeconds = MathUtil.fromX64(new
|
|
2102
|
+
const emissionSeconds = MathUtil.fromX64(new BN11(emissions_per_second));
|
|
2577
2103
|
const emissionsEveryDay = Math.floor(emissionSeconds.toNumber() * 60 * 60 * 24);
|
|
2578
2104
|
rewarders.push({
|
|
2579
2105
|
emissions_per_second,
|
|
@@ -2765,11 +2291,11 @@ function buildTickData(objects) {
|
|
|
2765
2291
|
const possition = {
|
|
2766
2292
|
objectId: getObjectId(objects),
|
|
2767
2293
|
index: asIntN(BigInt(valueItem.index.fields.bits)),
|
|
2768
|
-
sqrtPrice: new
|
|
2769
|
-
liquidityNet: new
|
|
2770
|
-
liquidityGross: new
|
|
2771
|
-
feeGrowthOutsideA: new
|
|
2772
|
-
feeGrowthOutsideB: new
|
|
2294
|
+
sqrtPrice: new BN11(valueItem.sqrt_price),
|
|
2295
|
+
liquidityNet: new BN11(valueItem.liquidity_net.fields.bits),
|
|
2296
|
+
liquidityGross: new BN11(valueItem.liquidity_gross),
|
|
2297
|
+
feeGrowthOutsideA: new BN11(valueItem.fee_growth_outside_a),
|
|
2298
|
+
feeGrowthOutsideB: new BN11(valueItem.fee_growth_outside_b),
|
|
2773
2299
|
rewardersGrowthOutside: valueItem.rewards_growth_outside
|
|
2774
2300
|
};
|
|
2775
2301
|
return possition;
|
|
@@ -2779,11 +2305,11 @@ function buildTickDataByEvent(fields) {
|
|
|
2779
2305
|
throw new ClmmpoolsError(`Invalid tick fields.`, "InvalidTickFields" /* InvalidTickFields */);
|
|
2780
2306
|
}
|
|
2781
2307
|
const index = asIntN(BigInt(fields.index.bits));
|
|
2782
|
-
const sqrtPrice = new
|
|
2783
|
-
const liquidityNet = new
|
|
2784
|
-
const liquidityGross = new
|
|
2785
|
-
const feeGrowthOutsideA = new
|
|
2786
|
-
const feeGrowthOutsideB = new
|
|
2308
|
+
const sqrtPrice = new BN11(fields.sqrt_price);
|
|
2309
|
+
const liquidityNet = new BN11(fields.liquidity_net.bits);
|
|
2310
|
+
const liquidityGross = new BN11(fields.liquidity_gross);
|
|
2311
|
+
const feeGrowthOutsideA = new BN11(fields.fee_growth_outside_a);
|
|
2312
|
+
const feeGrowthOutsideB = new BN11(fields.fee_growth_outside_b);
|
|
2787
2313
|
const rewardersGrowthOutside = fields.rewards_growth_outside || [];
|
|
2788
2314
|
const tick = {
|
|
2789
2315
|
objectId: "",
|
|
@@ -2802,7 +2328,7 @@ function buildClmmPositionName(pool_index, position_index) {
|
|
|
2802
2328
|
}
|
|
2803
2329
|
|
|
2804
2330
|
// src/utils/tick.ts
|
|
2805
|
-
import
|
|
2331
|
+
import BN12 from "bn.js";
|
|
2806
2332
|
var TickUtil = class {
|
|
2807
2333
|
/**
|
|
2808
2334
|
* Get min tick index.
|
|
@@ -2842,22 +2368,22 @@ function getRewardInTickRange(pool, tickLower, tickUpper, tickLowerIndex, tickUp
|
|
|
2842
2368
|
let rewarder_growth_below = growthGlobal[i];
|
|
2843
2369
|
if (tickLower !== null) {
|
|
2844
2370
|
if (pool.current_tick_index < tickLowerIndex) {
|
|
2845
|
-
rewarder_growth_below = growthGlobal[i].sub(new
|
|
2371
|
+
rewarder_growth_below = growthGlobal[i].sub(new BN12(tickLower.rewardersGrowthOutside[i]));
|
|
2846
2372
|
} else {
|
|
2847
2373
|
rewarder_growth_below = tickLower.rewardersGrowthOutside[i];
|
|
2848
2374
|
}
|
|
2849
2375
|
}
|
|
2850
|
-
let rewarder_growth_above = new
|
|
2376
|
+
let rewarder_growth_above = new BN12(0);
|
|
2851
2377
|
if (tickUpper !== null) {
|
|
2852
2378
|
if (pool.current_tick_index >= tickUpperIndex) {
|
|
2853
|
-
rewarder_growth_above = growthGlobal[i].sub(new
|
|
2379
|
+
rewarder_growth_above = growthGlobal[i].sub(new BN12(tickUpper.rewardersGrowthOutside[i]));
|
|
2854
2380
|
} else {
|
|
2855
2381
|
rewarder_growth_above = tickUpper.rewardersGrowthOutside[i];
|
|
2856
2382
|
}
|
|
2857
2383
|
}
|
|
2858
2384
|
const rewGrowthInside = MathUtil.subUnderflowU128(
|
|
2859
|
-
MathUtil.subUnderflowU128(new
|
|
2860
|
-
new
|
|
2385
|
+
MathUtil.subUnderflowU128(new BN12(growthGlobal[i]), new BN12(rewarder_growth_below)),
|
|
2386
|
+
new BN12(rewarder_growth_above)
|
|
2861
2387
|
);
|
|
2862
2388
|
rewarderGrowthInside.push(rewGrowthInside);
|
|
2863
2389
|
}
|
|
@@ -2865,8 +2391,8 @@ function getRewardInTickRange(pool, tickLower, tickUpper, tickLowerIndex, tickUp
|
|
|
2865
2391
|
}
|
|
2866
2392
|
|
|
2867
2393
|
// src/utils/transaction-util.ts
|
|
2868
|
-
import
|
|
2869
|
-
import
|
|
2394
|
+
import BN13 from "bn.js";
|
|
2395
|
+
import Decimal4 from "decimal.js";
|
|
2870
2396
|
import { Transaction } from "@mysten/sui/transactions";
|
|
2871
2397
|
function findAdjustCoin(coinPair) {
|
|
2872
2398
|
const isAdjustCoinA = CoinAssist.isSuiCoin(coinPair.coinTypeA);
|
|
@@ -2874,7 +2400,7 @@ function findAdjustCoin(coinPair) {
|
|
|
2874
2400
|
return { isAdjustCoinA, isAdjustCoinB };
|
|
2875
2401
|
}
|
|
2876
2402
|
function reverSlippageAmount(slippageAmount, slippage) {
|
|
2877
|
-
return
|
|
2403
|
+
return Decimal4.ceil(d(slippageAmount).div(1 + slippage)).toString();
|
|
2878
2404
|
}
|
|
2879
2405
|
async function printTransaction(tx, isPrint = true) {
|
|
2880
2406
|
console.log(`inputs`, tx.blockData.inputs);
|
|
@@ -3174,7 +2700,7 @@ var _TransactionUtil = class {
|
|
|
3174
2700
|
const liquidityInput = ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts(
|
|
3175
2701
|
Number(params.tick_lower),
|
|
3176
2702
|
Number(params.tick_upper),
|
|
3177
|
-
new
|
|
2703
|
+
new BN13(coinAmount),
|
|
3178
2704
|
params.fix_amount_a,
|
|
3179
2705
|
true,
|
|
3180
2706
|
slippage,
|
|
@@ -3248,12 +2774,12 @@ var _TransactionUtil = class {
|
|
|
3248
2774
|
max_amount_a = params.amount_a;
|
|
3249
2775
|
min_amount_a = params.amount_a;
|
|
3250
2776
|
max_amount_b = params.amount_b;
|
|
3251
|
-
min_amount_b = new
|
|
2777
|
+
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();
|
|
3252
2778
|
} else {
|
|
3253
2779
|
max_amount_b = params.amount_b;
|
|
3254
2780
|
min_amount_b = params.amount_b;
|
|
3255
2781
|
max_amount_a = params.amount_a;
|
|
3256
|
-
min_amount_a = new
|
|
2782
|
+
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();
|
|
3257
2783
|
}
|
|
3258
2784
|
const args = params.is_open ? [
|
|
3259
2785
|
tx.object(clmmConfig.global_config_id),
|
|
@@ -4242,7 +3768,7 @@ var _TransactionUtil = class {
|
|
|
4242
3768
|
const basePath = splitPath.basePaths[i];
|
|
4243
3769
|
a2b.push(basePath.direction);
|
|
4244
3770
|
poolAddress.push(basePath.poolAddress);
|
|
4245
|
-
rawAmountLimit.push(new
|
|
3771
|
+
rawAmountLimit.push(new BN13(basePath.inputAmount.toString()));
|
|
4246
3772
|
if (i === 0) {
|
|
4247
3773
|
coinType.push(basePath.fromCoin, basePath.toCoin);
|
|
4248
3774
|
} else {
|
|
@@ -4250,8 +3776,8 @@ var _TransactionUtil = class {
|
|
|
4250
3776
|
}
|
|
4251
3777
|
}
|
|
4252
3778
|
const onePath = {
|
|
4253
|
-
amountIn: new
|
|
4254
|
-
amountOut: new
|
|
3779
|
+
amountIn: new BN13(splitPath.inputAmount.toString()),
|
|
3780
|
+
amountOut: new BN13(splitPath.outputAmount.toString()),
|
|
4255
3781
|
poolAddress,
|
|
4256
3782
|
a2b,
|
|
4257
3783
|
rawAmountLimit,
|
|
@@ -4608,9 +4134,9 @@ var TxBlock = class {
|
|
|
4608
4134
|
};
|
|
4609
4135
|
|
|
4610
4136
|
// src/utils/deepbook-utils.ts
|
|
4611
|
-
import
|
|
4137
|
+
import BN14 from "bn.js";
|
|
4612
4138
|
import { Transaction as Transaction3 } from "@mysten/sui/transactions";
|
|
4613
|
-
var FLOAT_SCALING = new
|
|
4139
|
+
var FLOAT_SCALING = new BN14(1e9);
|
|
4614
4140
|
var DeepbookUtils = class {
|
|
4615
4141
|
static createAccountCap(senderAddress, sdkOptions, tx, isTransfer = false) {
|
|
4616
4142
|
if (senderAddress.length === 0) {
|
|
@@ -4756,9 +4282,9 @@ var DeepbookUtils = class {
|
|
|
4756
4282
|
static async preSwap(sdk, pool, a2b, amountIn) {
|
|
4757
4283
|
let isExceed = false;
|
|
4758
4284
|
let amountOut = ZERO;
|
|
4759
|
-
let remainAmount = new
|
|
4285
|
+
let remainAmount = new BN14(amountIn);
|
|
4760
4286
|
let feeAmount = ZERO;
|
|
4761
|
-
const initAmountIn = new
|
|
4287
|
+
const initAmountIn = new BN14(amountIn);
|
|
4762
4288
|
if (a2b) {
|
|
4763
4289
|
let bids = await this.getPoolBids(sdk, pool.poolID, pool.baseAsset, pool.quoteAsset);
|
|
4764
4290
|
if (bids === null) {
|
|
@@ -4768,16 +4294,16 @@ var DeepbookUtils = class {
|
|
|
4768
4294
|
return b.price - a.price;
|
|
4769
4295
|
});
|
|
4770
4296
|
for (let i = 0; i < bids.length; i += 1) {
|
|
4771
|
-
const curBidAmount = new
|
|
4772
|
-
const curBidPrice = new
|
|
4773
|
-
const fee = curBidAmount.mul(new
|
|
4297
|
+
const curBidAmount = new BN14(bids[i].quantity);
|
|
4298
|
+
const curBidPrice = new BN14(bids[i].price);
|
|
4299
|
+
const fee = curBidAmount.mul(new BN14(curBidPrice)).mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING).div(FLOAT_SCALING);
|
|
4774
4300
|
if (remainAmount.gt(curBidAmount)) {
|
|
4775
4301
|
remainAmount = remainAmount.sub(curBidAmount);
|
|
4776
4302
|
amountOut = amountOut.add(curBidAmount.mul(curBidPrice).div(FLOAT_SCALING).sub(fee));
|
|
4777
4303
|
feeAmount = feeAmount.add(fee);
|
|
4778
4304
|
} else {
|
|
4779
|
-
const curOut = remainAmount.mul(new
|
|
4780
|
-
const curFee = curOut.mul(new
|
|
4305
|
+
const curOut = remainAmount.mul(new BN14(bids[i].price)).div(FLOAT_SCALING);
|
|
4306
|
+
const curFee = curOut.mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING);
|
|
4781
4307
|
amountOut = amountOut.add(curOut.sub(curFee));
|
|
4782
4308
|
remainAmount = remainAmount.sub(remainAmount);
|
|
4783
4309
|
feeAmount = feeAmount.add(curFee);
|
|
@@ -4792,15 +4318,15 @@ var DeepbookUtils = class {
|
|
|
4792
4318
|
isExceed = true;
|
|
4793
4319
|
}
|
|
4794
4320
|
for (let i = 0; i < asks.length; i += 1) {
|
|
4795
|
-
const curAskAmount = new
|
|
4796
|
-
const fee = curAskAmount.mul(new
|
|
4321
|
+
const curAskAmount = new BN14(asks[i].price).mul(new BN14(asks[i].quantity)).div(new BN14(1e9));
|
|
4322
|
+
const fee = curAskAmount.mul(new BN14(pool.takerFeeRate)).div(FLOAT_SCALING);
|
|
4797
4323
|
const curAskAmountWithFee = curAskAmount.add(fee);
|
|
4798
4324
|
if (remainAmount.gt(curAskAmount)) {
|
|
4799
|
-
amountOut = amountOut.add(new
|
|
4325
|
+
amountOut = amountOut.add(new BN14(asks[i].quantity));
|
|
4800
4326
|
remainAmount = remainAmount.sub(curAskAmountWithFee);
|
|
4801
4327
|
feeAmount = feeAmount.add(fee);
|
|
4802
4328
|
} else {
|
|
4803
|
-
const splitNums = new
|
|
4329
|
+
const splitNums = new BN14(asks[i].quantity).div(new BN14(pool.lotSize));
|
|
4804
4330
|
const splitAmount = curAskAmountWithFee.div(splitNums);
|
|
4805
4331
|
const swapSplitNum = remainAmount.div(splitAmount);
|
|
4806
4332
|
amountOut = amountOut.add(swapSplitNum.muln(pool.lotSize));
|
|
@@ -5661,7 +5187,7 @@ var PoolModule = class {
|
|
|
5661
5187
|
};
|
|
5662
5188
|
|
|
5663
5189
|
// src/modules/positionModule.ts
|
|
5664
|
-
import
|
|
5190
|
+
import BN15 from "bn.js";
|
|
5665
5191
|
import { Transaction as Transaction5 } from "@mysten/sui/transactions";
|
|
5666
5192
|
import { isValidSuiObjectId } from "@mysten/sui/utils";
|
|
5667
5193
|
var PositionModule = class {
|
|
@@ -5883,8 +5409,8 @@ var PositionModule = class {
|
|
|
5883
5409
|
for (let i = 0; i < valueData.length; i += 1) {
|
|
5884
5410
|
const { parsedJson } = valueData[i];
|
|
5885
5411
|
const posRrewarderResult = {
|
|
5886
|
-
feeOwedA: new
|
|
5887
|
-
feeOwedB: new
|
|
5412
|
+
feeOwedA: new BN15(parsedJson.fee_owned_a),
|
|
5413
|
+
feeOwedB: new BN15(parsedJson.fee_owned_b),
|
|
5888
5414
|
position_id: parsedJson.position_id
|
|
5889
5415
|
};
|
|
5890
5416
|
result.push(posRrewarderResult);
|
|
@@ -6260,7 +5786,7 @@ var PositionModule = class {
|
|
|
6260
5786
|
};
|
|
6261
5787
|
|
|
6262
5788
|
// src/modules/rewarderModule.ts
|
|
6263
|
-
import
|
|
5789
|
+
import BN16 from "bn.js";
|
|
6264
5790
|
import { Transaction as Transaction6 } from "@mysten/sui/transactions";
|
|
6265
5791
|
var RewarderModule = class {
|
|
6266
5792
|
_sdk;
|
|
@@ -6286,7 +5812,7 @@ var RewarderModule = class {
|
|
|
6286
5812
|
}
|
|
6287
5813
|
const emissionsEveryDay = [];
|
|
6288
5814
|
for (const rewarderInfo of rewarderInfos) {
|
|
6289
|
-
const emissionSeconds = MathUtil.fromX64(new
|
|
5815
|
+
const emissionSeconds = MathUtil.fromX64(new BN16(rewarderInfo.emissions_per_second));
|
|
6290
5816
|
emissionsEveryDay.push({
|
|
6291
5817
|
emissions: Math.floor(emissionSeconds.toNumber() * 60 * 60 * 24),
|
|
6292
5818
|
coin_address: rewarderInfo.coinAddress
|
|
@@ -6305,20 +5831,20 @@ var RewarderModule = class {
|
|
|
6305
5831
|
const currentPool = await this.sdk.Pool.getPool(poolID);
|
|
6306
5832
|
const lastTime = currentPool.rewarder_last_updated_time;
|
|
6307
5833
|
currentPool.rewarder_last_updated_time = currentTime.toString();
|
|
6308
|
-
if (Number(currentPool.liquidity) === 0 || currentTime.eq(new
|
|
5834
|
+
if (Number(currentPool.liquidity) === 0 || currentTime.eq(new BN16(lastTime))) {
|
|
6309
5835
|
return currentPool;
|
|
6310
5836
|
}
|
|
6311
|
-
const timeDelta = currentTime.div(new
|
|
5837
|
+
const timeDelta = currentTime.div(new BN16(1e3)).sub(new BN16(lastTime)).add(new BN16(15));
|
|
6312
5838
|
const rewarderInfos = currentPool.rewarder_infos;
|
|
6313
5839
|
for (let i = 0; i < rewarderInfos.length; i += 1) {
|
|
6314
5840
|
const rewarderInfo = rewarderInfos[i];
|
|
6315
5841
|
const rewarderGrowthDelta = MathUtil.checkMulDivFloor(
|
|
6316
5842
|
timeDelta,
|
|
6317
|
-
new
|
|
6318
|
-
new
|
|
5843
|
+
new BN16(rewarderInfo.emissions_per_second),
|
|
5844
|
+
new BN16(currentPool.liquidity),
|
|
6319
5845
|
128
|
|
6320
5846
|
);
|
|
6321
|
-
this.growthGlobal[i] = new
|
|
5847
|
+
this.growthGlobal[i] = new BN16(rewarderInfo.growth_global).add(new BN16(rewarderGrowthDelta));
|
|
6322
5848
|
}
|
|
6323
5849
|
return currentPool;
|
|
6324
5850
|
}
|
|
@@ -6333,7 +5859,7 @@ var RewarderModule = class {
|
|
|
6333
5859
|
*/
|
|
6334
5860
|
async posRewardersAmount(poolID, positionHandle, positionID) {
|
|
6335
5861
|
const currentTime = Date.parse((/* @__PURE__ */ new Date()).toString());
|
|
6336
|
-
const pool = await this.updatePoolRewarder(poolID, new
|
|
5862
|
+
const pool = await this.updatePoolRewarder(poolID, new BN16(currentTime));
|
|
6337
5863
|
const position = await this.sdk.Position.getPositionRewarders(positionHandle, positionID);
|
|
6338
5864
|
if (position === void 0) {
|
|
6339
5865
|
return [];
|
|
@@ -6354,7 +5880,7 @@ var RewarderModule = class {
|
|
|
6354
5880
|
*/
|
|
6355
5881
|
async poolRewardersAmount(accountAddress, poolID) {
|
|
6356
5882
|
const currentTime = Date.parse((/* @__PURE__ */ new Date()).toString());
|
|
6357
|
-
const pool = await this.updatePoolRewarder(poolID, new
|
|
5883
|
+
const pool = await this.updatePoolRewarder(poolID, new BN16(currentTime));
|
|
6358
5884
|
const positions = await this.sdk.Position.getPositionList(accountAddress, [poolID]);
|
|
6359
5885
|
const tickDatas = await this.getPoolLowerAndUpperTicks(pool.ticks_handle, positions);
|
|
6360
5886
|
const rewarderAmount = [ZERO, ZERO, ZERO];
|
|
@@ -6381,38 +5907,38 @@ var RewarderModule = class {
|
|
|
6381
5907
|
const growthInside = [];
|
|
6382
5908
|
const AmountOwed = [];
|
|
6383
5909
|
if (rewardersInside.length > 0) {
|
|
6384
|
-
let growthDelta0 = MathUtil.subUnderflowU128(rewardersInside[0], new
|
|
6385
|
-
if (growthDelta0.gt(new
|
|
5910
|
+
let growthDelta0 = MathUtil.subUnderflowU128(rewardersInside[0], new BN16(position.reward_growth_inside_0));
|
|
5911
|
+
if (growthDelta0.gt(new BN16("3402823669209384634633745948738404"))) {
|
|
6386
5912
|
growthDelta0 = ONE;
|
|
6387
5913
|
}
|
|
6388
|
-
const amountOwed_0 = MathUtil.checkMulShiftRight(new
|
|
5914
|
+
const amountOwed_0 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta0, 64, 128);
|
|
6389
5915
|
growthInside.push(rewardersInside[0]);
|
|
6390
5916
|
AmountOwed.push({
|
|
6391
|
-
amount_owed: new
|
|
5917
|
+
amount_owed: new BN16(position.reward_amount_owed_0).add(amountOwed_0),
|
|
6392
5918
|
coin_address: pool.rewarder_infos[0].coinAddress
|
|
6393
5919
|
});
|
|
6394
5920
|
}
|
|
6395
5921
|
if (rewardersInside.length > 1) {
|
|
6396
|
-
let growthDelta_1 = MathUtil.subUnderflowU128(rewardersInside[1], new
|
|
6397
|
-
if (growthDelta_1.gt(new
|
|
5922
|
+
let growthDelta_1 = MathUtil.subUnderflowU128(rewardersInside[1], new BN16(position.reward_growth_inside_1));
|
|
5923
|
+
if (growthDelta_1.gt(new BN16("3402823669209384634633745948738404"))) {
|
|
6398
5924
|
growthDelta_1 = ONE;
|
|
6399
5925
|
}
|
|
6400
|
-
const amountOwed_1 = MathUtil.checkMulShiftRight(new
|
|
5926
|
+
const amountOwed_1 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta_1, 64, 128);
|
|
6401
5927
|
growthInside.push(rewardersInside[1]);
|
|
6402
5928
|
AmountOwed.push({
|
|
6403
|
-
amount_owed: new
|
|
5929
|
+
amount_owed: new BN16(position.reward_amount_owed_1).add(amountOwed_1),
|
|
6404
5930
|
coin_address: pool.rewarder_infos[1].coinAddress
|
|
6405
5931
|
});
|
|
6406
5932
|
}
|
|
6407
5933
|
if (rewardersInside.length > 2) {
|
|
6408
|
-
let growthDelta_2 = MathUtil.subUnderflowU128(rewardersInside[2], new
|
|
6409
|
-
if (growthDelta_2.gt(new
|
|
5934
|
+
let growthDelta_2 = MathUtil.subUnderflowU128(rewardersInside[2], new BN16(position.reward_growth_inside_2));
|
|
5935
|
+
if (growthDelta_2.gt(new BN16("3402823669209384634633745948738404"))) {
|
|
6410
5936
|
growthDelta_2 = ONE;
|
|
6411
5937
|
}
|
|
6412
|
-
const amountOwed_2 = MathUtil.checkMulShiftRight(new
|
|
5938
|
+
const amountOwed_2 = MathUtil.checkMulShiftRight(new BN16(position.liquidity), growthDelta_2, 64, 128);
|
|
6413
5939
|
growthInside.push(rewardersInside[2]);
|
|
6414
5940
|
AmountOwed.push({
|
|
6415
|
-
amount_owed: new
|
|
5941
|
+
amount_owed: new BN16(position.reward_amount_owed_2).add(amountOwed_2),
|
|
6416
5942
|
coin_address: pool.rewarder_infos[2].coinAddress
|
|
6417
5943
|
});
|
|
6418
5944
|
}
|
|
@@ -6526,8 +6052,8 @@ var RewarderModule = class {
|
|
|
6526
6052
|
for (let i = 0; i < valueData.length; i += 1) {
|
|
6527
6053
|
const { parsedJson } = valueData[i];
|
|
6528
6054
|
const posRrewarderResult = {
|
|
6529
|
-
feeOwedA: new
|
|
6530
|
-
feeOwedB: new
|
|
6055
|
+
feeOwedA: new BN16(parsedJson.fee_owned_a),
|
|
6056
|
+
feeOwedB: new BN16(parsedJson.fee_owned_b),
|
|
6531
6057
|
position_id: parsedJson.position_id
|
|
6532
6058
|
};
|
|
6533
6059
|
result.push(posRrewarderResult);
|
|
@@ -6590,7 +6116,7 @@ var RewarderModule = class {
|
|
|
6590
6116
|
};
|
|
6591
6117
|
for (let j = 0; j < params[i].rewarderInfo.length; j += 1) {
|
|
6592
6118
|
posRrewarderResult.rewarderAmountOwed.push({
|
|
6593
|
-
amount_owed: new
|
|
6119
|
+
amount_owed: new BN16(valueData[i].parsedJson.data[j]),
|
|
6594
6120
|
coin_address: params[i].rewarderInfo[j].coinAddress
|
|
6595
6121
|
});
|
|
6596
6122
|
}
|
|
@@ -6661,43 +6187,21 @@ var RewarderModule = class {
|
|
|
6661
6187
|
* @param tx
|
|
6662
6188
|
* @returns
|
|
6663
6189
|
*/
|
|
6664
|
-
async batchCollectRewardePayload(params, tx
|
|
6190
|
+
async batchCollectRewardePayload(params, tx) {
|
|
6665
6191
|
if (!checkInvalidSuiAddress(this._sdk.senderAddress)) {
|
|
6666
6192
|
throw new ClmmpoolsError("this config sdk senderAddress is not set right", "InvalidSendAddress" /* InvalidSendAddress */);
|
|
6667
6193
|
}
|
|
6668
6194
|
const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress, null);
|
|
6669
6195
|
tx = tx || new Transaction6();
|
|
6670
|
-
const
|
|
6196
|
+
const coinIdList = [];
|
|
6671
6197
|
params.forEach((item) => {
|
|
6672
6198
|
const coinTypeA = normalizeCoinType(item.coinTypeA);
|
|
6673
6199
|
const coinTypeB = normalizeCoinType(item.coinTypeB);
|
|
6674
6200
|
if (item.collect_fee) {
|
|
6675
|
-
|
|
6676
|
-
|
|
6677
|
-
|
|
6678
|
-
|
|
6679
|
-
coinAInput = {
|
|
6680
|
-
targetCoin: inputCoinA,
|
|
6681
|
-
remainCoins: [],
|
|
6682
|
-
isMintZeroCoin: true,
|
|
6683
|
-
tragetCoinAmount: "0"
|
|
6684
|
-
};
|
|
6685
|
-
coinIdMaps.push({ coin: coinAInput, coin_addr: coinTypeA });
|
|
6686
|
-
}
|
|
6687
|
-
let coinBInput = null;
|
|
6688
|
-
if (coinBInput == null) {
|
|
6689
|
-
if (inputCoinB == null) {
|
|
6690
|
-
coinBInput = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(0), coinTypeB, false, true);
|
|
6691
|
-
} else {
|
|
6692
|
-
coinBInput = {
|
|
6693
|
-
targetCoin: inputCoinB,
|
|
6694
|
-
remainCoins: [],
|
|
6695
|
-
isMintZeroCoin: true,
|
|
6696
|
-
tragetCoinAmount: "0"
|
|
6697
|
-
};
|
|
6698
|
-
}
|
|
6699
|
-
coinIdMaps.push({ coin: coinBInput, coin_addr: coinTypeB });
|
|
6700
|
-
}
|
|
6201
|
+
const coinAInput = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(0), coinTypeA, false, true);
|
|
6202
|
+
const coinBInput = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(0), coinTypeB, false, true);
|
|
6203
|
+
coinIdList.push({ coin: coinAInput, coin_addr: coinTypeA });
|
|
6204
|
+
coinIdList.push({ coin: coinBInput, coin_addr: coinTypeB });
|
|
6701
6205
|
tx = this._sdk.Position.createCollectFeeNoSendPaylod(
|
|
6702
6206
|
{
|
|
6703
6207
|
pool_id: item.pool_id,
|
|
@@ -6713,15 +6217,15 @@ var RewarderModule = class {
|
|
|
6713
6217
|
const primaryCoinInputs = [];
|
|
6714
6218
|
item.rewarder_coin_types.forEach((type) => {
|
|
6715
6219
|
const coinType = normalizeCoinType(type);
|
|
6716
|
-
|
|
6717
|
-
if (coinInput === void 0) {
|
|
6718
|
-
coinInput = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(0), coinType, false, true);
|
|
6719
|
-
coinIdMaps.push({ coin: coinInput, coin_addr: coinType });
|
|
6720
|
-
}
|
|
6220
|
+
const coinInput = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(0), coinType, false, true);
|
|
6721
6221
|
primaryCoinInputs.push(coinInput.targetCoin);
|
|
6222
|
+
coinIdList.push({ coin: coinInput, coin_addr: coinType });
|
|
6722
6223
|
});
|
|
6723
6224
|
tx = this.createCollectRewarderNoSendPaylod(item, tx, primaryCoinInputs);
|
|
6724
6225
|
});
|
|
6226
|
+
coinIdList.forEach((item) => {
|
|
6227
|
+
TransactionUtil.buildTransferCoin(this._sdk, tx, item.coin.targetCoin, item.coin_addr, this._sdk.senderAddress);
|
|
6228
|
+
});
|
|
6725
6229
|
return tx;
|
|
6726
6230
|
}
|
|
6727
6231
|
createCollectRewarderPaylod(params, tx, primaryCoinInputs) {
|
|
@@ -6771,7 +6275,7 @@ var RewarderModule = class {
|
|
|
6771
6275
|
};
|
|
6772
6276
|
|
|
6773
6277
|
// src/modules/routerModule.ts
|
|
6774
|
-
import
|
|
6278
|
+
import BN17 from "bn.js";
|
|
6775
6279
|
import { Graph, GraphEdge, GraphVertex } from "@syntsugar/cc-graph";
|
|
6776
6280
|
import { Transaction as Transaction7 } from "@mysten/sui/transactions";
|
|
6777
6281
|
function _pairSymbol(base, quote) {
|
|
@@ -7053,8 +6557,8 @@ var RouterModule = class {
|
|
|
7053
6557
|
if (swapWithMultiPoolParams != null) {
|
|
7054
6558
|
const preSwapResult2 = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
|
|
7055
6559
|
const onePath2 = {
|
|
7056
|
-
amountIn: new
|
|
7057
|
-
amountOut: new
|
|
6560
|
+
amountIn: new BN17(preSwapResult2.estimatedAmountIn),
|
|
6561
|
+
amountOut: new BN17(preSwapResult2.estimatedAmountOut),
|
|
7058
6562
|
poolAddress: [preSwapResult2.poolAddress],
|
|
7059
6563
|
a2b: [preSwapResult2.aToB],
|
|
7060
6564
|
rawAmountLimit: byAmountIn ? [preSwapResult2.estimatedAmountOut] : [preSwapResult2.estimatedAmountIn],
|
|
@@ -7067,8 +6571,8 @@ var RouterModule = class {
|
|
|
7067
6571
|
priceSlippagePoint
|
|
7068
6572
|
};
|
|
7069
6573
|
const result2 = {
|
|
7070
|
-
amountIn: new
|
|
7071
|
-
amountOut: new
|
|
6574
|
+
amountIn: new BN17(preSwapResult2.estimatedAmountIn),
|
|
6575
|
+
amountOut: new BN17(preSwapResult2.estimatedAmountOut),
|
|
7072
6576
|
paths: [onePath2],
|
|
7073
6577
|
a2b: preSwapResult2.aToB,
|
|
7074
6578
|
b2c: void 0,
|
|
@@ -7090,8 +6594,8 @@ var RouterModule = class {
|
|
|
7090
6594
|
if (swapWithMultiPoolParams != null) {
|
|
7091
6595
|
const preSwapResult2 = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
|
|
7092
6596
|
const onePath2 = {
|
|
7093
|
-
amountIn: new
|
|
7094
|
-
amountOut: new
|
|
6597
|
+
amountIn: new BN17(preSwapResult2.estimatedAmountIn),
|
|
6598
|
+
amountOut: new BN17(preSwapResult2.estimatedAmountOut),
|
|
7095
6599
|
poolAddress: [preSwapResult2.poolAddress],
|
|
7096
6600
|
a2b: [preSwapResult2.aToB],
|
|
7097
6601
|
rawAmountLimit: byAmountIn ? [preSwapResult2.estimatedAmountOut] : [preSwapResult2.estimatedAmountIn],
|
|
@@ -7104,8 +6608,8 @@ var RouterModule = class {
|
|
|
7104
6608
|
priceSlippagePoint
|
|
7105
6609
|
};
|
|
7106
6610
|
const result3 = {
|
|
7107
|
-
amountIn: new
|
|
7108
|
-
amountOut: new
|
|
6611
|
+
amountIn: new BN17(preSwapResult2.estimatedAmountIn),
|
|
6612
|
+
amountOut: new BN17(preSwapResult2.estimatedAmountOut),
|
|
7109
6613
|
paths: [onePath2],
|
|
7110
6614
|
a2b: preSwapResult2.aToB,
|
|
7111
6615
|
b2c: void 0,
|
|
@@ -7186,8 +6690,8 @@ var RouterModule = class {
|
|
|
7186
6690
|
if (swapWithMultiPoolParams != null) {
|
|
7187
6691
|
const preSwapResult = await this.sdk.Swap.preSwapWithMultiPool(swapWithMultiPoolParams);
|
|
7188
6692
|
const onePath = {
|
|
7189
|
-
amountIn: new
|
|
7190
|
-
amountOut: new
|
|
6693
|
+
amountIn: new BN17(preSwapResult.estimatedAmountIn),
|
|
6694
|
+
amountOut: new BN17(preSwapResult.estimatedAmountOut),
|
|
7191
6695
|
poolAddress: [preSwapResult.poolAddress],
|
|
7192
6696
|
a2b: [preSwapResult.aToB],
|
|
7193
6697
|
rawAmountLimit: byAmountIn ? [preSwapResult.estimatedAmountOut] : [preSwapResult.estimatedAmountIn],
|
|
@@ -7200,8 +6704,8 @@ var RouterModule = class {
|
|
|
7200
6704
|
priceSlippagePoint
|
|
7201
6705
|
};
|
|
7202
6706
|
const result = {
|
|
7203
|
-
amountIn: new
|
|
7204
|
-
amountOut: new
|
|
6707
|
+
amountIn: new BN17(preSwapResult.estimatedAmountIn),
|
|
6708
|
+
amountOut: new BN17(preSwapResult.estimatedAmountOut),
|
|
7205
6709
|
paths: [onePath],
|
|
7206
6710
|
a2b: preSwapResult.aToB,
|
|
7207
6711
|
b2c: void 0,
|
|
@@ -7285,13 +6789,13 @@ var RouterModule = class {
|
|
|
7285
6789
|
continue;
|
|
7286
6790
|
}
|
|
7287
6791
|
if (params[0].byAmountIn) {
|
|
7288
|
-
const amount = new
|
|
6792
|
+
const amount = new BN17(valueData[i].parsedJson.data.amount_out);
|
|
7289
6793
|
if (amount.gt(tempMaxAmount)) {
|
|
7290
6794
|
tempIndex = i;
|
|
7291
6795
|
tempMaxAmount = amount;
|
|
7292
6796
|
}
|
|
7293
6797
|
} else {
|
|
7294
|
-
const amount = params[i].stepNums > 1 ? new
|
|
6798
|
+
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));
|
|
7295
6799
|
if (amount.lt(tempMaxAmount)) {
|
|
7296
6800
|
tempIndex = i;
|
|
7297
6801
|
tempMaxAmount = amount;
|
|
@@ -7361,8 +6865,8 @@ var RouterModule = class {
|
|
|
7361
6865
|
};
|
|
7362
6866
|
|
|
7363
6867
|
// src/modules/swapModule.ts
|
|
7364
|
-
import
|
|
7365
|
-
import
|
|
6868
|
+
import BN18 from "bn.js";
|
|
6869
|
+
import Decimal5 from "decimal.js";
|
|
7366
6870
|
import { Transaction as Transaction8 } from "@mysten/sui/transactions";
|
|
7367
6871
|
var AMM_SWAP_MODULE = "amm_swap";
|
|
7368
6872
|
var POOL_STRUCT = "Pool";
|
|
@@ -7380,14 +6884,14 @@ var SwapModule = class {
|
|
|
7380
6884
|
const pathCount = item.basePaths.length;
|
|
7381
6885
|
if (pathCount > 0) {
|
|
7382
6886
|
const path = item.basePaths[0];
|
|
7383
|
-
const feeRate = path.label === "Magma" ? new
|
|
6887
|
+
const feeRate = path.label === "Magma" ? new Decimal5(path.feeRate).div(10 ** 6) : new Decimal5(path.feeRate).div(10 ** 9);
|
|
7384
6888
|
const feeAmount = d(path.inputAmount).div(10 ** path.fromDecimal).mul(feeRate);
|
|
7385
6889
|
fee = fee.add(feeAmount);
|
|
7386
6890
|
if (pathCount > 1) {
|
|
7387
6891
|
const path2 = item.basePaths[1];
|
|
7388
|
-
const price1 = path.direction ? path.currentPrice : new
|
|
7389
|
-
const price2 = path2.direction ? path2.currentPrice : new
|
|
7390
|
-
const feeRate2 = path2.label === "Magma" ? new
|
|
6892
|
+
const price1 = path.direction ? path.currentPrice : new Decimal5(1).div(path.currentPrice);
|
|
6893
|
+
const price2 = path2.direction ? path2.currentPrice : new Decimal5(1).div(path2.currentPrice);
|
|
6894
|
+
const feeRate2 = path2.label === "Magma" ? new Decimal5(path2.feeRate).div(10 ** 6) : new Decimal5(path2.feeRate).div(10 ** 9);
|
|
7391
6895
|
const feeAmount2 = d(path2.outputAmount).div(10 ** path2.toDecimal).mul(feeRate2);
|
|
7392
6896
|
const fee2 = feeAmount2.div(price1.mul(price2));
|
|
7393
6897
|
fee = fee.add(fee2);
|
|
@@ -7405,17 +6909,17 @@ var SwapModule = class {
|
|
|
7405
6909
|
const outputAmount = d(path.outputAmount).div(10 ** path.toDecimal);
|
|
7406
6910
|
const inputAmount = d(path.inputAmount).div(10 ** path.fromDecimal);
|
|
7407
6911
|
const rate = outputAmount.div(inputAmount);
|
|
7408
|
-
const cprice = path.direction ? new
|
|
6912
|
+
const cprice = path.direction ? new Decimal5(path.currentPrice) : new Decimal5(1).div(path.currentPrice);
|
|
7409
6913
|
impactValue = impactValue.add(this.calculateSingleImpact(rate, cprice));
|
|
7410
6914
|
}
|
|
7411
6915
|
if (pathCount === 2) {
|
|
7412
6916
|
const path = item.basePaths[0];
|
|
7413
6917
|
const path2 = item.basePaths[1];
|
|
7414
|
-
const cprice1 = path.direction ? new
|
|
7415
|
-
const cprice2 = path2.direction ? new
|
|
6918
|
+
const cprice1 = path.direction ? new Decimal5(path.currentPrice) : new Decimal5(1).div(path.currentPrice);
|
|
6919
|
+
const cprice2 = path2.direction ? new Decimal5(path2.currentPrice) : new Decimal5(1).div(path2.currentPrice);
|
|
7416
6920
|
const cprice = cprice1.mul(cprice2);
|
|
7417
|
-
const outputAmount = new
|
|
7418
|
-
const inputAmount = new
|
|
6921
|
+
const outputAmount = new Decimal5(path2.outputAmount).div(10 ** path2.toDecimal);
|
|
6922
|
+
const inputAmount = new Decimal5(path.inputAmount).div(10 ** path.fromDecimal);
|
|
7419
6923
|
const rate = outputAmount.div(inputAmount);
|
|
7420
6924
|
impactValue = impactValue.add(this.calculateSingleImpact(rate, cprice));
|
|
7421
6925
|
}
|
|
@@ -7477,13 +6981,13 @@ var SwapModule = class {
|
|
|
7477
6981
|
continue;
|
|
7478
6982
|
}
|
|
7479
6983
|
if (params.byAmountIn) {
|
|
7480
|
-
const amount = new
|
|
6984
|
+
const amount = new BN18(valueData[i].parsedJson.data.amount_out);
|
|
7481
6985
|
if (amount.gt(tempMaxAmount)) {
|
|
7482
6986
|
tempIndex = i;
|
|
7483
6987
|
tempMaxAmount = amount;
|
|
7484
6988
|
}
|
|
7485
6989
|
} else {
|
|
7486
|
-
const amount = new
|
|
6990
|
+
const amount = new BN18(valueData[i].parsedJson.data.amount_out);
|
|
7487
6991
|
if (amount.lt(tempMaxAmount)) {
|
|
7488
6992
|
tempIndex = i;
|
|
7489
6993
|
tempMaxAmount = amount;
|
|
@@ -7547,7 +7051,7 @@ var SwapModule = class {
|
|
|
7547
7051
|
return this.transformSwapData(params, valueData[0].parsedJson.data);
|
|
7548
7052
|
}
|
|
7549
7053
|
transformSwapData(params, data) {
|
|
7550
|
-
const estimatedAmountIn = data.amount_in && data.fee_amount ? new
|
|
7054
|
+
const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN18(data.amount_in).add(new BN18(data.fee_amount)).toString() : "";
|
|
7551
7055
|
return {
|
|
7552
7056
|
poolAddress: params.pool.poolAddress,
|
|
7553
7057
|
currentSqrtPrice: params.currentSqrtPrice,
|
|
@@ -7564,7 +7068,7 @@ var SwapModule = class {
|
|
|
7564
7068
|
transformSwapWithMultiPoolData(params, jsonData) {
|
|
7565
7069
|
const { data } = jsonData;
|
|
7566
7070
|
console.log("json data. ", data);
|
|
7567
|
-
const estimatedAmountIn = data.amount_in && data.fee_amount ? new
|
|
7071
|
+
const estimatedAmountIn = data.amount_in && data.fee_amount ? new BN18(data.amount_in).add(new BN18(data.fee_amount)).toString() : "";
|
|
7568
7072
|
return {
|
|
7569
7073
|
poolAddress: params.poolAddress,
|
|
7570
7074
|
estimatedAmountIn,
|
|
@@ -9077,8 +8581,8 @@ var TokenModule = class {
|
|
|
9077
8581
|
};
|
|
9078
8582
|
|
|
9079
8583
|
// src/modules/routerModuleV2.ts
|
|
9080
|
-
import
|
|
9081
|
-
import
|
|
8584
|
+
import BN19 from "bn.js";
|
|
8585
|
+
import Decimal6 from "decimal.js";
|
|
9082
8586
|
import { v4 as uuidv4 } from "uuid";
|
|
9083
8587
|
import axios from "axios";
|
|
9084
8588
|
var RouterModuleV2 = class {
|
|
@@ -9095,7 +8599,7 @@ var RouterModuleV2 = class {
|
|
|
9095
8599
|
if (label === "Magma") {
|
|
9096
8600
|
return TickMath.sqrtPriceX64ToPrice(currentSqrtPrice, decimalA, decimalB);
|
|
9097
8601
|
}
|
|
9098
|
-
return new
|
|
8602
|
+
return new Decimal6(currentSqrtPrice.toString()).div(new Decimal6(10).pow(new Decimal6(decimalB + 9 - decimalA)));
|
|
9099
8603
|
}
|
|
9100
8604
|
parseJsonResult(data) {
|
|
9101
8605
|
const result = {
|
|
@@ -9121,12 +8625,12 @@ var RouterModuleV2 = class {
|
|
|
9121
8625
|
outputAmount: basePath.output_amount,
|
|
9122
8626
|
inputAmount: basePath.input_amount,
|
|
9123
8627
|
feeRate: basePath.fee_rate,
|
|
9124
|
-
currentSqrtPrice: new
|
|
9125
|
-
afterSqrtPrice: basePath.label === "Magma" ? new
|
|
8628
|
+
currentSqrtPrice: new BN19(basePath.current_sqrt_price.toString()),
|
|
8629
|
+
afterSqrtPrice: basePath.label === "Magma" ? new BN19(basePath.after_sqrt_price.toString()) : ZERO,
|
|
9126
8630
|
fromDecimal: basePath.from_decimal,
|
|
9127
8631
|
toDecimal: basePath.to_decimal,
|
|
9128
8632
|
currentPrice: this.calculatePrice(
|
|
9129
|
-
new
|
|
8633
|
+
new BN19(basePath.current_sqrt_price.toString()),
|
|
9130
8634
|
basePath.from_decimal,
|
|
9131
8635
|
basePath.to_decimal,
|
|
9132
8636
|
basePath.direction,
|
|
@@ -9209,7 +8713,7 @@ var RouterModuleV2 = class {
|
|
|
9209
8713
|
const priceResult = await this.sdk.Router.priceUseV1(
|
|
9210
8714
|
from,
|
|
9211
8715
|
to,
|
|
9212
|
-
new
|
|
8716
|
+
new BN19(amount),
|
|
9213
8717
|
byAmountIn,
|
|
9214
8718
|
priceSplitPoint,
|
|
9215
8719
|
partner,
|
|
@@ -9221,7 +8725,7 @@ var RouterModuleV2 = class {
|
|
|
9221
8725
|
if (path.poolAddress.length > 1) {
|
|
9222
8726
|
const fromDecimal0 = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
|
|
9223
8727
|
const toDecimal0 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9224
|
-
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8728
|
+
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
|
|
9225
8729
|
const path0 = {
|
|
9226
8730
|
direction: path.a2b[0],
|
|
9227
8731
|
label: "Magma",
|
|
@@ -9238,7 +8742,7 @@ var RouterModuleV2 = class {
|
|
|
9238
8742
|
};
|
|
9239
8743
|
const fromDecimal1 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9240
8744
|
const toDecimal1 = this.sdk.Router.tokenInfo(path.coinType[2]).decimals;
|
|
9241
|
-
const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8745
|
+
const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
|
|
9242
8746
|
const path1 = {
|
|
9243
8747
|
direction: path.a2b[1],
|
|
9244
8748
|
label: "Magma",
|
|
@@ -9257,7 +8761,7 @@ var RouterModuleV2 = class {
|
|
|
9257
8761
|
} else {
|
|
9258
8762
|
const fromDecimal = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
|
|
9259
8763
|
const toDecimal = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9260
|
-
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8764
|
+
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
|
|
9261
8765
|
const path0 = {
|
|
9262
8766
|
direction: path.a2b[0],
|
|
9263
8767
|
label: "Magma",
|
|
@@ -9342,7 +8846,7 @@ var RouterModuleV2 = class {
|
|
|
9342
8846
|
const priceResult = await this.sdk.Router.priceUseV1(
|
|
9343
8847
|
from,
|
|
9344
8848
|
to,
|
|
9345
|
-
new
|
|
8849
|
+
new BN19(amount),
|
|
9346
8850
|
byAmountIn,
|
|
9347
8851
|
priceSplitPoint,
|
|
9348
8852
|
partner,
|
|
@@ -9354,7 +8858,7 @@ var RouterModuleV2 = class {
|
|
|
9354
8858
|
if (path.poolAddress.length > 1) {
|
|
9355
8859
|
const fromDecimal0 = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
|
|
9356
8860
|
const toDecimal0 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9357
|
-
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8861
|
+
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal0, toDecimal0) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal0, fromDecimal0);
|
|
9358
8862
|
const path0 = {
|
|
9359
8863
|
direction: path.a2b[0],
|
|
9360
8864
|
label: "Magma",
|
|
@@ -9371,7 +8875,7 @@ var RouterModuleV2 = class {
|
|
|
9371
8875
|
};
|
|
9372
8876
|
const fromDecimal1 = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9373
8877
|
const toDecimal1 = this.sdk.Router.tokenInfo(path.coinType[2]).decimals;
|
|
9374
|
-
const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8878
|
+
const currentPrice1 = path.a2b[1] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), fromDecimal1, toDecimal1) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[1]), toDecimal1, fromDecimal1);
|
|
9375
8879
|
const path1 = {
|
|
9376
8880
|
direction: path.a2b[1],
|
|
9377
8881
|
label: "Magma",
|
|
@@ -9390,7 +8894,7 @@ var RouterModuleV2 = class {
|
|
|
9390
8894
|
} else {
|
|
9391
8895
|
const fromDecimal = this.sdk.Router.tokenInfo(path.coinType[0]).decimals;
|
|
9392
8896
|
const toDecimal = this.sdk.Router.tokenInfo(path.coinType[1]).decimals;
|
|
9393
|
-
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new
|
|
8897
|
+
const currentPrice = path.a2b[0] ? TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), fromDecimal, toDecimal) : TickMath.sqrtPriceX64ToPrice(new BN19(priceResult.currentSqrtPrice[0]), toDecimal, fromDecimal);
|
|
9394
8898
|
const path0 = {
|
|
9395
8899
|
direction: path.a2b[0],
|
|
9396
8900
|
label: "Magma",
|
|
@@ -10265,21 +9769,6 @@ var GaugeModule = class {
|
|
|
10265
9769
|
});
|
|
10266
9770
|
return tx;
|
|
10267
9771
|
}
|
|
10268
|
-
async getAllRewardByPositions(paramsList) {
|
|
10269
|
-
const tx = new Transaction11();
|
|
10270
|
-
const { integrate } = this.sdk.sdkOptions;
|
|
10271
|
-
const { magma_token } = getPackagerConfigs(this.sdk.sdkOptions.magma_config);
|
|
10272
|
-
paramsList.forEach((params) => {
|
|
10273
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB, magma_token];
|
|
10274
|
-
const args = [tx.object(params.gaugeId), tx.object(params.poolId), tx.object(params.positionId), tx.object(CLOCK_ADDRESS)];
|
|
10275
|
-
tx.moveCall({
|
|
10276
|
-
target: `${integrate.published_at}::${Gauge}::get_reward_by_position`,
|
|
10277
|
-
arguments: args,
|
|
10278
|
-
typeArguments
|
|
10279
|
-
});
|
|
10280
|
-
});
|
|
10281
|
-
return tx;
|
|
10282
|
-
}
|
|
10283
9772
|
async getEpochRewardByPool(pool, incentive_tokens) {
|
|
10284
9773
|
const tx = new Transaction11();
|
|
10285
9774
|
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
@@ -10312,898 +9801,8 @@ var GaugeModule = class {
|
|
|
10312
9801
|
}
|
|
10313
9802
|
};
|
|
10314
9803
|
|
|
10315
|
-
// src/
|
|
10316
|
-
|
|
10317
|
-
import {
|
|
10318
|
-
get_price_x128_from_real_id as get_price_x128_from_real_id3,
|
|
10319
|
-
get_real_id,
|
|
10320
|
-
get_real_id_from_price_x128 as get_real_id_from_price_x1282,
|
|
10321
|
-
get_storage_id_from_real_id
|
|
10322
|
-
} from "@magmaprotocol/calc_dlmm";
|
|
10323
|
-
import Decimal10 from "decimal.js";
|
|
10324
|
-
var DlmmModule = class {
|
|
10325
|
-
_sdk;
|
|
10326
|
-
_cache = {};
|
|
10327
|
-
constructor(sdk) {
|
|
10328
|
-
this._sdk = sdk;
|
|
10329
|
-
}
|
|
10330
|
-
get sdk() {
|
|
10331
|
-
return this._sdk;
|
|
10332
|
-
}
|
|
10333
|
-
async getPoolInfo(pools) {
|
|
10334
|
-
const objects = await this._sdk.fullClient.batchGetObjects(pools, { showContent: true });
|
|
10335
|
-
const poolList = [];
|
|
10336
|
-
objects.forEach((obj) => {
|
|
10337
|
-
if (obj.error != null || obj.data?.content?.dataType !== "moveObject") {
|
|
10338
|
-
throw new ClmmpoolsError(`Invalid objects. error: ${obj.error}`, "InvalidType" /* InvalidType */);
|
|
10339
|
-
}
|
|
10340
|
-
const fields = getObjectFields(obj);
|
|
10341
|
-
poolList.push({
|
|
10342
|
-
pool_id: fields.id.id,
|
|
10343
|
-
bin_step: fields.bin_step,
|
|
10344
|
-
coin_a: fields.x.fields.name,
|
|
10345
|
-
coin_b: fields.y.fields.name,
|
|
10346
|
-
base_factor: fields.params.fields.base_factor,
|
|
10347
|
-
base_fee: fields.params.fields.base_factor / 1e4 * (fields.bin_step / 1e4),
|
|
10348
|
-
active_index: fields.params.fields.active_index
|
|
10349
|
-
});
|
|
10350
|
-
});
|
|
10351
|
-
return poolList;
|
|
10352
|
-
}
|
|
10353
|
-
// eg: fetch pool active_index
|
|
10354
|
-
async fetchPairParams(params) {
|
|
10355
|
-
const tx = new Transaction12();
|
|
10356
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
10357
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10358
|
-
const args = [tx.object(params.pair)];
|
|
10359
|
-
tx.moveCall({
|
|
10360
|
-
target: `${integrate.published_at}::${DlmmScript}::fetch_pair_params`,
|
|
10361
|
-
arguments: args,
|
|
10362
|
-
typeArguments
|
|
10363
|
-
});
|
|
10364
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
10365
|
-
transactionBlock: tx,
|
|
10366
|
-
sender: simulationAccount.address
|
|
10367
|
-
});
|
|
10368
|
-
if (simulateRes.error != null) {
|
|
10369
|
-
throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
|
|
10370
|
-
}
|
|
10371
|
-
let res = {
|
|
10372
|
-
base_factor: 0,
|
|
10373
|
-
filter_period: 0,
|
|
10374
|
-
decay_period: 0,
|
|
10375
|
-
reduction_factor: 0,
|
|
10376
|
-
variable_fee_control: 0,
|
|
10377
|
-
protocol_share: 0,
|
|
10378
|
-
max_volatility_accumulator: 0,
|
|
10379
|
-
volatility_accumulator: 0,
|
|
10380
|
-
volatility_reference: 0,
|
|
10381
|
-
index_reference: 0,
|
|
10382
|
-
time_of_last_update: 0,
|
|
10383
|
-
oracle_index: 0,
|
|
10384
|
-
active_index: 0
|
|
10385
|
-
};
|
|
10386
|
-
simulateRes.events?.forEach((item) => {
|
|
10387
|
-
console.log(extractStructTagFromType(item.type).name);
|
|
10388
|
-
if (extractStructTagFromType(item.type).name === `EventPairParams`) {
|
|
10389
|
-
res = item.parsedJson.params;
|
|
10390
|
-
}
|
|
10391
|
-
});
|
|
10392
|
-
return res;
|
|
10393
|
-
}
|
|
10394
|
-
// NOTE: x, y should be sorted
|
|
10395
|
-
async createPairPayload(params) {
|
|
10396
|
-
const storage_id = get_storage_id_from_real_id(
|
|
10397
|
-
BinMath.getBinIdFromPrice(params.priceTokenBPerTokenA, params.bin_step, params.coinADecimal, params.coinBDecimal)
|
|
10398
|
-
);
|
|
10399
|
-
const tx = new Transaction12();
|
|
10400
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10401
|
-
const { clmm_pool, dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10402
|
-
const { global_config_id } = getPackagerConfigs(clmm_pool);
|
|
10403
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10404
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10405
|
-
const args = [
|
|
10406
|
-
tx.object(dlmmConfig.factory),
|
|
10407
|
-
tx.object(global_config_id),
|
|
10408
|
-
tx.pure.u64(params.base_fee),
|
|
10409
|
-
tx.pure.u16(params.bin_step),
|
|
10410
|
-
tx.pure.u32(storage_id)
|
|
10411
|
-
];
|
|
10412
|
-
tx.moveCall({
|
|
10413
|
-
target: `${integrate.published_at}::${DlmmScript}::create_pair`,
|
|
10414
|
-
typeArguments,
|
|
10415
|
-
arguments: args
|
|
10416
|
-
});
|
|
10417
|
-
return tx;
|
|
10418
|
-
}
|
|
10419
|
-
// async mintByStrategySingle(params: MintByStrategySingleParams): Promise<Transaction> {}
|
|
10420
|
-
async mintByStrategy(params) {
|
|
10421
|
-
const tx = new Transaction12();
|
|
10422
|
-
const slippage = new Decimal10(params.slippage);
|
|
10423
|
-
const lower_slippage = new Decimal10(1).sub(slippage.div(new Decimal10(1e4)));
|
|
10424
|
-
const upper_slippage = new Decimal10(1).plus(slippage.div(new Decimal10(1e4)));
|
|
10425
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10426
|
-
const { dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10427
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10428
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10429
|
-
const price = get_price_x128_from_real_id3(params.active_bin, params.bin_step);
|
|
10430
|
-
const min_price = new Decimal10(price).mul(lower_slippage);
|
|
10431
|
-
const max_price = new Decimal10(price).mul(upper_slippage);
|
|
10432
|
-
const active_min = get_storage_id_from_real_id(get_real_id_from_price_x1282(min_price.toDecimalPlaces(0).toString(), params.bin_step));
|
|
10433
|
-
const active_max = get_storage_id_from_real_id(get_real_id_from_price_x1282(max_price.toDecimalPlaces(0).toString(), params.bin_step));
|
|
10434
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10435
|
-
let primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountATotal), params.coinTypeA, false, true);
|
|
10436
|
-
let primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountBTotal), params.coinTypeB, false, true);
|
|
10437
|
-
let amount_min = 0;
|
|
10438
|
-
let amount_max = 0;
|
|
10439
|
-
if (params.fixCoinA) {
|
|
10440
|
-
amount_min = new Decimal10(params.amountBTotal).mul(lower_slippage).toDecimalPlaces(0).toNumber();
|
|
10441
|
-
amount_max = new Decimal10(params.amountBTotal).mul(upper_slippage).toDecimalPlaces(0).toNumber();
|
|
10442
|
-
primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amount_max), params.coinTypeB, false, true);
|
|
10443
|
-
}
|
|
10444
|
-
if (params.fixCoinB) {
|
|
10445
|
-
amount_min = new Decimal10(params.amountATotal).mul(lower_slippage).toDecimalPlaces(0).toNumber();
|
|
10446
|
-
amount_max = new Decimal10(params.amountATotal).mul(upper_slippage).toDecimalPlaces(0).toNumber();
|
|
10447
|
-
primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amount_max), params.coinTypeB, false, true);
|
|
10448
|
-
}
|
|
10449
|
-
if (params.fixCoinA && params.fixCoinB) {
|
|
10450
|
-
} else if (params.fixCoinA) {
|
|
10451
|
-
params.amountBTotal = 0;
|
|
10452
|
-
} else if (params.fixCoinB) {
|
|
10453
|
-
params.amountATotal = 0;
|
|
10454
|
-
}
|
|
10455
|
-
const args = [
|
|
10456
|
-
tx.object(params.pair),
|
|
10457
|
-
tx.object(dlmmConfig.factory),
|
|
10458
|
-
primaryCoinAInputs.targetCoin,
|
|
10459
|
-
primaryCoinBInputs.targetCoin,
|
|
10460
|
-
tx.pure.u64(params.amountATotal),
|
|
10461
|
-
tx.pure.u64(params.amountBTotal),
|
|
10462
|
-
tx.pure.u8(params.strategy),
|
|
10463
|
-
tx.pure.u32(get_storage_id_from_real_id(params.min_bin)),
|
|
10464
|
-
tx.pure.u32(get_storage_id_from_real_id(params.max_bin)),
|
|
10465
|
-
tx.pure.u32(active_min),
|
|
10466
|
-
tx.pure.u32(active_max),
|
|
10467
|
-
tx.pure.u64(amount_min),
|
|
10468
|
-
tx.pure.u64(amount_max),
|
|
10469
|
-
tx.object(CLOCK_ADDRESS)
|
|
10470
|
-
];
|
|
10471
|
-
tx.moveCall({
|
|
10472
|
-
target: `${integrate.published_at}::${DlmmScript}::mint_by_strategy`,
|
|
10473
|
-
typeArguments,
|
|
10474
|
-
arguments: args
|
|
10475
|
-
});
|
|
10476
|
-
return tx;
|
|
10477
|
-
}
|
|
10478
|
-
// Create a position by percent
|
|
10479
|
-
async mintPercent(params) {
|
|
10480
|
-
const tx = new Transaction12();
|
|
10481
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10482
|
-
const { dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10483
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10484
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10485
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10486
|
-
const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountATotal), params.coinTypeA, false, true);
|
|
10487
|
-
const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountBTotal), params.coinTypeB, false, true);
|
|
10488
|
-
const args = [
|
|
10489
|
-
tx.object(params.pair),
|
|
10490
|
-
tx.object(dlmmConfig.factory),
|
|
10491
|
-
primaryCoinAInputs.targetCoin,
|
|
10492
|
-
primaryCoinBInputs.targetCoin,
|
|
10493
|
-
tx.pure.u64(params.amountATotal),
|
|
10494
|
-
tx.pure.u64(params.amountBTotal),
|
|
10495
|
-
tx.pure.vector("u32", params.storageIds),
|
|
10496
|
-
tx.pure.vector("u64", params.binsAPercent),
|
|
10497
|
-
tx.pure.vector("u64", params.binsBPercent),
|
|
10498
|
-
tx.pure.address(params.to),
|
|
10499
|
-
tx.object(CLOCK_ADDRESS)
|
|
10500
|
-
];
|
|
10501
|
-
tx.moveCall({
|
|
10502
|
-
target: `${integrate.published_at}::${DlmmScript}::mint_percent`,
|
|
10503
|
-
typeArguments,
|
|
10504
|
-
arguments: args
|
|
10505
|
-
});
|
|
10506
|
-
return tx;
|
|
10507
|
-
}
|
|
10508
|
-
// Create a position by amount
|
|
10509
|
-
async createPositionByAmount(params) {
|
|
10510
|
-
const tx = new Transaction12();
|
|
10511
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10512
|
-
const { dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10513
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10514
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10515
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10516
|
-
const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountATotal), params.coinTypeA, false, true);
|
|
10517
|
-
const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountBTotal), params.coinTypeB, false, true);
|
|
10518
|
-
const args = [
|
|
10519
|
-
tx.object(params.pair),
|
|
10520
|
-
tx.object(dlmmConfig.factory),
|
|
10521
|
-
primaryCoinAInputs.targetCoin,
|
|
10522
|
-
primaryCoinBInputs.targetCoin,
|
|
10523
|
-
tx.pure.vector("u32", params.storageIds),
|
|
10524
|
-
tx.pure.vector("u64", params.amountsA),
|
|
10525
|
-
tx.pure.vector("u64", params.amountsB),
|
|
10526
|
-
tx.pure.address(params.to),
|
|
10527
|
-
tx.object(CLOCK_ADDRESS)
|
|
10528
|
-
];
|
|
10529
|
-
tx.moveCall({
|
|
10530
|
-
target: `${integrate.published_at}::${DlmmScript}::mint_amounts`,
|
|
10531
|
-
typeArguments,
|
|
10532
|
-
arguments: args
|
|
10533
|
-
});
|
|
10534
|
-
return tx;
|
|
10535
|
-
}
|
|
10536
|
-
async addLiquidity(params) {
|
|
10537
|
-
if (params.rewards_token.length === 0) {
|
|
10538
|
-
return this._raisePositionByAmounts(params);
|
|
10539
|
-
}
|
|
10540
|
-
return this._raisePositionByAmountsReward(params);
|
|
10541
|
-
}
|
|
10542
|
-
async _raisePositionByAmounts(params) {
|
|
10543
|
-
const tx = new Transaction12();
|
|
10544
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10545
|
-
const { dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10546
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10547
|
-
const typeArguments = [params.coin_a, params.coin_b];
|
|
10548
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10549
|
-
const amountATotal = params.amounts_a.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10550
|
-
const amountBTotal = params.amounts_b.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10551
|
-
const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountATotal), params.coin_a, false, true);
|
|
10552
|
-
const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountBTotal), params.coin_b, false, true);
|
|
10553
|
-
const args = [
|
|
10554
|
-
tx.object(params.pool_id),
|
|
10555
|
-
tx.object(dlmmConfig.factory),
|
|
10556
|
-
tx.object(params.position_id),
|
|
10557
|
-
primaryCoinAInputs.targetCoin,
|
|
10558
|
-
primaryCoinBInputs.targetCoin,
|
|
10559
|
-
tx.pure.vector("u64", params.amounts_a),
|
|
10560
|
-
tx.pure.vector("u64", params.amounts_b),
|
|
10561
|
-
tx.pure.address(params.receiver),
|
|
10562
|
-
tx.object(CLOCK_ADDRESS)
|
|
10563
|
-
];
|
|
10564
|
-
tx.moveCall({
|
|
10565
|
-
target: `${integrate.published_at}::${DlmmScript}::raise_position_by_amounts`,
|
|
10566
|
-
typeArguments,
|
|
10567
|
-
arguments: args
|
|
10568
|
-
});
|
|
10569
|
-
return tx;
|
|
10570
|
-
}
|
|
10571
|
-
async _raisePositionByAmountsReward(params) {
|
|
10572
|
-
const tx = new Transaction12();
|
|
10573
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10574
|
-
const { dlmm_pool, integrate, clmm_pool } = this.sdk.sdkOptions;
|
|
10575
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10576
|
-
const clmmConfigs = getPackagerConfigs(clmm_pool);
|
|
10577
|
-
const typeArguments = [params.coin_a, params.coin_b, ...params.rewards_token];
|
|
10578
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10579
|
-
const amountATotal = params.amounts_a.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10580
|
-
const amountBTotal = params.amounts_b.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10581
|
-
const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountATotal), params.coin_a, false, true);
|
|
10582
|
-
const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountBTotal), params.coin_b, false, true);
|
|
10583
|
-
const args = [
|
|
10584
|
-
tx.object(params.pool_id),
|
|
10585
|
-
tx.object(dlmmConfig.factory),
|
|
10586
|
-
tx.object(clmmConfigs.global_vault_id),
|
|
10587
|
-
tx.object(params.position_id),
|
|
10588
|
-
primaryCoinAInputs.targetCoin,
|
|
10589
|
-
primaryCoinBInputs.targetCoin,
|
|
10590
|
-
tx.pure.vector("u64", params.amounts_a),
|
|
10591
|
-
tx.pure.vector("u64", params.amounts_b),
|
|
10592
|
-
tx.pure.address(params.receiver),
|
|
10593
|
-
tx.object(CLOCK_ADDRESS)
|
|
10594
|
-
];
|
|
10595
|
-
tx.moveCall({
|
|
10596
|
-
target: `${integrate.published_at}::${DlmmScript}::raise_position_by_amounts_reward${params.rewards_token.length}`,
|
|
10597
|
-
typeArguments,
|
|
10598
|
-
arguments: args
|
|
10599
|
-
});
|
|
10600
|
-
return tx;
|
|
10601
|
-
}
|
|
10602
|
-
async burnPosition(params) {
|
|
10603
|
-
const tx = new Transaction12();
|
|
10604
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10605
|
-
const { integrate, clmm_pool } = this.sdk.sdkOptions;
|
|
10606
|
-
const clmmConfigs = getPackagerConfigs(clmm_pool);
|
|
10607
|
-
const typeArguments = [params.coin_a, params.coin_b, ...params.rewards_token];
|
|
10608
|
-
let args = [tx.object(params.pool_id), tx.object(params.position_id), tx.object(CLOCK_ADDRESS)];
|
|
10609
|
-
let target = `${integrate.published_at}::${DlmmScript}::burn_position`;
|
|
10610
|
-
if (params.rewards_token.length > 0) {
|
|
10611
|
-
args = [tx.object(params.pool_id), tx.object(clmmConfigs.global_vault_id), tx.object(params.position_id), tx.object(CLOCK_ADDRESS)];
|
|
10612
|
-
target = `${integrate.published_at}::${DlmmScript}::burn_position_reward${params.rewards_token.length}`;
|
|
10613
|
-
}
|
|
10614
|
-
tx.moveCall({
|
|
10615
|
-
target,
|
|
10616
|
-
typeArguments,
|
|
10617
|
-
arguments: args
|
|
10618
|
-
});
|
|
10619
|
-
return tx;
|
|
10620
|
-
}
|
|
10621
|
-
async shrinkPosition(params) {
|
|
10622
|
-
const tx = new Transaction12();
|
|
10623
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10624
|
-
const { integrate, clmm_pool } = this.sdk.sdkOptions;
|
|
10625
|
-
const clmmConfigs = getPackagerConfigs(clmm_pool);
|
|
10626
|
-
const typeArguments = [params.coin_a, params.coin_b, ...params.rewards_token];
|
|
10627
|
-
let args = [tx.object(params.pool_id), tx.object(params.position_id), tx.pure.u64(params.delta_percentage), tx.object(CLOCK_ADDRESS)];
|
|
10628
|
-
let target = `${integrate.published_at}::${DlmmScript}::shrink_position`;
|
|
10629
|
-
if (params.rewards_token.length > 0) {
|
|
10630
|
-
args = [
|
|
10631
|
-
tx.object(params.pool_id),
|
|
10632
|
-
tx.object(clmmConfigs.global_vault_id),
|
|
10633
|
-
tx.object(params.position_id),
|
|
10634
|
-
tx.pure.u64(params.delta_percentage),
|
|
10635
|
-
tx.object(CLOCK_ADDRESS)
|
|
10636
|
-
];
|
|
10637
|
-
target = `${integrate.published_at}::${DlmmScript}::shrink_position_reward${params.rewards_token.length}`;
|
|
10638
|
-
}
|
|
10639
|
-
tx.moveCall({
|
|
10640
|
-
target,
|
|
10641
|
-
typeArguments,
|
|
10642
|
-
arguments: args
|
|
10643
|
-
});
|
|
10644
|
-
return tx;
|
|
10645
|
-
}
|
|
10646
|
-
async collectFeeAndReward(params) {
|
|
10647
|
-
let tx = new Transaction12();
|
|
10648
|
-
tx = await this.collectFees(params);
|
|
10649
|
-
if (params.rewards_token.length > 0) {
|
|
10650
|
-
tx = await this.collectReward(params);
|
|
10651
|
-
}
|
|
10652
|
-
return tx;
|
|
10653
|
-
}
|
|
10654
|
-
async collectReward(params, transaction) {
|
|
10655
|
-
const tx = transaction || new Transaction12();
|
|
10656
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10657
|
-
const { integrate, clmm_pool } = this.sdk.sdkOptions;
|
|
10658
|
-
const clmmConfigs = getPackagerConfigs(clmm_pool);
|
|
10659
|
-
const typeArguments = [params.coin_a, params.coin_b, ...params.rewards_token];
|
|
10660
|
-
const args = [
|
|
10661
|
-
tx.object(params.pool_id),
|
|
10662
|
-
tx.object(clmmConfigs.global_vault_id),
|
|
10663
|
-
tx.object(params.position_id),
|
|
10664
|
-
tx.object(CLOCK_ADDRESS)
|
|
10665
|
-
];
|
|
10666
|
-
let target = `${integrate.published_at}::${DlmmScript}::collect_reward`;
|
|
10667
|
-
if (params.rewards_token.length > 1) {
|
|
10668
|
-
target = `${integrate.published_at}::${DlmmScript}::collect_reward${params.rewards_token.length}`;
|
|
10669
|
-
}
|
|
10670
|
-
tx.moveCall({
|
|
10671
|
-
target,
|
|
10672
|
-
typeArguments,
|
|
10673
|
-
arguments: args
|
|
10674
|
-
});
|
|
10675
|
-
return tx;
|
|
10676
|
-
}
|
|
10677
|
-
async collectFees(params, transaction) {
|
|
10678
|
-
const tx = transaction || new Transaction12();
|
|
10679
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10680
|
-
const { integrate } = this.sdk.sdkOptions;
|
|
10681
|
-
const typeArguments = [params.coin_a, params.coin_b];
|
|
10682
|
-
const args = [tx.object(params.pool_id), tx.object(params.position_id), tx.object(CLOCK_ADDRESS)];
|
|
10683
|
-
const target = `${integrate.published_at}::${DlmmScript}::collect_fees`;
|
|
10684
|
-
tx.moveCall({
|
|
10685
|
-
target,
|
|
10686
|
-
typeArguments,
|
|
10687
|
-
arguments: args
|
|
10688
|
-
});
|
|
10689
|
-
return tx;
|
|
10690
|
-
}
|
|
10691
|
-
async createPairAddLiquidity(params) {
|
|
10692
|
-
const tx = new Transaction12();
|
|
10693
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10694
|
-
const { clmm_pool, dlmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10695
|
-
const { global_config_id } = getPackagerConfigs(clmm_pool);
|
|
10696
|
-
const dlmmConfig = getPackagerConfigs(dlmm_pool);
|
|
10697
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10698
|
-
const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10699
|
-
const amountATotal = params.amountsX.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10700
|
-
const amountBTotal = params.amountsY.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
|
|
10701
|
-
const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountATotal), params.coinTypeA, false, true);
|
|
10702
|
-
const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(amountBTotal), params.coinTypeB, false, true);
|
|
10703
|
-
const storageIds = [];
|
|
10704
|
-
params.realIds.forEach((i) => {
|
|
10705
|
-
storageIds.push(get_storage_id_from_real_id(i));
|
|
10706
|
-
});
|
|
10707
|
-
const args = [
|
|
10708
|
-
tx.object(dlmmConfig.factory),
|
|
10709
|
-
tx.object(global_config_id),
|
|
10710
|
-
tx.pure.u64(params.baseFee),
|
|
10711
|
-
tx.pure.u16(params.binStep),
|
|
10712
|
-
tx.pure.u32(get_storage_id_from_real_id(params.activeId)),
|
|
10713
|
-
primaryCoinAInputs.targetCoin,
|
|
10714
|
-
primaryCoinBInputs.targetCoin,
|
|
10715
|
-
tx.pure.vector("u32", storageIds),
|
|
10716
|
-
tx.pure.vector("u64", params.amountsX),
|
|
10717
|
-
tx.pure.vector("u64", params.amountsY),
|
|
10718
|
-
tx.pure.address(params.to),
|
|
10719
|
-
tx.object(CLOCK_ADDRESS)
|
|
10720
|
-
];
|
|
10721
|
-
const target = `${integrate.published_at}::${DlmmScript}::create_pair_add_liquidity`;
|
|
10722
|
-
tx.moveCall({
|
|
10723
|
-
target,
|
|
10724
|
-
typeArguments,
|
|
10725
|
-
arguments: args
|
|
10726
|
-
});
|
|
10727
|
-
return tx;
|
|
10728
|
-
}
|
|
10729
|
-
async swap(params) {
|
|
10730
|
-
const tx = new Transaction12();
|
|
10731
|
-
tx.setSender(this.sdk.senderAddress);
|
|
10732
|
-
const { clmm_pool, integrate } = this.sdk.sdkOptions;
|
|
10733
|
-
const { global_config_id } = getPackagerConfigs(clmm_pool);
|
|
10734
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10735
|
-
const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
|
|
10736
|
-
const primaryCoinInputA = TransactionUtil.buildCoinForAmount(
|
|
10737
|
-
tx,
|
|
10738
|
-
allCoinAsset,
|
|
10739
|
-
params.swapForY ? BigInt(params.amountIn) : BigInt(0),
|
|
10740
|
-
params.coinTypeA,
|
|
10741
|
-
false,
|
|
10742
|
-
true
|
|
10743
|
-
);
|
|
10744
|
-
const primaryCoinInputB = TransactionUtil.buildCoinForAmount(
|
|
10745
|
-
tx,
|
|
10746
|
-
allCoinAsset,
|
|
10747
|
-
params.swapForY ? BigInt(0) : BigInt(params.amountIn),
|
|
10748
|
-
params.coinTypeB,
|
|
10749
|
-
false,
|
|
10750
|
-
true
|
|
10751
|
-
);
|
|
10752
|
-
const args = [
|
|
10753
|
-
tx.object(params.pair),
|
|
10754
|
-
tx.object(global_config_id),
|
|
10755
|
-
primaryCoinInputA.targetCoin,
|
|
10756
|
-
primaryCoinInputB.targetCoin,
|
|
10757
|
-
tx.pure.u64(params.amountIn),
|
|
10758
|
-
tx.pure.u64(params.minAmountOut),
|
|
10759
|
-
tx.pure.bool(params.swapForY),
|
|
10760
|
-
tx.pure.address(params.to),
|
|
10761
|
-
tx.object(CLOCK_ADDRESS)
|
|
10762
|
-
];
|
|
10763
|
-
tx.moveCall({
|
|
10764
|
-
target: `${integrate.published_at}::${DlmmScript}::swap`,
|
|
10765
|
-
typeArguments,
|
|
10766
|
-
arguments: args
|
|
10767
|
-
});
|
|
10768
|
-
return tx;
|
|
10769
|
-
}
|
|
10770
|
-
async fetchBins(params) {
|
|
10771
|
-
const tx = new Transaction12();
|
|
10772
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
10773
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10774
|
-
const args = [tx.object(params.pair), tx.pure.u64(params.offset), tx.pure.u64(params.limit)];
|
|
10775
|
-
tx.moveCall({
|
|
10776
|
-
target: `${integrate.published_at}::${DlmmScript}::fetch_bins`,
|
|
10777
|
-
arguments: args,
|
|
10778
|
-
typeArguments
|
|
10779
|
-
});
|
|
10780
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
10781
|
-
transactionBlock: tx,
|
|
10782
|
-
sender: simulationAccount.address
|
|
10783
|
-
});
|
|
10784
|
-
if (simulateRes.error != null) {
|
|
10785
|
-
throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
|
|
10786
|
-
}
|
|
10787
|
-
let res = [];
|
|
10788
|
-
simulateRes.events?.forEach((item) => {
|
|
10789
|
-
if (extractStructTagFromType(item.type).name === `EventFetchBins`) {
|
|
10790
|
-
const { bins } = item.parsedJson;
|
|
10791
|
-
res = bins;
|
|
10792
|
-
}
|
|
10793
|
-
});
|
|
10794
|
-
return res;
|
|
10795
|
-
}
|
|
10796
|
-
/**
|
|
10797
|
-
* Gets a list of positions for the given account address.
|
|
10798
|
-
* @param accountAddress The account address to get positions for.
|
|
10799
|
-
* @param assignPoolIds An array of pool IDs to filter the positions by.
|
|
10800
|
-
* @returns array of Position objects.
|
|
10801
|
-
*/
|
|
10802
|
-
async getUserPositionById(positionId, showDisplay = true) {
|
|
10803
|
-
let position;
|
|
10804
|
-
const ownerRes = await this._sdk.fullClient.getObject({
|
|
10805
|
-
id: positionId,
|
|
10806
|
-
options: { showContent: true, showType: true, showDisplay, showOwner: true }
|
|
10807
|
-
});
|
|
10808
|
-
const type = extractStructTagFromType(ownerRes.data.type);
|
|
10809
|
-
if (type.full_address === this.buildPositionType()) {
|
|
10810
|
-
position = this.buildPosition(ownerRes);
|
|
10811
|
-
} else {
|
|
10812
|
-
throw new ClmmpoolsError(`Dlmm Position not exists. Get Position error:${ownerRes.error}`, "InvalidPositionObject" /* InvalidPositionObject */);
|
|
10813
|
-
}
|
|
10814
|
-
const poolMap = /* @__PURE__ */ new Set();
|
|
10815
|
-
poolMap.add(position.pool);
|
|
10816
|
-
const pool = (await this.getPoolInfo(Array.from(poolMap)))[0];
|
|
10817
|
-
const _params = [];
|
|
10818
|
-
_params.push({
|
|
10819
|
-
pool_id: pool.pool_id,
|
|
10820
|
-
coin_a: pool.coin_a,
|
|
10821
|
-
coin_b: pool.coin_b
|
|
10822
|
-
});
|
|
10823
|
-
const pool_reward_coins = await this.getPairRewarders(_params);
|
|
10824
|
-
const positionLiquidity = await this.getPositionLiquidity({
|
|
10825
|
-
pair: position.pool,
|
|
10826
|
-
positionId: position.pos_object_id,
|
|
10827
|
-
coinTypeA: pool.coin_a,
|
|
10828
|
-
coinTypeB: pool.coin_b
|
|
10829
|
-
});
|
|
10830
|
-
const rewards_token = pool_reward_coins.get(position.pool) || [];
|
|
10831
|
-
let positionRewards = { position_id: position.pos_object_id, reward: [], amount: [] };
|
|
10832
|
-
if (rewards_token.length > 0) {
|
|
10833
|
-
positionRewards = await this.getEarnedRewards({
|
|
10834
|
-
pool_id: position.pool,
|
|
10835
|
-
position_id: position.pos_object_id,
|
|
10836
|
-
coin_a: pool.coin_a,
|
|
10837
|
-
coin_b: pool.coin_b,
|
|
10838
|
-
rewards_token: pool_reward_coins.get(position.pool) || []
|
|
10839
|
-
});
|
|
10840
|
-
}
|
|
10841
|
-
const positionFees = await this.getEarnedFees({
|
|
10842
|
-
pool_id: position.pool,
|
|
10843
|
-
position_id: position.pos_object_id,
|
|
10844
|
-
coin_a: pool.coin_a,
|
|
10845
|
-
coin_b: pool.coin_b
|
|
10846
|
-
});
|
|
10847
|
-
return {
|
|
10848
|
-
position,
|
|
10849
|
-
liquidity: positionLiquidity,
|
|
10850
|
-
rewards: positionRewards,
|
|
10851
|
-
fees: positionFees,
|
|
10852
|
-
contractPool: pool
|
|
10853
|
-
};
|
|
10854
|
-
}
|
|
10855
|
-
/**
|
|
10856
|
-
* Gets a list of positions for the given account address.
|
|
10857
|
-
* @param accountAddress The account address to get positions for.
|
|
10858
|
-
* @param assignPoolIds An array of pool IDs to filter the positions by.
|
|
10859
|
-
* @returns array of Position objects.
|
|
10860
|
-
*/
|
|
10861
|
-
async getUserPositions(accountAddress, assignPoolIds = [], showDisplay = true) {
|
|
10862
|
-
const allPosition = [];
|
|
10863
|
-
const ownerRes = await this._sdk.fullClient.getOwnedObjectsByPage(accountAddress, {
|
|
10864
|
-
options: { showType: true, showContent: true, showDisplay, showOwner: true },
|
|
10865
|
-
filter: { Package: this._sdk.sdkOptions.dlmm_pool.package_id }
|
|
10866
|
-
});
|
|
10867
|
-
const hasAssignPoolIds = assignPoolIds.length > 0;
|
|
10868
|
-
for (const item of ownerRes.data) {
|
|
10869
|
-
const type = extractStructTagFromType(item.data.type);
|
|
10870
|
-
if (type.full_address === this.buildPositionType()) {
|
|
10871
|
-
const position = this.buildPosition(item);
|
|
10872
|
-
const cacheKey = `${position.pos_object_id}_getPositionList`;
|
|
10873
|
-
this.updateCache(cacheKey, position, cacheTime24h);
|
|
10874
|
-
if (hasAssignPoolIds) {
|
|
10875
|
-
if (assignPoolIds.includes(position.pool)) {
|
|
10876
|
-
allPosition.push(position);
|
|
10877
|
-
}
|
|
10878
|
-
} else {
|
|
10879
|
-
allPosition.push(position);
|
|
10880
|
-
}
|
|
10881
|
-
}
|
|
10882
|
-
}
|
|
10883
|
-
const poolMap = /* @__PURE__ */ new Set();
|
|
10884
|
-
for (const item of allPosition) {
|
|
10885
|
-
poolMap.add(item.pool);
|
|
10886
|
-
}
|
|
10887
|
-
const poolList = await this.getPoolInfo(Array.from(poolMap));
|
|
10888
|
-
this.updateCache(`${DlmmScript}_positionList_poolList`, poolList, cacheTime24h);
|
|
10889
|
-
const _params = [];
|
|
10890
|
-
for (const pool of poolList) {
|
|
10891
|
-
_params.push({
|
|
10892
|
-
pool_id: pool.pool_id,
|
|
10893
|
-
coin_a: pool.coin_a,
|
|
10894
|
-
coin_b: pool.coin_b
|
|
10895
|
-
});
|
|
10896
|
-
}
|
|
10897
|
-
const pool_reward_coins = await this.getPairRewarders(_params);
|
|
10898
|
-
const out = [];
|
|
10899
|
-
for (const item of allPosition) {
|
|
10900
|
-
const pool = poolList.find((pool2) => pool2.pool_id === item.pool);
|
|
10901
|
-
const positionLiquidity = await this.getPositionLiquidity({
|
|
10902
|
-
pair: item.pool,
|
|
10903
|
-
positionId: item.pos_object_id,
|
|
10904
|
-
coinTypeA: pool.coin_a,
|
|
10905
|
-
coinTypeB: pool.coin_b
|
|
10906
|
-
});
|
|
10907
|
-
const rewards_token = pool_reward_coins.get(item.pool) || [];
|
|
10908
|
-
let positionRewards = { position_id: item.pos_object_id, reward: [], amount: [] };
|
|
10909
|
-
if (rewards_token.length > 0) {
|
|
10910
|
-
positionRewards = await this.getEarnedRewards({
|
|
10911
|
-
pool_id: item.pool,
|
|
10912
|
-
position_id: item.pos_object_id,
|
|
10913
|
-
coin_a: pool.coin_a,
|
|
10914
|
-
coin_b: pool.coin_b,
|
|
10915
|
-
rewards_token: pool_reward_coins.get(item.pool) || []
|
|
10916
|
-
});
|
|
10917
|
-
}
|
|
10918
|
-
const positionFees = await this.getEarnedFees({
|
|
10919
|
-
pool_id: item.pool,
|
|
10920
|
-
position_id: item.pos_object_id,
|
|
10921
|
-
coin_a: pool.coin_a,
|
|
10922
|
-
coin_b: pool.coin_b
|
|
10923
|
-
});
|
|
10924
|
-
out.push({
|
|
10925
|
-
position: item,
|
|
10926
|
-
liquidity: positionLiquidity,
|
|
10927
|
-
rewards: positionRewards,
|
|
10928
|
-
fees: positionFees,
|
|
10929
|
-
contractPool: pool
|
|
10930
|
-
});
|
|
10931
|
-
}
|
|
10932
|
-
return out;
|
|
10933
|
-
}
|
|
10934
|
-
buildPosition(object) {
|
|
10935
|
-
if (object.error != null || object.data?.content?.dataType !== "moveObject") {
|
|
10936
|
-
throw new ClmmpoolsError(`Dlmm Position not exists. Get Position error:${object.error}`, "InvalidPositionObject" /* InvalidPositionObject */);
|
|
10937
|
-
}
|
|
10938
|
-
const fields = getObjectFields(object);
|
|
10939
|
-
const ownerWarp = getObjectOwner(object);
|
|
10940
|
-
return {
|
|
10941
|
-
pos_object_id: fields.id.id,
|
|
10942
|
-
owner: ownerWarp.AddressOwner,
|
|
10943
|
-
pool: fields.pair_id,
|
|
10944
|
-
bin_real_ids: fields.bin_ids.map((id) => get_real_id(id)),
|
|
10945
|
-
type: ""
|
|
10946
|
-
};
|
|
10947
|
-
}
|
|
10948
|
-
// return [coin_a, coin_b]
|
|
10949
|
-
async getPoolCoins(pools) {
|
|
10950
|
-
const res = await this._sdk.fullClient.multiGetObjects({ ids: pools, options: { showContent: true } });
|
|
10951
|
-
const poolCoins = /* @__PURE__ */ new Map();
|
|
10952
|
-
res.forEach((item) => {
|
|
10953
|
-
if (item.error != null || item.data?.content?.dataType !== "moveObject") {
|
|
10954
|
-
throw new Error(`Failed to get poolCoins with err: ${item.error}`);
|
|
10955
|
-
}
|
|
10956
|
-
const type = getObjectType(item);
|
|
10957
|
-
const poolTypeFields = extractStructTagFromType(type);
|
|
10958
|
-
poolCoins.set(item.data.objectId, poolTypeFields.type_arguments);
|
|
10959
|
-
});
|
|
10960
|
-
return poolCoins;
|
|
10961
|
-
}
|
|
10962
|
-
buildPositionType() {
|
|
10963
|
-
return `${this._sdk.sdkOptions.dlmm_pool.package_id}::dlmm_position::Position`;
|
|
10964
|
-
}
|
|
10965
|
-
async getPositionLiquidity(params) {
|
|
10966
|
-
const tx = new Transaction12();
|
|
10967
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
10968
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
10969
|
-
const args = [tx.object(params.pair), tx.object(params.positionId)];
|
|
10970
|
-
tx.moveCall({
|
|
10971
|
-
target: `${integrate.published_at}::${DlmmScript}::position_liquidity`,
|
|
10972
|
-
arguments: args,
|
|
10973
|
-
typeArguments
|
|
10974
|
-
});
|
|
10975
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
10976
|
-
transactionBlock: tx,
|
|
10977
|
-
sender: simulationAccount.address
|
|
10978
|
-
});
|
|
10979
|
-
if (simulateRes.error != null) {
|
|
10980
|
-
throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
|
|
10981
|
-
}
|
|
10982
|
-
const out = {
|
|
10983
|
-
shares: 0,
|
|
10984
|
-
liquidity: 0,
|
|
10985
|
-
x_equivalent: 0,
|
|
10986
|
-
y_equivalent: 0,
|
|
10987
|
-
bin_real_ids: [],
|
|
10988
|
-
bin_x_eq: [],
|
|
10989
|
-
bin_y_eq: [],
|
|
10990
|
-
bin_liquidity: []
|
|
10991
|
-
};
|
|
10992
|
-
simulateRes.events?.forEach((item) => {
|
|
10993
|
-
if (extractStructTagFromType(item.type).name === `EventPositionLiquidity`) {
|
|
10994
|
-
out.shares = item.parsedJson.shares;
|
|
10995
|
-
out.liquidity = item.parsedJson.liquidity;
|
|
10996
|
-
out.x_equivalent = item.parsedJson.x_equivalent;
|
|
10997
|
-
out.y_equivalent = item.parsedJson.y_equivalent;
|
|
10998
|
-
out.bin_real_ids = item.parsedJson.bin_ids.map((id) => get_real_id(id));
|
|
10999
|
-
out.bin_x_eq = item.parsedJson.bin_x_eq;
|
|
11000
|
-
out.bin_y_eq = item.parsedJson.bin_y_eq;
|
|
11001
|
-
out.bin_liquidity = item.parsedJson.bin_liquidity;
|
|
11002
|
-
}
|
|
11003
|
-
});
|
|
11004
|
-
return out;
|
|
11005
|
-
}
|
|
11006
|
-
async getPairLiquidity(params) {
|
|
11007
|
-
const tx = new Transaction12();
|
|
11008
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
11009
|
-
const typeArguments = [params.coinTypeA, params.coinTypeB];
|
|
11010
|
-
const args = [tx.object(params.pair)];
|
|
11011
|
-
tx.moveCall({
|
|
11012
|
-
target: `${integrate.published_at}::${DlmmScript}::pair_liquidity`,
|
|
11013
|
-
arguments: args,
|
|
11014
|
-
typeArguments
|
|
11015
|
-
});
|
|
11016
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
11017
|
-
transactionBlock: tx,
|
|
11018
|
-
sender: simulationAccount.address
|
|
11019
|
-
});
|
|
11020
|
-
if (simulateRes.error != null) {
|
|
11021
|
-
throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
|
|
11022
|
-
}
|
|
11023
|
-
const out = {
|
|
11024
|
-
shares: 0,
|
|
11025
|
-
liquidity: 0,
|
|
11026
|
-
x: 0,
|
|
11027
|
-
y: 0,
|
|
11028
|
-
bin_ids: [],
|
|
11029
|
-
bin_x: [],
|
|
11030
|
-
bin_y: []
|
|
11031
|
-
};
|
|
11032
|
-
simulateRes.events?.forEach((item) => {
|
|
11033
|
-
if (extractStructTagFromType(item.type).name === `EventPositionLiquidity`) {
|
|
11034
|
-
out.shares = item.parsedJson.shares;
|
|
11035
|
-
out.liquidity = item.parsedJson.liquidity;
|
|
11036
|
-
out.x = item.parsedJson.x;
|
|
11037
|
-
out.y = item.parsedJson.y;
|
|
11038
|
-
out.bin_ids = item.bin_ids;
|
|
11039
|
-
out.bin_x = item.bin_x;
|
|
11040
|
-
out.bin_y = item.bin_y;
|
|
11041
|
-
}
|
|
11042
|
-
});
|
|
11043
|
-
return out;
|
|
11044
|
-
}
|
|
11045
|
-
async getEarnedFees(params) {
|
|
11046
|
-
const tx = new Transaction12();
|
|
11047
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
11048
|
-
const typeArguments = [params.coin_a, params.coin_b];
|
|
11049
|
-
const args = [tx.object(params.pool_id), tx.object(params.position_id)];
|
|
11050
|
-
tx.moveCall({
|
|
11051
|
-
target: `${integrate.published_at}::${DlmmScript}::earned_fees`,
|
|
11052
|
-
arguments: args,
|
|
11053
|
-
typeArguments
|
|
11054
|
-
});
|
|
11055
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
11056
|
-
transactionBlock: tx,
|
|
11057
|
-
sender: simulationAccount.address
|
|
11058
|
-
});
|
|
11059
|
-
const out = {
|
|
11060
|
-
position_id: "",
|
|
11061
|
-
x: "",
|
|
11062
|
-
y: "",
|
|
11063
|
-
fee_x: 0,
|
|
11064
|
-
fee_y: 0
|
|
11065
|
-
};
|
|
11066
|
-
if (simulateRes.error != null) {
|
|
11067
|
-
throw new Error(`fetchPairRewards error code: ${simulateRes.error ?? "unknown error"}`);
|
|
11068
|
-
}
|
|
11069
|
-
simulateRes.events?.forEach((item) => {
|
|
11070
|
-
if (extractStructTagFromType(item.type).name === `EventPositionLiquidity`) {
|
|
11071
|
-
out.position_id = item.parsedJson.position_id;
|
|
11072
|
-
out.x = item.parsedJson.x;
|
|
11073
|
-
out.y = item.parsedJson.y;
|
|
11074
|
-
out.fee_x = item.parsedJson.fee_x;
|
|
11075
|
-
out.fee_y = item.parsedJson.fee_y;
|
|
11076
|
-
}
|
|
11077
|
-
});
|
|
11078
|
-
return out;
|
|
11079
|
-
}
|
|
11080
|
-
async getEarnedRewards(params) {
|
|
11081
|
-
const tx = new Transaction12();
|
|
11082
|
-
const { integrate, simulationAccount } = this.sdk.sdkOptions;
|
|
11083
|
-
const typeArguments = [params.coin_a, params.coin_b, ...params.rewards_token];
|
|
11084
|
-
const args = [tx.object(params.pool_id), tx.object(params.position_id), tx.object(CLOCK_ADDRESS)];
|
|
11085
|
-
let target = `${integrate.published_at}::${DlmmScript}::earned_rewards`;
|
|
11086
|
-
if (params.rewards_token.length > 1) {
|
|
11087
|
-
target = `${integrate.published_at}::${DlmmScript}::earned_rewards${params.rewards_token.length}`;
|
|
11088
|
-
}
|
|
11089
|
-
tx.moveCall({
|
|
11090
|
-
target,
|
|
11091
|
-
arguments: args,
|
|
11092
|
-
typeArguments
|
|
11093
|
-
});
|
|
11094
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
11095
|
-
transactionBlock: tx,
|
|
11096
|
-
sender: simulationAccount.address
|
|
11097
|
-
});
|
|
11098
|
-
const out = {
|
|
11099
|
-
position_id: "",
|
|
11100
|
-
reward: [],
|
|
11101
|
-
amount: []
|
|
11102
|
-
};
|
|
11103
|
-
if (simulateRes.error != null) {
|
|
11104
|
-
throw new Error(`getEarnedRewards error code: ${simulateRes.error ?? "unknown error"}`);
|
|
11105
|
-
}
|
|
11106
|
-
simulateRes.events?.forEach((item) => {
|
|
11107
|
-
if (extractStructTagFromType(item.type).name === `DlmmEventEarnedRewards`) {
|
|
11108
|
-
out.position_id = item.parsedJson.position_id;
|
|
11109
|
-
out.reward = [item.parsedJson.reward];
|
|
11110
|
-
out.amount = [item.parsedJson.amount];
|
|
11111
|
-
} else if (extractStructTagFromType(item.type).name === `DlmmEventEarnedRewards2`) {
|
|
11112
|
-
out.position_id = item.parsedJson.position_id;
|
|
11113
|
-
out.reward = [item.parsedJson.reward1, item.parsedJson.reward2];
|
|
11114
|
-
out.amount = [item.parsedJson.amount1, item.parsedJson.amount2];
|
|
11115
|
-
} else if (extractStructTagFromType(item.type).name === `EventEarnedRewards3`) {
|
|
11116
|
-
out.position_id = item.parsedJson.position_id;
|
|
11117
|
-
out.reward = [item.parsedJson.reward1, item.parsedJson.reward2, item.parsedJson.reward3];
|
|
11118
|
-
out.amount = [item.parsedJson.amount1, item.parsedJson.amount2, item.parsedJson.amount3];
|
|
11119
|
-
}
|
|
11120
|
-
});
|
|
11121
|
-
return out;
|
|
11122
|
-
}
|
|
11123
|
-
// return pool_id => reward_tokens
|
|
11124
|
-
async getPairRewarders(params) {
|
|
11125
|
-
let tx = new Transaction12();
|
|
11126
|
-
for (const param of params) {
|
|
11127
|
-
tx = await this._getPairRewarders(param, tx);
|
|
11128
|
-
}
|
|
11129
|
-
return this._parsePairRewarders(tx);
|
|
11130
|
-
}
|
|
11131
|
-
async _getPairRewarders(params, tx) {
|
|
11132
|
-
tx = tx || new Transaction12();
|
|
11133
|
-
const { integrate } = this.sdk.sdkOptions;
|
|
11134
|
-
const typeArguments = [params.coin_a, params.coin_b];
|
|
11135
|
-
const args = [tx.object(params.pool_id)];
|
|
11136
|
-
tx.moveCall({
|
|
11137
|
-
target: `${integrate.published_at}::${DlmmScript}::get_pair_rewarders`,
|
|
11138
|
-
arguments: args,
|
|
11139
|
-
typeArguments
|
|
11140
|
-
});
|
|
11141
|
-
return tx;
|
|
11142
|
-
}
|
|
11143
|
-
async _parsePairRewarders(tx) {
|
|
11144
|
-
const { simulationAccount } = this.sdk.sdkOptions;
|
|
11145
|
-
const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
|
|
11146
|
-
transactionBlock: tx,
|
|
11147
|
-
sender: simulationAccount.address
|
|
11148
|
-
});
|
|
11149
|
-
const out = /* @__PURE__ */ new Map();
|
|
11150
|
-
if (simulateRes.error != null) {
|
|
11151
|
-
throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
|
|
11152
|
-
}
|
|
11153
|
-
simulateRes.events?.forEach((item) => {
|
|
11154
|
-
if (extractStructTagFromType(item.type).name === `EventPairRewardTypes`) {
|
|
11155
|
-
const pairRewards = {
|
|
11156
|
-
pair_id: "",
|
|
11157
|
-
tokens: []
|
|
11158
|
-
};
|
|
11159
|
-
pairRewards.pair_id = item.parsedJson.pair_id;
|
|
11160
|
-
item.parsedJson.tokens.forEach((token) => {
|
|
11161
|
-
pairRewards.tokens.push(token.name);
|
|
11162
|
-
});
|
|
11163
|
-
out.set(pairRewards.pair_id, pairRewards.tokens);
|
|
11164
|
-
}
|
|
11165
|
-
});
|
|
11166
|
-
return out;
|
|
11167
|
-
}
|
|
11168
|
-
/**
|
|
11169
|
-
* Updates the cache for the given key.
|
|
11170
|
-
*
|
|
11171
|
-
* @param key The key of the cache entry to update.
|
|
11172
|
-
* @param data The data to store in the cache.
|
|
11173
|
-
* @param time The time in minutes after which the cache entry should expire.
|
|
11174
|
-
*/
|
|
11175
|
-
updateCache(key, data, time = cacheTime5min) {
|
|
11176
|
-
let cacheData = this._cache[key];
|
|
11177
|
-
if (cacheData) {
|
|
11178
|
-
cacheData.overdueTime = getFutureTime(time);
|
|
11179
|
-
cacheData.value = data;
|
|
11180
|
-
} else {
|
|
11181
|
-
cacheData = new CachedContent(data, getFutureTime(time));
|
|
11182
|
-
}
|
|
11183
|
-
this._cache[key] = cacheData;
|
|
11184
|
-
}
|
|
11185
|
-
/**
|
|
11186
|
-
* Gets the cache entry for the given key.
|
|
11187
|
-
*
|
|
11188
|
-
* @param key The key of the cache entry to get.
|
|
11189
|
-
* @param forceRefresh Whether to force a refresh of the cache entry.
|
|
11190
|
-
* @returns The cache entry for the given key, or undefined if the cache entry does not exist or is expired.
|
|
11191
|
-
*/
|
|
11192
|
-
getCache(key, forceRefresh = false) {
|
|
11193
|
-
const cacheData = this._cache[key];
|
|
11194
|
-
const isValid = cacheData?.isValid();
|
|
11195
|
-
if (!forceRefresh && isValid) {
|
|
11196
|
-
return cacheData.value;
|
|
11197
|
-
}
|
|
11198
|
-
if (!isValid) {
|
|
11199
|
-
delete this._cache[key];
|
|
11200
|
-
}
|
|
11201
|
-
return void 0;
|
|
11202
|
-
}
|
|
11203
|
-
};
|
|
11204
|
-
|
|
11205
|
-
// src/sdk.ts
|
|
11206
|
-
var MagmaClmmSDK = class {
|
|
9804
|
+
// src/sdk.ts
|
|
9805
|
+
var MagmaClmmSDK = class {
|
|
11207
9806
|
_cache = {};
|
|
11208
9807
|
/**
|
|
11209
9808
|
* RPC provider on the SUI chain
|
|
@@ -11226,7 +9825,6 @@ var MagmaClmmSDK = class {
|
|
|
11226
9825
|
*/
|
|
11227
9826
|
_lock;
|
|
11228
9827
|
_gauge;
|
|
11229
|
-
_dlmm;
|
|
11230
9828
|
/**
|
|
11231
9829
|
* Provide interact with a position rewarder interface.
|
|
11232
9830
|
*/
|
|
@@ -11264,7 +9862,6 @@ var MagmaClmmSDK = class {
|
|
|
11264
9862
|
this._swap = new SwapModule(this);
|
|
11265
9863
|
this._lock = new LockModule(this);
|
|
11266
9864
|
this._gauge = new GaugeModule(this);
|
|
11267
|
-
this._dlmm = new DlmmModule(this);
|
|
11268
9865
|
this._pool = new PoolModule(this);
|
|
11269
9866
|
this._position = new PositionModule(this);
|
|
11270
9867
|
this._rewarder = new RewarderModule(this);
|
|
@@ -11302,9 +9899,6 @@ var MagmaClmmSDK = class {
|
|
|
11302
9899
|
get Gauge() {
|
|
11303
9900
|
return this._gauge;
|
|
11304
9901
|
}
|
|
11305
|
-
get Dlmm() {
|
|
11306
|
-
return this._dlmm;
|
|
11307
|
-
}
|
|
11308
9902
|
/**
|
|
11309
9903
|
* Getter for the fullClient property.
|
|
11310
9904
|
* @returns {RpcModule} The fullClient property value.
|
|
@@ -11478,7 +10072,6 @@ var main_default = MagmaClmmSDK;
|
|
|
11478
10072
|
var SDKConfig = {
|
|
11479
10073
|
clmmConfig: {
|
|
11480
10074
|
pools_id: "0xfa145b9de10fe858be81edd1c6cdffcf27be9d016de02a1345eb1009a68ba8b2",
|
|
11481
|
-
// clmm and dlmm both use this global_config
|
|
11482
10075
|
global_config_id: "0x4c4e1402401f72c7d8533d0ed8d5f8949da363c7a3319ccef261ffe153d32f8a",
|
|
11483
10076
|
global_vault_id: "0xa7e1102f222b6eb81ccc8a126e7feb2353342be9df6f6646a77c4519da29c071",
|
|
11484
10077
|
admin_cap_id: "0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"
|
|
@@ -11498,11 +10091,7 @@ var SDKConfig = {
|
|
|
11498
10091
|
distribution_cfg: "0xaff8d151ac29317201151f97d28c546b3c5923d8cfc5499f40dea61c4022c949",
|
|
11499
10092
|
magma_token: "0x7161c6c6bb65f852797c8f7f5c4f8d57adaf796e1b840921f9e23fabeadfd54e::magma::MAGMA",
|
|
11500
10093
|
minter_id: "0x4fa5766cd83b33b215b139fec27ac344040f3bbd84fcbee7b61fc671aadc51fa"
|
|
11501
|
-
}
|
|
11502
|
-
dlmmConfig: {
|
|
11503
|
-
factory: ""
|
|
11504
|
-
},
|
|
11505
|
-
gaugeConfig: {}
|
|
10094
|
+
}
|
|
11506
10095
|
};
|
|
11507
10096
|
var clmmMainnet = {
|
|
11508
10097
|
fullRpcUrl: getFullnodeUrl("mainnet"),
|
|
@@ -11519,11 +10108,6 @@ var clmmMainnet = {
|
|
|
11519
10108
|
published_at: "0x4a35d3dfef55ed3631b7158544c6322a23bc434fe4fca1234cb680ce0505f82d",
|
|
11520
10109
|
config: SDKConfig.clmmConfig
|
|
11521
10110
|
},
|
|
11522
|
-
dlmm_pool: {
|
|
11523
|
-
package_id: "",
|
|
11524
|
-
published_at: "",
|
|
11525
|
-
config: SDKConfig.dlmmConfig
|
|
11526
|
-
},
|
|
11527
10111
|
distribution: {
|
|
11528
10112
|
package_id: "0xee4a1f231dc45a303389998fe26c4e39278cf68b404b32e4f0b9769129b8267b",
|
|
11529
10113
|
published_at: "0xee4a1f231dc45a303389998fe26c4e39278cf68b404b32e4f0b9769129b8267b"
|
|
@@ -11577,9 +10161,6 @@ var SDKConfig2 = {
|
|
|
11577
10161
|
distribution_cfg: "0x94e23846c975e2faf89a61bfc2b10ad64decab9069eb1f9fc39752b010868c74",
|
|
11578
10162
|
magma_token: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1::magma_token::MAGMA_TOKEN",
|
|
11579
10163
|
minter_id: "0x89435d6b2a510ba50ca23303f10e91ec058f138a88f69a43fe03cd22edb214c5"
|
|
11580
|
-
},
|
|
11581
|
-
dlmmConfig: {
|
|
11582
|
-
factory: ""
|
|
11583
10164
|
}
|
|
11584
10165
|
};
|
|
11585
10166
|
var clmmTestnet = {
|
|
@@ -11594,11 +10175,6 @@ var clmmTestnet = {
|
|
|
11594
10175
|
published_at: "0x23e0b5ab4aa63d0e6fd98fa5e247bcf9b36ad716b479d39e56b2ba9ff631e09d",
|
|
11595
10176
|
config: SDKConfig2.clmmConfig
|
|
11596
10177
|
},
|
|
11597
|
-
dlmm_pool: {
|
|
11598
|
-
package_id: "",
|
|
11599
|
-
published_at: "",
|
|
11600
|
-
config: SDKConfig2.dlmmConfig
|
|
11601
|
-
},
|
|
11602
10178
|
distribution: {
|
|
11603
10179
|
package_id: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1",
|
|
11604
10180
|
published_at: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1"
|
|
@@ -11646,7 +10222,6 @@ var src_default = MagmaClmmSDK;
|
|
|
11646
10222
|
export {
|
|
11647
10223
|
AMM_SWAP_MODULE,
|
|
11648
10224
|
AmountSpecified,
|
|
11649
|
-
BinMath,
|
|
11650
10225
|
CLOCK_ADDRESS,
|
|
11651
10226
|
CachedContent,
|
|
11652
10227
|
ClmmExpectSwapModule,
|
|
@@ -11674,7 +10249,6 @@ export {
|
|
|
11674
10249
|
DeepbookCustodianV2Moudle,
|
|
11675
10250
|
DeepbookEndpointsV2Moudle,
|
|
11676
10251
|
DeepbookUtils,
|
|
11677
|
-
DlmmScript,
|
|
11678
10252
|
FEE_RATE_DENOMINATOR,
|
|
11679
10253
|
GAS_SYMBOL,
|
|
11680
10254
|
GAS_TYPE_ARG,
|
|
@@ -11702,7 +10276,6 @@ export {
|
|
|
11702
10276
|
SUI_SYSTEM_STATE_OBJECT_ID,
|
|
11703
10277
|
SplitSwap,
|
|
11704
10278
|
SplitUnit,
|
|
11705
|
-
StrategyType,
|
|
11706
10279
|
SwapDirection,
|
|
11707
10280
|
SwapModule,
|
|
11708
10281
|
SwapUtils,
|
|
@@ -11724,10 +10297,6 @@ export {
|
|
|
11724
10297
|
adjustForSlippage,
|
|
11725
10298
|
asIntN,
|
|
11726
10299
|
asUintN,
|
|
11727
|
-
autoFillXByStrategy,
|
|
11728
|
-
autoFillXByWeight,
|
|
11729
|
-
autoFillYByStrategy,
|
|
11730
|
-
autoFillYByWeight,
|
|
11731
10300
|
bufferToHex,
|
|
11732
10301
|
buildClmmPositionName,
|
|
11733
10302
|
buildNFT,
|
|
@@ -11766,14 +10335,12 @@ export {
|
|
|
11766
10335
|
getAmountUnfixedDelta,
|
|
11767
10336
|
getCoinAFromLiquidity,
|
|
11768
10337
|
getCoinBFromLiquidity,
|
|
11769
|
-
getCoinXYForLiquidity,
|
|
11770
10338
|
getDefaultSuiInputType,
|
|
11771
10339
|
getDeltaA,
|
|
11772
10340
|
getDeltaB,
|
|
11773
10341
|
getDeltaDownFromOutput,
|
|
11774
10342
|
getDeltaUpFromInput,
|
|
11775
10343
|
getFutureTime,
|
|
11776
|
-
getLiquidityAndCoinYByCoinX,
|
|
11777
10344
|
getLiquidityFromCoinA,
|
|
11778
10345
|
getLiquidityFromCoinB,
|
|
11779
10346
|
getLowerSqrtPriceFromCoinA,
|
|
@@ -11797,7 +10364,6 @@ export {
|
|
|
11797
10364
|
getObjectType,
|
|
11798
10365
|
getObjectVersion,
|
|
11799
10366
|
getPackagerConfigs,
|
|
11800
|
-
getPriceOfBinByBinId,
|
|
11801
10367
|
getRewardInTickRange,
|
|
11802
10368
|
getSuiObjectData,
|
|
11803
10369
|
getTickDataFromUrlData,
|
|
@@ -11821,15 +10387,10 @@ export {
|
|
|
11821
10387
|
shortAddress,
|
|
11822
10388
|
shortString,
|
|
11823
10389
|
tickScore,
|
|
11824
|
-
toAmountAskSide,
|
|
11825
|
-
toAmountBidSide,
|
|
11826
|
-
toAmountBothSide,
|
|
11827
|
-
toAmountsBothSideByStrategy,
|
|
11828
10390
|
toBuffer,
|
|
11829
10391
|
toCoinAmount,
|
|
11830
10392
|
toDecimalsAmount,
|
|
11831
10393
|
transClmmpoolDataWithoutTicks,
|
|
11832
|
-
utf8to16
|
|
11833
|
-
withLiquiditySlippage
|
|
10394
|
+
utf8to16
|
|
11834
10395
|
};
|
|
11835
10396
|
//# sourceMappingURL=index.mjs.map
|