@morpho-org/consumer-sdk 0.4.0 → 0.5.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 +136 -10
- package/lib/actions/index.d.ts +1 -0
- package/lib/actions/index.js +1 -0
- package/lib/actions/marketV1/borrow.d.ts +34 -0
- package/lib/actions/marketV1/borrow.js +62 -0
- package/lib/actions/marketV1/buildReallocationActions.d.ts +17 -0
- package/lib/actions/marketV1/buildReallocationActions.js +36 -0
- package/lib/actions/marketV1/index.d.ts +6 -0
- package/lib/actions/marketV1/index.js +22 -0
- package/lib/actions/marketV1/repay.d.ts +44 -0
- package/lib/actions/marketV1/repay.js +93 -0
- package/lib/actions/marketV1/repayWithdrawCollateral.d.ts +51 -0
- package/lib/actions/marketV1/repayWithdrawCollateral.js +108 -0
- package/lib/actions/marketV1/supplyCollateral.d.ts +28 -0
- package/lib/actions/marketV1/supplyCollateral.js +85 -0
- package/lib/actions/marketV1/supplyCollateralBorrow.d.ts +37 -0
- package/lib/actions/marketV1/supplyCollateralBorrow.js +109 -0
- package/lib/actions/marketV1/withdrawCollateral.d.ts +28 -0
- package/lib/actions/marketV1/withdrawCollateral.js +51 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.js +4 -1
- package/lib/actions/requirements/encode/encodeErc20Permit2.js +4 -1
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
- package/lib/actions/requirements/index.d.ts +1 -0
- package/lib/actions/requirements/index.js +1 -0
- package/lib/actions/vaultV2/forceWithdraw.js +5 -1
- package/lib/client/morphoClient.d.ts +3 -1
- package/lib/client/morphoClient.js +3 -0
- package/lib/entities/index.d.ts +1 -0
- package/lib/entities/index.js +1 -0
- package/lib/entities/marketV1/index.d.ts +1 -0
- package/lib/entities/marketV1/index.js +17 -0
- package/lib/entities/marketV1/marketV1.d.ts +290 -0
- package/lib/entities/marketV1/marketV1.js +528 -0
- package/lib/entities/vaultV1/vaultV1.js +4 -0
- package/lib/entities/vaultV2/vaultV2.js +4 -0
- package/lib/helpers/computeReallocations.d.ts +23 -0
- package/lib/helpers/computeReallocations.js +98 -0
- package/lib/helpers/constant.d.ts +5 -0
- package/lib/helpers/constant.js +6 -1
- package/lib/helpers/index.d.ts +3 -0
- package/lib/helpers/index.js +18 -1
- package/lib/helpers/slippage.d.ts +46 -0
- package/lib/helpers/slippage.js +73 -0
- package/lib/helpers/validate.d.ts +150 -0
- package/lib/helpers/validate.js +279 -0
- package/lib/types/action.d.ts +75 -3
- package/lib/types/action.js +12 -1
- package/lib/types/client.d.ts +3 -1
- package/lib/types/error.d.ts +106 -1
- package/lib/types/error.js +165 -3
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +1 -0
- package/lib/types/sharedLiquidity.d.ts +41 -0
- package/lib/types/sharedLiquidity.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1RepayWithdrawCollateral = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const helpers_1 = require("../../helpers");
|
|
9
|
+
const types_1 = require("../../types");
|
|
10
|
+
const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
|
|
11
|
+
/**
|
|
12
|
+
* Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
|
|
13
|
+
*
|
|
14
|
+
* Routed through bundler3. The bundle order is critical:
|
|
15
|
+
* 1. ERC20 transfer (loan token to GeneralAdapter1)
|
|
16
|
+
* 2. `morphoRepay` — reduces debt FIRST
|
|
17
|
+
* 3. `morphoWithdrawCollateral` — then withdraws collateral
|
|
18
|
+
*
|
|
19
|
+
* If the order were reversed, Morpho would revert because the position would be
|
|
20
|
+
* insolvent at the time of the withdraw.
|
|
21
|
+
*
|
|
22
|
+
* Supports two repay modes:
|
|
23
|
+
* - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
|
|
24
|
+
* - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay).
|
|
25
|
+
*
|
|
26
|
+
* **Prerequisites:**
|
|
27
|
+
* - ERC20 approval for loan token to GeneralAdapter1 (for the repay).
|
|
28
|
+
* - GeneralAdapter1 must be authorized on Morpho (for the withdraw).
|
|
29
|
+
*
|
|
30
|
+
* @param params - Combined repay and withdraw collateral parameters.
|
|
31
|
+
* @returns Deep-frozen transaction.
|
|
32
|
+
*/
|
|
33
|
+
const marketV1RepayWithdrawCollateral = ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, }, metadata, }) => {
|
|
34
|
+
(0, helpers_1.validateRepayParams)({
|
|
35
|
+
assets,
|
|
36
|
+
shares,
|
|
37
|
+
transferAmount,
|
|
38
|
+
maxSharePrice,
|
|
39
|
+
marketId: marketParams.id,
|
|
40
|
+
});
|
|
41
|
+
if (withdrawAmount <= 0n) {
|
|
42
|
+
throw new types_1.NonPositiveWithdrawCollateralAmountError(marketParams.id);
|
|
43
|
+
}
|
|
44
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
45
|
+
const actions = [];
|
|
46
|
+
if (requirementSignature) {
|
|
47
|
+
actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
|
|
48
|
+
chainId,
|
|
49
|
+
asset: marketParams.loanToken,
|
|
50
|
+
amount: transferAmount,
|
|
51
|
+
requirementSignature,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
actions.push({
|
|
56
|
+
type: "erc20TransferFrom",
|
|
57
|
+
args: [marketParams.loanToken, transferAmount, generalAdapter1, false],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
// REPAY FIRST — reduces debt before withdrawing collateral
|
|
61
|
+
actions.push({
|
|
62
|
+
type: "morphoRepay",
|
|
63
|
+
args: [marketParams, assets, shares, maxSharePrice, onBehalf, [], false],
|
|
64
|
+
});
|
|
65
|
+
// Skim residual loan tokens back to the payer when repaying by shares.
|
|
66
|
+
// In shares mode, transferAmount is an upper-bound estimate; morphoRepay
|
|
67
|
+
// consumes only the exact amount needed, leaving a residual in the adapter.
|
|
68
|
+
if (shares > 0n) {
|
|
69
|
+
actions.push({
|
|
70
|
+
type: "erc20Transfer",
|
|
71
|
+
args: [
|
|
72
|
+
marketParams.loanToken,
|
|
73
|
+
receiver,
|
|
74
|
+
viem_1.maxUint256,
|
|
75
|
+
generalAdapter1,
|
|
76
|
+
false,
|
|
77
|
+
],
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
actions.push({
|
|
81
|
+
type: "morphoWithdrawCollateral",
|
|
82
|
+
args: [marketParams, withdrawAmount, receiver, false],
|
|
83
|
+
});
|
|
84
|
+
let tx = {
|
|
85
|
+
...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
|
|
86
|
+
value: 0n,
|
|
87
|
+
};
|
|
88
|
+
if (metadata) {
|
|
89
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
90
|
+
}
|
|
91
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
92
|
+
...tx,
|
|
93
|
+
action: {
|
|
94
|
+
type: "marketV1RepayWithdrawCollateral",
|
|
95
|
+
args: {
|
|
96
|
+
market: marketParams.id,
|
|
97
|
+
repayAssets: assets,
|
|
98
|
+
repayShares: shares,
|
|
99
|
+
transferAmount,
|
|
100
|
+
withdrawAmount,
|
|
101
|
+
maxSharePrice,
|
|
102
|
+
onBehalf,
|
|
103
|
+
receiver,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
exports.marketV1RepayWithdrawCollateral = marketV1RepayWithdrawCollateral;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import { type DepositAmountArgs, type MarketV1SupplyCollateralAction, type Metadata, type RequirementSignature, type Transaction } from "../../types";
|
|
4
|
+
/** Parameters for {@link marketV1SupplyCollateral}. */
|
|
5
|
+
export interface MarketV1SupplyCollateralParams {
|
|
6
|
+
market: {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly marketParams: MarketParams;
|
|
9
|
+
};
|
|
10
|
+
args: DepositAmountArgs & {
|
|
11
|
+
onBehalf: Address;
|
|
12
|
+
requirementSignature?: RequirementSignature;
|
|
13
|
+
};
|
|
14
|
+
metadata?: Metadata;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Prepares a supply-collateral transaction for a Morpho Blue market.
|
|
18
|
+
*
|
|
19
|
+
* Routed through bundler via GeneralAdapter1.
|
|
20
|
+
* When `nativeAmount` is provided, native token is wrapped via GeneralAdapter1.
|
|
21
|
+
* Collateral token must be the chain's wNative when `nativeAmount` is used.
|
|
22
|
+
*
|
|
23
|
+
* Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
|
|
24
|
+
*
|
|
25
|
+
* @param params - Supply collateral parameters.
|
|
26
|
+
* @returns Deep-frozen transaction.
|
|
27
|
+
*/
|
|
28
|
+
export declare const marketV1SupplyCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, requirementSignature, nativeAmount }, metadata, }: MarketV1SupplyCollateralParams) => Readonly<Transaction<MarketV1SupplyCollateralAction>>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1SupplyCollateral = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const helpers_1 = require("../../helpers");
|
|
8
|
+
const types_1 = require("../../types");
|
|
9
|
+
const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
|
|
10
|
+
/**
|
|
11
|
+
* Prepares a supply-collateral transaction for a Morpho Blue market.
|
|
12
|
+
*
|
|
13
|
+
* Routed through bundler via GeneralAdapter1.
|
|
14
|
+
* When `nativeAmount` is provided, native token is wrapped via GeneralAdapter1.
|
|
15
|
+
* Collateral token must be the chain's wNative when `nativeAmount` is used.
|
|
16
|
+
*
|
|
17
|
+
* Zero loss: all collateral reaches Morpho. No dust left in bundler or adapter.
|
|
18
|
+
*
|
|
19
|
+
* @param params - Supply collateral parameters.
|
|
20
|
+
* @returns Deep-frozen transaction.
|
|
21
|
+
*/
|
|
22
|
+
const marketV1SupplyCollateral = ({ market: { chainId, marketParams }, args: { amount = 0n, onBehalf, requirementSignature, nativeAmount }, metadata, }) => {
|
|
23
|
+
if (amount < 0n) {
|
|
24
|
+
throw new types_1.NonPositiveAssetAmountError(marketParams.collateralToken);
|
|
25
|
+
}
|
|
26
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
27
|
+
throw new types_1.NegativeNativeAmountError(nativeAmount);
|
|
28
|
+
}
|
|
29
|
+
const totalCollateral = amount + (nativeAmount ?? 0n);
|
|
30
|
+
if (totalCollateral === 0n) {
|
|
31
|
+
throw new types_1.ZeroCollateralAmountError(marketParams.id);
|
|
32
|
+
}
|
|
33
|
+
const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
34
|
+
const actions = [];
|
|
35
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
36
|
+
(0, helpers_1.validateNativeCollateral)(chainId, marketParams.collateralToken);
|
|
37
|
+
actions.push({
|
|
38
|
+
type: "nativeTransfer",
|
|
39
|
+
args: [bundler3, generalAdapter1, nativeAmount, false],
|
|
40
|
+
}, {
|
|
41
|
+
type: "wrapNative",
|
|
42
|
+
args: [nativeAmount, generalAdapter1, false],
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (amount > 0n) {
|
|
46
|
+
if (requirementSignature) {
|
|
47
|
+
actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
|
|
48
|
+
chainId,
|
|
49
|
+
asset: marketParams.collateralToken,
|
|
50
|
+
amount,
|
|
51
|
+
requirementSignature,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
actions.push({
|
|
56
|
+
type: "erc20TransferFrom",
|
|
57
|
+
args: [marketParams.collateralToken, amount, generalAdapter1, false],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
actions.push({
|
|
62
|
+
type: "morphoSupplyCollateral",
|
|
63
|
+
args: [marketParams, totalCollateral, onBehalf, [], false],
|
|
64
|
+
});
|
|
65
|
+
let tx = {
|
|
66
|
+
...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
|
|
67
|
+
value: nativeAmount ?? 0n,
|
|
68
|
+
};
|
|
69
|
+
if (metadata) {
|
|
70
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
71
|
+
}
|
|
72
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
73
|
+
...tx,
|
|
74
|
+
action: {
|
|
75
|
+
type: "marketV1SupplyCollateral",
|
|
76
|
+
args: {
|
|
77
|
+
market: marketParams.id,
|
|
78
|
+
amount: totalCollateral,
|
|
79
|
+
onBehalf,
|
|
80
|
+
nativeAmount,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
exports.marketV1SupplyCollateral = marketV1SupplyCollateral;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import { type DepositAmountArgs, type MarketV1SupplyCollateralBorrowAction, type Metadata, type RequirementSignature, type Transaction, type VaultReallocation } from "../../types";
|
|
4
|
+
/** Parameters for {@link marketV1SupplyCollateralBorrow}. */
|
|
5
|
+
export interface MarketV1SupplyCollateralBorrowParams {
|
|
6
|
+
market: {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly marketParams: MarketParams;
|
|
9
|
+
};
|
|
10
|
+
args: DepositAmountArgs & {
|
|
11
|
+
borrowAmount: bigint;
|
|
12
|
+
onBehalf: Address;
|
|
13
|
+
receiver: Address;
|
|
14
|
+
/** Minimum borrow share price (in ray). Protects against share price manipulation. */
|
|
15
|
+
minSharePrice: bigint;
|
|
16
|
+
requirementSignature?: RequirementSignature;
|
|
17
|
+
/** Vault reallocations to execute before borrowing (computed by entity). */
|
|
18
|
+
reallocations?: readonly VaultReallocation[];
|
|
19
|
+
};
|
|
20
|
+
metadata?: Metadata;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
|
|
24
|
+
*
|
|
25
|
+
* Routed through the bundler: collateral transfer + `morphoSupplyCollateral` + `morphoBorrow`.
|
|
26
|
+
* When `nativeAmount` is provided, native ETH is wrapped via GeneralAdapter1.
|
|
27
|
+
*
|
|
28
|
+
* **Prerequisite:** GeneralAdapter1 must be authorized on Morpho to borrow on behalf of the user.
|
|
29
|
+
* Use `getRequirements()` on the entity to check and obtain the authorization transaction.
|
|
30
|
+
*
|
|
31
|
+
* Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver.
|
|
32
|
+
* No dust left in bundler or adapter.
|
|
33
|
+
*
|
|
34
|
+
* @param params - Combined supply collateral and borrow parameters.
|
|
35
|
+
* @returns Deep-frozen transaction.
|
|
36
|
+
*/
|
|
37
|
+
export declare const marketV1SupplyCollateralBorrow: ({ market: { chainId, marketParams }, args: { amount, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }: MarketV1SupplyCollateralBorrowParams) => Readonly<Transaction<MarketV1SupplyCollateralBorrowAction>>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1SupplyCollateralBorrow = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const helpers_1 = require("../../helpers");
|
|
8
|
+
const types_1 = require("../../types");
|
|
9
|
+
const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
|
|
10
|
+
const buildReallocationActions_1 = require("./buildReallocationActions");
|
|
11
|
+
/**
|
|
12
|
+
* Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
|
|
13
|
+
*
|
|
14
|
+
* Routed through the bundler: collateral transfer + `morphoSupplyCollateral` + `morphoBorrow`.
|
|
15
|
+
* When `nativeAmount` is provided, native ETH is wrapped via GeneralAdapter1.
|
|
16
|
+
*
|
|
17
|
+
* **Prerequisite:** GeneralAdapter1 must be authorized on Morpho to borrow on behalf of the user.
|
|
18
|
+
* Use `getRequirements()` on the entity to check and obtain the authorization transaction.
|
|
19
|
+
*
|
|
20
|
+
* Zero loss: all collateral reaches Morpho, all borrowed tokens reach the receiver.
|
|
21
|
+
* No dust left in bundler or adapter.
|
|
22
|
+
*
|
|
23
|
+
* @param params - Combined supply collateral and borrow parameters.
|
|
24
|
+
* @returns Deep-frozen transaction.
|
|
25
|
+
*/
|
|
26
|
+
const marketV1SupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: { amount = 0n, borrowAmount, onBehalf, receiver, minSharePrice, requirementSignature, nativeAmount, reallocations, }, metadata, }) => {
|
|
27
|
+
if (amount < 0n) {
|
|
28
|
+
throw new types_1.NonPositiveAssetAmountError(marketParams.collateralToken);
|
|
29
|
+
}
|
|
30
|
+
if (nativeAmount !== undefined && nativeAmount < 0n) {
|
|
31
|
+
throw new types_1.NegativeNativeAmountError(nativeAmount);
|
|
32
|
+
}
|
|
33
|
+
if (borrowAmount <= 0n) {
|
|
34
|
+
throw new types_1.NonPositiveBorrowAmountError(marketParams.id);
|
|
35
|
+
}
|
|
36
|
+
if (minSharePrice < 0n) {
|
|
37
|
+
throw new types_1.NonPositiveMinBorrowSharePriceError(marketParams.id);
|
|
38
|
+
}
|
|
39
|
+
const totalCollateral = amount + (nativeAmount ?? 0n);
|
|
40
|
+
if (totalCollateral === 0n) {
|
|
41
|
+
throw new types_1.ZeroCollateralAmountError(marketParams.id);
|
|
42
|
+
}
|
|
43
|
+
const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
44
|
+
const actions = [];
|
|
45
|
+
if (nativeAmount !== undefined && nativeAmount > 0n) {
|
|
46
|
+
(0, helpers_1.validateNativeCollateral)(chainId, marketParams.collateralToken);
|
|
47
|
+
actions.push({
|
|
48
|
+
type: "nativeTransfer",
|
|
49
|
+
args: [bundler3, generalAdapter1, nativeAmount, false],
|
|
50
|
+
}, {
|
|
51
|
+
type: "wrapNative",
|
|
52
|
+
args: [nativeAmount, generalAdapter1, false],
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (amount > 0n) {
|
|
56
|
+
if (requirementSignature) {
|
|
57
|
+
actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({
|
|
58
|
+
chainId,
|
|
59
|
+
asset: marketParams.collateralToken,
|
|
60
|
+
amount,
|
|
61
|
+
requirementSignature,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
actions.push({
|
|
66
|
+
type: "erc20TransferFrom",
|
|
67
|
+
args: [marketParams.collateralToken, amount, generalAdapter1, false],
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
actions.push({
|
|
72
|
+
type: "morphoSupplyCollateral",
|
|
73
|
+
args: [marketParams, totalCollateral, onBehalf, [], false],
|
|
74
|
+
});
|
|
75
|
+
let reallocationFee = 0n;
|
|
76
|
+
if (reallocations && reallocations.length > 0) {
|
|
77
|
+
const result = (0, buildReallocationActions_1.buildReallocationActions)(reallocations, marketParams);
|
|
78
|
+
actions.push(...result.actions);
|
|
79
|
+
reallocationFee = result.fee;
|
|
80
|
+
}
|
|
81
|
+
actions.push({
|
|
82
|
+
type: "morphoBorrow",
|
|
83
|
+
args: [marketParams, borrowAmount, 0n, minSharePrice, receiver, false],
|
|
84
|
+
});
|
|
85
|
+
let tx = {
|
|
86
|
+
...bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions),
|
|
87
|
+
value: (nativeAmount ?? 0n) + reallocationFee,
|
|
88
|
+
};
|
|
89
|
+
if (metadata) {
|
|
90
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
91
|
+
}
|
|
92
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
93
|
+
...tx,
|
|
94
|
+
action: {
|
|
95
|
+
type: "marketV1SupplyCollateralBorrow",
|
|
96
|
+
args: {
|
|
97
|
+
market: marketParams.id,
|
|
98
|
+
collateralAmount: totalCollateral,
|
|
99
|
+
borrowAmount,
|
|
100
|
+
minSharePrice,
|
|
101
|
+
onBehalf,
|
|
102
|
+
receiver,
|
|
103
|
+
nativeAmount,
|
|
104
|
+
reallocationFee,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
exports.marketV1SupplyCollateralBorrow = marketV1SupplyCollateralBorrow;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type MarketParams } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type MarketV1WithdrawCollateralAction, type Metadata, type Transaction } from "../../types";
|
|
4
|
+
/** Parameters for {@link marketV1WithdrawCollateral}. */
|
|
5
|
+
export interface MarketV1WithdrawCollateralParams {
|
|
6
|
+
market: {
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
readonly marketParams: MarketParams;
|
|
9
|
+
};
|
|
10
|
+
args: {
|
|
11
|
+
amount: bigint;
|
|
12
|
+
onBehalf: Address;
|
|
13
|
+
receiver: Address;
|
|
14
|
+
};
|
|
15
|
+
metadata?: Metadata;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares a withdraw-collateral transaction for a Morpho Blue market.
|
|
19
|
+
*
|
|
20
|
+
* Direct call to `morpho.withdrawCollateral`. No bundler needed — collateral
|
|
21
|
+
* flows out of Morpho, so there is no attack surface requiring the bundler.
|
|
22
|
+
*
|
|
23
|
+
* The caller (`msg.sender`) must be `onBehalf` or be authorized by them.
|
|
24
|
+
*
|
|
25
|
+
* @param params - Withdraw collateral parameters.
|
|
26
|
+
* @returns Deep-frozen transaction.
|
|
27
|
+
*/
|
|
28
|
+
export declare const marketV1WithdrawCollateral: ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }: MarketV1WithdrawCollateralParams) => Readonly<Transaction<MarketV1WithdrawCollateralAction>>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketV1WithdrawCollateral = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const helpers_1 = require("../../helpers");
|
|
9
|
+
const types_1 = require("../../types");
|
|
10
|
+
/**
|
|
11
|
+
* Prepares a withdraw-collateral transaction for a Morpho Blue market.
|
|
12
|
+
*
|
|
13
|
+
* Direct call to `morpho.withdrawCollateral`. No bundler needed — collateral
|
|
14
|
+
* flows out of Morpho, so there is no attack surface requiring the bundler.
|
|
15
|
+
*
|
|
16
|
+
* The caller (`msg.sender`) must be `onBehalf` or be authorized by them.
|
|
17
|
+
*
|
|
18
|
+
* @param params - Withdraw collateral parameters.
|
|
19
|
+
* @returns Deep-frozen transaction.
|
|
20
|
+
*/
|
|
21
|
+
const marketV1WithdrawCollateral = ({ market: { chainId, marketParams }, args: { amount, onBehalf, receiver }, metadata, }) => {
|
|
22
|
+
if (amount <= 0n) {
|
|
23
|
+
throw new types_1.NonPositiveWithdrawCollateralAmountError(marketParams.id);
|
|
24
|
+
}
|
|
25
|
+
const { morpho } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
26
|
+
let tx = {
|
|
27
|
+
to: morpho,
|
|
28
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
29
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
30
|
+
functionName: "withdrawCollateral",
|
|
31
|
+
args: [marketParams, amount, onBehalf, receiver],
|
|
32
|
+
}),
|
|
33
|
+
value: 0n,
|
|
34
|
+
};
|
|
35
|
+
if (metadata) {
|
|
36
|
+
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
37
|
+
}
|
|
38
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
39
|
+
...tx,
|
|
40
|
+
action: {
|
|
41
|
+
type: "marketV1WithdrawCollateral",
|
|
42
|
+
args: {
|
|
43
|
+
market: marketParams.id,
|
|
44
|
+
amount,
|
|
45
|
+
onBehalf,
|
|
46
|
+
receiver,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
exports.marketV1WithdrawCollateral = marketV1WithdrawCollateral;
|
|
@@ -45,11 +45,14 @@ const encodeErc20Permit = async (viemClient, params) => {
|
|
|
45
45
|
...typedData,
|
|
46
46
|
account: client.account,
|
|
47
47
|
});
|
|
48
|
-
await (0, viem_1.verifyTypedData)({
|
|
48
|
+
const isValid = await (0, viem_1.verifyTypedData)({
|
|
49
49
|
...typedData,
|
|
50
50
|
address: userAddress, // Verify against the permit's owner.
|
|
51
51
|
signature,
|
|
52
52
|
});
|
|
53
|
+
if (!isValid) {
|
|
54
|
+
throw new types_1.InvalidSignatureError();
|
|
55
|
+
}
|
|
53
56
|
return (0, morpho_ts_1.deepFreeze)({
|
|
54
57
|
args: {
|
|
55
58
|
owner: userAddress,
|
|
@@ -44,11 +44,14 @@ const encodeErc20Permit2 = (params) => {
|
|
|
44
44
|
...typedData,
|
|
45
45
|
account: client.account,
|
|
46
46
|
});
|
|
47
|
-
await (0, viem_1.verifyTypedData)({
|
|
47
|
+
const isValid = await (0, viem_1.verifyTypedData)({
|
|
48
48
|
...typedData,
|
|
49
49
|
address: userAddress,
|
|
50
50
|
signature,
|
|
51
51
|
});
|
|
52
|
+
if (!isValid) {
|
|
53
|
+
throw new types_1.InvalidSignatureError();
|
|
54
|
+
}
|
|
52
55
|
return (0, morpho_ts_1.deepFreeze)({
|
|
53
56
|
args: {
|
|
54
57
|
owner: userAddress,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Client } from "viem";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type MorphoAuthorizationAction, type Transaction } from "../../types";
|
|
4
|
+
/**
|
|
5
|
+
* Checks whether GeneralAdapter1 is authorized on Morpho for the given user.
|
|
6
|
+
* If not authorized, returns a deep-frozen `morpho.setAuthorization(generalAdapter1, true)` transaction.
|
|
7
|
+
* Returns `null` if authorization is already in place.
|
|
8
|
+
*
|
|
9
|
+
* Required before any bundled borrow on behalf of the user (e.g. `supplyCollateralBorrow`).
|
|
10
|
+
*
|
|
11
|
+
* @param params - Request parameters.
|
|
12
|
+
* @param params.viemClient - Connected viem client.
|
|
13
|
+
* @param params.chainId - Target chain ID (used to look up Morpho and GeneralAdapter1 addresses).
|
|
14
|
+
* @param params.userAddress - The user who needs to authorize GeneralAdapter1.
|
|
15
|
+
* @returns Deep-frozen authorization transaction, or `null` if already authorized.
|
|
16
|
+
*/
|
|
17
|
+
export declare const getMorphoAuthorizationRequirement: (params: {
|
|
18
|
+
viemClient: Client;
|
|
19
|
+
chainId: number;
|
|
20
|
+
userAddress: Address;
|
|
21
|
+
}) => Promise<Readonly<Transaction<MorphoAuthorizationAction>> | null>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMorphoAuthorizationRequirement = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const types_1 = require("../../types");
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether GeneralAdapter1 is authorized on Morpho for the given user.
|
|
11
|
+
* If not authorized, returns a deep-frozen `morpho.setAuthorization(generalAdapter1, true)` transaction.
|
|
12
|
+
* Returns `null` if authorization is already in place.
|
|
13
|
+
*
|
|
14
|
+
* Required before any bundled borrow on behalf of the user (e.g. `supplyCollateralBorrow`).
|
|
15
|
+
*
|
|
16
|
+
* @param params - Request parameters.
|
|
17
|
+
* @param params.viemClient - Connected viem client.
|
|
18
|
+
* @param params.chainId - Target chain ID (used to look up Morpho and GeneralAdapter1 addresses).
|
|
19
|
+
* @param params.userAddress - The user who needs to authorize GeneralAdapter1.
|
|
20
|
+
* @returns Deep-frozen authorization transaction, or `null` if already authorized.
|
|
21
|
+
*/
|
|
22
|
+
const getMorphoAuthorizationRequirement = async (params) => {
|
|
23
|
+
const { viemClient, chainId, userAddress } = params;
|
|
24
|
+
if (viemClient.chain?.id !== chainId) {
|
|
25
|
+
throw new types_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
26
|
+
}
|
|
27
|
+
const { morpho, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
28
|
+
const pc = viemClient.extend(viem_1.publicActions);
|
|
29
|
+
const isAuthorized = await pc.readContract({
|
|
30
|
+
address: morpho,
|
|
31
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
32
|
+
functionName: "isAuthorized",
|
|
33
|
+
args: [userAddress, generalAdapter1],
|
|
34
|
+
});
|
|
35
|
+
if (isAuthorized) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
39
|
+
to: morpho,
|
|
40
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
41
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
42
|
+
functionName: "setAuthorization",
|
|
43
|
+
args: [generalAdapter1, true],
|
|
44
|
+
}),
|
|
45
|
+
value: 0n,
|
|
46
|
+
action: {
|
|
47
|
+
type: "morphoAuthorization",
|
|
48
|
+
args: {
|
|
49
|
+
authorized: generalAdapter1,
|
|
50
|
+
isAuthorized: true,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
exports.getMorphoAuthorizationRequirement = getMorphoAuthorizationRequirement;
|
|
@@ -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("./encode/index"), exports);
|
|
18
|
+
__exportStar(require("./getMorphoAuthorizationRequirement"), exports);
|
|
18
19
|
__exportStar(require("./getRequirements"), exports);
|
|
19
20
|
__exportStar(require("./getRequirementsApproval"), exports);
|
|
20
21
|
__exportStar(require("./getRequirementsPermit"), exports);
|
|
@@ -39,7 +39,11 @@ const vaultV2ForceWithdraw = ({ vault: { address: vaultAddress }, args: { deallo
|
|
|
39
39
|
}
|
|
40
40
|
const totalDeallocated = deallocations.reduce((sum, d) => sum + d.amount, 0n);
|
|
41
41
|
if (withdraw.amount < totalDeallocated) {
|
|
42
|
-
throw new types_1.DeallocationsExceedWithdrawError(
|
|
42
|
+
throw new types_1.DeallocationsExceedWithdrawError({
|
|
43
|
+
vault: vaultAddress,
|
|
44
|
+
withdrawAmount: withdraw.amount,
|
|
45
|
+
totalDeallocated,
|
|
46
|
+
});
|
|
43
47
|
}
|
|
44
48
|
const calls = [];
|
|
45
49
|
for (const deallocation of deallocations) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { MarketParams } from "@morpho-org/blue-sdk";
|
|
1
2
|
import type { Address, Client } from "viem";
|
|
2
|
-
import { MorphoVaultV1, MorphoVaultV2 } from "../entities";
|
|
3
|
+
import { MorphoMarketV1, MorphoVaultV1, MorphoVaultV2 } from "../entities";
|
|
3
4
|
import type { Metadata, MorphoClientType } from "../types";
|
|
4
5
|
export declare class MorphoClient implements MorphoClientType {
|
|
5
6
|
readonly viemClient: Client;
|
|
@@ -20,4 +21,5 @@ export declare class MorphoClient implements MorphoClientType {
|
|
|
20
21
|
} | undefined);
|
|
21
22
|
vaultV1(vault: Address, chainId: number): MorphoVaultV1;
|
|
22
23
|
vaultV2(vault: Address, chainId: number): MorphoVaultV2;
|
|
24
|
+
marketV1(marketParams: MarketParams, chainId: number): MorphoMarketV1;
|
|
23
25
|
}
|
|
@@ -21,5 +21,8 @@ class MorphoClient {
|
|
|
21
21
|
vaultV2(vault, chainId) {
|
|
22
22
|
return new entities_1.MorphoVaultV2(this, vault, chainId);
|
|
23
23
|
}
|
|
24
|
+
marketV1(marketParams, chainId) {
|
|
25
|
+
return new entities_1.MorphoMarketV1(this, marketParams, chainId);
|
|
26
|
+
}
|
|
24
27
|
}
|
|
25
28
|
exports.MorphoClient = MorphoClient;
|
package/lib/entities/index.d.ts
CHANGED
package/lib/entities/index.js
CHANGED
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./marketV1"), exports);
|
|
17
18
|
__exportStar(require("./vaultV1"), exports);
|
|
18
19
|
__exportStar(require("./vaultV2"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./marketV1";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./marketV1"), exports);
|