Flash Swaps

Casperswap flash swaps allow you to withdraw up to the full reserves of any CEP-18 token on Casperswap and execute arbitrary logic at no upfront cost, provided that by the end of the transaction you either:

  • pay for the withdrawn CEP-18 tokens with the corresponding pair tokens

  • return the withdrawn CEP-18 tokens along with a small fee

Flash swaps are incredibly useful because they obviate upfront capital requirements and unnecessary order-of-operations constraints for multi-step transactions involving CasperSwap.

Examples

Capital Free Arbitrage

One particularly interesting use case for flash swaps is capital-free arbitrage. It is well-known that an integral part of CasperSwap's design is the creation of incentives for arbitrageurs to trade the Casperswap price to a "fair" market price.

While game-theoretically sound, this strategy is accessible only to those with sufficient capital to take advantage of arbitrage opportunities. Flash swaps remove this barrier entirely, effectively democratizing arbitrage.

Imagine a scenario where the cost of buying 1 ETH on Casperswap is 200 DAI (which is calculated by calling getAmountIn with 1 ETH specified as an exact output), and on Oasis (or any other trading venue), 1 ETH buys 220 DAI. To anyone with 200 DAI available, this situation represents a risk-free profit of 20 DAI less fees. Unfortunately, you may not have 200 DAI lying around. With flash swaps, however, this risk-free profit is available for anyone to take as long as they are able to pay gas fees.

Withdrawing ETH from CasperSwap

The first step is to optimistically withdraw 1 ETH from CasperSwap via a flash swap. This will serve as the capital used to execute our arbitrage. Note that this scenario, assumes that:

  • 1 ETH is the pre-calculated profit-maximizing trade

  • The price has not changed on CasperSwap or Oasis since our calculation. It may be the case that we'd like to calculate the profit-maximizing trade on-chain at the moment of execution, which is robust to price fluctuation. This can be somewhat complex, depending on the strategy being executed. One common strategy, however, is trading as profitably as possible against a fixed external price (This price, for example, may be the average execution price of one or more orders on Oasis).

Trade at External Venue

Once we have obtained our temporary capital of 1 ETH from CasperSwap, we are able to trade this for 220 DAI on Oasis. Once we've received the DAI, we have to pay CasperSwap back.

As mentioned, the amount required to cover 1 ETH is 200 DAI, calculated via getAmountIn, which means that after sending 200 of the DAI back to the CasperSwap pair, you are left with 20 DAI of profit less fees!

Instant Leverage

Flash swaps can be used to improve the efficiency of levering up when using lending protocols and CasperSwap.

Consider Maker in its simplest form: a system that accepts ETH as collateral and allows DAI to be minted against it, while ensuring that the value of the ETH never drops below 150% the DAI value.

Say we use this system to deposit an initial amount of 3 ETH, and mint the maximum amount of DAI. At a price of 1 ETH / 200 DAI, we receive 400 DAI.

In theory, we could lever this position up by selling the resulting DAI for more ETH, depositing this ETH, then minting the maximum amount of DAI (which would be less this time), and repeating until we've reached our desired leverage level.

It is quite simple to use CasperSwap as a liquidity source for the DAI-to-ETH component of this process. However, looping through protocols in this way is not particularly elegant, and can be gas-intensive.

Luckily, flash swaps enable us to withdraw the full ETH amount upfront. If we wanted 2x leverage against our 3 ETH principal, we could simply request 3 ETH in a flash swap and deposit 6 ETH into Maker. This gives us the ability to mint 800 DAI.

If we mint as much as we need to cover our flash swap (say 605), the remainder serves as a safety margin against price movements.

Last updated