@morpho-org/morpho-sdk 10.0.1 → 10.1.0-next.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 +31 -10
- package/lib/cjs/abis.d.ts +1 -1
- package/lib/cjs/abis.js +5 -1
- package/lib/cjs/actions/blue/refinance.d.ts +1 -1
- package/lib/cjs/actions/blue/refinance.js +3 -8
- package/lib/cjs/actions/blue/refinanceValidation.d.ts +19 -0
- package/lib/cjs/actions/blue/refinanceValidation.js +46 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +3 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +4 -20
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +7 -21
- package/lib/cjs/actions/requirements/validateClientChainId.d.ts +1 -26
- package/lib/cjs/actions/requirements/validateClientChainId.js +2 -31
- package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +2 -1
- package/lib/cjs/actions/vaultV2/forceRedeem.js +20 -2
- package/lib/cjs/blue/abis.d.ts +1 -1
- package/lib/cjs/blue/abis.js +5 -1
- package/lib/cjs/blue/entities.d.ts +4 -4
- package/lib/cjs/blue/entities.js +3 -1
- package/lib/cjs/blue/errors.d.ts +2 -2
- package/lib/cjs/blue/errors.js +5 -1
- package/lib/cjs/blue/fetch.d.ts +2 -2
- package/lib/cjs/blue/fetch.js +3 -1
- package/lib/cjs/blue/utils.d.ts +1 -1
- package/lib/cjs/blue/utils.js +2 -1
- package/lib/cjs/entities/blue/blue.js +24 -55
- package/lib/cjs/entities/index.d.ts +4 -4
- package/lib/cjs/entities/index.js +3 -1
- package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +2 -5
- package/lib/cjs/entities/requirements/getErc2612Permit.js +9 -58
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/cjs/entities/vaultV2/vaultV2.js +39 -1
- package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -2
- package/lib/cjs/entities/vaultV2BlueReallocationData.js +6 -2
- package/lib/cjs/errors.d.ts +4 -4
- package/lib/cjs/errors.js +7 -5
- package/lib/cjs/fetch.d.ts +2 -2
- package/lib/cjs/fetch.js +3 -1
- package/lib/cjs/helpers/deadShares.js +3 -0
- package/lib/cjs/helpers/quoteBlueRepay.d.ts +16 -0
- package/lib/cjs/helpers/quoteBlueRepay.js +46 -0
- package/lib/cjs/helpers/tokenApproval.d.ts +14 -0
- package/lib/cjs/helpers/tokenApproval.js +20 -0
- package/lib/cjs/helpers/validate.d.ts +1 -1
- package/lib/cjs/helpers/validate.js +2 -2
- package/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +13 -1
- package/lib/cjs/midnight/errors.d.ts +2 -1
- package/lib/cjs/midnight/errors.js +5 -4
- package/lib/cjs/reads/readErc2612Permit.d.ts +22 -0
- package/lib/cjs/reads/readErc2612Permit.js +77 -0
- package/lib/cjs/reads/validateClientChainId.d.ts +26 -0
- package/lib/cjs/reads/validateClientChainId.js +34 -0
- package/lib/cjs/transactionPlan/blue/authorization.d.ts +23 -0
- package/lib/cjs/transactionPlan/blue/authorization.js +128 -0
- package/lib/cjs/transactionPlan/blue/borrow.d.ts +80 -0
- package/lib/cjs/transactionPlan/blue/borrow.js +248 -0
- package/lib/cjs/transactionPlan/blue/prepare.d.ts +102 -0
- package/lib/cjs/transactionPlan/blue/prepare.js +191 -0
- package/lib/cjs/transactionPlan/blue/refinance.d.ts +93 -0
- package/lib/cjs/transactionPlan/blue/refinance.js +281 -0
- package/lib/cjs/transactionPlan/blue/refinanceValidation.d.ts +11 -0
- package/lib/cjs/transactionPlan/blue/refinanceValidation.js +20 -0
- package/lib/cjs/transactionPlan/blue/repay.d.ts +80 -0
- package/lib/cjs/transactionPlan/blue/repay.js +229 -0
- package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
- package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +379 -0
- package/lib/cjs/transactionPlan/blue/supply.d.ts +181 -0
- package/lib/cjs/transactionPlan/blue/supply.js +238 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +97 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateral.js +231 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
- package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +395 -0
- package/lib/cjs/transactionPlan/blue/withdraw.d.ts +136 -0
- package/lib/cjs/transactionPlan/blue/withdraw.js +340 -0
- package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
- package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +246 -0
- package/lib/cjs/transactionPlan/deposit.js +8 -19
- package/lib/cjs/transactionPlan/error.d.ts +50 -0
- package/lib/cjs/transactionPlan/error.js +60 -1
- package/lib/cjs/transactionPlan/getTokenPermit.d.ts +17 -8
- package/lib/cjs/transactionPlan/getTokenPermit.js +35 -7
- package/lib/cjs/transactionPlan/index.d.ts +21 -1
- package/lib/cjs/transactionPlan/index.js +27 -5
- package/lib/cjs/transactionPlan/intent.d.ts +2 -2
- package/lib/cjs/transactionPlan/intent.js +8 -4
- package/lib/cjs/transactionPlan/permit.d.ts +16 -2
- package/lib/cjs/transactionPlan/permit.js +77 -45
- package/lib/cjs/transactionPlan/plan.d.ts +67 -12
- package/lib/cjs/transactionPlan/plan.js +284 -50
- package/lib/cjs/transactionPlan/requirements.d.ts +14 -5
- package/lib/cjs/transactionPlan/requirements.js +30 -12
- package/lib/cjs/transactionPlan/serde.d.ts +7 -4
- package/lib/cjs/transactionPlan/serde.js +80 -61
- package/lib/cjs/transactionPlan/types.d.ts +141 -13
- package/lib/cjs/transactionPlan/validateActions.js +11 -1
- package/lib/cjs/transactionPlan/validation.d.ts +8 -0
- package/lib/cjs/transactionPlan/validation.js +90 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/deposit.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv1/withdraw.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/deposit.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/redeem.js +1 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -0
- package/lib/cjs/transactionPlan/vaultv2/withdraw.js +1 -0
- package/lib/cjs/utils.d.ts +2 -2
- package/lib/cjs/utils.js +4 -2
- package/lib/cjs/version.d.ts +1 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/abis.d.ts +1 -1
- package/lib/esm/abis.js +1 -1
- package/lib/esm/actions/blue/refinance.d.ts +1 -1
- package/lib/esm/actions/blue/refinance.js +4 -9
- package/lib/esm/actions/blue/refinanceValidation.d.ts +19 -0
- package/lib/esm/actions/blue/refinanceValidation.js +41 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -8
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +1 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +5 -21
- package/lib/esm/actions/requirements/getRequirementsApproval.js +7 -21
- package/lib/esm/actions/requirements/validateClientChainId.d.ts +1 -26
- package/lib/esm/actions/requirements/validateClientChainId.js +1 -30
- package/lib/esm/actions/vaultV2/forceRedeem.d.ts +2 -1
- package/lib/esm/actions/vaultV2/forceRedeem.js +20 -2
- package/lib/esm/blue/abis.d.ts +1 -1
- package/lib/esm/blue/abis.js +1 -1
- package/lib/esm/blue/entities.d.ts +4 -4
- package/lib/esm/blue/entities.js +2 -2
- package/lib/esm/blue/errors.d.ts +2 -2
- package/lib/esm/blue/errors.js +2 -2
- package/lib/esm/blue/fetch.d.ts +2 -2
- package/lib/esm/blue/fetch.js +2 -2
- package/lib/esm/blue/utils.d.ts +1 -1
- package/lib/esm/blue/utils.js +1 -1
- package/lib/esm/entities/blue/blue.js +26 -57
- package/lib/esm/entities/index.d.ts +4 -4
- package/lib/esm/entities/index.js +2 -2
- package/lib/esm/entities/requirements/getErc2612Permit.d.ts +2 -5
- package/lib/esm/entities/requirements/getErc2612Permit.js +9 -58
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
- package/lib/esm/entities/vaultV2/vaultV2.js +40 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -2
- package/lib/esm/entities/vaultV2BlueReallocationData.js +6 -2
- package/lib/esm/errors.d.ts +4 -4
- package/lib/esm/errors.js +4 -4
- package/lib/esm/fetch.d.ts +2 -2
- package/lib/esm/fetch.js +2 -2
- package/lib/esm/helpers/deadShares.js +4 -1
- package/lib/esm/helpers/quoteBlueRepay.d.ts +16 -0
- package/lib/esm/helpers/quoteBlueRepay.js +42 -0
- package/lib/esm/helpers/tokenApproval.d.ts +14 -0
- package/lib/esm/helpers/tokenApproval.js +16 -0
- package/lib/esm/helpers/validate.d.ts +1 -1
- package/lib/esm/helpers/validate.js +3 -3
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/midnight/errors.d.ts +2 -1
- package/lib/esm/midnight/errors.js +2 -1
- package/lib/esm/reads/readErc2612Permit.d.ts +22 -0
- package/lib/esm/reads/readErc2612Permit.js +73 -0
- package/lib/esm/reads/validateClientChainId.d.ts +26 -0
- package/lib/esm/reads/validateClientChainId.js +30 -0
- package/lib/esm/transactionPlan/blue/authorization.d.ts +23 -0
- package/lib/esm/transactionPlan/blue/authorization.js +122 -0
- package/lib/esm/transactionPlan/blue/borrow.d.ts +80 -0
- package/lib/esm/transactionPlan/blue/borrow.js +243 -0
- package/lib/esm/transactionPlan/blue/prepare.d.ts +102 -0
- package/lib/esm/transactionPlan/blue/prepare.js +187 -0
- package/lib/esm/transactionPlan/blue/refinance.d.ts +93 -0
- package/lib/esm/transactionPlan/blue/refinance.js +276 -0
- package/lib/esm/transactionPlan/blue/refinanceValidation.d.ts +11 -0
- package/lib/esm/transactionPlan/blue/refinanceValidation.js +17 -0
- package/lib/esm/transactionPlan/blue/repay.d.ts +80 -0
- package/lib/esm/transactionPlan/blue/repay.js +223 -0
- package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.d.ts +98 -0
- package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +373 -0
- package/lib/esm/transactionPlan/blue/supply.d.ts +181 -0
- package/lib/esm/transactionPlan/blue/supply.js +232 -0
- package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +97 -0
- package/lib/esm/transactionPlan/blue/supplyCollateral.js +225 -0
- package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.d.ts +102 -0
- package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +389 -0
- package/lib/esm/transactionPlan/blue/withdraw.d.ts +136 -0
- package/lib/esm/transactionPlan/blue/withdraw.js +334 -0
- package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +123 -0
- package/lib/esm/transactionPlan/blue/withdrawCollateral.js +240 -0
- package/lib/esm/transactionPlan/deposit.js +9 -20
- package/lib/esm/transactionPlan/error.d.ts +50 -0
- package/lib/esm/transactionPlan/error.js +57 -0
- package/lib/esm/transactionPlan/getTokenPermit.d.ts +17 -8
- package/lib/esm/transactionPlan/getTokenPermit.js +35 -7
- package/lib/esm/transactionPlan/index.d.ts +21 -1
- package/lib/esm/transactionPlan/index.js +11 -1
- package/lib/esm/transactionPlan/intent.d.ts +2 -2
- package/lib/esm/transactionPlan/intent.js +8 -4
- package/lib/esm/transactionPlan/permit.d.ts +16 -2
- package/lib/esm/transactionPlan/permit.js +76 -44
- package/lib/esm/transactionPlan/plan.d.ts +67 -12
- package/lib/esm/transactionPlan/plan.js +279 -46
- package/lib/esm/transactionPlan/requirements.d.ts +14 -5
- package/lib/esm/transactionPlan/requirements.js +30 -12
- package/lib/esm/transactionPlan/serde.d.ts +7 -4
- package/lib/esm/transactionPlan/serde.js +73 -54
- package/lib/esm/transactionPlan/types.d.ts +141 -13
- package/lib/esm/transactionPlan/validateActions.js +11 -1
- package/lib/esm/transactionPlan/validation.d.ts +8 -0
- package/lib/esm/transactionPlan/validation.js +88 -1
- package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/deposit.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/redeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv1/withdraw.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/deposit.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/redeem.js +1 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -0
- package/lib/esm/transactionPlan/vaultv2/withdraw.js +1 -0
- package/lib/esm/utils.d.ts +2 -2
- package/lib/esm/utils.js +2 -2
- package/lib/esm/version.d.ts +1 -1
- package/lib/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateBlueSupplyPlanAction = validateBlueSupplyPlanAction;
|
|
4
|
+
exports.prepareBlueSupply = prepareBlueSupply;
|
|
5
|
+
exports.validateBlueSupplyPermitRequest = validateBlueSupplyPermitRequest;
|
|
6
|
+
exports.buildBlueSupply = buildBlueSupply;
|
|
7
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
8
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
9
|
+
const supply_js_1 = require("../../actions/blue/supply.js");
|
|
10
|
+
const common_js_1 = require("../../actions/bundles/common.js");
|
|
11
|
+
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
12
|
+
const error_js_1 = require("../error.js");
|
|
13
|
+
const permit_js_1 = require("../permit.js");
|
|
14
|
+
const validation_js_1 = require("../validation.js");
|
|
15
|
+
const prepare_js_1 = require("./prepare.js");
|
|
16
|
+
/** @internal Validates the stored supply recipe without requiring class instances. */
|
|
17
|
+
function validateBlueSupplyPlanAction(action, path) {
|
|
18
|
+
const inputs = (0, validation_js_1.getPlanObject)(action.inputs, `${path}.inputs`);
|
|
19
|
+
(0, validation_js_1.validatePlanFields)(inputs, {
|
|
20
|
+
path: `${path}.inputs`,
|
|
21
|
+
fields: {
|
|
22
|
+
assets: "uint256 bigint",
|
|
23
|
+
deadline: "uint256 bigint",
|
|
24
|
+
referralFeePct: "uint256 bigint",
|
|
25
|
+
referralFeeRecipient: "address",
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
if (inputs.nativeAssets !== undefined) {
|
|
29
|
+
(0, validation_js_1.validatePlanField)(inputs.nativeAssets, {
|
|
30
|
+
path: `${path}.inputs.nativeAssets`,
|
|
31
|
+
type: "uint256 bigint",
|
|
32
|
+
});
|
|
33
|
+
validateNativeAssets(inputs, `${path}.inputs.nativeAssets`);
|
|
34
|
+
}
|
|
35
|
+
const market = (0, validation_js_1.getPlanObject)(inputs.marketParams, `${path}.inputs.marketParams`);
|
|
36
|
+
(0, validation_js_1.validatePlanFields)(market, {
|
|
37
|
+
path: `${path}.inputs.marketParams`,
|
|
38
|
+
fields: {
|
|
39
|
+
loanToken: "address",
|
|
40
|
+
collateralToken: "address",
|
|
41
|
+
oracle: "address",
|
|
42
|
+
irm: "address",
|
|
43
|
+
lltv: "uint256 bigint",
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
(0, validation_js_1.getPlanObject)(action.signatureRequestIds, `${path}.signatureRequestIds`);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Prepares a static BlueBundlesV1 supply plan from a caller-fetched market snapshot.
|
|
50
|
+
* Insufficient allowance produces an unlimited direct approval, or an exact ERC-2612 permit
|
|
51
|
+
* when supported and requested. Native funding requires wNative and produces no token prerequisite.
|
|
52
|
+
* No Permit2 or Morpho authorization is requested. Only signatures are added before building.
|
|
53
|
+
* @param client - Viem client for the intent chain, used for prerequisite and deployment reads.
|
|
54
|
+
* @param params - Account, chain, snapshot, gross assets and shared funding/fee/expiry intent.
|
|
55
|
+
* @returns A frozen plan with funding prerequisites followed by the supply transaction.
|
|
56
|
+
* @throws {ZeroAccountError} when `account` is the zero address.
|
|
57
|
+
* @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
|
|
58
|
+
* @throws {NonPositiveInputError} when `expiresAt`, `assets` or the deadline is not positive.
|
|
59
|
+
* @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now` (device clock when `now` is omitted).
|
|
60
|
+
* @throws {NegativeInputError} when `referralFee.bps` is negative.
|
|
61
|
+
* @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `assets` exceeds uint256.
|
|
62
|
+
* @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
|
|
63
|
+
* @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
|
|
64
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient or a zero recipient.
|
|
65
|
+
* @throws {MarketIdMismatchError} When the snapshot describes another market.
|
|
66
|
+
* @throws {BlueSnapshotChainMismatchError} When the snapshot describes another chain.
|
|
67
|
+
* @throws {NativeAmountOnNonWNativeAssetError} When native funding targets another token.
|
|
68
|
+
* @throws {ChainIdMismatchError} When the client targets another chain.
|
|
69
|
+
* @throws {MissingContractCodeError} When BlueBundlesV1 or Morpho is not deployed.
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { MarketParams } from "@morpho-org/morpho-sdk/blue/entities";
|
|
73
|
+
* import { fetchMarket } from "@morpho-org/morpho-sdk/blue/fetch";
|
|
74
|
+
* import {
|
|
75
|
+
* applySignatures, buildTransactions, getPendingSignatureRequests,
|
|
76
|
+
* prepareBlueSupply, type BlueSupplyPlanParams,
|
|
77
|
+
* } from "@morpho-org/morpho-sdk";
|
|
78
|
+
* import {
|
|
79
|
+
* createPublicClient, createWalletClient, custom, http, type EIP1193Provider,
|
|
80
|
+
* } from "viem";
|
|
81
|
+
* import { mainnet } from "viem/chains";
|
|
82
|
+
*
|
|
83
|
+
* // Run in a browser with an injected wallet connected to Ethereum mainnet.
|
|
84
|
+
* const { ethereum } = window as Window & { ethereum?: EIP1193Provider };
|
|
85
|
+
* if (!ethereum) throw new Error("Install an Ethereum wallet to continue.");
|
|
86
|
+
* const walletClient = createWalletClient({
|
|
87
|
+
* chain: mainnet, transport: custom(ethereum),
|
|
88
|
+
* });
|
|
89
|
+
* const [account] = await walletClient.requestAddresses();
|
|
90
|
+
* if (!account) throw new Error("Connect a wallet before preparing supply.");
|
|
91
|
+
* const publicClient = createPublicClient({
|
|
92
|
+
* chain: mainnet, transport: http(),
|
|
93
|
+
* });
|
|
94
|
+
* // cbBTC collateral / USDC loan market on Ethereum.
|
|
95
|
+
* const marketParams = new MarketParams({
|
|
96
|
+
* loanToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
97
|
+
* collateralToken: "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf",
|
|
98
|
+
* oracle: "0xA6D6950c9F177F1De7f7757FB33539e3Ec60182a",
|
|
99
|
+
* irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
|
|
100
|
+
* lltv: 860_000_000_000_000_000n,
|
|
101
|
+
* });
|
|
102
|
+
* const block = await publicClient.getBlock();
|
|
103
|
+
* const market = await fetchMarket(marketParams.id, publicClient, {
|
|
104
|
+
* blockNumber: block.number,
|
|
105
|
+
* });
|
|
106
|
+
* const params: BlueSupplyPlanParams = {
|
|
107
|
+
* chainId: mainnet.id,
|
|
108
|
+
* account,
|
|
109
|
+
* marketId: market.id,
|
|
110
|
+
* marketData: { chainId: mainnet.id, market },
|
|
111
|
+
* assets: 1_000_000n, // Supply 1 USDC, before any referral fee.
|
|
112
|
+
* funding: { type: "erc20" },
|
|
113
|
+
* supportSignature: true,
|
|
114
|
+
* now: block.timestamp,
|
|
115
|
+
* expiresAt: Number(block.timestamp + 1_200n),
|
|
116
|
+
* };
|
|
117
|
+
* const plan = await prepareBlueSupply(publicClient, params);
|
|
118
|
+
* const signatures = await Promise.all(
|
|
119
|
+
* getPendingSignatureRequests(plan).map(async (request) => ({
|
|
120
|
+
* id: request.id,
|
|
121
|
+
* signature: await walletClient.signTypedData({
|
|
122
|
+
* ...request.typedData, account,
|
|
123
|
+
* }),
|
|
124
|
+
* })),
|
|
125
|
+
* );
|
|
126
|
+
* const signedPlan = applySignatures(plan, signatures);
|
|
127
|
+
* const transactions = buildTransactions(signedPlan);
|
|
128
|
+
* // transactions: readonly Readonly<Transaction>[].
|
|
129
|
+
* // An optional ERC-20 approval precedes the BlueBundlesV1 supply transaction;
|
|
130
|
+
* // an ERC-2612 permit is embedded in supply instead. No Permit2 is used.
|
|
131
|
+
* // Simulate, then send each { to, data, value } in order from account on mainnet.
|
|
132
|
+
* ```
|
|
133
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
134
|
+
*/
|
|
135
|
+
async function prepareBlueSupply(client, params) {
|
|
136
|
+
const { chainId, account, assets } = params;
|
|
137
|
+
const source = params.marketData.market.params;
|
|
138
|
+
const marketParams = {
|
|
139
|
+
loanToken: source.loanToken,
|
|
140
|
+
collateralToken: source.collateralToken,
|
|
141
|
+
oracle: source.oracle,
|
|
142
|
+
irm: source.irm,
|
|
143
|
+
lltv: source.lltv,
|
|
144
|
+
};
|
|
145
|
+
const nativeAmount = params.funding?.type === "native" ? assets : undefined;
|
|
146
|
+
const prepared = await (0, prepare_js_1.prepareBlue)({ client, params }, (resolvedIntent) => {
|
|
147
|
+
// Run the pure encoder's validation before any prerequisite reads.
|
|
148
|
+
(0, supply_js_1.blueSupply)({
|
|
149
|
+
market: { chainId, marketParams: source },
|
|
150
|
+
args: { userAddress: account, assets, nativeAmount, ...resolvedIntent },
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
id: "blueSupply",
|
|
154
|
+
tokenPull: nativeAmount === undefined
|
|
155
|
+
? { token: source.loanToken, amount: assets }
|
|
156
|
+
: undefined,
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
await (0, validateClientChainId_js_1.validateClientChainId)(client, chainId);
|
|
160
|
+
const { intent, steps, signatureRequests } = prepared;
|
|
161
|
+
return (0, morpho_ts_1.deepFreeze)(structuredClone({
|
|
162
|
+
version: 2,
|
|
163
|
+
chainId,
|
|
164
|
+
account,
|
|
165
|
+
expiresAt: intent.expiresAt,
|
|
166
|
+
...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
|
|
167
|
+
steps: [
|
|
168
|
+
...steps,
|
|
169
|
+
{
|
|
170
|
+
id: "blueSupply",
|
|
171
|
+
label: "Supply to Blue",
|
|
172
|
+
action: {
|
|
173
|
+
type: "blueSupply",
|
|
174
|
+
inputs: {
|
|
175
|
+
chainId,
|
|
176
|
+
account,
|
|
177
|
+
marketParams,
|
|
178
|
+
assets,
|
|
179
|
+
...(nativeAmount !== undefined && { nativeAssets: nativeAmount }),
|
|
180
|
+
...(0, common_js_1.normalizeBundlesCommonParams)(intent),
|
|
181
|
+
},
|
|
182
|
+
signatureRequestIds: signatureRequests.length
|
|
183
|
+
? { tokenPermit: "blueSupply:tokenPermit" }
|
|
184
|
+
: {},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
signatureRequests,
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
/** @internal Binds an exact token permit to this supply's pull. */
|
|
192
|
+
function validateBlueSupplyPermitRequest(request, action) {
|
|
193
|
+
(0, permit_js_1.validateTokenPermitRequest)(request, {
|
|
194
|
+
chainId: action.inputs.chainId,
|
|
195
|
+
account: action.inputs.account,
|
|
196
|
+
deadline: action.inputs.deadline,
|
|
197
|
+
bundler: "bundles.blueBundlesV1",
|
|
198
|
+
asset: action.inputs.marketParams.loanToken,
|
|
199
|
+
amount: action.inputs.nativeAssets === undefined
|
|
200
|
+
? action.inputs.assets
|
|
201
|
+
: undefined,
|
|
202
|
+
exact: true,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/** @internal Encodes supply from stored inputs and resolved signatures without a client. */
|
|
206
|
+
function buildBlueSupply(action, requests) {
|
|
207
|
+
const { chainId, account, marketParams, nativeAssets, ...args } = action.inputs;
|
|
208
|
+
// Enforce native funding consistency even when parsing was skipped.
|
|
209
|
+
validateNativeAssets(action.inputs, "action.inputs.nativeAssets");
|
|
210
|
+
return (0, supply_js_1.blueSupply)({
|
|
211
|
+
market: {
|
|
212
|
+
chainId,
|
|
213
|
+
marketParams: {
|
|
214
|
+
...marketParams,
|
|
215
|
+
id: blue_sdk_1.MarketUtils.getMarketId(marketParams),
|
|
216
|
+
liquidationIncentiveFactor: blue_sdk_1.MarketUtils.getLiquidationIncentiveFactor(marketParams),
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
args: {
|
|
220
|
+
...args,
|
|
221
|
+
nativeAmount: nativeAssets,
|
|
222
|
+
userAddress: account,
|
|
223
|
+
requirementSignature: (0, permit_js_1.getVaultPermit)(requests, {
|
|
224
|
+
id: action.signatureRequestIds.tokenPermit,
|
|
225
|
+
asset: marketParams.loanToken,
|
|
226
|
+
owner: account,
|
|
227
|
+
}),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function validateNativeAssets({ assets, nativeAssets, }, path) {
|
|
232
|
+
if (nativeAssets !== undefined && nativeAssets !== assets) {
|
|
233
|
+
throw new error_js_1.InvalidTransactionPlanError({
|
|
234
|
+
path,
|
|
235
|
+
reason: "nativeAssets must equal assets when present",
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import { type BlueSupplyCollateralAction, type ReferenceTimestampParams, type Transaction } from "../../types/index.js";
|
|
4
|
+
import type { Funding, IntentParams } from "../intent.js";
|
|
5
|
+
import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
|
|
6
|
+
import { type BlueMarketSnapshot } from "./prepare.js";
|
|
7
|
+
type SupplyCollateralAction = Extract<Action, {
|
|
8
|
+
type: "blueSupplyCollateral";
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Intent for supplying collateral without borrowing in a Blue market.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import type { BlueSupplyCollateralPlanParams } from "@morpho-org/morpho-sdk";
|
|
15
|
+
* const withNativeFunding = (params: BlueSupplyCollateralPlanParams): BlueSupplyCollateralPlanParams =>
|
|
16
|
+
* ({ ...params, funding: { type: "native" } }); // Only for a wNative collateral market.
|
|
17
|
+
* ```
|
|
18
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
19
|
+
*/
|
|
20
|
+
export interface BlueSupplyCollateralPlanParams extends IntentParams, ReferenceTimestampParams {
|
|
21
|
+
/** ID of the market in which to supply collateral. */
|
|
22
|
+
readonly marketId: MarketId;
|
|
23
|
+
/** Market snapshot fetched by the caller on the target chain. */
|
|
24
|
+
readonly marketData: BlueMarketSnapshot;
|
|
25
|
+
/** Gross collateral supplied in token base units, before referral fees. */
|
|
26
|
+
readonly collateralAssets: bigint;
|
|
27
|
+
/** Defaults to ERC-20; native funding requires wNative collateral and stores `nativeAssets = collateralAssets`. */
|
|
28
|
+
readonly funding?: Funding;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Prepares a collateral-only supply through BlueBundlesV1 from a caller-fetched market snapshot.
|
|
32
|
+
* No Morpho authorization is needed. ERC-20 funding reuses an adequate allowance, requests an
|
|
33
|
+
* ERC-2612 permit when supported and enabled, or adds a direct approval step. Native funding
|
|
34
|
+
* needs neither an approval nor a signature.
|
|
35
|
+
* @param client - Viem client for the chain on which the market was fetched.
|
|
36
|
+
* @param params - Market snapshot, amount and shared transaction intent.
|
|
37
|
+
* @param params.chainId - Chain containing the market and BlueBundlesV1.
|
|
38
|
+
* @param params.account - Non-zero account funding, signing and sending the transaction.
|
|
39
|
+
* @param params.marketId - ID of the market in which to supply collateral.
|
|
40
|
+
* @param params.marketData - Caller-fetched snapshot of the selected market and chain.
|
|
41
|
+
* @param params.collateralAssets - Positive gross collateral amount in token base units.
|
|
42
|
+
* @param params.funding.type - `"erc20"` (default) or `"native"` for wNative collateral.
|
|
43
|
+
* @param params.referralFee.bps - Optional fee in integer basis points, below 10,000.
|
|
44
|
+
* @param params.referralFee.recipient - Non-zero recipient required for a positive fee.
|
|
45
|
+
* @param params.supportSignature - Prefer an ERC-2612 permit where available; defaults to false.
|
|
46
|
+
* @param params.now - Reference Unix timestamp in seconds; defaults to the device clock.
|
|
47
|
+
* @param params.expiresAt - Expiry in Unix seconds, later than `now`; defaults to `now + 2h`.
|
|
48
|
+
* @param params.calldataOrigin - Optional non-empty hex bytes appended to each built transaction.
|
|
49
|
+
* @param params.deployless - Whether permit metadata reads may use deployless aggregation.
|
|
50
|
+
* @returns A frozen plan containing the collateral supply step and any token prerequisites.
|
|
51
|
+
* @throws {BlueSnapshotChainMismatchError} when the snapshot comes from another chain.
|
|
52
|
+
* @throws {MarketIdMismatchError} when the snapshot does not identify the selected market.
|
|
53
|
+
* @throws {ZeroAccountError} when `account` is the zero address.
|
|
54
|
+
* @throws {NonIntegerInputError} when expiry or fee basis points are not safe integers.
|
|
55
|
+
* @throws {NonPositiveInputError} when the collateral amount or expiry is zero.
|
|
56
|
+
* @throws {ExpiredDeadlineError} when expiry is at or before the reference time.
|
|
57
|
+
* @throws {NegativeInputError} when the collateral amount or fee basis points are negative.
|
|
58
|
+
* @throws {InputExceedsMaxError} when collateral exceeds uint256 or fee basis points exceed 10,000.
|
|
59
|
+
* @throws {InvalidCalldataOriginError} when attribution bytes are empty or malformed.
|
|
60
|
+
* @throws {InvalidTransactionPlanError} when `calldataOrigin` exceeds 1 MiB.
|
|
61
|
+
* @throws {ReferralFeePctExceededError} when the referral fee is 100%.
|
|
62
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
|
|
63
|
+
* @throws {ChainWNativeMissingError} when native funding is requested on a chain with no wNative.
|
|
64
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets non-wNative collateral.
|
|
65
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
66
|
+
* @throws {UnknownAddressError} when Blue or BlueBundlesV1 is not registered.
|
|
67
|
+
* @throws {ChainIdMismatchError} when the client declares or serves another chain.
|
|
68
|
+
* @throws {MissingContractCodeError} when Blue or BlueBundlesV1 has no code on the chain.
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { prepareBlueSupplyCollateral } from "@morpho-org/morpho-sdk";
|
|
72
|
+
* import { fetchMarket } from "@morpho-org/morpho-sdk/blue/fetch";
|
|
73
|
+
* import { createPublicClient, http } from "viem";
|
|
74
|
+
* import { mainnet } from "viem/chains";
|
|
75
|
+
*
|
|
76
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
77
|
+
* const prepare = async (marketId: Parameters<typeof fetchMarket>[0], account: `0x${string}`) =>
|
|
78
|
+
* prepareBlueSupplyCollateral(client, {
|
|
79
|
+
* chainId: mainnet.id,
|
|
80
|
+
* account,
|
|
81
|
+
* marketId,
|
|
82
|
+
* collateralAssets: 1_000_000n,
|
|
83
|
+
* marketData: { chainId: mainnet.id, market: await fetchMarket(marketId, client) },
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
87
|
+
*/
|
|
88
|
+
export declare function prepareBlueSupplyCollateral(client: Client, params: BlueSupplyCollateralPlanParams): Promise<TransactionPlan>;
|
|
89
|
+
/** @internal Validates the stored collateral supply action and its token permit role. */
|
|
90
|
+
export declare function validateSupplyCollateralPlanAction(action: Record<string, unknown>, path: string): void;
|
|
91
|
+
/** @internal Checks that the permit authorizes this exact ERC-20 collateral pull. */
|
|
92
|
+
export declare function validateSupplyCollateralPermitRequest(request: Extract<SignatureRequest, {
|
|
93
|
+
kind: "permit";
|
|
94
|
+
}>, action: SupplyCollateralAction): void;
|
|
95
|
+
/** @internal Encodes a collateral supply from stored inputs and an optional signed permit. */
|
|
96
|
+
export declare function buildBlueSupplyCollateral(action: SupplyCollateralAction, requests: readonly SignatureRequest[]): Readonly<Transaction<BlueSupplyCollateralAction>>;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareBlueSupplyCollateral = prepareBlueSupplyCollateral;
|
|
4
|
+
exports.validateSupplyCollateralPlanAction = validateSupplyCollateralPlanAction;
|
|
5
|
+
exports.validateSupplyCollateralPermitRequest = validateSupplyCollateralPermitRequest;
|
|
6
|
+
exports.buildBlueSupplyCollateral = buildBlueSupplyCollateral;
|
|
7
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
8
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
9
|
+
const viem_1 = require("viem");
|
|
10
|
+
const supplyCollateral_js_1 = require("../../actions/blue/supplyCollateral.js");
|
|
11
|
+
const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
|
|
12
|
+
const validate_js_1 = require("../../helpers/validate.js");
|
|
13
|
+
const index_js_1 = require("../../types/index.js");
|
|
14
|
+
const error_js_1 = require("../error.js");
|
|
15
|
+
const permit_js_1 = require("../permit.js");
|
|
16
|
+
const validation_js_1 = require("../validation.js");
|
|
17
|
+
const prepare_js_1 = require("./prepare.js");
|
|
18
|
+
const actionId = "blueSupplyCollateral";
|
|
19
|
+
/**
|
|
20
|
+
* Prepares a collateral-only supply through BlueBundlesV1 from a caller-fetched market snapshot.
|
|
21
|
+
* No Morpho authorization is needed. ERC-20 funding reuses an adequate allowance, requests an
|
|
22
|
+
* ERC-2612 permit when supported and enabled, or adds a direct approval step. Native funding
|
|
23
|
+
* needs neither an approval nor a signature.
|
|
24
|
+
* @param client - Viem client for the chain on which the market was fetched.
|
|
25
|
+
* @param params - Market snapshot, amount and shared transaction intent.
|
|
26
|
+
* @param params.chainId - Chain containing the market and BlueBundlesV1.
|
|
27
|
+
* @param params.account - Non-zero account funding, signing and sending the transaction.
|
|
28
|
+
* @param params.marketId - ID of the market in which to supply collateral.
|
|
29
|
+
* @param params.marketData - Caller-fetched snapshot of the selected market and chain.
|
|
30
|
+
* @param params.collateralAssets - Positive gross collateral amount in token base units.
|
|
31
|
+
* @param params.funding.type - `"erc20"` (default) or `"native"` for wNative collateral.
|
|
32
|
+
* @param params.referralFee.bps - Optional fee in integer basis points, below 10,000.
|
|
33
|
+
* @param params.referralFee.recipient - Non-zero recipient required for a positive fee.
|
|
34
|
+
* @param params.supportSignature - Prefer an ERC-2612 permit where available; defaults to false.
|
|
35
|
+
* @param params.now - Reference Unix timestamp in seconds; defaults to the device clock.
|
|
36
|
+
* @param params.expiresAt - Expiry in Unix seconds, later than `now`; defaults to `now + 2h`.
|
|
37
|
+
* @param params.calldataOrigin - Optional non-empty hex bytes appended to each built transaction.
|
|
38
|
+
* @param params.deployless - Whether permit metadata reads may use deployless aggregation.
|
|
39
|
+
* @returns A frozen plan containing the collateral supply step and any token prerequisites.
|
|
40
|
+
* @throws {BlueSnapshotChainMismatchError} when the snapshot comes from another chain.
|
|
41
|
+
* @throws {MarketIdMismatchError} when the snapshot does not identify the selected market.
|
|
42
|
+
* @throws {ZeroAccountError} when `account` is the zero address.
|
|
43
|
+
* @throws {NonIntegerInputError} when expiry or fee basis points are not safe integers.
|
|
44
|
+
* @throws {NonPositiveInputError} when the collateral amount or expiry is zero.
|
|
45
|
+
* @throws {ExpiredDeadlineError} when expiry is at or before the reference time.
|
|
46
|
+
* @throws {NegativeInputError} when the collateral amount or fee basis points are negative.
|
|
47
|
+
* @throws {InputExceedsMaxError} when collateral exceeds uint256 or fee basis points exceed 10,000.
|
|
48
|
+
* @throws {InvalidCalldataOriginError} when attribution bytes are empty or malformed.
|
|
49
|
+
* @throws {InvalidTransactionPlanError} when `calldataOrigin` exceeds 1 MiB.
|
|
50
|
+
* @throws {ReferralFeePctExceededError} when the referral fee is 100%.
|
|
51
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
|
|
52
|
+
* @throws {ChainWNativeMissingError} when native funding is requested on a chain with no wNative.
|
|
53
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets non-wNative collateral.
|
|
54
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the registry.
|
|
55
|
+
* @throws {UnknownAddressError} when Blue or BlueBundlesV1 is not registered.
|
|
56
|
+
* @throws {ChainIdMismatchError} when the client declares or serves another chain.
|
|
57
|
+
* @throws {MissingContractCodeError} when Blue or BlueBundlesV1 has no code on the chain.
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { prepareBlueSupplyCollateral } from "@morpho-org/morpho-sdk";
|
|
61
|
+
* import { fetchMarket } from "@morpho-org/morpho-sdk/blue/fetch";
|
|
62
|
+
* import { createPublicClient, http } from "viem";
|
|
63
|
+
* import { mainnet } from "viem/chains";
|
|
64
|
+
*
|
|
65
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
66
|
+
* const prepare = async (marketId: Parameters<typeof fetchMarket>[0], account: `0x${string}`) =>
|
|
67
|
+
* prepareBlueSupplyCollateral(client, {
|
|
68
|
+
* chainId: mainnet.id,
|
|
69
|
+
* account,
|
|
70
|
+
* marketId,
|
|
71
|
+
* collateralAssets: 1_000_000n,
|
|
72
|
+
* marketData: { chainId: mainnet.id, market: await fetchMarket(marketId, client) },
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
76
|
+
*/
|
|
77
|
+
async function prepareBlueSupplyCollateral(client, params) {
|
|
78
|
+
const { chainId, account, collateralAssets, marketData } = params;
|
|
79
|
+
const { id, loanToken, collateralToken, oracle, irm, lltv } = marketData.market.params;
|
|
80
|
+
const marketParams = { id, loanToken, collateralToken, oracle, irm, lltv };
|
|
81
|
+
const native = params.funding?.type === "native";
|
|
82
|
+
const { intent, steps, signatureRequests } = await (0, prepare_js_1.prepareBlue)({ client, params }, (resolvedIntent) => {
|
|
83
|
+
(0, validate_js_1.validateUint256Field)("collateralAssets", collateralAssets);
|
|
84
|
+
if (collateralAssets === 0n) {
|
|
85
|
+
throw new index_js_1.NonPositiveInputError("collateralAssets", collateralAssets);
|
|
86
|
+
}
|
|
87
|
+
// Reject invalid fees before resolving any token prerequisites.
|
|
88
|
+
(0, validate_js_1.validateReferralFee)(resolvedIntent);
|
|
89
|
+
if (native) {
|
|
90
|
+
(0, validate_js_1.validateNativeAsset)(chainId, collateralToken);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
id: actionId,
|
|
94
|
+
tokenPull: native
|
|
95
|
+
? undefined
|
|
96
|
+
: { token: collateralToken, amount: collateralAssets },
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
if (native) {
|
|
100
|
+
await (0, validateClientChainId_js_1.validateClientChainId)(client, chainId);
|
|
101
|
+
}
|
|
102
|
+
const tokenPermit = signatureRequests.find(({ kind }) => kind === "permit");
|
|
103
|
+
return (0, morpho_ts_1.deepFreeze)(structuredClone({
|
|
104
|
+
version: 2,
|
|
105
|
+
chainId,
|
|
106
|
+
account,
|
|
107
|
+
expiresAt: intent.expiresAt,
|
|
108
|
+
...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
|
|
109
|
+
steps: [
|
|
110
|
+
...steps,
|
|
111
|
+
{
|
|
112
|
+
id: actionId,
|
|
113
|
+
label: "Supply Blue collateral",
|
|
114
|
+
action: {
|
|
115
|
+
type: actionId,
|
|
116
|
+
inputs: {
|
|
117
|
+
chainId,
|
|
118
|
+
account,
|
|
119
|
+
marketParams,
|
|
120
|
+
collateralAssets,
|
|
121
|
+
...(native && { nativeAssets: collateralAssets }),
|
|
122
|
+
deadline: intent.deadline,
|
|
123
|
+
referralFeePct: intent.referralFeePct,
|
|
124
|
+
referralFeeRecipient: intent.referralFeeRecipient ?? viem_1.zeroAddress,
|
|
125
|
+
},
|
|
126
|
+
signatureRequestIds: tokenPermit
|
|
127
|
+
? { tokenPermit: tokenPermit.id }
|
|
128
|
+
: {},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
signatureRequests,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
/** @internal Validates the stored collateral supply action and its token permit role. */
|
|
136
|
+
function validateSupplyCollateralPlanAction(action, path) {
|
|
137
|
+
const inputsPath = `${path}.inputs`;
|
|
138
|
+
const inputs = (0, validation_js_1.getPlanObject)(action.inputs, inputsPath);
|
|
139
|
+
(0, validation_js_1.validatePlanFields)(inputs, {
|
|
140
|
+
path: inputsPath,
|
|
141
|
+
fields: {
|
|
142
|
+
collateralAssets: "uint256 bigint",
|
|
143
|
+
deadline: "uint256 bigint",
|
|
144
|
+
referralFeePct: "uint256 bigint",
|
|
145
|
+
referralFeeRecipient: "address",
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
const marketPath = `${inputsPath}.marketParams`;
|
|
149
|
+
const market = (0, validation_js_1.getPlanObject)(inputs.marketParams, marketPath);
|
|
150
|
+
(0, validation_js_1.validatePlanFields)(market, {
|
|
151
|
+
path: marketPath,
|
|
152
|
+
fields: {
|
|
153
|
+
id: "hex",
|
|
154
|
+
loanToken: "address",
|
|
155
|
+
collateralToken: "address",
|
|
156
|
+
irm: "address",
|
|
157
|
+
oracle: "address",
|
|
158
|
+
lltv: "uint256 bigint",
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
if (typeof market.id !== "string" || market.id.length !== 66) {
|
|
162
|
+
throw new error_js_1.InvalidTransactionPlanError({
|
|
163
|
+
path: `${marketPath}.id`,
|
|
164
|
+
reason: "expected a 32-byte market ID",
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
validateNativeAssets(inputs, `${inputsPath}.nativeAssets`);
|
|
168
|
+
const rolesPath = `${path}.signatureRequestIds`;
|
|
169
|
+
const roles = (0, validation_js_1.getPlanObject)(action.signatureRequestIds, rolesPath);
|
|
170
|
+
for (const role of Object.keys(roles)) {
|
|
171
|
+
if (role !== "tokenPermit" || inputs.nativeAssets !== undefined) {
|
|
172
|
+
throw new error_js_1.InvalidTransactionPlanError({
|
|
173
|
+
path: `${rolesPath}.${role}`,
|
|
174
|
+
reason: "only ERC-20 funding can reference a token permit",
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function validateNativeAssets({ collateralAssets, nativeAssets, }, path) {
|
|
180
|
+
if (nativeAssets !== undefined &&
|
|
181
|
+
(nativeAssets === 0n || nativeAssets !== collateralAssets)) {
|
|
182
|
+
throw new error_js_1.InvalidTransactionPlanError({
|
|
183
|
+
path,
|
|
184
|
+
reason: "expected the full collateral amount for native funding",
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** @internal Checks that the permit authorizes this exact ERC-20 collateral pull. */
|
|
189
|
+
function validateSupplyCollateralPermitRequest(request, action) {
|
|
190
|
+
const { chainId, account, marketParams, collateralAssets, deadline, nativeAssets, } = action.inputs;
|
|
191
|
+
if (action.signatureRequestIds.tokenPermit !== request.id ||
|
|
192
|
+
nativeAssets !== undefined) {
|
|
193
|
+
throw new error_js_1.TransactionPlanSignatureMismatchError(request.id, "reference");
|
|
194
|
+
}
|
|
195
|
+
(0, permit_js_1.validateTokenPermitRequest)(request, {
|
|
196
|
+
chainId,
|
|
197
|
+
account,
|
|
198
|
+
asset: marketParams.collateralToken,
|
|
199
|
+
amount: collateralAssets,
|
|
200
|
+
deadline,
|
|
201
|
+
bundler: "bundles.blueBundlesV1",
|
|
202
|
+
exact: true,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/** @internal Encodes a collateral supply from stored inputs and an optional signed permit. */
|
|
206
|
+
function buildBlueSupplyCollateral(action, requests) {
|
|
207
|
+
const { chainId, account, marketParams, collateralAssets, nativeAssets, deadline, referralFeePct, referralFeeRecipient, } = action.inputs;
|
|
208
|
+
validateNativeAssets(action.inputs, "action.inputs.nativeAssets");
|
|
209
|
+
return (0, supplyCollateral_js_1.blueSupplyCollateral)({
|
|
210
|
+
market: {
|
|
211
|
+
chainId,
|
|
212
|
+
marketParams: {
|
|
213
|
+
...marketParams,
|
|
214
|
+
liquidationIncentiveFactor: blue_sdk_1.MarketUtils.getLiquidationIncentiveFactor(marketParams),
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
args: {
|
|
218
|
+
collateralAssets,
|
|
219
|
+
nativeAmount: nativeAssets,
|
|
220
|
+
deadline,
|
|
221
|
+
referralFeePct,
|
|
222
|
+
referralFeeRecipient,
|
|
223
|
+
userAddress: account,
|
|
224
|
+
requirementSignature: (0, permit_js_1.getSignedTokenPermit)(requests, {
|
|
225
|
+
id: action.signatureRequestIds.tokenPermit,
|
|
226
|
+
asset: marketParams.collateralToken,
|
|
227
|
+
owner: account,
|
|
228
|
+
}),
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Client } from "viem";
|
|
3
|
+
import { type ReferenceTimestampParams, type VaultV2BlueReallocation } from "../../types/index.js";
|
|
4
|
+
import type { Funding, IntentParams } from "../intent.js";
|
|
5
|
+
import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
|
|
6
|
+
import { type BlueMarketSnapshot, type BluePositionSnapshot } from "./prepare.js";
|
|
7
|
+
/**
|
|
8
|
+
* Intent and caller-fetched snapshots for an atomic Blue collateral supply and borrow.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { BlueSupplyCollateralBorrowPlanParams } from "@morpho-org/morpho-sdk";
|
|
12
|
+
* const withAmounts = (params: BlueSupplyCollateralBorrowPlanParams) => ({ ...params, collateralAssets: 10n ** 18n, borrowAssets: 100n });
|
|
13
|
+
* ```
|
|
14
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
15
|
+
*/
|
|
16
|
+
export interface BlueSupplyCollateralBorrowPlanParams extends IntentParams, ReferenceTimestampParams {
|
|
17
|
+
readonly marketId: MarketId;
|
|
18
|
+
readonly marketData: BlueMarketSnapshot;
|
|
19
|
+
/** Required when borrowing; belongs to the account and market. */
|
|
20
|
+
readonly positionData?: BluePositionSnapshot;
|
|
21
|
+
readonly collateralAssets: bigint;
|
|
22
|
+
readonly borrowAssets: bigint;
|
|
23
|
+
readonly funding?: Funding;
|
|
24
|
+
readonly reallocations?: Iterable<VaultV2BlueReallocation>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Prepares collateral funding and Morpho authorization for one BlueBundlesV1 call. Snapshots
|
|
28
|
+
* are validated before reads; the position is quoted through max(now, lastUpdate) + two hours,
|
|
29
|
+
* matching the stable entity handle. The SDK never signs. Native funding is exclusive and
|
|
30
|
+
* only supports the chain's wrapped native collateral token. Reallocation penalties reduce proceeds.
|
|
31
|
+
* @param client - Viem client used for prerequisite and registered-contract reads.
|
|
32
|
+
* @param params - Intent, amounts and caller-fetched market/position snapshots.
|
|
33
|
+
* @returns A data-only plan with approvals, authorization and the combined operation.
|
|
34
|
+
* @throws {ZeroAccountError} when the account is zero.
|
|
35
|
+
* @throws {NonIntegerInputError} when expiry or referral basis points are not safe integers.
|
|
36
|
+
* @throws {ExpiredDeadlineError} when expiry is not later than the reference timestamp.
|
|
37
|
+
* @throws {InvalidCalldataOriginError} when attribution bytes are malformed or empty.
|
|
38
|
+
* @throws {BlueSnapshotChainMismatchError} when a snapshot belongs to another chain.
|
|
39
|
+
* @throws {MarketIdMismatchError} when a snapshot or market tuple belongs to another market.
|
|
40
|
+
* @throws {AccrualPositionUserMismatchError} when the position belongs to another account.
|
|
41
|
+
* @throws {MissingAccrualPositionError} when a borrow has no position snapshot.
|
|
42
|
+
* @throws {MissingMarketPriceError} when the position snapshot has no usable oracle price.
|
|
43
|
+
* @throws {BorrowExceedsSafeLtvError} when the quoted debt exceeds the buffered LLTV bound.
|
|
44
|
+
* @throws {NegativeInputError} when an amount or referral fee is negative.
|
|
45
|
+
* @throws {NonPositiveInputError} when both legs are zero, or a reallocation amount is not positive.
|
|
46
|
+
* @throws {InputExceedsMaxError} when an amount exceeds its ABI bound or aggregate penalties exceed borrowing.
|
|
47
|
+
* @throws {ReallocationsRequireBorrowError} when reallocations accompany no borrow.
|
|
48
|
+
* @throws {InvalidReallocationAddressError} when a vault or adapter address is malformed.
|
|
49
|
+
* @throws {InvalidVaultV2BlueReallocationShapeError} when a reallocation has an invalid shape.
|
|
50
|
+
* @throws {InvalidReallocationSourceTypeError} when a source is incomplete or unknown.
|
|
51
|
+
* @throws {InconsistentReallocationPenaltyError} when one vault has conflicting penalties.
|
|
52
|
+
* @throws {ReallocationWithdrawalOnTargetMarketError} when the source equals the target market.
|
|
53
|
+
* @throws {ReallocationLoanTokenMismatchError} when a source has another loan token.
|
|
54
|
+
* @throws {ReferralFeePctExceededError} when the referral fee reaches 100%.
|
|
55
|
+
* @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
|
|
56
|
+
* @throws {NativeAmountOnNonWNativeAssetError} when native funding targets another token.
|
|
57
|
+
* @throws {ChainWNativeMissingError} when native funding has no registered wrapped native token.
|
|
58
|
+
* @throws {ChainIdMismatchError} when the client targets another chain.
|
|
59
|
+
* @throws {MissingContractCodeError} when a registered execution target is absent.
|
|
60
|
+
* @throws {UnsupportedChainIdError} when the chain is not registered.
|
|
61
|
+
* @throws {UnknownAddressError} when the chain has no registered execution target.
|
|
62
|
+
* @see blueSupplyCollateralBorrow for the underlying encoder constraints.
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { prepareBlueSupplyCollateralBorrow } from "@morpho-org/morpho-sdk";
|
|
66
|
+
* import { MarketParams } from "@morpho-org/morpho-sdk/blue/entities";
|
|
67
|
+
* import { fetchAccrualPosition, fetchMarket } from "@morpho-org/morpho-sdk/blue/fetch";
|
|
68
|
+
* import { createPublicClient, http, parseEther } from "viem";
|
|
69
|
+
* import { mainnet } from "viem/chains";
|
|
70
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
71
|
+
* const marketParams = new MarketParams({
|
|
72
|
+
* loanToken: "0xdC035D45d973E3EC169d2276DDab16f1e407384F",
|
|
73
|
+
* collateralToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
74
|
+
* oracle: "0x76b2242ea5BE1FCBBF4206EA09601EA5aB22Af4d",
|
|
75
|
+
* irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
|
|
76
|
+
* lltv: 860000000000000000n,
|
|
77
|
+
* });
|
|
78
|
+
* const account = "0x0000000000000000000000000000000000000001";
|
|
79
|
+
* const block = await client.getBlock();
|
|
80
|
+
* const fetchParams = { blockNumber: block.number };
|
|
81
|
+
* const plan = await prepareBlueSupplyCollateralBorrow(client, {
|
|
82
|
+
* chainId: mainnet.id, account, marketId: marketParams.id,
|
|
83
|
+
* marketData: { chainId: mainnet.id, market: await fetchMarket(marketParams.id, client, fetchParams) },
|
|
84
|
+
* positionData: { chainId: mainnet.id, position: await fetchAccrualPosition(account, marketParams.id, client, fetchParams) },
|
|
85
|
+
* collateralAssets: parseEther("1"), borrowAssets: parseEther("100"),
|
|
86
|
+
* now: block.timestamp, supportSignature: true,
|
|
87
|
+
* });
|
|
88
|
+
* // plan: TransactionPlan; sign its pending requests externally before building.
|
|
89
|
+
* ```
|
|
90
|
+
* @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
|
|
91
|
+
*/
|
|
92
|
+
export declare function prepareBlueSupplyCollateralBorrow(client: Client, params: BlueSupplyCollateralBorrowPlanParams): Promise<TransactionPlan>;
|
|
93
|
+
/** @internal Binds either signature role to the combined operation's stored inputs. */
|
|
94
|
+
export declare function validateSupplyCollateralBorrowRequest(request: SignatureRequest, action: Extract<Action, {
|
|
95
|
+
type: "blueSupplyCollateralBorrow";
|
|
96
|
+
}>): void;
|
|
97
|
+
/** @internal Encodes the operation using only plain inputs and attached signatures. */
|
|
98
|
+
export declare function buildBlueSupplyCollateralBorrow(action: Extract<Action, {
|
|
99
|
+
type: "blueSupplyCollateralBorrow";
|
|
100
|
+
}>, requests: readonly SignatureRequest[]): Readonly<import("../../index.js").Transaction<import("../../index.js").BlueSupplyCollateralBorrowAction>>;
|
|
101
|
+
/** @internal Validates the complete stored input and reallocation shape. */
|
|
102
|
+
export declare function validateSupplyCollateralBorrowPlanAction(action: Record<string, unknown>, path: string): void;
|