~/arifin
GitHub ↗
~/arifin · Indonesia
WorkWritingAbout
GitHub ↗LinkedIn ↗Email
← work
2025·Solo build·oss

Vault Strategy

OSS ↗
backgroundManaging Bitcoin leverage positions manually requires constant monitoring and precise timing — humans consistently miss the signals or act too late.
goalAutomate leverage management using EMA signals with self-custody vaults integrated into Aave for yield, removing human timing from the equation.
outcomeVault Strategy — an automated EMA-based leverage manager with isolated user vaults, three risk tiers, and 119 passing tests.
TypeScriptDeFiEMAAutomationSmart ContractsEVMAaveBitcoinFactory PatternSolidity

Leveraged Bitcoin positions are powerful but exhausting to manage manually. You're watching EMA crossovers, deciding when to scale up or down leverage based on trend strength, and if you're asleep or distracted when the signal fires, the position runs ahead of you. I wanted to automate the timing decisions entirely, using signals I already trusted, without giving custody to a centralized platform.

The system uses 20/50/200-day EMA signals to detect trend strength across five levels: strong bullish, bullish, neutral, bearish, strong bearish. Each level maps to a leverage target within the user's chosen risk tier (capped at 1.1x, 1.3x, or 1.5x maximum). The architecture is a factory contract pattern — each user gets their own isolated strategy contract, separately deployed, that integrates with Aave for collateral deposits and borrowing. Rebalancing is triggered on-chain when the signal changes, not by a centralized cron job. User deposits stay separate from active collateral, and users can manually repay debt or withdraw from Aave at any time.

The main engineering challenge was gas efficiency. Rebalancing operations interact with Aave — which involves multiple storage reads and external calls — and if the gas cost of rebalancing is significant relative to the leverage adjustment being made, the system starts to eat its own returns. Optimizing the Solidity callpath, reducing redundant storage reads, and batching Aave interactions required careful work. The 119 passing tests include end-to-end simulations that model rebalancing under different market regimes: trending markets, choppy sideways action, and rapid reversals.

The result is a self-custody leverage manager where the rules are transparent and auditable, the vaults are isolated per user, and the timing decisions are removed from the human loop entirely. One command deploys both contracts and frontend locally.

← all workwriting →
OSS ↗
TypeScriptDeFiEMAAutomationSmart ContractsEVMAaveBitcoinFactory PatternSolidity
← all workwriting →