Uniswap’s trading volume skyrocketed 🚀 in 2020. Starting from $60 million in Jan 2020, it reached almost $10 billion in August 2020. In addition, Uniswap surpassed Coinbase’s daily trading volume.

What is Uniswap?

Uniswap is an automated liquidity protocol to enable decentralized token swaps on the Ethereum blockchain. These token swaps are powered by liquidity pools/pairs.

Anyone can create an ERC20 token pair (ex. DAI/wETH) on Uniswap for pairs that do not already exist. Once created, it needs to be seeded with initial token deposits for both tokens. This initial deposit sets the initial price of the pair on the Uniswap.

However, at any time, if the price of any token does not reflect the outside market price, arbitrageurs will profit from it by executing arbitrage trades and balancing the token price.

Anyone can add liquidity to Uniswap pools/pairs. Every trade on Uniswap has a 0.3% fee attached to it and this fee goes to the liquidity providers.

Liquidity pools or pairs are essentially ERC20 tokens itself and whenever you add liquidity to pools, new pool shares get minted on a pro-rata basis. These pool shares/LP shares can be redeemed for the underlying token assets at the time of withdrawing.

To learn more about Uniswap inner workings, check out the Uniswap white paper.

Flash Swaps

Uniswap Flash Swaps are similar to Aave Flash Loans, which allow you to withdraw all the liquidity of any ERC20 token from a Uniswap without any cost, given at the end of the transaction you either:

  • pay for the withdrawn ERC20 tokens with the corresponding pool/pair tokens
  • return the withdrawn ERC20 tokens

If you were unable to meet either of the conditions mentioned above, the flash swap transaction will fail, and any other arbitrary execution involved in that transaction will be rolled back.

This is possible because flash swaps are atomic Ethereum transactions. In addition, you need to pay a small protocol fee to make flash swaps successful.

For example, you can withdraw all the wETH or DAI from a wETH/DAI pool on Uniswap.

Let’s say the current wETH/DAI price is 1 wETH = 150DAI on the Unsiwap. Now you withdraw 150 DAI from that wETH/DAI pool.

Now, to make this transaction successful, you need to either return 1 wETH or 150 DAI and a fee.

Flash Swaps Usecases

Between withdrawing a token from Uniswap and returning it back, you can execute any arbitrary logic. In other words, you can transfer the withdrawn tokens to other protocols, for example, mint DAI on MarkerDAO or take a loan on Aave or margin trade on dYdX, before returning the amount to Unsiwap protocol.

This is a very powerful feature because now you don’t need any capital (other than the GAS fee) to benefit from the financial opportunities of any size. There are many use cases of flash swaps.

For example:

Arbitrage

Uniswap incentivizes arbitrageurs to balance the on-chain asset price with the outside market. However, you need capital to execute these arbitrage trades. Uniswap flash swaps democratize arbitrage by removing the capital requirement.

Let’s say ETH's price is $120 on Kyber protocol, and $100 on Uniswap. Now you can execute a flash swap in which you can withdraw ETH on Uniswap and sell it to Kyber and return the original amount to Uniswap and profit from $20 price difference without investing any capital.

Debt Refinance

Let’s say you have taken debt from Aave protocol at a 10% interest rate. However, another protocol only charges a 5% interest rate. In this case, you can now refinance your debt from Aave to another protocol without paying the original debt. Here are the steps.

  • Withdraw the asset from the Unsiwap
  • Pay your debt on the Aave protocol
  • Borrow on 2nd protocol at 5% protocol
  • Return asset on Uniswap

How does Flash Swap work?

Under the hood, all the swaps on Uniswap are flash swaps. Unlike trades on Uniswap (where you have to send tokens upfront before triggering the swap function), in flash swaps "pair contracts" send output tokens to the recipient before enforcing that enough input tokens have been received. "pair token" then invokes a callback method on the to address to make sure input tokens are delivered. If not, the whole transaction gets rolled back.

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data);

Above is the example of a swap function on Uniswap. Here you can see two tokens which are getting exchanged, and the address which will receive the exchanged tokens. In addition, you can see an additional parameter “data”.

The length of this parameter actually determines if the swap call is a normal swap call or a flash swap call.

Uniswap SDK

Uniswap provides a NodeJs SDK for developers to build on top of Uniswap protocol. You can execute trades, fetch asset pricing, and pools/pairs related data from using this SDK.

Currently, it doesn’t have support for flash swaps, however, you can see a flash swap example here.

Conclusion

To execute a flash swap you need to deploy a smart contract, hence you need some sort of technical ability. Besides, you always need to pay the Gas fee from your pocket even if the transaction fails.

In addition, you need to understand the inner workings of the Uniswap protocol to execute a Uniswap flash swap successfully.

Democratizing access to capital is the innovation that only exists on the blockchain and will be beneficial for many in the future.

To dive deeper into flash swaps, read this ... and for technical details read this.

Leverage QuikNode to execute flash swaps

There is always a chance a miner or any other third party can front-run the opportunity you are trying to benefit from. Using a dedicated Ethereum node can help suppress front-run risk, ensure your transaction gets broadcasted and recognized by the network.

QuikNode provides the fastest Ethereum nodes in the market, so if you are trying to execute flash swaps, check it out!


Need help with your project or have questions? Contact us (hi@quiknode.io), on Twitter @QuikNode or ping us on DISCORD!

About QuikNode

QuikNode.io is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum node access. Subscribe to our newsletter for more content like this and stay in the loop on what’s happening in Web3!! 😃