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