This tutorial explains implementation of the famous UniswapV2 DEX in Sway. We will see that there is a couple of differences comparing to the original Solidity code that we have to redesign in order to make it fit for the Fuel VM.
Prerequisites:
- Knowledge of the Fuel VM and Sway language. You can start with my previous tutorial on SRC20.
- Familiarity with UniswapV2 code. Great resource can be found in the UniswapV2 Book.
The tutorial is split into parts:
- Part 1 - Pair - the equivalent of UniswapV2 UniswapV2Pair.sol - where the core of the LP and swapping logic resides.
- Part 2 - Pair Tests - Tests of the pair contract.
- Part 3 - Factory - this is where we define pool creations through the Factory (originally the UniswapV2Factory).
- Part 4 - Router - Here we define the periphery contract for interacting safely with different pools.
We call this implementation FuniSwapV2 because it is a Fuel version of UniswapV2.
You can download and launch the code according to the following instructions:
git clone https://github.com/jecikpo/Tutorial-Sway-UniswapV2
Then build the project:
cd Tutorial-Sway-UniswapV2
forc build
Put your secrect key into the tests/utils/setup.rs
file here:
pub const SECRECT_KEY: &str = "<YOUR_SECRECT_KEY_HERE>";
and run tests:
cargo test