@lifi/composer-sdk 0.2.0-staging.5 → 0.3.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.
Files changed (101) hide show
  1. package/README.md +103 -2
  2. package/dist/client.cjs +46 -8
  3. package/dist/client.cjs.map +1 -1
  4. package/dist/client.d.cts +27 -3
  5. package/dist/client.d.ts +27 -3
  6. package/dist/client.js +49 -8
  7. package/dist/client.js.map +1 -1
  8. package/dist/errors.cjs +4 -1
  9. package/dist/errors.cjs.map +1 -1
  10. package/dist/errors.js +6 -1
  11. package/dist/errors.js.map +1 -1
  12. package/dist/generated/config.generated.cjs.map +1 -1
  13. package/dist/generated/config.generated.d.cts +80 -20
  14. package/dist/generated/config.generated.d.ts +80 -20
  15. package/dist/generated/materialisers.generated.cjs +2 -8
  16. package/dist/generated/materialisers.generated.cjs.map +1 -1
  17. package/dist/generated/materialisers.generated.d.cts +1 -1
  18. package/dist/generated/materialisers.generated.d.ts +1 -1
  19. package/dist/generated/materialisers.generated.js +1 -6
  20. package/dist/generated/materialisers.generated.js.map +1 -1
  21. package/dist/generated/operations.generated.cjs +1 -30
  22. package/dist/generated/operations.generated.cjs.map +1 -1
  23. package/dist/generated/operations.generated.d.cts +2 -94
  24. package/dist/generated/operations.generated.d.ts +2 -94
  25. package/dist/generated/operations.generated.js +1 -30
  26. package/dist/generated/operations.generated.js.map +1 -1
  27. package/dist/index.cjs +8 -0
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +4 -3
  30. package/dist/index.d.ts +4 -3
  31. package/dist/index.js +8 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/{materialisers.generated-BfWJ-g7t.d.ts → materialisers.generated-BMkBQgWZ.d.ts} +3 -8
  34. package/dist/{materialisers.generated-DaJRwiRe.d.cts → materialisers.generated-BWq5oePO.d.cts} +3 -8
  35. package/dist/responseSchemas.cjs +110 -0
  36. package/dist/responseSchemas.cjs.map +1 -0
  37. package/dist/responseSchemas.d.cts +36 -0
  38. package/dist/responseSchemas.d.ts +36 -0
  39. package/dist/responseSchemas.js +74 -0
  40. package/dist/responseSchemas.js.map +1 -0
  41. package/dist/run/simulate.cjs +41 -0
  42. package/dist/run/simulate.cjs.map +1 -0
  43. package/dist/run/simulate.d.cts +59 -0
  44. package/dist/run/simulate.d.ts +59 -0
  45. package/dist/run/simulate.js +17 -0
  46. package/dist/run/simulate.js.map +1 -0
  47. package/dist/sdk.cjs +6 -1
  48. package/dist/sdk.cjs.map +1 -1
  49. package/dist/sdk.d.cts +18 -6
  50. package/dist/sdk.d.ts +18 -6
  51. package/dist/sdk.js +6 -1
  52. package/dist/sdk.js.map +1 -1
  53. package/package.json +8 -6
  54. package/src/client.ts +96 -16
  55. package/src/errors.ts +8 -10
  56. package/src/examples/aaveClaimRewards.ts +2 -2
  57. package/src/examples/aaveRepay.ts +2 -2
  58. package/src/examples/aaveRepayWithATokens.ts +2 -2
  59. package/src/examples/aaveSetEMode.ts +2 -2
  60. package/src/examples/approveAndDeposit.ts +2 -2
  61. package/src/examples/callContract.ts +4 -4
  62. package/src/examples/config.ts +4 -1
  63. package/src/examples/consolidateToEth.ts +2 -2
  64. package/src/examples/consolidateToUsdc.ts +2 -2
  65. package/src/examples/depositFromProxy.ts +2 -2
  66. package/src/examples/dustSweep.ts +2 -2
  67. package/src/examples/emitCustomEvent.ts +2 -2
  68. package/src/examples/invariantChecks.ts +2 -2
  69. package/src/examples/lifiSwap.ts +2 -2
  70. package/src/examples/lifiZap.ts +2 -2
  71. package/src/examples/rawCallWithArithmetic.ts +2 -2
  72. package/src/examples/readContractState.ts +2 -2
  73. package/src/examples/simulateCompiledSwap.ts +76 -0
  74. package/src/examples/simulateRawTransaction.ts +39 -0
  75. package/src/examples/splitAndZap.ts +2 -2
  76. package/src/examples/splitWithArithmetic.ts +2 -2
  77. package/src/examples/swapAndZap.ts +2 -2
  78. package/src/examples/swapToRecipient.ts +2 -2
  79. package/src/examples/swapWithAllowRevert.ts +3 -3
  80. package/src/examples/swapWithBalanceCheck.ts +2 -2
  81. package/src/examples/swapWithFee.ts +2 -2
  82. package/src/examples/swapWithOutputValidation.ts +2 -2
  83. package/src/examples/transferTokens.ts +3 -3
  84. package/src/examples/untypedOpWithTypedRef.ts +2 -2
  85. package/src/generated/config.generated.ts +221 -20
  86. package/src/generated/materialisers.generated.ts +0 -12
  87. package/src/generated/operations.generated.ts +0 -177
  88. package/src/index.ts +25 -0
  89. package/src/responseSchemas.ts +143 -0
  90. package/src/run/simulate.ts +78 -0
  91. package/src/sdk.ts +25 -6
  92. package/src/examples/staged/aaveBorrow.ts +0 -128
  93. package/src/examples/staged/aaveDebtRebalanceExactOut.ts +0 -301
  94. package/src/examples/staged/aaveHealthFactorExplode.ts +0 -70
  95. package/src/examples/staged/aaveToMorphoDebtMigration.ts +0 -200
  96. package/src/examples/staged/aaveToMorphoDebtMigrationWithSwap.ts +0 -231
  97. package/src/examples/staged/aaveToMorphoDebtMigrationWithSwapExactOut.ts +0 -307
  98. package/src/examples/staged/flashloanRepay.ts +0 -142
  99. package/src/examples/staged/morphoBlueBorrow.ts +0 -93
  100. package/src/examples/staged/morphoBlueRepay.ts +0 -132
  101. package/src/examples/staged/registry.ts +0 -80
@@ -1,128 +0,0 @@
1
- import type { ComposeCompileRequest, Flow } from '@lifi/compose-spec';
2
-
3
- import {
4
- createComposeSdk,
5
- guards,
6
- materialisers,
7
- resources,
8
- } from '../../index.js';
9
- import type { Address } from '../../types.js';
10
- import { BASE_URL } from '../config.js';
11
-
12
- // Aave v3 contracts on Ethereum mainnet.
13
- const AAVE_V3_POOL = '0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2';
14
- const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
15
- // Aave v3 aEthUSDC receipt token and variable-debt USDC token.
16
- const A_ETH_USDC = '0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c';
17
- const VDEBT_USDC = '0x72E95b8931767C79bA4EeE721354d6E99a61D004';
18
-
19
- // 1.05 WAD (1e18) — minimum acceptable Aave health factor after the borrow.
20
- // The on-chain VM reverts the run if the post-borrow HF drops below this.
21
- const MIN_HEALTH_FACTOR_WAD = '1050000000000000000';
22
-
23
- export interface AaveBorrowInput {
24
- readonly owner: Address;
25
- readonly recipient: Address;
26
- /** USDC supplied as collateral so the proxy can borrow (6 dp). */
27
- readonly collateralAmount: `${bigint}`;
28
- /** USDC borrowed against the collateral and sent to the recipient (6 dp). */
29
- readonly borrowAmount: `${bigint}`;
30
- }
31
-
32
- /**
33
- * Supply USDC to Aave v3, borrow USDC against it, and transfer the borrowed
34
- * funds to a recipient — with an on-chain health-factor guard that reverts
35
- * if the post-borrow position is too thin.
36
- *
37
- * The in-flow supply makes the example self-contained: the borrow is backed
38
- * by collateral created in the same transaction, so it simulates cleanly
39
- * for a fresh proxy. Keep `borrowAmount` comfortably below the collateral's
40
- * borrowing power or the health-factor guard reverts the run.
41
- *
42
- * Demonstrates:
43
- * - `lifi.zap` to supply USDC into Aave (USDC → aEthUSDC routing edge)
44
- * - `aave.borrow` with no resource input — the borrowed asset materialises
45
- * as the `borrowed` resource output.
46
- * - Chaining the borrow output into a `core.transfer` to forward funds.
47
- * - `aave.getHealthFactor` + `core.numericInvariant` guard asserts that
48
- * `healthFactor >= MIN_HEALTH_FACTOR_WAD` after the borrow; the run reverts
49
- * on-chain if the threshold is violated.
50
- *
51
- * The supplied aEthUSDC stays on the proxy: it backs the open debt, and
52
- * Aave rejects aToken transfers that would break the position's health
53
- * factor, so it cannot be swept while the borrow is outstanding.
54
- */
55
- export const buildAaveBorrow = ({
56
- owner,
57
- recipient,
58
- collateralAmount,
59
- borrowAmount,
60
- }: AaveBorrowInput): {
61
- flow: Flow;
62
- request: ComposeCompileRequest;
63
- } => {
64
- const sdk = createComposeSdk({ baseUrl: BASE_URL });
65
-
66
- const builder = sdk.flow(1, {
67
- name: 'aave-borrow-usdc',
68
- inputs: {
69
- collateralIn: resources.erc20(USDC, 1),
70
- recipient: 'address',
71
- },
72
- });
73
-
74
- // Supply USDC as collateral (USDC → aEthUSDC) so the proxy can borrow.
75
- builder.lifi.zap('supply', {
76
- bind: { amountIn: builder.inputs.collateralIn },
77
- config: { resourceOut: resources.erc20(A_ETH_USDC, 1) },
78
- });
79
-
80
- const borrowOut = builder.aave.borrow('borrow', {
81
- bind: {},
82
- config: {
83
- pool: AAVE_V3_POOL,
84
- asset: USDC,
85
- variableDebtToken: VDEBT_USDC,
86
- amount: borrowAmount,
87
- },
88
- });
89
-
90
- // Read the post-borrow Aave health factor and assert it stays above the
91
- // minimum threshold. The numericInvariant guard lowers to an on-chain
92
- // `AssertRawInvariant` IR1 instruction; the run reverts if HF < threshold.
93
- builder.aave.getHealthFactor('hf', {
94
- bind: {},
95
- config: { pool: AAVE_V3_POOL },
96
- guards: [
97
- guards.coreNumericInvariant({
98
- port: 'healthFactor',
99
- op: 'gte',
100
- threshold: MIN_HEALTH_FACTOR_WAD,
101
- }),
102
- ],
103
- });
104
-
105
- // Forward the freshly borrowed USDC to the recipient.
106
- builder.core.transfer('send-borrowed', {
107
- bind: {
108
- amount: borrowOut.borrowed,
109
- recipient: builder.inputs.recipient,
110
- },
111
- config: {},
112
- });
113
-
114
- const flow = builder.build();
115
-
116
- // No sweepTo: the aEthUSDC collateral must stay on the proxy while the
117
- // debt is open — Aave blocks aToken transfers that would leave the
118
- // position undercollateralized.
119
- const request = sdk.request(flow, {
120
- signer: owner,
121
- inputs: {
122
- collateralIn: materialisers.directDeposit({ amount: collateralAmount }),
123
- recipient,
124
- },
125
- });
126
-
127
- return { flow, request };
128
- };
@@ -1,301 +0,0 @@
1
- import type { ComposeCompileRequest, Flow } from '@lifi/compose-spec';
2
-
3
- import { createComposeSdk, materialisers, resources } from '../../index.js';
4
- import type { Address } from '../../types.js';
5
- import { BASE_URL } from '../config.js';
6
-
7
- // Base mainnet token addresses.
8
- const BASE_WETH = '0x4200000000000000000000000000000000000006';
9
- const BASE_USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
10
- // Circle EURC on Base (6 decimals).
11
- const BASE_EURC = '0x60a3E35Cc302bFA44Cb288Bc5a4F316Fdb1adb42';
12
-
13
- // Token decimals for the Paraswap BUY (raw-address requests require them).
14
- const EURC_DECIMALS = 6;
15
- const USDC_DECIMALS = 6;
16
-
17
- // Aave V3 on Base: pool + WETH aToken + variable-debt tokens for the old (USDC)
18
- // and new (EURC) debt assets. The variable-debt token only feeds aave.borrow
19
- // (to read the post-borrow debt balance); aave.repay infers the asset from its
20
- // bound input resource. Verified live against Pool.getReserveData on Base.
21
- const AAVE_V3_POOL_BASE = '0xA238Dd80C259a72e81d7e4664a9801593F98d1c5';
22
- const A_BAS_WETH = '0xD4a0e0b9149BCee3C920d2E00b5dE09138fd8bb7';
23
- const VDEBT_BAS_USDC = '0x59dca05b6c26dbd64b5381374aAaC5CD05644C28';
24
- const VDEBT_BAS_EURC = '0x03D01595769333174036832e18fA2f17C74f8161';
25
-
26
- // Aave V3 flashloan premium (FLASHLOAN_PREMIUM_TOTAL) = 5 bps of principal.
27
- // Mirrors the builtins package's `computeDefaultFee('aave-v3', amount)`,
28
- // inlined so the example stays free of compose-internals imports (the SDK must
29
- // not cross the manifest boundary into the builtins package). Override if the
30
- // pool's premium has changed on-chain.
31
- const AAVE_V3_FEE_BPS = 5n;
32
- const BPS_DENOMINATOR = 10_000n;
33
- const computeAaveV3Fee = (principal: bigint): bigint =>
34
- (principal * AAVE_V3_FEE_BPS) / BPS_DENOMINATOR;
35
-
36
- // Source-cap slippage on the EURC → USDC BUY, in basis points. This bounds how
37
- // much extra EURC Augustus may pull above the quoted source before refunding.
38
- // `newDebtBorrowAmount` must exceed the resulting `maxAmountIn`.
39
- const DEFAULT_MAX_SLIPPAGE_BPS = 100; // 1%
40
-
41
- export interface AaveDebtRebalanceExactOutInput {
42
- readonly owner: Address;
43
- /**
44
- * WETH collateral used to seed the position (in smallest units, 18 decimals).
45
- * In production the position already exists and this seed step is omitted —
46
- * the collateral is never touched by the rebalance itself.
47
- */
48
- readonly collateralAmount: `${bigint}`;
49
- /**
50
- * Outstanding USDC debt to rebalance away from (6 decimals). The flashloan is
51
- * sized to this; in production size it just above the live debt so the capped
52
- * repay clears it after accrued interest.
53
- */
54
- readonly debtAmount: `${bigint}`;
55
- /**
56
- * EURC to borrow as the *new* debt (6 decimals). Sized as a *generous upper
57
- * bound* — large enough that the exact-output BUY can always produce
58
- * `debtAmount + aaveV3Fee` USDC even after `maxSlippageBps` of source slippage
59
- * (i.e. `newDebtBorrowAmount > maxAmountIn`). The BUY pulls only what it needs;
60
- * the unspent EURC is repaid straight back into the freshly-minted EURC debt,
61
- * so the *net* new debt is the original USDC debt (in EURC terms) plus only
62
- * the slippage/fee actually consumed — NOT the full upper bound.
63
- * Note: EUR/USD is not 1:1 — size off a live BUY quote, e.g.
64
- * newDebtBorrowAmount ≈ ceil((debtAmount + aaveV3Fee) * eurcPerUsdc * (1 + slippage))
65
- */
66
- readonly newDebtBorrowAmount: `${bigint}`;
67
- /** Optional source-cap slippage on the BUY, in bps (default 100 = 1%). */
68
- readonly maxSlippageBps?: number;
69
- /** Optional integrator label for analytics / fee capture. */
70
- readonly partner?: string;
71
- /** Optional fee recipient; required to actually receive a partner fee. */
72
- readonly partnerAddress?: Address;
73
- /** Optional fixed partner fee in bps (e.g. '50' = 0.5%); needs partnerAddress. */
74
- readonly partnerFeeBps?: string;
75
- }
76
-
77
- /**
78
- * Atomically **rebalance** (debt-swap) an Aave V3 position on Base from USDC
79
- * debt into EURC debt — same protocol, same WETH collateral, only the debt
80
- * *token* changes — using a native **exact-output** (Paraswap/Velora BUY) swap.
81
- *
82
- * This is the single-protocol "debt switch" pattern productised by Aave's
83
- * `ParaSwapDebtSwapAdapter` and DeFi Saver's "Debt Switch": flashloan/borrow the
84
- * new debt slightly over a cap, exact-out-swap to produce exactly the old debt
85
- * owed, repay the old debt, then repay the leftover new asset back into the
86
- * freshly-created new debt so lasting debt = only what was consumed. Contrast
87
- * with `aaveToMorphoDebtMigrationWithSwapExactOut.ts`, which *migrates* the whole
88
- * position across protocols (Aave → Morpho) and therefore needs a second
89
- * flashloan to move the collateral; here the collateral never moves, so a single
90
- * flashloan of the old debt token suffices.
91
- *
92
- * Why rebalance USDC → EURC: at the time of writing EURC's Base Aave V3 variable
93
- * borrow APR (~3.87%) sits below USDC's (~4.28%), and a EUR-based borrower may
94
- * also want the liability denominated in EUR to match income/assets (removing
95
- * USD/EUR FX risk on the debt). The reward is the APR delta over the holding
96
- * period; the cost is the Aave flashloan premium plus the realised swap
97
- * slippage on the consumed notional only.
98
- *
99
- * Phase 1 — provision the starting position on Aave V3 (illustrative; omit in
100
- * production where the position already exists):
101
- * - User direct-deposits WETH.
102
- * - `lifi.zap` supplies the WETH to Aave (WETH → aBasWETH routing edge).
103
- * - `aave.borrow` mints the USDC debt being rebalanced; the borrowed USDC is
104
- * swept back to the signer as the user's existing loan proceeds.
105
- *
106
- * Phase 2 — rebalance USDC debt → EURC debt with one flashloan:
107
- * - Flashloan (Aave V3, USDC) bridges the old-debt payoff before the new debt
108
- * exists.
109
- * - `aave.repay(mode: 'max')` clears the USDC debt with the flashloan; Aave's
110
- * Pool clamps to the outstanding debt exactly, any cap residual is swept.
111
- * - `aave.borrow` mints a *capped* (generously over-sized) `newDebtBorrowAmount`
112
- * of EURC against the same, still-supplied WETH collateral.
113
- * - `paraswap.buy` buys *exactly* `debtAmount + aaveV3Fee` USDC with the borrowed
114
- * EURC, pulling only the EURC needed. `amountOut` is the exact figure that
115
- * settles the USDC flashloan leg; `unspentIn` is the refunded, unspent EURC.
116
- * - `lifi.flashloanRepay` settles the USDC leg from `buy.amountOut` (no
117
- * feeBuffer: `amountOut == principal + fee` by construction).
118
- * - `aave.repay(mode: 'max')` repays the unspent EURC (`buy.unspentIn`) straight
119
- * back into the freshly-minted EURC debt, unwinding the borrow buffer. Any
120
- * genuine over-repay surfaces through the repay op's `residual`, swept to the
121
- * signer.
122
- *
123
- * After execution the signer holds the same WETH collateral on Aave V3 with EURC
124
- * debt equal to the original USDC debt (in EUR terms) plus only the consumed
125
- * slippage/fee; the USDC debt is closed.
126
- */
127
- export const buildAaveDebtRebalanceExactOut = ({
128
- owner,
129
- collateralAmount,
130
- debtAmount,
131
- newDebtBorrowAmount,
132
- maxSlippageBps = DEFAULT_MAX_SLIPPAGE_BPS,
133
- partner,
134
- partnerAddress,
135
- partnerFeeBps,
136
- }: AaveDebtRebalanceExactOutInput): {
137
- flow: Flow;
138
- request: ComposeCompileRequest;
139
- } => {
140
- const sdk = createComposeSdk({ baseUrl: BASE_URL });
141
-
142
- // The USDC the BUY must produce: the flashloan principal plus the Aave V3
143
- // flashloan premium. This is exactly what settles the debt flashloan leg.
144
- const exactUsdcOut = (
145
- BigInt(debtAmount) + computeAaveV3Fee(BigInt(debtAmount))
146
- ).toString() as `${bigint}`;
147
-
148
- const builder = sdk.flow(8453, {
149
- name: 'aave-debt-rebalance-usdc-to-eurc-exact-out',
150
- inputs: {
151
- initialCollateral: resources.erc20(BASE_WETH, 8453),
152
- // Single flashloan: the old (USDC) debt token. No collateral flashloan is
153
- // needed because the WETH collateral never leaves Aave.
154
- debtFlashloan: resources.erc20(BASE_USDC, 8453),
155
- },
156
- });
157
-
158
- // --- Phase 1: provision the starting Aave V3 USDC borrow -----------------
159
-
160
- // WETH → aBasWETH via the aave routing edge family (Aave V3 supply). The
161
- // supplied collateral is never withdrawn — it backs the new EURC debt — so
162
- // the aBasWETH output is intentionally left unconsumed AND unswept: it is the
163
- // user's resulting collateral position and must stay on the (persistent,
164
- // per-user) proxy. This is why the flow does NOT set a blanket `sweepTo`
165
- // (which would try to transfer the aToken out and revert, since collateral
166
- // backing an open debt cannot be moved); instead we explicitly transfer only
167
- // the genuinely-loose tokens below.
168
- builder.lifi.zap('supply-collateral', {
169
- bind: { amountIn: builder.inputs.initialCollateral },
170
- config: { resourceOut: resources.erc20(A_BAS_WETH, 8453) },
171
- });
172
-
173
- // Borrow the USDC debt being rebalanced. The proceeds are the user's loan;
174
- // they remain as the proxy's residual USDC and are forwarded to the signer at
175
- // the end of the flow (see `payout-borrow-proceeds`).
176
- builder.aave.borrow('borrow-usdc-debt', {
177
- bind: {},
178
- config: {
179
- pool: AAVE_V3_POOL_BASE,
180
- asset: BASE_USDC,
181
- variableDebtToken: VDEBT_BAS_USDC,
182
- amount: debtAmount,
183
- },
184
- });
185
-
186
- // --- Phase 2: rebalance USDC debt → EURC debt ----------------------------
187
-
188
- // Settle the USDC debt with the flashloan. mode: 'max' makes Aave's Pool
189
- // clamp paybackAmount to the outstanding debt exactly; unspent flashloan
190
- // principal flows through the op's residual port (swept).
191
- builder.aave.repay('repay-usdc-debt', {
192
- bind: {
193
- assetIn: builder.inputs.debtFlashloan,
194
- onBehalfOf: builder.context.executionAddress,
195
- },
196
- config: { pool: AAVE_V3_POOL_BASE, mode: 'max' },
197
- });
198
-
199
- // Borrow a capped (generously over-sized) EURC amount against the same WETH
200
- // collateral, now that the USDC debt is cleared. The over-borrow is
201
- // intentional and is unwound below.
202
- const borrowEurc = builder.aave.borrow('borrow-eurc-debt', {
203
- bind: {},
204
- config: {
205
- pool: AAVE_V3_POOL_BASE,
206
- asset: BASE_EURC,
207
- variableDebtToken: VDEBT_BAS_EURC,
208
- amount: newDebtBorrowAmount,
209
- },
210
- });
211
-
212
- // Exact-output BUY: produce EXACTLY `exactUsdcOut` USDC from the borrowed
213
- // EURC, pulling only the EURC needed and refunding the rest to the proxy
214
- // (surfaced as `unspentIn`). `amountOut` is the exact figure that settles the
215
- // USDC flashloan leg.
216
- const buyUsdc = builder.paraswap.buy('buy-usdc-exact', {
217
- bind: { amountIn: borrowEurc.borrowed },
218
- config: {
219
- resourceOut: resources.erc20(BASE_USDC, 8453),
220
- exactAmountOut: exactUsdcOut,
221
- srcDecimals: EURC_DECIMALS,
222
- destDecimals: USDC_DECIMALS,
223
- maxSlippageBps,
224
- ...(partner !== undefined ? { partner } : {}),
225
- ...(partnerAddress !== undefined ? { partnerAddress } : {}),
226
- ...(partnerFeeBps !== undefined ? { partnerFeeBps } : {}),
227
- },
228
- });
229
-
230
- // Settle the USDC flashloan leg with the exact BUY output. No feeBuffer:
231
- // `amountOut == principal + fee` by construction.
232
- builder.lifi.flashloanRepay('repay-debt-flashloan', {
233
- bind: { funds: buyUsdc.amountOut },
234
- config: { leg: 'debtFlashloan' },
235
- });
236
-
237
- // Unwind the over-borrow: repay the proxy's FULL residual EURC back into the
238
- // freshly-minted EURC debt, so the new debt equals exactly the EURC the swap
239
- // consumed. We read the live EURC balance rather than binding `buy.unspentIn`:
240
- // unspentIn is only Augustus's refund (cap − used), but the proxy ALSO still
241
- // holds the headroom we over-borrowed above the swap cap (borrow − cap), which
242
- // Augustus never pulled. The live balance captures both, leaving zero idle.
243
- // mode: 'exact' pulls precisely this balance (Aave clamps to min(balance,
244
- // debt)); mode: 'max' would pass type(uint256).max and try to pull the whole
245
- // outstanding debt, far exceeding the residual on the proxy, and revert.
246
- const eurcResidual = builder.core.balanceOf('read-eurc-residual', {
247
- bind: {},
248
- config: { token: BASE_EURC },
249
- });
250
- builder.aave.repay('repay-eurc-remainder', {
251
- bind: {
252
- assetIn: eurcResidual.balance,
253
- onBehalfOf: builder.context.executionAddress,
254
- },
255
- config: { pool: AAVE_V3_POOL_BASE, mode: 'exact' },
256
- });
257
-
258
- // Forward the genuinely-loose token — the residual USDC loan proceeds — to the
259
- // signer. We read the proxy's LIVE USDC balance rather than the fixed borrowed
260
- // amount: Aave's `mode: 'max'` USDC-debt repay overshoots the borrow by up to
261
- // 1 wei (ray-math round-trip), so the proxy ends a wei short of `borrowed` —
262
- // transferring a fixed amount would revert "transfer amount exceeds balance".
263
- // The live balance always moves exactly what is there.
264
- //
265
- // We deliberately do NOT use a blanket `sweepTo`: the only other proxy-held
266
- // balance is the aBasWETH collateral position, which cannot be transferred
267
- // while the EURC debt is open (Aave's finalizeTransfer reverts). The position
268
- // therefore stays on the persistent per-user proxy; we move out only the USDC.
269
- const usdcResidual = builder.core.balanceOf('read-usdc-residual', {
270
- bind: {},
271
- config: { token: BASE_USDC },
272
- });
273
- builder.core.transfer('payout-borrow-proceeds', {
274
- bind: {
275
- amount: usdcResidual.balance,
276
- recipient: builder.context.sender,
277
- },
278
- config: {},
279
- });
280
-
281
- const flow = builder.build();
282
-
283
- const request = sdk.request(flow, {
284
- signer: owner,
285
- inputs: {
286
- initialCollateral: materialisers.directDeposit({
287
- amount: collateralAmount,
288
- }),
289
- debtFlashloan: materialisers.flashloan({
290
- providerKind: 'aave-v3',
291
- amount: debtAmount,
292
- }),
293
- },
294
- // No blanket `sweepTo`: the resulting aBasWETH collateral position stays on
295
- // the proxy (it backs the open EURC debt and cannot be moved). The loose
296
- // USDC proceeds are forwarded via the explicit `payout-borrow-proceeds`
297
- // transfer above.
298
- });
299
-
300
- return { flow, request };
301
- };
@@ -1,70 +0,0 @@
1
- import type { ComposeCompileRequest, Flow } from '@lifi/compose-spec';
2
-
3
- import { createComposeSdk, guards } from '../../index.js';
4
- import type { Address } from '../../types.js';
5
- import { BASE_URL } from '../config.js';
6
-
7
- // Base mainnet Aave v3 Pool.
8
- const AAVE_V3_POOL = '0xA238Dd80C259a72e81d7e4664a9801593F98d1c5';
9
-
10
- // 1.0 WAD (1e18) — minimum acceptable health factor.
11
- const MIN_HEALTH_FACTOR_WAD = '1000000000000000000';
12
-
13
- export interface AaveHealthFactorExplodeInput {
14
- readonly signer: Address;
15
- }
16
-
17
- /**
18
- * EXPLODE / DecomposeValue showcase.
19
- *
20
- * NOTE: currently disabled end-to-end. `aave.getHealthFactor` is in the
21
- * default `DISABLED_COMPOSE_OPS` set on zap-backend because the EXPLODE
22
- * opcode is not yet supported on-chain. The example still builds a valid
23
- * Flow JSON (and its spec passes), but submitting the request to a backend
24
- * with default config will fail at op resolution. Re-enable by unsetting
25
- * `aave.getHealthFactor` in the backend's `DISABLED_COMPOSE_OPS` once
26
- * contracts ship EXPLODE.
27
- *
28
- * Builds a minimal compose flow that:
29
- * - StaticCalls `IPool.getUserAccountData(user)` on Aave v3 (Base)
30
- * - Decomposes the returned 6-tuple via the EXPLODE opcode
31
- * - Asserts `healthFactor >= 1.0 WAD` via `core.numericInvariant`
32
- *
33
- * The signer must hold an Aave v3 position on the target chain so HF > 0.
34
- * Uses `allow-revert` so the compiled calldata is returned even if the
35
- * assertion would fail — handy for tracing on a fork.
36
- */
37
- export const buildAaveHealthFactorExplode = ({
38
- signer,
39
- }: AaveHealthFactorExplodeInput): {
40
- flow: Flow;
41
- request: ComposeCompileRequest;
42
- } => {
43
- const sdk = createComposeSdk({ baseUrl: BASE_URL });
44
-
45
- const builder = sdk.flow(8453 /* Base */, {
46
- name: 'aave-hf-explode-showcase',
47
- inputs: {},
48
- });
49
-
50
- builder.aave.getHealthFactor('checkHf', {
51
- bind: {},
52
- config: { pool: AAVE_V3_POOL, user: signer },
53
- guards: [
54
- guards.coreNumericInvariant({
55
- port: 'healthFactor',
56
- op: 'gte',
57
- threshold: MIN_HEALTH_FACTOR_WAD,
58
- }),
59
- ],
60
- });
61
-
62
- const flow = builder.build();
63
- const request = sdk.request(flow, {
64
- signer,
65
- inputs: {},
66
- simulationPolicy: 'allow-revert',
67
- });
68
-
69
- return { flow, request };
70
- };