Skip to main content

Flash Swaps

Sigmaswap's flash swaps allow you to withdraw up to the full reserves of any HTS token on the protocol and execute arbitrary logic at no upfront cost, provided that by the end of the transaction you either:

  • pay for the withdrawn HTS tokens with the corresponding pair tokens
  • return the withdrawn HTS 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 Sigmaswap.

note

Keep in mind that on Hedera, a user who performs a flash swap must associate both input and output tokens before executing the swap.

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 Sigmaswap's design is to create incentives for arbitrageurs to trade the Sigmaswap 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 1000 HBAR on Sigmaswap is 200 USDC (which is calculated by calling getAmountIn with 1000 HBAR specified as an exact output), and on another DEX (or any other trading venue on Hedera), 1000 HBAR is being traded for 220 USDC. To anyone with 200 USDC available, this situation represents a risk-free profit of 20 USDC. Unfortunately, you may not have 200 USDC 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 HBAR from Sigmaswap

The first step is to optimistically withdraw 1000 HBAR from Sigmaswap 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:

  • 1000 HBAR is the pre-calculated profit-maximizing trade
  • The price has not changed on Sigmaswap or another DEX since our calculation

It may be the case that we would like to calculate the profit-maximizing trade on-chain at the moment of execution, which is robust to price movements. This could be complex and difficult, depending on the strategy being executed. However, one common strategy is to make as much profit as possible from trading against a fixed external price. (This price may be e.g., the average execution price of one or more orders on another DEX.) If the Sigmaswap market price is far enough above or below this external price, abitraging could be an opportunity to produce maximum profit in trading at the time.

Trade at External Venue

Once the temporary capital of 1000 HBAR from Sigmaswap is obtained. We now can trade this for 220 USDC on another DEX. Once the USDC is received, we need to return (pay) the USDC back to Sigmaswap. As mentioned, the amount required to cover 1000 HBAR is 200 USDC, calculated via getAmountIn. Therefore, after returning 200 of the USDC back to the Sigmaswap pair, you are left with 20 USDC of profit!

Instant Leverage

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

Consider a lending protocol in its simplest form: a system which accepts HBAR as collateral and allows USDC to be minted against it while ensuring that the value of the HBAR never drops below 150% of the value of the USDC.

Say we use this system to deposit a principal amount of 3000 HBAR, and mint the maximum amount of USDC. At a price of 1000 HBAR / 200 USDC, we receive 400 USDC. In theory, we could lever this position up by selling the USDC for more HBAR, depositing this HBAR, minting the maximum amount of USDC (which would be less this time), and repeating until we've reached our desired leverage level.

It is quite simple to use Sigmaswap as a liquidity source for the USDC-to-HBAR component of this process. Even though gas fees on Hedera cost less than a penny, looping through protocols in this way is particularly unwise, and can lead to an issue in a volatile market condition.

Fortunately, flash swaps enable us to withdraw the full HBAR amount upfront. If we wanted 2x leverage against our 3000 HBAR principal, we could simply request 3000 HBAR in a flash swap and deposit 6000 HBAR into a lending protocol. This creates the ability to mint 800 USDC. If we mint as much as we need to cover our flash swap (e.g. 605), the remainder serves as a safety margin against price movements.