galacticcouncil / galacticcouncil/hydration-node
Swap debt or collateral to different asset
- Dominant language
- Rust
- Stars
- 208
- Forks
- 109
- Avg merge
- 6d 3h
- Merged PRs (30d)
- 3
Description
### **Title**
Aave v3: Integrate ParaSwap Debt/Collateral Swap via Hydra Router
---
### **Background**
Hydration already runs an Aave v3 deployment. Aave itself is unaware of ParaSwap/routers and exposes only the standard pool interface (`flashLoan`, `borrow`, `repay`, `supply`, `withdraw`, etc.).
ParaSwap provides audited Aave adapters (debt and liquidity adapters) that orchestrate:
- Flash loan on Aave
- Asset swap via Augustus
- Repay old debt / collateral
- Open new debt / collateral
On Hydration, we want to reuse these adapters but replace the Augustus “swap” leg with a Hydra-native swap via the router precompile, without touching Aave v3 contracts.
---
### **Problem**
Users currently cannot:
- Swap **debt asset X → debt asset Y** within an Aave v3 position (e.g. borrow USDC → move to DAI).
- Swap **collateral asset A → collateral asset B** (e.g. wETH → wBTC) in a controlled way.
We want:
- Fully **atomic** position transformations.
- Minimal changes to existing, audited contracts.
- A design that cleanly integrates with Hydration’s DEX/router.
---
### **High-Level Design**
1. **Reuse ParaSwap Aave adapters**
Use the existing ParaSwap adapter contracts as the orchestration layer for **collateral** and **debt** swaps.
We only change how they execute the swap:
- Original design: adapters call ParaSwap’s **Augustus** (`bytes paraswapData`).
- Hydration design: adapters call a **Hydra Augustus** contract at a configured address.
No changes to Aave v3 contracts, no new Substrate pallets.
2. **Implement Hydra Augustus (swap adapter)**
Implement a new EVM contract (“Hydra Augustus”) which:
- Exposes the minimal subset of the **Augustus interface** that the ParaSwap Aave adapters use.
- Accepts `bytes paraswapData` as an opaque payload.
- Decodes `paraswapData` into a Hydra-specific struct, for example:
```solidity
struct HydraRouteData {
bytes route; // router-specific route/path encoding
uint256 minAmountOut;
// optional: flags, deadline, etc.
}
```
- Calls the **Hydra router precompile** to perform the actual token swap:
- `tokenIn`, `tokenOut`, `amountIn` from adapter context
- `route` and `minAmountOut` from `paraswapData`
- Bubbles up errors on failed swaps so the adapter can revert the entire transaction.
3. **Route encoding via `paraswapData`**
We use `paraswapData` to carry all routing information needed by the Hydra router:
- Frontend/SDK:
- Computes a route using Hydration’s routing logic (path, pools, hops, etc.).
- Packs it into `HydraRouteData` and ABI-encodes it into `paraswapData`.
- Hydra Augustus:
- Decodes `paraswapData` to recover `HydraRouteData`.
- Forwards `route` and `minAmountOut` to the router precompile.
This mirrors ParaSwap’s original usage (opaque routing blob), but now interpreted by Hydra Augustus instead of the original Augustus.
4. **Flashloans**
- v1 uses **Aave’s native `flashLoan`** exactly as ParaSwap adapters expect.
- Hydration’s flashloan precompile is **not required** for the initial implementation.
- Future work can explore using Hydration flashloans as an additional/alternative liquidity source; that is explicitly out of scope for this issue.
---
### **User Flows**
1. **Debt Swap: debt asset X → Y**
User has collateral and an existing debt position in asset X.
- User / UI calls ParaSwap `DebtSwapAdapter` on EVM with:
- `debtAssetX`, `newDebtAssetY`, `debtAmountX`, `minAmountY`, `paraswapData`.
- Adapter:
- Takes Aave `flashLoan` in asset **Y**.
- Calls **Hydra Augustus** with the provided `paraswapData`.
- Hydra Augustus:
- Decodes `HydraRouteData` from `paraswapData`.
- Swaps **Y → X** via the router precompile, respecting `minAmountOut`.
- Adapter:
- Repays old debt **X** using swapped X.
- Borrows **Y** on Aave as new debt.
- Repays the Y flashLoan with the newly borrowed Y.
- Entire operation is atomic; any failure (swap, repay, borrow) reverts everything.
2. **Collateral Swap: collateral asset A → B**
User has collateral asset A supplied on Aave.
- User / UI calls `LiquiditySwapAdapter` with:
- `collateralAssetA`, `collateralAssetB`, `amountA`, `minAmountB`, `paraswapData`.
- Adapter:
- Orchestrates withdraw of collateral A (possibly via `flashLoan`, per adapter logic).
- Calls **Hydra Augustus** to swap **A → B** using the router + route from `paraswapData`.
- Supplies B back as collateral on Aave.
- Repays any flashLoan if used.
- Result: collateral asset updated while overall HF is preserved according to adapter logic.
---
### **Scope (v1)**
**In scope**
- Deploy ParaSwap Aave adapters on Hydration (if not already deployed).
- Implement **Hydra Augustus** contract:
- Minimal Augustus-compatible interface required by the adapters.
- Internal integration with Hydra router precompile.
- Define and document the **Hydra `paraswapData` ABI**:
- `HydraRouteData` structure.
- Route encoding expectations (opaque `bytes` understood by the router precompile, or a defined path format).
- Configure adapters to use the Hydra Augustus address.
- Add tests that:
- Perform a debt swap X → Y via Hydra Augustus + router.
- Perform a collateral swap A → B via Hydra Augustus + router.
- Validate Aave account data (debt, collateral, HF) before/after.
**Out of scope (v1)**
- Using Hydration flashloan precompile (Aave `flashLoan` only).
- Adding new Substrate pallets or runtime changes.
- Advanced risk controls (HF guardrails, Omnipool impact limits, asset allowlists, etc.) – can be follow-up tasks.
---
### **Open Questions**
- Exact **route format** for the router precompile:
- Do we use a path of AssetIds, or a fully opaque `bytes` blob interpreted by the precompile?
- Any basic route validation in Hydra Augustus?
- Max hops, max encoded size, etc., or treat `paraswapData` as fully opaque and trust the frontend/routing engine.
- Governance / configuration:
- Which asset pairs are allowed for debt/collateral swaps?
- Do we enforce any extra Health Factor constraints on top of what the adapters already enforce?
---
### **Acceptance Criteria**
- [ ] ParaSwap Aave adapters are deployed and configured to use a Hydra Augustus address.
- [ ] Hydra Augustus is implemented and successfully calls the Hydra router precompile for swaps.
- [ ] `paraswapData` format for Hydration is documented and tested end-to-end.
- [ ] A debt swap X → Y completes successfully on a test environment, with correct Aave position and HF after the swap.
- [ ] A collateral swap A → B completes successfully on a test environment, with correct collateral composition and HF after the swap.
- [ ] Failure cases (invalid route, slippage too high, router/precompile failure) revert the entire transaction and leave user positions unchanged.
Contributor guide
Assessment
This issue has not been assessed yet.