@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,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TakeAmountsLib = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
6
+ const TickLib_js_1 = require("./TickLib.js");
7
+ /**
8
+ * TypeScript port of Midnight `TakeAmountsLib`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
13
+ *
14
+ * console.log(typeof TakeAmountsLib.toUnits);
15
+ * ```
16
+ */
17
+ var TakeAmountsLib;
18
+ (function (TakeAmountsLib) {
19
+ /**
20
+ * Computes buyer and seller prices for an offer and settlement fee.
21
+ *
22
+ * @param params.offer.buy - Whether the maker buys loan assets.
23
+ * @param params.offer.tick - Offer tick used to compute the base price.
24
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
25
+ * @returns Buyer and seller prices.
26
+ * @throws {NegativeValueError} when `settlementFee` or the offer tick is negative.
27
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
28
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
29
+ * @example
30
+ * ```ts
31
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
32
+ *
33
+ * const offer = {
34
+ * buy: true,
35
+ * tick: 5_820n,
36
+ * };
37
+ *
38
+ * const prices = TakeAmountsLib.prices({ offer, settlementFee: 1_000000000000n });
39
+ * console.log(prices.buyerPrice);
40
+ * ```
41
+ */
42
+ function prices(params) {
43
+ const settlementFee = BigInt(params.settlementFee);
44
+ (0, morpho_ts_1.assertNonNegative)("settlementFee", settlementFee);
45
+ const offerPrice = TickLib_js_1.TickLib.tickToPrice(params.offer.tick);
46
+ if (params.offer.buy && offerPrice < settlementFee) {
47
+ throw new errors_js_1.SettlementFeeExceedsPriceError(settlementFee, offerPrice);
48
+ }
49
+ const sellerPrice = params.offer.buy
50
+ ? offerPrice - settlementFee
51
+ : offerPrice;
52
+ const buyerPrice = sellerPrice + settlementFee;
53
+ return { buyerPrice, sellerPrice };
54
+ }
55
+ TakeAmountsLib.prices = prices;
56
+ /**
57
+ * Converts a target buyer-asset amount into units for an offer.
58
+ *
59
+ * `settlementFee` must be the fee for the offer market's current time to maturity.
60
+ *
61
+ * @param params.offer.buy - Whether the maker buys loan assets.
62
+ * @param params.offer.tick - Offer tick used to compute the buyer price.
63
+ * @param params.targetBuyerAssets - Target buyer-side asset amount.
64
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
65
+ * @returns Units that round-trip to the target buyer assets where reachable.
66
+ * @throws {NegativeValueError} when `targetBuyerAssets`, `settlementFee`, or the offer tick is negative.
67
+ * @throws {DivisionByZeroError} when the computed buyer price is zero.
68
+ * @throws {PriceGreaterThanOneError} when buyer price is above WAD.
69
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
70
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
71
+ * @example
72
+ * ```ts
73
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
74
+ *
75
+ * const offer = {
76
+ * buy: true,
77
+ * tick: 5_820n,
78
+ * };
79
+ *
80
+ * const units = TakeAmountsLib.buyerAssetsToUnits({
81
+ * offer,
82
+ * targetBuyerAssets: 100n,
83
+ * settlementFee: 0n,
84
+ * });
85
+ * console.log(units);
86
+ * ```
87
+ */
88
+ function buyerAssetsToUnits(params) {
89
+ const settlementFee = BigInt(params.settlementFee);
90
+ const targetBuyerAssets = BigInt(params.targetBuyerAssets);
91
+ (0, morpho_ts_1.assertNonNegative)("targetBuyerAssets", targetBuyerAssets);
92
+ const { buyerPrice } = prices({ offer: params.offer, settlementFee });
93
+ if (buyerPrice === 0n)
94
+ throw new morpho_ts_1.DivisionByZeroError("buyerPrice");
95
+ if (buyerPrice > morpho_ts_1.MathLib.WAD)
96
+ throw new errors_js_1.PriceGreaterThanOneError(buyerPrice);
97
+ return morpho_ts_1.MathLib.mulDiv(targetBuyerAssets, morpho_ts_1.MathLib.WAD, buyerPrice, params.offer.buy ? "Up" : "Down");
98
+ }
99
+ TakeAmountsLib.buyerAssetsToUnits = buyerAssetsToUnits;
100
+ /**
101
+ * Converts a target seller-asset amount into units for an offer.
102
+ *
103
+ * `settlementFee` must be the fee for the offer market's current time to maturity.
104
+ *
105
+ * @param params.offer.buy - Whether the maker buys loan assets.
106
+ * @param params.offer.tick - Offer tick used to compute the seller price.
107
+ * @param params.targetSellerAssets - Target seller-side asset amount.
108
+ * @param params.settlementFee - WAD-scaled settlement fee for the market and time to maturity.
109
+ * @returns Units that round-trip to the target seller assets where reachable.
110
+ * @throws {NegativeValueError} when `targetSellerAssets`, `settlementFee`, or the offer tick is negative.
111
+ * @throws {DivisionByZeroError} when the computed seller price is zero.
112
+ * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
113
+ * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
114
+ * @example
115
+ * ```ts
116
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
117
+ *
118
+ * const offer = {
119
+ * buy: false,
120
+ * tick: 5_820n,
121
+ * };
122
+ *
123
+ * const units = TakeAmountsLib.sellerAssetsToUnits({
124
+ * offer,
125
+ * targetSellerAssets: 100n,
126
+ * settlementFee: 0n,
127
+ * });
128
+ * console.log(units);
129
+ * ```
130
+ */
131
+ function sellerAssetsToUnits(params) {
132
+ const settlementFee = BigInt(params.settlementFee);
133
+ const targetSellerAssets = BigInt(params.targetSellerAssets);
134
+ (0, morpho_ts_1.assertNonNegative)("targetSellerAssets", targetSellerAssets);
135
+ const { sellerPrice } = prices({ offer: params.offer, settlementFee });
136
+ if (sellerPrice === 0n)
137
+ throw new morpho_ts_1.DivisionByZeroError("sellerPrice");
138
+ return morpho_ts_1.MathLib.mulDiv(targetSellerAssets, morpho_ts_1.MathLib.WAD, sellerPrice, params.offer.buy ? "Up" : "Down");
139
+ }
140
+ TakeAmountsLib.sellerAssetsToUnits = sellerAssetsToUnits;
141
+ /**
142
+ * Converts assets to units at a WAD price.
143
+ *
144
+ * This is an SDK-only generic conversion convenience.
145
+ *
146
+ * @param params.assets - Asset amount to convert.
147
+ * @param params.price - WAD-scaled price used for the conversion.
148
+ * @param params.rounding - Rounding direction applied by the fixed-point conversion.
149
+ * @returns Units.
150
+ * @throws {NegativeValueError} when `assets` or `price` is negative.
151
+ * @throws {DivisionByZeroError} when price is zero.
152
+ * @example
153
+ * ```ts
154
+ * import { MathLib } from "@morpho-org/morpho-ts";
155
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
156
+ *
157
+ * const units = TakeAmountsLib.toUnits({ assets: 100n, price: MathLib.WAD, rounding: "Down" });
158
+ * console.log(units);
159
+ * ```
160
+ */
161
+ function toUnits(params) {
162
+ const assets = BigInt(params.assets);
163
+ const price = BigInt(params.price);
164
+ (0, morpho_ts_1.assertNonNegative)("assets", assets);
165
+ (0, morpho_ts_1.assertNonNegative)("price", price);
166
+ if (price === 0n)
167
+ throw new morpho_ts_1.DivisionByZeroError("price");
168
+ return morpho_ts_1.MathLib.mulDiv(assets, morpho_ts_1.MathLib.WAD, price, params.rounding);
169
+ }
170
+ TakeAmountsLib.toUnits = toUnits;
171
+ /**
172
+ * Converts assets to units at a tick price.
173
+ *
174
+ * This is an SDK-only tick-priced conversion convenience.
175
+ *
176
+ * @param params.assets - Asset amount to convert.
177
+ * @param params.tick - Offer tick whose price is used for the conversion.
178
+ * @param params.rounding - Rounding direction applied by the fixed-point conversion.
179
+ * @returns Units.
180
+ * @throws {NegativeValueError} when `assets` or `tick` is negative.
181
+ * @throws {DivisionByZeroError} when the tick price is zero.
182
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
183
+ * @example
184
+ * ```ts
185
+ * import { TakeAmountsLib } from "@morpho-org/midnight-sdk";
186
+ *
187
+ * const units = TakeAmountsLib.toUnitsAtTick({ assets: 100n, tick: 6744n, rounding: "Down" });
188
+ * console.log(units);
189
+ * ```
190
+ */
191
+ function toUnitsAtTick(params) {
192
+ return toUnits({
193
+ assets: params.assets,
194
+ price: TickLib_js_1.TickLib.tickToPrice(params.tick),
195
+ rounding: params.rounding,
196
+ });
197
+ }
198
+ TakeAmountsLib.toUnitsAtTick = toUnitsAtTick;
199
+ })(TakeAmountsLib || (exports.TakeAmountsLib = TakeAmountsLib = {}));
@@ -0,0 +1,200 @@
1
+ import { type BigIntish } from "@morpho-org/morpho-ts";
2
+ /**
3
+ * TypeScript port of Midnight `TickLib`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { TickLib } from "@morpho-org/midnight-sdk";
8
+ *
9
+ * console.log(TickLib.tickToPrice(6744n));
10
+ * ```
11
+ */
12
+ export declare namespace TickLib {
13
+ /**
14
+ * WAD-scaled natural log of one plus Midnight's tick delta.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { TickLib } from "@morpho-org/midnight-sdk";
19
+ *
20
+ * console.log(TickLib.LN_ONE_PLUS_DELTA);
21
+ * ```
22
+ */
23
+ const LN_ONE_PLUS_DELTA = 4987541511039073n;
24
+ /**
25
+ * WAD-scaled natural log of two.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { TickLib } from "@morpho-org/midnight-sdk";
30
+ *
31
+ * console.log(TickLib.LN_2);
32
+ * ```
33
+ */
34
+ const LN_2 = 693147180559945309n;
35
+ /**
36
+ * Exponent offset used by the Midnight exponential approximation.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { TickLib } from "@morpho-org/midnight-sdk";
41
+ *
42
+ * console.log(TickLib.EXP_OFFSET);
43
+ * ```
44
+ */
45
+ const EXP_OFFSET = 322611214989459870n;
46
+ /**
47
+ * Divides with half-down rounding without validating the denominator.
48
+ *
49
+ * @param x - Dividend.
50
+ * @param d - Divisor.
51
+ * @returns Quotient rounded half down.
52
+ * @example
53
+ * ```ts
54
+ * import { TickLib } from "@morpho-org/midnight-sdk";
55
+ *
56
+ * const value = TickLib.divHalfDownUnchecked(5n, 2n);
57
+ * console.log(value);
58
+ * ```
59
+ */
60
+ const divHalfDownUnchecked: (x: bigint, d: bigint) => bigint;
61
+ /**
62
+ * Asserts that a tick is non-negative and within Midnight's deployed range.
63
+ *
64
+ * @param tick - Tick to validate.
65
+ * @returns Tick as a bigint.
66
+ * @throws {NegativeValueError} when `tick` is negative.
67
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
68
+ * @example
69
+ * ```ts
70
+ * import { TickLib } from "@morpho-org/midnight-sdk";
71
+ *
72
+ * const tick = TickLib.assertTickInRange(100n);
73
+ * console.log(tick);
74
+ * ```
75
+ */
76
+ const assertTickInRange: (tick: BigIntish) => bigint;
77
+ /**
78
+ * Computes the WAD-scaled exponential approximation used by Midnight ticks.
79
+ *
80
+ * @param x - WAD-scaled exponent.
81
+ * @returns WAD-scaled exponential value.
82
+ * @example
83
+ * ```ts
84
+ * import { TickLib } from "@morpho-org/midnight-sdk";
85
+ *
86
+ * const value = TickLib.wExp(0n);
87
+ * console.log(value);
88
+ * ```
89
+ */
90
+ const wExp: (x: bigint) => bigint;
91
+ /**
92
+ * Converts a Midnight tick into a WAD price.
93
+ *
94
+ * @param tick - Tick in the deployed range.
95
+ * @returns WAD price rounded to `PRICE_ROUNDING_STEP`.
96
+ * @throws {NegativeValueError} when `tick` is negative.
97
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
98
+ * @example
99
+ * ```ts
100
+ * import { TickLib } from "@morpho-org/midnight-sdk";
101
+ *
102
+ * const price = TickLib.tickToPrice(6744n);
103
+ * console.log(price);
104
+ * ```
105
+ */
106
+ function tickToPrice(tick: BigIntish): bigint;
107
+ /**
108
+ * Converts a WAD price into the lowest spacing-aligned tick whose price is at least the input.
109
+ *
110
+ * @param price - WAD price.
111
+ * @param spacing - Tick spacing; defaults to `DEFAULT_TICK_SPACING`.
112
+ * @returns Lowest aligned tick with price greater than or equal to `price`.
113
+ * @throws {NegativeValueError} when `price` is negative.
114
+ * @throws {PriceGreaterThanOneError} when price is above WAD.
115
+ * @throws {InvalidTickSpacingError} when spacing is invalid.
116
+ * @example
117
+ * ```ts
118
+ * import { TickLib } from "@morpho-org/midnight-sdk";
119
+ *
120
+ * const tick = TickLib.priceToTick(500000000000000000n);
121
+ * console.log(tick);
122
+ * ```
123
+ */
124
+ function priceToTick(price: BigIntish, spacing?: BigIntish): bigint;
125
+ /**
126
+ * Snaps a WAD price to the price of the lowest spacing-aligned tick at or above it.
127
+ *
128
+ * This is an SDK-only convenience around `priceToTick` and `tickToPrice`.
129
+ *
130
+ * @param price - WAD price.
131
+ * @param spacing - Tick spacing.
132
+ * @returns Snapped WAD price.
133
+ * @throws {NegativeValueError} when `price` is negative.
134
+ * @throws {PriceGreaterThanOneError} when price is above WAD.
135
+ * @throws {InvalidTickSpacingError} when spacing is invalid.
136
+ * @example
137
+ * ```ts
138
+ * import { TickLib } from "@morpho-org/midnight-sdk";
139
+ *
140
+ * const price = TickLib.snapPriceToTick(500000000000000000n, 4n);
141
+ * console.log(price);
142
+ * ```
143
+ */
144
+ function snapPriceToTick(price: BigIntish, spacing?: BigIntish): bigint;
145
+ /**
146
+ * Converts a WAD fixed rate into a WAD zero-coupon price.
147
+ *
148
+ * This is an SDK-only rate conversion convenience.
149
+ *
150
+ * @param rate - WAD fixed rate.
151
+ * @returns WAD price rounded down.
152
+ * @throws {NegativeValueError} when `rate` is negative.
153
+ * @example
154
+ * ```ts
155
+ * import { TickLib } from "@morpho-org/midnight-sdk";
156
+ *
157
+ * const price = TickLib.rateToPrice(50000000000000000n);
158
+ * console.log(price);
159
+ * ```
160
+ */
161
+ function rateToPrice(rate: BigIntish): bigint;
162
+ /**
163
+ * Converts a Midnight tick into a WAD fixed rate.
164
+ *
165
+ * This is an SDK-only rate conversion convenience.
166
+ *
167
+ * @param tick - Midnight tick.
168
+ * @returns WAD fixed rate rounded up.
169
+ * @throws {NegativeValueError} when `tick` is negative.
170
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
171
+ * @throws {DivisionByZeroError} when the tick price is zero.
172
+ * @example
173
+ * ```ts
174
+ * import { TickLib } from "@morpho-org/midnight-sdk";
175
+ *
176
+ * const rate = TickLib.tickToRate(6744n);
177
+ * console.log(rate);
178
+ * ```
179
+ */
180
+ function tickToRate(tick: BigIntish): bigint;
181
+ /**
182
+ * Asserts that a tick is aligned to the provided spacing.
183
+ *
184
+ * This is an SDK-only tick-domain assertion.
185
+ *
186
+ * @param tick - Tick to validate.
187
+ * @param spacing - Market tick spacing.
188
+ * @returns Tick as a bigint.
189
+ * @throws {NegativeValueError} when `tick` is negative.
190
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
191
+ * @throws {InvalidTickSpacingError} when spacing is invalid or the tick is not aligned.
192
+ * @example
193
+ * ```ts
194
+ * import { TickLib } from "@morpho-org/midnight-sdk";
195
+ *
196
+ * TickLib.assertTickAlignedToSpacing(100n, 4n);
197
+ * ```
198
+ */
199
+ function assertTickAlignedToSpacing(tick: BigIntish, spacing?: BigIntish): bigint;
200
+ }
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TickLib = void 0;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const constants_js_1 = require("../constants.js");
6
+ const errors_js_1 = require("../errors.js");
7
+ /**
8
+ * TypeScript port of Midnight `TickLib`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { TickLib } from "@morpho-org/midnight-sdk";
13
+ *
14
+ * console.log(TickLib.tickToPrice(6744n));
15
+ * ```
16
+ */
17
+ var TickLib;
18
+ (function (TickLib) {
19
+ /**
20
+ * WAD-scaled natural log of one plus Midnight's tick delta.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { TickLib } from "@morpho-org/midnight-sdk";
25
+ *
26
+ * console.log(TickLib.LN_ONE_PLUS_DELTA);
27
+ * ```
28
+ */
29
+ TickLib.LN_ONE_PLUS_DELTA = 4987541511039073n;
30
+ /**
31
+ * WAD-scaled natural log of two.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { TickLib } from "@morpho-org/midnight-sdk";
36
+ *
37
+ * console.log(TickLib.LN_2);
38
+ * ```
39
+ */
40
+ TickLib.LN_2 = 693147180559945309n;
41
+ /**
42
+ * Exponent offset used by the Midnight exponential approximation.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { TickLib } from "@morpho-org/midnight-sdk";
47
+ *
48
+ * console.log(TickLib.EXP_OFFSET);
49
+ * ```
50
+ */
51
+ TickLib.EXP_OFFSET = 322611214989459870n;
52
+ /**
53
+ * Divides with half-down rounding without validating the denominator.
54
+ *
55
+ * @param x - Dividend.
56
+ * @param d - Divisor.
57
+ * @returns Quotient rounded half down.
58
+ * @example
59
+ * ```ts
60
+ * import { TickLib } from "@morpho-org/midnight-sdk";
61
+ *
62
+ * const value = TickLib.divHalfDownUnchecked(5n, 2n);
63
+ * console.log(value);
64
+ * ```
65
+ */
66
+ TickLib.divHalfDownUnchecked = (x, d) => (x + (d - 1n) / 2n) / d;
67
+ /**
68
+ * Asserts that a tick is non-negative and within Midnight's deployed range.
69
+ *
70
+ * @param tick - Tick to validate.
71
+ * @returns Tick as a bigint.
72
+ * @throws {NegativeValueError} when `tick` is negative.
73
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
74
+ * @example
75
+ * ```ts
76
+ * import { TickLib } from "@morpho-org/midnight-sdk";
77
+ *
78
+ * const tick = TickLib.assertTickInRange(100n);
79
+ * console.log(tick);
80
+ * ```
81
+ */
82
+ TickLib.assertTickInRange = (tick) => {
83
+ const tickValue = BigInt(tick);
84
+ (0, morpho_ts_1.assertNonNegative)("tick", tickValue);
85
+ if (tickValue > constants_js_1.MAX_TICK)
86
+ throw new errors_js_1.TickOutOfRangeError(tickValue, constants_js_1.MAX_TICK);
87
+ return tickValue;
88
+ };
89
+ /**
90
+ * Computes the WAD-scaled exponential approximation used by Midnight ticks.
91
+ *
92
+ * @param x - WAD-scaled exponent.
93
+ * @returns WAD-scaled exponential value.
94
+ * @example
95
+ * ```ts
96
+ * import { TickLib } from "@morpho-org/midnight-sdk";
97
+ *
98
+ * const value = TickLib.wExp(0n);
99
+ * console.log(value);
100
+ * ```
101
+ */
102
+ TickLib.wExp = (x) => {
103
+ if (x < 0n)
104
+ return 1000000000000000000000000000000000000n / TickLib.wExp(-x);
105
+ const q = (x + TickLib.EXP_OFFSET) / TickLib.LN_2;
106
+ const r = x - q * TickLib.LN_2;
107
+ const secondTerm = (r * r) / (2n * morpho_ts_1.MathLib.WAD);
108
+ const thirdTerm = (secondTerm * r) / (3n * morpho_ts_1.MathLib.WAD);
109
+ const expR = morpho_ts_1.MathLib.WAD + r + secondTerm + thirdTerm;
110
+ return expR << q;
111
+ };
112
+ /**
113
+ * Converts a Midnight tick into a WAD price.
114
+ *
115
+ * @param tick - Tick in the deployed range.
116
+ * @returns WAD price rounded to `PRICE_ROUNDING_STEP`.
117
+ * @throws {NegativeValueError} when `tick` is negative.
118
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
119
+ * @example
120
+ * ```ts
121
+ * import { TickLib } from "@morpho-org/midnight-sdk";
122
+ *
123
+ * const price = TickLib.tickToPrice(6744n);
124
+ * console.log(price);
125
+ * ```
126
+ */
127
+ function tickToPrice(tick) {
128
+ const tickValue = TickLib.assertTickInRange(tick);
129
+ const exponent = TickLib.LN_ONE_PLUS_DELTA * (constants_js_1.MAX_TICK / 2n - tickValue);
130
+ const rawPrice = TickLib.divHalfDownUnchecked(1000000000000000000000000000000000000n, morpho_ts_1.MathLib.WAD + TickLib.wExp(exponent));
131
+ return (TickLib.divHalfDownUnchecked(rawPrice, constants_js_1.PRICE_ROUNDING_STEP) * constants_js_1.PRICE_ROUNDING_STEP);
132
+ }
133
+ TickLib.tickToPrice = tickToPrice;
134
+ /**
135
+ * Converts a WAD price into the lowest spacing-aligned tick whose price is at least the input.
136
+ *
137
+ * @param price - WAD price.
138
+ * @param spacing - Tick spacing; defaults to `DEFAULT_TICK_SPACING`.
139
+ * @returns Lowest aligned tick with price greater than or equal to `price`.
140
+ * @throws {NegativeValueError} when `price` is negative.
141
+ * @throws {PriceGreaterThanOneError} when price is above WAD.
142
+ * @throws {InvalidTickSpacingError} when spacing is invalid.
143
+ * @example
144
+ * ```ts
145
+ * import { TickLib } from "@morpho-org/midnight-sdk";
146
+ *
147
+ * const tick = TickLib.priceToTick(500000000000000000n);
148
+ * console.log(tick);
149
+ * ```
150
+ */
151
+ function priceToTick(price, spacing = constants_js_1.DEFAULT_TICK_SPACING) {
152
+ const priceValue = BigInt(price);
153
+ const spacingValue = BigInt(spacing);
154
+ (0, morpho_ts_1.assertNonNegative)("price", priceValue);
155
+ if (priceValue > morpho_ts_1.MathLib.WAD)
156
+ throw new errors_js_1.PriceGreaterThanOneError(priceValue);
157
+ if (spacingValue <= 0n || constants_js_1.MAX_TICK % spacingValue !== 0n) {
158
+ throw new errors_js_1.InvalidTickSpacingError(spacingValue);
159
+ }
160
+ let low = 0n;
161
+ let high = constants_js_1.MAX_TICK;
162
+ while (low !== high) {
163
+ const mid = (low + high) / 2n;
164
+ if (tickToPrice(mid) < priceValue)
165
+ low = mid + 1n;
166
+ else
167
+ high = mid;
168
+ }
169
+ return ((low + spacingValue - 1n) / spacingValue) * spacingValue;
170
+ }
171
+ TickLib.priceToTick = priceToTick;
172
+ /**
173
+ * Snaps a WAD price to the price of the lowest spacing-aligned tick at or above it.
174
+ *
175
+ * This is an SDK-only convenience around `priceToTick` and `tickToPrice`.
176
+ *
177
+ * @param price - WAD price.
178
+ * @param spacing - Tick spacing.
179
+ * @returns Snapped WAD price.
180
+ * @throws {NegativeValueError} when `price` is negative.
181
+ * @throws {PriceGreaterThanOneError} when price is above WAD.
182
+ * @throws {InvalidTickSpacingError} when spacing is invalid.
183
+ * @example
184
+ * ```ts
185
+ * import { TickLib } from "@morpho-org/midnight-sdk";
186
+ *
187
+ * const price = TickLib.snapPriceToTick(500000000000000000n, 4n);
188
+ * console.log(price);
189
+ * ```
190
+ */
191
+ function snapPriceToTick(price, spacing = constants_js_1.DEFAULT_TICK_SPACING) {
192
+ return tickToPrice(priceToTick(price, spacing));
193
+ }
194
+ TickLib.snapPriceToTick = snapPriceToTick;
195
+ /**
196
+ * Converts a WAD fixed rate into a WAD zero-coupon price.
197
+ *
198
+ * This is an SDK-only rate conversion convenience.
199
+ *
200
+ * @param rate - WAD fixed rate.
201
+ * @returns WAD price rounded down.
202
+ * @throws {NegativeValueError} when `rate` is negative.
203
+ * @example
204
+ * ```ts
205
+ * import { TickLib } from "@morpho-org/midnight-sdk";
206
+ *
207
+ * const price = TickLib.rateToPrice(50000000000000000n);
208
+ * console.log(price);
209
+ * ```
210
+ */
211
+ function rateToPrice(rate) {
212
+ const rateValue = BigInt(rate);
213
+ (0, morpho_ts_1.assertNonNegative)("rate", rateValue);
214
+ return morpho_ts_1.MathLib.mulDiv(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD + rateValue, "Down");
215
+ }
216
+ TickLib.rateToPrice = rateToPrice;
217
+ /**
218
+ * Converts a Midnight tick into a WAD fixed rate.
219
+ *
220
+ * This is an SDK-only rate conversion convenience.
221
+ *
222
+ * @param tick - Midnight tick.
223
+ * @returns WAD fixed rate rounded up.
224
+ * @throws {NegativeValueError} when `tick` is negative.
225
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
226
+ * @throws {DivisionByZeroError} when the tick price is zero.
227
+ * @example
228
+ * ```ts
229
+ * import { TickLib } from "@morpho-org/midnight-sdk";
230
+ *
231
+ * const rate = TickLib.tickToRate(6744n);
232
+ * console.log(rate);
233
+ * ```
234
+ */
235
+ function tickToRate(tick) {
236
+ const price = tickToPrice(tick);
237
+ if (price === 0n)
238
+ throw new morpho_ts_1.DivisionByZeroError("price");
239
+ return morpho_ts_1.MathLib.mulDiv(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, price, "Up") - morpho_ts_1.MathLib.WAD;
240
+ }
241
+ TickLib.tickToRate = tickToRate;
242
+ /**
243
+ * Asserts that a tick is aligned to the provided spacing.
244
+ *
245
+ * This is an SDK-only tick-domain assertion.
246
+ *
247
+ * @param tick - Tick to validate.
248
+ * @param spacing - Market tick spacing.
249
+ * @returns Tick as a bigint.
250
+ * @throws {NegativeValueError} when `tick` is negative.
251
+ * @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
252
+ * @throws {InvalidTickSpacingError} when spacing is invalid or the tick is not aligned.
253
+ * @example
254
+ * ```ts
255
+ * import { TickLib } from "@morpho-org/midnight-sdk";
256
+ *
257
+ * TickLib.assertTickAlignedToSpacing(100n, 4n);
258
+ * ```
259
+ */
260
+ function assertTickAlignedToSpacing(tick, spacing = constants_js_1.DEFAULT_TICK_SPACING) {
261
+ const tickValue = TickLib.assertTickInRange(tick);
262
+ const spacingValue = BigInt(spacing);
263
+ if (spacingValue <= 0n || constants_js_1.MAX_TICK % spacingValue !== 0n) {
264
+ throw new errors_js_1.InvalidTickSpacingError(spacingValue);
265
+ }
266
+ if (tickValue % spacingValue !== 0n) {
267
+ throw new errors_js_1.InvalidTickSpacingError(tickValue, spacingValue);
268
+ }
269
+ return tickValue;
270
+ }
271
+ TickLib.assertTickAlignedToSpacing = assertTickAlignedToSpacing;
272
+ })(TickLib || (exports.TickLib = TickLib = {}));
@@ -0,0 +1,3 @@
1
+ export * from "./ConsumableUnitsLib.js";
2
+ export * from "./TakeAmountsLib.js";
3
+ export * from "./TickLib.js";
@@ -0,0 +1,19 @@
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("./ConsumableUnitsLib.js"), exports);
18
+ __exportStar(require("./TakeAmountsLib.js"), exports);
19
+ __exportStar(require("./TickLib.js"), exports);