@morpho-org/midnight-sdk 0.1.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 +252 -0
- package/lib/cjs/abis.d.ts +3170 -0
- package/lib/cjs/abis.js +4073 -0
- package/lib/cjs/api/MidnightApi.d.ts +532 -0
- package/lib/cjs/api/MidnightApi.js +713 -0
- package/lib/cjs/api/helpers.d.ts +20 -0
- package/lib/cjs/api/helpers.js +189 -0
- package/lib/cjs/api/index.d.ts +2 -0
- package/lib/cjs/api/index.js +21 -0
- package/lib/cjs/api/types.d.ts +453 -0
- package/lib/cjs/api/types.js +2 -0
- package/lib/cjs/constants.d.ts +168 -0
- package/lib/cjs/constants.js +187 -0
- package/lib/cjs/errors.d.ts +253 -0
- package/lib/cjs/errors.js +309 -0
- package/lib/cjs/fetch/Market.d.ts +65 -0
- package/lib/cjs/fetch/Market.js +117 -0
- package/lib/cjs/fetch/Offer.d.ts +74 -0
- package/lib/cjs/fetch/Offer.js +119 -0
- package/lib/cjs/fetch/Position.d.ts +85 -0
- package/lib/cjs/fetch/Position.js +148 -0
- package/lib/cjs/fetch/Ratifier.d.ts +38 -0
- package/lib/cjs/fetch/Ratifier.js +57 -0
- package/lib/cjs/fetch/index.d.ts +5 -0
- package/lib/cjs/fetch/index.js +21 -0
- package/lib/cjs/fetch/types.d.ts +57 -0
- package/lib/cjs/fetch/types.js +2 -0
- package/lib/cjs/fetch/utils.d.ts +8 -0
- package/lib/cjs/fetch/utils.js +18 -0
- package/lib/cjs/index.d.ts +18 -0
- package/lib/cjs/index.js +57 -0
- package/lib/cjs/market/Market.d.ts +613 -0
- package/lib/cjs/market/Market.js +503 -0
- package/lib/cjs/market/MarketUtils.d.ts +189 -0
- package/lib/cjs/market/MarketUtils.js +303 -0
- package/lib/cjs/market/Position.d.ts +424 -0
- package/lib/cjs/market/Position.js +425 -0
- package/lib/cjs/market/PositionUtils.d.ts +115 -0
- package/lib/cjs/market/PositionUtils.js +194 -0
- package/lib/cjs/market/index.d.ts +4 -0
- package/lib/cjs/market/index.js +20 -0
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
- package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
- package/lib/cjs/math/TakeAmountsLib.js +199 -0
- package/lib/cjs/math/TickLib.d.ts +200 -0
- package/lib/cjs/math/TickLib.js +272 -0
- package/lib/cjs/math/index.d.ts +3 -0
- package/lib/cjs/math/index.js +19 -0
- package/lib/cjs/offers/Offer.d.ts +534 -0
- package/lib/cjs/offers/Offer.js +343 -0
- package/lib/cjs/offers/OfferUtils.d.ts +565 -0
- package/lib/cjs/offers/OfferUtils.js +659 -0
- package/lib/cjs/offers/index.d.ts +2 -0
- package/lib/cjs/offers/index.js +18 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/queries/GetPosition.d.ts +55 -0
- package/lib/cjs/queries/GetPosition.js +74 -0
- package/lib/cjs/queries/index.d.ts +1 -0
- package/lib/cjs/queries/index.js +37 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
- package/lib/cjs/signatures/Group.d.ts +178 -0
- package/lib/cjs/signatures/Group.js +193 -0
- package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
- package/lib/cjs/signatures/GroupUtils.js +127 -0
- package/lib/cjs/signatures/Payload.d.ts +283 -0
- package/lib/cjs/signatures/Payload.js +570 -0
- package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
- package/lib/cjs/signatures/RatifierUtils.js +76 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
- package/lib/cjs/signatures/Tree.d.ts +241 -0
- package/lib/cjs/signatures/Tree.js +263 -0
- package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
- package/lib/cjs/signatures/TreeUtils.js +461 -0
- package/lib/cjs/signatures/index.d.ts +7 -0
- package/lib/cjs/signatures/index.js +23 -0
- package/lib/esm/abis.d.ts +3170 -0
- package/lib/esm/abis.js +4070 -0
- package/lib/esm/api/MidnightApi.d.ts +532 -0
- package/lib/esm/api/MidnightApi.js +709 -0
- package/lib/esm/api/helpers.d.ts +20 -0
- package/lib/esm/api/helpers.js +180 -0
- package/lib/esm/api/index.d.ts +2 -0
- package/lib/esm/api/index.js +2 -0
- package/lib/esm/api/types.d.ts +453 -0
- package/lib/esm/api/types.js +1 -0
- package/lib/esm/constants.d.ts +168 -0
- package/lib/esm/constants.js +184 -0
- package/lib/esm/errors.d.ts +253 -0
- package/lib/esm/errors.js +291 -0
- package/lib/esm/fetch/Market.d.ts +65 -0
- package/lib/esm/fetch/Market.js +113 -0
- package/lib/esm/fetch/Offer.d.ts +74 -0
- package/lib/esm/fetch/Offer.js +116 -0
- package/lib/esm/fetch/Position.d.ts +85 -0
- package/lib/esm/fetch/Position.js +144 -0
- package/lib/esm/fetch/Ratifier.d.ts +38 -0
- package/lib/esm/fetch/Ratifier.js +54 -0
- package/lib/esm/fetch/index.d.ts +5 -0
- package/lib/esm/fetch/index.js +5 -0
- package/lib/esm/fetch/types.d.ts +57 -0
- package/lib/esm/fetch/types.js +1 -0
- package/lib/esm/fetch/utils.d.ts +8 -0
- package/lib/esm/fetch/utils.js +12 -0
- package/lib/esm/index.d.ts +18 -0
- package/lib/esm/index.js +18 -0
- package/lib/esm/market/Market.d.ts +613 -0
- package/lib/esm/market/Market.js +498 -0
- package/lib/esm/market/MarketUtils.d.ts +189 -0
- package/lib/esm/market/MarketUtils.js +300 -0
- package/lib/esm/market/Position.d.ts +424 -0
- package/lib/esm/market/Position.js +420 -0
- package/lib/esm/market/PositionUtils.d.ts +115 -0
- package/lib/esm/market/PositionUtils.js +191 -0
- package/lib/esm/market/index.d.ts +4 -0
- package/lib/esm/market/index.js +4 -0
- package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/esm/math/ConsumableUnitsLib.js +74 -0
- package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
- package/lib/esm/math/TakeAmountsLib.js +196 -0
- package/lib/esm/math/TickLib.d.ts +200 -0
- package/lib/esm/math/TickLib.js +269 -0
- package/lib/esm/math/index.d.ts +3 -0
- package/lib/esm/math/index.js +3 -0
- package/lib/esm/offers/Offer.d.ts +534 -0
- package/lib/esm/offers/Offer.js +339 -0
- package/lib/esm/offers/OfferUtils.d.ts +565 -0
- package/lib/esm/offers/OfferUtils.js +656 -0
- package/lib/esm/offers/index.d.ts +2 -0
- package/lib/esm/offers/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/queries/GetPosition.d.ts +55 -0
- package/lib/esm/queries/GetPosition.js +71 -0
- package/lib/esm/queries/index.d.ts +1 -0
- package/lib/esm/queries/index.js +1 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
- package/lib/esm/signatures/Group.d.ts +178 -0
- package/lib/esm/signatures/Group.js +189 -0
- package/lib/esm/signatures/GroupUtils.d.ts +191 -0
- package/lib/esm/signatures/GroupUtils.js +124 -0
- package/lib/esm/signatures/Payload.d.ts +283 -0
- package/lib/esm/signatures/Payload.js +564 -0
- package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
- package/lib/esm/signatures/RatifierUtils.js +73 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
- package/lib/esm/signatures/Tree.d.ts +241 -0
- package/lib/esm/signatures/Tree.js +259 -0
- package/lib/esm/signatures/TreeUtils.d.ts +573 -0
- package/lib/esm/signatures/TreeUtils.js +458 -0
- package/lib/esm/signatures/index.d.ts +7 -0
- package/lib/esm/signatures/index.js +7 -0
- package/package.json +69 -0
- package/src/index.ts +19 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { Hash } from "viem";
|
|
2
|
+
import { type IOffer, Offer } from "../offers/index.js";
|
|
3
|
+
import { type GroupInput, type IGroup } from "./GroupUtils.js";
|
|
4
|
+
/**
|
|
5
|
+
* Protocol offer group with one shared consumption group id.
|
|
6
|
+
*
|
|
7
|
+
* Create a group after building related offers and before building the tree to
|
|
8
|
+
* publish. Offers inside one group must share maker, side, and loan token. The
|
|
9
|
+
* constructor hashes every offer to derive the group id, then copies each offer
|
|
10
|
+
* with that id. Offers inside one group must also share cap mode and value
|
|
11
|
+
* because Midnight tracks one consumed scalar per maker and group; group
|
|
12
|
+
* creation is resource-intensive compared to offer construction.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
17
|
+
* import { zeroAddress } from "viem";
|
|
18
|
+
*
|
|
19
|
+
* const offer = Offer.create({
|
|
20
|
+
* market: {
|
|
21
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
22
|
+
* collateralParams: [
|
|
23
|
+
* {
|
|
24
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
25
|
+
* lltv: 770000000000000000n,
|
|
26
|
+
* maxLif: 1061007957559681697n,
|
|
27
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
28
|
+
* },
|
|
29
|
+
* ],
|
|
30
|
+
* maturity: 54_000n,
|
|
31
|
+
* rcfThreshold: 0n,
|
|
32
|
+
* enterGate: zeroAddress,
|
|
33
|
+
* liquidatorGate: zeroAddress,
|
|
34
|
+
* },
|
|
35
|
+
* buy: true,
|
|
36
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
37
|
+
* tick: 5_000n,
|
|
38
|
+
* expiry: 3_600n,
|
|
39
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
40
|
+
* maxUnits: 100n,
|
|
41
|
+
* });
|
|
42
|
+
* const group = Group.create([offer]);
|
|
43
|
+
* console.log(group.offers.length);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class Group implements IGroup {
|
|
47
|
+
private readonly _offers;
|
|
48
|
+
/** Content-addressed group id. */
|
|
49
|
+
readonly id: Hash;
|
|
50
|
+
private constructor();
|
|
51
|
+
/**
|
|
52
|
+
* Offers in this protocol group.
|
|
53
|
+
*
|
|
54
|
+
* Returns a fresh array so callers cannot mutate the group's offer list.
|
|
55
|
+
*
|
|
56
|
+
* @returns Offers in caller order.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
60
|
+
* import { zeroAddress } from "viem";
|
|
61
|
+
*
|
|
62
|
+
* const offer = Offer.create({
|
|
63
|
+
* market: {
|
|
64
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
65
|
+
* collateralParams: [
|
|
66
|
+
* {
|
|
67
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
68
|
+
* lltv: 770000000000000000n,
|
|
69
|
+
* maxLif: 1061007957559681697n,
|
|
70
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
71
|
+
* },
|
|
72
|
+
* ],
|
|
73
|
+
* maturity: 54_000n,
|
|
74
|
+
* rcfThreshold: 0n,
|
|
75
|
+
* enterGate: zeroAddress,
|
|
76
|
+
* liquidatorGate: zeroAddress,
|
|
77
|
+
* },
|
|
78
|
+
* buy: true,
|
|
79
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
80
|
+
* tick: 5_000n,
|
|
81
|
+
* expiry: 3_600n,
|
|
82
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
83
|
+
* maxUnits: 100n,
|
|
84
|
+
* });
|
|
85
|
+
* const group = Group.create([offer]);
|
|
86
|
+
* console.log(group.offers.length);
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
get offers(): readonly Offer[];
|
|
90
|
+
/**
|
|
91
|
+
* Returns a group instance from group or standalone offer input.
|
|
92
|
+
*
|
|
93
|
+
* Use at boundaries that accept either a prebuilt `Group`, a plain `IGroup`,
|
|
94
|
+
* or a standalone offer that should form its own group. Existing `Group`
|
|
95
|
+
* instances are returned as-is.
|
|
96
|
+
*
|
|
97
|
+
* @param entry - Group object or standalone offer.
|
|
98
|
+
* @returns Group instance.
|
|
99
|
+
* @throws {InvalidOfferGroupError} when group mechanics are invalid.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
103
|
+
* import { zeroAddress } from "viem";
|
|
104
|
+
*
|
|
105
|
+
* const offer = Offer.create({
|
|
106
|
+
* market: {
|
|
107
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
108
|
+
* collateralParams: [
|
|
109
|
+
* {
|
|
110
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
111
|
+
* lltv: 770000000000000000n,
|
|
112
|
+
* maxLif: 1061007957559681697n,
|
|
113
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
114
|
+
* },
|
|
115
|
+
* ],
|
|
116
|
+
* maturity: 54_000n,
|
|
117
|
+
* rcfThreshold: 0n,
|
|
118
|
+
* enterGate: zeroAddress,
|
|
119
|
+
* liquidatorGate: zeroAddress,
|
|
120
|
+
* },
|
|
121
|
+
* buy: true,
|
|
122
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
123
|
+
* tick: 5_000n,
|
|
124
|
+
* expiry: 3_600n,
|
|
125
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
126
|
+
* maxUnits: 100n,
|
|
127
|
+
* });
|
|
128
|
+
* const group = Group.from(offer);
|
|
129
|
+
* console.log(group.offers.length);
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
static from(entry: GroupInput): Group;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a protocol-valid offer group.
|
|
135
|
+
*
|
|
136
|
+
* Use after `Offer.create` for laddered offers with the same cap mode and
|
|
137
|
+
* value that should consume from one group id. Pass the returned group into
|
|
138
|
+
* `Tree.create` alongside other groups or standalone offers. This hashes every
|
|
139
|
+
* offer and copies the validated offers into group-owned instances, so it is
|
|
140
|
+
* resource-intensive and should be done once per group definition.
|
|
141
|
+
*
|
|
142
|
+
* @param offers - Iterable of offers to group.
|
|
143
|
+
* @returns Group instance.
|
|
144
|
+
* @throws {InvalidOfferGroupError} when group mechanics are invalid.
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
148
|
+
* import { zeroAddress } from "viem";
|
|
149
|
+
*
|
|
150
|
+
* const offer = Offer.create({
|
|
151
|
+
* market: {
|
|
152
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
153
|
+
* collateralParams: [
|
|
154
|
+
* {
|
|
155
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
156
|
+
* lltv: 770000000000000000n,
|
|
157
|
+
* maxLif: 1061007957559681697n,
|
|
158
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
159
|
+
* },
|
|
160
|
+
* ],
|
|
161
|
+
* maturity: 54_000n,
|
|
162
|
+
* rcfThreshold: 0n,
|
|
163
|
+
* enterGate: zeroAddress,
|
|
164
|
+
* liquidatorGate: zeroAddress,
|
|
165
|
+
* },
|
|
166
|
+
* buy: true,
|
|
167
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
168
|
+
* tick: 5_000n,
|
|
169
|
+
* expiry: 3_600n,
|
|
170
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
171
|
+
* maxUnits: 100n,
|
|
172
|
+
* });
|
|
173
|
+
* const group = Group.create([offer]);
|
|
174
|
+
* console.log(group.offers.length);
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
static create(offers: Iterable<IOffer>): Group;
|
|
178
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Group = void 0;
|
|
4
|
+
const index_js_1 = require("../offers/index.js");
|
|
5
|
+
const GroupUtils_js_1 = require("./GroupUtils.js");
|
|
6
|
+
/**
|
|
7
|
+
* Protocol offer group with one shared consumption group id.
|
|
8
|
+
*
|
|
9
|
+
* Create a group after building related offers and before building the tree to
|
|
10
|
+
* publish. Offers inside one group must share maker, side, and loan token. The
|
|
11
|
+
* constructor hashes every offer to derive the group id, then copies each offer
|
|
12
|
+
* with that id. Offers inside one group must also share cap mode and value
|
|
13
|
+
* because Midnight tracks one consumed scalar per maker and group; group
|
|
14
|
+
* creation is resource-intensive compared to offer construction.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
19
|
+
* import { zeroAddress } from "viem";
|
|
20
|
+
*
|
|
21
|
+
* const offer = Offer.create({
|
|
22
|
+
* market: {
|
|
23
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
24
|
+
* collateralParams: [
|
|
25
|
+
* {
|
|
26
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
27
|
+
* lltv: 770000000000000000n,
|
|
28
|
+
* maxLif: 1061007957559681697n,
|
|
29
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
30
|
+
* },
|
|
31
|
+
* ],
|
|
32
|
+
* maturity: 54_000n,
|
|
33
|
+
* rcfThreshold: 0n,
|
|
34
|
+
* enterGate: zeroAddress,
|
|
35
|
+
* liquidatorGate: zeroAddress,
|
|
36
|
+
* },
|
|
37
|
+
* buy: true,
|
|
38
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
39
|
+
* tick: 5_000n,
|
|
40
|
+
* expiry: 3_600n,
|
|
41
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
42
|
+
* maxUnits: 100n,
|
|
43
|
+
* });
|
|
44
|
+
* const group = Group.create([offer]);
|
|
45
|
+
* console.log(group.offers.length);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
class Group {
|
|
49
|
+
_offers;
|
|
50
|
+
/** Content-addressed group id. */
|
|
51
|
+
id;
|
|
52
|
+
constructor(offers) {
|
|
53
|
+
const validatedOffers = index_js_1.OfferUtils.validateOfferGroup({ offers });
|
|
54
|
+
this.id = GroupUtils_js_1.GroupUtils.hash(validatedOffers);
|
|
55
|
+
this._offers = validatedOffers.map((offer) => new index_js_1.Offer({ ...offer, group: this.id }));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Offers in this protocol group.
|
|
59
|
+
*
|
|
60
|
+
* Returns a fresh array so callers cannot mutate the group's offer list.
|
|
61
|
+
*
|
|
62
|
+
* @returns Offers in caller order.
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
66
|
+
* import { zeroAddress } from "viem";
|
|
67
|
+
*
|
|
68
|
+
* const offer = Offer.create({
|
|
69
|
+
* market: {
|
|
70
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
71
|
+
* collateralParams: [
|
|
72
|
+
* {
|
|
73
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
74
|
+
* lltv: 770000000000000000n,
|
|
75
|
+
* maxLif: 1061007957559681697n,
|
|
76
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
77
|
+
* },
|
|
78
|
+
* ],
|
|
79
|
+
* maturity: 54_000n,
|
|
80
|
+
* rcfThreshold: 0n,
|
|
81
|
+
* enterGate: zeroAddress,
|
|
82
|
+
* liquidatorGate: zeroAddress,
|
|
83
|
+
* },
|
|
84
|
+
* buy: true,
|
|
85
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
86
|
+
* tick: 5_000n,
|
|
87
|
+
* expiry: 3_600n,
|
|
88
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
89
|
+
* maxUnits: 100n,
|
|
90
|
+
* });
|
|
91
|
+
* const group = Group.create([offer]);
|
|
92
|
+
* console.log(group.offers.length);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
get offers() {
|
|
96
|
+
return [...this._offers];
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns a group instance from group or standalone offer input.
|
|
100
|
+
*
|
|
101
|
+
* Use at boundaries that accept either a prebuilt `Group`, a plain `IGroup`,
|
|
102
|
+
* or a standalone offer that should form its own group. Existing `Group`
|
|
103
|
+
* instances are returned as-is.
|
|
104
|
+
*
|
|
105
|
+
* @param entry - Group object or standalone offer.
|
|
106
|
+
* @returns Group instance.
|
|
107
|
+
* @throws {InvalidOfferGroupError} when group mechanics are invalid.
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
111
|
+
* import { zeroAddress } from "viem";
|
|
112
|
+
*
|
|
113
|
+
* const offer = Offer.create({
|
|
114
|
+
* market: {
|
|
115
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
116
|
+
* collateralParams: [
|
|
117
|
+
* {
|
|
118
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
119
|
+
* lltv: 770000000000000000n,
|
|
120
|
+
* maxLif: 1061007957559681697n,
|
|
121
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
122
|
+
* },
|
|
123
|
+
* ],
|
|
124
|
+
* maturity: 54_000n,
|
|
125
|
+
* rcfThreshold: 0n,
|
|
126
|
+
* enterGate: zeroAddress,
|
|
127
|
+
* liquidatorGate: zeroAddress,
|
|
128
|
+
* },
|
|
129
|
+
* buy: true,
|
|
130
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
131
|
+
* tick: 5_000n,
|
|
132
|
+
* expiry: 3_600n,
|
|
133
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
134
|
+
* maxUnits: 100n,
|
|
135
|
+
* });
|
|
136
|
+
* const group = Group.from(offer);
|
|
137
|
+
* console.log(group.offers.length);
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
static from(entry) {
|
|
141
|
+
return entry instanceof Group
|
|
142
|
+
? entry
|
|
143
|
+
: new Group("offers" in entry ? entry.offers : [entry]);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates a protocol-valid offer group.
|
|
147
|
+
*
|
|
148
|
+
* Use after `Offer.create` for laddered offers with the same cap mode and
|
|
149
|
+
* value that should consume from one group id. Pass the returned group into
|
|
150
|
+
* `Tree.create` alongside other groups or standalone offers. This hashes every
|
|
151
|
+
* offer and copies the validated offers into group-owned instances, so it is
|
|
152
|
+
* resource-intensive and should be done once per group definition.
|
|
153
|
+
*
|
|
154
|
+
* @param offers - Iterable of offers to group.
|
|
155
|
+
* @returns Group instance.
|
|
156
|
+
* @throws {InvalidOfferGroupError} when group mechanics are invalid.
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* import { Group, Offer } from "@morpho-org/midnight-sdk";
|
|
160
|
+
* import { zeroAddress } from "viem";
|
|
161
|
+
*
|
|
162
|
+
* const offer = Offer.create({
|
|
163
|
+
* market: {
|
|
164
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
165
|
+
* collateralParams: [
|
|
166
|
+
* {
|
|
167
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
168
|
+
* lltv: 770000000000000000n,
|
|
169
|
+
* maxLif: 1061007957559681697n,
|
|
170
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
171
|
+
* },
|
|
172
|
+
* ],
|
|
173
|
+
* maturity: 54_000n,
|
|
174
|
+
* rcfThreshold: 0n,
|
|
175
|
+
* enterGate: zeroAddress,
|
|
176
|
+
* liquidatorGate: zeroAddress,
|
|
177
|
+
* },
|
|
178
|
+
* buy: true,
|
|
179
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
180
|
+
* tick: 5_000n,
|
|
181
|
+
* expiry: 3_600n,
|
|
182
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
183
|
+
* maxUnits: 100n,
|
|
184
|
+
* });
|
|
185
|
+
* const group = Group.create([offer]);
|
|
186
|
+
* console.log(group.offers.length);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
static create(offers) {
|
|
190
|
+
return new Group(offers);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.Group = Group;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { type Hash } from "viem";
|
|
2
|
+
import { type IOffer, type OfferStruct } from "../offers/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Plain offer group shape accepted by group and tree utilities.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Offer, type IGroup } from "@morpho-org/midnight-sdk";
|
|
9
|
+
* import { zeroAddress } from "viem";
|
|
10
|
+
*
|
|
11
|
+
* const group: IGroup = {
|
|
12
|
+
* offers: [
|
|
13
|
+
* Offer.create({
|
|
14
|
+
* market: {
|
|
15
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
16
|
+
* collateralParams: [
|
|
17
|
+
* {
|
|
18
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
19
|
+
* lltv: 770000000000000000n,
|
|
20
|
+
* maxLif: 1061007957559681697n,
|
|
21
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
22
|
+
* },
|
|
23
|
+
* ],
|
|
24
|
+
* maturity: 54_000n,
|
|
25
|
+
* rcfThreshold: 0n,
|
|
26
|
+
* enterGate: zeroAddress,
|
|
27
|
+
* liquidatorGate: zeroAddress,
|
|
28
|
+
* },
|
|
29
|
+
* buy: true,
|
|
30
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
31
|
+
* tick: 5_000n,
|
|
32
|
+
* group: zeroHash,
|
|
33
|
+
* expiry: 3_600n,
|
|
34
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
35
|
+
* maxUnits: 100n,
|
|
36
|
+
* }),
|
|
37
|
+
* ],
|
|
38
|
+
* };
|
|
39
|
+
* console.log(group.offers.length);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export interface IGroup {
|
|
43
|
+
/** Offers in this protocol group. The group id is derived from this list. */
|
|
44
|
+
readonly offers: readonly IOffer[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Group object or standalone offer accepted by tree helpers.
|
|
48
|
+
*
|
|
49
|
+
* Use standalone offers for independent consumption groups. Use `Group.create`
|
|
50
|
+
* first when multiple offers from the same maker, side, loan token, cap mode,
|
|
51
|
+
* and cap value should share one consumption group.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { Offer, type GroupInput } from "@morpho-org/midnight-sdk";
|
|
56
|
+
* import { zeroAddress } from "viem";
|
|
57
|
+
*
|
|
58
|
+
* const input: GroupInput = Offer.create({
|
|
59
|
+
* market: {
|
|
60
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
61
|
+
* collateralParams: [
|
|
62
|
+
* {
|
|
63
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
64
|
+
* lltv: 770000000000000000n,
|
|
65
|
+
* maxLif: 1061007957559681697n,
|
|
66
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
67
|
+
* },
|
|
68
|
+
* ],
|
|
69
|
+
* maturity: 54_000n,
|
|
70
|
+
* rcfThreshold: 0n,
|
|
71
|
+
* enterGate: zeroAddress,
|
|
72
|
+
* liquidatorGate: zeroAddress,
|
|
73
|
+
* },
|
|
74
|
+
* buy: true,
|
|
75
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
76
|
+
* tick: 5_000n,
|
|
77
|
+
* expiry: 3_600n,
|
|
78
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
79
|
+
* maxUnits: 100n,
|
|
80
|
+
* });
|
|
81
|
+
* console.log(input);
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export type GroupInput = IGroup | IOffer;
|
|
85
|
+
/**
|
|
86
|
+
* Make-side helpers for Midnight offer groups.
|
|
87
|
+
*
|
|
88
|
+
* Groups sit between `Offer.create` and `Tree.create`: they assign a
|
|
89
|
+
* content-addressed group id to offers that share one consumption bucket. One
|
|
90
|
+
* group must use one cap mode and value because Midnight tracks a single
|
|
91
|
+
* consumed scalar per maker and group. Tree helpers derive that id for explicit
|
|
92
|
+
* groups and preserve it on grouped offer copies.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { GroupUtils } from "@morpho-org/midnight-sdk";
|
|
97
|
+
*
|
|
98
|
+
* console.log(typeof GroupUtils.hash);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare namespace GroupUtils {
|
|
102
|
+
/**
|
|
103
|
+
* Derives the deterministic content-addressed id for a group of offers.
|
|
104
|
+
*
|
|
105
|
+
* This mirrors the router implementation: hash each offer with `group = 0`,
|
|
106
|
+
* sort those hashes, concatenate them, then keccak the result.
|
|
107
|
+
*
|
|
108
|
+
* @param offers - Offers to hash as one group.
|
|
109
|
+
* @returns Content-addressed group id.
|
|
110
|
+
* @throws {InvalidOfferGroupError} when `offers` is empty.
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* import { GroupUtils, Offer } from "@morpho-org/midnight-sdk";
|
|
114
|
+
* import { zeroAddress } from "viem";
|
|
115
|
+
*
|
|
116
|
+
* const offer = Offer.create({
|
|
117
|
+
* market: {
|
|
118
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
119
|
+
* collateralParams: [
|
|
120
|
+
* {
|
|
121
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
122
|
+
* lltv: 770000000000000000n,
|
|
123
|
+
* maxLif: 1061007957559681697n,
|
|
124
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
125
|
+
* },
|
|
126
|
+
* ],
|
|
127
|
+
* maturity: 54_000n,
|
|
128
|
+
* rcfThreshold: 0n,
|
|
129
|
+
* enterGate: zeroAddress,
|
|
130
|
+
* liquidatorGate: zeroAddress,
|
|
131
|
+
* },
|
|
132
|
+
* buy: true,
|
|
133
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
134
|
+
* tick: 5_000n,
|
|
135
|
+
* expiry: 3_600n,
|
|
136
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
137
|
+
* maxUnits: 100n,
|
|
138
|
+
* });
|
|
139
|
+
* const id = GroupUtils.hash([offer]);
|
|
140
|
+
* console.log(id);
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
function hash(offers: Iterable<IOffer>): Hash;
|
|
144
|
+
/**
|
|
145
|
+
* Converts a group into ABI-compatible offers carrying the derived group id.
|
|
146
|
+
*
|
|
147
|
+
* Use after `Group.create` for custom encoders that need offer structs with
|
|
148
|
+
* their final group id. The id is derived from the full offer list and
|
|
149
|
+
* applied while encoding, so plain groups cannot drift from offer fields.
|
|
150
|
+
*
|
|
151
|
+
* This helper is encode-only and does not validate protocol group mechanics.
|
|
152
|
+
* Validate with `Group.create` or {@link OfferUtils.validateOfferGroup}
|
|
153
|
+
* before relying on the output for signing, tree roots, or calldata.
|
|
154
|
+
*
|
|
155
|
+
* @param group - Group to encode.
|
|
156
|
+
* @returns ABI-compatible offers in caller order.
|
|
157
|
+
* @throws {InvalidOfferGroupError} when the group has no offers.
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { Group, GroupUtils, Offer } from "@morpho-org/midnight-sdk";
|
|
161
|
+
* import { zeroAddress } from "viem";
|
|
162
|
+
*
|
|
163
|
+
* const offer = Offer.create({
|
|
164
|
+
* market: {
|
|
165
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
166
|
+
* collateralParams: [
|
|
167
|
+
* {
|
|
168
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
169
|
+
* lltv: 770000000000000000n,
|
|
170
|
+
* maxLif: 1061007957559681697n,
|
|
171
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
172
|
+
* },
|
|
173
|
+
* ],
|
|
174
|
+
* maturity: 54_000n,
|
|
175
|
+
* rcfThreshold: 0n,
|
|
176
|
+
* enterGate: zeroAddress,
|
|
177
|
+
* liquidatorGate: zeroAddress,
|
|
178
|
+
* },
|
|
179
|
+
* buy: true,
|
|
180
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
181
|
+
* tick: 5_000n,
|
|
182
|
+
* expiry: 3_600n,
|
|
183
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
184
|
+
* maxUnits: 100n,
|
|
185
|
+
* });
|
|
186
|
+
* const structs = GroupUtils.toStructs(Group.create([offer]));
|
|
187
|
+
* console.log(structs.length);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
function toStructs(group: IGroup): readonly OfferStruct[];
|
|
191
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GroupUtils = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const errors_js_1 = require("../errors.js");
|
|
6
|
+
const index_js_1 = require("../offers/index.js");
|
|
7
|
+
/**
|
|
8
|
+
* Make-side helpers for Midnight offer groups.
|
|
9
|
+
*
|
|
10
|
+
* Groups sit between `Offer.create` and `Tree.create`: they assign a
|
|
11
|
+
* content-addressed group id to offers that share one consumption bucket. One
|
|
12
|
+
* group must use one cap mode and value because Midnight tracks a single
|
|
13
|
+
* consumed scalar per maker and group. Tree helpers derive that id for explicit
|
|
14
|
+
* groups and preserve it on grouped offer copies.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { GroupUtils } from "@morpho-org/midnight-sdk";
|
|
19
|
+
*
|
|
20
|
+
* console.log(typeof GroupUtils.hash);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
var GroupUtils;
|
|
24
|
+
(function (GroupUtils) {
|
|
25
|
+
/**
|
|
26
|
+
* Derives the deterministic content-addressed id for a group of offers.
|
|
27
|
+
*
|
|
28
|
+
* This mirrors the router implementation: hash each offer with `group = 0`,
|
|
29
|
+
* sort those hashes, concatenate them, then keccak the result.
|
|
30
|
+
*
|
|
31
|
+
* @param offers - Offers to hash as one group.
|
|
32
|
+
* @returns Content-addressed group id.
|
|
33
|
+
* @throws {InvalidOfferGroupError} when `offers` is empty.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { GroupUtils, Offer } from "@morpho-org/midnight-sdk";
|
|
37
|
+
* import { zeroAddress } from "viem";
|
|
38
|
+
*
|
|
39
|
+
* const offer = Offer.create({
|
|
40
|
+
* market: {
|
|
41
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
42
|
+
* collateralParams: [
|
|
43
|
+
* {
|
|
44
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
45
|
+
* lltv: 770000000000000000n,
|
|
46
|
+
* maxLif: 1061007957559681697n,
|
|
47
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
48
|
+
* },
|
|
49
|
+
* ],
|
|
50
|
+
* maturity: 54_000n,
|
|
51
|
+
* rcfThreshold: 0n,
|
|
52
|
+
* enterGate: zeroAddress,
|
|
53
|
+
* liquidatorGate: zeroAddress,
|
|
54
|
+
* },
|
|
55
|
+
* buy: true,
|
|
56
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
57
|
+
* tick: 5_000n,
|
|
58
|
+
* expiry: 3_600n,
|
|
59
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
60
|
+
* maxUnits: 100n,
|
|
61
|
+
* });
|
|
62
|
+
* const id = GroupUtils.hash([offer]);
|
|
63
|
+
* console.log(id);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
function hash(offers) {
|
|
67
|
+
const offerInputs = Array.from(offers);
|
|
68
|
+
if (offerInputs.length === 0) {
|
|
69
|
+
throw new errors_js_1.InvalidOfferGroupError("Provide at least one offer in the group.");
|
|
70
|
+
}
|
|
71
|
+
const offerHashes = offerInputs.map((offer) => index_js_1.OfferUtils.groupHash(offer));
|
|
72
|
+
const sorted = offerHashes.length > 1 ? [...offerHashes].sort() : offerHashes;
|
|
73
|
+
return (0, viem_1.keccak256)((0, viem_1.concat)(sorted));
|
|
74
|
+
}
|
|
75
|
+
GroupUtils.hash = hash;
|
|
76
|
+
/**
|
|
77
|
+
* Converts a group into ABI-compatible offers carrying the derived group id.
|
|
78
|
+
*
|
|
79
|
+
* Use after `Group.create` for custom encoders that need offer structs with
|
|
80
|
+
* their final group id. The id is derived from the full offer list and
|
|
81
|
+
* applied while encoding, so plain groups cannot drift from offer fields.
|
|
82
|
+
*
|
|
83
|
+
* This helper is encode-only and does not validate protocol group mechanics.
|
|
84
|
+
* Validate with `Group.create` or {@link OfferUtils.validateOfferGroup}
|
|
85
|
+
* before relying on the output for signing, tree roots, or calldata.
|
|
86
|
+
*
|
|
87
|
+
* @param group - Group to encode.
|
|
88
|
+
* @returns ABI-compatible offers in caller order.
|
|
89
|
+
* @throws {InvalidOfferGroupError} when the group has no offers.
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { Group, GroupUtils, Offer } from "@morpho-org/midnight-sdk";
|
|
93
|
+
* import { zeroAddress } from "viem";
|
|
94
|
+
*
|
|
95
|
+
* const offer = Offer.create({
|
|
96
|
+
* market: {
|
|
97
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
98
|
+
* collateralParams: [
|
|
99
|
+
* {
|
|
100
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
101
|
+
* lltv: 770000000000000000n,
|
|
102
|
+
* maxLif: 1061007957559681697n,
|
|
103
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
104
|
+
* },
|
|
105
|
+
* ],
|
|
106
|
+
* maturity: 54_000n,
|
|
107
|
+
* rcfThreshold: 0n,
|
|
108
|
+
* enterGate: zeroAddress,
|
|
109
|
+
* liquidatorGate: zeroAddress,
|
|
110
|
+
* },
|
|
111
|
+
* buy: true,
|
|
112
|
+
* maker: "0x0000000000000000000000000000000000009000",
|
|
113
|
+
* tick: 5_000n,
|
|
114
|
+
* expiry: 3_600n,
|
|
115
|
+
* ratifier: "0x0000000000000000000000000000000000004000",
|
|
116
|
+
* maxUnits: 100n,
|
|
117
|
+
* });
|
|
118
|
+
* const structs = GroupUtils.toStructs(Group.create([offer]));
|
|
119
|
+
* console.log(structs.length);
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
function toStructs(group) {
|
|
123
|
+
const groupId = hash(group.offers);
|
|
124
|
+
return group.offers.map((offer) => index_js_1.OfferUtils.toStruct({ offer, group: groupId }));
|
|
125
|
+
}
|
|
126
|
+
GroupUtils.toStructs = toStructs;
|
|
127
|
+
})(GroupUtils || (exports.GroupUtils = GroupUtils = {}));
|