Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Blockchain for Python Developers

You're reading from   Hands-On Blockchain for Python Developers Empowering Python developers in the world of blockchain and smart contracts

Arrow left icon
Product type Paperback
Published in Jun 2024
Publisher Packt
ISBN-13 9781805121367
Length 436 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Arjuna Sky Kok Arjuna Sky Kok
Author Profile Icon Arjuna Sky Kok
Arjuna Sky Kok
Arrow right icon
View More author details
Toc

Table of Contents (27) Chapters Close

Preface 1. Part 1:Blockchain and Smart Contract FREE CHAPTER
2. Chapter 1: Introduction to Blockchain Programming 3. Chapter 2: Smart Contract Fundamentals 4. Chapter 3: Using Vyper to Implement a Smart Contract 5. Part 2: Web3 and Ape Framework
6. Chapter 4: Using Web3.py to Interact with Smart Contracts 7. Chapter 5: Ape Framework 8. Chapter 6: Building a Practical Decentralized Application 9. Part 3: Graphical User Interface Applications
10. Chapter 7: Front-End Decentralized Application 11. Chapter 8: Cryptocurrency Wallet 12. Part 4: Related Technologies
13. Chapter 9: InterPlanetary: A Brave New File System 14. Chapter 10: Implementing a Decentralized Application Using IPFS 15. Chapter 11: Exploring Layer 2 16. Part 5: Cryptocurrency and NFT
17. Chapter 12: Creating Tokens on Ethereum 18. Chapter 13: How to Create an NFT 19. Part 6: Writing Complex Smart Contracts
20. Chapter 14: Writing NFT Marketplace Smart Contracts 21. Chapter 15: Writing a Lending Vault Smart Contract 22. Chapter 16: Decentralized Exchange 23. Part 7: Building a Full-Stack Web3 Application
24. Chapter 17: Token-Gated Applications 25. Index 26. Other Books You May Enjoy

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime