@morpho-org/midnight-sdk 2.7.0-next.1 → 2.8.0-next.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/lib/cjs/api/MidnightApi.js +63 -52
- package/lib/cjs/api/helpers.js +30 -17
- package/lib/cjs/fetch/Position.js +2 -1
- package/lib/cjs/fetch/Ratifier.js +4 -5
- package/lib/cjs/market/Market.js +2 -1
- package/lib/cjs/market/MarketUtils.js +16 -14
- package/lib/cjs/math/TakeAmountsLib.js +8 -4
- package/lib/cjs/math/TickLib.js +12 -6
- package/lib/cjs/offers/OfferChainUtils.js +35 -26
- package/lib/cjs/offers/OfferUtils.js +17 -10
- package/lib/cjs/position/Position.js +2 -1
- package/lib/cjs/signatures/EcrecoverRatifier.js +22 -17
- package/lib/cjs/signatures/Group.js +4 -2
- package/lib/cjs/signatures/Payload.js +18 -9
- package/lib/cjs/signatures/PriceRatifierV1.js +5 -4
- package/lib/cjs/signatures/RateRatifierV1.js +19 -11
- package/lib/cjs/signatures/Ratifier.js +4 -4
- package/lib/cjs/signatures/SetterRatifier.js +2 -1
- package/lib/cjs/signatures/Tree.js +8 -4
- package/lib/cjs/signatures/TreeUtils.js +16 -13
- package/lib/cjs/signatures/ratifierV1Internal.js +34 -24
- package/lib/cjs/signatures/treeMathInternal.d.ts +2 -0
- package/lib/cjs/signatures/treeMathInternal.js +10 -0
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/api/MidnightApi.js +63 -52
- package/lib/esm/api/helpers.js +30 -17
- package/lib/esm/fetch/Position.js +2 -1
- package/lib/esm/fetch/Ratifier.js +4 -5
- package/lib/esm/market/Market.js +2 -1
- package/lib/esm/market/MarketUtils.js +16 -14
- package/lib/esm/math/TakeAmountsLib.js +8 -4
- package/lib/esm/math/TickLib.js +12 -6
- package/lib/esm/offers/OfferChainUtils.js +35 -26
- package/lib/esm/offers/OfferUtils.js +17 -10
- package/lib/esm/position/Position.js +2 -1
- package/lib/esm/signatures/EcrecoverRatifier.js +22 -17
- package/lib/esm/signatures/Group.js +4 -2
- package/lib/esm/signatures/Payload.js +18 -9
- package/lib/esm/signatures/PriceRatifierV1.js +5 -4
- package/lib/esm/signatures/RateRatifierV1.js +19 -11
- package/lib/esm/signatures/Ratifier.js +5 -5
- package/lib/esm/signatures/SetterRatifier.js +2 -1
- package/lib/esm/signatures/Tree.js +8 -4
- package/lib/esm/signatures/TreeUtils.js +17 -14
- package/lib/esm/signatures/ratifierV1Internal.js +35 -25
- package/lib/esm/signatures/treeMathInternal.d.ts +2 -0
- package/lib/esm/signatures/treeMathInternal.js +9 -0
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +13 -13
|
@@ -14,10 +14,11 @@ import { RateRatifierV1 } from "./RateRatifierV1.js";
|
|
|
14
14
|
import { Ratifier } from "./Ratifier.js";
|
|
15
15
|
import { SetterRatifierUtils } from "./SetterRatifier.js";
|
|
16
16
|
import { Tree } from "./Tree.js";
|
|
17
|
-
import { isPowerOfTwo, nextPowerOfTwo } from "./treeMathInternal.js";
|
|
17
|
+
import { isPowerOfTwo, nextPowerOfTwo, requireTreeItem, } from "./treeMathInternal.js";
|
|
18
18
|
function padOfferStructs(offers) {
|
|
19
|
-
if (isPowerOfTwo(offers.length))
|
|
19
|
+
if (isPowerOfTwo(offers.length)) {
|
|
20
20
|
return [...offers];
|
|
21
|
+
}
|
|
21
22
|
const paddedLength = nextPowerOfTwo(offers.length);
|
|
22
23
|
return [
|
|
23
24
|
...offers,
|
|
@@ -27,9 +28,10 @@ function padOfferStructs(offers) {
|
|
|
27
28
|
function assertLeafOffers(offers, leafHashes) {
|
|
28
29
|
const seen = new Set();
|
|
29
30
|
for (const [index, offer] of offers.entries()) {
|
|
30
|
-
if (isEmptyOfferStruct(offer, { allowDefaultGroup: true }))
|
|
31
|
+
if (isEmptyOfferStruct(offer, { allowDefaultGroup: true })) {
|
|
31
32
|
continue;
|
|
32
|
-
|
|
33
|
+
}
|
|
34
|
+
const leafHash = requireTreeItem(leafHashes, index);
|
|
33
35
|
if (seen.has(leafHash)) {
|
|
34
36
|
throw new InvalidTreeError(`Duplicate offer hash "${leafHash}" in tree.`);
|
|
35
37
|
}
|
|
@@ -41,12 +43,12 @@ function assertLeafOffers(offers, leafHashes) {
|
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
function buildLayers(leaves) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
let level = [...leaves];
|
|
47
|
+
const layers = [level];
|
|
46
48
|
while (level.length > 1) {
|
|
47
49
|
const next = [];
|
|
48
50
|
for (let i = 0; i < level.length; i += 2) {
|
|
49
|
-
next.push(TreeUtils.hashNode(level
|
|
51
|
+
next.push(TreeUtils.hashNode(requireTreeItem(level, i), requireTreeItem(level, i + 1)));
|
|
50
52
|
}
|
|
51
53
|
layers.push(next);
|
|
52
54
|
level = next;
|
|
@@ -58,14 +60,15 @@ function assertProvableLeaves(leaves) {
|
|
|
58
60
|
throw new InvalidTreeError(`Tree leaf count "${leaves.length}" is not a power of two.`);
|
|
59
61
|
}
|
|
60
62
|
const height = Math.log2(leaves.length);
|
|
61
|
-
if (height > MAX_TREE_HEIGHT)
|
|
63
|
+
if (height > MAX_TREE_HEIGHT) {
|
|
62
64
|
throw new InvalidTreeHeightError(height);
|
|
65
|
+
}
|
|
63
66
|
return height;
|
|
64
67
|
}
|
|
65
68
|
function computeRootFromLeaves(leaves) {
|
|
66
69
|
const height = assertProvableLeaves(leaves);
|
|
67
70
|
const layers = buildLayers(leaves);
|
|
68
|
-
return { root: layers
|
|
71
|
+
return { root: requireTreeItem(requireTreeItem(layers, height), 0), height };
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* Object-compatible tree hashing, root, proof, and verification helpers.
|
|
@@ -223,17 +226,17 @@ export var TreeUtils;
|
|
|
223
226
|
}
|
|
224
227
|
}
|
|
225
228
|
else if (params.ratification.type === "ecrecover") {
|
|
226
|
-
if (params.ratification.signature
|
|
229
|
+
if (params.ratification.signature == null) {
|
|
227
230
|
items = await EcrecoverRatifierUtils.ratify({
|
|
228
231
|
tree,
|
|
229
|
-
|
|
232
|
+
client: params.ratification.client,
|
|
230
233
|
account: params.ratification.account,
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
else {
|
|
234
237
|
items = await EcrecoverRatifierUtils.ratify({
|
|
235
238
|
tree,
|
|
236
|
-
|
|
239
|
+
signature: params.ratification.signature,
|
|
237
240
|
account: params.ratification.account,
|
|
238
241
|
});
|
|
239
242
|
}
|
|
@@ -479,7 +482,7 @@ export var TreeUtils;
|
|
|
479
482
|
const layers = buildLayers(params.tree.leaves);
|
|
480
483
|
const proof = layers
|
|
481
484
|
.slice(0, -1)
|
|
482
|
-
.map((level, layerIndex) => level
|
|
485
|
+
.map((level, layerIndex) => requireTreeItem(level, (index >> layerIndex) ^ 1));
|
|
483
486
|
return deepFreeze({ root: params.tree.root, leafIndex, proof });
|
|
484
487
|
}
|
|
485
488
|
TreeUtils.buildProof = buildProof;
|
|
@@ -547,7 +550,7 @@ export var TreeUtils;
|
|
|
547
550
|
return deepFreeze(Array.from({ length: count }, (_, index) => ({
|
|
548
551
|
root: params.tree.root,
|
|
549
552
|
leafIndex: BigInt(index),
|
|
550
|
-
proof: siblingLayers.map((level, layerIndex) => level
|
|
553
|
+
proof: siblingLayers.map((level, layerIndex) => requireTreeItem(level, (index >> layerIndex) ^ 1)),
|
|
551
554
|
})));
|
|
552
555
|
}
|
|
553
556
|
TreeUtils.buildProofs = buildProofs;
|
|
@@ -5,7 +5,7 @@ import { Offer, OfferUtils, } from "../offers/index.js";
|
|
|
5
5
|
import { GroupUtils } from "./GroupUtils.js";
|
|
6
6
|
import { isZeroAddress } from "./offerStructInternal.js";
|
|
7
7
|
import { TreeUtils } from "./TreeUtils.js";
|
|
8
|
-
import { isPowerOfTwo, nextPowerOfTwo } from "./treeMathInternal.js";
|
|
8
|
+
import { isPowerOfTwo, nextPowerOfTwo, requireTreeItem, } from "./treeMathInternal.js";
|
|
9
9
|
/** @internal Resolves V1 leaf groups mirroring the router's group_identity content ids and validates explicit groups. */
|
|
10
10
|
export function assignRatifierV1Groups(leaves, groupId) {
|
|
11
11
|
const offers = leaves.map((leaf) => leaf.offer.hasExplicitGroup
|
|
@@ -13,17 +13,20 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
13
13
|
: Offer.from({ ...leaf.offer, group: groupId([leaf]) }));
|
|
14
14
|
const buckets = new Map();
|
|
15
15
|
for (const [index, leaf] of leaves.entries()) {
|
|
16
|
-
if (!leaf.offer.hasExplicitGroup)
|
|
16
|
+
if (!leaf.offer.hasExplicitGroup) {
|
|
17
17
|
continue;
|
|
18
|
+
}
|
|
18
19
|
const label = leaf.offer.group.toLowerCase();
|
|
19
20
|
const bucket = buckets.get(label);
|
|
20
|
-
if (bucket)
|
|
21
|
+
if (bucket) {
|
|
21
22
|
bucket.push(index);
|
|
22
|
-
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
23
25
|
buckets.set(label, [index]);
|
|
26
|
+
}
|
|
24
27
|
}
|
|
25
28
|
for (const [label, indices] of buckets) {
|
|
26
|
-
const bucketLeaves = indices.map((index) => leaves
|
|
29
|
+
const bucketLeaves = indices.map((index) => requireTreeItem(leaves, index));
|
|
27
30
|
// Called only to throw on incompatible group members.
|
|
28
31
|
OfferUtils.validateOfferGroup({
|
|
29
32
|
offers: bucketLeaves.map((leaf) => leaf.offer),
|
|
@@ -34,13 +37,17 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
34
37
|
}
|
|
35
38
|
const group = groupId(bucketLeaves);
|
|
36
39
|
for (const index of indices) {
|
|
37
|
-
offers[index] = Offer.from({
|
|
40
|
+
offers[index] = Offer.from({
|
|
41
|
+
...requireTreeItem(leaves, index).offer,
|
|
42
|
+
group,
|
|
43
|
+
});
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
const finalGroups = new Map();
|
|
41
47
|
for (const [index, offer] of offers.entries()) {
|
|
42
48
|
const label = offer.group.toLowerCase();
|
|
43
|
-
const hasExplicitGroup = leaves
|
|
49
|
+
const hasExplicitGroup = requireTreeItem(leaves, index).offer
|
|
50
|
+
.hasExplicitGroup;
|
|
44
51
|
const bucket = finalGroups.get(label);
|
|
45
52
|
if (bucket) {
|
|
46
53
|
bucket.offers.push(offer);
|
|
@@ -51,8 +58,9 @@ export function assignRatifierV1Groups(leaves, groupId) {
|
|
|
51
58
|
}
|
|
52
59
|
}
|
|
53
60
|
for (const { offers: groupOffers, hasExplicitGroup, } of finalGroups.values()) {
|
|
54
|
-
if (!hasExplicitGroup)
|
|
61
|
+
if (!hasExplicitGroup) {
|
|
55
62
|
continue;
|
|
63
|
+
}
|
|
56
64
|
// Catches explicit groups that collide with a defaulted or re-derived id.
|
|
57
65
|
OfferUtils.validateOfferGroup({ offers: groupOffers });
|
|
58
66
|
}
|
|
@@ -78,9 +86,9 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
78
86
|
throw new InvalidTreeError(`All offers in a ${label} tree must use one ratifier; expected "${ratifier}", got "${other}". Build separate trees per ratifier.`);
|
|
79
87
|
}
|
|
80
88
|
}
|
|
81
|
-
const
|
|
89
|
+
const leaves = entries.map(hashLeaf);
|
|
82
90
|
const seen = new Set();
|
|
83
|
-
for (const leafHash of
|
|
91
|
+
for (const leafHash of leaves) {
|
|
84
92
|
if (seen.has(leafHash)) {
|
|
85
93
|
throw new InvalidTreeError(`Duplicate leaf hash "${leafHash}" in tree.`);
|
|
86
94
|
}
|
|
@@ -93,11 +101,12 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
93
101
|
...entries,
|
|
94
102
|
...Array.from({ length: paddingCount }, () => padding),
|
|
95
103
|
];
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
if (paddingCount > 0) {
|
|
105
|
+
const paddingHash = hashLeaf(padding);
|
|
106
|
+
for (let index = 0; index < paddingCount; index++) {
|
|
107
|
+
leaves.push(paddingHash);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
101
110
|
const { root, height } = TreeUtils.buildRootFromLeaves(leaves);
|
|
102
111
|
return { entries: paddedEntries, leaves, root, height };
|
|
103
112
|
}
|
|
@@ -113,8 +122,9 @@ export function buildRatifierV1Descriptor(params) {
|
|
|
113
122
|
*/
|
|
114
123
|
export function resolveRatifierV1Tree(tree, helpers) {
|
|
115
124
|
const { buildDescriptor, hashLeaf, isPadding, ratifierOf, label } = helpers;
|
|
116
|
-
if (Array.isArray(tree))
|
|
125
|
+
if (Array.isArray(tree)) {
|
|
117
126
|
return buildDescriptor(tree);
|
|
127
|
+
}
|
|
118
128
|
const descriptor = tree;
|
|
119
129
|
if ("type" in descriptor &&
|
|
120
130
|
descriptor.type != null &&
|
|
@@ -135,8 +145,9 @@ export function resolveRatifierV1Tree(tree, helpers) {
|
|
|
135
145
|
expectedLength !== nextPowerOfTwo(descriptor.offers.length)) {
|
|
136
146
|
throw new InvalidTreeError("Tree entries, leaves, offers, and height describe different trees.");
|
|
137
147
|
}
|
|
138
|
-
const
|
|
139
|
-
const
|
|
148
|
+
const entries = Array.from({ length: expectedLength }, (_, index) => requireTreeItem(descriptor.entries, index));
|
|
149
|
+
const visibleEntries = entries.slice(0, descriptor.offers.length);
|
|
150
|
+
const ratifier = ratifierOf(requireTreeItem(visibleEntries, 0));
|
|
140
151
|
for (const entry of visibleEntries.slice(1)) {
|
|
141
152
|
const other = ratifierOf(entry);
|
|
142
153
|
if (!isAddressEqual(other, ratifier)) {
|
|
@@ -150,25 +161,24 @@ export function resolveRatifierV1Tree(tree, helpers) {
|
|
|
150
161
|
}
|
|
151
162
|
helpers.validateEntry?.(entry);
|
|
152
163
|
}
|
|
153
|
-
const computedLeaves =
|
|
164
|
+
const computedLeaves = entries.map((entry) => hashLeaf(entry));
|
|
154
165
|
const seen = new Set();
|
|
155
166
|
for (const [index, offer] of descriptor.offers.entries()) {
|
|
156
|
-
const entry =
|
|
167
|
+
const entry = requireTreeItem(entries, index);
|
|
157
168
|
if (OfferUtils.hashStruct(OfferUtils.toStruct({ offer: Offer.from(offer) })).toLowerCase() !== OfferUtils.hashStruct(entry.offer).toLowerCase()) {
|
|
158
169
|
throw new InvalidTreeError("Visible offers do not match the tree entries.");
|
|
159
170
|
}
|
|
160
|
-
const leafHash = computedLeaves
|
|
171
|
+
const leafHash = requireTreeItem(computedLeaves, index).toLowerCase();
|
|
161
172
|
if (seen.has(leafHash)) {
|
|
162
173
|
throw new InvalidTreeError(`Duplicate leaf hash "${leafHash}" in tree.`);
|
|
163
174
|
}
|
|
164
175
|
seen.add(leafHash);
|
|
165
176
|
}
|
|
166
|
-
if (descriptor.
|
|
167
|
-
.slice(descriptor.offers.length)
|
|
168
|
-
.some((entry) => !isPadding(entry))) {
|
|
177
|
+
if (entries.slice(descriptor.offers.length).some((entry) => !isPadding(entry))) {
|
|
169
178
|
throw new InvalidTreeError("Tree padding contains entries hidden from the visible offer list.");
|
|
170
179
|
}
|
|
171
|
-
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !==
|
|
180
|
+
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !==
|
|
181
|
+
requireTreeItem(descriptor.leaves, index).toLowerCase())) {
|
|
172
182
|
throw new InvalidTreeError("Tree leaves do not match its entries.");
|
|
173
183
|
}
|
|
174
184
|
if (TreeUtils.buildRootFromLeaves(descriptor.leaves).root.toLowerCase() !==
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** @internal Reads a tree item at a required index, rejecting missing entries. */
|
|
2
|
+
export declare function requireTreeItem<T>(items: readonly T[], index: number): T;
|
|
1
3
|
/** @internal Returns whether `value` is a positive power of two. */
|
|
2
4
|
export declare function isPowerOfTwo(value: number): boolean;
|
|
3
5
|
/** @internal Returns the smallest power of two greater than or equal to `value`. */
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import { InvalidTreeError } from "../errors.js";
|
|
2
|
+
/** @internal Reads a tree item at a required index, rejecting missing entries. */
|
|
3
|
+
export function requireTreeItem(items, index) {
|
|
4
|
+
const item = items[index];
|
|
5
|
+
if (item == null) {
|
|
6
|
+
throw new InvalidTreeError(`Tree item at index "${index}" is missing.`);
|
|
7
|
+
}
|
|
8
|
+
return item;
|
|
9
|
+
}
|
|
1
10
|
/** @internal Returns whether `value` is a positive power of two. */
|
|
2
11
|
export function isPowerOfTwo(value) {
|
|
3
12
|
return value > 0 && (value & (value - 1)) === 0;
|
package/lib/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Installed @morpho-org/midnight-sdk package version. */
|
|
2
|
-
export declare const version = "2.
|
|
2
|
+
export declare const version = "2.8.0-next.0";
|
package/lib/esm/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/midnight-sdk",
|
|
3
3
|
"description": "Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.8.0-next.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -40,25 +40,25 @@
|
|
|
40
40
|
"lib",
|
|
41
41
|
"MIGRATION-v1-to-v2.md"
|
|
42
42
|
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"prepublish": "bun run build",
|
|
45
|
+
"compile": "node ../../scripts/compile-solidity.ts midnight-sdk",
|
|
46
|
+
"build": "tsc --noEmit && bun run build:cjs && bun run build:esm",
|
|
47
|
+
"build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
|
|
48
|
+
"build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
|
|
49
|
+
"test": "vitest --root ../.. --project midnight-sdk --project midnight-sdk-fork"
|
|
50
|
+
},
|
|
43
51
|
"peerDependencies": {
|
|
44
|
-
"@morpho-org/morpho-ts": "^4.
|
|
52
|
+
"@morpho-org/morpho-ts": "^4.6.0-0",
|
|
45
53
|
"viem": "^2.0.0"
|
|
46
54
|
},
|
|
47
55
|
"devDependencies": {
|
|
48
|
-
"@morpho-org/morpho-ts": "^4.
|
|
49
|
-
"@morpho-org/test": "^2.
|
|
56
|
+
"@morpho-org/morpho-ts": "^4.6.0-next.0",
|
|
57
|
+
"@morpho-org/test": "^2.15.0-next.0",
|
|
50
58
|
"fast-check": "^4.9.0",
|
|
51
59
|
"typescript": "7.0.2",
|
|
52
60
|
"viem": "^2.55.10",
|
|
53
61
|
"vitest": "^4.1.12"
|
|
54
62
|
},
|
|
55
|
-
"scripts": {
|
|
56
|
-
"prepublish": "pnpm build",
|
|
57
|
-
"compile": "node ../../scripts/compile-solidity.ts midnight-sdk",
|
|
58
|
-
"build": "tsc --noEmit && pnpm build:cjs && pnpm build:esm",
|
|
59
|
-
"build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
|
|
60
|
-
"build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json",
|
|
61
|
-
"test": "vitest --root ../.. --project midnight-sdk --project midnight-sdk-fork"
|
|
62
|
-
},
|
|
63
63
|
"types": "./lib/esm/index.d.ts"
|
|
64
|
-
}
|
|
64
|
+
}
|