@morpho-org/morpho-sdk 5.0.0 → 5.1.1

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.
Files changed (51) hide show
  1. package/README.md +4 -4
  2. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +47 -0
  3. package/lib/cjs/actions/blue/buildAssetFundingActions.js +63 -0
  4. package/lib/cjs/actions/blue/repay.d.ts +39 -39
  5. package/lib/cjs/actions/blue/repay.js +65 -46
  6. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +37 -37
  7. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +64 -41
  8. package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +69 -0
  9. package/lib/cjs/actions/blue/resolveRepayFunding.js +82 -0
  10. package/lib/cjs/actions/blue/supply.d.ts +1 -1
  11. package/lib/cjs/actions/blue/supply.js +8 -22
  12. package/lib/cjs/actions/blue/supplyCollateral.d.ts +1 -1
  13. package/lib/cjs/actions/blue/supplyCollateral.js +8 -22
  14. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +1 -1
  15. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +8 -21
  16. package/lib/cjs/entities/blue/blue.js +161 -87
  17. package/lib/cjs/helpers/index.d.ts +1 -1
  18. package/lib/cjs/helpers/index.js +1 -2
  19. package/lib/cjs/helpers/validate.d.ts +0 -18
  20. package/lib/cjs/helpers/validate.js +1 -38
  21. package/lib/cjs/types/action.d.ts +51 -3
  22. package/lib/cjs/types/error.d.ts +24 -2
  23. package/lib/cjs/types/error.js +26 -5
  24. package/lib/cjs/utils.d.ts +1 -1
  25. package/lib/cjs/utils.js +2 -3
  26. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +47 -0
  27. package/lib/esm/actions/blue/buildAssetFundingActions.js +59 -0
  28. package/lib/esm/actions/blue/repay.d.ts +39 -39
  29. package/lib/esm/actions/blue/repay.js +66 -47
  30. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +37 -37
  31. package/lib/esm/actions/blue/repayWithdrawCollateral.js +65 -42
  32. package/lib/esm/actions/blue/resolveRepayFunding.d.ts +69 -0
  33. package/lib/esm/actions/blue/resolveRepayFunding.js +78 -0
  34. package/lib/esm/actions/blue/supply.d.ts +1 -1
  35. package/lib/esm/actions/blue/supply.js +9 -23
  36. package/lib/esm/actions/blue/supplyCollateral.d.ts +1 -1
  37. package/lib/esm/actions/blue/supplyCollateral.js +9 -23
  38. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +1 -1
  39. package/lib/esm/actions/blue/supplyCollateralBorrow.js +9 -22
  40. package/lib/esm/entities/blue/blue.js +162 -88
  41. package/lib/esm/helpers/index.d.ts +1 -1
  42. package/lib/esm/helpers/index.js +1 -1
  43. package/lib/esm/helpers/validate.d.ts +0 -18
  44. package/lib/esm/helpers/validate.js +1 -37
  45. package/lib/esm/types/action.d.ts +51 -3
  46. package/lib/esm/types/error.d.ts +24 -2
  47. package/lib/esm/types/error.js +23 -3
  48. package/lib/esm/utils.d.ts +1 -1
  49. package/lib/esm/utils.js +1 -1
  50. package/package.json +19 -18
  51. package/src/index.ts +0 -4
@@ -8,7 +8,8 @@ const index_js_1 = require("../../bundler/index.js");
8
8
  const index_js_2 = require("../../helpers/index.js");
9
9
  const index_js_3 = require("../../types/index.js");
10
10
  const getBlueAuthorizationAction_js_1 = require("../signatures/getBlueAuthorizationAction.js");
11
- const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequirementActions.js");
11
+ const buildAssetFundingActions_js_1 = require("./buildAssetFundingActions.js");
12
+ const resolveRepayFunding_js_1 = require("./resolveRepayFunding.js");
12
13
  /**
13
14
  * Prepares an atomic repay-and-withdraw-collateral transaction for a Morpho Blue market.
14
15
  *
@@ -19,23 +20,29 @@ const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequireme
19
20
  * 3. `morphoWithdrawCollateral` — then withdraws collateral.
20
21
  *
21
22
  * If the order were reversed, Morpho would revert because the position would be insolvent at the
22
- * time of the withdraw. Supports two repay modes:
23
+ * time of the withdraw. All amount arithmetic is done upstream (see
24
+ * `MorphoBlue.repayWithdrawCollateral`); this builder just assembles the bundle from the
25
+ * pre-resolved {@link RepayActionAmountArgs}. The mode is discriminated on `shares`, plus optional
26
+ * native wrapping (when `nativeAmount > 0`, native ETH is wrapped via `GeneralAdapter1.wrapNative()`
27
+ * before the repay; the loan token must be the chain's wNative):
23
28
  *
24
- * - **By assets** (`assets > 0, shares = 0`): repays an exact asset amount.
25
- * - **By shares** (`assets = 0, shares > 0`): repays exact shares (full repay), with residual
26
- * loan tokens skimmed back to `receiver`.
29
+ * - **assets mode** (`shares` unset/`0n`): repays `transferAmount` assets (`= amount + nativeAmount`,
30
+ * additive like `blueSupply`), pulling `amount` ERC-20.
31
+ * - **shares mode** (`shares > 0n`): repays exact shares (full repay), pulling `transferAmount`
32
+ * ERC-20 (already net of native); residual loan tokens are skimmed back to `receiver`.
27
33
  *
28
34
  * Prerequisites: ERC-20 approval for the loan token to `GeneralAdapter1` (for the repay) **and**
29
- * `GeneralAdapter1` must be authorized on Morpho (for the withdraw).
35
+ * `GeneralAdapter1` must be authorized on Morpho (for the withdraw). Passing an
36
+ * `authorizationSignature` prepends the authorization in-bundle instead.
30
37
  *
31
38
  * @param params.market.chainId - The chain the market lives on.
32
39
  * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv).
33
- * @param params.args.assets - Repay amount in loan-token assets. Set to `0n` when repaying by shares.
34
- * @param params.args.shares - Repay amount in borrow shares. Set to `0n` when repaying by assets.
35
- * @param params.args.transferAmount - ERC-20 amount to pull into `GeneralAdapter1`. In assets
36
- * mode, must equal `assets` exactly (`TransferAmountNotEqualToAssetsError` fires otherwise).
37
- * In shares mode, this is an upper-bound estimate to absorb share-price drift; residual loan
38
- * tokens are skimmed back to `receiver`.
40
+ * @param params.args.amount - (assets mode) ERC-20 loan tokens pulled from the payer. Defaults to `0n`.
41
+ * @param params.args.shares - (shares mode) Repay amount in borrow shares. Discriminates the mode.
42
+ * @param params.args.transferAmount - Loan tokens routed into `GeneralAdapter1`: assets mode = the
43
+ * total repaid (`amount + nativeAmount`); shares mode = the ERC-20 pulled (net of native).
44
+ * @param params.args.nativeAmount - Optional native token to wrap into wNative to fund the repay.
45
+ * Requires the loan token to be the chain's wNative.
39
46
  * @param params.args.withdrawAmount - Amount of collateral to withdraw after the repay leg
40
47
  * completes.
41
48
  * @param params.args.onBehalf - Address whose Morpho debt is being repaid.
@@ -49,18 +56,23 @@ const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequireme
49
56
  * a `setAuthorizationWithSig` call is prepended to the bundle.
50
57
  * @param params.metadata - Optional analytics metadata attached to the bundle.
51
58
  * @returns A deep-frozen `Transaction<BlueRepayWithdrawCollateralAction>` with `to`,
52
- * `value`, `data`, and the typed `action` discriminator the simulation layer consumes.
59
+ * `value` (= `nativeAmount`), `data`, and the typed `action` discriminator the simulation layer consumes.
53
60
  * @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
54
- * @throws {NonPositiveRepayAmountError} when either `assets` or `shares` is negative, or when
55
- * both are zero.
56
- * @throws {MutuallyExclusiveRepayAmountsError} when both `assets` and `shares` are non-zero.
57
- * @throws {NonPositiveTransferAmountError} when `transferAmount <= 0n`.
58
- * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and `transferAmount !== assets`.
61
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
62
+ * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
63
+ * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
64
+ * `transferAmount !== amount + nativeAmount`.
65
+ * @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode
66
+ * and `transferAmount <= 0n`, or when in shares mode and `transferAmount` is negative or the total
67
+ * funding (`transferAmount + nativeAmount`) is `<= 0n`.
59
68
  * @throws {NonPositiveWithdrawCollateralAmountError} when `withdrawAmount <= 0n`.
69
+ * @throws {ChainWNativeMissingError} when `nativeAmount > 0n` but the chain has no configured wNative.
70
+ * @throws {NativeAmountOnNonWNativeAssetError} when `nativeAmount > 0n` but the loan token is not
71
+ * the chain's wNative.
60
72
  * @throws {DepositAssetMismatchError} from `getTokenRequirementActions` when `requirementSignature`
61
73
  * is provided and the signed asset differs from `marketParams.loanToken`.
62
74
  * @throws {DepositAmountMismatchError} from `getTokenRequirementActions` when `requirementSignature`
63
- * is provided and the signed amount differs from `args.transferAmount`.
75
+ * is provided and the signed amount differs from the ERC-20 amount pulled.
64
76
  * @throws {Permit2ExpirationMissingError} from `getTokenRequirementActions` when a Permit2 requirement
65
77
  * signature is missing its expiration.
66
78
  * @example
@@ -68,51 +80,57 @@ const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequireme
68
80
  * import { blueRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
69
81
  *
70
82
  * const tx = blueRepayWithdrawCollateral({
71
- * market: { chainId: 1, marketParams },
83
+ * market: { chainId: 1, marketParams }, // marketParams.loanToken === wNative
72
84
  * args: {
73
- * assets: 500_000_000n,
74
- * shares: 0n,
75
- * transferAmount: 500_000_000n,
85
+ * shares: 500_000_000_000_000_000_000_000n,
86
+ * transferAmount: 310_000_000_000_000_000n, // ERC-20 pulled (net of native)
87
+ * nativeAmount: 200_000_000_000_000_000n, // 0.2 funded by wrapping native ETH
76
88
  * withdrawAmount: 1_000_000_000_000_000_000n,
77
89
  * onBehalf: borrower,
78
90
  * receiver: borrower,
79
91
  * maxSharePrice: 1_010_000_000_000_000_000_000_000_000n, // RAY-scaled, 1.01x
80
92
  * },
81
93
  * });
82
- * // tx satisfies Readonly<Transaction<BlueRepayWithdrawCollateralAction>>
94
+ * // tx.value === 200_000_000_000_000_000n
83
95
  * ```
84
96
  */
85
- const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args: { assets, shares, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, authorizationSignature, }, metadata, }) => {
86
- (0, index_js_2.validateRepayParams)({
87
- assets,
88
- shares,
89
- transferAmount,
90
- maxSharePrice,
91
- marketId: marketParams.id,
92
- });
97
+ const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args, metadata, }) => {
98
+ const { amount = 0n, shares = 0n, nativeAmount = 0n, transferAmount, withdrawAmount, onBehalf, receiver, maxSharePrice, requirementSignature, authorizationSignature, } = args;
99
+ const { isSharesMode, repayAssets, repayShares, erc20Amount } = (0, resolveRepayFunding_js_1.resolveRepayFunding)({ amount, shares, nativeAmount, transferAmount, maxSharePrice }, marketParams.id);
93
100
  if (withdrawAmount <= 0n) {
94
101
  throw new index_js_3.NonPositiveWithdrawCollateralAmountError(marketParams.id);
95
102
  }
96
103
  const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
97
104
  const actions = [];
105
+ // Authorize GeneralAdapter1 in-bundle when a signed authorization is supplied.
98
106
  if (authorizationSignature) {
99
107
  actions.push((0, getBlueAuthorizationAction_js_1.getBlueAuthorizationAction)(chainId, authorizationSignature));
100
108
  }
101
- actions.push(...(0, getTokenRequirementActions_js_1.getTokenRequirementActions)({
109
+ // Fund the repay: wrap native (if any) then pull the ERC-20 remainder.
110
+ actions.push(...(0, buildAssetFundingActions_js_1.buildAssetFundingActions)({
111
+ chainId,
102
112
  asset: marketParams.loanToken,
103
- amount: transferAmount,
104
- recipient: generalAdapter1,
113
+ erc20Amount,
114
+ nativeAmount,
105
115
  requirementSignature,
106
116
  }));
107
117
  // REPAY FIRST — reduces debt before withdrawing collateral
108
118
  actions.push({
109
119
  type: "morphoRepay",
110
- args: [marketParams, assets, shares, maxSharePrice, onBehalf, [], false],
120
+ args: [
121
+ marketParams,
122
+ repayAssets,
123
+ repayShares,
124
+ maxSharePrice,
125
+ onBehalf,
126
+ [],
127
+ false,
128
+ ],
111
129
  });
112
130
  // Skim residual loan tokens back to the payer when repaying by shares.
113
131
  // In shares mode, transferAmount is an upper-bound estimate; morphoRepay
114
132
  // consumes only the exact amount needed, leaving a residual in the adapter.
115
- if (shares > 0n) {
133
+ if (isSharesMode) {
116
134
  actions.push({
117
135
  type: "erc20Transfer",
118
136
  args: [
@@ -128,7 +146,10 @@ const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args:
128
146
  type: "morphoWithdrawCollateral",
129
147
  args: [marketParams, withdrawAmount, receiver, false],
130
148
  });
131
- let tx = index_js_1.BundlerAction.encodeBundle(chainId, actions);
149
+ let tx = {
150
+ ...index_js_1.BundlerAction.encodeBundle(chainId, actions),
151
+ value: nativeAmount,
152
+ };
132
153
  if (metadata) {
133
154
  tx = (0, index_js_2.addTransactionMetadata)(tx, metadata);
134
155
  }
@@ -138,13 +159,15 @@ const blueRepayWithdrawCollateral = ({ market: { chainId, marketParams }, args:
138
159
  type: "blueRepayWithdrawCollateral",
139
160
  args: {
140
161
  market: marketParams.id,
141
- repayAssets: assets,
142
- repayShares: shares,
143
- transferAmount,
162
+ repayAssets,
163
+ repayShares,
164
+ // Total loan tokens routed to the adapter: ERC-20 pulled + native wrapped.
165
+ transferAmount: erc20Amount + nativeAmount,
144
166
  withdrawAmount,
145
167
  maxSharePrice,
146
168
  onBehalf,
147
169
  receiver,
170
+ nativeAmount: nativeAmount > 0n ? nativeAmount : undefined,
148
171
  },
149
172
  },
150
173
  });
@@ -0,0 +1,69 @@
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 {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
60
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
61
+ * @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode and
62
+ * the repaid assets are `<= 0n`, or when in shares mode and `transferAmount` is negative or the total
63
+ * funding (`transferAmount + nativeAmount`) is `<= 0n`.
64
+ * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
65
+ * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
66
+ * `transferAmount !== amount + nativeAmount`.
67
+ * @internal
68
+ */
69
+ export declare const resolveRepayFunding: ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }: RepayFundingInput, marketId: MarketId) => RepayFunding;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveRepayFunding = void 0;
4
+ const index_js_1 = require("../../types/index.js");
5
+ /**
6
+ * Resolves and validates the repay funding envelope shared by `blueRepay` and
7
+ * `blueRepayWithdrawCollateral`.
8
+ *
9
+ * Encode-only and synchronous — reads no on-chain state and does no amount arithmetic beyond
10
+ * deriving the bundle values from the pre-resolved args (the entity layer resolves the amounts
11
+ * upstream). Runs the shared mode/amount guards, discriminates assets vs shares mode on
12
+ * `shares > 0n`, then validates the funding so the assembled bundle neither strands over-pulled
13
+ * tokens nor under-funds the repay:
14
+ *
15
+ * - **assets mode** (`shares` `0n`): repays `transferAmount` (`= amount + nativeAmount`, additive
16
+ * like `blueSupply`); ERC-20 pulled = `amount`.
17
+ * - **shares mode** (`shares > 0n`): repays exact `shares`; ERC-20 pulled = `transferAmount` (already
18
+ * net of native).
19
+ *
20
+ * Single source of truth for the funding math both repay builders depend on — a correctness fix here
21
+ * applies to both paths instead of risking a silent divergence between two copies.
22
+ *
23
+ * @param input - The pre-resolved repay amounts plus the repay `maxSharePrice`.
24
+ * @param marketId - Market id used to tag the thrown errors.
25
+ * @returns `{ isSharesMode, repayAssets, repayShares, erc20Amount }` — the mode discriminator and the
26
+ * amounts the repay builders assemble the bundle from.
27
+ * @throws {NonPositiveRepayMaxSharePriceError} when `maxSharePrice <= 0n`.
28
+ * @throws {NegativeNativeAmountError} when `nativeAmount < 0n`.
29
+ * @throws {NonPositiveRepayAmountError} when `amount` or `shares` is negative, when in assets mode and
30
+ * the repaid assets are `<= 0n`, or when in shares mode and `transferAmount` is negative or the total
31
+ * funding (`transferAmount + nativeAmount`) is `<= 0n`.
32
+ * @throws {MutuallyExclusiveRepayAmountsError} when both `amount` and `shares` are `> 0n`.
33
+ * @throws {TransferAmountNotEqualToAssetsError} when in assets mode and
34
+ * `transferAmount !== amount + nativeAmount`.
35
+ * @internal
36
+ */
37
+ const resolveRepayFunding = ({ amount, shares, nativeAmount, transferAmount, maxSharePrice, }, marketId) => {
38
+ if (maxSharePrice <= 0n) {
39
+ throw new index_js_1.NonPositiveRepayMaxSharePriceError(marketId);
40
+ }
41
+ if (nativeAmount < 0n) {
42
+ throw new index_js_1.NegativeNativeAmountError(nativeAmount);
43
+ }
44
+ if (amount < 0n || shares < 0n) {
45
+ throw new index_js_1.NonPositiveRepayAmountError(marketId);
46
+ }
47
+ if (amount > 0n && shares > 0n) {
48
+ throw new index_js_1.MutuallyExclusiveRepayAmountsError(marketId);
49
+ }
50
+ // Shares mode repays an exact share count and pulls `transferAmount` ERC-20
51
+ // (already net of native), skimming the residual. Assets mode repays
52
+ // `transferAmount` (= amount + native, additive) and pulls `amount`.
53
+ const isSharesMode = shares > 0n;
54
+ const repayAssets = isSharesMode ? 0n : transferAmount;
55
+ const repayShares = shares;
56
+ const erc20Amount = isSharesMode ? transferAmount : amount;
57
+ // Validate the pre-resolved funding. Assets mode is additive like `blueSupply`:
58
+ // the ERC-20 pulled (`amount`) plus the wrapped `nativeAmount` must equal the
59
+ // assets repaid, and the total must be positive — so the adapter neither strands
60
+ // over-pulled tokens nor under-funds the repay. Shares mode pulls `transferAmount`
61
+ // ERC-20 (0 on a fully-native repay) and wraps `nativeAmount`; the bundle must
62
+ // fund the repay with a non-negative transfer and a positive total.
63
+ if (isSharesMode) {
64
+ if (transferAmount < 0n || transferAmount + nativeAmount <= 0n) {
65
+ throw new index_js_1.NonPositiveRepayAmountError(marketId);
66
+ }
67
+ }
68
+ else {
69
+ if (transferAmount !== amount + nativeAmount) {
70
+ throw new index_js_1.TransferAmountNotEqualToAssetsError({
71
+ transferAmount,
72
+ assets: amount + nativeAmount,
73
+ market: marketId,
74
+ });
75
+ }
76
+ if (repayAssets <= 0n) {
77
+ throw new index_js_1.NonPositiveRepayAmountError(marketId);
78
+ }
79
+ }
80
+ return { isSharesMode, repayAssets, repayShares, erc20Amount };
81
+ };
82
+ exports.resolveRepayFunding = resolveRepayFunding;
@@ -1,4 +1,4 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
3
  import { type BlueSupplyAction, type DepositAmountArgs, type Metadata, type PermitRequirementSignature, type Transaction } from "../../types/index.js";
4
4
  /** Parameters for {@link blueSupply}. */
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.blueSupply = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
5
  const index_js_1 = require("../../bundler/index.js");
7
6
  const index_js_2 = require("../../helpers/index.js");
8
7
  const index_js_3 = require("../../types/index.js");
9
- const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequirementActions.js");
8
+ const buildAssetFundingActions_js_1 = require("./buildAssetFundingActions.js");
10
9
  /**
11
10
  * Prepares a loan-asset supply transaction for a Morpho Blue market.
12
11
  *
@@ -70,26 +69,13 @@ const blueSupply = ({ market: { chainId, marketParams }, args: { amount = 0n, on
70
69
  if (totalAssets === 0n) {
71
70
  throw new index_js_3.ZeroSupplyAmountError(marketParams.id);
72
71
  }
73
- const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
74
- const actions = [];
75
- if (nativeAmount !== undefined && nativeAmount > 0n) {
76
- (0, index_js_2.validateNativeAsset)(chainId, marketParams.loanToken);
77
- actions.push({
78
- type: "nativeTransfer",
79
- args: [bundler3, generalAdapter1, nativeAmount, false],
80
- }, {
81
- type: "wrapNative",
82
- args: [nativeAmount, generalAdapter1, false],
83
- });
84
- }
85
- if (amount > 0n) {
86
- actions.push(...(0, getTokenRequirementActions_js_1.getTokenRequirementActions)({
87
- asset: marketParams.loanToken,
88
- amount,
89
- recipient: generalAdapter1,
90
- requirementSignature,
91
- }));
92
- }
72
+ const actions = (0, buildAssetFundingActions_js_1.buildAssetFundingActions)({
73
+ chainId,
74
+ asset: marketParams.loanToken,
75
+ erc20Amount: amount,
76
+ nativeAmount: nativeAmount ?? 0n,
77
+ requirementSignature,
78
+ });
93
79
  actions.push({
94
80
  type: "morphoSupply",
95
81
  args: [marketParams, totalAssets, 0n, maxSharePrice, onBehalf, [], false],
@@ -1,4 +1,4 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
3
  import { type BlueSupplyCollateralAction, type DepositAmountArgs, type Metadata, type PermitRequirementSignature, type Transaction } from "../../types/index.js";
4
4
  /** Parameters for {@link blueSupplyCollateral}. */
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.blueSupplyCollateral = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
5
  const index_js_1 = require("../../bundler/index.js");
7
6
  const index_js_2 = require("../../helpers/index.js");
8
7
  const index_js_3 = require("../../types/index.js");
9
- const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequirementActions.js");
8
+ const buildAssetFundingActions_js_1 = require("./buildAssetFundingActions.js");
10
9
  /**
11
10
  * Prepares a supply-collateral transaction for a Morpho Blue market.
12
11
  *
@@ -61,26 +60,13 @@ const blueSupplyCollateral = ({ market: { chainId, marketParams }, args: { amoun
61
60
  if (totalCollateral === 0n) {
62
61
  throw new index_js_3.ZeroCollateralAmountError(marketParams.id);
63
62
  }
64
- const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
65
- const actions = [];
66
- if (nativeAmount !== undefined && nativeAmount > 0n) {
67
- (0, index_js_2.validateNativeAsset)(chainId, marketParams.collateralToken);
68
- actions.push({
69
- type: "nativeTransfer",
70
- args: [bundler3, generalAdapter1, nativeAmount, false],
71
- }, {
72
- type: "wrapNative",
73
- args: [nativeAmount, generalAdapter1, false],
74
- });
75
- }
76
- if (amount > 0n) {
77
- actions.push(...(0, getTokenRequirementActions_js_1.getTokenRequirementActions)({
78
- asset: marketParams.collateralToken,
79
- amount,
80
- recipient: generalAdapter1,
81
- requirementSignature,
82
- }));
83
- }
63
+ const actions = (0, buildAssetFundingActions_js_1.buildAssetFundingActions)({
64
+ chainId,
65
+ asset: marketParams.collateralToken,
66
+ erc20Amount: amount,
67
+ nativeAmount: nativeAmount ?? 0n,
68
+ requirementSignature,
69
+ });
84
70
  actions.push({
85
71
  type: "morphoSupplyCollateral",
86
72
  args: [marketParams, totalCollateral, onBehalf, [], false],
@@ -1,4 +1,4 @@
1
- import { type MarketParams } from "@morpho-org/blue-sdk";
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
3
  import { type AuthorizationRequirementSignature, type BlueSupplyCollateralBorrowAction, type DepositAmountArgs, type Metadata, type PermitRequirementSignature, type Transaction, type VaultReallocation } from "../../types/index.js";
4
4
  /** Parameters for {@link blueSupplyCollateralBorrow}. */
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.blueSupplyCollateralBorrow = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
5
  const index_js_1 = require("../../bundler/index.js");
7
6
  const index_js_2 = require("../../helpers/index.js");
8
7
  const index_js_3 = require("../../types/index.js");
9
8
  const getBlueAuthorizationAction_js_1 = require("../signatures/getBlueAuthorizationAction.js");
10
- const getTokenRequirementActions_js_1 = require("../signatures/getTokenRequirementActions.js");
9
+ const buildAssetFundingActions_js_1 = require("./buildAssetFundingActions.js");
11
10
  const buildReallocationActions_js_1 = require("./buildReallocationActions.js");
12
11
  /**
13
12
  * Prepares an atomic supply-collateral-and-borrow transaction for a Morpho Blue market.
@@ -99,29 +98,17 @@ const blueSupplyCollateralBorrow = ({ market: { chainId, marketParams }, args: {
99
98
  if (totalCollateral === 0n) {
100
99
  throw new index_js_3.ZeroCollateralAmountError(marketParams.id);
101
100
  }
102
- const { bundler3: { generalAdapter1, bundler3 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
103
101
  const actions = [];
104
102
  if (authorizationSignature) {
105
103
  actions.push((0, getBlueAuthorizationAction_js_1.getBlueAuthorizationAction)(chainId, authorizationSignature));
106
104
  }
107
- if (nativeAmount !== undefined && nativeAmount > 0n) {
108
- (0, index_js_2.validateNativeAsset)(chainId, marketParams.collateralToken);
109
- actions.push({
110
- type: "nativeTransfer",
111
- args: [bundler3, generalAdapter1, nativeAmount, false],
112
- }, {
113
- type: "wrapNative",
114
- args: [nativeAmount, generalAdapter1, false],
115
- });
116
- }
117
- if (amount > 0n) {
118
- actions.push(...(0, getTokenRequirementActions_js_1.getTokenRequirementActions)({
119
- asset: marketParams.collateralToken,
120
- amount,
121
- recipient: generalAdapter1,
122
- requirementSignature,
123
- }));
124
- }
105
+ actions.push(...(0, buildAssetFundingActions_js_1.buildAssetFundingActions)({
106
+ chainId,
107
+ asset: marketParams.collateralToken,
108
+ erc20Amount: amount,
109
+ nativeAmount: nativeAmount ?? 0n,
110
+ requirementSignature,
111
+ }));
125
112
  actions.push({
126
113
  type: "morphoSupplyCollateral",
127
114
  args: [marketParams, totalCollateral, onBehalf, [], false],