@morpho-org/midnight-sdk 2.4.0-next.0 → 2.4.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 (37) hide show
  1. package/lib/cjs/abis.d.ts +382 -0
  2. package/lib/cjs/abis.js +488 -1
  3. package/lib/cjs/api/MidnightApi.d.ts +14 -12
  4. package/lib/cjs/api/MidnightApi.js +35 -25
  5. package/lib/cjs/api/helpers.d.ts +10 -0
  6. package/lib/cjs/api/helpers.js +97 -10
  7. package/lib/cjs/api/types.d.ts +5 -0
  8. package/lib/cjs/api/version.generated.d.ts +1 -1
  9. package/lib/cjs/api/version.generated.js +1 -1
  10. package/lib/cjs/errors.d.ts +27 -13
  11. package/lib/cjs/errors.js +39 -8
  12. package/lib/cjs/offers/Offer.d.ts +3 -0
  13. package/lib/cjs/offers/Offer.js +3 -0
  14. package/lib/cjs/offers/OfferUtils.d.ts +6 -0
  15. package/lib/cjs/offers/OfferUtils.js +14 -0
  16. package/lib/cjs/position/Position.d.ts +3 -0
  17. package/lib/cjs/position/Position.js +13 -0
  18. package/lib/cjs/position/PositionUtils.js +35 -11
  19. package/lib/esm/abis.d.ts +382 -0
  20. package/lib/esm/abis.js +487 -0
  21. package/lib/esm/api/MidnightApi.d.ts +14 -12
  22. package/lib/esm/api/MidnightApi.js +36 -26
  23. package/lib/esm/api/helpers.d.ts +10 -0
  24. package/lib/esm/api/helpers.js +91 -8
  25. package/lib/esm/api/types.d.ts +5 -0
  26. package/lib/esm/api/version.generated.d.ts +1 -1
  27. package/lib/esm/api/version.generated.js +1 -1
  28. package/lib/esm/errors.d.ts +27 -13
  29. package/lib/esm/errors.js +36 -8
  30. package/lib/esm/offers/Offer.d.ts +3 -0
  31. package/lib/esm/offers/Offer.js +3 -0
  32. package/lib/esm/offers/OfferUtils.d.ts +6 -0
  33. package/lib/esm/offers/OfferUtils.js +15 -1
  34. package/lib/esm/position/Position.d.ts +3 -0
  35. package/lib/esm/position/Position.js +14 -1
  36. package/lib/esm/position/PositionUtils.js +36 -12
  37. package/package.json +4 -4
@@ -170,6 +170,9 @@ export interface IAccrualPosition extends Omit<IPosition, "marketId"> {
170
170
  */
171
171
  export declare class AccrualPosition extends Position implements IAccrualPosition {
172
172
  protected readonly _market: Market;
173
+ /**
174
+ * @throws {InvalidMarketParameterError} when `position` carries a `marketId` that differs from `market.id`.
175
+ */
173
176
  constructor(position: IAccrualPosition, market: IMarket | Market);
174
177
  /** Hydrated market for this position. */
175
178
  get market(): Market;
@@ -144,8 +144,21 @@ exports.Position = Position;
144
144
  */
145
145
  class AccrualPosition extends Position {
146
146
  _market;
147
+ /**
148
+ * @throws {InvalidMarketParameterError} when `position` carries a `marketId` that differs from `market.id`.
149
+ */
147
150
  constructor(position, market) {
148
151
  const _market = Market_js_1.Market.from(market);
152
+ const carriedMarketId = "marketId" in position ? position.marketId : undefined;
153
+ if (carriedMarketId !== undefined &&
154
+ (typeof carriedMarketId !== "string" ||
155
+ carriedMarketId.toLowerCase() !== _market.id.toLowerCase())) {
156
+ throw new errors_js_1.InvalidMarketParameterError({
157
+ parameter: "marketId",
158
+ value: carriedMarketId,
159
+ instruction: "Pair the position with the market it was read from.",
160
+ });
161
+ }
149
162
  super({ ...position, marketId: _market.id });
150
163
  this._market = _market;
151
164
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PositionUtils = void 0;
4
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const errors_js_1 = require("../errors.js");
5
6
  /**
6
7
  * Domain helpers for Midnight positions.
7
8
  *
@@ -134,17 +135,40 @@ var PositionUtils;
134
135
  */
135
136
  function accrueInterest(params) {
136
137
  const timestamp = BigInt(params.timestamp);
138
+ const credit = BigInt(params.position.credit);
139
+ const pendingFee = BigInt(params.position.pendingFee);
140
+ const lastLossFactor = BigInt(params.position.lastLossFactor);
141
+ const lastAccrual = BigInt(params.position.lastAccrual);
137
142
  const marketLossFactor = BigInt(params.market.lossFactor);
143
+ if (timestamp < lastAccrual) {
144
+ throw new errors_js_1.InvalidPositionAccrualTimestampError(timestamp, lastAccrual);
145
+ }
146
+ if (marketLossFactor < lastLossFactor) {
147
+ throw new errors_js_1.InvalidPositionLossFactorError(marketLossFactor, lastLossFactor);
148
+ }
149
+ if (pendingFee > credit) {
150
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending fee must be less than or equal to credit.");
151
+ }
152
+ const postSlashCredit = lastLossFactor < morpho_ts_1.MathLib.MAX_UINT_128
153
+ ? morpho_ts_1.MathLib.mulDivDown(credit, morpho_ts_1.MathLib.MAX_UINT_128 - marketLossFactor, morpho_ts_1.MathLib.MAX_UINT_128 - lastLossFactor)
154
+ : 0n;
155
+ if (postSlashCredit > credit) {
156
+ throw new errors_js_1.InvalidPositionAccrualStateError("Post-slash credit cannot exceed stored credit.");
157
+ }
158
+ const creditDecrease = credit - postSlashCredit;
159
+ const pendingFeeDecrease = credit > 0n ? morpho_ts_1.MathLib.mulDivUp(pendingFee, creditDecrease, credit) : 0n;
160
+ if (pendingFeeDecrease > pendingFee) {
161
+ throw new errors_js_1.InvalidPositionAccrualStateError("Pending-fee decrease cannot exceed pending fee.");
162
+ }
163
+ const postSlashPendingFee = pendingFee - pendingFeeDecrease;
138
164
  const maturity = BigInt(params.market.params.maturity);
139
- const { credit, pendingFee, accruedFee } = morpho_ts_1.MidnightPositionLib.accrue({
140
- credit: params.position.credit,
141
- pendingFee: params.position.pendingFee,
142
- lastLossFactor: params.position.lastLossFactor,
143
- lastAccrual: params.position.lastAccrual,
144
- marketLossFactor,
145
- maturity,
146
- timestamp,
147
- });
165
+ const accrualEnd = timestamp < maturity ? timestamp : maturity;
166
+ const accruedFee = lastAccrual < maturity
167
+ ? morpho_ts_1.MathLib.mulDivDown(postSlashPendingFee, accrualEnd - lastAccrual, maturity - lastAccrual)
168
+ : 0n;
169
+ if (accruedFee > postSlashCredit || accruedFee > postSlashPendingFee) {
170
+ throw new errors_js_1.InvalidPositionAccrualStateError("Accrued fee cannot exceed post-slash credit or pending fee.");
171
+ }
148
172
  const collateral = params.position.collateral.map((assets) => BigInt(assets));
149
173
  const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
150
174
  token: collateralParam.token,
@@ -175,8 +199,8 @@ var PositionUtils;
175
199
  position: {
176
200
  user: params.position.user,
177
201
  marketId: params.position.marketId,
178
- credit,
179
- pendingFee,
202
+ credit: postSlashCredit - accruedFee,
203
+ pendingFee: postSlashPendingFee - accruedFee,
180
204
  lastLossFactor: marketLossFactor,
181
205
  lastAccrual: timestamp,
182
206
  debt: BigInt(params.position.debt),
package/lib/esm/abis.d.ts CHANGED
@@ -5382,3 +5382,385 @@ export declare const midnightEventsAbi: readonly [{
5382
5382
  }];
5383
5383
  readonly anonymous: false;
5384
5384
  }];
5385
+ /**
5386
+ * ABI JSON for a per-owner BlueBuyCallback, which parks Midnight buy-offer funds as Morpho Blue
5387
+ * supply. Its constructor authorizes `OWNER` on Morpho Blue to manage the callback position.
5388
+ *
5389
+ * Source: `morpho-org/midnight` commit `4603969e80d6f911c33586c6b971e8edd7821bbc`,
5390
+ * `src/periphery/blue-buy-callback/BlueBuyCallback.sol` (compiled with `forge inspect ... abi`).
5391
+ *
5392
+ * @example
5393
+ * ```ts
5394
+ * import { blueBuyCallbackAbi } from "@morpho-org/midnight-sdk";
5395
+ *
5396
+ * console.log(blueBuyCallbackAbi.length);
5397
+ * ```
5398
+ */
5399
+ export declare const blueBuyCallbackAbi: readonly [{
5400
+ readonly type: "constructor";
5401
+ readonly inputs: readonly [{
5402
+ readonly name: "_owner";
5403
+ readonly type: "address";
5404
+ readonly internalType: "address";
5405
+ }, {
5406
+ readonly name: "_midnight";
5407
+ readonly type: "address";
5408
+ readonly internalType: "address";
5409
+ }, {
5410
+ readonly name: "_blue";
5411
+ readonly type: "address";
5412
+ readonly internalType: "address";
5413
+ }];
5414
+ readonly stateMutability: "nonpayable";
5415
+ }, {
5416
+ readonly type: "function";
5417
+ readonly name: "BLUE";
5418
+ readonly inputs: readonly [];
5419
+ readonly outputs: readonly [{
5420
+ readonly name: "";
5421
+ readonly type: "address";
5422
+ readonly internalType: "address";
5423
+ }];
5424
+ readonly stateMutability: "view";
5425
+ }, {
5426
+ readonly type: "function";
5427
+ readonly name: "MIDNIGHT";
5428
+ readonly inputs: readonly [];
5429
+ readonly outputs: readonly [{
5430
+ readonly name: "";
5431
+ readonly type: "address";
5432
+ readonly internalType: "address";
5433
+ }];
5434
+ readonly stateMutability: "view";
5435
+ }, {
5436
+ readonly type: "function";
5437
+ readonly name: "OWNER";
5438
+ readonly inputs: readonly [];
5439
+ readonly outputs: readonly [{
5440
+ readonly name: "";
5441
+ readonly type: "address";
5442
+ readonly internalType: "address";
5443
+ }];
5444
+ readonly stateMutability: "view";
5445
+ }, {
5446
+ readonly type: "function";
5447
+ readonly name: "buyerAssetsBound";
5448
+ readonly inputs: readonly [{
5449
+ readonly name: "";
5450
+ readonly type: "bytes32";
5451
+ readonly internalType: "bytes32";
5452
+ }, {
5453
+ readonly name: "";
5454
+ readonly type: "tuple";
5455
+ readonly internalType: "struct Market";
5456
+ readonly components: readonly [{
5457
+ readonly name: "chainId";
5458
+ readonly type: "uint256";
5459
+ readonly internalType: "uint256";
5460
+ }, {
5461
+ readonly name: "midnight";
5462
+ readonly type: "address";
5463
+ readonly internalType: "address";
5464
+ }, {
5465
+ readonly name: "loanToken";
5466
+ readonly type: "address";
5467
+ readonly internalType: "address";
5468
+ }, {
5469
+ readonly name: "collateralParams";
5470
+ readonly type: "tuple[]";
5471
+ readonly internalType: "struct CollateralParams[]";
5472
+ readonly components: readonly [{
5473
+ readonly name: "token";
5474
+ readonly type: "address";
5475
+ readonly internalType: "address";
5476
+ }, {
5477
+ readonly name: "lltv";
5478
+ readonly type: "uint256";
5479
+ readonly internalType: "uint256";
5480
+ }, {
5481
+ readonly name: "liquidationCursor";
5482
+ readonly type: "uint256";
5483
+ readonly internalType: "uint256";
5484
+ }, {
5485
+ readonly name: "oracle";
5486
+ readonly type: "address";
5487
+ readonly internalType: "address";
5488
+ }];
5489
+ }, {
5490
+ readonly name: "maturity";
5491
+ readonly type: "uint256";
5492
+ readonly internalType: "uint256";
5493
+ }, {
5494
+ readonly name: "rcfThreshold";
5495
+ readonly type: "uint256";
5496
+ readonly internalType: "uint256";
5497
+ }, {
5498
+ readonly name: "enterGate";
5499
+ readonly type: "address";
5500
+ readonly internalType: "address";
5501
+ }, {
5502
+ readonly name: "liquidatorGate";
5503
+ readonly type: "address";
5504
+ readonly internalType: "address";
5505
+ }];
5506
+ }, {
5507
+ readonly name: "";
5508
+ readonly type: "address";
5509
+ readonly internalType: "address";
5510
+ }, {
5511
+ readonly name: "data";
5512
+ readonly type: "bytes";
5513
+ readonly internalType: "bytes";
5514
+ }];
5515
+ readonly outputs: readonly [{
5516
+ readonly name: "";
5517
+ readonly type: "uint256";
5518
+ readonly internalType: "uint256";
5519
+ }];
5520
+ readonly stateMutability: "view";
5521
+ }, {
5522
+ readonly type: "function";
5523
+ readonly name: "nonce";
5524
+ readonly inputs: readonly [];
5525
+ readonly outputs: readonly [{
5526
+ readonly name: "";
5527
+ readonly type: "uint256";
5528
+ readonly internalType: "uint256";
5529
+ }];
5530
+ readonly stateMutability: "view";
5531
+ }, {
5532
+ readonly type: "function";
5533
+ readonly name: "onBuy";
5534
+ readonly inputs: readonly [{
5535
+ readonly name: "";
5536
+ readonly type: "bytes32";
5537
+ readonly internalType: "bytes32";
5538
+ }, {
5539
+ readonly name: "market";
5540
+ readonly type: "tuple";
5541
+ readonly internalType: "struct Market";
5542
+ readonly components: readonly [{
5543
+ readonly name: "chainId";
5544
+ readonly type: "uint256";
5545
+ readonly internalType: "uint256";
5546
+ }, {
5547
+ readonly name: "midnight";
5548
+ readonly type: "address";
5549
+ readonly internalType: "address";
5550
+ }, {
5551
+ readonly name: "loanToken";
5552
+ readonly type: "address";
5553
+ readonly internalType: "address";
5554
+ }, {
5555
+ readonly name: "collateralParams";
5556
+ readonly type: "tuple[]";
5557
+ readonly internalType: "struct CollateralParams[]";
5558
+ readonly components: readonly [{
5559
+ readonly name: "token";
5560
+ readonly type: "address";
5561
+ readonly internalType: "address";
5562
+ }, {
5563
+ readonly name: "lltv";
5564
+ readonly type: "uint256";
5565
+ readonly internalType: "uint256";
5566
+ }, {
5567
+ readonly name: "liquidationCursor";
5568
+ readonly type: "uint256";
5569
+ readonly internalType: "uint256";
5570
+ }, {
5571
+ readonly name: "oracle";
5572
+ readonly type: "address";
5573
+ readonly internalType: "address";
5574
+ }];
5575
+ }, {
5576
+ readonly name: "maturity";
5577
+ readonly type: "uint256";
5578
+ readonly internalType: "uint256";
5579
+ }, {
5580
+ readonly name: "rcfThreshold";
5581
+ readonly type: "uint256";
5582
+ readonly internalType: "uint256";
5583
+ }, {
5584
+ readonly name: "enterGate";
5585
+ readonly type: "address";
5586
+ readonly internalType: "address";
5587
+ }, {
5588
+ readonly name: "liquidatorGate";
5589
+ readonly type: "address";
5590
+ readonly internalType: "address";
5591
+ }];
5592
+ }, {
5593
+ readonly name: "buyerAssets";
5594
+ readonly type: "uint256";
5595
+ readonly internalType: "uint256";
5596
+ }, {
5597
+ readonly name: "";
5598
+ readonly type: "uint256";
5599
+ readonly internalType: "uint256";
5600
+ }, {
5601
+ readonly name: "";
5602
+ readonly type: "uint256";
5603
+ readonly internalType: "uint256";
5604
+ }, {
5605
+ readonly name: "buyer";
5606
+ readonly type: "address";
5607
+ readonly internalType: "address";
5608
+ }, {
5609
+ readonly name: "data";
5610
+ readonly type: "bytes";
5611
+ readonly internalType: "bytes";
5612
+ }];
5613
+ readonly outputs: readonly [{
5614
+ readonly name: "";
5615
+ readonly type: "bytes32";
5616
+ readonly internalType: "bytes32";
5617
+ }];
5618
+ readonly stateMutability: "nonpayable";
5619
+ }, {
5620
+ readonly type: "function";
5621
+ readonly name: "setAuthorization";
5622
+ readonly inputs: readonly [{
5623
+ readonly name: "authorized";
5624
+ readonly type: "address";
5625
+ readonly internalType: "address";
5626
+ }, {
5627
+ readonly name: "newIsAuthorized";
5628
+ readonly type: "bool";
5629
+ readonly internalType: "bool";
5630
+ }];
5631
+ readonly outputs: readonly [];
5632
+ readonly stateMutability: "nonpayable";
5633
+ }, {
5634
+ readonly type: "function";
5635
+ readonly name: "setAuthorizationWithSig";
5636
+ readonly inputs: readonly [{
5637
+ readonly name: "authorization";
5638
+ readonly type: "tuple";
5639
+ readonly internalType: "struct Authorization";
5640
+ readonly components: readonly [{
5641
+ readonly name: "authorizer";
5642
+ readonly type: "address";
5643
+ readonly internalType: "address";
5644
+ }, {
5645
+ readonly name: "authorized";
5646
+ readonly type: "address";
5647
+ readonly internalType: "address";
5648
+ }, {
5649
+ readonly name: "isAuthorized";
5650
+ readonly type: "bool";
5651
+ readonly internalType: "bool";
5652
+ }, {
5653
+ readonly name: "nonce";
5654
+ readonly type: "uint256";
5655
+ readonly internalType: "uint256";
5656
+ }, {
5657
+ readonly name: "deadline";
5658
+ readonly type: "uint256";
5659
+ readonly internalType: "uint256";
5660
+ }];
5661
+ }, {
5662
+ readonly name: "signature";
5663
+ readonly type: "tuple";
5664
+ readonly internalType: "struct Signature";
5665
+ readonly components: readonly [{
5666
+ readonly name: "v";
5667
+ readonly type: "uint8";
5668
+ readonly internalType: "uint8";
5669
+ }, {
5670
+ readonly name: "r";
5671
+ readonly type: "bytes32";
5672
+ readonly internalType: "bytes32";
5673
+ }, {
5674
+ readonly name: "s";
5675
+ readonly type: "bytes32";
5676
+ readonly internalType: "bytes32";
5677
+ }];
5678
+ }];
5679
+ readonly outputs: readonly [];
5680
+ readonly stateMutability: "nonpayable";
5681
+ }, {
5682
+ readonly type: "function";
5683
+ readonly name: "skim";
5684
+ readonly inputs: readonly [{
5685
+ readonly name: "token";
5686
+ readonly type: "address";
5687
+ readonly internalType: "address";
5688
+ }];
5689
+ readonly outputs: readonly [];
5690
+ readonly stateMutability: "nonpayable";
5691
+ }, {
5692
+ readonly type: "event";
5693
+ readonly name: "SetAuthorizationWithSig";
5694
+ readonly inputs: readonly [{
5695
+ readonly name: "caller";
5696
+ readonly type: "address";
5697
+ readonly indexed: true;
5698
+ readonly internalType: "address";
5699
+ }, {
5700
+ readonly name: "nonce";
5701
+ readonly type: "uint256";
5702
+ readonly indexed: false;
5703
+ readonly internalType: "uint256";
5704
+ }];
5705
+ readonly anonymous: false;
5706
+ }, {
5707
+ readonly type: "event";
5708
+ readonly name: "Skim";
5709
+ readonly inputs: readonly [{
5710
+ readonly name: "caller";
5711
+ readonly type: "address";
5712
+ readonly indexed: true;
5713
+ readonly internalType: "address";
5714
+ }, {
5715
+ readonly name: "token";
5716
+ readonly type: "address";
5717
+ readonly indexed: true;
5718
+ readonly internalType: "address";
5719
+ }, {
5720
+ readonly name: "assets";
5721
+ readonly type: "uint256";
5722
+ readonly indexed: false;
5723
+ readonly internalType: "uint256";
5724
+ }];
5725
+ readonly anonymous: false;
5726
+ }, {
5727
+ readonly type: "error";
5728
+ readonly name: "ApproveReturnedFalse";
5729
+ readonly inputs: readonly [];
5730
+ }, {
5731
+ readonly type: "error";
5732
+ readonly name: "AuthorizationExpired";
5733
+ readonly inputs: readonly [];
5734
+ }, {
5735
+ readonly type: "error";
5736
+ readonly name: "InconsistentLoanToken";
5737
+ readonly inputs: readonly [];
5738
+ }, {
5739
+ readonly type: "error";
5740
+ readonly name: "InvalidNonce";
5741
+ readonly inputs: readonly [];
5742
+ }, {
5743
+ readonly type: "error";
5744
+ readonly name: "InvalidSignature";
5745
+ readonly inputs: readonly [];
5746
+ }, {
5747
+ readonly type: "error";
5748
+ readonly name: "NoCode";
5749
+ readonly inputs: readonly [];
5750
+ }, {
5751
+ readonly type: "error";
5752
+ readonly name: "NotMidnight";
5753
+ readonly inputs: readonly [];
5754
+ }, {
5755
+ readonly type: "error";
5756
+ readonly name: "NotOwner";
5757
+ readonly inputs: readonly [];
5758
+ }, {
5759
+ readonly type: "error";
5760
+ readonly name: "NotOwnerBuyer";
5761
+ readonly inputs: readonly [];
5762
+ }, {
5763
+ readonly type: "error";
5764
+ readonly name: "TransferReturnedFalse";
5765
+ readonly inputs: readonly [];
5766
+ }];