@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.
Files changed (157) hide show
  1. package/README.md +252 -0
  2. package/lib/cjs/abis.d.ts +3170 -0
  3. package/lib/cjs/abis.js +4073 -0
  4. package/lib/cjs/api/MidnightApi.d.ts +532 -0
  5. package/lib/cjs/api/MidnightApi.js +713 -0
  6. package/lib/cjs/api/helpers.d.ts +20 -0
  7. package/lib/cjs/api/helpers.js +189 -0
  8. package/lib/cjs/api/index.d.ts +2 -0
  9. package/lib/cjs/api/index.js +21 -0
  10. package/lib/cjs/api/types.d.ts +453 -0
  11. package/lib/cjs/api/types.js +2 -0
  12. package/lib/cjs/constants.d.ts +168 -0
  13. package/lib/cjs/constants.js +187 -0
  14. package/lib/cjs/errors.d.ts +253 -0
  15. package/lib/cjs/errors.js +309 -0
  16. package/lib/cjs/fetch/Market.d.ts +65 -0
  17. package/lib/cjs/fetch/Market.js +117 -0
  18. package/lib/cjs/fetch/Offer.d.ts +74 -0
  19. package/lib/cjs/fetch/Offer.js +119 -0
  20. package/lib/cjs/fetch/Position.d.ts +85 -0
  21. package/lib/cjs/fetch/Position.js +148 -0
  22. package/lib/cjs/fetch/Ratifier.d.ts +38 -0
  23. package/lib/cjs/fetch/Ratifier.js +57 -0
  24. package/lib/cjs/fetch/index.d.ts +5 -0
  25. package/lib/cjs/fetch/index.js +21 -0
  26. package/lib/cjs/fetch/types.d.ts +57 -0
  27. package/lib/cjs/fetch/types.js +2 -0
  28. package/lib/cjs/fetch/utils.d.ts +8 -0
  29. package/lib/cjs/fetch/utils.js +18 -0
  30. package/lib/cjs/index.d.ts +18 -0
  31. package/lib/cjs/index.js +57 -0
  32. package/lib/cjs/market/Market.d.ts +613 -0
  33. package/lib/cjs/market/Market.js +503 -0
  34. package/lib/cjs/market/MarketUtils.d.ts +189 -0
  35. package/lib/cjs/market/MarketUtils.js +303 -0
  36. package/lib/cjs/market/Position.d.ts +424 -0
  37. package/lib/cjs/market/Position.js +425 -0
  38. package/lib/cjs/market/PositionUtils.d.ts +115 -0
  39. package/lib/cjs/market/PositionUtils.js +194 -0
  40. package/lib/cjs/market/index.d.ts +4 -0
  41. package/lib/cjs/market/index.js +20 -0
  42. package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
  43. package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
  44. package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
  45. package/lib/cjs/math/TakeAmountsLib.js +199 -0
  46. package/lib/cjs/math/TickLib.d.ts +200 -0
  47. package/lib/cjs/math/TickLib.js +272 -0
  48. package/lib/cjs/math/index.d.ts +3 -0
  49. package/lib/cjs/math/index.js +19 -0
  50. package/lib/cjs/offers/Offer.d.ts +534 -0
  51. package/lib/cjs/offers/Offer.js +343 -0
  52. package/lib/cjs/offers/OfferUtils.d.ts +565 -0
  53. package/lib/cjs/offers/OfferUtils.js +659 -0
  54. package/lib/cjs/offers/index.d.ts +2 -0
  55. package/lib/cjs/offers/index.js +18 -0
  56. package/lib/cjs/package.json +1 -0
  57. package/lib/cjs/queries/GetPosition.d.ts +55 -0
  58. package/lib/cjs/queries/GetPosition.js +74 -0
  59. package/lib/cjs/queries/index.d.ts +1 -0
  60. package/lib/cjs/queries/index.js +37 -0
  61. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  62. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
  63. package/lib/cjs/signatures/Group.d.ts +178 -0
  64. package/lib/cjs/signatures/Group.js +193 -0
  65. package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
  66. package/lib/cjs/signatures/GroupUtils.js +127 -0
  67. package/lib/cjs/signatures/Payload.d.ts +283 -0
  68. package/lib/cjs/signatures/Payload.js +570 -0
  69. package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
  70. package/lib/cjs/signatures/RatifierUtils.js +76 -0
  71. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
  72. package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
  73. package/lib/cjs/signatures/Tree.d.ts +241 -0
  74. package/lib/cjs/signatures/Tree.js +263 -0
  75. package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
  76. package/lib/cjs/signatures/TreeUtils.js +461 -0
  77. package/lib/cjs/signatures/index.d.ts +7 -0
  78. package/lib/cjs/signatures/index.js +23 -0
  79. package/lib/esm/abis.d.ts +3170 -0
  80. package/lib/esm/abis.js +4070 -0
  81. package/lib/esm/api/MidnightApi.d.ts +532 -0
  82. package/lib/esm/api/MidnightApi.js +709 -0
  83. package/lib/esm/api/helpers.d.ts +20 -0
  84. package/lib/esm/api/helpers.js +180 -0
  85. package/lib/esm/api/index.d.ts +2 -0
  86. package/lib/esm/api/index.js +2 -0
  87. package/lib/esm/api/types.d.ts +453 -0
  88. package/lib/esm/api/types.js +1 -0
  89. package/lib/esm/constants.d.ts +168 -0
  90. package/lib/esm/constants.js +184 -0
  91. package/lib/esm/errors.d.ts +253 -0
  92. package/lib/esm/errors.js +291 -0
  93. package/lib/esm/fetch/Market.d.ts +65 -0
  94. package/lib/esm/fetch/Market.js +113 -0
  95. package/lib/esm/fetch/Offer.d.ts +74 -0
  96. package/lib/esm/fetch/Offer.js +116 -0
  97. package/lib/esm/fetch/Position.d.ts +85 -0
  98. package/lib/esm/fetch/Position.js +144 -0
  99. package/lib/esm/fetch/Ratifier.d.ts +38 -0
  100. package/lib/esm/fetch/Ratifier.js +54 -0
  101. package/lib/esm/fetch/index.d.ts +5 -0
  102. package/lib/esm/fetch/index.js +5 -0
  103. package/lib/esm/fetch/types.d.ts +57 -0
  104. package/lib/esm/fetch/types.js +1 -0
  105. package/lib/esm/fetch/utils.d.ts +8 -0
  106. package/lib/esm/fetch/utils.js +12 -0
  107. package/lib/esm/index.d.ts +18 -0
  108. package/lib/esm/index.js +18 -0
  109. package/lib/esm/market/Market.d.ts +613 -0
  110. package/lib/esm/market/Market.js +498 -0
  111. package/lib/esm/market/MarketUtils.d.ts +189 -0
  112. package/lib/esm/market/MarketUtils.js +300 -0
  113. package/lib/esm/market/Position.d.ts +424 -0
  114. package/lib/esm/market/Position.js +420 -0
  115. package/lib/esm/market/PositionUtils.d.ts +115 -0
  116. package/lib/esm/market/PositionUtils.js +191 -0
  117. package/lib/esm/market/index.d.ts +4 -0
  118. package/lib/esm/market/index.js +4 -0
  119. package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
  120. package/lib/esm/math/ConsumableUnitsLib.js +74 -0
  121. package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
  122. package/lib/esm/math/TakeAmountsLib.js +196 -0
  123. package/lib/esm/math/TickLib.d.ts +200 -0
  124. package/lib/esm/math/TickLib.js +269 -0
  125. package/lib/esm/math/index.d.ts +3 -0
  126. package/lib/esm/math/index.js +3 -0
  127. package/lib/esm/offers/Offer.d.ts +534 -0
  128. package/lib/esm/offers/Offer.js +339 -0
  129. package/lib/esm/offers/OfferUtils.d.ts +565 -0
  130. package/lib/esm/offers/OfferUtils.js +656 -0
  131. package/lib/esm/offers/index.d.ts +2 -0
  132. package/lib/esm/offers/index.js +2 -0
  133. package/lib/esm/package.json +1 -0
  134. package/lib/esm/queries/GetPosition.d.ts +55 -0
  135. package/lib/esm/queries/GetPosition.js +71 -0
  136. package/lib/esm/queries/index.d.ts +1 -0
  137. package/lib/esm/queries/index.js +1 -0
  138. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
  139. package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
  140. package/lib/esm/signatures/Group.d.ts +178 -0
  141. package/lib/esm/signatures/Group.js +189 -0
  142. package/lib/esm/signatures/GroupUtils.d.ts +191 -0
  143. package/lib/esm/signatures/GroupUtils.js +124 -0
  144. package/lib/esm/signatures/Payload.d.ts +283 -0
  145. package/lib/esm/signatures/Payload.js +564 -0
  146. package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
  147. package/lib/esm/signatures/RatifierUtils.js +73 -0
  148. package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
  149. package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
  150. package/lib/esm/signatures/Tree.d.ts +241 -0
  151. package/lib/esm/signatures/Tree.js +259 -0
  152. package/lib/esm/signatures/TreeUtils.d.ts +573 -0
  153. package/lib/esm/signatures/TreeUtils.js +458 -0
  154. package/lib/esm/signatures/index.d.ts +7 -0
  155. package/lib/esm/signatures/index.js +7 -0
  156. package/package.json +69 -0
  157. package/src/index.ts +19 -0
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PositionUtils = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
6
+ /**
7
+ * Domain helpers for Midnight positions.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
12
+ *
13
+ * const accrued = PositionUtils.accrueInterest({
14
+ * position: {
15
+ * credit: 1_000n,
16
+ * pendingFee: 100n,
17
+ * lastLossFactor: 0n,
18
+ * lastAccrual: 1_000n,
19
+ * debt: 0n,
20
+ * collateralBitmap: 1n,
21
+ * collateral: [50n],
22
+ * },
23
+ * market: {
24
+ * chainId: 8453,
25
+ * params: {
26
+ * loanToken: "0x0000000000000000000000000000000000006000",
27
+ * collateralParams: [
28
+ * {
29
+ * token: "0x0000000000000000000000000000000000007000",
30
+ * lltv: 770000000000000000n,
31
+ * maxLif: 1061007957559681697n,
32
+ * oracle: "0x0000000000000000000000000000000000008000",
33
+ * },
34
+ * ],
35
+ * maturity: 54_000n,
36
+ * rcfThreshold: 0n,
37
+ * enterGate: "0x0000000000000000000000000000000000000000",
38
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
39
+ * },
40
+ * totalUnits: 1_000n,
41
+ * lossFactor: 0n,
42
+ * withdrawable: 500n,
43
+ * continuousFeeCredit: 0n,
44
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
45
+ * continuousFee: 10,
46
+ * tickSpacing: 4,
47
+ * },
48
+ * timestamp: 1_500n,
49
+ * });
50
+ * console.log(accrued.position.credit);
51
+ * ```
52
+ */
53
+ var PositionUtils;
54
+ (function (PositionUtils) {
55
+ /**
56
+ * Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
57
+ *
58
+ * @param params.position - Position state to accrue.
59
+ * @param params.market - Hydrated market state used for loss factor and continuous-fee accrual.
60
+ * @param params.timestamp - Timestamp at which to accrue.
61
+ * @returns New plain position and market objects with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
62
+ * @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
63
+ * @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
64
+ * @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
65
+ * @example
66
+ * ```ts
67
+ * import { PositionUtils } from "@morpho-org/midnight-sdk";
68
+ *
69
+ * const { position, market } = PositionUtils.accrueInterest({
70
+ * position: {
71
+ * credit: 1_000n,
72
+ * pendingFee: 100n,
73
+ * lastLossFactor: 0n,
74
+ * lastAccrual: 1_000n,
75
+ * debt: 0n,
76
+ * collateralBitmap: 1n,
77
+ * collateral: [50n],
78
+ * },
79
+ * market: {
80
+ * chainId: 8453,
81
+ * params: {
82
+ * loanToken: "0x0000000000000000000000000000000000006000",
83
+ * collateralParams: [
84
+ * {
85
+ * token: "0x0000000000000000000000000000000000007000",
86
+ * lltv: 770000000000000000n,
87
+ * maxLif: 1061007957559681697n,
88
+ * oracle: "0x0000000000000000000000000000000000008000",
89
+ * },
90
+ * ],
91
+ * maturity: 54_000n,
92
+ * rcfThreshold: 0n,
93
+ * enterGate: "0x0000000000000000000000000000000000000000",
94
+ * liquidatorGate: "0x0000000000000000000000000000000000000000",
95
+ * },
96
+ * totalUnits: 1_000n,
97
+ * lossFactor: 0n,
98
+ * withdrawable: 500n,
99
+ * continuousFeeCredit: 0n,
100
+ * settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
101
+ * continuousFee: 10,
102
+ * tickSpacing: 4,
103
+ * },
104
+ * timestamp: 1_500n,
105
+ * });
106
+ * console.log(position.lastAccrual, market.continuousFeeCredit);
107
+ * ```
108
+ */
109
+ function accrueInterest(params) {
110
+ const timestamp = BigInt(params.timestamp);
111
+ const credit = BigInt(params.position.credit);
112
+ const pendingFee = BigInt(params.position.pendingFee);
113
+ const lastLossFactor = BigInt(params.position.lastLossFactor);
114
+ const lastAccrual = BigInt(params.position.lastAccrual);
115
+ const marketLossFactor = BigInt(params.market.lossFactor);
116
+ if (timestamp < lastAccrual) {
117
+ throw new errors_js_1.InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
118
+ }
119
+ if (marketLossFactor < lastLossFactor) {
120
+ throw new errors_js_1.InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
121
+ }
122
+ if (pendingFee > credit) {
123
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
124
+ }
125
+ const postSlashCredit = lastLossFactor < morpho_ts_1.MathLib.MAX_UINT_128
126
+ ? morpho_ts_1.MathLib.mulDivDown(credit, morpho_ts_1.MathLib.MAX_UINT_128 - marketLossFactor, morpho_ts_1.MathLib.MAX_UINT_128 - lastLossFactor)
127
+ : 0n;
128
+ if (postSlashCredit > credit) {
129
+ throw new errors_js_1.InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
130
+ }
131
+ const creditDecrease = credit - postSlashCredit;
132
+ const pendingFeeDecrease = credit > 0n ? morpho_ts_1.MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
133
+ if (pendingFeeDecrease > pendingFee) {
134
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
135
+ }
136
+ const postSlashPendingFee = pendingFee - pendingFeeDecrease;
137
+ const maturity = BigInt(params.market.params.maturity);
138
+ const accrualEnd = timestamp < maturity ? timestamp : maturity;
139
+ const accruedFee = lastAccrual < maturity
140
+ ? morpho_ts_1.MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
141
+ : 0n;
142
+ if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
143
+ throw new errors_js_1.InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
144
+ }
145
+ const collateral = params.position.collateral.map((assets) => BigInt(assets));
146
+ const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
147
+ token: collateralParam.token,
148
+ lltv: BigInt(collateralParam.lltv),
149
+ maxLif: BigInt(collateralParam.maxLif),
150
+ oracle: collateralParam.oracle,
151
+ }));
152
+ const marketParams = {
153
+ loanToken: params.market.params.loanToken,
154
+ collateralParams,
155
+ maturity,
156
+ rcfThreshold: BigInt(params.market.params.rcfThreshold),
157
+ enterGate: params.market.params.enterGate,
158
+ liquidatorGate: params.market.params.liquidatorGate,
159
+ };
160
+ const settlementFeeCbps = [
161
+ params.market.settlementFeeCbps[0],
162
+ params.market.settlementFeeCbps[1],
163
+ params.market.settlementFeeCbps[2],
164
+ params.market.settlementFeeCbps[3],
165
+ params.market.settlementFeeCbps[4],
166
+ params.market.settlementFeeCbps[5],
167
+ params.market.settlementFeeCbps[6],
168
+ ];
169
+ return {
170
+ position: {
171
+ credit: postSlashCredit - accruedFee,
172
+ pendingFee: postSlashPendingFee - accruedFee,
173
+ lastLossFactor: marketLossFactor,
174
+ lastAccrual: timestamp,
175
+ debt: BigInt(params.position.debt),
176
+ collateralBitmap: BigInt(params.position.collateralBitmap),
177
+ collateral,
178
+ },
179
+ market: {
180
+ chainId: BigInt(params.market.chainId),
181
+ params: marketParams,
182
+ totalUnits: BigInt(params.market.totalUnits),
183
+ lossFactor: marketLossFactor,
184
+ withdrawable: BigInt(params.market.withdrawable),
185
+ continuousFeeCredit: BigInt(params.market.continuousFeeCredit) + accruedFee,
186
+ settlementFeeCbps,
187
+ continuousFee: params.market.continuousFee,
188
+ tickSpacing: params.market.tickSpacing,
189
+ },
190
+ accruedFee,
191
+ };
192
+ }
193
+ PositionUtils.accrueInterest = accrueInterest;
194
+ })(PositionUtils || (exports.PositionUtils = PositionUtils = {}));
@@ -0,0 +1,4 @@
1
+ export * from "./Market.js";
2
+ export * from "./MarketUtils.js";
3
+ export * from "./Position.js";
4
+ export * from "./PositionUtils.js";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Market.js"), exports);
18
+ __exportStar(require("./MarketUtils.js"), exports);
19
+ __exportStar(require("./Position.js"), exports);
20
+ __exportStar(require("./PositionUtils.js"), exports);
@@ -0,0 +1,57 @@
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
2
+ /**
3
+ * TypeScript port of Midnight `ConsumableUnitsLib`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
8
+ *
9
+ * console.log(typeof ConsumableUnitsLib.consumableUnits);
10
+ * ```
11
+ */
12
+ export declare namespace ConsumableUnitsLib {
13
+ /**
14
+ * Returns units that would fully consume an offer.
15
+ *
16
+ * @param params.offer.buy - Whether the maker buys loan assets.
17
+ * @param params.offer.tick - Offer tick used for asset-capped conversion.
18
+ * @param params.offer.maxUnits - Unit cap; when non-zero, units are capped directly.
19
+ * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
20
+ * @param params.consumed - Amount already consumed from the offer group.
21
+ * @param params.settlementFee - WAD-scaled settlement fee used for asset-capped conversion.
22
+ * @returns Remaining consumable units.
23
+ * @throws {NegativeValueError} when `consumed`, offer limits, delegated asset inputs, or the offer tick is negative.
24
+ * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
25
+ * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
26
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
27
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
28
+ * @example
29
+ * ```ts
30
+ * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
31
+ *
32
+ * const offer = {
33
+ * buy: true,
34
+ * tick: 5_820n,
35
+ * maxUnits: 0n,
36
+ * maxAssets: 1_000n,
37
+ * };
38
+ *
39
+ * const units = ConsumableUnitsLib.consumableUnits({
40
+ * offer,
41
+ * consumed: 0n,
42
+ * settlementFee: 0n,
43
+ * });
44
+ * console.log(units);
45
+ * ```
46
+ */
47
+ function consumableUnits(params: {
48
+ readonly offer: {
49
+ readonly buy: boolean;
50
+ readonly tick: BigIntish;
51
+ readonly maxUnits: BigIntish;
52
+ readonly maxAssets: BigIntish;
53
+ };
54
+ readonly consumed: BigIntish;
55
+ readonly settlementFee: BigIntish;
56
+ }): bigint;
57
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConsumableUnitsLib = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const index_js_1 = require("../offers/index.js");
6
+ const TakeAmountsLib_js_1 = require("./TakeAmountsLib.js");
7
+ /**
8
+ * TypeScript port of Midnight `ConsumableUnitsLib`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
13
+ *
14
+ * console.log(typeof ConsumableUnitsLib.consumableUnits);
15
+ * ```
16
+ */
17
+ var ConsumableUnitsLib;
18
+ (function (ConsumableUnitsLib) {
19
+ /**
20
+ * Returns units that would fully consume an offer.
21
+ *
22
+ * @param params.offer.buy - Whether the maker buys loan assets.
23
+ * @param params.offer.tick - Offer tick used for asset-capped conversion.
24
+ * @param params.offer.maxUnits - Unit cap; when non-zero, units are capped directly.
25
+ * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
26
+ * @param params.consumed - Amount already consumed from the offer group.
27
+ * @param params.settlementFee - WAD-scaled settlement fee used for asset-capped conversion.
28
+ * @returns Remaining consumable units.
29
+ * @throws {NegativeValueError} when `consumed`, offer limits, delegated asset inputs, or the offer tick is negative.
30
+ * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
31
+ * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
32
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
33
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
34
+ * @example
35
+ * ```ts
36
+ * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
37
+ *
38
+ * const offer = {
39
+ * buy: true,
40
+ * tick: 5_820n,
41
+ * maxUnits: 0n,
42
+ * maxAssets: 1_000n,
43
+ * };
44
+ *
45
+ * const units = ConsumableUnitsLib.consumableUnits({
46
+ * offer,
47
+ * consumed: 0n,
48
+ * settlementFee: 0n,
49
+ * });
50
+ * console.log(units);
51
+ * ```
52
+ */
53
+ function consumableUnits(params) {
54
+ const consumed = BigInt(params.consumed);
55
+ const maxUnits = BigInt(params.offer.maxUnits);
56
+ const maxAssets = BigInt(params.offer.maxAssets);
57
+ (0, morpho_ts_1.assertNonNegative)("consumed", consumed);
58
+ (0, morpho_ts_1.assertNonNegative)("offer.maxUnits", maxUnits);
59
+ (0, morpho_ts_1.assertNonNegative)("offer.maxAssets", maxAssets);
60
+ index_js_1.OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
61
+ if (maxUnits > 0n)
62
+ return morpho_ts_1.MathLib.zeroFloorSub(maxUnits, consumed);
63
+ const remainingAssets = morpho_ts_1.MathLib.zeroFloorSub(maxAssets, consumed);
64
+ return params.offer.buy
65
+ ? TakeAmountsLib_js_1.TakeAmountsLib.buyerAssetsToUnits({
66
+ offer: params.offer,
67
+ targetBuyerAssets: remainingAssets,
68
+ settlementFee: params.settlementFee,
69
+ })
70
+ : TakeAmountsLib_js_1.TakeAmountsLib.sellerAssetsToUnits({
71
+ offer: params.offer,
72
+ targetSellerAssets: remainingAssets,
73
+ settlementFee: params.settlementFee,
74
+ });
75
+ }
76
+ ConsumableUnitsLib.consumableUnits = consumableUnits;
77
+ })(ConsumableUnitsLib || (exports.ConsumableUnitsLib = ConsumableUnitsLib = {}));
@@ -0,0 +1,175 @@
1
+ import { type BigIntish, type RoundingDirection } from "@morpho-org/morpho-ts";
2
+ /**
3
+ * TypeScript port of Midnight `TakeAmountsLib`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
8
+ *
9
+ * console.log(typeof TakeAmountsLib.toUnits);
10
+ * ```
11
+ */
12
+ export declare namespace TakeAmountsLib {
13
+ /**
14
+ * Computes buyer and seller prices for an offer and settlement fee.
15
+ *
16
+ * @param params.offer.buy - Whether the maker buys loan assets.
17
+ * @param params.offer.tick - Offer tick used to compute the base price.
18
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
19
+ * @returns Buyer and seller prices.
20
+ * @throws {NegativeValueError} when `settlementFee` or the offer tick is negative.
21
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
22
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
23
+ * @example
24
+ * ```ts
25
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
26
+ *
27
+ * const offer = {
28
+ * buy: true,
29
+ * tick: 5_820n,
30
+ * };
31
+ *
32
+ * const prices = TakeAmountsLib.prices({ offer, settlementFee: 1_000000000000n });
33
+ * console.log(prices.buyerPrice);
34
+ * ```
35
+ */
36
+ function prices(params: {
37
+ readonly offer: {
38
+ readonly buy: boolean;
39
+ readonly tick: BigIntish;
40
+ };
41
+ readonly settlementFee: BigIntish;
42
+ }): {
43
+ readonly buyerPrice: bigint;
44
+ readonly sellerPrice: bigint;
45
+ };
46
+ /**
47
+ * Converts a target buyer-asset amount into units for an offer.
48
+ *
49
+ * `settlementFee` must be the fee for the offer market's current time to maturity.
50
+ *
51
+ * @param params.offer.buy - Whether the maker buys loan assets.
52
+ * @param params.offer.tick - Offer tick used to compute the buyer price.
53
+ * @param params.targetBuyerAssets - Target buyer-side asset amount.
54
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
55
+ * @returns Units that round-trip to the target buyer assets where reachable.
56
+ * @throws {NegativeValueError} when `targetBuyerAssets`, `settlementFee`, or the offer tick is negative.
57
+ * @throws {DivisionByZeroError} when the computed buyer price is zero.
58
+ * @throws {PriceGreaterThanOneError} when buyer price is above WAD.
59
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
60
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
61
+ * @example
62
+ * ```ts
63
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
64
+ *
65
+ * const offer = {
66
+ * buy: true,
67
+ * tick: 5_820n,
68
+ * };
69
+ *
70
+ * const units = TakeAmountsLib.buyerAssetsToUnits({
71
+ * offer,
72
+ * targetBuyerAssets: 100n,
73
+ * settlementFee: 0n,
74
+ * });
75
+ * console.log(units);
76
+ * ```
77
+ */
78
+ function buyerAssetsToUnits(params: {
79
+ readonly offer: {
80
+ readonly buy: boolean;
81
+ readonly tick: BigIntish;
82
+ };
83
+ readonly targetBuyerAssets: BigIntish;
84
+ readonly settlementFee: BigIntish;
85
+ }): bigint;
86
+ /**
87
+ * Converts a target seller-asset amount into units for an offer.
88
+ *
89
+ * `settlementFee` must be the fee for the offer market's current time to maturity.
90
+ *
91
+ * @param params.offer.buy - Whether the maker buys loan assets.
92
+ * @param params.offer.tick - Offer tick used to compute the seller price.
93
+ * @param params.targetSellerAssets - Target seller-side asset amount.
94
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
95
+ * @returns Units that round-trip to the target seller assets where reachable.
96
+ * @throws {NegativeValueError} when `targetSellerAssets`, `settlementFee`, or the offer tick is negative.
97
+ * @throws {DivisionByZeroError} when the computed seller price is zero.
98
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
99
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
100
+ * @example
101
+ * ```ts
102
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
103
+ *
104
+ * const offer = {
105
+ * buy: false,
106
+ * tick: 5_820n,
107
+ * };
108
+ *
109
+ * const units = TakeAmountsLib.sellerAssetsToUnits({
110
+ * offer,
111
+ * targetSellerAssets: 100n,
112
+ * settlementFee: 0n,
113
+ * });
114
+ * console.log(units);
115
+ * ```
116
+ */
117
+ function sellerAssetsToUnits(params: {
118
+ readonly offer: {
119
+ readonly buy: boolean;
120
+ readonly tick: BigIntish;
121
+ };
122
+ readonly targetSellerAssets: BigIntish;
123
+ readonly settlementFee: BigIntish;
124
+ }): bigint;
125
+ /**
126
+ * Converts assets to units at a WAD price.
127
+ *
128
+ * This is an SDK-only generic conversion convenience.
129
+ *
130
+ * @param params.assets - Asset amount to convert.
131
+ * @param params.price - WAD-scaled price used for the conversion.
132
+ * @param params.rounding - Rounding direction applied by the fixed-point conversion.
133
+ * @returns Units.
134
+ * @throws {NegativeValueError} when `assets` or `price` is negative.
135
+ * @throws {DivisionByZeroError} when price is zero.
136
+ * @example
137
+ * ```ts
138
+ * import { MathLib } from "@morpho-org/morpho-ts";
139
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
140
+ *
141
+ * const units = TakeAmountsLib.toUnits({ assets: 100n, price: MathLib.WAD, rounding: "Down" });
142
+ * console.log(units);
143
+ * ```
144
+ */
145
+ function toUnits(params: {
146
+ readonly assets: BigIntish;
147
+ readonly price: BigIntish;
148
+ readonly rounding: RoundingDirection;
149
+ }): bigint;
150
+ /**
151
+ * Converts assets to units at a tick price.
152
+ *
153
+ * This is an SDK-only tick-priced conversion convenience.
154
+ *
155
+ * @param params.assets - Asset amount to convert.
156
+ * @param params.tick - Offer tick whose price is used for the conversion.
157
+ * @param params.rounding - Rounding direction applied by the fixed-point conversion.
158
+ * @returns Units.
159
+ * @throws {NegativeValueError} when `assets` or `tick` is negative.
160
+ * @throws {DivisionByZeroError} when the tick price is zero.
161
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
162
+ * @example
163
+ * ```ts
164
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
165
+ *
166
+ * const units = TakeAmountsLib.toUnitsAtTick({ assets: 100n, tick: 6744n, rounding: "Down" });
167
+ * console.log(units);
168
+ * ```
169
+ */
170
+ function toUnitsAtTick(params: {
171
+ readonly assets: BigIntish;
172
+ readonly tick: BigIntish;
173
+ readonly rounding: RoundingDirection;
174
+ }): bigint;
175
+ }