Flashloans in the squeeth ecosystem

Joseph Clark
Opyn
Published in
8 min readMay 31, 2022

Opyn is deploying a helper contract to improve squeeth trade flows. Most of the improvements use some kind of flashloan, where an asset is borrowed and repaid in a single transaction.

The Crab contract already uses flashswaps to mint and burn oSQTH. The new ControllerHelper contract provides flashswaps for any squeeth vault, and adds new features to help manage Uniswap LPs.

Here we revise the basic idea of a flashloan and show how they help with four flows:

  1. Flashmint: go from long oSQTH (or zero) to short
  2. Flashburn: go from short oSQTH to long oSQTH (or zero)
  3. Flashswitch: replace eth collateral in a vault for a Uniswap LP
  4. Flash-rearrange: swap eth for oSQTH inside a Uniswap LP while it’s in a vault

Squeeth review

Squeeth is a system that gives an exposure to the squared price of ethereum. Owning the oSQTH token gives a payoff that increases quadratically as the eth price increases. It’s a way to make a leveraged bet on ethereum, where the leverage increases as the price increases, without risking liquidation.

Squeeth is squared ethereum

The oSQTH token is created by locking some collateral into a vault which allows the depositor to mint an amount of oSQTH. The contract mechanism delivers funding from longs to shorts and handles liquidations.

Squeeth’s Controller contract accepts two kinds of collateral: eth and Uniswap LPs for the oSQTH/eth pool. Using Uniswap LPs as collateral allows oSQTH minters to earn trading fees on their collateral while also improving liquidity for the contract.

Interactions with the vault can be cumbersome because collateral cannot be moved without affecting the solvency of the vault. Collateral is frozen in place until it is removed from a vault.

The new ControllerHelper contract un-freezes collateral inside a vault, allowing a vault holder to re-arrange collateral in-place.

Flash loans unlock frozen liquidity

Flashloans and flashswaps

A flashloan is borrowed and repaid within a single transaction. This is useful in situations where the order of transfers matters.

A simple example is trade credit. An apple store buys apples (organic, not silicon) from a farmer and sells them to the customer. The apple store gets a loan of dollars to buy apples and repays the loan after selling the apples.

The lender usually takes the risk that the loan will not be repaid, but these are blockchain apples, so the chain of transfers can be conducted in a single transaction:

  • Borrow $100
  • Buy 100 apples for $1 each from farmer
  • Sell 100 apples for $2 each to customers
  • Repay $100 loan

The flashloan is structured so that all these things happen or nothing happens. The lender can safely lend a large amount of $, the farmer will get $ before handing over the apples, and the apple store will earn a guaranteed profit of $1 per apple.

A flashswap is a species of flashloan where a user sells an asset they do not own, provided that they acquire the asset by the end of the transaction. It is a flashloan where the loan is provided by the swap contract itself. In our apple example if the apple farmer provided the flashloan as well as selling the apples, this would be a flashswap.

Flashloans are implemented through a ‘callback’ function in a contract. It works like this: a contract calls a loan function on an external flashloan provider contract. The provider contract calls the callback function on the original contract, sending the loaned amount. The callback function conducts its business, and repays the loan as its final action. If the callback function doesn’t have enough money to repay the loan, or if it fails for any other reason, the whole transaction reverts.

Flashloan contract flow

Four use cases

Flashloans and flashswaps implemented in the ControllerHelper contract help with four important trade flows:

  1. Flashmint: borrow eth to mint oSQTH and repay by selling oSQTH
  2. Flashshburn: borrow oSQTH to withdraw collateral and repay by buying oSQTH
  3. Flashswitch: borrow eth to keep vault collateralized while creating a Uniswap LP to deposit into vault
  4. Flash-rearrange: borrow eth to keep vault collateralized while swapping eth for oSQTH in the Uniswap LP.

Use case 1: Flash mint

The squeeth contract mints oSQTH up to a 150% collateral to debt ratio. A user might deposit eth, mint oSQTH up to the 150% limit, then sell the oSQTH for eth and deposit that eth as collateral. The final collateral ratio will be more than 150%, so the user will mint again up to the 150% limit, sell the minted oSQTH and deposit the collateral. The user will need to do this an infinite number of times to get 150% overall. This is Zeno’s paradox for vaults (see figure).

A flashswap solves this problem. The idea is that we sell oSQTH that we don’t yet have in the flashwap, then deposit this eth and mint oSQTH to repay the loan.

Minting to 150% without a flashswap

Example flashmint:

A user starts with 60 eth in a vault and wants to mint 200 oSQTH. The flashswap makes this easy by borrowing the expected proceeds of selling the oSQTH (60 eth) and using this as collateral to mint a larger amount of oSQTH

Flashmint state changes

The spreadsheet compares this trade flow with and without the flashmint.

Use case 2: Flash burn

Say a user wants to close a short oSQTH position.The user might do something like

  • Withdraw eth from vault
  • Buy some oSQTH
  • Burn oSQTH in vault
  • Withdraw more eth from vault
  • Buy more oSQTH
  • etc

After a number of cycles the user can buy enough oSQTH to pay off the debt and withdraw the collateral. The lower the collateral ratio, the more cycles we will need.

Again, a flashswap improves our situation. The idea is that we can buy oSQTH by selling eth that we don’t yet have, then burn oSQTH in the vault so we can withdraw eth to repay the loan. This means the vault can be closed in a single transaction, even when the collateral ratio is close to 150%.

Example flashburn:

A user starts with a vault with 100 oSQTH debt and 60 eth collateral. The user wants to close the position and buy 50 oSQTH. The flashswap makes this easy by first borrowing 150 oSQTH, buringing 100 against the vault, and withdrawing the remaining collateral.

Flashburn state changes

The spreadsheet compares this trade flow with and without the flashburn.

Use case 3: Flash switch collateral for NFT

The squeeth contract accepts two kinds of collateral: eth, or a Uniswap v3 LP for the eth/oSQTH pool.

A user currently is short oSQTH with eth collateral and wants to switch to only LP as collateral. The user might try to withdraw some eth and make a Uniswap LP to deposit into the vault:

  • Buy back oSQTH to close short position (see use case 3)
  • Mint some oSQTH
  • Withdraw some eth
  • Deposit oSQTH and eth in Uniswap LP
  • Deposit Uniswap LP in vault
  • Mint more oSQTH and sell for eth
  • Deposit eth in vault
  • etc

But this simply won’t do. Once the LP is deposited into the vault it’s frozen in place and we can’t add any liquidity to it so there’s no way to convert eth collateral to LP collateral entirely. Unless…

Unless we have a flashloan. The flashloan flow is straightforward:

  • Flashloan enough eth to mint the full final oSQTH amount
  • Mint some oSQTH and combine with eth to make new Uniswap LP
  • Deposit LP and withdraw eth from vault to repay flashloan

The flashloan lets us reduce the eth collateral to zero and completely replace it with an LP.

Example flashswitch:

A user starts with a vault with 100 oSQTH debt and 60 eth collateral. The user wants to replace the collateral with a Uniswap LP with 60 eth and 100 oSQTH. A flashloan of 60 eth lets the user withdraw 60 eth and mint 100 oSQTH to make the LP.

Flashswitch state changes

The spreadsheet compares flows with and without a flashloan. This example is impossible without a flashloan since collateral can’t be fully withdrawn without making the vault insolvent.

User case 4: Flash rearrange a Uniswap LP (aka Uniswap LP flashdance)

From the last example we have a Uniswap LP as collateral for some short oSQTH, what now?

A user now wants to re-arrange the Uniswap LP collateral in the vault, selling all the oSQTH and replacing it with an LP that is 100% eth (say the user thinks that oSQTH is temporarily overvalued).

This is difficult without flashloans. One path we might try:

Mint more oSQTH and sell for eth

  • Deposit eth collateral until we have enough collateral to withdraw the LP
  • Liquidate LP
  • Sell oSQTH for eth
  • Make new LP with only eth
  • Deposit LP into vault

This will only work if we start with a very overcollateralized vault. Otherwise we will will find a point where we can’t mint enough to liberate the LP from the vault and the whole enterprise fails.

The flash-rearrange makes things simple:

  • Flashloan enough eth to keep collateral ratio above 150%
  • Deposit eth into vault
  • Withdraw Uniswap LP
  • Liquidate LP and sell all oSQTH for eth
  • Create new LP with all eth
  • Deposit LP into vault
  • Withdraw flashloaned eth from vault and repay

This all occurs within a single transaction, and can be made a single click operation for a user.

Example flash-rearrange:

A user starts with a vault with 100 oSQTH debt and a Uniswap LP with 100 eth and no oSQTH. The user wants to exchange the LP for one with 300 oSQTH and no eth. A flashloan of 100 eth deposited temporarily to the vault lets the user withdraw the LP, buy the 300 oSQTH on Uniswap, and deposit back to the vault.

Flash-rearrange state changes

The spreadsheet compares flows with and without a flashloan. Here again this would be impossible without a flashloan since the LP must be withdrawn first and there is otherwise no eth collateral in the vault.

Superfluid

The ControllerHelper contract makes it easy to work with squeeth collateral.

It’s easy to mint and burn squeeth to a target collateral ratio in one operation, without cycling through mints and sells.

It’s easy to use collateral to LP in the squeeth pool, so you can earn yield on your collateral while also improving the liquidity of squeeth.

It’s easy to use collateral to trade the relative value of squeeth. If you think the price of squeeth is too high you can sell oSQTH inside your vault, then buy it back when the price is cheap. And you can do this with your full collateral which is usually around 200% of the size of your short squeeth position.

All this means better pricing, lower transaction costs, and more uses for squeeth as a clearinghouse wherever eth/$ convexity, wherever it trades.

--

--