@morpho-org/calldata-verifier 0.1.0 → 0.3.0
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 +8 -3
- package/lib/cjs/midnight.d.ts +47 -0
- package/lib/cjs/midnight.js +57 -4
- package/lib/cjs/midnightMakers.d.ts +17 -0
- package/lib/cjs/midnightMakers.js +383 -0
- package/lib/cjs/midnightOffers.d.ts +0 -5
- package/lib/cjs/midnightOffers.js +0 -7
- package/lib/cjs/types.d.ts +1 -0
- package/lib/cjs/verifyCalldata.js +2 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/midnight.d.ts +47 -0
- package/lib/esm/midnight.js +58 -7
- package/lib/esm/midnightMakers.d.ts +17 -0
- package/lib/esm/midnightMakers.js +380 -0
- package/lib/esm/midnightOffers.d.ts +0 -5
- package/lib/esm/midnightOffers.js +1 -8
- package/lib/esm/types.d.ts +1 -0
- package/lib/esm/verifyCalldata.js +2 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +6 -5
package/lib/cjs/version.js
CHANGED
package/lib/esm/midnight.d.ts
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
|
+
import { midnightBundlesV2Abi } from "@morpho-org/midnight-sdk";
|
|
1
2
|
import type { ChainAddresses } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { type Address } from "viem";
|
|
4
|
+
import { decode } from "./checks.js";
|
|
2
5
|
import type { CalldataCall, DecodedCalldataCall, MidnightMarketIntent, MidnightTakeIntent } from "./types.js";
|
|
3
6
|
type Intent = MidnightMarketIntent | MidnightTakeIntent;
|
|
7
|
+
type BundleCall = ReturnType<typeof decode<typeof midnightBundlesV2Abi>>;
|
|
8
|
+
type Transfer = {
|
|
9
|
+
readonly collateralIndex: bigint;
|
|
10
|
+
readonly assets: bigint;
|
|
11
|
+
};
|
|
12
|
+
type TokenPull = {
|
|
13
|
+
readonly token: Address;
|
|
14
|
+
readonly assets: bigint;
|
|
15
|
+
readonly spender?: Address;
|
|
16
|
+
};
|
|
17
|
+
type RequestedTransfer = {
|
|
18
|
+
readonly collateral: {
|
|
19
|
+
readonly address: Address;
|
|
20
|
+
};
|
|
21
|
+
readonly amount: {
|
|
22
|
+
readonly unit: "all";
|
|
23
|
+
} | {
|
|
24
|
+
readonly unit: "assets";
|
|
25
|
+
readonly value: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
type Market = Extract<BundleCall, {
|
|
29
|
+
functionName: "midnightBundlesV2BuyWithAssetsTargetAndWithdrawCollateral";
|
|
30
|
+
}>["args"][0];
|
|
31
|
+
/** @internal */
|
|
32
|
+
export declare function checkCollateral(transfers: readonly Transfer[], { requested, market, call, withdrawal, }: {
|
|
33
|
+
requested: readonly RequestedTransfer[];
|
|
34
|
+
market: Market;
|
|
35
|
+
call: CalldataCall;
|
|
36
|
+
withdrawal?: "core" | "bundle";
|
|
37
|
+
}): {
|
|
38
|
+
token: `0x${string}`;
|
|
39
|
+
assets: bigint;
|
|
40
|
+
}[];
|
|
41
|
+
/** @internal */
|
|
42
|
+
export declare function checkPrerequisites(calls: readonly CalldataCall[], { intent, chain, call: main, pulls, allowAuthorization, }: {
|
|
43
|
+
readonly intent: {
|
|
44
|
+
readonly account: Address;
|
|
45
|
+
};
|
|
46
|
+
readonly chain: ChainAddresses;
|
|
47
|
+
readonly call: CalldataCall;
|
|
48
|
+
readonly pulls: readonly TokenPull[];
|
|
49
|
+
readonly allowAuthorization: boolean;
|
|
50
|
+
}): DecodedCalldataCall[];
|
|
4
51
|
declare function verifyMidnight(intent: Intent, { calls, chain, }: {
|
|
5
52
|
readonly calls: readonly CalldataCall[];
|
|
6
53
|
readonly chain: ChainAddresses;
|
package/lib/esm/midnight.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MarketUtils, midnightAbi, midnightBundlesV2Abi, } from "@morpho-org/midnight-sdk";
|
|
2
|
-
import { maxUint256, zeroAddress } from "viem";
|
|
2
|
+
import { maxUint256, zeroAddress, zeroHash } from "viem";
|
|
3
3
|
import { approval, checked, decode, innerCall, mainCall, requireAccount, requireAmount, requireFee, requireNativeBudget, requireTarget, sameAddress, sameHex, uint, } from "./checks.js";
|
|
4
4
|
import { CalldataAmountError, CalldataCallError, CalldataFeeError, CalldataTargetError, CalldataUnsupportedError, } from "./errors.js";
|
|
5
5
|
function isTake(intent) {
|
|
@@ -43,7 +43,8 @@ function assetsMatch(assets, { requested, withdrawal, call, }) {
|
|
|
43
43
|
field: "collateral.amount",
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
/** @internal */
|
|
47
|
+
export function checkCollateral(transfers, { requested, market, call, withdrawal, }) {
|
|
47
48
|
if (!Array.isArray(requested) || transfers.length !== requested.length) {
|
|
48
49
|
throw new CalldataCallError("collateral", call.id);
|
|
49
50
|
}
|
|
@@ -66,7 +67,8 @@ function noValue(call) {
|
|
|
66
67
|
field: "value",
|
|
67
68
|
});
|
|
68
69
|
}
|
|
69
|
-
|
|
70
|
+
/** @internal */
|
|
71
|
+
export function checkPrerequisites(calls, { intent, chain, call: main, pulls, allowAuthorization, }) {
|
|
70
72
|
let authorized = false;
|
|
71
73
|
const approved = new Map();
|
|
72
74
|
return calls.map((call, index) => {
|
|
@@ -95,9 +97,9 @@ function checkPrerequisites(calls, { intent, chain, call: main, pulls, allowAuth
|
|
|
95
97
|
if (pull == null) {
|
|
96
98
|
throw new CalldataTargetError("approval.token", call.id);
|
|
97
99
|
}
|
|
98
|
-
const { token, assets } = pull;
|
|
100
|
+
const { token, assets, spender = main.to } = pull;
|
|
99
101
|
const verified = approval(call, {
|
|
100
|
-
spender
|
|
102
|
+
spender,
|
|
101
103
|
token,
|
|
102
104
|
minAmount: assets,
|
|
103
105
|
allowReset: sameAddress(calls[index + 1]?.to, token),
|
|
@@ -232,9 +234,11 @@ function checkCoreWithdrawals(decoded, context) {
|
|
|
232
234
|
})
|
|
233
235
|
: first.result;
|
|
234
236
|
}
|
|
235
|
-
function checkNative(wrappedNative, { intent, chain, call, cap }) {
|
|
237
|
+
function checkNative(wrappedNative, { intent, chain, call, cap, requireValue = false, }) {
|
|
236
238
|
const value = uint(call.value, { field: "value", call });
|
|
237
|
-
if (
|
|
239
|
+
if (!requireValue &&
|
|
240
|
+
value === 0n &&
|
|
241
|
+
sameAddress(wrappedNative, zeroAddress)) {
|
|
238
242
|
return;
|
|
239
243
|
}
|
|
240
244
|
if (value === 0n ||
|
|
@@ -434,6 +438,50 @@ function checkSell(decoded, context) {
|
|
|
434
438
|
});
|
|
435
439
|
return collateral;
|
|
436
440
|
}
|
|
441
|
+
function checkSupplyCollateralWithNative(decoded, context) {
|
|
442
|
+
const { intent, call } = context;
|
|
443
|
+
if (intent.action !== "midnight.supply_collateral") {
|
|
444
|
+
throw new CalldataCallError("function", call.id);
|
|
445
|
+
}
|
|
446
|
+
const [blueMarket, assetsToPark, callbackSalt, market, supplies, ratifier, newRoot, signatureHeight, signatureNonce, signatureDeadline, v, r, s, groupsToCancel, payload, deadline, wrappedNative,] = decoded.args;
|
|
447
|
+
const emptyBlueMarket = sameAddress(blueMarket.loanToken, zeroAddress) &&
|
|
448
|
+
sameAddress(blueMarket.collateralToken, zeroAddress) &&
|
|
449
|
+
sameAddress(blueMarket.oracle, zeroAddress) &&
|
|
450
|
+
sameAddress(blueMarket.irm, zeroAddress) &&
|
|
451
|
+
blueMarket.lltv === 0n;
|
|
452
|
+
const emptyPublication = sameAddress(ratifier, zeroAddress) &&
|
|
453
|
+
sameHex(newRoot, zeroHash) &&
|
|
454
|
+
signatureHeight === 0n &&
|
|
455
|
+
signatureNonce === 0n &&
|
|
456
|
+
signatureDeadline === 0n &&
|
|
457
|
+
v === 0 &&
|
|
458
|
+
sameHex(r, zeroHash) &&
|
|
459
|
+
sameHex(s, zeroHash) &&
|
|
460
|
+
groupsToCancel.length === 0 &&
|
|
461
|
+
sameHex(payload, "0x");
|
|
462
|
+
if (!emptyBlueMarket ||
|
|
463
|
+
assetsToPark !== 0n ||
|
|
464
|
+
!sameHex(callbackSalt, zeroHash) ||
|
|
465
|
+
!emptyPublication) {
|
|
466
|
+
throw new CalldataCallError("cancel_and_make", call.id);
|
|
467
|
+
}
|
|
468
|
+
if (deadline === 0n) {
|
|
469
|
+
throw new CalldataAmountError("deadline", call.id);
|
|
470
|
+
}
|
|
471
|
+
requireMarket(market, context);
|
|
472
|
+
const collateral = checkCollateral(supplies, {
|
|
473
|
+
requested: [intent],
|
|
474
|
+
market,
|
|
475
|
+
call,
|
|
476
|
+
});
|
|
477
|
+
const nativeCollateral = collateral.find(({ token }) => sameAddress(token, wrappedNative));
|
|
478
|
+
checkNative(wrappedNative, {
|
|
479
|
+
...context,
|
|
480
|
+
cap: nativeCollateral?.assets ?? 0n,
|
|
481
|
+
requireValue: true,
|
|
482
|
+
});
|
|
483
|
+
return collateral;
|
|
484
|
+
}
|
|
437
485
|
function checkFills(fills, context) {
|
|
438
486
|
for (const { offer, units } of fills) {
|
|
439
487
|
requireMarket(offer.market, context);
|
|
@@ -501,6 +549,9 @@ function verifyMidnight(intent, { calls, chain, }) {
|
|
|
501
549
|
case "midnightBundlesV2SupplyCollateralAndSellWithUnitsTarget":
|
|
502
550
|
pulls = checkSell(decoded, context);
|
|
503
551
|
break;
|
|
552
|
+
case "midnightBundlesV2CancelAndMake":
|
|
553
|
+
pulls = checkSupplyCollateralWithNative(decoded, context);
|
|
554
|
+
break;
|
|
504
555
|
default:
|
|
505
556
|
throw new CalldataCallError("function", call.id);
|
|
506
557
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChainAddresses } from "@morpho-org/morpho-ts";
|
|
2
|
+
import type { CalldataCall, DecodedCalldataCall, MidnightOfferManagementIntent } from "./types.js";
|
|
3
|
+
type MakeIntent = Extract<MidnightOfferManagementIntent, {
|
|
4
|
+
readonly offers: unknown;
|
|
5
|
+
}>;
|
|
6
|
+
declare function verifyMake(intent: MakeIntent, { calls, chain, }: {
|
|
7
|
+
readonly calls: readonly CalldataCall[];
|
|
8
|
+
readonly chain: ChainAddresses;
|
|
9
|
+
}): readonly DecodedCalldataCall[];
|
|
10
|
+
/** @internal */
|
|
11
|
+
export declare const midnightMakerRules: {
|
|
12
|
+
"midnight.make_borrow": typeof verifyMake;
|
|
13
|
+
"midnight.make_lend": typeof verifyMake;
|
|
14
|
+
"midnight.make_repay": typeof verifyMake;
|
|
15
|
+
"midnight.make_withdraw": typeof verifyMake;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { GroupUtils, getBlueBuyCallbackAddress, MarketUtils, midnightBundlesV2Abi, OfferUtils, Payload, PriceRatifierV1, RateRatifierV1, Tree, } from "@morpho-org/midnight-sdk";
|
|
2
|
+
import { marketParamsAbi } from "@morpho-org/morpho-ts/abis";
|
|
3
|
+
import { Gunzip } from "fflate";
|
|
4
|
+
import { encodeAbiParameters, hexToBytes, keccak256, maxUint128, zeroAddress, zeroHash, } from "viem";
|
|
5
|
+
import { checked, decode, mainCall, requireAccount, requireAmount, requireNativeBudget, requireTarget, sameAddress, sameHex, uint, } from "./checks.js";
|
|
6
|
+
import { CalldataAmountError, CalldataCallError, CalldataDecodeError, CalldataTargetError, CalldataUnsupportedError, } from "./errors.js";
|
|
7
|
+
import { checkCollateral, checkPrerequisites } from "./midnight.js";
|
|
8
|
+
const sides = {
|
|
9
|
+
"midnight.make_lend": { buy: true, reduceOnly: false, unit: "assets" },
|
|
10
|
+
"midnight.make_borrow": { buy: false, reduceOnly: false, unit: "assets" },
|
|
11
|
+
"midnight.make_repay": { buy: true, reduceOnly: true, unit: "units" },
|
|
12
|
+
"midnight.make_withdraw": { buy: false, reduceOnly: true, unit: "units" },
|
|
13
|
+
};
|
|
14
|
+
const GUNZIP_SLICE_BYTES = 1_024;
|
|
15
|
+
function gunzip(compressed, maxBytes) {
|
|
16
|
+
const chunks = [];
|
|
17
|
+
let length = 0;
|
|
18
|
+
const stream = new Gunzip((chunk) => {
|
|
19
|
+
length += chunk.length;
|
|
20
|
+
if (length > maxBytes) {
|
|
21
|
+
throw new RangeError(`gzip output exceeds ${maxBytes} bytes`);
|
|
22
|
+
}
|
|
23
|
+
chunks.push(chunk);
|
|
24
|
+
});
|
|
25
|
+
// fflate inflates a whole push before reporting output, so small slices bound the work done past the cap.
|
|
26
|
+
for (let offset = 0; offset < compressed.length; offset += GUNZIP_SLICE_BYTES) {
|
|
27
|
+
const end = offset + GUNZIP_SLICE_BYTES;
|
|
28
|
+
stream.push(compressed.subarray(offset, end), end >= compressed.length);
|
|
29
|
+
}
|
|
30
|
+
const output = new Uint8Array(length);
|
|
31
|
+
let offset = 0;
|
|
32
|
+
for (const chunk of chunks) {
|
|
33
|
+
output.set(chunk, offset);
|
|
34
|
+
offset += chunk.length;
|
|
35
|
+
}
|
|
36
|
+
return output;
|
|
37
|
+
}
|
|
38
|
+
function decodePayload(payload, { call, maxItems }) {
|
|
39
|
+
try {
|
|
40
|
+
const bytes = hexToBytes(payload);
|
|
41
|
+
// The API never appends an attribution suffix after the gzip stream.
|
|
42
|
+
if (bytes.length !== 5 + new DataView(bytes.buffer).getUint32(1)) {
|
|
43
|
+
throw new CalldataDecodeError("payload", call.id);
|
|
44
|
+
}
|
|
45
|
+
return Payload.decodeSync(payload, { gunzip, maxItems });
|
|
46
|
+
}
|
|
47
|
+
catch (cause) {
|
|
48
|
+
if (cause instanceof CalldataDecodeError) {
|
|
49
|
+
throw cause;
|
|
50
|
+
}
|
|
51
|
+
throw new CalldataDecodeError("payload", { callId: call.id, cause });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function rebuildTree(items, { intent, call }) {
|
|
55
|
+
try {
|
|
56
|
+
if (intent.offers.type === "rate") {
|
|
57
|
+
const entries = items.map(({ offer, ratifierData }) => ({
|
|
58
|
+
offer,
|
|
59
|
+
...RateRatifierV1.decodeRatifierData(ratifierData),
|
|
60
|
+
}));
|
|
61
|
+
const tree = Tree.create({ type: "rateV1", entries });
|
|
62
|
+
return {
|
|
63
|
+
leaves: entries.map(({ offer, rate, allowedTaker }) => ({
|
|
64
|
+
offer: OfferUtils.toStruct({ offer }),
|
|
65
|
+
rate,
|
|
66
|
+
allowedTaker,
|
|
67
|
+
})),
|
|
68
|
+
root: tree.root,
|
|
69
|
+
group: RateRatifierV1.groupId(entries),
|
|
70
|
+
items: RateRatifierV1.ratify({ tree }),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const entries = items.map(({ offer, ratifierData }) => ({
|
|
74
|
+
offer,
|
|
75
|
+
...PriceRatifierV1.decodeRatifierData(ratifierData),
|
|
76
|
+
}));
|
|
77
|
+
const tree = Tree.create({ type: "priceV1", entries });
|
|
78
|
+
return {
|
|
79
|
+
leaves: entries.map(({ offer, allowedTaker }) => ({
|
|
80
|
+
offer: OfferUtils.toStruct({ offer }),
|
|
81
|
+
allowedTaker,
|
|
82
|
+
})),
|
|
83
|
+
root: tree.root,
|
|
84
|
+
group: PriceRatifierV1.groupId(entries),
|
|
85
|
+
items: PriceRatifierV1.ratify({ tree }),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
catch (cause) {
|
|
89
|
+
throw new CalldataCallError("payload", { callId: call.id, cause });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function marketId(market, call) {
|
|
93
|
+
try {
|
|
94
|
+
const sorted = market.collateralParams.every((collateral, index) => index === 0 ||
|
|
95
|
+
MarketUtils.compareCollateralTokens(market.collateralParams[index - 1], collateral) < 0);
|
|
96
|
+
if (sorted) {
|
|
97
|
+
return MarketUtils.toId(market);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (cause) {
|
|
101
|
+
throw new CalldataTargetError("market", { callId: call.id, cause });
|
|
102
|
+
}
|
|
103
|
+
throw new CalldataTargetError("market", call.id);
|
|
104
|
+
}
|
|
105
|
+
function checkOffer({ offer, rate }, { entry, ratifier, cap, context: { intent, chain, call, blue }, }) {
|
|
106
|
+
const { buy, reduceOnly, unit } = sides[intent.action];
|
|
107
|
+
if (offer.market.chainId !== BigInt(intent.chain_id) ||
|
|
108
|
+
!sameAddress(offer.market.midnight, chain.midnight) ||
|
|
109
|
+
!sameHex(marketId(offer.market, call), entry.market_id)) {
|
|
110
|
+
throw new CalldataTargetError("offers.market_id", call.id);
|
|
111
|
+
}
|
|
112
|
+
requireAccount(offer.maker, {
|
|
113
|
+
expected: intent.account,
|
|
114
|
+
field: "offers.maker",
|
|
115
|
+
call,
|
|
116
|
+
});
|
|
117
|
+
requireAccount(offer.receiverIfMakerIsSeller, {
|
|
118
|
+
expected: buy ? zeroAddress : intent.account,
|
|
119
|
+
field: "offers.receiver",
|
|
120
|
+
call,
|
|
121
|
+
});
|
|
122
|
+
if (offer.buy !== buy ||
|
|
123
|
+
offer.reduceOnly !== reduceOnly ||
|
|
124
|
+
!sameAddress(offer.ratifier, ratifier) ||
|
|
125
|
+
!sameAddress(offer.callback, blue?.callback ?? zeroAddress) ||
|
|
126
|
+
!sameHex(offer.callbackData, blue?.callbackData ?? "0x") ||
|
|
127
|
+
offer.continuousFeeCap !== 0n) {
|
|
128
|
+
throw new CalldataCallError("offers", call.id);
|
|
129
|
+
}
|
|
130
|
+
if (offer.start !==
|
|
131
|
+
uint(String(entry.starts_at), { field: "offers.starts_at", call }) ||
|
|
132
|
+
offer.expiry !==
|
|
133
|
+
uint(String(entry.expires_at), { field: "offers.expires_at", call })) {
|
|
134
|
+
throw new CalldataAmountError("offers.expires_at", call.id);
|
|
135
|
+
}
|
|
136
|
+
if ((intent.offers.type === "price" || entry.tick != null) &&
|
|
137
|
+
offer.tick !== uint(entry.tick ?? "", { field: "offers.tick", call })) {
|
|
138
|
+
throw new CalldataAmountError("offers.tick", call.id);
|
|
139
|
+
}
|
|
140
|
+
if (intent.offers.type === "rate" &&
|
|
141
|
+
rate !==
|
|
142
|
+
uint(("rate_wad" in entry ? entry.rate_wad : undefined) ?? "", {
|
|
143
|
+
field: "offers.rate_wad",
|
|
144
|
+
call,
|
|
145
|
+
})) {
|
|
146
|
+
throw new CalldataAmountError("offers.rate_wad", call.id);
|
|
147
|
+
}
|
|
148
|
+
requireAmount(unit === "assets" ? offer.maxAssets : offer.maxUnits, {
|
|
149
|
+
expected: blue == null ? cap : maxUint128,
|
|
150
|
+
field: "amount",
|
|
151
|
+
call,
|
|
152
|
+
});
|
|
153
|
+
requireAmount(unit === "assets" ? offer.maxUnits : offer.maxAssets, {
|
|
154
|
+
expected: 0n,
|
|
155
|
+
field: "amount",
|
|
156
|
+
call,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function checkOffers(args, context) {
|
|
160
|
+
const { intent, chain, call, cap } = context;
|
|
161
|
+
const [, , , , , ratifier, root, , , , , , , , payload] = args;
|
|
162
|
+
const { offers } = intent;
|
|
163
|
+
const expectedRatifier = offers?.type === "rate" || offers?.type === "price"
|
|
164
|
+
? { rate: chain.rateRatifierV1, price: chain.priceRatifierV1 }[offers.type]
|
|
165
|
+
: undefined;
|
|
166
|
+
if (expectedRatifier == null || !sameAddress(ratifier, expectedRatifier)) {
|
|
167
|
+
throw new CalldataTargetError("ratifier", call.id);
|
|
168
|
+
}
|
|
169
|
+
if (!Array.isArray(offers.entries) ||
|
|
170
|
+
offers.entries.length === 0 ||
|
|
171
|
+
Array.from(offers.entries).some((entry) => typeof entry !== "object" || entry == null)) {
|
|
172
|
+
throw new CalldataCallError("offers.entries", call.id);
|
|
173
|
+
}
|
|
174
|
+
const items = decodePayload(payload, {
|
|
175
|
+
call,
|
|
176
|
+
maxItems: offers.entries.length,
|
|
177
|
+
});
|
|
178
|
+
if (items.length !== offers.entries.length) {
|
|
179
|
+
throw new CalldataCallError("offers.entries", call.id);
|
|
180
|
+
}
|
|
181
|
+
const { blue } = context;
|
|
182
|
+
if (blue != null &&
|
|
183
|
+
!sameHex(blue.salt, GroupUtils.hash(items.map(({ offer }) => ({
|
|
184
|
+
...offer,
|
|
185
|
+
maxAssets: cap,
|
|
186
|
+
callback: zeroAddress,
|
|
187
|
+
callbackData: "0x",
|
|
188
|
+
}))))) {
|
|
189
|
+
throw new CalldataCallError("callback_salt", call.id);
|
|
190
|
+
}
|
|
191
|
+
const tree = rebuildTree(items, context);
|
|
192
|
+
if (!sameHex(tree.root, root) ||
|
|
193
|
+
tree.items.some(({ ratifierData }, index) => !sameHex(ratifierData, items[index]?.ratifierData))) {
|
|
194
|
+
throw new CalldataCallError("root", call.id);
|
|
195
|
+
}
|
|
196
|
+
const entries = offers.entries;
|
|
197
|
+
tree.leaves.forEach((leaf, index) => {
|
|
198
|
+
if (!sameAddress(leaf.allowedTaker, zeroAddress)) {
|
|
199
|
+
throw new CalldataCallError("offers.allowed_taker", call.id);
|
|
200
|
+
}
|
|
201
|
+
checkOffer(leaf, {
|
|
202
|
+
entry: entries[index],
|
|
203
|
+
ratifier: expectedRatifier,
|
|
204
|
+
cap,
|
|
205
|
+
context,
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
if (tree.leaves.some(({ offer }) => !sameHex(offer.group, tree.group))) {
|
|
209
|
+
throw new CalldataCallError("offers.group", call.id);
|
|
210
|
+
}
|
|
211
|
+
return tree.leaves.map(({ offer }) => offer);
|
|
212
|
+
}
|
|
213
|
+
function checkEnvelope(args, { call }) {
|
|
214
|
+
const [, , , , , , , height, nonce, signatureDeadline, v, r, s, groupsToCancel, , deadline,] = args;
|
|
215
|
+
if (height !== 0n ||
|
|
216
|
+
nonce !== 0n ||
|
|
217
|
+
signatureDeadline !== 0n ||
|
|
218
|
+
v !== 0 ||
|
|
219
|
+
!sameHex(r, zeroHash) ||
|
|
220
|
+
!sameHex(s, zeroHash)) {
|
|
221
|
+
throw new CalldataCallError("root_signature", call.id);
|
|
222
|
+
}
|
|
223
|
+
if (groupsToCancel.length !== 0) {
|
|
224
|
+
throw new CalldataCallError("cancellations", call.id);
|
|
225
|
+
}
|
|
226
|
+
if (deadline === 0n) {
|
|
227
|
+
throw new CalldataAmountError("deadline", call.id);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function checkBlueFunding(args, { intent, call, cap }) {
|
|
231
|
+
const [market, assets, salt] = args;
|
|
232
|
+
const source = intent.action === "midnight.make_lend" ? intent.funding_source : undefined;
|
|
233
|
+
if (source != null &&
|
|
234
|
+
!["wallet", "variable_rate_market"].includes(source.type)) {
|
|
235
|
+
throw new CalldataUnsupportedError("funding_source", call.id);
|
|
236
|
+
}
|
|
237
|
+
if (intent.action !== "midnight.make_lend" ||
|
|
238
|
+
intent.funding_source?.type !== "variable_rate_market") {
|
|
239
|
+
if (!sameAddress(market.loanToken, zeroAddress) ||
|
|
240
|
+
!sameAddress(market.collateralToken, zeroAddress) ||
|
|
241
|
+
!sameAddress(market.oracle, zeroAddress) ||
|
|
242
|
+
!sameAddress(market.irm, zeroAddress) ||
|
|
243
|
+
market.lltv !== 0n ||
|
|
244
|
+
assets !== 0n ||
|
|
245
|
+
!sameHex(salt, zeroHash)) {
|
|
246
|
+
throw new CalldataCallError("funding_source", call.id);
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
const callbackData = encodeAbiParameters([marketParamsAbi], [market]);
|
|
251
|
+
if (!sameHex(keccak256(callbackData), intent.funding_source.market_id)) {
|
|
252
|
+
throw new CalldataTargetError("funding_source.market_id", call.id);
|
|
253
|
+
}
|
|
254
|
+
if (cap === 0n || cap > maxUint128) {
|
|
255
|
+
throw new CalldataAmountError("amount", call.id);
|
|
256
|
+
}
|
|
257
|
+
requireAmount(assets, {
|
|
258
|
+
expected: cap,
|
|
259
|
+
field: "amount",
|
|
260
|
+
call,
|
|
261
|
+
});
|
|
262
|
+
return {
|
|
263
|
+
funding: intent.funding,
|
|
264
|
+
loanToken: market.loanToken,
|
|
265
|
+
assets,
|
|
266
|
+
salt,
|
|
267
|
+
callback: getBlueBuyCallbackAddress({
|
|
268
|
+
chainId: intent.chain_id,
|
|
269
|
+
owner: intent.account,
|
|
270
|
+
salt,
|
|
271
|
+
}),
|
|
272
|
+
callbackData,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function checkCollateralSupplies(args, { offers, context }) {
|
|
276
|
+
const { intent, chain, call, blue } = context;
|
|
277
|
+
const [, , , market, supplies, , , , , , , , , , , , wrappedNative] = args;
|
|
278
|
+
const value = uint(call.value, { field: "value", call });
|
|
279
|
+
const { requested, funding } = intent.action === "midnight.make_borrow"
|
|
280
|
+
? { requested: intent.collateral_supplies, funding: intent.funding }
|
|
281
|
+
: { requested: undefined, funding: undefined };
|
|
282
|
+
if (requested == null) {
|
|
283
|
+
if (market.chainId !== 0n ||
|
|
284
|
+
!sameAddress(market.midnight, zeroAddress) ||
|
|
285
|
+
!sameAddress(market.loanToken, zeroAddress) ||
|
|
286
|
+
market.collateralParams.length !== 0 ||
|
|
287
|
+
market.maturity !== 0n ||
|
|
288
|
+
market.rcfThreshold !== 0n ||
|
|
289
|
+
!sameAddress(market.enterGate, zeroAddress) ||
|
|
290
|
+
!sameAddress(market.liquidatorGate, zeroAddress) ||
|
|
291
|
+
supplies.length !== 0 ||
|
|
292
|
+
(blue == null &&
|
|
293
|
+
(value !== 0n || !sameAddress(wrappedNative, zeroAddress)))) {
|
|
294
|
+
throw new CalldataCallError("collateral_supplies", call.id);
|
|
295
|
+
}
|
|
296
|
+
if (blue == null ||
|
|
297
|
+
(value === 0n && sameAddress(wrappedNative, zeroAddress))) {
|
|
298
|
+
return [];
|
|
299
|
+
}
|
|
300
|
+
if (value === 0n ||
|
|
301
|
+
!sameAddress(blue.loanToken, chain.wNative) ||
|
|
302
|
+
!sameAddress(wrappedNative, chain.wNative) ||
|
|
303
|
+
value > blue.assets) {
|
|
304
|
+
throw new CalldataAmountError("value", call.id);
|
|
305
|
+
}
|
|
306
|
+
requireNativeBudget(value, { funding: blue.funding, call });
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
const collateralMarketId = marketId(market, call);
|
|
310
|
+
if (market.chainId !== BigInt(intent.chain_id) ||
|
|
311
|
+
!sameAddress(market.midnight, chain.midnight) ||
|
|
312
|
+
offers.some((offer) => !sameHex(marketId(offer.market, call), collateralMarketId))) {
|
|
313
|
+
throw new CalldataTargetError("market", call.id);
|
|
314
|
+
}
|
|
315
|
+
const collateral = checkCollateral(supplies, { requested, market, call });
|
|
316
|
+
if (value === 0n && sameAddress(wrappedNative, zeroAddress)) {
|
|
317
|
+
return collateral;
|
|
318
|
+
}
|
|
319
|
+
const nativeCap = collateral.find(({ token }) => sameAddress(token, chain.wNative))?.assets ??
|
|
320
|
+
0n;
|
|
321
|
+
if (value === 0n ||
|
|
322
|
+
!sameAddress(wrappedNative, chain.wNative) ||
|
|
323
|
+
value > nativeCap) {
|
|
324
|
+
throw new CalldataAmountError("value", call.id);
|
|
325
|
+
}
|
|
326
|
+
requireNativeBudget(value, { funding, call });
|
|
327
|
+
return collateral;
|
|
328
|
+
}
|
|
329
|
+
function loanTokenPulls(offers, { intent, chain, call, cap, blue }) {
|
|
330
|
+
if (!sides[intent.action].buy) {
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
const token = offers[0].market.loanToken;
|
|
334
|
+
if (blue != null && !sameAddress(blue.loanToken, token)) {
|
|
335
|
+
throw new CalldataTargetError("funding_source.market_id", call.id);
|
|
336
|
+
}
|
|
337
|
+
return [
|
|
338
|
+
{
|
|
339
|
+
token,
|
|
340
|
+
spender: blue == null ? chain.midnight : chain.midnightBundlesV2,
|
|
341
|
+
assets: intent.action === "midnight.make_lend" ? cap : 0n,
|
|
342
|
+
},
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
function verifyMake(intent, { calls, chain, }) {
|
|
346
|
+
const call = mainCall(calls, Number.MAX_SAFE_INTEGER);
|
|
347
|
+
requireTarget(call, chain.midnightBundlesV2);
|
|
348
|
+
const decoded = decode(call, { abi: midnightBundlesV2Abi });
|
|
349
|
+
if (decoded.functionName !== "midnightBundlesV2CancelAndMake") {
|
|
350
|
+
throw new CalldataCallError("function", call.id);
|
|
351
|
+
}
|
|
352
|
+
if (intent.amount?.unit !== sides[intent.action].unit) {
|
|
353
|
+
throw new CalldataAmountError("amount.unit", call.id);
|
|
354
|
+
}
|
|
355
|
+
const base = {
|
|
356
|
+
intent,
|
|
357
|
+
chain,
|
|
358
|
+
call,
|
|
359
|
+
cap: uint(intent.amount.value, { field: "amount", call }),
|
|
360
|
+
};
|
|
361
|
+
const context = { ...base, blue: checkBlueFunding(decoded.args, base) };
|
|
362
|
+
checkEnvelope(decoded.args, context);
|
|
363
|
+
const offers = checkOffers(decoded.args, context);
|
|
364
|
+
const collateral = checkCollateralSupplies(decoded.args, { offers, context });
|
|
365
|
+
return [
|
|
366
|
+
...checkPrerequisites(calls.slice(0, -1), {
|
|
367
|
+
...context,
|
|
368
|
+
pulls: [...collateral, ...loanTokenPulls(offers, context)],
|
|
369
|
+
allowAuthorization: true,
|
|
370
|
+
}),
|
|
371
|
+
checked(call, decoded),
|
|
372
|
+
];
|
|
373
|
+
}
|
|
374
|
+
/** @internal */
|
|
375
|
+
export const midnightMakerRules = {
|
|
376
|
+
"midnight.make_borrow": verifyMake,
|
|
377
|
+
"midnight.make_lend": verifyMake,
|
|
378
|
+
"midnight.make_repay": verifyMake,
|
|
379
|
+
"midnight.make_withdraw": verifyMake,
|
|
380
|
+
};
|
|
@@ -10,14 +10,9 @@ declare function cancel(intent: Extract<MidnightOfferManagementIntent, {
|
|
|
10
10
|
declare function withdrawFunding(intent: Extract<MidnightOfferManagementIntent, {
|
|
11
11
|
action: "midnight.withdraw_funding";
|
|
12
12
|
}>, context: Context): import("./types.js").DecodedCalldataCall[];
|
|
13
|
-
declare function unsupported(intent: MidnightOfferManagementIntent, { calls }: Context): never;
|
|
14
13
|
/** @internal */
|
|
15
14
|
export declare const midnightOfferRules: {
|
|
16
15
|
"midnight.cancel_offers": typeof cancel;
|
|
17
16
|
"midnight.withdraw_funding": typeof withdrawFunding;
|
|
18
|
-
"midnight.make_borrow": typeof unsupported;
|
|
19
|
-
"midnight.make_lend": typeof unsupported;
|
|
20
|
-
"midnight.make_repay": typeof unsupported;
|
|
21
|
-
"midnight.make_withdraw": typeof unsupported;
|
|
22
17
|
};
|
|
23
18
|
export {};
|
|
@@ -3,7 +3,7 @@ import { midnightAbi } from "@morpho-org/midnight-sdk";
|
|
|
3
3
|
import { marketParamsAbi } from "@morpho-org/morpho-ts/abis";
|
|
4
4
|
import { encodeAbiParameters, keccak256, maxUint128, zeroAddress } from "viem";
|
|
5
5
|
import { checked, decode, innerCall, isCallAddress, mainCall, requireAccount, requireAmount, requireTarget, sameAddress, sameHex, uint, } from "./checks.js";
|
|
6
|
-
import { CalldataAmountError, CalldataCallError, CalldataTargetError,
|
|
6
|
+
import { CalldataAmountError, CalldataCallError, CalldataTargetError, } from "./errors.js";
|
|
7
7
|
function singleCall({ calls }) {
|
|
8
8
|
const call = mainCall(calls, 1);
|
|
9
9
|
requireAmount(uint(call.value, { field: "value", call }), {
|
|
@@ -91,15 +91,8 @@ function withdrawFunding(intent, context) {
|
|
|
91
91
|
}
|
|
92
92
|
return [checked(call, decoded)];
|
|
93
93
|
}
|
|
94
|
-
function unsupported(intent, { calls }) {
|
|
95
|
-
throw new CalldataUnsupportedError(intent.action, calls[0]?.id);
|
|
96
|
-
}
|
|
97
94
|
/** @internal */
|
|
98
95
|
export const midnightOfferRules = {
|
|
99
96
|
"midnight.cancel_offers": cancel,
|
|
100
97
|
"midnight.withdraw_funding": withdrawFunding,
|
|
101
|
-
"midnight.make_borrow": unsupported,
|
|
102
|
-
"midnight.make_lend": unsupported,
|
|
103
|
-
"midnight.make_repay": unsupported,
|
|
104
|
-
"midnight.make_withdraw": unsupported,
|
|
105
98
|
};
|
package/lib/esm/types.d.ts
CHANGED
|
@@ -232,6 +232,7 @@ export type MidnightMarketIntent = MidnightMarket & (({
|
|
|
232
232
|
readonly referral_fee?: never;
|
|
233
233
|
})) | ({
|
|
234
234
|
readonly action: "midnight.supply_collateral";
|
|
235
|
+
readonly funding?: Funding;
|
|
235
236
|
} & MidnightCollateral<Amount<"assets">>) | ({
|
|
236
237
|
readonly action: "midnight.withdraw_collateral";
|
|
237
238
|
} & MidnightWithdrawals));
|
|
@@ -5,6 +5,7 @@ import { blueRules } from "./blue.js";
|
|
|
5
5
|
import { isCallAddress, sameAddress } from "./checks.js";
|
|
6
6
|
import { CalldataCallError, CalldataChainError, CalldataFeeError, CalldataRecipientError, CalldataTargetError, } from "./errors.js";
|
|
7
7
|
import { midnightRules } from "./midnight.js";
|
|
8
|
+
import { midnightMakerRules } from "./midnightMakers.js";
|
|
8
9
|
import { midnightOfferRules } from "./midnightOffers.js";
|
|
9
10
|
import { vaultRules } from "./vault.js";
|
|
10
11
|
const rules = {
|
|
@@ -12,6 +13,7 @@ const rules = {
|
|
|
12
13
|
...blueRules,
|
|
13
14
|
...midnightRules,
|
|
14
15
|
...midnightOfferRules,
|
|
16
|
+
...midnightMakerRules,
|
|
15
17
|
};
|
|
16
18
|
/**
|
|
17
19
|
* Statically checks Actions API calldata against the original request before signing; does not simulate execution.
|
package/lib/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Installed @morpho-org/calldata-verifier package version. */
|
|
2
|
-
export declare const version = "0.
|
|
2
|
+
export declare const version = "0.3.0";
|
package/lib/esm/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/calldata-verifier",
|
|
3
3
|
"description": "Verify Morpho action calldata locally before signing.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@morpho-org/blue-sdk-viem": "^7.6.0",
|
|
31
|
-
"@morpho-org/midnight-sdk": "^2.
|
|
32
|
-
"@morpho-org/morpho-sdk": "^10.
|
|
33
|
-
"@morpho-org/morpho-ts": "^4.5.
|
|
34
|
-
"@noble/curves": "1.9.1"
|
|
31
|
+
"@morpho-org/midnight-sdk": "^2.10.0",
|
|
32
|
+
"@morpho-org/morpho-sdk": "^10.4.0",
|
|
33
|
+
"@morpho-org/morpho-ts": "^4.5.1",
|
|
34
|
+
"@noble/curves": "1.9.1",
|
|
35
|
+
"fflate": "0.8.3"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
38
|
"viem": "^2.9.2"
|