Video thumbnail

Buy and Chill

Innovative Always Up DeFi instrument! Reduces ETH and BTC volatility. Not financial advice. DYOR!

Buy and Chill

Created At

ETHGlobal Prague

Project Description

This DApp implements a new DeFi instrument that tracks the price of a simple moving average like Bitcoin's 200 week moving average or Ether's 2000 day moving average. These two examples of moving averages have never had a negative day in history! Not financial advice! Do your own research!

The goal is to create a less volatile investment vehicle that still captures the long-term trend of the cryptocurrency. It reduces volatility from the underlying asset like ETH/USD or BTC/USD. While most of the time, these particular MAs (moving averages) underperform the spot price for ETH and BTC, they are historically proven to give returns significantly higher than stable coins in protocols like AAVE or USD treasury bills. And they never had a negative day in history (doesn't guarantee future performance)!

A few pros for this instrument as opposed to the underlying assets or yield bearing stable coins:

Perfect for the granny: A BTC or ETH investment mechanism if you don’t like volatility, but still dislike FIAT devaluation. Stablecoins portfolio alternative: Instead of protecting themselves from volatility by partially or fully parking jn stablecoins with yield, it is better to keep in one of the two indeces and get better return on it because it never went down so far. Users can do that at least until it has its first negative day. These two instruments have medium of exchange qualities because they have low volatility. Finally! A form of deflationary money with steady appreciation and no negative movements. These statements do not constitute financial advice! Do your own research and note that past performance does not guarantee future returns! << TODO: Add chart for comparative metrics BTC200WMA and ETH2000DMA vs AAVE USDC yield >>

Quick How It Works: The user deposits DAI and gets a newly minted index token. One unit of the index token closely follows the value of the corresponding Simple Moving Average. For example, at the time of writing, the ETH/USD 2000 Day SMA values $1991 (ETH/USD spot price is $2640). To get 1 ETHUSD2000DMA, the user must deposit 1991 DAI. The reverse - the user burns the index token and receives the equivalent DAI amount. The on-chain implementation is based on a vault that holds a combination of ETH and DAI to match the value of the moving average. When the user deposits DAI, the vault contract calculates if it is necessary to rebalance the vault and if necessary, execute a swap (i.e. buy a specific amount of ETH). When the user burns the index token, the vault calculates and sells enough ETH to keep the moving average and transfer the correct amount of DAI to the user. In general, every interaction with the vault can result in rebalancing ETH DAI holding if a given tolerance value has been exceeded (save gas for small amounts).

How it's Made

High level architecure

This is a DApp that is built with solidity smart contracts and besides a frontend and a few initialisation scripts, it has no other centralised components. As frameworks and tooling - the Scaffold-ETH 2 is used both for contracts, scripts, tooling and frontend. The DApp is deployed on Arbitrum Mainnet and was developed entirely based on a local Anvil mainnet fork.

Vault Contract: This handles token management and rebalancing logic.

Pyth Network Oracle: The integration with Pyth Oracle is done via PythIntegrator.sol contract. The role of the oracle is to update daily asset prices and add them to the set of datapoints based on which the moving averages are calculated.

1Inch: We need to do rebalancing - it means we need to use a DEX. 1inch was chosen because we need TWAP strategy using Limit Orders to gradually build positions at the moving average price.

Contract interactions (Flow) There are in fact two main flows in the whole scheme, BUY and SELL MA tokens. Both of them should have the same flow and trigger interaction between the same components. The happy case result of the two operations is a transfer of DAI tokens and a transfer of MA tokens. Depending on the current underlying asset price, and the stored MA price, the transaction can first trigger a rebalance. After the rebalance has completed (which involves swapping tokens), the MA tokens and DAI are transfered.

So, a user triggers a BUY or SELL from the Frontend. The Frontend first gets the latest price feed for an MA from Pyth Hermes in order to push it to the contracts along within the same transaction that calls the @Vault.sol contract. The @Vault.sol contract needs to check if it needs rebalancing. It calls @PythIntegrator.sol to validate the offchain price (Pull method) and @PythIntegrator.sol updates @PriceDataStorage.sol with daily price and MA. Then the @Vault.sol , having fresh price data, can decide whether to trigger a rebalance or not. If a rebalance needs to be triggered, the @Vault.sol initiates a swap by calling @1inchIntegrator.sol that in turn calls 1inch on chain contract (aggregator contract). After that, for the BUY operation, the @Vault.sol mints MA tokens to user's wallet and transfers DAI tokens from the user. For the SELL operation, the @Vault.sol burns MA tokens and transfers DAI tokens to the user.

Pyth Integration The data set is initially populated in the PriceDataStorage.sol contract by a script that reads all the prices from the price_data.json file. The file in turn is populated with historic data retrieved from PYTH TradingView API endpoint by FetchPythHistoricalData.js . Once initialized, the system needs regular price updates to maintain accurate moving averages. The PythIntegrator.sol contract connects to the official Pyth Network contract on-chain. It implements the "Pull" model where fresh price data must be fetched from Pyth's Hermes service off-chain.

1inch Integration Implemented in the 1inchIntegrator.sol

Vault Strategy Asset Allocation For each MA token type, the vault should maintain a dynamic allocation between:

The underlying cryptocurrency (ETH or BTC) Stablecoins (DAI) The allocation ratio is determined by the relationship between the current price and the MA:

When price = MA: 50% crypto, 50% stablecoins When price > MA: Less crypto, more stablecoins When price < MA: More crypto, less stablecoins The percentage allocated to the cryptocurrency can be calculated as: crypto_allocation = 50% * (MA / current_price)

This formula automatically adjusts allocation based on price deviation from the MA:

At MA price: 50% * (MA/MA) = 50% in crypto At 2x MA price: 50% * (MA/(2MA)) = 25% in crypto At 0.5x MA price: 50% * (MA/(0.5MA)) = 100% in crypto (capped at 100%)

Rebalancing When rebalancing is triggered:

Calculate Target Allocation: Determine the current price and MA value Calculate target crypto percentage using the formula Calculate target DAI percentage (100% - crypto percentage)

Execute Swaps: If current allocation deviates by >5% from target: Swap crypto for DAI or vice versa to reach target allocation

When users deposit DAI: Calculate target allocation at current price/MA ratio Immediately swap appropriate portion of DAI for crypto

When users withdraw MA tokens: Burn the tokens Return proportional amount of crypto and DAI based on current vault composition

Frontend components The majority of the frontend code is implemented in @packages/nextjs/app/page.tsx and @packages/nextjs/components/buy-and-chill/TradingViewChart.tsx One page react app that has two screens (wallet not connected and connected).

Future improvements Optimize Rebalancing Strategy There are several scenarios where the vault's value could decrease from say 1000 DAI to 900 DAI even when the MA index remains stable.

  1. Trading Losses and Slippage. BTC oscillates around the MA but never strays far from it. This is a classic "whipsaw" problem where frequent trading around a range erodes capital. After multiple swaps, these costs accumulate to reduce vault value

  2. Gas Costs for On-chain Rebalancing

  3. Impermanent Loss-like Effect. When price moves above MA, the vault reduces crypto exposure. If price continues upward significantly before returning to MA, the vault misses some upside. When price finally returns to MA, the vault has less total value than if it had simply held With back testing and careful calibration of parameters, these risks of decreasing vault NAV can be mitigated. And at the same time, these losses be compensated with “accidental” profits from swapping.

background image mobile

Join the mailing list

Get the latest news and updates