Writing a DEX smart contract
Let’s start by creating an Ape project. You should follow these steps:
$ mkdir dex $ cd dex $ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ pip install eth-ape'[recommended-plugins]'==0.7.23 (.venv) $ ape init Please enter project name: DEX SUCCESS: DEX is written in ape-config.yaml
The full code of the DEX smart contract can be found at this URL: https://github.com/PacktPublishing/Hands-On-Blockchain-for-Python-Developers--2nd-Edition/blob/main/chapter_16/dex/contracts/DEX.vy. You can download it and place the smart contract file inside the contracts
folder.
But let’s create a smart contract from scratch. Create a new file named DEX.vy
inside the contracts
folder, then add the following code:
#pragma version ^0.3.0 # Based on https://jeiwan.net/posts/programming-defi-uniswap-1/ from vyper.interfaces import ERC20 as IERC20
The pragma
line constrains the version of the Vyper compiler that can compile this...