@morpho-org/midnight-sdk 0.1.0 → 1.1.1

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 (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +89 -19
  7. package/lib/cjs/api/helpers.d.ts +11 -1
  8. package/lib/cjs/api/helpers.js +54 -4
  9. package/lib/cjs/api/types.d.ts +35 -9
  10. package/lib/cjs/constants.d.ts +12 -11
  11. package/lib/cjs/constants.js +14 -12
  12. package/lib/cjs/errors.d.ts +89 -1
  13. package/lib/cjs/errors.js +114 -3
  14. package/lib/cjs/fetch/Market.js +37 -3
  15. package/lib/cjs/fetch/index.d.ts +0 -1
  16. package/lib/cjs/fetch/index.js +0 -1
  17. package/lib/cjs/fetch/utils.d.ts +1 -1
  18. package/lib/cjs/fetch/utils.js +9 -2
  19. package/lib/cjs/index.d.ts +1 -1
  20. package/lib/cjs/index.js +2 -23
  21. package/lib/cjs/market/Market.d.ts +99 -33
  22. package/lib/cjs/market/Market.js +137 -28
  23. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  24. package/lib/cjs/market/MarketUtils.js +108 -48
  25. package/lib/cjs/market/Position.d.ts +16 -11
  26. package/lib/cjs/market/Position.js +18 -12
  27. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  28. package/lib/cjs/market/PositionUtils.js +9 -6
  29. package/lib/cjs/math/TickLib.d.ts +22 -0
  30. package/lib/cjs/math/TickLib.js +30 -0
  31. package/lib/cjs/math/index.d.ts +0 -1
  32. package/lib/cjs/math/index.js +0 -1
  33. package/lib/cjs/offers/Offer.d.ts +218 -12
  34. package/lib/cjs/offers/Offer.js +215 -7
  35. package/lib/cjs/offers/OfferUtils.d.ts +249 -143
  36. package/lib/cjs/offers/OfferUtils.js +326 -45
  37. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  38. package/lib/cjs/queries/GetPosition.js +1 -1
  39. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  40. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
  41. package/lib/cjs/signatures/Group.d.ts +12 -4
  42. package/lib/cjs/signatures/Group.js +17 -7
  43. package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
  44. package/lib/cjs/signatures/GroupUtils.js +57 -2
  45. package/lib/cjs/signatures/Payload.d.ts +277 -277
  46. package/lib/cjs/signatures/Payload.js +517 -534
  47. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  48. package/lib/cjs/signatures/RatifierUtils.js +105 -0
  49. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
  50. package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
  51. package/lib/cjs/signatures/Tree.d.ts +28 -16
  52. package/lib/cjs/signatures/Tree.js +35 -15
  53. package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
  54. package/lib/cjs/signatures/TreeUtils.js +89 -79
  55. package/lib/cjs/signatures/eip712.d.ts +22 -0
  56. package/lib/cjs/signatures/eip712.js +26 -0
  57. package/lib/cjs/signatures/index.d.ts +2 -0
  58. package/lib/cjs/signatures/index.js +4 -0
  59. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  60. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  61. package/lib/esm/abis.d.ts +1185 -996
  62. package/lib/esm/abis.js +1445 -1211
  63. package/lib/esm/api/MidnightApi.d.ts +22 -16
  64. package/lib/esm/api/MidnightApi.js +91 -21
  65. package/lib/esm/api/helpers.d.ts +11 -1
  66. package/lib/esm/api/helpers.js +54 -5
  67. package/lib/esm/api/types.d.ts +35 -9
  68. package/lib/esm/constants.d.ts +12 -11
  69. package/lib/esm/constants.js +13 -11
  70. package/lib/esm/errors.d.ts +89 -1
  71. package/lib/esm/errors.js +108 -2
  72. package/lib/esm/fetch/Market.js +38 -4
  73. package/lib/esm/fetch/index.d.ts +0 -1
  74. package/lib/esm/fetch/index.js +0 -1
  75. package/lib/esm/fetch/utils.d.ts +1 -1
  76. package/lib/esm/fetch/utils.js +9 -2
  77. package/lib/esm/index.d.ts +1 -1
  78. package/lib/esm/index.js +1 -1
  79. package/lib/esm/market/Market.d.ts +99 -33
  80. package/lib/esm/market/Market.js +137 -28
  81. package/lib/esm/market/MarketUtils.d.ts +61 -41
  82. package/lib/esm/market/MarketUtils.js +109 -49
  83. package/lib/esm/market/Position.d.ts +16 -11
  84. package/lib/esm/market/Position.js +19 -13
  85. package/lib/esm/market/PositionUtils.d.ts +6 -4
  86. package/lib/esm/market/PositionUtils.js +9 -6
  87. package/lib/esm/math/TickLib.d.ts +22 -0
  88. package/lib/esm/math/TickLib.js +31 -1
  89. package/lib/esm/math/index.d.ts +0 -1
  90. package/lib/esm/math/index.js +0 -1
  91. package/lib/esm/offers/Offer.d.ts +218 -12
  92. package/lib/esm/offers/Offer.js +216 -8
  93. package/lib/esm/offers/OfferUtils.d.ts +249 -143
  94. package/lib/esm/offers/OfferUtils.js +329 -48
  95. package/lib/esm/queries/GetPosition.d.ts +1 -1
  96. package/lib/esm/queries/GetPosition.js +1 -1
  97. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  98. package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
  99. package/lib/esm/signatures/Group.d.ts +12 -4
  100. package/lib/esm/signatures/Group.js +17 -7
  101. package/lib/esm/signatures/GroupUtils.d.ts +60 -4
  102. package/lib/esm/signatures/GroupUtils.js +57 -2
  103. package/lib/esm/signatures/Payload.d.ts +277 -277
  104. package/lib/esm/signatures/Payload.js +517 -531
  105. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  106. package/lib/esm/signatures/RatifierUtils.js +105 -0
  107. package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
  108. package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
  109. package/lib/esm/signatures/Tree.d.ts +28 -16
  110. package/lib/esm/signatures/Tree.js +36 -16
  111. package/lib/esm/signatures/TreeUtils.d.ts +185 -63
  112. package/lib/esm/signatures/TreeUtils.js +91 -81
  113. package/lib/esm/signatures/eip712.d.ts +22 -0
  114. package/lib/esm/signatures/eip712.js +22 -0
  115. package/lib/esm/signatures/index.d.ts +2 -0
  116. package/lib/esm/signatures/index.js +2 -0
  117. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  118. package/lib/esm/signatures/offerStructInternal.js +67 -0
  119. package/package.json +31 -34
  120. package/lib/cjs/fetch/Offer.d.ts +0 -74
  121. package/lib/cjs/fetch/Offer.js +0 -119
  122. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  123. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  124. package/lib/esm/fetch/Offer.d.ts +0 -74
  125. package/lib/esm/fetch/Offer.js +0 -116
  126. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  127. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
  128. package/src/index.ts +0 -19
@@ -1,119 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchConsumableUnits = fetchConsumableUnits;
4
- const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
- const actions_1 = require("viem/actions");
6
- const abis_js_1 = require("../abis.js");
7
- const index_js_1 = require("../math/index.js");
8
- const index_js_2 = require("../offers/index.js");
9
- const utils_js_1 = require("./utils.js");
10
- /**
11
- * Fetches and computes remaining consumable units for an offer.
12
- *
13
- * Unit-capped offers skip settlement-fee lookup, matching the Solidity
14
- * library's early return. For asset-capped offers, pass the time to maturity
15
- * for the same block context as the quote.
16
- *
17
- * Reads `eth_chainId` only when the viem client has no configured chain id,
18
- * then reads `Midnight.consumed(offer.maker, group)` and
19
- * `Midnight.continuousFee(marketId)`. If the offer's continuous fee cap is
20
- * below the market's current continuous fee, returns zero. For asset-capped
21
- * offers where `offer.maxUnits` is zero, also reads
22
- * `Midnight.settlementFee(marketId, timeToMaturity)`.
23
- *
24
- * @param client - Viem client used for the reads.
25
- * @param params.marketId - Market id used for settlement-fee lookup on asset-capped offers.
26
- * @param params.offer.buy - Whether the maker buys loan assets.
27
- * @param params.offer.maker - Maker address whose consumed group amount is read.
28
- * @param params.offer.tick - Offer tick used for asset-to-unit conversion.
29
- * @param params.offer.maxUnits - Unit cap; when non-zero, settlement-fee lookup is skipped.
30
- * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
31
- * @param params.offer.continuousFeeCap - Maximum market continuous fee accepted by this offer.
32
- * @param params.group - Offer group id whose consumed amount is read.
33
- * @param params.timeToMaturity - Time to maturity used for settlement-fee lookup on asset-capped offers.
34
- * @param params.account - Optional account used as the `from` field for the reads.
35
- * @param params.blockNumber - Optional block number used for the reads.
36
- * @param params.blockTag - Optional block tag used for the reads.
37
- * @param params.stateOverride - Optional state override set used for the reads.
38
- * @returns Consumable units.
39
- * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
40
- * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
41
- * @throws {NegativeValueError} when asset-capped `timeToMaturity`, offer caps, or SDK math inputs are negative.
42
- * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
43
- * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
44
- * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
45
- * @example
46
- * ```ts
47
- * import { fetchConsumableUnits } from "@morpho-org/midnight-sdk";
48
- * import { createPublicClient, http, zeroHash } from "viem";
49
- * import { base } from "viem/chains";
50
- *
51
- * const client = createPublicClient({ chain: base, transport: http() });
52
- * const units = await fetchConsumableUnits(client, {
53
- * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
54
- * offer: {
55
- * buy: true,
56
- * maker: "0x0000000000000000000000000000000000009000",
57
- * tick: 5_000n,
58
- * maxUnits: 100n,
59
- * maxAssets: 0n,
60
- * continuousFeeCap: 317097919n,
61
- * },
62
- * group: zeroHash,
63
- * timeToMaturity: 3_600n,
64
- * });
65
- * console.log(units);
66
- * ```
67
- */
68
- async function fetchConsumableUnits(client, params) {
69
- const maxUnits = BigInt(params.offer.maxUnits);
70
- const maxAssets = BigInt(params.offer.maxAssets);
71
- const continuousFeeCap = BigInt(params.offer.continuousFeeCap);
72
- (0, morpho_ts_1.assertNonNegative)("offer.maxUnits", maxUnits);
73
- (0, morpho_ts_1.assertNonNegative)("offer.maxAssets", maxAssets);
74
- (0, morpho_ts_1.assertNonNegative)("offer.continuousFeeCap", continuousFeeCap);
75
- index_js_2.OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
76
- const needsSettlementFee = maxUnits === 0n;
77
- const timeToMaturity = needsSettlementFee
78
- ? BigInt(params.timeToMaturity)
79
- : 0n;
80
- if (needsSettlementFee) {
81
- (0, morpho_ts_1.assertNonNegative)("timeToMaturity", timeToMaturity);
82
- }
83
- const chainId = await (0, utils_js_1.resolveChainId)(client);
84
- const midnight = (0, morpho_ts_1.getChainAddress)(chainId, "midnight");
85
- const consumed = (0, actions_1.readContract)(client, {
86
- ...(0, utils_js_1.callParameters)(params),
87
- address: midnight,
88
- abi: abis_js_1.midnightAbi,
89
- functionName: "consumed",
90
- args: [params.offer.maker, params.group],
91
- });
92
- const continuousFee = (0, actions_1.readContract)(client, {
93
- ...(0, utils_js_1.callParameters)(params),
94
- address: midnight,
95
- abi: abis_js_1.midnightAbi,
96
- functionName: "continuousFee",
97
- args: [params.marketId],
98
- });
99
- const [consumedValue, marketContinuousFee] = await Promise.all([
100
- consumed,
101
- continuousFee,
102
- ]);
103
- if (continuousFeeCap < BigInt(marketContinuousFee))
104
- return 0n;
105
- const settlementFee = needsSettlementFee
106
- ? await (0, actions_1.readContract)(client, {
107
- ...(0, utils_js_1.callParameters)(params),
108
- address: midnight,
109
- abi: abis_js_1.midnightAbi,
110
- functionName: "settlementFee",
111
- args: [params.marketId, timeToMaturity],
112
- })
113
- : 0n;
114
- return index_js_1.ConsumableUnitsLib.consumableUnits({
115
- offer: params.offer,
116
- consumed: consumedValue,
117
- settlementFee,
118
- });
119
- }
@@ -1,57 +0,0 @@
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
- }
@@ -1,77 +0,0 @@
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 = {}));
@@ -1,74 +0,0 @@
1
- import { type BigIntish } from "@morpho-org/morpho-ts";
2
- import type { Address, Client, Hash } from "viem";
3
- import type { MidnightFetchParams } from "./types.js";
4
- /**
5
- * Fetches and computes remaining consumable units for an offer.
6
- *
7
- * Unit-capped offers skip settlement-fee lookup, matching the Solidity
8
- * library's early return. For asset-capped offers, pass the time to maturity
9
- * for the same block context as the quote.
10
- *
11
- * Reads `eth_chainId` only when the viem client has no configured chain id,
12
- * then reads `Midnight.consumed(offer.maker, group)` and
13
- * `Midnight.continuousFee(marketId)`. If the offer's continuous fee cap is
14
- * below the market's current continuous fee, returns zero. For asset-capped
15
- * offers where `offer.maxUnits` is zero, also reads
16
- * `Midnight.settlementFee(marketId, timeToMaturity)`.
17
- *
18
- * @param client - Viem client used for the reads.
19
- * @param params.marketId - Market id used for settlement-fee lookup on asset-capped offers.
20
- * @param params.offer.buy - Whether the maker buys loan assets.
21
- * @param params.offer.maker - Maker address whose consumed group amount is read.
22
- * @param params.offer.tick - Offer tick used for asset-to-unit conversion.
23
- * @param params.offer.maxUnits - Unit cap; when non-zero, settlement-fee lookup is skipped.
24
- * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
25
- * @param params.offer.continuousFeeCap - Maximum market continuous fee accepted by this offer.
26
- * @param params.group - Offer group id whose consumed amount is read.
27
- * @param params.timeToMaturity - Time to maturity used for settlement-fee lookup on asset-capped offers.
28
- * @param params.account - Optional account used as the `from` field for the reads.
29
- * @param params.blockNumber - Optional block number used for the reads.
30
- * @param params.blockTag - Optional block tag used for the reads.
31
- * @param params.stateOverride - Optional state override set used for the reads.
32
- * @returns Consumable units.
33
- * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
34
- * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
35
- * @throws {NegativeValueError} when asset-capped `timeToMaturity`, offer caps, or SDK math inputs are negative.
36
- * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
37
- * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
38
- * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
39
- * @example
40
- * ```ts
41
- * import { fetchConsumableUnits } from "@morpho-org/midnight-sdk";
42
- * import { createPublicClient, http, zeroHash } from "viem";
43
- * import { base } from "viem/chains";
44
- *
45
- * const client = createPublicClient({ chain: base, transport: http() });
46
- * const units = await fetchConsumableUnits(client, {
47
- * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
48
- * offer: {
49
- * buy: true,
50
- * maker: "0x0000000000000000000000000000000000009000",
51
- * tick: 5_000n,
52
- * maxUnits: 100n,
53
- * maxAssets: 0n,
54
- * continuousFeeCap: 317097919n,
55
- * },
56
- * group: zeroHash,
57
- * timeToMaturity: 3_600n,
58
- * });
59
- * console.log(units);
60
- * ```
61
- */
62
- export declare function fetchConsumableUnits(client: Client, params: MidnightFetchParams & {
63
- readonly marketId: Hash;
64
- readonly offer: {
65
- readonly buy: boolean;
66
- readonly maker: Address;
67
- readonly tick: BigIntish;
68
- readonly maxUnits: BigIntish;
69
- readonly maxAssets: BigIntish;
70
- readonly continuousFeeCap: BigIntish;
71
- };
72
- readonly group: Hash;
73
- readonly timeToMaturity: BigIntish;
74
- }): Promise<bigint>;
@@ -1,116 +0,0 @@
1
- import { assertNonNegative, getChainAddress, } from "@morpho-org/morpho-ts";
2
- import { readContract } from "viem/actions";
3
- import { midnightAbi } from "../abis.js";
4
- import { ConsumableUnitsLib } from "../math/index.js";
5
- import { OfferUtils } from "../offers/index.js";
6
- import { callParameters, resolveChainId } from "./utils.js";
7
- /**
8
- * Fetches and computes remaining consumable units for an offer.
9
- *
10
- * Unit-capped offers skip settlement-fee lookup, matching the Solidity
11
- * library's early return. For asset-capped offers, pass the time to maturity
12
- * for the same block context as the quote.
13
- *
14
- * Reads `eth_chainId` only when the viem client has no configured chain id,
15
- * then reads `Midnight.consumed(offer.maker, group)` and
16
- * `Midnight.continuousFee(marketId)`. If the offer's continuous fee cap is
17
- * below the market's current continuous fee, returns zero. For asset-capped
18
- * offers where `offer.maxUnits` is zero, also reads
19
- * `Midnight.settlementFee(marketId, timeToMaturity)`.
20
- *
21
- * @param client - Viem client used for the reads.
22
- * @param params.marketId - Market id used for settlement-fee lookup on asset-capped offers.
23
- * @param params.offer.buy - Whether the maker buys loan assets.
24
- * @param params.offer.maker - Maker address whose consumed group amount is read.
25
- * @param params.offer.tick - Offer tick used for asset-to-unit conversion.
26
- * @param params.offer.maxUnits - Unit cap; when non-zero, settlement-fee lookup is skipped.
27
- * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
28
- * @param params.offer.continuousFeeCap - Maximum market continuous fee accepted by this offer.
29
- * @param params.group - Offer group id whose consumed amount is read.
30
- * @param params.timeToMaturity - Time to maturity used for settlement-fee lookup on asset-capped offers.
31
- * @param params.account - Optional account used as the `from` field for the reads.
32
- * @param params.blockNumber - Optional block number used for the reads.
33
- * @param params.blockTag - Optional block tag used for the reads.
34
- * @param params.stateOverride - Optional state override set used for the reads.
35
- * @returns Consumable units.
36
- * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
37
- * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
38
- * @throws {NegativeValueError} when asset-capped `timeToMaturity`, offer caps, or SDK math inputs are negative.
39
- * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
40
- * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
41
- * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
42
- * @example
43
- * ```ts
44
- * import { fetchConsumableUnits } from "@morpho-org/midnight-sdk";
45
- * import { createPublicClient, http, zeroHash } from "viem";
46
- * import { base } from "viem/chains";
47
- *
48
- * const client = createPublicClient({ chain: base, transport: http() });
49
- * const units = await fetchConsumableUnits(client, {
50
- * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
51
- * offer: {
52
- * buy: true,
53
- * maker: "0x0000000000000000000000000000000000009000",
54
- * tick: 5_000n,
55
- * maxUnits: 100n,
56
- * maxAssets: 0n,
57
- * continuousFeeCap: 317097919n,
58
- * },
59
- * group: zeroHash,
60
- * timeToMaturity: 3_600n,
61
- * });
62
- * console.log(units);
63
- * ```
64
- */
65
- export async function fetchConsumableUnits(client, params) {
66
- const maxUnits = BigInt(params.offer.maxUnits);
67
- const maxAssets = BigInt(params.offer.maxAssets);
68
- const continuousFeeCap = BigInt(params.offer.continuousFeeCap);
69
- assertNonNegative("offer.maxUnits", maxUnits);
70
- assertNonNegative("offer.maxAssets", maxAssets);
71
- assertNonNegative("offer.continuousFeeCap", continuousFeeCap);
72
- OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
73
- const needsSettlementFee = maxUnits === 0n;
74
- const timeToMaturity = needsSettlementFee
75
- ? BigInt(params.timeToMaturity)
76
- : 0n;
77
- if (needsSettlementFee) {
78
- assertNonNegative("timeToMaturity", timeToMaturity);
79
- }
80
- const chainId = await resolveChainId(client);
81
- const midnight = getChainAddress(chainId, "midnight");
82
- const consumed = readContract(client, {
83
- ...callParameters(params),
84
- address: midnight,
85
- abi: midnightAbi,
86
- functionName: "consumed",
87
- args: [params.offer.maker, params.group],
88
- });
89
- const continuousFee = readContract(client, {
90
- ...callParameters(params),
91
- address: midnight,
92
- abi: midnightAbi,
93
- functionName: "continuousFee",
94
- args: [params.marketId],
95
- });
96
- const [consumedValue, marketContinuousFee] = await Promise.all([
97
- consumed,
98
- continuousFee,
99
- ]);
100
- if (continuousFeeCap < BigInt(marketContinuousFee))
101
- return 0n;
102
- const settlementFee = needsSettlementFee
103
- ? await readContract(client, {
104
- ...callParameters(params),
105
- address: midnight,
106
- abi: midnightAbi,
107
- functionName: "settlementFee",
108
- args: [params.marketId, timeToMaturity],
109
- })
110
- : 0n;
111
- return ConsumableUnitsLib.consumableUnits({
112
- offer: params.offer,
113
- consumed: consumedValue,
114
- settlementFee,
115
- });
116
- }
@@ -1,57 +0,0 @@
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
- }
@@ -1,74 +0,0 @@
1
- import { assertNonNegative, MathLib, } from "@morpho-org/morpho-ts";
2
- import { OfferUtils } from "../offers/index.js";
3
- import { TakeAmountsLib } from "./TakeAmountsLib.js";
4
- /**
5
- * TypeScript port of Midnight `ConsumableUnitsLib`.
6
- *
7
- * @example
8
- * ```ts
9
- * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
10
- *
11
- * console.log(typeof ConsumableUnitsLib.consumableUnits);
12
- * ```
13
- */
14
- export var ConsumableUnitsLib;
15
- (function (ConsumableUnitsLib) {
16
- /**
17
- * Returns units that would fully consume an offer.
18
- *
19
- * @param params.offer.buy - Whether the maker buys loan assets.
20
- * @param params.offer.tick - Offer tick used for asset-capped conversion.
21
- * @param params.offer.maxUnits - Unit cap; when non-zero, units are capped directly.
22
- * @param params.offer.maxAssets - Asset cap used when `params.offer.maxUnits` is zero.
23
- * @param params.consumed - Amount already consumed from the offer group.
24
- * @param params.settlementFee - WAD-scaled settlement fee used for asset-capped conversion.
25
- * @returns Remaining consumable units.
26
- * @throws {NegativeValueError} when `consumed`, offer limits, delegated asset inputs, or the offer tick is negative.
27
- * @throws {InvalidOfferParameterError} when offer caps are both zero or both non-zero.
28
- * @throws {DivisionByZeroError} when the delegated units conversion divides by zero.
29
- * @throws {TickOutOfRangeError} when the offer tick exceeds `MAX_TICK`.
30
- * @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy offer price.
31
- * @example
32
- * ```ts
33
- * import { ConsumableUnitsLib } from "@morpho-org/midnight-sdk";
34
- *
35
- * const offer = {
36
- * buy: true,
37
- * tick: 5_820n,
38
- * maxUnits: 0n,
39
- * maxAssets: 1_000n,
40
- * };
41
- *
42
- * const units = ConsumableUnitsLib.consumableUnits({
43
- * offer,
44
- * consumed: 0n,
45
- * settlementFee: 0n,
46
- * });
47
- * console.log(units);
48
- * ```
49
- */
50
- function consumableUnits(params) {
51
- const consumed = BigInt(params.consumed);
52
- const maxUnits = BigInt(params.offer.maxUnits);
53
- const maxAssets = BigInt(params.offer.maxAssets);
54
- assertNonNegative("consumed", consumed);
55
- assertNonNegative("offer.maxUnits", maxUnits);
56
- assertNonNegative("offer.maxAssets", maxAssets);
57
- OfferUtils.validateOfferCaps({ maxUnits, maxAssets });
58
- if (maxUnits > 0n)
59
- return MathLib.zeroFloorSub(maxUnits, consumed);
60
- const remainingAssets = MathLib.zeroFloorSub(maxAssets, consumed);
61
- return params.offer.buy
62
- ? TakeAmountsLib.buyerAssetsToUnits({
63
- offer: params.offer,
64
- targetBuyerAssets: remainingAssets,
65
- settlementFee: params.settlementFee,
66
- })
67
- : TakeAmountsLib.sellerAssetsToUnits({
68
- offer: params.offer,
69
- targetSellerAssets: remainingAssets,
70
- settlementFee: params.settlementFee,
71
- });
72
- }
73
- ConsumableUnitsLib.consumableUnits = consumableUnits;
74
- })(ConsumableUnitsLib || (ConsumableUnitsLib = {}));
package/src/index.ts DELETED
@@ -1,19 +0,0 @@
1
- export * from "./abis.js";
2
- export * from "./constants.js";
3
- export * from "./errors.js";
4
- export * from "./fetch/index.js";
5
- export * from "./market/index.js";
6
- export * from "./math/index.js";
7
- export * from "./offers/index.js";
8
- export * from "./signatures/index.js";
9
-
10
- /**
11
- * Namespace for Midnight mempool payload utilities.
12
- *
13
- * Use `Payload.encode` to turn ratified offer items into wire bytes for mempool
14
- * publication or raw payload API validation, and `Payload.decode` to inspect
15
- * published maker offers, ratifier data, or attribution-tagged payloads.
16
- * Encoded payloads are always `Hex` strings ready to be included in onchain
17
- * mempool submission calldata.
18
- */
19
- export * as Payload from "./signatures/Payload.js";