BUIDL Launchpad is now live! 🎉

Down the Rabbit Hole: Mean Finance

Mean Finance is a Dollar Cost Average (DCA) open protocol that allows users or Dapps to DCA any ERC20 token to any other ERC20 token with access to their preferred frequency, all while maintaining decentralization and protecting personal data from being given to any centralized parties. Mean Finance uses Chainlink and Uniswap V3 TWAP oracles to accomplish all of this.

The protocol is set up so that customers are protected against volatility and market makers have good incentives to carry out trades:

Here is what Mean Finance offers to the users and the market makers:

Users:

  • Enhanced protection against the volatility of the markets.
  • Access to trade which is MEV protected.
  • Access to gas-less DCA.

Market Makers:

  • They may leverage the entire protocol balance for trade execution.
  • Earn some percentage of fees.
  • Arbitrage the pools.

The design of Mean Finance helps minimize all sorts of MEV-related activities for the users. With the occurrence of coincidence of wants within the pools, the users get matched by the same pool; otherwise, the market maker takes care of trade executions at the desired cost.

Swaps in Mean Finance

Swaps play a crucial role in the ecosystem of Mean Finance. There are several incentives in place to encourage developers to carry out swaps.

Multi Swaps

They used a Factory/Pairs strategy in v1. Now, with the Hub, everything comes under the same contract, thus allowing Multi Swaps thanks to this update.

Typically, swaps are carried out between two separate tokens, and you exchange one token for another by sending one. In Mean Finance, you can perform simultaneous exchanges involving a variety of tokens.

This helps save gas while lowering the liquidity amount required for swap executions.

Here is an instance of how it works:

  • Suppose for WETH/DAI swap, on offering 100 DAI, and one can get 200 WETH in return.
  • For WETH/USDC swaps, one may offer 100 WETH to get 50 USDC in return.
  • Now, for USDC/DAI swaps, one will have to provide 200 USDC to get rewarded with 200 DAI in return.

If there's a need to execute the swaps individually, swappers must provide different tokens every time. Even on re-using the awards from a given pair for the other, several token transfers will occur, causing a rapid rise in the gas fee.

However, with multi swaps, there isn't any need for unnecessary token transfers and no custom code requirements, as it all gets handled by the Hub to be used by the swappers.

Here is what the swappers get in return for providing liquidity and spending gas for executing swaps:

  • Due to the price of oracles used by the Mean Finance pairings, there may be a chance for arbitrage between oracles and the other DEXes.
  • Like flash swaps, Mean Finance sends a reward when the swap is executed.
  • Likewise, flash loans, Mean Financing, will permit swappers to borrow the liquidity of the protocol for free in the swap.
  • They will split the protocol fees for the trade with the swapper. Currently, they will receive 0.45% of the traded tokens, 3/4 of the 0.6 percent.

Next Swap

In contrast to AMMs, Mean Finance swappers cannot define the amount they would like to swap, and the placements established for the various pairs determine these sums. Understanding which tokens must be delivered and how much of a reward must be offered would therefore be the first step in carrying out a swap.

Until the Next Swap

All positions have swap intervals that govern when to swap. Therefore, a pair may run out of swap execution at some point. If so, attempting the swap execution for just that pair would result in a revert. Check the availability of the next swap to find out when it will be available.

Information about Next Swap

If secondsUntilNextSwap for any pair returns zero, you can perform a swap immediately. If so, you can call getNextSwapInfo to learn everything there is to know about this exchange and decide if you want to carry it out or not.

Here you can find their functions.

Performing a Swap

You can carry out the trade once satisfied with the tokens to supply and the reward. There are several ways to do it, including:

  • Flash swap: A flash swap allows you to borrow liquidity and receive rewards before returning the needed funds.
  • Swap for caller: You only need to transmit the required amount for a "swap for caller," which is more straightforward and doesn't require much Ethereum knowledge to receive the reward.
  • Swap for DEX: A "swap with DEX" is one in which liquidity is not required. You just cover the expense of the petrol and keep the payment to yourself.

Managing Positions

Mean Finance is centered on positions. Users declare their intent to trade tokens when they open a position, and these jobs are open to creation, modification, and termination.

To try and make things clearer, here's an example:

Consider a person who wants to purchase WETH and has some DAI. He wants to establish a Mean Finance position because he lacks market timing expertise. His position would be as follows if he wants to purchase 5 DAI worth of WETH everyday for a year:

  • From: DAI
  • To: WETH
  • Rate: 5 DAI
  • Amount of swaps: 365
  • Swap interval: daily

John must deposit all essential funds at the outset of constructing the position. In his situation, the amount would be 5 x 365, or 1825 DAI. On the other hand, since the swaps are made daily, he won't have to make any further deposits or pay any petrol costs.

Modifying Positions

Users have a few options for changing their positions. People can:

  • Add extra money to their account
  • Take money out of their position
  • Restructure the available resources

As you may have observed, a position can only have its "rate" and "number of swaps" properties changed. There is no way to modify the "swap interval," "from," or "to" properties.

Additionally, users have the option to leave their job at any time. The remaining and exchanged balance is refunded to the user, and the position is destroyed.

Stale Positions

Various incentives are in place to encourage swappers to complete all transactions. Now, multiple factors can have an impact on these incentives:

  • The general sentiment of the crypto market
  • The demand/popularity of the tokens used in the swap
  • The token volumes involved in the swap

Therefore, some posts may remain unfilled for a long time. These positions are referred to as "stale." Fees are only incurred on swaps; thus, the balance stays unaffected if the positions become stale.

Architecture

Mean Finance v2 is separated into Core and Periphery like Uniswap.

Smart contracts on the Periphery side are intended to facilitate domain-specific interactions with the Core.

Whereas, the Core contains all contracts required for the protocol to function.

Contracts

The most significant contracts are:

1. The Companion

The Hub is the most significant contract in the protocol. However, the user experience is not the best because of contract size limitations. The Companion can help in this situation.

A Companion is a smart contract that coexists with the Hub and communicates with it to enhance user experience and offer some lacking functionalities.

2. Hub

In terms of architecture, Mean Finance used a Factory/Pairs strategy in version 1. There was a factory contract that would deploy new pairs while still keeping track of the current ones, and each pair would be a separate contract.

Although the strategy was effective, deploying new contracts for every new pair was pretty expensive, particularly on the Ethereum Mainnet. This method also made it very difficult to benefit from the Coincidence of Wants when exchanging.

They, therefore, developed "The Hub" for v2. There is no need to deploy new contracts because the Hub is a single contract that manages all pairs. Additionally, carrying out swaps for numerous pairs in a single transaction is more straightforward and less expensive.

3. Permission Manager

The contract that manages NFT ownership/transfer and other rights is called the Permission Manager.

Responsibilities:

  • Handles ownership and transfer of NFT
  • Only the Hub may perform mints and burns when a position is established or terminated
  • Permits for supports
  • Manages extra permissions
  • Encourages the use of the EIP-2612 permit for further permissions

4. Oracles

Oracles are an essential component of the system since they reveal the token prices at the time of swaps. This means that a pair can only be supported by Mean Finance if there is an on-chain oracle that does so. Thus, Mean Finance merged two alternative sources, Uniswap v3 and Chainlink, to enable as many pairs as possible.

They have developed an "Oracle Aggregator" to use both. This contract serves as a proxy when requests for quotes are made because it is aware of the other two oracles. The proxy logic is relatively straightforward. If the Chainlink oracle supports the specified pair, we shall use Chainlink, and if not, we shall switch to Uniswap v3 as a backup.


This is because the volume and liquidity of each pair affect how reliable Uniswap v3 oracles are. It would be safer to use Chainlink when it's available and to attempt to steer clear of erroneous quotes because there's no control over any of this.

5. Libraries

On the Periphery, several libraries ought to do the job as a developer simpler.

  • InputBuilding: The specific parameters required to carry out a swap are covered in the swap section. This library helps with integrations because it translates parameters from a more user-friendly format into the one the Hub expects.
  • ModifyPositionWithRate: When changing a position in version 1, you had to specify a new rate and number of swaps. Although they altered the strategy in version 2, they still built the library for users who favor the earlier method.
  • SecondsUntilNextSwap: Timing is everything when carrying out a DCA position. Use this library to determine the length of time it will take you to receive the next swap if you'd like to know how long you'll have to wait.

Fees

In Mean Finance, fees are very straightforward. Gas and protocol fees are the two categories.

Gas fee

Gas costs are only incurred when users interact with the positions. This includes:

  • Creating their position
  • Modifying their position
  • Withdrawing their swapped balance
  • Terminating their position
  • Granting or rescinding authorizations

Users won't have to pay for gas when making exchanges. Swappers are responsible for paying this network cost.

Protocol costs

Users are not charged a protocol fee when interacting with the positions; only exchanges are subject to protocol fees. Each user involved in a swap is charged a protocol cost at the execution time, and that charge is currently 0.6 percent.

Token Integration

Mean Finance does not support all of the tokens that the oracle provider might claim to. They have an allow list system that regulates which tokens can be traded or deposited.

The allowlist enables us to research new coins before integrating them into Mean Finance and avert harmful situations.

Additionally, if you want Mean Finance to support a new token, join the #new-token-request channel on their Discord server.


Looking for more content to learn about Web3?
Subscribe to our newsletter for weekly updates 👇

Connect with us