@morpho-org/morpho-sdk 3.1.1 → 4.0.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.
- package/README.md +63 -13
- package/lib/cjs/actions/{marketV1 → blue}/borrow.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/borrow.js +8 -8
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/buildReallocationActions.d.ts +1 -1
- package/lib/cjs/actions/{marketV1 → blue}/buildReallocationActions.js +1 -1
- package/lib/cjs/actions/{marketV1 → blue}/index.d.ts +1 -0
- package/lib/cjs/actions/{marketV1 → blue}/index.js +1 -0
- package/lib/cjs/actions/blue/refinance.d.ts +117 -0
- package/lib/cjs/actions/blue/refinance.js +218 -0
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/repay.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/repay.js +8 -8
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/repayWithdrawCollateral.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/repayWithdrawCollateral.js +8 -8
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/supply.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/supply.js +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/supplyCollateral.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/supplyCollateral.js +8 -8
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/supplyCollateralBorrow.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/supplyCollateralBorrow.js +8 -8
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/withdraw.d.ts +11 -11
- package/lib/cjs/actions/{marketV1 → blue}/withdraw.js +10 -10
- package/lib/{esm/actions/marketV1 → cjs/actions/blue}/withdrawCollateral.d.ts +8 -8
- package/lib/cjs/actions/{marketV1 → blue}/withdrawCollateral.js +8 -8
- package/lib/cjs/actions/index.d.ts +2 -2
- package/lib/cjs/actions/index.js +1 -1
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.d.ts +1 -1
- package/lib/cjs/actions/requirements/getMorphoAuthorizationRequirement.js +1 -1
- package/lib/cjs/client/index.d.ts +0 -1
- package/lib/cjs/client/index.js +0 -1
- package/lib/cjs/client/morphoViemExtension.d.ts +17 -11
- package/lib/cjs/client/morphoViemExtension.js +55 -10
- package/lib/cjs/entities/{marketV1/marketV1.d.ts → blue/blue.d.ts} +76 -21
- package/lib/cjs/entities/{marketV1/marketV1.js → blue/blue.js} +169 -11
- package/lib/cjs/entities/blue/index.d.ts +1 -0
- package/lib/cjs/entities/{marketV1 → blue}/index.js +1 -1
- package/lib/cjs/entities/index.d.ts +1 -1
- package/lib/cjs/entities/index.js +3 -3
- package/lib/cjs/entities/reallocationData.d.ts +1 -1
- package/lib/cjs/entities/reallocationData.js +1 -1
- package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
- package/lib/cjs/helpers/computeReallocations.d.ts +1 -1
- package/lib/cjs/helpers/computeReallocations.js +1 -1
- package/lib/cjs/helpers/validate.d.ts +2 -2
- package/lib/cjs/helpers/validate.js +2 -2
- package/lib/cjs/types/action.d.ts +21 -9
- package/lib/cjs/types/client.d.ts +5 -4
- package/lib/cjs/types/error.d.ts +57 -0
- package/lib/cjs/types/error.js +82 -1
- package/lib/esm/actions/{marketV1 → blue}/borrow.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/borrow.js +6 -6
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/buildReallocationActions.d.ts +1 -1
- package/lib/esm/actions/{marketV1 → blue}/buildReallocationActions.js +1 -1
- package/lib/esm/actions/{marketV1 → blue}/index.d.ts +1 -0
- package/lib/esm/actions/{marketV1 → blue}/index.js +1 -0
- package/lib/esm/actions/blue/refinance.d.ts +117 -0
- package/lib/esm/actions/blue/refinance.js +214 -0
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/repay.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/repay.js +6 -6
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/repayWithdrawCollateral.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/repayWithdrawCollateral.js +6 -6
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/supply.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/supply.js +6 -6
- package/lib/esm/actions/{marketV1 → blue}/supplyCollateral.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/supplyCollateral.js +6 -6
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/supplyCollateralBorrow.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/supplyCollateralBorrow.js +6 -6
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/withdraw.d.ts +11 -11
- package/lib/esm/actions/{marketV1 → blue}/withdraw.js +8 -8
- package/lib/{cjs/actions/marketV1 → esm/actions/blue}/withdrawCollateral.d.ts +8 -8
- package/lib/esm/actions/{marketV1 → blue}/withdrawCollateral.js +6 -6
- package/lib/esm/actions/index.d.ts +2 -2
- package/lib/esm/actions/index.js +1 -1
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.d.ts +1 -1
- package/lib/esm/actions/requirements/getMorphoAuthorizationRequirement.js +1 -1
- package/lib/esm/client/index.d.ts +0 -1
- package/lib/esm/client/index.js +0 -1
- package/lib/esm/client/morphoViemExtension.d.ts +17 -11
- package/lib/esm/client/morphoViemExtension.js +55 -10
- package/lib/esm/entities/{marketV1/marketV1.d.ts → blue/blue.d.ts} +76 -21
- package/lib/esm/entities/{marketV1/marketV1.js → blue/blue.js} +170 -12
- package/lib/esm/entities/blue/index.d.ts +1 -0
- package/lib/esm/entities/blue/index.js +1 -0
- package/lib/esm/entities/index.d.ts +1 -1
- package/lib/esm/entities/index.js +1 -1
- package/lib/esm/entities/reallocationData.d.ts +1 -1
- package/lib/esm/entities/reallocationData.js +1 -1
- package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
- package/lib/esm/helpers/computeReallocations.d.ts +1 -1
- package/lib/esm/helpers/computeReallocations.js +1 -1
- package/lib/esm/helpers/validate.d.ts +2 -2
- package/lib/esm/helpers/validate.js +2 -2
- package/lib/esm/types/action.d.ts +21 -9
- package/lib/esm/types/client.d.ts +5 -4
- package/lib/esm/types/error.d.ts +57 -0
- package/lib/esm/types/error.js +72 -0
- package/package.json +6 -6
- package/lib/cjs/client/morphoClient.d.ts +0 -152
- package/lib/cjs/client/morphoClient.js +0 -162
- package/lib/cjs/entities/marketV1/index.d.ts +0 -1
- package/lib/esm/client/morphoClient.d.ts +0 -152
- package/lib/esm/client/morphoClient.js +0 -158
- package/lib/esm/entities/marketV1/index.d.ts +0 -1
- package/lib/esm/entities/marketV1/index.js +0 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
> **The abstraction layer that simplifies Morpho protocol**
|
|
11
11
|
|
|
12
|
-
Build transactions for **VaultV1** (MetaMorpho), **VaultV2**, and **
|
|
12
|
+
Build transactions for **VaultV1** (MetaMorpho), **VaultV2**, and **Blue** (Morpho Blue) on EVM-compatible chains.
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
@@ -31,7 +31,7 @@ pnpm add @morpho-org/morpho-sdk
|
|
|
31
31
|
| **VaultV1** | `deposit` | Bundler (general adapter) | Same ERC-4626 inflation attack prevention as V2. Supports native token wrapping. |
|
|
32
32
|
| | `withdraw` | Direct vault call | No attack surface |
|
|
33
33
|
| | `redeem` | Direct vault call | No attack surface |
|
|
34
|
-
| **
|
|
34
|
+
| **Blue** | `supply` | Bundler (general adapter) | `erc20TransferFrom` + `morphoSupply` with `maxSharePrice` (inflation guard). Supports native wrapping when `loanToken === wNative`. |
|
|
35
35
|
| | `supplyCollateral` | Bundler (general adapter) | `erc20TransferFrom` + `morphoSupplyCollateral`. Supports native wrapping. |
|
|
36
36
|
| | `borrow` | Bundler (general adapter) | `morphoBorrow` with `minSharePrice` slippage protection. Requires GA1 auth. Supports reallocations. |
|
|
37
37
|
| | `supplyCollateralBorrow` | Bundler (general adapter) | Atomic supply + borrow. LLTV buffer prevents instant liquidation. Supports reallocations. |
|
|
@@ -39,6 +39,7 @@ pnpm add @morpho-org/morpho-sdk
|
|
|
39
39
|
| | `withdraw` | Bundler (general adapter) | `morphoWithdraw` with `minSharePrice` slippage protection. Requires GA1 auth. Supports reallocations. |
|
|
40
40
|
| | `withdrawCollateral` | Direct Morpho call | No bundler overhead. Validates position health after withdrawal. |
|
|
41
41
|
| | `repayWithdrawCollateral` | Bundler (general adapter) | Atomic repay + withdraw. Bundle order matters: repay first, then withdraw. |
|
|
42
|
+
| | `refinance` | Bundler (general adapter) | Atomic position migration to another market with the same loan + collateral tokens. Flash-collateral via the target's `onMorphoSupplyCollateral` callback: borrow target → repay source → withdraw source collateral. Requires GA1 auth. Supports target reallocations. |
|
|
42
43
|
|
|
43
44
|
### The `getRequirements` flow
|
|
44
45
|
|
|
@@ -83,11 +84,12 @@ const tx = buildTx(permitSignature);
|
|
|
83
84
|
| | `withdraw` | Direct vault call | No attack surface |
|
|
84
85
|
| | `redeem` | Direct vault call | No attack surface |
|
|
85
86
|
| | `migrateToV2` | Bundler (general adapter) | Atomic V1 → V2 migration: redeem V1 shares + deposit into V2 in one tx. Slippage-protected. |
|
|
86
|
-
| **
|
|
87
|
+
| **Blue** | `supply` | Bundler (general adapter) | `erc20TransferFrom` + `morphoSupply` with `maxSharePrice` (inflation guard). Supports native wrapping when `loanToken === wNative`. |
|
|
87
88
|
| | `supplyCollateral` | Bundler (general adapter) | `erc20TransferFrom` + `morphoSupplyCollateral`. Supports native wrapping. |
|
|
88
89
|
| | `borrow` | Bundler (general adapter) | `morphoBorrow` with `minSharePrice` slippage protection. Requires GA1 auth. Supports reallocations. |
|
|
89
90
|
| | `supplyCollateralBorrow` | Bundler (general adapter) | Atomic supply + borrow. LLTV buffer prevents instant liquidation. Supports reallocations. |
|
|
90
91
|
| | `withdraw` | Bundler (general adapter) | `morphoWithdraw` with `minSharePrice` slippage protection. Requires GA1 auth. Supports reallocations. |
|
|
92
|
+
| | `refinance` | Bundler (general adapter) | Atomic position migration to another market sharing the same loan + collateral tokens. Requires GA1 auth. Supports reallocations. |
|
|
91
93
|
|
|
92
94
|
### VaultV2
|
|
93
95
|
|
|
@@ -241,10 +243,13 @@ const requirements = await getRequirements();
|
|
|
241
243
|
const tx = buildTx(requirementSignature);
|
|
242
244
|
```
|
|
243
245
|
|
|
244
|
-
###
|
|
246
|
+
### Blue
|
|
247
|
+
|
|
248
|
+
Blue (Morpho Blue) is Morpho's immutable, variable-rate lending primitive: isolated markets whose
|
|
249
|
+
borrow rate floats with utilization. Each market is identified by its `MarketParams`.
|
|
245
250
|
|
|
246
251
|
```typescript
|
|
247
|
-
const market = client.morpho.
|
|
252
|
+
const market = client.morpho.blue(
|
|
248
253
|
{
|
|
249
254
|
loanToken: "0xLoan...",
|
|
250
255
|
collateralToken: "0xCollateral...",
|
|
@@ -418,6 +423,49 @@ const tx = buildTx(requirementSignature);
|
|
|
418
423
|
|
|
419
424
|
Atomically bundles repay → withdraw collateral via bundler3. Bundle order is critical: repay runs first to reduce debt, then withdraw. Requires both a loan token approval (for repay) and a Morpho authorization (for withdraw). The SDK validates combined position health by simulating the repay before checking withdrawal safety.
|
|
420
425
|
|
|
426
|
+
#### Refinance
|
|
427
|
+
|
|
428
|
+
Atomically migrate a position from this market to another Morpho Blue market that shares the **same loan and collateral tokens** on the same chain — no flash loan, no upfront capital. The bundle flash-collateralizes the target via its `onMorphoSupplyCollateral` callback: inside the callback `GeneralAdapter1` borrows on the target, repays the source debt, then withdraws the source collateral to settle the deferred transfer.
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
const source = client.morpho.blue(sourceMarketParams, 1);
|
|
432
|
+
const target = client.morpho.blue(targetMarketParams, 1);
|
|
433
|
+
|
|
434
|
+
const positionData = await source.getPositionData("0xUser...");
|
|
435
|
+
const targetPositionData = await target.getPositionData("0xUser...");
|
|
436
|
+
|
|
437
|
+
// Refinance by assets — exact-asset borrow and repay, no GA1 dust
|
|
438
|
+
const { buildTx, getRequirements } = source.refinance({
|
|
439
|
+
userAddress: "0xUser...",
|
|
440
|
+
positionData,
|
|
441
|
+
target: { marketParams: targetMarketParams, positionData: targetPositionData },
|
|
442
|
+
collateralAmount: 1000000000000000000n,
|
|
443
|
+
borrowAssets: 500000000000000000n,
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// Or migrate the full debt by shares (immune to interest accrual between quote and inclusion)
|
|
447
|
+
const { buildTx, getRequirements } = source.refinance({
|
|
448
|
+
userAddress: "0xUser...",
|
|
449
|
+
positionData,
|
|
450
|
+
target: { marketParams: targetMarketParams, positionData: targetPositionData },
|
|
451
|
+
collateralAmount: 1000000000000000000n,
|
|
452
|
+
borrowShares: positionData.borrowShares,
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// Collateral-only migration — omit both borrow fields
|
|
456
|
+
const { buildTx, getRequirements } = source.refinance({
|
|
457
|
+
userAddress: "0xUser...",
|
|
458
|
+
positionData,
|
|
459
|
+
target: { marketParams: targetMarketParams, positionData: targetPositionData },
|
|
460
|
+
collateralAmount: 1000000000000000000n,
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const requirements = await getRequirements();
|
|
464
|
+
const tx = buildTx();
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
The SDK validates ownership, token/id match, and that amounts do not exceed the source position. Health is checked against `LLTV − buffer` where it can degrade: the residual source position is validated whenever debt remains after the repay, and the aggregate target position is validated whenever a borrow leg is migrated. Collateral-only migrations (both borrow fields omitted) skip the target health check — they can't degrade target health and would otherwise fail on missing-oracle target markets. Both markets are forward-accrued to `now`; in shares mode the target borrow overshoots by `slippageTolerance` and the callback sweeps the residual back into the target debt (or skims it to the user). `getRequirements` returns the `setAuthorization(generalAdapter1, true)` transaction when GA1 is not yet authorized — a single global authorization covers both markets. Optional `targetReallocations` top up target-market liquidity via the **PublicAllocator** (same mechanism as `borrow`); their fees add to `tx.value`.
|
|
468
|
+
|
|
421
469
|
#### Borrow with Shared Liquidity (Reallocations)
|
|
422
470
|
|
|
423
471
|
When a market lacks sufficient liquidity, you can reallocate liquidity from other markets managed by MetaMorpho Vaults via the **PublicAllocator** contract:
|
|
@@ -473,7 +521,7 @@ graph LR
|
|
|
473
521
|
|
|
474
522
|
MC -->|.vaultV1| MV1
|
|
475
523
|
MC -->|.vaultV2| MV2
|
|
476
|
-
MC -->|.
|
|
524
|
+
MC -->|.blue| MM1
|
|
477
525
|
|
|
478
526
|
subgraph VaultV1 Flow
|
|
479
527
|
MV1[MorphoVaultV1]
|
|
@@ -503,19 +551,21 @@ graph LR
|
|
|
503
551
|
V2FR -->|multicall| V2C
|
|
504
552
|
end
|
|
505
553
|
|
|
506
|
-
subgraph
|
|
507
|
-
MM1[
|
|
508
|
-
MM1 --> M1S[
|
|
509
|
-
MM1 --> M1SC[
|
|
510
|
-
MM1 --> M1B[
|
|
511
|
-
MM1 --> M1SCB[
|
|
512
|
-
MM1 --> M1W[
|
|
554
|
+
subgraph Blue Flow
|
|
555
|
+
MM1[MorphoBlue]
|
|
556
|
+
MM1 --> M1S[blueSupply]
|
|
557
|
+
MM1 --> M1SC[blueSupplyCollateral]
|
|
558
|
+
MM1 --> M1B[blueBorrow]
|
|
559
|
+
MM1 --> M1SCB[blueSupplyCollateralBorrow]
|
|
560
|
+
MM1 --> M1W[blueWithdraw]
|
|
561
|
+
MM1 --> M1RF[blueRefinance]
|
|
513
562
|
|
|
514
563
|
M1S -->|nativeWrap? + erc20TransferFrom + morphoSupply| B3[Bundler3]
|
|
515
564
|
M1SC -->|erc20TransferFrom + morphoSupplyCollateral| B3
|
|
516
565
|
M1B -->|reallocateTo? + morphoBorrow| B3
|
|
517
566
|
M1SCB -->|transfer + supplyCollateral + reallocateTo? + borrow| B3
|
|
518
567
|
M1W -->|reallocateTo? + morphoWithdraw| B3
|
|
568
|
+
M1RF -->|reallocateTo? + supplyCollateral callback: borrow + repay + withdrawCollateral| B3
|
|
519
569
|
|
|
520
570
|
B3 -.->|reallocateTo| PA[PublicAllocator]
|
|
521
571
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { Address } from "viem";
|
|
3
|
-
import { type
|
|
4
|
-
/** Parameters for {@link
|
|
5
|
-
export interface
|
|
3
|
+
import { type BlueBorrowAction, type Metadata, type Transaction, type VaultReallocation } from "../../types/index.js";
|
|
4
|
+
/** Parameters for {@link blueBorrow}. */
|
|
5
|
+
export interface BlueBorrowParams {
|
|
6
6
|
market: {
|
|
7
7
|
readonly chainId: number;
|
|
8
8
|
readonly marketParams: MarketParams;
|
|
@@ -36,7 +36,7 @@ export interface MarketV1BorrowParams {
|
|
|
36
36
|
* @param params.args.reallocations - Optional vault reallocations to execute before borrowing,
|
|
37
37
|
* computed by the entity layer.
|
|
38
38
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
39
|
-
* @returns A deep-frozen `Transaction<
|
|
39
|
+
* @returns A deep-frozen `Transaction<BlueBorrowAction>` with `to`, `value`, `data`, and the
|
|
40
40
|
* typed `action` discriminator the simulation layer consumes.
|
|
41
41
|
* @throws {NonPositiveBorrowAmountError} when `amount <= 0n`.
|
|
42
42
|
* @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n` (negative; zero is
|
|
@@ -53,9 +53,9 @@ export interface MarketV1BorrowParams {
|
|
|
53
53
|
* reallocation withdrawals are not strictly sorted by market id.
|
|
54
54
|
* @example
|
|
55
55
|
* ```ts
|
|
56
|
-
* import {
|
|
56
|
+
* import { blueBorrow } from "@morpho-org/morpho-sdk";
|
|
57
57
|
*
|
|
58
|
-
* const tx =
|
|
58
|
+
* const tx = blueBorrow({
|
|
59
59
|
* market: { chainId: 1, marketParams },
|
|
60
60
|
* args: {
|
|
61
61
|
* amount: 1_000_000n,
|
|
@@ -63,7 +63,7 @@ export interface MarketV1BorrowParams {
|
|
|
63
63
|
* minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
|
|
64
64
|
* },
|
|
65
65
|
* });
|
|
66
|
-
* // tx satisfies Readonly<Transaction<
|
|
66
|
+
* // tx satisfies Readonly<Transaction<BlueBorrowAction>>
|
|
67
67
|
* ```
|
|
68
68
|
*/
|
|
69
|
-
export declare const
|
|
69
|
+
export declare const blueBorrow: ({ market: { chainId, marketParams }, args: { amount, receiver, minSharePrice, reallocations }, metadata, }: BlueBorrowParams) => Readonly<Transaction<BlueBorrowAction>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.blueBorrow = void 0;
|
|
4
4
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
5
|
const index_js_1 = require("../../bundler/index.js");
|
|
6
6
|
const index_js_2 = require("../../helpers/index.js");
|
|
@@ -25,7 +25,7 @@ const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
|
25
25
|
* @param params.args.reallocations - Optional vault reallocations to execute before borrowing,
|
|
26
26
|
* computed by the entity layer.
|
|
27
27
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
28
|
-
* @returns A deep-frozen `Transaction<
|
|
28
|
+
* @returns A deep-frozen `Transaction<BlueBorrowAction>` with `to`, `value`, `data`, and the
|
|
29
29
|
* typed `action` discriminator the simulation layer consumes.
|
|
30
30
|
* @throws {NonPositiveBorrowAmountError} when `amount <= 0n`.
|
|
31
31
|
* @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n` (negative; zero is
|
|
@@ -42,9 +42,9 @@ const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
|
42
42
|
* reallocation withdrawals are not strictly sorted by market id.
|
|
43
43
|
* @example
|
|
44
44
|
* ```ts
|
|
45
|
-
* import {
|
|
45
|
+
* import { blueBorrow } from "@morpho-org/morpho-sdk";
|
|
46
46
|
*
|
|
47
|
-
* const tx =
|
|
47
|
+
* const tx = blueBorrow({
|
|
48
48
|
* market: { chainId: 1, marketParams },
|
|
49
49
|
* args: {
|
|
50
50
|
* amount: 1_000_000n,
|
|
@@ -52,10 +52,10 @@ const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
|
52
52
|
* minSharePrice: 0n, // disables slippage protection — production code should compute via `computeMinBorrowSharePrice` from market state + slippage tolerance
|
|
53
53
|
* },
|
|
54
54
|
* });
|
|
55
|
-
* // tx satisfies Readonly<Transaction<
|
|
55
|
+
* // tx satisfies Readonly<Transaction<BlueBorrowAction>>
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
const
|
|
58
|
+
const blueBorrow = ({ market: { chainId, marketParams }, args: { amount, receiver, minSharePrice, reallocations }, metadata, }) => {
|
|
59
59
|
if (amount <= 0n) {
|
|
60
60
|
throw new index_js_3.NonPositiveBorrowAmountError(marketParams.id);
|
|
61
61
|
}
|
|
@@ -80,7 +80,7 @@ const marketV1Borrow = ({ market: { chainId, marketParams }, args: { amount, rec
|
|
|
80
80
|
return (0, morpho_ts_1.deepFreeze)({
|
|
81
81
|
...tx,
|
|
82
82
|
action: {
|
|
83
|
-
type: "
|
|
83
|
+
type: "blueBorrow",
|
|
84
84
|
args: {
|
|
85
85
|
market: marketParams.id,
|
|
86
86
|
amount,
|
|
@@ -91,4 +91,4 @@ const marketV1Borrow = ({ market: { chainId, marketParams }, args: { amount, rec
|
|
|
91
91
|
},
|
|
92
92
|
});
|
|
93
93
|
};
|
|
94
|
-
exports.
|
|
94
|
+
exports.blueBorrow = blueBorrow;
|
|
@@ -7,7 +7,7 @@ import type { VaultReallocation } from "../../types/index.js";
|
|
|
7
7
|
* Validates the reallocations, then encodes each as a `reallocateTo` action against the target
|
|
8
8
|
* market. Returns `{ actions: [], fee: 0n }` for an empty input — callers gate on
|
|
9
9
|
* `reallocations.length > 0` and skip the call entirely in that case. Internal helper —
|
|
10
|
-
* consumed only by `
|
|
10
|
+
* consumed only by `blueBorrow` and `blueSupplyCollateralBorrow`; not re-exported on the
|
|
11
11
|
* public surface.
|
|
12
12
|
*
|
|
13
13
|
* @param reallocations - The vault reallocations to encode.
|
|
@@ -8,7 +8,7 @@ const index_js_1 = require("../../helpers/index.js");
|
|
|
8
8
|
* Validates the reallocations, then encodes each as a `reallocateTo` action against the target
|
|
9
9
|
* market. Returns `{ actions: [], fee: 0n }` for an empty input — callers gate on
|
|
10
10
|
* `reallocations.length > 0` and skip the call entirely in that case. Internal helper —
|
|
11
|
-
* consumed only by `
|
|
11
|
+
* consumed only by `blueBorrow` and `blueSupplyCollateralBorrow`; not re-exported on the
|
|
12
12
|
* public surface.
|
|
13
13
|
*
|
|
14
14
|
* @param reallocations - The vault reallocations to encode.
|
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./borrow.js"), exports);
|
|
18
|
+
__exportStar(require("./refinance.js"), exports);
|
|
18
19
|
__exportStar(require("./repay.js"), exports);
|
|
19
20
|
__exportStar(require("./repayWithdrawCollateral.js"), exports);
|
|
20
21
|
__exportStar(require("./supply.js"), exports);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type BlueRefinanceAction, type Metadata, type Transaction, type VaultReallocation } from "../../types/index.js";
|
|
4
|
+
/** Parameters for {@link blueRefinance}. */
|
|
5
|
+
export interface BlueRefinanceParams {
|
|
6
|
+
source: {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly marketParams: MarketParams;
|
|
9
|
+
};
|
|
10
|
+
target: {
|
|
11
|
+
readonly marketParams: MarketParams;
|
|
12
|
+
};
|
|
13
|
+
args: {
|
|
14
|
+
user: Address;
|
|
15
|
+
collateralAmount: bigint;
|
|
16
|
+
/**
|
|
17
|
+
* Loan assets to borrow on the target. Assets mode: the exact borrow (exclusive with
|
|
18
|
+
* `borrowShares`). Shares mode: the positive overshoot covering accrual + slippage; omitting it
|
|
19
|
+
* throws {@link RefinanceSharesMissingBorrowAssetsError}.
|
|
20
|
+
*/
|
|
21
|
+
borrowAssets?: bigint;
|
|
22
|
+
/** Source borrow shares to repay (immune to mid-tx accrual); exclusive with `borrowAssets`. */
|
|
23
|
+
borrowShares?: bigint;
|
|
24
|
+
/** Minimum borrow share price on the target market (in ray). */
|
|
25
|
+
minBorrowSharePrice: bigint;
|
|
26
|
+
/** Maximum repay share price on the source market (in ray); must be > 0 when a repay leg exists. */
|
|
27
|
+
maxRepaySharePrice: bigint;
|
|
28
|
+
/** PublicAllocator reallocations into the target market, run before the bundle. Fees add to `tx.value`. */
|
|
29
|
+
targetReallocations?: readonly VaultReallocation[];
|
|
30
|
+
};
|
|
31
|
+
metadata?: Metadata;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Prepares an atomic refinance migrating a Morpho Blue position to another market on the same
|
|
35
|
+
* chain that shares the same loan and collateral tokens.
|
|
36
|
+
*
|
|
37
|
+
* Strategy: flash-collateral via the target's `onMorphoSupplyCollateral` callback. The collateral
|
|
38
|
+
* is credited before the deferred `safeTransferFrom`, so inside the callback GA1 borrows on the
|
|
39
|
+
* target, repays the source, then withdraws the source collateral to settle the transfer.
|
|
40
|
+
*
|
|
41
|
+
* Bundle shape (callback contents depend on borrow mode):
|
|
42
|
+
*
|
|
43
|
+
* ```text
|
|
44
|
+
* // optional: one reallocateTo per targetReallocations entry, run first
|
|
45
|
+
* reallocateTo(vault_i, fee_i, withdrawals_i, target, false),
|
|
46
|
+
*
|
|
47
|
+
* morphoSupplyCollateral(target, collateralAmount, user, [
|
|
48
|
+
* // omitted in collat-only mode
|
|
49
|
+
* morphoBorrow(target, borrowAssets, 0, minBorrowSharePrice, GA1),
|
|
50
|
+
* morphoRepay(source, assets|0, 0|shares, maxRepaySharePrice, user, []),
|
|
51
|
+
* // shares mode only: sweep overshoot before the withdraw so same-token markets aren't drained
|
|
52
|
+
* morphoRepay(target, maxUint256, 0, maxUint256, user, [], skipRevert=true),
|
|
53
|
+
* // shares mode only: fallback if the repay is skipped, skim residual loan tokens to the user
|
|
54
|
+
* erc20Transfer(loanToken, user, maxUint256, GA1, skipRevert=false),
|
|
55
|
+
* morphoWithdrawCollateral(source, collateralAmount, GA1),
|
|
56
|
+
* ])
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* Borrow modes:
|
|
60
|
+
*
|
|
61
|
+
* - **Assets mode** (`borrowAssets > 0n`): exact-asset borrow and repay, no GA1 dust.
|
|
62
|
+
* - **Shares mode** (`borrowShares > 0n`, `borrowAssets` is the overshoot): the trailing
|
|
63
|
+
* `morphoRepay(target, maxUint256, …, skipRevert=true)` sweeps the residual into the target debt,
|
|
64
|
+
* then an `erc20Transfer` skims any residual to the user if that repay is skipped.
|
|
65
|
+
* - **Collat-only** (both zero/omitted): only collateral is migrated; borrow/repay legs omitted.
|
|
66
|
+
*
|
|
67
|
+
* Prerequisite: GA1 must be authorized on Morpho — the entity's `getRequirements()` returns the
|
|
68
|
+
* `setAuthorization` transaction when needed.
|
|
69
|
+
*
|
|
70
|
+
* @param params.source.chainId - The chain both markets live on.
|
|
71
|
+
* @param params.source.marketParams - Source market params (the position being closed).
|
|
72
|
+
* @param params.target.marketParams - Target market params; must share both tokens with the source.
|
|
73
|
+
* @param params.args.user - Position owner on both markets.
|
|
74
|
+
* @param params.args.collateralAmount - Amount of collateral to migrate.
|
|
75
|
+
* @param params.args.borrowAssets - Loan assets to borrow on the target; exclusive with `borrowShares`. Defaults to `0n`.
|
|
76
|
+
* @param params.args.borrowShares - Borrow shares to repay on the source; exclusive with `borrowAssets`. Defaults to `0n`.
|
|
77
|
+
* @param params.args.minBorrowSharePrice - Minimum borrow share price (ray) on the target.
|
|
78
|
+
* @param params.args.maxRepaySharePrice - Maximum repay share price (ray) on the source.
|
|
79
|
+
* @param params.args.targetReallocations - PublicAllocator reallocations into the target, run before the supply leg.
|
|
80
|
+
* @param params.metadata - Optional analytics metadata appended to `tx.data`.
|
|
81
|
+
* @returns A deep-frozen `Transaction<BlueRefinanceAction>`.
|
|
82
|
+
* @remarks `borrowAssets` and `borrowShares` describe different markets (target borrow vs. source
|
|
83
|
+
* repay); in shares mode the entity passes both. Caller-facing mutual exclusivity is enforced at the entity layer.
|
|
84
|
+
* @throws {ZeroCollateralAmountError} when `collateralAmount <= 0n`.
|
|
85
|
+
* @throws {NonPositiveAssetAmountError} when `borrowAssets < 0n`.
|
|
86
|
+
* @throws {NegativeBorrowSharesError} when `borrowShares < 0n`.
|
|
87
|
+
* @throws {NonPositiveMinBorrowSharePriceError} when `minBorrowSharePrice < 0n`.
|
|
88
|
+
* @throws {NegativeMaxRepaySharePriceError} when `maxRepaySharePrice < 0n`.
|
|
89
|
+
* @throws {RefinanceSameMarketError} when source and target market ids are equal.
|
|
90
|
+
* @throws {RefinanceTokenMismatchError} when source and target do not share both tokens.
|
|
91
|
+
* @throws {RefinanceSharesMissingBorrowAssetsError} when `borrowShares > 0n` but `borrowAssets` is omitted or non-positive.
|
|
92
|
+
* @throws {NonPositiveRepayMaxSharePriceError} when a repay leg is encoded and `maxRepaySharePrice <= 0n`.
|
|
93
|
+
* @throws {NegativeReallocationFeeError} when any `reallocation.fee < 0n`.
|
|
94
|
+
* @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
|
|
95
|
+
* @throws {NonPositiveReallocationAmountError} when any `reallocation.withdrawals[i].amount <= 0n`.
|
|
96
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references the target market.
|
|
97
|
+
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly sorted by market id.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { blueRefinance } from "@morpho-org/morpho-sdk";
|
|
101
|
+
*
|
|
102
|
+
* const tx = blueRefinance({
|
|
103
|
+
* source: { chainId: 1, marketParams: sourceParams },
|
|
104
|
+
* target: { marketParams: targetParams },
|
|
105
|
+
* args: {
|
|
106
|
+
* user: borrower,
|
|
107
|
+
* collateralAmount: 1_000_000_000_000_000_000n,
|
|
108
|
+
* borrowShares: 500_000_000_000n,
|
|
109
|
+
* borrowAssets: 501_000_000n, // overshoot computed by entity layer
|
|
110
|
+
* minBorrowSharePrice: 0n,
|
|
111
|
+
* maxRepaySharePrice: 1_500_000_000_000_000_000_000_000_000n,
|
|
112
|
+
* },
|
|
113
|
+
* });
|
|
114
|
+
* // tx satisfies Readonly<Transaction<BlueRefinanceAction>>
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare const blueRefinance: ({ source: { chainId, marketParams: sourceParams }, target: { marketParams: targetParams }, args: { user, collateralAmount, borrowAssets, borrowShares, minBorrowSharePrice, maxRepaySharePrice, targetReallocations, }, metadata, }: BlueRefinanceParams) => Readonly<Transaction<BlueRefinanceAction>>;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blueRefinance = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const index_js_1 = require("../../bundler/index.js");
|
|
8
|
+
const index_js_2 = require("../../helpers/index.js");
|
|
9
|
+
const index_js_3 = require("../../types/index.js");
|
|
10
|
+
const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
|
|
11
|
+
/**
|
|
12
|
+
* Prepares an atomic refinance migrating a Morpho Blue position to another market on the same
|
|
13
|
+
* chain that shares the same loan and collateral tokens.
|
|
14
|
+
*
|
|
15
|
+
* Strategy: flash-collateral via the target's `onMorphoSupplyCollateral` callback. The collateral
|
|
16
|
+
* is credited before the deferred `safeTransferFrom`, so inside the callback GA1 borrows on the
|
|
17
|
+
* target, repays the source, then withdraws the source collateral to settle the transfer.
|
|
18
|
+
*
|
|
19
|
+
* Bundle shape (callback contents depend on borrow mode):
|
|
20
|
+
*
|
|
21
|
+
* ```text
|
|
22
|
+
* // optional: one reallocateTo per targetReallocations entry, run first
|
|
23
|
+
* reallocateTo(vault_i, fee_i, withdrawals_i, target, false),
|
|
24
|
+
*
|
|
25
|
+
* morphoSupplyCollateral(target, collateralAmount, user, [
|
|
26
|
+
* // omitted in collat-only mode
|
|
27
|
+
* morphoBorrow(target, borrowAssets, 0, minBorrowSharePrice, GA1),
|
|
28
|
+
* morphoRepay(source, assets|0, 0|shares, maxRepaySharePrice, user, []),
|
|
29
|
+
* // shares mode only: sweep overshoot before the withdraw so same-token markets aren't drained
|
|
30
|
+
* morphoRepay(target, maxUint256, 0, maxUint256, user, [], skipRevert=true),
|
|
31
|
+
* // shares mode only: fallback if the repay is skipped, skim residual loan tokens to the user
|
|
32
|
+
* erc20Transfer(loanToken, user, maxUint256, GA1, skipRevert=false),
|
|
33
|
+
* morphoWithdrawCollateral(source, collateralAmount, GA1),
|
|
34
|
+
* ])
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Borrow modes:
|
|
38
|
+
*
|
|
39
|
+
* - **Assets mode** (`borrowAssets > 0n`): exact-asset borrow and repay, no GA1 dust.
|
|
40
|
+
* - **Shares mode** (`borrowShares > 0n`, `borrowAssets` is the overshoot): the trailing
|
|
41
|
+
* `morphoRepay(target, maxUint256, …, skipRevert=true)` sweeps the residual into the target debt,
|
|
42
|
+
* then an `erc20Transfer` skims any residual to the user if that repay is skipped.
|
|
43
|
+
* - **Collat-only** (both zero/omitted): only collateral is migrated; borrow/repay legs omitted.
|
|
44
|
+
*
|
|
45
|
+
* Prerequisite: GA1 must be authorized on Morpho — the entity's `getRequirements()` returns the
|
|
46
|
+
* `setAuthorization` transaction when needed.
|
|
47
|
+
*
|
|
48
|
+
* @param params.source.chainId - The chain both markets live on.
|
|
49
|
+
* @param params.source.marketParams - Source market params (the position being closed).
|
|
50
|
+
* @param params.target.marketParams - Target market params; must share both tokens with the source.
|
|
51
|
+
* @param params.args.user - Position owner on both markets.
|
|
52
|
+
* @param params.args.collateralAmount - Amount of collateral to migrate.
|
|
53
|
+
* @param params.args.borrowAssets - Loan assets to borrow on the target; exclusive with `borrowShares`. Defaults to `0n`.
|
|
54
|
+
* @param params.args.borrowShares - Borrow shares to repay on the source; exclusive with `borrowAssets`. Defaults to `0n`.
|
|
55
|
+
* @param params.args.minBorrowSharePrice - Minimum borrow share price (ray) on the target.
|
|
56
|
+
* @param params.args.maxRepaySharePrice - Maximum repay share price (ray) on the source.
|
|
57
|
+
* @param params.args.targetReallocations - PublicAllocator reallocations into the target, run before the supply leg.
|
|
58
|
+
* @param params.metadata - Optional analytics metadata appended to `tx.data`.
|
|
59
|
+
* @returns A deep-frozen `Transaction<BlueRefinanceAction>`.
|
|
60
|
+
* @remarks `borrowAssets` and `borrowShares` describe different markets (target borrow vs. source
|
|
61
|
+
* repay); in shares mode the entity passes both. Caller-facing mutual exclusivity is enforced at the entity layer.
|
|
62
|
+
* @throws {ZeroCollateralAmountError} when `collateralAmount <= 0n`.
|
|
63
|
+
* @throws {NonPositiveAssetAmountError} when `borrowAssets < 0n`.
|
|
64
|
+
* @throws {NegativeBorrowSharesError} when `borrowShares < 0n`.
|
|
65
|
+
* @throws {NonPositiveMinBorrowSharePriceError} when `minBorrowSharePrice < 0n`.
|
|
66
|
+
* @throws {NegativeMaxRepaySharePriceError} when `maxRepaySharePrice < 0n`.
|
|
67
|
+
* @throws {RefinanceSameMarketError} when source and target market ids are equal.
|
|
68
|
+
* @throws {RefinanceTokenMismatchError} when source and target do not share both tokens.
|
|
69
|
+
* @throws {RefinanceSharesMissingBorrowAssetsError} when `borrowShares > 0n` but `borrowAssets` is omitted or non-positive.
|
|
70
|
+
* @throws {NonPositiveRepayMaxSharePriceError} when a repay leg is encoded and `maxRepaySharePrice <= 0n`.
|
|
71
|
+
* @throws {NegativeReallocationFeeError} when any `reallocation.fee < 0n`.
|
|
72
|
+
* @throws {EmptyReallocationWithdrawalsError} when any `reallocation.withdrawals` is empty.
|
|
73
|
+
* @throws {NonPositiveReallocationAmountError} when any `reallocation.withdrawals[i].amount <= 0n`.
|
|
74
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when a reallocation withdrawal references the target market.
|
|
75
|
+
* @throws {UnsortedReallocationWithdrawalsError} when reallocation withdrawals are not strictly sorted by market id.
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { blueRefinance } from "@morpho-org/morpho-sdk";
|
|
79
|
+
*
|
|
80
|
+
* const tx = blueRefinance({
|
|
81
|
+
* source: { chainId: 1, marketParams: sourceParams },
|
|
82
|
+
* target: { marketParams: targetParams },
|
|
83
|
+
* args: {
|
|
84
|
+
* user: borrower,
|
|
85
|
+
* collateralAmount: 1_000_000_000_000_000_000n,
|
|
86
|
+
* borrowShares: 500_000_000_000n,
|
|
87
|
+
* borrowAssets: 501_000_000n, // overshoot computed by entity layer
|
|
88
|
+
* minBorrowSharePrice: 0n,
|
|
89
|
+
* maxRepaySharePrice: 1_500_000_000_000_000_000_000_000_000n,
|
|
90
|
+
* },
|
|
91
|
+
* });
|
|
92
|
+
* // tx satisfies Readonly<Transaction<BlueRefinanceAction>>
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
const blueRefinance = ({ source: { chainId, marketParams: sourceParams }, target: { marketParams: targetParams }, args: { user, collateralAmount, borrowAssets = 0n, borrowShares = 0n, minBorrowSharePrice, maxRepaySharePrice, targetReallocations, }, metadata, }) => {
|
|
96
|
+
if (collateralAmount <= 0n) {
|
|
97
|
+
throw new index_js_3.ZeroCollateralAmountError(sourceParams.id);
|
|
98
|
+
}
|
|
99
|
+
if (borrowAssets < 0n) {
|
|
100
|
+
throw new index_js_3.NonPositiveAssetAmountError(sourceParams.loanToken);
|
|
101
|
+
}
|
|
102
|
+
if (borrowShares < 0n) {
|
|
103
|
+
throw new index_js_3.NegativeBorrowSharesError(sourceParams.id);
|
|
104
|
+
}
|
|
105
|
+
if (minBorrowSharePrice < 0n) {
|
|
106
|
+
throw new index_js_3.NonPositiveMinBorrowSharePriceError(targetParams.id);
|
|
107
|
+
}
|
|
108
|
+
if (maxRepaySharePrice < 0n) {
|
|
109
|
+
throw new index_js_3.NegativeMaxRepaySharePriceError(sourceParams.id);
|
|
110
|
+
}
|
|
111
|
+
if (sourceParams.id === targetParams.id) {
|
|
112
|
+
throw new index_js_3.RefinanceSameMarketError(sourceParams.id);
|
|
113
|
+
}
|
|
114
|
+
if (!(0, viem_1.isAddressEqual)(sourceParams.collateralToken, targetParams.collateralToken) ||
|
|
115
|
+
!(0, viem_1.isAddressEqual)(sourceParams.loanToken, targetParams.loanToken)) {
|
|
116
|
+
throw new index_js_3.RefinanceTokenMismatchError(sourceParams.id, targetParams.id);
|
|
117
|
+
}
|
|
118
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
119
|
+
const sharesMode = borrowShares > 0n;
|
|
120
|
+
const shouldMigrateBorrow = borrowAssets > 0n || sharesMode;
|
|
121
|
+
// Shares mode borrows in assets; Morpho rejects a zero borrow, so require a positive overshoot.
|
|
122
|
+
if (sharesMode && borrowAssets <= 0n) {
|
|
123
|
+
throw new index_js_3.RefinanceSharesMissingBorrowAssetsError(sourceParams.id);
|
|
124
|
+
}
|
|
125
|
+
// A repay leg with maxRepaySharePrice = 0n always reverts; require a positive cap when debt is migrated.
|
|
126
|
+
if (shouldMigrateBorrow && maxRepaySharePrice <= 0n) {
|
|
127
|
+
throw new index_js_3.NonPositiveRepayMaxSharePriceError(sourceParams.id);
|
|
128
|
+
}
|
|
129
|
+
const callback = [];
|
|
130
|
+
if (shouldMigrateBorrow) {
|
|
131
|
+
callback.push({
|
|
132
|
+
type: "morphoBorrow",
|
|
133
|
+
args: [
|
|
134
|
+
targetParams,
|
|
135
|
+
borrowAssets,
|
|
136
|
+
0n,
|
|
137
|
+
minBorrowSharePrice,
|
|
138
|
+
generalAdapter1,
|
|
139
|
+
false,
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
callback.push(sharesMode
|
|
143
|
+
? {
|
|
144
|
+
type: "morphoRepay",
|
|
145
|
+
args: [
|
|
146
|
+
sourceParams,
|
|
147
|
+
0n,
|
|
148
|
+
borrowShares,
|
|
149
|
+
maxRepaySharePrice,
|
|
150
|
+
user,
|
|
151
|
+
[],
|
|
152
|
+
false,
|
|
153
|
+
],
|
|
154
|
+
}
|
|
155
|
+
: {
|
|
156
|
+
type: "morphoRepay",
|
|
157
|
+
args: [
|
|
158
|
+
sourceParams,
|
|
159
|
+
borrowAssets,
|
|
160
|
+
0n,
|
|
161
|
+
maxRepaySharePrice,
|
|
162
|
+
user,
|
|
163
|
+
[],
|
|
164
|
+
false,
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// Sweep the borrow overshoot back into target debt so GA1 ends drained. Must run before the
|
|
169
|
+
// withdraw: in same-token markets maxUint256 would otherwise drain the just-withdrawn collateral.
|
|
170
|
+
if (sharesMode) {
|
|
171
|
+
callback.push({
|
|
172
|
+
type: "morphoRepay",
|
|
173
|
+
args: [targetParams, viem_1.maxUint256, 0n, viem_1.maxUint256, user, [], true],
|
|
174
|
+
});
|
|
175
|
+
// Fallback: if the repay above is skipped, skim any residual loan tokens to the user.
|
|
176
|
+
callback.push({
|
|
177
|
+
type: "erc20Transfer",
|
|
178
|
+
args: [targetParams.loanToken, user, viem_1.maxUint256, generalAdapter1, false],
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
callback.push({
|
|
182
|
+
type: "morphoWithdrawCollateral",
|
|
183
|
+
args: [sourceParams, collateralAmount, generalAdapter1, false],
|
|
184
|
+
});
|
|
185
|
+
const actions = [];
|
|
186
|
+
let reallocationFee = 0n;
|
|
187
|
+
if (targetReallocations && targetReallocations.length > 0) {
|
|
188
|
+
const result = (0, buildReallocationActions_js_1.buildReallocationActions)(targetReallocations, targetParams);
|
|
189
|
+
actions.push(...result.actions);
|
|
190
|
+
reallocationFee = result.fee;
|
|
191
|
+
}
|
|
192
|
+
actions.push({
|
|
193
|
+
type: "morphoSupplyCollateral",
|
|
194
|
+
args: [targetParams, collateralAmount, user, callback, false],
|
|
195
|
+
});
|
|
196
|
+
let tx = index_js_1.BundlerAction.encodeBundle(chainId, actions);
|
|
197
|
+
if (metadata) {
|
|
198
|
+
tx = (0, index_js_2.addTransactionMetadata)(tx, metadata);
|
|
199
|
+
}
|
|
200
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
201
|
+
...tx,
|
|
202
|
+
action: {
|
|
203
|
+
type: "blueRefinance",
|
|
204
|
+
args: {
|
|
205
|
+
sourceMarket: sourceParams.id,
|
|
206
|
+
targetMarket: targetParams.id,
|
|
207
|
+
collateralAmount,
|
|
208
|
+
borrowAssets,
|
|
209
|
+
borrowShares,
|
|
210
|
+
minBorrowSharePrice,
|
|
211
|
+
maxRepaySharePrice,
|
|
212
|
+
user,
|
|
213
|
+
reallocationFee,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
exports.blueRefinance = blueRefinance;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { type Address } from "viem";
|
|
3
|
-
import type {
|
|
4
|
-
/** Parameters for {@link
|
|
5
|
-
export interface
|
|
3
|
+
import type { BlueRepayAction, Metadata, RequirementSignature, Transaction } from "../../types/index.js";
|
|
4
|
+
/** Parameters for {@link blueRepay}. */
|
|
5
|
+
export interface BlueRepayParams {
|
|
6
6
|
market: {
|
|
7
7
|
readonly chainId: number;
|
|
8
8
|
readonly marketParams: MarketParams;
|
|
@@ -56,7 +56,7 @@ export interface MarketV1RepayParams {
|
|
|
56
56
|
* @param params.args.requirementSignature - Optional pre-signed permit/permit2 approval for the
|
|
57
57
|
* loan-token transfer.
|
|
58
58
|
* @param params.metadata - Optional analytics metadata attached to the bundle.
|
|
59
|
-
* @returns A deep-frozen `Transaction<
|
|
59
|
+
* @returns A deep-frozen `Transaction<BlueRepayAction>` with `to`, `value`, `data`, and the
|
|
60
60
|
* typed `action` discriminator the simulation layer consumes.
|
|
61
61
|
* @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
|
|
62
62
|
* @throws {NonPositiveRepayAmountError} when either `assets` or `shares` is negative, or when
|
|
@@ -72,9 +72,9 @@ export interface MarketV1RepayParams {
|
|
|
72
72
|
* signature is missing its expiration.
|
|
73
73
|
* @example
|
|
74
74
|
* ```ts
|
|
75
|
-
* import {
|
|
75
|
+
* import { blueRepay } from "@morpho-org/morpho-sdk";
|
|
76
76
|
*
|
|
77
|
-
* const tx =
|
|
77
|
+
* const tx = blueRepay({
|
|
78
78
|
* market: { chainId: 1, marketParams },
|
|
79
79
|
* args: {
|
|
80
80
|
* assets: 500_000_000n,
|
|
@@ -85,7 +85,7 @@ export interface MarketV1RepayParams {
|
|
|
85
85
|
* maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
|
|
86
86
|
* },
|
|
87
87
|
* });
|
|
88
|
-
* // tx satisfies Readonly<Transaction<
|
|
88
|
+
* // tx satisfies Readonly<Transaction<BlueRepayAction>>
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
|
-
export declare const
|
|
91
|
+
export declare const blueRepay: ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }: BlueRepayParams) => Readonly<Transaction<BlueRepayAction>>;
|