@morpho-org/morpho-sdk 5.9.0 → 6.0.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/MIGRATION-v5-to-v6.md +201 -0
- package/README.md +58 -40
- package/lib/cjs/actions/blue/borrow.d.ts +62 -61
- package/lib/cjs/actions/blue/borrow.js +48 -80
- package/lib/cjs/actions/blue/common.d.ts +99 -0
- package/lib/cjs/actions/blue/common.js +318 -0
- package/lib/cjs/actions/blue/refinance.d.ts +72 -112
- package/lib/cjs/actions/blue/refinance.js +97 -199
- package/lib/cjs/actions/blue/repay.d.ts +72 -68
- package/lib/cjs/actions/blue/repay.js +56 -118
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
- package/lib/cjs/actions/blue/supply.d.ts +62 -45
- package/lib/cjs/actions/blue/supply.js +94 -70
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
- package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
- package/lib/cjs/actions/blue/withdraw.js +101 -97
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
- package/lib/cjs/actions/index.d.ts +1 -1
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
- package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/cjs/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +207 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +2 -0
- package/lib/cjs/actions/requirements/blue/index.js +2 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +7 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +33 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +123 -0
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- package/lib/cjs/bundler/actions.d.ts +4 -4
- package/lib/cjs/bundler/actions.js +4 -4
- package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
- package/lib/cjs/client/morphoViemExtension.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +806 -374
- package/lib/cjs/entities/blue/blue.js +447 -696
- package/lib/cjs/entities/index.d.ts +1 -0
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/validate.d.ts +14 -23
- package/lib/cjs/helpers/validate.js +30 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +2 -0
- package/lib/cjs/types/action.d.ts +146 -162
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +141 -14
- package/lib/cjs/types/error.js +198 -21
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- package/lib/esm/actions/blue/borrow.d.ts +62 -61
- package/lib/esm/actions/blue/borrow.js +48 -80
- package/lib/esm/actions/blue/common.d.ts +99 -0
- package/lib/esm/actions/blue/common.js +306 -0
- package/lib/esm/actions/blue/refinance.d.ts +72 -112
- package/lib/esm/actions/blue/refinance.js +98 -200
- package/lib/esm/actions/blue/repay.d.ts +72 -68
- package/lib/esm/actions/blue/repay.js +56 -118
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
- package/lib/esm/actions/blue/supply.d.ts +62 -45
- package/lib/esm/actions/blue/supply.js +94 -70
- package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/esm/actions/blue/supplyCollateral.js +52 -69
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
- package/lib/esm/actions/blue/withdraw.d.ts +68 -79
- package/lib/esm/actions/blue/withdraw.js +100 -96
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
- package/lib/esm/actions/index.d.ts +1 -1
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
- package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.d.ts +80 -0
- package/lib/esm/actions/requirements/blue/getBlueBundlesV1TokenRequirements.js +203 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +2 -0
- package/lib/esm/actions/requirements/blue/index.js +2 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +8 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +34 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2TransferFrom.js +119 -0
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- package/lib/esm/bundler/actions.d.ts +4 -4
- package/lib/esm/bundler/actions.js +4 -4
- package/lib/esm/client/morphoViemExtension.d.ts +2 -1
- package/lib/esm/client/morphoViemExtension.js +2 -1
- package/lib/esm/entities/blue/blue.d.ts +806 -374
- package/lib/esm/entities/blue/blue.js +453 -702
- package/lib/esm/entities/index.d.ts +1 -0
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/validate.d.ts +14 -23
- package/lib/esm/helpers/validate.js +30 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +2 -0
- package/lib/esm/types/action.d.ts +146 -162
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +141 -14
- package/lib/esm/types/error.js +184 -17
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +4 -3
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { getChainAddresses, VaultV2BluePublicAllocatorConfigUtils, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { validateAndNormalizeReallocations } from "../../helpers/validate.js";
|
|
3
|
-
/**
|
|
4
|
-
* Builds PublicAllocator V1 reallocation actions for a Morpho Blue target market.
|
|
5
|
-
*
|
|
6
|
-
* Preserves the supplied reallocation order and aggregates each allocator call's native fee.
|
|
7
|
-
*
|
|
8
|
-
* @param params.reallocations - Validated Vault V1 reallocations to execute.
|
|
9
|
-
* @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
|
|
10
|
-
* @returns The reallocation actions and aggregate native fee, with zero V2 penalty assets.
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
export const buildVaultV1ReallocationActions = ({ reallocations, targetMarketParams, }) => {
|
|
14
|
-
let fee = 0n;
|
|
15
|
-
const actions = [];
|
|
16
|
-
for (const reallocation of reallocations) {
|
|
17
|
-
actions.push({
|
|
18
|
-
type: "reallocateTo",
|
|
19
|
-
args: [
|
|
20
|
-
reallocation.vault,
|
|
21
|
-
reallocation.fee,
|
|
22
|
-
reallocation.withdrawals.map((withdrawal) => ({
|
|
23
|
-
marketParams: withdrawal.marketParams,
|
|
24
|
-
amount: withdrawal.amount,
|
|
25
|
-
})),
|
|
26
|
-
targetMarketParams,
|
|
27
|
-
false,
|
|
28
|
-
],
|
|
29
|
-
});
|
|
30
|
-
fee += reallocation.fee;
|
|
31
|
-
}
|
|
32
|
-
return { actions, fee, penaltyAssets: 0n };
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Builds BluePublicAllocator reallocation actions for a Morpho Blue target market.
|
|
36
|
-
*
|
|
37
|
-
* Prepends one aggregate loan-token funding action when penalties are non-zero, then builds one
|
|
38
|
-
* allocator action per validated Vault V2 reallocation.
|
|
39
|
-
*
|
|
40
|
-
* @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
|
|
41
|
-
* @param params.reallocations - Validated Vault V2 Blue reallocations to execute.
|
|
42
|
-
* @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
|
|
43
|
-
* @param params.penaltyFundingSource - Optional source of penalty assets. Defaults to the
|
|
44
|
-
* transaction initiator.
|
|
45
|
-
* @returns The funding and reallocation actions and aggregate penalty assets, with zero native fee.
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
export const buildVaultV2BlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource = "initiator", }) => {
|
|
49
|
-
const actions = [];
|
|
50
|
-
const penaltyAssets = reallocations.reduce((total, reallocation) => total +
|
|
51
|
-
VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
|
|
52
|
-
if (penaltyAssets > 0n) {
|
|
53
|
-
const { bundler3: { bundler3, generalAdapter1 }, } = getChainAddresses(chainId);
|
|
54
|
-
actions.push(penaltyFundingSource === "generalAdapter1"
|
|
55
|
-
? {
|
|
56
|
-
type: "erc20Transfer",
|
|
57
|
-
args: [
|
|
58
|
-
targetMarketParams.loanToken,
|
|
59
|
-
bundler3,
|
|
60
|
-
penaltyAssets,
|
|
61
|
-
generalAdapter1,
|
|
62
|
-
false,
|
|
63
|
-
],
|
|
64
|
-
}
|
|
65
|
-
: {
|
|
66
|
-
type: "erc20TransferFrom",
|
|
67
|
-
args: [
|
|
68
|
-
targetMarketParams.loanToken,
|
|
69
|
-
penaltyAssets,
|
|
70
|
-
bundler3,
|
|
71
|
-
false,
|
|
72
|
-
],
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
for (const reallocation of reallocations) {
|
|
76
|
-
actions.push(reallocation.from.type === "market"
|
|
77
|
-
? {
|
|
78
|
-
type: "vaultV2BluePublicAllocatorReallocate",
|
|
79
|
-
args: [
|
|
80
|
-
reallocation.vault,
|
|
81
|
-
reallocation.from.adapter,
|
|
82
|
-
reallocation.from.marketParams,
|
|
83
|
-
reallocation.to.adapter,
|
|
84
|
-
targetMarketParams,
|
|
85
|
-
reallocation.assets,
|
|
86
|
-
reallocation.penalty,
|
|
87
|
-
false,
|
|
88
|
-
],
|
|
89
|
-
}
|
|
90
|
-
: {
|
|
91
|
-
type: "vaultV2BluePublicAllocatorAllocateFromIdle",
|
|
92
|
-
args: [
|
|
93
|
-
reallocation.vault,
|
|
94
|
-
reallocation.to.adapter,
|
|
95
|
-
targetMarketParams,
|
|
96
|
-
reallocation.assets,
|
|
97
|
-
reallocation.penalty,
|
|
98
|
-
false,
|
|
99
|
-
],
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return { actions, fee: 0n, penaltyAssets };
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* Validates a homogeneous Blue reallocation plan and builds its Bundler actions.
|
|
106
|
-
*
|
|
107
|
-
* Dispatches Vault V1 and Vault V2 plans to their version-specific builders. V1 plans aggregate
|
|
108
|
-
* native fees, while V2 plans aggregate loan-token penalties using the selected funding source.
|
|
109
|
-
*
|
|
110
|
-
* @param params.chainId - Chain whose registered Bundler3 and allocator addresses are used.
|
|
111
|
-
* @param params.reallocations - Optional homogeneous Vault V1 or Vault V2 reallocation plan.
|
|
112
|
-
* @param params.targetMarketParams - Morpho Blue market receiving the reallocated liquidity.
|
|
113
|
-
* @param params.penaltyFundingSource - Optional source of V2 penalty assets. Defaults to the
|
|
114
|
-
* transaction initiator.
|
|
115
|
-
* @returns The reallocation actions, aggregate V1 native fee, and aggregate V2 penalty assets.
|
|
116
|
-
* @internal
|
|
117
|
-
*/
|
|
118
|
-
export const buildBlueReallocationActions = ({ chainId, reallocations, targetMarketParams, penaltyFundingSource, }) => {
|
|
119
|
-
const reallocationPlan = validateAndNormalizeReallocations({
|
|
120
|
-
reallocations,
|
|
121
|
-
targetMarketId: targetMarketParams.id,
|
|
122
|
-
chainId,
|
|
123
|
-
});
|
|
124
|
-
return reallocationPlan.type === "vaultV1"
|
|
125
|
-
? buildVaultV1ReallocationActions({
|
|
126
|
-
reallocations: reallocationPlan.reallocations,
|
|
127
|
-
targetMarketParams,
|
|
128
|
-
})
|
|
129
|
-
: buildVaultV2BlueReallocationActions({
|
|
130
|
-
chainId,
|
|
131
|
-
reallocations: reallocationPlan.reallocations,
|
|
132
|
-
targetMarketParams,
|
|
133
|
-
penaltyFundingSource,
|
|
134
|
-
});
|
|
135
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { MarketId } from "@morpho-org/blue-sdk";
|
|
2
|
-
/**
|
|
3
|
-
* Pre-resolved repay amounts (plus `maxSharePrice`) validated by {@link resolveRepayFunding}.
|
|
4
|
-
*
|
|
5
|
-
* These are the fields the entity layer (`MorphoBlue.repay` / `repayWithdrawCollateral`) resolves
|
|
6
|
-
* from live market/position state before either repay builder assembles the bundle.
|
|
7
|
-
*
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export interface RepayFundingInput {
|
|
11
|
-
/** (assets mode) ERC-20 loan tokens pulled from the payer. */
|
|
12
|
-
readonly amount: bigint;
|
|
13
|
-
/** (shares mode) Borrow shares to repay. Discriminates the mode when `> 0n`. */
|
|
14
|
-
readonly shares: bigint;
|
|
15
|
-
/** Native ETH wrapped into wNative to help fund the repay. */
|
|
16
|
-
readonly nativeAmount: bigint;
|
|
17
|
-
/** Loan tokens routed into `GeneralAdapter1` (see `RepayActionAmountArgs`). */
|
|
18
|
-
readonly transferAmount: bigint;
|
|
19
|
-
/** Maximum acceptable repay share price (in ray). */
|
|
20
|
-
readonly maxSharePrice: bigint;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Derived repay funding values consumed by the repay builders.
|
|
24
|
-
*
|
|
25
|
-
* @internal
|
|
26
|
-
*/
|
|
27
|
-
export interface RepayFunding {
|
|
28
|
-
/** `true` when repaying an exact share count (full close); `false` in assets mode. */
|
|
29
|
-
readonly isSharesMode: boolean;
|
|
30
|
-
/** Assets passed to `morphoRepay` (`transferAmount` in assets mode, `0n` in shares mode). */
|
|
31
|
-
readonly repayAssets: bigint;
|
|
32
|
-
/** Shares passed to `morphoRepay` (`shares` in shares mode, `0n` in assets mode). */
|
|
33
|
-
readonly repayShares: bigint;
|
|
34
|
-
/** ERC-20 loan tokens pulled into `GeneralAdapter1` (net of any wrapped native). */
|
|
35
|
-
readonly erc20Amount: bigint;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Resolves and validates the repay funding envelope shared by `blueRepay` and
|
|
39
|
-
* `blueRepayWithdrawCollateral`.
|
|
40
|
-
*
|
|
41
|
-
* Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
|
|
42
|
-
* deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
|
|
43
|
-
* upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
|
|
44
|
-
* `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
|
|
45
|
-
* tokens nor under-funds the repay:
|
|
46
|
-
*
|
|
47
|
-
* - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
|
|
48
|
-
* like `blueSupply`); ERC-20 pulled = `amount`.
|
|
49
|
-
* - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
|
|
50
|
-
* net of native).
|
|
51
|
-
*
|
|
52
|
-
* Single source of truth for the funding math both repay builders depend on — a correctness fix here
|
|
53
|
-
* applies to both paths instead of risking a silent divergence between two copies.
|
|
54
|
-
*
|
|
55
|
-
* @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
|
|
56
|
-
* @param marketId - Market id used to tag the thrown errors.
|
|
57
|
-
* @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
|
|
58
|
-
* amounts the repay builders assemble the bundle from.
|
|
59
|
-
* @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
|
|
60
|
-
* @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
|
|
61
|
-
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
62
|
-
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
63
|
-
* `transferAmount !== amount + nativeAmount`.
|
|
64
|
-
* @internal
|
|
65
|
-
*/
|
|
66
|
-
export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { MutuallyExclusiveRepayAmountsError, NegativeInputError, NonPositiveInputError, TransferAmountNotEqualToAssetsError, } from "../../types/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* Resolves and validates the repay funding envelope shared by `blueRepay` and
|
|
4
|
-
* `blueRepayWithdrawCollateral`.
|
|
5
|
-
*
|
|
6
|
-
* Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
|
|
7
|
-
* deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
|
|
8
|
-
* upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
|
|
9
|
-
* `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
|
|
10
|
-
* tokens nor under-funds the repay:
|
|
11
|
-
*
|
|
12
|
-
* - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
|
|
13
|
-
* like `blueSupply`); ERC-20 pulled = `amount`.
|
|
14
|
-
* - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
|
|
15
|
-
* net of native).
|
|
16
|
-
*
|
|
17
|
-
* Single source of truth for the funding math both repay builders depend on — a correctness fix here
|
|
18
|
-
* applies to both paths instead of risking a silent divergence between two copies.
|
|
19
|
-
*
|
|
20
|
-
* @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
|
|
21
|
-
* @param marketId - Market id used to tag the thrown errors.
|
|
22
|
-
* @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
|
|
23
|
-
* amounts the repay builders assemble the bundle from.
|
|
24
|
-
* @throws {NonPositiveInputError} when `maxSharePrice <= 0n` or total repay funding is zero.
|
|
25
|
-
* @throws {NegativeInputError} when `amount`, `shares`, `nativeAmount`, or `transferAmount` is negative.
|
|
26
|
-
* @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
|
|
27
|
-
* @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
|
|
28
|
-
* `transferAmount !== amount + nativeAmount`.
|
|
29
|
-
* @internal
|
|
30
|
-
*/
|
|
31
|
-
export const resolveRepayFunding = ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }, marketId) => {
|
|
32
|
-
if (maxSharePrice <= 0n) {
|
|
33
|
-
throw new NonPositiveInputError("maxSharePrice", maxSharePrice);
|
|
34
|
-
}
|
|
35
|
-
if (nativeAmount < 0n) {
|
|
36
|
-
throw new NegativeInputError("nativeAmount", nativeAmount);
|
|
37
|
-
}
|
|
38
|
-
if (amount < 0n) {
|
|
39
|
-
throw new NegativeInputError("amount", amount);
|
|
40
|
-
}
|
|
41
|
-
if (shares < 0n) {
|
|
42
|
-
throw new NegativeInputError("shares", shares);
|
|
43
|
-
}
|
|
44
|
-
if (amount > 0n && shares > 0n) {
|
|
45
|
-
throw new MutuallyExclusiveRepayAmountsError(marketId);
|
|
46
|
-
}
|
|
47
|
-
// Shares mode repays an exact share count and pulls `transferAmount` ERC-20
|
|
48
|
-
// (already net of native), skimming the residual. Assets mode repays
|
|
49
|
-
// `transferAmount` (= amount + native, additive) and pulls `amount`.
|
|
50
|
-
const isSharesMode = shares > 0n;
|
|
51
|
-
const repayAssets = isSharesMode ? 0n : transferAmount;
|
|
52
|
-
const repayShares = shares;
|
|
53
|
-
const erc20Amount = isSharesMode ? transferAmount : amount;
|
|
54
|
-
// Validate the pre-resolved funding. Assets mode is additive like `blueSupply`:
|
|
55
|
-
// the ERC-20 pulled (`amount`) plus the wrapped `nativeAmount` must equal the
|
|
56
|
-
// assets repaid, and the total must be positive — so the adapter neither strands
|
|
57
|
-
// over-pulled tokens nor under-funds the repay. Shares mode pulls `transferAmount`
|
|
58
|
-
// ERC-20 (0 on a fully-native repay) and wraps `nativeAmount`; the bundle must
|
|
59
|
-
// fund the repay with a non-negative transfer and a positive total.
|
|
60
|
-
if (isSharesMode) {
|
|
61
|
-
if (transferAmount < 0n) {
|
|
62
|
-
throw new NegativeInputError("transferAmount", transferAmount);
|
|
63
|
-
}
|
|
64
|
-
if (transferAmount + nativeAmount <= 0n) {
|
|
65
|
-
throw new NonPositiveInputError("transferAmount + nativeAmount", transferAmount + nativeAmount);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
if (transferAmount !== amount + nativeAmount) {
|
|
70
|
-
throw new TransferAmountNotEqualToAssetsError({
|
|
71
|
-
transferAmount,
|
|
72
|
-
assets: amount + nativeAmount,
|
|
73
|
-
market: marketId,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
if (repayAssets <= 0n) {
|
|
77
|
-
throw new NonPositiveInputError("amount + nativeAmount", repayAssets);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return { isSharesMode, repayAssets, repayShares, erc20Amount };
|
|
81
|
-
};
|