What is Flash Swap
Simply put, you are allowed to pay on credit on DODO! When you buy tokens DODO, you can first get the tokens you want to buy, do anything you want with the tokens, and pay for them later.
How Does Flash Swap Work
The figure above illustrates the four steps in a flash swap happening under the hood
- Call the
buyBaseTokenfunction from the
DODO Pairsmart contract
DODO Pairtransfers the base tokens to the message sender
- If the parameter
buyBaseTokenfunction call is not null, the
DODO Pairsmart contract will call the
dodoCallmethod of the message sender
- After the
dodoCallis executed, the
DODO Pairsmart contract will retrieve the quote tokens required for this transaction from the message sender
sellBaseToken function can also perform flash swap in the same way.
Flash swap requires the message sender to be a contract that implements the
What Can Flash Swap Do
Flash swap can significantly improve market efficiency. Market parity is maintained by arbitrageurs, and flash swap completely removes capital requirements for them, essentially eliminating the barrier of entry to arbitrage trading.
We will demonstrate a completely trustless and risk-free arbitrage trading contract as a use case of flash swap. Please refer to the
UniswapArbitrageur.sol source code for a concrete example. It has already been deployed and you can check out its Etherscan link here
The following figure illustrates how an arbitrageur might take advantage of the price discrepancies between DODO and Uniswap.
A complete arbitrage trading maneuver consists of the following 9 steps:
- The user calls
DODO Pairand triggers flash swap
DODO Pairtransfers 1 WETH to
UniswapArbitrageurtransfers 1 WETH received from
UniswapV2transfers 200 USDC to
transferFromand retrieves 150 USDC from
UniswapArbitrageurtransfers the remaining 50 USDC to the user
- Steps 2, 3, 4, and 8 take care of the DODO front
- Steps 5, 6, and 7 take care of the Uniswap front
- The user is only exposed to the process of sending transactions and making profits, with everything else abstracted away!
The best part about the
UniswapArbitrageur contract is that users do not need any capital, nor do they need to know how DODO and Uniswap work. They would simply call a function and, if the execution succeeds, make a profit. If the execution fails, the users would only lose some gas.
In order to avoid unnecessary gas consumption, we recommend that users use
eth_call to execute
executeSellArbitrage in advance to estimate arbitrage returns. If there is an arbitrage opportunity, these two functions will return profit of quote tokens and base tokens after successful execution.
Some Thoughts on Flash Swap
Once you have a deep understanding of flash swap, you will realize the superiority of the DeFi world over the centralized world. The composability of smart contracts has elevated the fund utilization of DeFi to an unprecedented level. Thanks to trustlessness, the cost of credit in DeFi is incredibly low. Once this financial system is integrated into the real world, its potential for improving our society and productivity will be truly boundless. The DODO team hopes that flash swap serves as a primer for DeFi builders and beginners alike to gain an appreciation for the power of DeFi.