Deploy BAL & aBAL from the Collector Contract

Aave + CoW Swap Trade

What is CoW Swap?

CoW Protocol is a fully permissionless trading protocol that leverages Batch Auctions as its price finding mechanism. CoW Protocol enables batch auctions to maximize liquidity via Coincidence of Wants (CoWs) in addition to tapping all available on-chain liquidity whenever needed. Batch Auctions are continuously run by the protocol because the solvers, the parties in charge of finding the most optimal settlement for the batch, are in competition to settle it. The winning solver will be the one that can maximize trader surplus by either having the most optimal CoW, finding the best liquidity sources, or combining both in a single settlement.

Check out documentation here

Why CoW Swap?

CoW has been used before for big DAO trades to protect against MEV, to protect against bad slippage, and to find liquidity for trades of this magnitude.

The biggest profile trade to date is the ENS trade.

Implementation

The current code can be found here

We will be leveraging Milkman to do a contract-to-contract interaction. This allows Aave to avoid the need to interact with the CoW Swap API offchain and allows for onchain enforceable dynamic slippage protection. In other words, the trade will execute only if the buy amount is higher than the minimum defined by the milkman contract instance.

ENS DAO used Milkman as well for their trades.

What is Milkman?

Milkman is maintained by the team at Yearn. They developed it as an extension of the CoW Swap protocol.

Using contracts, it can add extra validation in the form of price checkers:

To protect customers against bad slippage that might be offered by COW Swap, by using an oracle to validate the price, we can be certain that the DAO will not trade at a lower price (accounting for our permitted slippage) than our specified Oracle is returning at the time of the transaction, otherwise, the transaction will revert.

Liquidity

CoW Swap allows orders to be matched against other orders via Coincidence of Wants or against any available external liquidity.
Solvers (SeaSolver) are responsible for finding trading paths and proposing them as part of the auction mechanism of the protocol.
For tokens without available liquidity pools, solvers can propose a path that involves depositing liquidity or collateral and minting of LP tokens.

For our particular 80BAL-20WETH BPT trade, CoW Swap solvers might just deposit directly into Balancer and mint the BPT tokens for us, always accounting for our specified slippage. For more information on SeaSolver, the latest proprietary solver developed by CoW Swap, please read here.

Solvers are private independent entities that participate in the CoW Protocol. They are not required to open source their code, and this is not necessary as eventually the execution parameters are checked onchain to be within the defined slippage tolerance the user has defined.

Here is an example of 80BAL-20WETH trade that incurred no slippage: Ethereum Transaction Hash (Txhash) Details | Etherscan

The Trade

The trade will do the following:

  1. Handle approvals and transfers from the Collector contract onto a newly deployed COWTrader.sol contract.
    a. Withdraw aBAL.
    b. Withdraw aEthBAL.
    c. Transfer all BAL now held in Collector to COWTrader.sol
    d. Transfer specified wETH quantity to COWTrader.sol
  2. The COWTrader contract has one function to execute two trades using Milkman utilizing its balance of wETH and BAL respectively. Both trades take the held token for 80-BAL-20-WETH BPT.
  3. Milkman creates a new instance per trade, which will then hold the funds until the trades are settled via COWSwap. What Milkman does under the hood is approve COWSwap’s Settlement contract so it can transfer funds once there is a match.
  4. Once there is a match, the dynamic price checker will protect Aave’s funds against a bad quote. If it passes our specified parameters, then the trade will go through and the Collector contract will receive 80-BAL-20-WETH.

If everything goes well and the number of tokens to be received matches our parameters (slippage, price against our oracle) then the collector contract will receive the BPT.

The trade can remain open in perpetuity until it is canceled (by Aave) or settled.

Reasons to cancel

If the trade is not settled, the trade can be canceled by a function provided in the AAVECOWTrader contract. In that case, all funds will be returned to the collector contract.

This can be updated to extend the contract where slippage can be increased in order to settle.

After speaking with CoW Swap, they are confident 1.5% slippage should suffice, though we could go with 2% (they will try to match as best they can, so it doesn’t mean 2% slippage will be 2%, that’s just the maximum that can be incurred).

Protection of Funds

Milkman uses msg.sender to execute trades and to cancel, so the call can only be coming from AAVECOWTrader to cancel and it has hardcoded the AAVE Collector as the receiver of funds.

We’ve added reentrancy protection as well, as an owner concept which can be an AAVE DAO Controlled address.

The price checker was reviewed by Llama and Balancer teams, and we discovered a potential vulnerability if the price of the Chainlink oracle returned 0 which was subsequently mitigated by the CoW Swap team.

The price checker code can be found here.

The calculator used by the price checker can be found here.

Trade Example

wETH address: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
Amount to trade: 338.10 wETH

Expected BPTs: 40,941

This comes out to ~121 BPT per wETH, which matches the TVL / supply of the BPTs and would come at around $1,750 worth of wETH using CoinGecko’s price.

BAL address: 0xba100000625a3754423978a60c9317c58a424e3D

Expected BPTs: 111,716

BPTs per BAL: 0.37

BPT Info

Here.

1 Like