@morpho-org/morpho-sdk 1.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/LICENSE +21 -0
- package/README.md +496 -0
- package/lib/actions/index.d.ts +4 -0
- package/lib/actions/index.js +20 -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/encodeErc20Approval.d.ts +10 -0
- package/lib/actions/requirements/encode/encodeErc20Approval.js +25 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +13 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.js +70 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +11 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.js +70 -0
- package/lib/actions/requirements/encode/index.d.ts +3 -0
- package/lib/actions/requirements/encode/index.js +19 -0
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.d.ts +21 -0
- package/lib/actions/requirements/getMorphoAuthorizationRequirement.js +55 -0
- package/lib/actions/requirements/getRequirements.d.ts +42 -0
- package/lib/actions/requirements/getRequirements.js +80 -0
- package/lib/actions/requirements/getRequirementsAction.d.ts +17 -0
- package/lib/actions/requirements/getRequirementsAction.js +64 -0
- package/lib/actions/requirements/getRequirementsApproval.d.ts +31 -0
- package/lib/actions/requirements/getRequirementsApproval.js +51 -0
- package/lib/actions/requirements/getRequirementsPermit.d.ts +28 -0
- package/lib/actions/requirements/getRequirementsPermit.js +40 -0
- package/lib/actions/requirements/getRequirementsPermit2.d.ts +37 -0
- package/lib/actions/requirements/getRequirementsPermit2.js +59 -0
- package/lib/actions/requirements/index.d.ts +6 -0
- package/lib/actions/requirements/index.js +22 -0
- package/lib/actions/vaultV1/deposit.d.ts +41 -0
- package/lib/actions/vaultV1/deposit.js +116 -0
- package/lib/actions/vaultV1/index.d.ts +4 -0
- package/lib/actions/vaultV1/index.js +20 -0
- package/lib/actions/vaultV1/migrateToV2.d.ts +50 -0
- package/lib/actions/vaultV1/migrateToV2.js +108 -0
- package/lib/actions/vaultV1/redeem.d.ts +29 -0
- package/lib/actions/vaultV1/redeem.js +48 -0
- package/lib/actions/vaultV1/withdraw.d.ts +29 -0
- package/lib/actions/vaultV1/withdraw.js +48 -0
- package/lib/actions/vaultV2/deposit.d.ts +41 -0
- package/lib/actions/vaultV2/deposit.js +116 -0
- package/lib/actions/vaultV2/forceRedeem.d.ts +48 -0
- package/lib/actions/vaultV2/forceRedeem.js +85 -0
- package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
- package/lib/actions/vaultV2/forceWithdraw.js +85 -0
- package/lib/actions/vaultV2/index.d.ts +5 -0
- package/lib/actions/vaultV2/index.js +21 -0
- package/lib/actions/vaultV2/redeem.d.ts +32 -0
- package/lib/actions/vaultV2/redeem.js +51 -0
- package/lib/actions/vaultV2/withdraw.d.ts +32 -0
- package/lib/actions/vaultV2/withdraw.js +51 -0
- package/lib/client/index.d.ts +2 -0
- package/lib/client/index.js +18 -0
- package/lib/client/morphoClient.d.ts +25 -0
- package/lib/client/morphoClient.js +28 -0
- package/lib/client/morphoViemExtension.d.ts +39 -0
- package/lib/client/morphoViemExtension.js +40 -0
- package/lib/entities/index.d.ts +3 -0
- package/lib/entities/index.js +19 -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 +531 -0
- package/lib/entities/vaultV1/index.d.ts +1 -0
- package/lib/entities/vaultV1/index.js +17 -0
- package/lib/entities/vaultV1/vaultV1.d.ts +133 -0
- package/lib/entities/vaultV1/vaultV1.js +186 -0
- package/lib/entities/vaultV2/index.d.ts +1 -0
- package/lib/entities/vaultV2/index.js +17 -0
- package/lib/entities/vaultV2/vaultV2.d.ts +180 -0
- package/lib/entities/vaultV2/vaultV2.js +167 -0
- package/lib/helpers/computeReallocations.d.ts +23 -0
- package/lib/helpers/computeReallocations.js +98 -0
- package/lib/helpers/constant.d.ts +6 -0
- package/lib/helpers/constant.js +10 -0
- package/lib/helpers/encodeDeallocation.d.ts +10 -0
- package/lib/helpers/encodeDeallocation.js +37 -0
- package/lib/helpers/index.d.ts +4 -0
- package/lib/helpers/index.js +22 -0
- package/lib/helpers/metadata.d.ts +33 -0
- package/lib/helpers/metadata.js +49 -0
- package/lib/helpers/slippage.d.ts +46 -0
- package/lib/helpers/slippage.js +73 -0
- package/lib/helpers/validate.d.ts +156 -0
- package/lib/helpers/validate.js +287 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +21 -0
- package/lib/types/action.d.ts +216 -0
- package/lib/types/action.js +26 -0
- package/lib/types/client.d.ts +15 -0
- package/lib/types/client.js +2 -0
- package/lib/types/data.d.ts +2 -0
- package/lib/types/data.js +2 -0
- package/lib/types/deallocation.d.ts +15 -0
- package/lib/types/deallocation.js +2 -0
- package/lib/types/entity.d.ts +5 -0
- package/lib/types/entity.js +2 -0
- package/lib/types/error.d.ts +166 -0
- package/lib/types/error.js +285 -0
- package/lib/types/index.d.ts +7 -0
- package/lib/types/index.js +23 -0
- package/lib/types/metadata.d.ts +4 -0
- package/lib/types/metadata.js +2 -0
- package/lib/types/sharedLiquidity.d.ts +41 -0
- package/lib/types/sharedLiquidity.js +2 -0
- package/package.json +81 -0
- package/src/index.ts +5 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Maximum slippage tolerance: 10% */
|
|
2
|
+
export declare const MAX_SLIPPAGE_TOLERANCE: bigint;
|
|
3
|
+
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
4
|
+
export declare const DEFAULT_LLTV_BUFFER: bigint;
|
|
5
|
+
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
6
|
+
export declare const MAX_ABSOLUTE_SHARE_PRICE: bigint;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_LLTV_BUFFER = exports.MAX_SLIPPAGE_TOLERANCE = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
/** Maximum slippage tolerance: 10% */
|
|
6
|
+
exports.MAX_SLIPPAGE_TOLERANCE = blue_sdk_1.MathLib.WAD / 10n;
|
|
7
|
+
/** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
|
|
8
|
+
exports.DEFAULT_LLTV_BUFFER = blue_sdk_1.MathLib.WAD / 200n;
|
|
9
|
+
/** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
|
|
10
|
+
exports.MAX_ABSOLUTE_SHARE_PRICE = 100n * blue_sdk_1.MathLib.RAY;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
import { type Deallocation } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Encodes a single `forceDeallocate` call as ABI-encoded calldata.
|
|
5
|
+
*
|
|
6
|
+
* @param deallocation - A deallocation entry.
|
|
7
|
+
* @param onBehalf - The address from which the penalty is taken (share owner).
|
|
8
|
+
* @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function encodeForceDeallocateCall(deallocation: Deallocation, onBehalf: Address): Hex;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeForceDeallocateCall = encodeForceDeallocateCall;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
const types_1 = require("../types");
|
|
8
|
+
/**
|
|
9
|
+
* Encodes the adapter-specific `data` bytes for a `forceDeallocate` call.
|
|
10
|
+
*
|
|
11
|
+
* @param deallocation - A deallocation entry.
|
|
12
|
+
* @returns The ABI-encoded bytes to pass as `data` to VaultV2.forceDeallocate.
|
|
13
|
+
*/
|
|
14
|
+
function encodeDeallocateData(deallocation) {
|
|
15
|
+
if (deallocation.marketParams) {
|
|
16
|
+
return (0, viem_1.encodeAbiParameters)([blue_sdk_1.marketParamsAbi], [deallocation.marketParams]);
|
|
17
|
+
}
|
|
18
|
+
return "0x";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Encodes a single `forceDeallocate` call as ABI-encoded calldata.
|
|
22
|
+
*
|
|
23
|
+
* @param deallocation - A deallocation entry.
|
|
24
|
+
* @param onBehalf - The address from which the penalty is taken (share owner).
|
|
25
|
+
* @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
|
|
26
|
+
*/
|
|
27
|
+
function encodeForceDeallocateCall(deallocation, onBehalf) {
|
|
28
|
+
if (deallocation.amount <= 0n) {
|
|
29
|
+
throw new types_1.NonPositiveAssetAmountError(deallocation.adapter);
|
|
30
|
+
}
|
|
31
|
+
const data = encodeDeallocateData(deallocation);
|
|
32
|
+
return (0, viem_1.encodeFunctionData)({
|
|
33
|
+
abi: blue_sdk_viem_1.vaultV2Abi,
|
|
34
|
+
functionName: "forceDeallocate",
|
|
35
|
+
args: [deallocation.adapter, data, deallocation.amount, onBehalf],
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { computeReallocations } from "./computeReallocations";
|
|
2
|
+
export { addTransactionMetadata } from "./metadata";
|
|
3
|
+
export { computeMaxRepaySharePrice, computeMinBorrowSharePrice, } from "./slippage";
|
|
4
|
+
export { validateAccrualPosition, validateChainId, validateNativeCollateral, validatePositionHealth, validatePositionHealthAfterWithdraw, validateReallocations, validateRepayAmount, validateRepayParams, validateRepayShares, validateSlippageTolerance, validateUserAddress, } from "./validate";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayParams = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeCollateral = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinBorrowSharePrice = exports.computeMaxRepaySharePrice = exports.addTransactionMetadata = exports.computeReallocations = void 0;
|
|
4
|
+
var computeReallocations_1 = require("./computeReallocations");
|
|
5
|
+
Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return computeReallocations_1.computeReallocations; } });
|
|
6
|
+
var metadata_1 = require("./metadata");
|
|
7
|
+
Object.defineProperty(exports, "addTransactionMetadata", { enumerable: true, get: function () { return metadata_1.addTransactionMetadata; } });
|
|
8
|
+
var slippage_1 = require("./slippage");
|
|
9
|
+
Object.defineProperty(exports, "computeMaxRepaySharePrice", { enumerable: true, get: function () { return slippage_1.computeMaxRepaySharePrice; } });
|
|
10
|
+
Object.defineProperty(exports, "computeMinBorrowSharePrice", { enumerable: true, get: function () { return slippage_1.computeMinBorrowSharePrice; } });
|
|
11
|
+
var validate_1 = require("./validate");
|
|
12
|
+
Object.defineProperty(exports, "validateAccrualPosition", { enumerable: true, get: function () { return validate_1.validateAccrualPosition; } });
|
|
13
|
+
Object.defineProperty(exports, "validateChainId", { enumerable: true, get: function () { return validate_1.validateChainId; } });
|
|
14
|
+
Object.defineProperty(exports, "validateNativeCollateral", { enumerable: true, get: function () { return validate_1.validateNativeCollateral; } });
|
|
15
|
+
Object.defineProperty(exports, "validatePositionHealth", { enumerable: true, get: function () { return validate_1.validatePositionHealth; } });
|
|
16
|
+
Object.defineProperty(exports, "validatePositionHealthAfterWithdraw", { enumerable: true, get: function () { return validate_1.validatePositionHealthAfterWithdraw; } });
|
|
17
|
+
Object.defineProperty(exports, "validateReallocations", { enumerable: true, get: function () { return validate_1.validateReallocations; } });
|
|
18
|
+
Object.defineProperty(exports, "validateRepayAmount", { enumerable: true, get: function () { return validate_1.validateRepayAmount; } });
|
|
19
|
+
Object.defineProperty(exports, "validateRepayParams", { enumerable: true, get: function () { return validate_1.validateRepayParams; } });
|
|
20
|
+
Object.defineProperty(exports, "validateRepayShares", { enumerable: true, get: function () { return validate_1.validateRepayShares; } });
|
|
21
|
+
Object.defineProperty(exports, "validateSlippageTolerance", { enumerable: true, get: function () { return validate_1.validateSlippageTolerance; } });
|
|
22
|
+
Object.defineProperty(exports, "validateUserAddress", { enumerable: true, get: function () { return validate_1.validateUserAddress; } });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
import type { Metadata } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Adds metadata to a transaction object by concatenating additional
|
|
5
|
+
* hex-encoded data to the transaction's `data` field. The additional
|
|
6
|
+
* data may include a timestamp and an origin identifier derived
|
|
7
|
+
* from the metadata.
|
|
8
|
+
*
|
|
9
|
+
* The function ensures that the transaction data is correctly formatted
|
|
10
|
+
* and includes optional metadata elements if provided.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} tx - The original transaction object.
|
|
13
|
+
* @param {Hex} tx.data - The existing hex-encoded data for the transaction.
|
|
14
|
+
* @param {bigint} tx.value - The value to be sent with the transaction.
|
|
15
|
+
* @param {Address} tx.to - The recipient address of the transaction.
|
|
16
|
+
* @param {Metadata} metadata - An object containing optional metadata fields
|
|
17
|
+
* such as `timestamp` and `origin`.
|
|
18
|
+
*
|
|
19
|
+
* @returns {Object} - A new transaction object with the modified `data` field
|
|
20
|
+
* including the concatenated metadata.
|
|
21
|
+
*
|
|
22
|
+
* If no `data` is present in the original transaction, the function returns
|
|
23
|
+
* the transaction unmodified.
|
|
24
|
+
*/
|
|
25
|
+
export declare function addTransactionMetadata(tx: {
|
|
26
|
+
data: Hex;
|
|
27
|
+
value: bigint;
|
|
28
|
+
to: Address;
|
|
29
|
+
}, metadata: Metadata): {
|
|
30
|
+
data: Hex;
|
|
31
|
+
value: bigint;
|
|
32
|
+
to: Address;
|
|
33
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addTransactionMetadata = addTransactionMetadata;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
|
+
/**
|
|
7
|
+
* Adds metadata to a transaction object by concatenating additional
|
|
8
|
+
* hex-encoded data to the transaction's `data` field. The additional
|
|
9
|
+
* data may include a timestamp and an origin identifier derived
|
|
10
|
+
* from the metadata.
|
|
11
|
+
*
|
|
12
|
+
* The function ensures that the transaction data is correctly formatted
|
|
13
|
+
* and includes optional metadata elements if provided.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} tx - The original transaction object.
|
|
16
|
+
* @param {Hex} tx.data - The existing hex-encoded data for the transaction.
|
|
17
|
+
* @param {bigint} tx.value - The value to be sent with the transaction.
|
|
18
|
+
* @param {Address} tx.to - The recipient address of the transaction.
|
|
19
|
+
* @param {Metadata} metadata - An object containing optional metadata fields
|
|
20
|
+
* such as `timestamp` and `origin`.
|
|
21
|
+
*
|
|
22
|
+
* @returns {Object} - A new transaction object with the modified `data` field
|
|
23
|
+
* including the concatenated metadata.
|
|
24
|
+
*
|
|
25
|
+
* If no `data` is present in the original transaction, the function returns
|
|
26
|
+
* the transaction unmodified.
|
|
27
|
+
*/
|
|
28
|
+
function addTransactionMetadata(tx, metadata) {
|
|
29
|
+
const { data, ..._tx } = tx;
|
|
30
|
+
if (!data)
|
|
31
|
+
return tx;
|
|
32
|
+
const concatItems = [data];
|
|
33
|
+
if (metadata.timestamp) {
|
|
34
|
+
concatItems.push((0, viem_1.numberToHex)(morpho_ts_1.Time.timestamp(), { size: 4 }));
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const origin = metadata.origin.replace(/^(?!0x)/, "");
|
|
38
|
+
if (!(0, viem_1.isHex)(`0x${origin}`))
|
|
39
|
+
throw Error("Calldata origin must consists of only hex characters");
|
|
40
|
+
if (origin.length > 8)
|
|
41
|
+
throw Error("Calldata origin must be at most 8 characters long");
|
|
42
|
+
concatItems.push(`0x${origin}`);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
// eslint-disable-next-line no-console
|
|
46
|
+
console.warn("Invalid calldata origin:\n", error);
|
|
47
|
+
}
|
|
48
|
+
return { data: (0, viem_1.concatHex)(concatItems), ..._tx };
|
|
49
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type Market } from "@morpho-org/blue-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors the on-chain check in GeneralAdapter1's `morphoBorrow`:
|
|
6
|
+
* ```solidity
|
|
7
|
+
* require(borrowedAssets.rDivDown(borrowedShares) >= minSharePriceE27)
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* @param params - Computation parameters.
|
|
11
|
+
* @param params.borrowAmount - The amount of assets to borrow.
|
|
12
|
+
* @param params.market - The market to compute the minimum borrow share price for.
|
|
13
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. 0.003e18 = 0.3%).
|
|
14
|
+
* @returns minSharePriceE27 in RAY scale (1e27).
|
|
15
|
+
*/
|
|
16
|
+
export declare function computeMinBorrowSharePrice(params: {
|
|
17
|
+
borrowAmount: bigint;
|
|
18
|
+
market: Market;
|
|
19
|
+
slippageTolerance: bigint;
|
|
20
|
+
}): bigint;
|
|
21
|
+
/**
|
|
22
|
+
* Computes the maximum repay share price (in RAY, 1e27) for slippage protection.
|
|
23
|
+
*
|
|
24
|
+
* Supports both repay-by-assets and repay-by-shares paths:
|
|
25
|
+
* - By assets: derives expected shares from the repay amount via `toBorrowShares("Down")`.
|
|
26
|
+
* - By shares: derives expected assets from the shares via `toBorrowAssets("Up")`.
|
|
27
|
+
*
|
|
28
|
+
* Direction is opposite of borrow's `minSharePrice`:
|
|
29
|
+
* - Borrow uses `(WAD - slippage)` → lower bound (protects borrower from getting fewer assets per share).
|
|
30
|
+
* - Repay uses `(WAD + slippage)` → upper bound (protects repayer from paying too many assets per share).
|
|
31
|
+
*
|
|
32
|
+
* Capped at {@link MAX_ABSOLUTE_SHARE_PRICE} to prevent absurd values.
|
|
33
|
+
*
|
|
34
|
+
* @param params - Computation parameters.
|
|
35
|
+
* @param params.repayAssets - The amount of assets to repay (0n when repaying by shares).
|
|
36
|
+
* @param params.repayShares - The amount of shares to repay (0n when repaying by assets).
|
|
37
|
+
* @param params.market - The market to compute the maximum repay share price for.
|
|
38
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. 0.003e18 = 0.3%).
|
|
39
|
+
* @returns maxSharePriceE27 in RAY scale (1e27).
|
|
40
|
+
*/
|
|
41
|
+
export declare function computeMaxRepaySharePrice(params: {
|
|
42
|
+
repayAssets: bigint;
|
|
43
|
+
repayShares: bigint;
|
|
44
|
+
market: Market;
|
|
45
|
+
slippageTolerance: bigint;
|
|
46
|
+
}): bigint;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeMinBorrowSharePrice = computeMinBorrowSharePrice;
|
|
4
|
+
exports.computeMaxRepaySharePrice = computeMaxRepaySharePrice;
|
|
5
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
6
|
+
const types_1 = require("../types");
|
|
7
|
+
const constant_1 = require("./constant");
|
|
8
|
+
/**
|
|
9
|
+
* Computes the minimum borrow share price (in RAY, 1e27) for slippage protection.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors the on-chain check in GeneralAdapter1's `morphoBorrow`:
|
|
12
|
+
* ```solidity
|
|
13
|
+
* require(borrowedAssets.rDivDown(borrowedShares) >= minSharePriceE27)
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param params - Computation parameters.
|
|
17
|
+
* @param params.borrowAmount - The amount of assets to borrow.
|
|
18
|
+
* @param params.market - The market to compute the minimum borrow share price for.
|
|
19
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. 0.003e18 = 0.3%).
|
|
20
|
+
* @returns minSharePriceE27 in RAY scale (1e27).
|
|
21
|
+
*/
|
|
22
|
+
function computeMinBorrowSharePrice(params) {
|
|
23
|
+
const { borrowAmount, market, slippageTolerance } = params;
|
|
24
|
+
if (slippageTolerance >= blue_sdk_1.MathLib.WAD) {
|
|
25
|
+
throw new types_1.ExcessiveSlippageToleranceError(slippageTolerance);
|
|
26
|
+
}
|
|
27
|
+
const expectedShares = market.toBorrowShares(borrowAmount, "Up");
|
|
28
|
+
if (expectedShares === 0n) {
|
|
29
|
+
throw new types_1.ShareDivideByZeroError(market.params.id);
|
|
30
|
+
}
|
|
31
|
+
return blue_sdk_1.MathLib.mulDivDown(borrowAmount, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD - slippageTolerance), expectedShares);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Computes the maximum repay share price (in RAY, 1e27) for slippage protection.
|
|
35
|
+
*
|
|
36
|
+
* Supports both repay-by-assets and repay-by-shares paths:
|
|
37
|
+
* - By assets: derives expected shares from the repay amount via `toBorrowShares("Down")`.
|
|
38
|
+
* - By shares: derives expected assets from the shares via `toBorrowAssets("Up")`.
|
|
39
|
+
*
|
|
40
|
+
* Direction is opposite of borrow's `minSharePrice`:
|
|
41
|
+
* - Borrow uses `(WAD - slippage)` → lower bound (protects borrower from getting fewer assets per share).
|
|
42
|
+
* - Repay uses `(WAD + slippage)` → upper bound (protects repayer from paying too many assets per share).
|
|
43
|
+
*
|
|
44
|
+
* Capped at {@link MAX_ABSOLUTE_SHARE_PRICE} to prevent absurd values.
|
|
45
|
+
*
|
|
46
|
+
* @param params - Computation parameters.
|
|
47
|
+
* @param params.repayAssets - The amount of assets to repay (0n when repaying by shares).
|
|
48
|
+
* @param params.repayShares - The amount of shares to repay (0n when repaying by assets).
|
|
49
|
+
* @param params.market - The market to compute the maximum repay share price for.
|
|
50
|
+
* @param params.slippageTolerance - Slippage tolerance in WAD (e.g. 0.003e18 = 0.3%).
|
|
51
|
+
* @returns maxSharePriceE27 in RAY scale (1e27).
|
|
52
|
+
*/
|
|
53
|
+
function computeMaxRepaySharePrice(params) {
|
|
54
|
+
const { repayAssets, repayShares, market, slippageTolerance } = params;
|
|
55
|
+
if (slippageTolerance >= blue_sdk_1.MathLib.WAD) {
|
|
56
|
+
throw new types_1.ExcessiveSlippageToleranceError(slippageTolerance);
|
|
57
|
+
}
|
|
58
|
+
let assets;
|
|
59
|
+
let shares;
|
|
60
|
+
if (repayShares > 0n) {
|
|
61
|
+
assets = market.toBorrowAssets(repayShares, "Up");
|
|
62
|
+
shares = repayShares;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
assets = repayAssets;
|
|
66
|
+
shares = market.toBorrowShares(repayAssets, "Down");
|
|
67
|
+
}
|
|
68
|
+
if (shares === 0n) {
|
|
69
|
+
throw new types_1.ShareDivideByZeroError(market.params.id);
|
|
70
|
+
}
|
|
71
|
+
const maxSharePrice = blue_sdk_1.MathLib.mulDivUp(assets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippageTolerance), shares);
|
|
72
|
+
return blue_sdk_1.MathLib.min(maxSharePrice, constant_1.MAX_ABSOLUTE_SHARE_PRICE);
|
|
73
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Address } from "viem";
|
|
3
|
+
import { type VaultReallocation } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* Validates that the client has a connected account AND that it matches
|
|
6
|
+
* the provided user address.
|
|
7
|
+
*
|
|
8
|
+
* Enforces the builder = executor invariant: `userAddress` MUST equal the
|
|
9
|
+
* connected client account. Some bundle actions (e.g. `erc20TransferFrom`,
|
|
10
|
+
* `morphoWithdrawCollateral`) act implicitly on the initiator rather than
|
|
11
|
+
* on `userAddress`, so a divergence can produce mixed-account bundles.
|
|
12
|
+
*
|
|
13
|
+
* Throws {@link MissingClientPropertyError} if the client has no account.
|
|
14
|
+
* Throws {@link AddressMismatchError} if the client account differs from
|
|
15
|
+
* `userAddress`.
|
|
16
|
+
*
|
|
17
|
+
* @param clientAccountAddress - The client's account address; if undefined,
|
|
18
|
+
* `MissingClientPropertyError` is thrown.
|
|
19
|
+
* @param userAddress - The user address provided by the caller.
|
|
20
|
+
*/
|
|
21
|
+
export declare const validateUserAddress: (clientAccountAddress: Address | undefined, userAddress: Address) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Validates that the accrual position belongs to the expected market and user.
|
|
24
|
+
* Throws {@link MarketIdMismatchError} if the position's market ID
|
|
25
|
+
* does not match the expected market.
|
|
26
|
+
* Throws {@link AccrualPositionUserMismatchError} if the position's user
|
|
27
|
+
* does not match the expected user.
|
|
28
|
+
*
|
|
29
|
+
* @param params - Validation parameters.
|
|
30
|
+
* @param params.positionData - The accrual position to validate.
|
|
31
|
+
* @param params.expectedMarketId - The market ID the position must belong to.
|
|
32
|
+
* @param params.expectedUser - The user address the position must belong to.
|
|
33
|
+
*/
|
|
34
|
+
export declare const validateAccrualPosition: (params: {
|
|
35
|
+
positionData: AccrualPosition;
|
|
36
|
+
expectedMarketId: MarketId;
|
|
37
|
+
expectedUser: Address;
|
|
38
|
+
}) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Validates that the resulting position stays within the safe LTV threshold
|
|
41
|
+
* (LLTV minus buffer) after supplying additional collateral and borrowing.
|
|
42
|
+
*
|
|
43
|
+
* @param params - Validation parameters.
|
|
44
|
+
* @param params.positionData - The current accrual position with market data.
|
|
45
|
+
* @param params.additionalCollateral - Amount of collateral being added.
|
|
46
|
+
* @param params.borrowAmount - Amount being borrowed.
|
|
47
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
48
|
+
* @param params.lltv - The market's liquidation LTV.
|
|
49
|
+
*/
|
|
50
|
+
export declare const validatePositionHealth: (params: {
|
|
51
|
+
positionData: AccrualPosition;
|
|
52
|
+
additionalCollateral: bigint;
|
|
53
|
+
borrowAmount: bigint;
|
|
54
|
+
marketId: MarketId;
|
|
55
|
+
lltv: bigint;
|
|
56
|
+
}) => void;
|
|
57
|
+
/**
|
|
58
|
+
* Validates that the viem client chain ID matches the expected chain ID.
|
|
59
|
+
* Throws {@link ChainIdMismatchError} if they differ.
|
|
60
|
+
*
|
|
61
|
+
* @param clientChainId - Chain ID reported by the viem client (may be undefined).
|
|
62
|
+
* @param expectedChainId - Chain ID expected by the entity or action.
|
|
63
|
+
*/
|
|
64
|
+
export declare const validateChainId: (clientChainId: number | undefined, expectedChainId: number) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Validates that the given collateral token is the chain's wrapped native token.
|
|
67
|
+
* Throws {@link ChainWNativeMissingError} if wNative is not configured for the chain.
|
|
68
|
+
* Throws {@link NativeAmountOnNonWNativeCollateralError} if collateral is not wNative.
|
|
69
|
+
*
|
|
70
|
+
* @param chainId - The chain to look up wNative on.
|
|
71
|
+
* @param collateralToken - The market's collateral token address.
|
|
72
|
+
*/
|
|
73
|
+
export declare const validateNativeCollateral: (chainId: number, collateralToken: Address) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Validates that the resulting position stays within the safe LTV threshold
|
|
76
|
+
* (LLTV minus buffer) after withdrawing collateral.
|
|
77
|
+
*
|
|
78
|
+
* @param params - Validation parameters.
|
|
79
|
+
* @param params.positionData - The current accrual position with market data.
|
|
80
|
+
* @param params.withdrawAmount - Amount of collateral being withdrawn.
|
|
81
|
+
* @param params.lltv - The market's liquidation LTV.
|
|
82
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
83
|
+
*/
|
|
84
|
+
export declare const validatePositionHealthAfterWithdraw: (params: {
|
|
85
|
+
positionData: AccrualPosition;
|
|
86
|
+
withdrawAmount: bigint;
|
|
87
|
+
lltv: bigint;
|
|
88
|
+
marketId: MarketId;
|
|
89
|
+
}) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Validates that the repay amount assets does not exceed the outstanding debt.
|
|
92
|
+
*
|
|
93
|
+
* @param params - Validation parameters.
|
|
94
|
+
* @param params.positionData - The current accrual position.
|
|
95
|
+
* @param params.repayAssets - The amount of assets to repay.
|
|
96
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
97
|
+
*/
|
|
98
|
+
export declare const validateRepayAmount: (params: {
|
|
99
|
+
positionData: AccrualPosition;
|
|
100
|
+
repayAssets: bigint;
|
|
101
|
+
marketId: MarketId;
|
|
102
|
+
}) => void;
|
|
103
|
+
/**
|
|
104
|
+
* Validates that the repay shares do not exceed the outstanding borrow shares.
|
|
105
|
+
*
|
|
106
|
+
* @param params - Validation parameters.
|
|
107
|
+
* @param params.positionData - The current accrual position.
|
|
108
|
+
* @param params.repayShares - The amount of shares to repay.
|
|
109
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
110
|
+
*/
|
|
111
|
+
export declare const validateRepayShares: (params: {
|
|
112
|
+
positionData: AccrualPosition;
|
|
113
|
+
repayShares: bigint;
|
|
114
|
+
marketId: MarketId;
|
|
115
|
+
}) => void;
|
|
116
|
+
/**
|
|
117
|
+
* Validates the common repay input parameters shared by `marketV1Repay`
|
|
118
|
+
* and `marketV1RepayWithdrawCollateral`.
|
|
119
|
+
*
|
|
120
|
+
* @param params - Validation parameters.
|
|
121
|
+
* @param params.assets - Repay assets amount (0n when repaying by shares).
|
|
122
|
+
* @param params.shares - Repay shares amount (0n when repaying by assets).
|
|
123
|
+
* @param params.transferAmount - ERC20 amount to transfer to GeneralAdapter1.
|
|
124
|
+
* @param params.maxSharePrice - Maximum repay share price (in ray). Must be positive.
|
|
125
|
+
* @param params.marketId - The market identifier (for error messages).
|
|
126
|
+
*/
|
|
127
|
+
export declare const validateRepayParams: (params: {
|
|
128
|
+
assets: bigint;
|
|
129
|
+
shares: bigint;
|
|
130
|
+
transferAmount: bigint;
|
|
131
|
+
maxSharePrice: bigint;
|
|
132
|
+
marketId: MarketId;
|
|
133
|
+
}) => void;
|
|
134
|
+
/**
|
|
135
|
+
* Validates that vault reallocations are well-formed.
|
|
136
|
+
*
|
|
137
|
+
* Enforces the following invariants for each {@link VaultReallocation}:
|
|
138
|
+
* - `fee` must be non-negative.
|
|
139
|
+
* - `withdrawals` must be non-empty.
|
|
140
|
+
* - Every withdrawal `amount` must be strictly positive.
|
|
141
|
+
* - No withdrawal may target `targetMarketId` (the borrow market).
|
|
142
|
+
* - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
|
|
143
|
+
*
|
|
144
|
+
* @param reallocations - The reallocations to validate.
|
|
145
|
+
* @param targetMarketId - The ID of the market being borrowed from. No withdrawal may reference this market.
|
|
146
|
+
*/
|
|
147
|
+
export declare const validateReallocations: (reallocations: readonly VaultReallocation[], targetMarketId: MarketId) => void;
|
|
148
|
+
/**
|
|
149
|
+
* Validates that a slippage tolerance is within an acceptable range.
|
|
150
|
+
*
|
|
151
|
+
* Throws {@link NegativeSlippageToleranceError} if negative.
|
|
152
|
+
* Throws {@link ExcessiveSlippageToleranceError} if greater than {@link MAX_SLIPPAGE_TOLERANCE}.
|
|
153
|
+
*
|
|
154
|
+
* @param slippageTolerance - The slippage tolerance in WAD.
|
|
155
|
+
*/
|
|
156
|
+
export declare const validateSlippageTolerance: (slippageTolerance: bigint) => void;
|