@morpho-org/morpho-sdk 5.0.0 → 5.1.2
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.
- package/README.md +4 -4
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +47 -0
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +63 -0
- package/lib/cjs/actions/blue/repay.d.ts +39 -39
- package/lib/cjs/actions/blue/repay.js +65 -46
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +37 -37
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +64 -41
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +69 -0
- package/lib/cjs/actions/blue/resolveRepayFunding.js +82 -0
- package/lib/cjs/actions/blue/supply.d.ts +1 -1
- package/lib/cjs/actions/blue/supply.js +8 -22
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +1 -1
- package/lib/cjs/actions/blue/supplyCollateral.js +8 -22
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +1 -1
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +8 -21
- package/lib/cjs/entities/blue/blue.js +161 -87
- package/lib/cjs/helpers/index.d.ts +1 -1
- package/lib/cjs/helpers/index.js +1 -2
- package/lib/cjs/helpers/validate.d.ts +0 -18
- package/lib/cjs/helpers/validate.js +1 -38
- package/lib/cjs/types/action.d.ts +51 -3
- package/lib/cjs/types/error.d.ts +24 -2
- package/lib/cjs/types/error.js +26 -5
- package/lib/cjs/utils.d.ts +1 -1
- package/lib/cjs/utils.js +2 -3
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +47 -0
- package/lib/esm/actions/blue/buildAssetFundingActions.js +59 -0
- package/lib/esm/actions/blue/repay.d.ts +39 -39
- package/lib/esm/actions/blue/repay.js +66 -47
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +37 -37
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +65 -42
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +69 -0
- package/lib/esm/actions/blue/resolveRepayFunding.js +78 -0
- package/lib/esm/actions/blue/supply.d.ts +1 -1
- package/lib/esm/actions/blue/supply.js +9 -23
- package/lib/esm/actions/blue/supplyCollateral.d.ts +1 -1
- package/lib/esm/actions/blue/supplyCollateral.js +9 -23
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +1 -1
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +9 -22
- package/lib/esm/entities/blue/blue.js +162 -88
- package/lib/esm/helpers/index.d.ts +1 -1
- package/lib/esm/helpers/index.js +1 -1
- package/lib/esm/helpers/validate.d.ts +0 -18
- package/lib/esm/helpers/validate.js +1 -37
- package/lib/esm/types/action.d.ts +51 -3
- package/lib/esm/types/error.d.ts +24 -2
- package/lib/esm/types/error.js +23 -3
- package/lib/esm/utils.d.ts +1 -1
- package/lib/esm/utils.js +1 -1
- package/package.json +20 -19
- package/src/index.ts +0 -4
|
@@ -265,19 +265,12 @@ class MorphoBlue {
|
|
|
265
265
|
repay(params) {
|
|
266
266
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
267
267
|
const { userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
|
|
268
|
-
if ("
|
|
268
|
+
if ("amount" in params && "shares" in params) {
|
|
269
269
|
throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
|
|
270
270
|
}
|
|
271
|
-
const
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
if (params.assets <= 0n) {
|
|
279
|
-
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
280
|
-
}
|
|
271
|
+
const nativeAmount = params.nativeAmount ?? 0n;
|
|
272
|
+
if (nativeAmount < 0n) {
|
|
273
|
+
throw new index_js_3.NegativeNativeAmountError(nativeAmount);
|
|
281
274
|
}
|
|
282
275
|
(0, index_js_2.validateSlippageTolerance)(slippageTolerance);
|
|
283
276
|
if (!positionData) {
|
|
@@ -288,51 +281,83 @@ class MorphoBlue {
|
|
|
288
281
|
expectedMarketId: this.marketParams.id,
|
|
289
282
|
expectedUser: userAddress,
|
|
290
283
|
});
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
284
|
+
if (nativeAmount > 0n) {
|
|
285
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
|
|
286
|
+
}
|
|
287
|
+
let repayAssets;
|
|
288
|
+
let repayShares;
|
|
289
|
+
let erc20Amount;
|
|
294
290
|
let marketForRepay;
|
|
295
|
-
if (
|
|
291
|
+
if ("shares" in params) {
|
|
292
|
+
const shares = params.shares;
|
|
293
|
+
if (shares <= 0n) {
|
|
294
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
295
|
+
}
|
|
296
296
|
(0, index_js_2.validateRepayShares)({
|
|
297
297
|
positionData,
|
|
298
|
-
repayShares:
|
|
298
|
+
repayShares: shares,
|
|
299
299
|
marketId: this.marketParams.id,
|
|
300
300
|
});
|
|
301
|
-
|
|
302
|
-
|
|
301
|
+
repayAssets = 0n;
|
|
302
|
+
repayShares = shares;
|
|
303
303
|
// 2h forward accrual upper-bounds the on-chain repay price; bundle
|
|
304
304
|
// skims residual back to the receiver.
|
|
305
305
|
const accrualTimestamp = blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), positionData.market.lastUpdate) +
|
|
306
306
|
morpho_ts_1.Time.s.from.h(2n);
|
|
307
307
|
marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
|
|
308
|
-
|
|
308
|
+
const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
|
|
309
|
+
// Native funds part of the transfer; the ERC-20 pulled is the remainder.
|
|
310
|
+
if (nativeAmount > borrowAssets) {
|
|
311
|
+
throw new index_js_3.NativeAmountExceedsTransferAmountError({
|
|
312
|
+
nativeAmount,
|
|
313
|
+
transferAmount: borrowAssets,
|
|
314
|
+
market: this.marketParams.id,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
erc20Amount = borrowAssets - nativeAmount;
|
|
309
318
|
}
|
|
310
319
|
else {
|
|
320
|
+
// Assets mode is additive, like supply: repaid = amount + nativeAmount.
|
|
321
|
+
const amount = params.amount ?? 0n;
|
|
322
|
+
// Reject a negative ERC-20 amount before it can be masked by nativeAmount
|
|
323
|
+
// in the sum below (a negative amount would otherwise yield a negative
|
|
324
|
+
// erc20Amount and a negative approval in getRequirements).
|
|
325
|
+
if (amount < 0n) {
|
|
326
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
327
|
+
}
|
|
328
|
+
repayAssets = amount + nativeAmount;
|
|
329
|
+
if (repayAssets <= 0n) {
|
|
330
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
331
|
+
}
|
|
311
332
|
(0, index_js_2.validateRepayAmount)({
|
|
312
333
|
positionData,
|
|
313
|
-
repayAssets
|
|
334
|
+
repayAssets,
|
|
314
335
|
marketId: this.marketParams.id,
|
|
315
336
|
});
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
transferAmount = params.assets;
|
|
337
|
+
repayShares = 0n;
|
|
338
|
+
erc20Amount = amount;
|
|
319
339
|
marketForRepay = positionData.market;
|
|
320
340
|
}
|
|
321
341
|
const maxSharePrice = (0, index_js_2.computeMaxRepaySharePrice)({
|
|
322
|
-
repayAssets
|
|
323
|
-
repayShares
|
|
342
|
+
repayAssets,
|
|
343
|
+
repayShares,
|
|
324
344
|
market: marketForRepay,
|
|
325
345
|
slippageTolerance,
|
|
326
346
|
});
|
|
327
347
|
return {
|
|
328
|
-
getRequirements: (reqParams) =>
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
348
|
+
getRequirements: (reqParams) => {
|
|
349
|
+
// Fully native repay pulls no ERC-20, so it needs no approval/permit.
|
|
350
|
+
if (erc20Amount === 0n)
|
|
351
|
+
return Promise.resolve([]);
|
|
352
|
+
return (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
|
|
353
|
+
address: this.marketParams.loanToken,
|
|
354
|
+
chainId: this.chainId,
|
|
355
|
+
supportSignature: this.client.options.supportSignature,
|
|
356
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
357
|
+
useSimplePermit: reqParams?.useSimplePermit,
|
|
358
|
+
args: { amount: erc20Amount, from: userAddress },
|
|
359
|
+
});
|
|
360
|
+
},
|
|
336
361
|
buildTx: (signatures) => {
|
|
337
362
|
const { permit } = (0, index_js_3.selectRequirementSignatures)(signatures, {
|
|
338
363
|
permit: true,
|
|
@@ -342,15 +367,27 @@ class MorphoBlue {
|
|
|
342
367
|
chainId: this.chainId,
|
|
343
368
|
marketParams: this.marketParams,
|
|
344
369
|
},
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
370
|
+
// Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
|
|
371
|
+
// Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
|
|
372
|
+
args: repayShares > 0n
|
|
373
|
+
? {
|
|
374
|
+
shares: repayShares,
|
|
375
|
+
transferAmount: erc20Amount,
|
|
376
|
+
nativeAmount,
|
|
377
|
+
onBehalf: userAddress,
|
|
378
|
+
receiver: userAddress,
|
|
379
|
+
maxSharePrice,
|
|
380
|
+
requirementSignature: permit,
|
|
381
|
+
}
|
|
382
|
+
: {
|
|
383
|
+
amount: erc20Amount,
|
|
384
|
+
transferAmount: repayAssets,
|
|
385
|
+
nativeAmount,
|
|
386
|
+
onBehalf: userAddress,
|
|
387
|
+
receiver: userAddress,
|
|
388
|
+
maxSharePrice,
|
|
389
|
+
requirementSignature: permit,
|
|
390
|
+
},
|
|
354
391
|
metadata: this.client.options.metadata,
|
|
355
392
|
});
|
|
356
393
|
},
|
|
@@ -400,19 +437,12 @@ class MorphoBlue {
|
|
|
400
437
|
repayWithdrawCollateral(params) {
|
|
401
438
|
(0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
|
|
402
439
|
const { userAddress, withdrawAmount, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = params;
|
|
403
|
-
if ("
|
|
440
|
+
if ("amount" in params && "shares" in params) {
|
|
404
441
|
throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
|
|
405
442
|
}
|
|
406
|
-
const
|
|
407
|
-
if (
|
|
408
|
-
|
|
409
|
-
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
else {
|
|
413
|
-
if (params.assets <= 0n) {
|
|
414
|
-
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
415
|
-
}
|
|
443
|
+
const nativeAmount = params.nativeAmount ?? 0n;
|
|
444
|
+
if (nativeAmount < 0n) {
|
|
445
|
+
throw new index_js_3.NegativeNativeAmountError(nativeAmount);
|
|
416
446
|
}
|
|
417
447
|
if (withdrawAmount <= 0n) {
|
|
418
448
|
throw new index_js_3.NonPositiveWithdrawCollateralAmountError(this.marketParams.id);
|
|
@@ -426,34 +456,61 @@ class MorphoBlue {
|
|
|
426
456
|
expectedMarketId: this.marketParams.id,
|
|
427
457
|
expectedUser: userAddress,
|
|
428
458
|
});
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
459
|
+
if (nativeAmount > 0n) {
|
|
460
|
+
(0, index_js_2.validateNativeAsset)(this.chainId, this.marketParams.loanToken);
|
|
461
|
+
}
|
|
462
|
+
let repayAssets;
|
|
463
|
+
let repayShares;
|
|
464
|
+
let erc20Amount;
|
|
432
465
|
let marketForRepay;
|
|
433
466
|
// 2h forward accrual upper-bounds the on-chain repay price (shares
|
|
434
467
|
// mode) and the post-repay health check; bundle skims residual back.
|
|
435
468
|
const accrualTimestamp = blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), positionData.market.lastUpdate) +
|
|
436
469
|
morpho_ts_1.Time.s.from.h(2n);
|
|
437
|
-
if (
|
|
470
|
+
if ("shares" in params) {
|
|
471
|
+
const shares = params.shares;
|
|
472
|
+
if (shares <= 0n) {
|
|
473
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
474
|
+
}
|
|
438
475
|
(0, index_js_2.validateRepayShares)({
|
|
439
476
|
positionData,
|
|
440
|
-
repayShares:
|
|
477
|
+
repayShares: shares,
|
|
441
478
|
marketId: this.marketParams.id,
|
|
442
479
|
});
|
|
443
|
-
|
|
444
|
-
|
|
480
|
+
repayAssets = 0n;
|
|
481
|
+
repayShares = shares;
|
|
445
482
|
marketForRepay = positionData.market.accrueInterest(accrualTimestamp);
|
|
446
|
-
|
|
483
|
+
const borrowAssets = marketForRepay.toBorrowAssets(shares, "Up");
|
|
484
|
+
// Native funds part of the transfer; the ERC-20 pulled is the remainder.
|
|
485
|
+
if (nativeAmount > borrowAssets) {
|
|
486
|
+
throw new index_js_3.NativeAmountExceedsTransferAmountError({
|
|
487
|
+
nativeAmount,
|
|
488
|
+
transferAmount: borrowAssets,
|
|
489
|
+
market: this.marketParams.id,
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
erc20Amount = borrowAssets - nativeAmount;
|
|
447
493
|
}
|
|
448
494
|
else {
|
|
495
|
+
// Assets mode is additive, like supply: repaid = amount + nativeAmount.
|
|
496
|
+
const amount = params.amount ?? 0n;
|
|
497
|
+
// Reject a negative ERC-20 amount before it can be masked by nativeAmount
|
|
498
|
+
// in the sum below (a negative amount would otherwise yield a negative
|
|
499
|
+
// erc20Amount and a negative approval in getRequirements).
|
|
500
|
+
if (amount < 0n) {
|
|
501
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
502
|
+
}
|
|
503
|
+
repayAssets = amount + nativeAmount;
|
|
504
|
+
if (repayAssets <= 0n) {
|
|
505
|
+
throw new index_js_3.NonPositiveRepayAmountError(this.marketParams.id);
|
|
506
|
+
}
|
|
449
507
|
(0, index_js_2.validateRepayAmount)({
|
|
450
508
|
positionData,
|
|
451
|
-
repayAssets
|
|
509
|
+
repayAssets,
|
|
452
510
|
marketId: this.marketParams.id,
|
|
453
511
|
});
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
transferAmount = params.assets;
|
|
512
|
+
repayShares = 0n;
|
|
513
|
+
erc20Amount = amount;
|
|
457
514
|
marketForRepay = positionData.market;
|
|
458
515
|
}
|
|
459
516
|
if (withdrawAmount > positionData.collateral) {
|
|
@@ -463,7 +520,7 @@ class MorphoBlue {
|
|
|
463
520
|
market: positionData.marketId,
|
|
464
521
|
});
|
|
465
522
|
}
|
|
466
|
-
const { position: positionAfterRepay } = positionData.repay(
|
|
523
|
+
const { position: positionAfterRepay } = positionData.repay(repayAssets, repayShares, accrualTimestamp);
|
|
467
524
|
(0, index_js_2.validatePositionHealthAfterWithdraw)({
|
|
468
525
|
positionData: positionAfterRepay,
|
|
469
526
|
withdrawAmount,
|
|
@@ -471,22 +528,25 @@ class MorphoBlue {
|
|
|
471
528
|
marketId: this.marketParams.id,
|
|
472
529
|
});
|
|
473
530
|
const maxSharePrice = (0, index_js_2.computeMaxRepaySharePrice)({
|
|
474
|
-
repayAssets
|
|
475
|
-
repayShares
|
|
531
|
+
repayAssets,
|
|
532
|
+
repayShares,
|
|
476
533
|
market: marketForRepay,
|
|
477
534
|
slippageTolerance,
|
|
478
535
|
});
|
|
479
536
|
return {
|
|
480
537
|
getRequirements: async (reqParams) => {
|
|
481
538
|
const [erc20Requirements, authTx] = await Promise.all([
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
539
|
+
// Fully native repay pulls no ERC-20, so it needs no approval/permit.
|
|
540
|
+
erc20Amount === 0n
|
|
541
|
+
? Promise.resolve([])
|
|
542
|
+
: (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
|
|
543
|
+
address: this.marketParams.loanToken,
|
|
544
|
+
chainId: this.chainId,
|
|
545
|
+
supportSignature: this.client.options.supportSignature,
|
|
546
|
+
supportDeployless: this.client.options.supportDeployless,
|
|
547
|
+
useSimplePermit: reqParams?.useSimplePermit,
|
|
548
|
+
args: { amount: erc20Amount, from: userAddress },
|
|
549
|
+
}),
|
|
490
550
|
(0, index_js_1.getBlueAuthorizationRequirement)({
|
|
491
551
|
viemClient: this.client.viemClient,
|
|
492
552
|
chainId: this.chainId,
|
|
@@ -503,17 +563,31 @@ class MorphoBlue {
|
|
|
503
563
|
chainId: this.chainId,
|
|
504
564
|
marketParams: this.marketParams,
|
|
505
565
|
},
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
566
|
+
// Shares mode: repay `shares`, ERC-20 to pull = `erc20Amount`.
|
|
567
|
+
// Assets mode: repay `repayAssets` (= amount + native), pull `erc20Amount`.
|
|
568
|
+
args: repayShares > 0n
|
|
569
|
+
? {
|
|
570
|
+
shares: repayShares,
|
|
571
|
+
transferAmount: erc20Amount,
|
|
572
|
+
nativeAmount,
|
|
573
|
+
withdrawAmount,
|
|
574
|
+
onBehalf: userAddress,
|
|
575
|
+
receiver: userAddress,
|
|
576
|
+
maxSharePrice,
|
|
577
|
+
requirementSignature: permit,
|
|
578
|
+
authorizationSignature: authorization,
|
|
579
|
+
}
|
|
580
|
+
: {
|
|
581
|
+
amount: erc20Amount,
|
|
582
|
+
transferAmount: repayAssets,
|
|
583
|
+
nativeAmount,
|
|
584
|
+
withdrawAmount,
|
|
585
|
+
onBehalf: userAddress,
|
|
586
|
+
receiver: userAddress,
|
|
587
|
+
maxSharePrice,
|
|
588
|
+
requirementSignature: permit,
|
|
589
|
+
authorizationSignature: authorization,
|
|
590
|
+
},
|
|
517
591
|
metadata: this.client.options.metadata,
|
|
518
592
|
});
|
|
519
593
|
},
|
|
@@ -2,4 +2,4 @@ export { computeReallocations } from "./computeReallocations.js";
|
|
|
2
2
|
export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
|
|
3
3
|
export { addTransactionMetadata } from "./metadata.js";
|
|
4
4
|
export { computeMaxRepaySharePrice, computeMaxSupplySharePrice, computeMinBorrowSharePrice, computeMinWithdrawSharePrice, } from "./slippage.js";
|
|
5
|
-
export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount,
|
|
5
|
+
export { validateAccrualPosition, validateChainId, validateNativeAsset, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayShares, validateSlippageTolerance, validateUserAddress, validateWithdrawAmount, validateWithdrawShares, } from "./validate.js";
|
package/lib/cjs/helpers/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.
|
|
3
|
+
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeAsset = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinWithdrawSharePrice = exports.computeMinBorrowSharePrice = exports.computeMaxSupplySharePrice = exports.computeMaxRepaySharePrice = exports.addTransactionMetadata = exports.MAX_TOKEN_APPROVALS = exports.MAX_SLIPPAGE_TOLERANCE = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_LLTV_BUFFER = exports.APPROVE_ONLY_ONCE_TOKENS = exports.computeReallocations = void 0;
|
|
4
4
|
var computeReallocations_js_1 = require("./computeReallocations.js");
|
|
5
5
|
Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return computeReallocations_js_1.computeReallocations; } });
|
|
6
6
|
var constant_js_1 = require("./constant.js");
|
|
@@ -26,7 +26,6 @@ Object.defineProperty(exports, "validatePositionHealth", { enumerable: true, get
|
|
|
26
26
|
Object.defineProperty(exports, "validatePositionHealthAfterWithdraw", { enumerable: true, get: function () { return validate_js_1.validatePositionHealthAfterWithdraw; } });
|
|
27
27
|
Object.defineProperty(exports, "validateReallocations", { enumerable: true, get: function () { return validate_js_1.validateReallocations; } });
|
|
28
28
|
Object.defineProperty(exports, "validateRepayAmount", { enumerable: true, get: function () { return validate_js_1.validateRepayAmount; } });
|
|
29
|
-
Object.defineProperty(exports, "validateRepayParams", { enumerable: true, get: function () { return validate_js_1.validateRepayParams; } });
|
|
30
29
|
Object.defineProperty(exports, "validateRepayShares", { enumerable: true, get: function () { return validate_js_1.validateRepayShares; } });
|
|
31
30
|
Object.defineProperty(exports, "validateSlippageTolerance", { enumerable: true, get: function () { return validate_js_1.validateSlippageTolerance; } });
|
|
32
31
|
Object.defineProperty(exports, "validateUserAddress", { enumerable: true, get: function () { return validate_js_1.validateUserAddress; } });
|
|
@@ -121,24 +121,6 @@ export declare const validateRepayShares: (params: {
|
|
|
121
121
|
repayShares: bigint;
|
|
122
122
|
marketId: MarketId;
|
|
123
123
|
}) => void;
|
|
124
|
-
/**
|
|
125
|
-
* Validates the common repay input parameters shared by `blueRepay`
|
|
126
|
-
* and `blueRepayWithdrawCollateral`.
|
|
127
|
-
*
|
|
128
|
-
* @param params - Validation parameters.
|
|
129
|
-
* @param params.assets - Repay assets amount (0n when repaying by shares).
|
|
130
|
-
* @param params.shares - Repay shares amount (0n when repaying by assets).
|
|
131
|
-
* @param params.transferAmount - ERC20 amount to transfer to GeneralAdapter1.
|
|
132
|
-
* @param params.maxSharePrice - Maximum repay share price (in ray). Must be positive.
|
|
133
|
-
* @param params.marketId - The market identifier (for error messages).
|
|
134
|
-
*/
|
|
135
|
-
export declare const validateRepayParams: (params: {
|
|
136
|
-
assets: bigint;
|
|
137
|
-
shares: bigint;
|
|
138
|
-
transferAmount: bigint;
|
|
139
|
-
maxSharePrice: bigint;
|
|
140
|
-
marketId: MarketId;
|
|
141
|
-
}) => void;
|
|
142
124
|
/**
|
|
143
125
|
* Validates that vault reallocations are well-formed.
|
|
144
126
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateReallocations = exports.
|
|
3
|
+
exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.compareMarketIds = void 0;
|
|
4
4
|
exports.validateUserAddress = validateUserAddress;
|
|
5
5
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
@@ -211,43 +211,6 @@ const validateRepayShares = (params) => {
|
|
|
211
211
|
}
|
|
212
212
|
};
|
|
213
213
|
exports.validateRepayShares = validateRepayShares;
|
|
214
|
-
/**
|
|
215
|
-
* Validates the common repay input parameters shared by `blueRepay`
|
|
216
|
-
* and `blueRepayWithdrawCollateral`.
|
|
217
|
-
*
|
|
218
|
-
* @param params - Validation parameters.
|
|
219
|
-
* @param params.assets - Repay assets amount (0n when repaying by shares).
|
|
220
|
-
* @param params.shares - Repay shares amount (0n when repaying by assets).
|
|
221
|
-
* @param params.transferAmount - ERC20 amount to transfer to GeneralAdapter1.
|
|
222
|
-
* @param params.maxSharePrice - Maximum repay share price (in ray). Must be positive.
|
|
223
|
-
* @param params.marketId - The market identifier (for error messages).
|
|
224
|
-
*/
|
|
225
|
-
const validateRepayParams = (params) => {
|
|
226
|
-
const { assets, shares, transferAmount, maxSharePrice, marketId } = params;
|
|
227
|
-
if (maxSharePrice <= 0n) {
|
|
228
|
-
throw new index_js_1.NonPositiveRepayMaxSharePriceError(marketId);
|
|
229
|
-
}
|
|
230
|
-
if (assets < 0n || shares < 0n) {
|
|
231
|
-
throw new index_js_1.NonPositiveRepayAmountError(marketId);
|
|
232
|
-
}
|
|
233
|
-
if (assets > 0n && shares > 0n) {
|
|
234
|
-
throw new index_js_1.MutuallyExclusiveRepayAmountsError(marketId);
|
|
235
|
-
}
|
|
236
|
-
if (assets === 0n && shares === 0n) {
|
|
237
|
-
throw new index_js_1.NonPositiveRepayAmountError(marketId);
|
|
238
|
-
}
|
|
239
|
-
if (transferAmount <= 0n) {
|
|
240
|
-
throw new index_js_1.NonPositiveTransferAmountError(marketId);
|
|
241
|
-
}
|
|
242
|
-
if (assets > 0n && transferAmount !== assets) {
|
|
243
|
-
throw new index_js_1.TransferAmountNotEqualToAssetsError({
|
|
244
|
-
transferAmount,
|
|
245
|
-
assets,
|
|
246
|
-
market: marketId,
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
exports.validateRepayParams = validateRepayParams;
|
|
251
214
|
/**
|
|
252
215
|
* Validates that vault reallocations are well-formed.
|
|
253
216
|
*
|
|
@@ -132,6 +132,8 @@ export interface BlueRepayAction extends BaseAction<"blueRepay", {
|
|
|
132
132
|
onBehalf: Address;
|
|
133
133
|
receiver: Address;
|
|
134
134
|
maxSharePrice: bigint;
|
|
135
|
+
/** Native token wrapped into wNative to fund the repay. Present when `> 0n`. */
|
|
136
|
+
nativeAmount?: bigint;
|
|
135
137
|
}> {
|
|
136
138
|
}
|
|
137
139
|
export interface BlueWithdrawCollateralAction extends BaseAction<"blueWithdrawCollateral", {
|
|
@@ -150,6 +152,8 @@ export interface BlueRepayWithdrawCollateralAction extends BaseAction<"blueRepay
|
|
|
150
152
|
maxSharePrice: bigint;
|
|
151
153
|
onBehalf: Address;
|
|
152
154
|
receiver: Address;
|
|
155
|
+
/** Native token wrapped into wNative to fund the repay. Present when `> 0n`. */
|
|
156
|
+
nativeAmount?: bigint;
|
|
153
157
|
}> {
|
|
154
158
|
}
|
|
155
159
|
export interface BlueRefinanceAction extends BaseAction<"blueRefinance", {
|
|
@@ -171,15 +175,59 @@ export interface BlueRefinanceAction extends BaseAction<"blueRefinance", {
|
|
|
171
175
|
* - `shares`: operate on an exact share count (typical for full position closes,
|
|
172
176
|
* immune to interest accrual between tx construction and execution).
|
|
173
177
|
*
|
|
174
|
-
* Used by
|
|
178
|
+
* Used by withdraw (asserts on supply side). Repay uses {@link RepayAmountArgs},
|
|
179
|
+
* which additionally supports native wrapping.
|
|
175
180
|
*/
|
|
176
181
|
export type AssetsOrSharesArgs = {
|
|
177
182
|
assets: bigint;
|
|
178
183
|
} | {
|
|
179
184
|
shares: bigint;
|
|
180
185
|
};
|
|
181
|
-
/**
|
|
182
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Repay funding sources for the **entity layer** (`MorphoBlue.repay` /
|
|
188
|
+
* `MorphoBlue.repayWithdrawCollateral`), which computes the loan-token
|
|
189
|
+
* `transferAmount` itself from live market state.
|
|
190
|
+
*
|
|
191
|
+
* - **assets mode** ({@link DepositAmountArgs}): repay an exact asset total of
|
|
192
|
+
* `amount` (ERC-20) + `nativeAmount` (wrapped native). Additive — mirrors `blueSupply`.
|
|
193
|
+
* - **shares mode** (`{ shares }`): repay an exact borrow-share count (full close,
|
|
194
|
+
* immune to interest accrual). `nativeAmount` funds part of the transfer.
|
|
195
|
+
*
|
|
196
|
+
* `nativeAmount` requires the market's loan token to be the chain's wNative.
|
|
197
|
+
*/
|
|
198
|
+
export type RepayAmountArgs = DepositAmountArgs | {
|
|
199
|
+
shares: bigint;
|
|
200
|
+
nativeAmount?: bigint;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Repay funding sources for the **action layer** (`blueRepay` /
|
|
204
|
+
* `blueRepayWithdrawCollateral`) — a flat, pre-resolved shape. The entity layer
|
|
205
|
+
* ({@link RepayAmountArgs}) derives these from live market state; the action does no
|
|
206
|
+
* amount arithmetic. The mode is discriminated on `shares`:
|
|
207
|
+
*
|
|
208
|
+
* - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets
|
|
209
|
+
* (`= amount + nativeAmount`, additive like `blueSupply`), pulling `amount` ERC-20
|
|
210
|
+
* and wrapping `nativeAmount`. No residual.
|
|
211
|
+
* - **shares mode** (`shares > 0n`): repays an exact borrow-share count (full close),
|
|
212
|
+
* pulling `transferAmount` ERC-20 (already net of native) and wrapping `nativeAmount`;
|
|
213
|
+
* the residual loan token is skimmed back to `receiver`.
|
|
214
|
+
*
|
|
215
|
+
* `nativeAmount` requires the market's loan token to be the chain's wNative.
|
|
216
|
+
*/
|
|
217
|
+
export interface RepayActionAmountArgs {
|
|
218
|
+
/** Assets-mode ERC-20 loan tokens pulled from the payer. Omit (or `0n`) in shares mode. */
|
|
219
|
+
amount?: bigint;
|
|
220
|
+
/** Shares-mode borrow shares to repay (full close). Omit (or `0n`) in assets mode. */
|
|
221
|
+
shares?: bigint;
|
|
222
|
+
/** Native ETH wrapped into wNative to help fund the repay. Loan token must be wNative. */
|
|
223
|
+
nativeAmount?: bigint;
|
|
224
|
+
/**
|
|
225
|
+
* Loan tokens routed into `GeneralAdapter1`. Assets mode: the total repaid
|
|
226
|
+
* (`amount + nativeAmount`). Shares mode: the ERC-20 pulled
|
|
227
|
+
* (`toBorrowAssets(shares) − nativeAmount`).
|
|
228
|
+
*/
|
|
229
|
+
transferAmount: bigint;
|
|
230
|
+
}
|
|
183
231
|
/** Metadata for a Blue authorization prerequisite transaction. */
|
|
184
232
|
export interface BlueAuthorizationAction extends BaseAction<"blueAuthorization", {
|
|
185
233
|
authorized: Address;
|
package/lib/cjs/types/error.d.ts
CHANGED
|
@@ -244,11 +244,22 @@ export declare class ReallocationWithdrawalOnTargetMarketError extends Error {
|
|
|
244
244
|
export declare class UnsortedReallocationWithdrawalsError extends Error {
|
|
245
245
|
constructor(vault: string, marketId: string);
|
|
246
246
|
}
|
|
247
|
-
/**
|
|
247
|
+
/**
|
|
248
|
+
* Thrown when a market repay's `transferAmount` is zero or negative.
|
|
249
|
+
*
|
|
250
|
+
* @deprecated No longer thrown. The repay action layer no longer validates `transferAmount`
|
|
251
|
+
* directly (assets mode uses {@link NonPositiveRepayAmountError}; shares mode allows a zero
|
|
252
|
+
* ERC-20 transfer for fully-native repays). Retained as exported API for back-compat; slated
|
|
253
|
+
* for removal in a future major.
|
|
254
|
+
*/
|
|
248
255
|
export declare class NonPositiveTransferAmountError extends Error {
|
|
249
256
|
constructor(market: string);
|
|
250
257
|
}
|
|
251
|
-
/**
|
|
258
|
+
/**
|
|
259
|
+
* Thrown when a market repay in assets mode has `transferAmount !== amount + nativeAmount` — the
|
|
260
|
+
* pre-resolved ERC-20 pull plus the wrapped native must equal the assets repaid, so the bundle
|
|
261
|
+
* neither strands over-pulled loan tokens on `GeneralAdapter1` nor under-funds the repay.
|
|
262
|
+
*/
|
|
252
263
|
export declare class TransferAmountNotEqualToAssetsError extends Error {
|
|
253
264
|
constructor(params: {
|
|
254
265
|
transferAmount: bigint;
|
|
@@ -256,6 +267,17 @@ export declare class TransferAmountNotEqualToAssetsError extends Error {
|
|
|
256
267
|
market: string;
|
|
257
268
|
});
|
|
258
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Thrown when a shares-mode repay's `nativeAmount` exceeds `transferAmount`, which
|
|
272
|
+
* would make the ERC-20 amount to pull (`transferAmount − nativeAmount`) negative.
|
|
273
|
+
*/
|
|
274
|
+
export declare class NativeAmountExceedsTransferAmountError extends Error {
|
|
275
|
+
constructor(params: {
|
|
276
|
+
nativeAmount: bigint;
|
|
277
|
+
transferAmount: bigint;
|
|
278
|
+
market: string;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
259
281
|
/** Thrown when a market repay specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
|
|
260
282
|
export declare class MutuallyExclusiveRepayAmountsError extends Error {
|
|
261
283
|
constructor(market: string);
|
package/lib/cjs/types/error.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.RefinanceSharesMissingBorrowAssetsError = exports.RefinanceExceedsBorrowAssetsError = exports.RefinanceExceedsBorrowSharesError = exports.RefinanceExceedsCollateralError = exports.RefinanceTokenMismatchError = exports.RefinanceSameMarketError = exports.NegativeMaxRepaySharePriceError = exports.BorrowAmountAndSharesExclusiveError = exports.NegativeBorrowSharesError = exports.NegativeMinSharePriceError = exports.VaultAssetMismatchError = exports.ReallocationWithdrawExceedsMarketSupplyError = exports.WithdrawSharesExceedSupplyError = exports.WithdrawExceedsSupplyError = exports.MutuallyExclusiveWithdrawAmountsError = exports.NegativeWithdrawMinSharePriceError = exports.NonPositiveWithdrawAmountError = exports.ZeroSupplyAmountError = exports.NegativeSupplyMaxSharePriceError = exports.NegativeSupplyAmountError = exports.NonPositiveMinBorrowSharePriceError = exports.UnknownReallocationPositionError = exports.UnknownReallocationVaultMarketConfigError = exports.UnknownReallocationVaultError = exports.UnknownReallocationMarketError = exports.InsufficientSharedLiquidityError = void 0;
|
|
3
|
+
exports.MissingPublicAllocatorConfigError = exports.RepaySharesExceedDebtError = exports.InvalidSignatureError = exports.RepayExceedsDebtError = exports.ShareDivideByZeroError = exports.WithdrawMakesPositionUnhealthyError = exports.WithdrawExceedsCollateralError = exports.NonPositiveWithdrawCollateralAmountError = exports.NonPositiveRepayMaxSharePriceError = exports.NonPositiveRepayAmountError = exports.MutuallyExclusiveRepayAmountsError = exports.NativeAmountExceedsTransferAmountError = exports.TransferAmountNotEqualToAssetsError = exports.NonPositiveTransferAmountError = exports.UnsortedReallocationWithdrawalsError = exports.ReallocationWithdrawalOnTargetMarketError = exports.NonPositiveReallocationAmountError = exports.EmptyReallocationWithdrawalsError = exports.NegativeReallocationFeeError = exports.AccrualPositionUserMismatchError = exports.MarketIdMismatchError = exports.MissingMarketPriceError = exports.BorrowExceedsSafeLtvError = exports.NativeAmountOnNonWNativeCollateralError = exports.NativeAmountOnNonWNativeAssetError = exports.ZeroCollateralAmountError = exports.NonPositiveBorrowAmountError = exports.VaultAddressMismatchError = exports.ZeroDepositAmountError = exports.NegativeNativeAmountError = exports.ChainWNativeMissingError = exports.NativeAmountOnNonWNativeVaultError = exports.Permit2ExpirationMissingError = exports.DepositAssetMismatchError = exports.DepositAmountMismatchError = exports.EmptyDeallocationsError = exports.ExcessiveSlippageToleranceError = exports.MissingAccrualPositionError = exports.NegativeSlippageToleranceError = exports.UnsupportedErc20ApprovalSpenderError = exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.NonPositiveMaxSharePriceError = exports.NonPositiveSharesAmountError = exports.NonPositiveAssetAmountError = exports.UnexpectedRequirementSignatureError = exports.AmbiguousRequirementSignaturesError = exports.BundlerErrors = void 0;
|
|
4
|
+
exports.RefinanceSharesMissingBorrowAssetsError = exports.RefinanceExceedsBorrowAssetsError = exports.RefinanceExceedsBorrowSharesError = exports.RefinanceExceedsCollateralError = exports.RefinanceTokenMismatchError = exports.RefinanceSameMarketError = exports.NegativeMaxRepaySharePriceError = exports.BorrowAmountAndSharesExclusiveError = exports.NegativeBorrowSharesError = exports.NegativeMinSharePriceError = exports.VaultAssetMismatchError = exports.ReallocationWithdrawExceedsMarketSupplyError = exports.WithdrawSharesExceedSupplyError = exports.WithdrawExceedsSupplyError = exports.MutuallyExclusiveWithdrawAmountsError = exports.NegativeWithdrawMinSharePriceError = exports.NonPositiveWithdrawAmountError = exports.ZeroSupplyAmountError = exports.NegativeSupplyMaxSharePriceError = exports.NegativeSupplyAmountError = exports.NonPositiveMinBorrowSharePriceError = exports.UnknownReallocationPositionError = exports.UnknownReallocationVaultMarketConfigError = exports.UnknownReallocationVaultError = exports.UnknownReallocationMarketError = exports.InsufficientSharedLiquidityError = exports.DisabledReallocationMarketError = void 0;
|
|
5
5
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
6
|
/**
|
|
7
7
|
* Typed errors thrown while encoding supported Bundler3 actions.
|
|
@@ -358,20 +358,41 @@ class UnsortedReallocationWithdrawalsError extends Error {
|
|
|
358
358
|
}
|
|
359
359
|
}
|
|
360
360
|
exports.UnsortedReallocationWithdrawalsError = UnsortedReallocationWithdrawalsError;
|
|
361
|
-
/**
|
|
361
|
+
/**
|
|
362
|
+
* Thrown when a market repay's `transferAmount` is zero or negative.
|
|
363
|
+
*
|
|
364
|
+
* @deprecated No longer thrown. The repay action layer no longer validates `transferAmount`
|
|
365
|
+
* directly (assets mode uses {@link NonPositiveRepayAmountError}; shares mode allows a zero
|
|
366
|
+
* ERC-20 transfer for fully-native repays). Retained as exported API for back-compat; slated
|
|
367
|
+
* for removal in a future major.
|
|
368
|
+
*/
|
|
362
369
|
class NonPositiveTransferAmountError extends Error {
|
|
363
370
|
constructor(market) {
|
|
364
371
|
super(`Transfer amount must be positive for market: ${market}`);
|
|
365
372
|
}
|
|
366
373
|
}
|
|
367
374
|
exports.NonPositiveTransferAmountError = NonPositiveTransferAmountError;
|
|
368
|
-
/**
|
|
375
|
+
/**
|
|
376
|
+
* Thrown when a market repay in assets mode has `transferAmount !== amount + nativeAmount` — the
|
|
377
|
+
* pre-resolved ERC-20 pull plus the wrapped native must equal the assets repaid, so the bundle
|
|
378
|
+
* neither strands over-pulled loan tokens on `GeneralAdapter1` nor under-funds the repay.
|
|
379
|
+
*/
|
|
369
380
|
class TransferAmountNotEqualToAssetsError extends Error {
|
|
370
381
|
constructor(params) {
|
|
371
|
-
super(`Transfer amount ${params.transferAmount} is not equal to repay assets ${params.assets} for market: ${params.market}
|
|
382
|
+
super(`Transfer amount ${params.transferAmount} is not equal to repay assets ${params.assets} for market: ${params.market}. In assets mode, transferAmount must equal amount + nativeAmount.`);
|
|
372
383
|
}
|
|
373
384
|
}
|
|
374
385
|
exports.TransferAmountNotEqualToAssetsError = TransferAmountNotEqualToAssetsError;
|
|
386
|
+
/**
|
|
387
|
+
* Thrown when a shares-mode repay's `nativeAmount` exceeds `transferAmount`, which
|
|
388
|
+
* would make the ERC-20 amount to pull (`transferAmount − nativeAmount`) negative.
|
|
389
|
+
*/
|
|
390
|
+
class NativeAmountExceedsTransferAmountError extends Error {
|
|
391
|
+
constructor(params) {
|
|
392
|
+
super(`Native amount ${params.nativeAmount} exceeds transfer amount ${params.transferAmount} for market: ${params.market}. Reduce nativeAmount to at most transferAmount.`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
exports.NativeAmountExceedsTransferAmountError = NativeAmountExceedsTransferAmountError;
|
|
375
396
|
/** Thrown when a market repay specifies both `assets` and `shares` as non-zero (modes are mutually exclusive). */
|
|
376
397
|
class MutuallyExclusiveRepayAmountsError extends Error {
|
|
377
398
|
constructor(market) {
|