Flash Swaps
ZeniqSwap flash swaps allow you to withdraw up to the full reserves of any ZEN20 token on ZeniqSwap and execute arbitrary logic at no upfront cost, provided that by the end of the transaction you either:
- pay for the withdrawn ZEN20 tokens with the corresponding pair tokens
- return the withdrawn ZEN20 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 ZeniqSwap.
Examples
Capital Free Arbitrage
One particularly interesting use case for flash swaps is capital-free arbitrage. It's well-known that an integral part of ZeniqSwap's design is to create incentives for arbitrageurs to trade the ZeniqSwap 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 ZENIQ on ZeniqSwap is 200 BRZ (which is calculated by calling getAmountIn
with 1 ZENIQ specified as an exact output), and on Oasis (or any other trading venue), 1 ZENIQ buys 220 BRZ. To anyone with 200 BRZ available, this situation represents a risk-free profit of 20 BRZ. Unfortunately, you may not have 200 BRZ lying around. With flash swaps, however, this risk-free profit is available for anyone to take as long as they're able to pay gas fees.
Withdrawing ZENIQ from ZeniqSwap
The first step is to optimistically withdraw 1 ZENIQ from ZeniqSwap via a flash swap. This will serve as the capital that we use to execute our arbitrage. Note that in this scenario, we're assuming that:
- 1 ZENIQ is the pre-calculated profit-maximizing trade
- The price has not changed on ZeniqSwap 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 movements. This can be somewhat complex, depending on the strategy being executed. However, one common strategy is trading as profitably as possible against a fixed external price. (This price may be e.g., the average execution price of one or more orders on Oasis.)
Trade at External Venue
Once we've obtained our temporary capital of 1 ZENIQ from ZeniqSwap, we now can trade this for 220 BRZ on Oasis. Once we've received the BRZ, we need to pay ZeniqSwap back. We've mentioned that the amount required to cover 1 ZENIQ is 200 BRZ, calculated via getAmountIn
. So, after sending 200 of the BRZ back to the ZeniqSwap pair, you're left with 20 BRZ of profit!
Instant Leverage
Flash swaps can be used to improve the efficiency of levering up using lending protocols and ZeniqSwap.
Consider Maker in its simplest form: a system which accepts ZENIQ as collateral and allows BRZ to be minted against it while ensuring that the value of the ZENIQ never drops below 150% of the value of the BRZ.
Say we use this system to deposit a principal amount of 3 ZENIQ, and mint the maximum amount of BRZ. At a price of 1 ZENIQ / 200 BRZ, we receive 400 BRZ. In theory, we could lever this position up by selling the BRZ for more ZENIQ, depositing this ZENIQ, minting the maximum amount of BRZ (which would be less this time), and repeating until we've reached our desired leverage level.
It's quite simple to use ZeniqSwap as a liquidity source for the BRZ-to-ZENIQ component of this process. However, looping through protocols in this way isn't particularly elegant, and can be gas-intensive.
Luckily, flash swaps enable us to withdraw the full ZENIQ amount upfront. If we wanted 2x leverage against our 3 ZENIQ principal, we could simply request 3 ZENIQ in a flash swap and deposit 6 ZENIQ into Maker. This gives us the ability to mint 800 BRZ. 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.