@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.browser.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
|
-
import { parseUnits, maxUint256, formatUnits
|
|
3
|
+
import { parseUnits, maxUint256, formatUnits } from 'viem';
|
|
4
4
|
import { z } from 'zod/v4';
|
|
5
5
|
import { createDocument } from 'zod-openapi';
|
|
6
6
|
import { Base64 } from 'js-base64';
|
|
@@ -1351,18 +1351,6 @@ function memory(parameters) {
|
|
|
1351
1351
|
};
|
|
1352
1352
|
}
|
|
1353
1353
|
|
|
1354
|
-
// src/RouterEvent.ts
|
|
1355
|
-
var RouterEvent_exports = {};
|
|
1356
|
-
__export(RouterEvent_exports, {
|
|
1357
|
-
from: () => from2,
|
|
1358
|
-
types: () => types
|
|
1359
|
-
});
|
|
1360
|
-
var types = ["offer_created", "offer_consumed", "offer_validation"];
|
|
1361
|
-
function from2(base) {
|
|
1362
|
-
const id = base.type === "offer_consumed" ? `${base.type}:${base.offerConsumed.id}` : `${base.type}:${base.offer.hash.toLowerCase()}`;
|
|
1363
|
-
return { id, ...base };
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
1354
|
// src/Validation.ts
|
|
1367
1355
|
var Validation_exports = {};
|
|
1368
1356
|
__export(Validation_exports, {
|
|
@@ -1423,25 +1411,19 @@ function single(name, run2) {
|
|
|
1423
1411
|
function batch2(name, run2) {
|
|
1424
1412
|
return { kind: "batch", name, run: run2 };
|
|
1425
1413
|
}
|
|
1426
|
-
function morpho(
|
|
1427
|
-
const {
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
new Set(Array.from(chain.whitelistedAssets).map((a) => a.toLowerCase()))
|
|
1433
|
-
])
|
|
1434
|
-
);
|
|
1435
|
-
const morphoPerChain = new Map(
|
|
1436
|
-
whitelistedChains.map((chain) => [chain.id, chain.morpho.toLowerCase()])
|
|
1437
|
-
);
|
|
1438
|
-
const chainId = single("chain_id", (offer, _) => {
|
|
1439
|
-
if (!whitelistedChainIds.has(offer.chainId)) {
|
|
1440
|
-
return { message: `Chain ID ${offer.chainId} is not whitelisted` };
|
|
1414
|
+
function morpho() {
|
|
1415
|
+
const chainId = single("chain_id", (offer, { chain }) => {
|
|
1416
|
+
if (chain.id !== offer.chainId) {
|
|
1417
|
+
return {
|
|
1418
|
+
message: `Chain ID ${offer.chainId} is not the same as the chain ID in the context (${chain.id})`
|
|
1419
|
+
};
|
|
1441
1420
|
}
|
|
1442
1421
|
});
|
|
1443
|
-
const loanToken = single("loan_token", (offer,
|
|
1444
|
-
|
|
1422
|
+
const loanToken = single("loan_token", (offer, { chain }) => {
|
|
1423
|
+
const tokens = new Set(
|
|
1424
|
+
Array.from(chain.whitelistedAssets.values()).map((a) => a.toLowerCase())
|
|
1425
|
+
);
|
|
1426
|
+
if (!tokens.has(offer.loanToken.toLowerCase())) {
|
|
1445
1427
|
return {
|
|
1446
1428
|
message: `Loan token ${offer.loanToken} is not whitelisted on chain ${offer.chainId}`
|
|
1447
1429
|
};
|
|
@@ -1452,130 +1434,21 @@ function morpho(parameters) {
|
|
|
1452
1434
|
return { message: "Expiry mismatch" };
|
|
1453
1435
|
}
|
|
1454
1436
|
});
|
|
1455
|
-
const
|
|
1456
|
-
if (offer.callback.data !== "0x") {
|
|
1457
|
-
return { message: "Callback
|
|
1437
|
+
const callback = single("empty_callback", (offer, _) => {
|
|
1438
|
+
if (!offer.buy || offer.callback.data !== "0x") {
|
|
1439
|
+
return { message: "Callback not supported yet." };
|
|
1458
1440
|
}
|
|
1459
1441
|
});
|
|
1460
|
-
const sellOffersEmptyCallback = single(
|
|
1461
|
-
"sell_offers_empty_callback",
|
|
1462
|
-
(offer, _) => {
|
|
1463
|
-
if (!offer.buy && offer.callback.data === "0x") {
|
|
1464
|
-
return { message: "Sell offers with empty callback are not supported yet." };
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
);
|
|
1468
|
-
const buyOffersEmptyCallback = batch2(
|
|
1469
|
-
"buy_offers_empty_callback",
|
|
1470
|
-
async (offers, { publicClients }) => {
|
|
1471
|
-
const issues = /* @__PURE__ */ new Map();
|
|
1472
|
-
const hashToIndex = /* @__PURE__ */ new Map();
|
|
1473
|
-
for (let i = 0; i < offers.length; i++) {
|
|
1474
|
-
const offer = offers[i];
|
|
1475
|
-
hashToIndex.set(offer.hash, i);
|
|
1476
|
-
}
|
|
1477
|
-
const { buyOffers, sellOffers: _sellOffers } = offers.reduce(
|
|
1478
|
-
(acc, offer) => {
|
|
1479
|
-
offer.buy ? acc.buyOffers.push(offer) : issues.set(hashToIndex.get(offer.hash), {
|
|
1480
|
-
message: "Onchain callback for sell offers is not supported yet."
|
|
1481
|
-
});
|
|
1482
|
-
return acc;
|
|
1483
|
-
},
|
|
1484
|
-
{ buyOffers: [], sellOffers: [] }
|
|
1485
|
-
);
|
|
1486
|
-
const buyOffersPerLoanAsset = /* @__PURE__ */ new Map();
|
|
1487
|
-
for (const offer of buyOffers) {
|
|
1488
|
-
const chainName = Chain.getChain(offer.chainId)?.name;
|
|
1489
|
-
const loanTokens = buyOffersPerLoanAsset.get(chainName) ?? /* @__PURE__ */ new Map();
|
|
1490
|
-
const offers2 = loanTokens.get(offer.loanToken.toLowerCase()) ?? [];
|
|
1491
|
-
offers2.push(offer);
|
|
1492
|
-
loanTokens.set(offer.loanToken.toLowerCase(), offers2);
|
|
1493
|
-
buyOffersPerLoanAsset.set(chainName, loanTokens);
|
|
1494
|
-
}
|
|
1495
|
-
await Promise.all(
|
|
1496
|
-
Array.from(buyOffersPerLoanAsset.entries()).map(async ([name, loanTokens]) => {
|
|
1497
|
-
const chainName = name;
|
|
1498
|
-
const publicClient = publicClients[chainName];
|
|
1499
|
-
const morpho2 = morphoPerChain.get(Chain.chains[chainName].id);
|
|
1500
|
-
if (!publicClient) {
|
|
1501
|
-
const offers2 = Array.from(loanTokens.values()).flat();
|
|
1502
|
-
for (const offer of offers2) {
|
|
1503
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1504
|
-
message: `Public client for chain "${chainName}" is not available`
|
|
1505
|
-
});
|
|
1506
|
-
}
|
|
1507
|
-
return;
|
|
1508
|
-
}
|
|
1509
|
-
const balances = /* @__PURE__ */ new Map();
|
|
1510
|
-
const allowances = /* @__PURE__ */ new Map();
|
|
1511
|
-
for (const [loanToken2, offers2] of loanTokens) {
|
|
1512
|
-
const data = await Promise.all(
|
|
1513
|
-
offers2.flatMap((offer) => [
|
|
1514
|
-
publicClient.readContract({
|
|
1515
|
-
address: loanToken2,
|
|
1516
|
-
abi: parseAbi([
|
|
1517
|
-
"function balanceOf(address owner) view returns (uint256 balance)"
|
|
1518
|
-
]),
|
|
1519
|
-
functionName: "balanceOf",
|
|
1520
|
-
args: [offer.offering]
|
|
1521
|
-
}),
|
|
1522
|
-
publicClient.readContract({
|
|
1523
|
-
address: loanToken2,
|
|
1524
|
-
abi: parseAbi([
|
|
1525
|
-
"function allowance(address owner, address spender) public view returns (uint256 remaining)"
|
|
1526
|
-
]),
|
|
1527
|
-
functionName: "allowance",
|
|
1528
|
-
args: [offer.offering, morpho2]
|
|
1529
|
-
})
|
|
1530
|
-
])
|
|
1531
|
-
);
|
|
1532
|
-
for (let i = 0; i < offers2.length; i++) {
|
|
1533
|
-
const user = offers2[i].offering.toLowerCase();
|
|
1534
|
-
const balance = data[i * 2] || 0n;
|
|
1535
|
-
const allowance = data[i * 2 + 1] || 0n;
|
|
1536
|
-
const userBalances = balances.get(user) ?? /* @__PURE__ */ new Map();
|
|
1537
|
-
userBalances.set(loanToken2.toLowerCase(), balance);
|
|
1538
|
-
const userAllowances = allowances.get(user) ?? /* @__PURE__ */ new Map();
|
|
1539
|
-
userAllowances.set(loanToken2.toLowerCase(), allowance);
|
|
1540
|
-
balances.set(user, userBalances);
|
|
1541
|
-
allowances.set(user, userAllowances);
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
for (const offer of Array.from(loanTokens.values()).flat()) {
|
|
1545
|
-
const user = offer.offering.toLowerCase();
|
|
1546
|
-
const userBalances = balances.get(user);
|
|
1547
|
-
const balance = userBalances?.get(offer.loanToken.toLowerCase());
|
|
1548
|
-
if (balance < offer.assets) {
|
|
1549
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1550
|
-
message: `Insufficient balance for ${offer.loanToken} on chain ${offer.chainId} (${balance.toString()} < ${offer.assets.toString()})`
|
|
1551
|
-
});
|
|
1552
|
-
continue;
|
|
1553
|
-
}
|
|
1554
|
-
const userAllowances = allowances.get(user);
|
|
1555
|
-
const allowance = userAllowances?.get(offer.loanToken.toLowerCase());
|
|
1556
|
-
if (allowance < offer.assets) {
|
|
1557
|
-
issues.set(hashToIndex.get(offer.hash), {
|
|
1558
|
-
message: `Insufficient allowance for ${offer.loanToken} on chain ${offer.chainId} (${allowance.toString()} < ${offer.assets.toString()})`
|
|
1559
|
-
});
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
})
|
|
1563
|
-
);
|
|
1564
|
-
return issues;
|
|
1565
|
-
}
|
|
1566
|
-
);
|
|
1567
1442
|
return [
|
|
1568
1443
|
chainId,
|
|
1569
1444
|
loanToken,
|
|
1570
1445
|
expiry,
|
|
1571
|
-
// note: callback
|
|
1572
|
-
// integrators should be able to
|
|
1573
|
-
|
|
1574
|
-
sellOffersEmptyCallback,
|
|
1575
|
-
buyOffersEmptyCallback
|
|
1446
|
+
// note: callback rule should be the last one, since it does not mean that the offer is forever invalid
|
|
1447
|
+
// integrators should be able to choose if they want to keep the offer or not
|
|
1448
|
+
callback
|
|
1576
1449
|
];
|
|
1577
1450
|
}
|
|
1578
1451
|
|
|
1579
|
-
export { OfferStore_exports as OfferStore, Client_exports as Router,
|
|
1452
|
+
export { OfferStore_exports as OfferStore, Client_exports as Router, RouterOffer_exports as RouterOffer, utils_exports as Utils, Validation_exports as Validation, ValidationRule_exports as ValidationRule };
|
|
1580
1453
|
//# sourceMappingURL=index.browser.mjs.map
|
|
1581
1454
|
//# sourceMappingURL=index.browser.mjs.map
|