@morpho-dev/router 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.d.cts +9 -84
- package/dist/index.browser.d.ts +9 -84
- package/dist/index.browser.js +17 -145
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.mjs +20 -147
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.node.d.cts +9 -84
- package/dist/index.node.d.ts +9 -84
- package/dist/index.node.js +17 -145
- package/dist/index.node.js.map +1 -1
- package/dist/index.node.mjs +20 -147
- package/dist/index.node.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.node.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Errors, LLTV, Offer, Format, Time, Maturity
|
|
1
|
+
import { Errors, LLTV, Offer, Format, Time, Maturity } from '@morpho-dev/mempool';
|
|
2
2
|
export * from '@morpho-dev/mempool';
|
|
3
3
|
import { z } from 'zod/v4';
|
|
4
4
|
import { createDocument } from 'zod-openapi';
|
|
5
|
-
import { parseUnits, maxUint256, formatUnits
|
|
5
|
+
import { parseUnits, maxUint256, formatUnits } from 'viem';
|
|
6
6
|
import { Base64 } from 'js-base64';
|
|
7
7
|
import { serve as serve$1 } from '@hono/node-server';
|
|
8
8
|
import { Hono } from 'hono';
|
|
@@ -1621,18 +1621,6 @@ function getLogger() {
|
|
|
1621
1621
|
return loggerContext.getStore() ?? defaultLogger();
|
|
1622
1622
|
}
|
|
1623
1623
|
|
|
1624
|
-
// src/RouterEvent.ts
|
|
1625
|
-
var RouterEvent_exports = {};
|
|
1626
|
-
__export(RouterEvent_exports, {
|
|
1627
|
-
from: () => from2,
|
|
1628
|
-
types: () => types
|
|
1629
|
-
});
|
|
1630
|
-
var types = ["offer_created", "offer_consumed", "offer_validation"];
|
|
1631
|
-
function from2(base) {
|
|
1632
|
-
const id = base.type === "offer_consumed" ? `${base.type}:${base.offerConsumed.id}` : `${base.type}:${base.offer.hash.toLowerCase()}`;
|
|
1633
|
-
return { id, ...base };
|
|
1634
|
-
}
|
|
1635
|
-
|
|
1636
1624
|
// src/Validation.ts
|
|
1637
1625
|
var Validation_exports = {};
|
|
1638
1626
|
__export(Validation_exports, {
|
|
@@ -1693,25 +1681,19 @@ function single(name, run2) {
|
|
|
1693
1681
|
function batch2(name, run2) {
|
|
1694
1682
|
return { kind: "batch", name, run: run2 };
|
|
1695
1683
|
}
|
|
1696
|
-
function morpho(
|
|
1697
|
-
const {
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
new Set(Array.from(chain.whitelistedAssets).map((a) => a.toLowerCase()))
|
|
1703
|
-
])
|
|
1704
|
-
);
|
|
1705
|
-
const morphoPerChain = new Map(
|
|
1706
|
-
whitelistedChains.map((chain) => [chain.id, chain.morpho.toLowerCase()])
|
|
1707
|
-
);
|
|
1708
|
-
const chainId = single("chain_id", (offer, _) => {
|
|
1709
|
-
if (!whitelistedChainIds.has(offer.chainId)) {
|
|
1710
|
-
return { message: `Chain ID ${offer.chainId} is not whitelisted` };
|
|
1684
|
+
function morpho() {
|
|
1685
|
+
const chainId = single("chain_id", (offer, { chain }) => {
|
|
1686
|
+
if (chain.id !== offer.chainId) {
|
|
1687
|
+
return {
|
|
1688
|
+
message: `Chain ID ${offer.chainId} is not the same as the chain ID in the context (${chain.id})`
|
|
1689
|
+
};
|
|
1711
1690
|
}
|
|
1712
1691
|
});
|
|
1713
|
-
const loanToken = single("loan_token", (offer,
|
|
1714
|
-
|
|
1692
|
+
const loanToken = single("loan_token", (offer, { chain }) => {
|
|
1693
|
+
const tokens = new Set(
|
|
1694
|
+
Array.from(chain.whitelistedAssets.values()).map((a) => a.toLowerCase())
|
|
1695
|
+
);
|
|
1696
|
+
if (!tokens.has(offer.loanToken.toLowerCase())) {
|
|
1715
1697
|
return {
|
|
1716
1698
|
message: `Loan token ${offer.loanToken} is not whitelisted on chain ${offer.chainId}`
|
|
1717
1699
|
};
|
|
@@ -1722,130 +1704,21 @@ function morpho(parameters) {
|
|
|
1722
1704
|
return { message: "Expiry mismatch" };
|
|
1723
1705
|
}
|
|
1724
1706
|
});
|
|
1725
|
-
const
|
|
1726
|
-
if (offer.callback.data !== "0x") {
|
|
1727
|
-
return { message: "Callback
|
|
1707
|
+
const callback = single("empty_callback", (offer, _) => {
|
|
1708
|
+
if (!offer.buy || offer.callback.data !== "0x") {
|
|
1709
|
+
return { message: "Callback not supported yet." };
|
|
1728
1710
|
}
|
|
1729
1711
|
});
|
|
1730
|
-
const sellOffersEmptyCallback = single(
|
|
1731
|
-
"sell_offers_empty_callback",
|
|
1732
|
-
(offer, _) => {
|
|
1733
|
-
if (!offer.buy && offer.callback.data === "0x") {
|
|
1734
|
-
return { message: "Sell offers with empty callback are not supported yet." };
|
|
1735
|
-
}
|
|
1736
|
-
}
|
|
1737
|
-
);
|
|
1738
|
-
const buyOffersEmptyCallback = batch2(
|
|
1739
|
-
"buy_offers_empty_callback",
|
|
1740
|
-
async (offers, { publicClients }) => {
|
|
1741
|
-
const issues = /* @__PURE__ */ new Map();
|
|
1742
|
-
const hashToIndex = /* @__PURE__ */ new Map();
|
|
1743
|
-
for (let i = 0; i < offers.length; i++) {
|
|
1744
|
-
const offer = offers[i];
|
|
1745
|
-
hashToIndex.set(offer.hash, i);
|
|
1746
|
-
}
|
|
1747
|
-
const { buyOffers, sellOffers: _sellOffers } = offers.reduce(
|
|
1748
|
-
(acc, offer) => {
|
|
1749
|
-
offer.buy ? acc.buyOffers.push(offer) : issues.set(hashToIndex.get(offer.hash), {
|
|
1750
|
-
message: "Onchain callback for sell offers is not supported yet."
|
|
1751
|
-
});
|
|
1752
|
-
return acc;
|
|
1753
|
-
},
|
|
1754
|
-
{ buyOffers: [], sellOffers: [] }
|
|
1755
|
-
);
|
|
1756
|
-
const buyOffersPerLoanAsset = /* @__PURE__ */ new Map();
|
|
1757
|
-
for (const offer of buyOffers) {
|
|
1758
|
-
const chainName = Chain.getChain(offer.chainId)?.name;
|
|
1759
|
-
const loanTokens = buyOffersPerLoanAsset.get(chainName) ?? /* @__PURE__ */ new Map();
|
|
1760
|
-
const offers2 = loanTokens.get(offer.loanToken.toLowerCase()) ?? [];
|
|
1761
|
-
offers2.push(offer);
|
|
1762
|
-
loanTokens.set(offer.loanToken.toLowerCase(), offers2);
|
|
1763
|
-
buyOffersPerLoanAsset.set(chainName, loanTokens);
|
|
1764
|
-
}
|
|
1765
|
-
await Promise.all(
|
|
1766
|
-
Array.from(buyOffersPerLoanAsset.entries()).map(async ([name, loanTokens]) => {
|
|
1767
|
-
const chainName = name;
|
|
1768
|
-
const publicClient = publicClients[chainName];
|
|
1769
|
-
const morpho2 = morphoPerChain.get(Chain.chains[chainName].id);
|
|
1770
|
-
if (!publicClient) {
|
|
1771
|
-
const offers2 = Array.from(loanTokens.values()).flat();
|
|
1772
|
-
for (const offer of offers2) {
|
|
1773
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1774
|
-
message: `Public client for chain "${chainName}" is not available`
|
|
1775
|
-
});
|
|
1776
|
-
}
|
|
1777
|
-
return;
|
|
1778
|
-
}
|
|
1779
|
-
const balances = /* @__PURE__ */ new Map();
|
|
1780
|
-
const allowances = /* @__PURE__ */ new Map();
|
|
1781
|
-
for (const [loanToken2, offers2] of loanTokens) {
|
|
1782
|
-
const data = await Promise.all(
|
|
1783
|
-
offers2.flatMap((offer) => [
|
|
1784
|
-
publicClient.readContract({
|
|
1785
|
-
address: loanToken2,
|
|
1786
|
-
abi: parseAbi([
|
|
1787
|
-
"function balanceOf(address owner) view returns (uint256 balance)"
|
|
1788
|
-
]),
|
|
1789
|
-
functionName: "balanceOf",
|
|
1790
|
-
args: [offer.offering]
|
|
1791
|
-
}),
|
|
1792
|
-
publicClient.readContract({
|
|
1793
|
-
address: loanToken2,
|
|
1794
|
-
abi: parseAbi([
|
|
1795
|
-
"function allowance(address owner, address spender) public view returns (uint256 remaining)"
|
|
1796
|
-
]),
|
|
1797
|
-
functionName: "allowance",
|
|
1798
|
-
args: [offer.offering, morpho2]
|
|
1799
|
-
})
|
|
1800
|
-
])
|
|
1801
|
-
);
|
|
1802
|
-
for (let i = 0; i < offers2.length; i++) {
|
|
1803
|
-
const user = offers2[i].offering.toLowerCase();
|
|
1804
|
-
const balance = data[i * 2] || 0n;
|
|
1805
|
-
const allowance = data[i * 2 + 1] || 0n;
|
|
1806
|
-
const userBalances = balances.get(user) ?? /* @__PURE__ */ new Map();
|
|
1807
|
-
userBalances.set(loanToken2.toLowerCase(), balance);
|
|
1808
|
-
const userAllowances = allowances.get(user) ?? /* @__PURE__ */ new Map();
|
|
1809
|
-
userAllowances.set(loanToken2.toLowerCase(), allowance);
|
|
1810
|
-
balances.set(user, userBalances);
|
|
1811
|
-
allowances.set(user, userAllowances);
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1814
|
-
for (const offer of Array.from(loanTokens.values()).flat()) {
|
|
1815
|
-
const user = offer.offering.toLowerCase();
|
|
1816
|
-
const userBalances = balances.get(user);
|
|
1817
|
-
const balance = userBalances?.get(offer.loanToken.toLowerCase());
|
|
1818
|
-
if (balance < offer.assets) {
|
|
1819
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1820
|
-
message: `Insufficient balance for ${offer.loanToken} on chain ${offer.chainId} (${balance.toString()} < ${offer.assets.toString()})`
|
|
1821
|
-
});
|
|
1822
|
-
continue;
|
|
1823
|
-
}
|
|
1824
|
-
const userAllowances = allowances.get(user);
|
|
1825
|
-
const allowance = userAllowances?.get(offer.loanToken.toLowerCase());
|
|
1826
|
-
if (allowance < offer.assets) {
|
|
1827
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1828
|
-
message: `Insufficient allowance for ${offer.loanToken} on chain ${offer.chainId} (${allowance.toString()} < ${offer.assets.toString()})`
|
|
1829
|
-
});
|
|
1830
|
-
}
|
|
1831
|
-
}
|
|
1832
|
-
})
|
|
1833
|
-
);
|
|
1834
|
-
return issues;
|
|
1835
|
-
}
|
|
1836
|
-
);
|
|
1837
1712
|
return [
|
|
1838
1713
|
chainId,
|
|
1839
1714
|
loanToken,
|
|
1840
1715
|
expiry,
|
|
1841
|
-
// note: callback
|
|
1842
|
-
// integrators should be able to
|
|
1843
|
-
|
|
1844
|
-
sellOffersEmptyCallback,
|
|
1845
|
-
buyOffersEmptyCallback
|
|
1716
|
+
// note: callback rule should be the last one, since it does not mean that the offer is forever invalid
|
|
1717
|
+
// integrators should be able to choose if they want to keep the offer or not
|
|
1718
|
+
callback
|
|
1846
1719
|
];
|
|
1847
1720
|
}
|
|
1848
1721
|
|
|
1849
|
-
export { apiSchema_exports as ApiSchema, Logger_exports as Logger, OfferStore_exports as OfferStore, router_exports as Router,
|
|
1722
|
+
export { apiSchema_exports as ApiSchema, Logger_exports as Logger, OfferStore_exports as OfferStore, router_exports as Router, RouterOffer_exports as RouterOffer, utils_exports as Utils, Validation_exports as Validation, ValidationRule_exports as ValidationRule };
|
|
1850
1723
|
//# sourceMappingURL=index.node.mjs.map
|
|
1851
1724
|
//# sourceMappingURL=index.node.mjs.map
|