Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Hands-On Blockchain for Python Developers
Hands-On Blockchain for Python Developers

Hands-On Blockchain for Python Developers: Empowering Python developers in the world of blockchain and smart contracts , Second Edition

Arrow left icon
Profile Icon Arjuna Sky Kok
Arrow right icon
€18.99 per month
Paperback Jun 2024 436 pages 2nd Edition
eBook
€19.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Arjuna Sky Kok
Arrow right icon
€18.99 per month
Paperback Jun 2024 436 pages 2nd Edition
eBook
€19.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€19.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Blockchain for Python Developers

Introduction to Blockchain Programming

In this book, we’ll learn about blockchain programming so that you can become a force to be reckoned with when finding blockchain opportunities. To achieve this, you need to begin by understanding blockchain technology and what it entails. In this chapter, we will learn what blockchain technology is. We’ll delve into questions such as, how does blockchain empower Bitcoin and Ethereum? By the end of this chapter, we will get an intuitive understanding of blockchain technology, and we should be able to replicate some of the basic functions behind blockchain.

The following topics will be covered in this chapter:

  • The rise of cryptocurrency and blockchain
  • Blockchain technology
  • Cryptography
  • The hashing function
  • Consensus
  • Coding on the blockchain

Technical requirements

To follow this chapter, you need one of the more recent versions of Linux and Python – no older than 3.8. You could try implementing what will be covered in this chapter on other platforms, such as Mac or Windows, but Linux is recommended. You could also run Linux with virtualization software such as VirtualBox. We will use Ubuntu Linux 22.04 LTS and Python 3.10.

The rise of cryptocurrency and blockchain

Assuming that you haven’t lived a secluded life as a hermit on a mountain, you have probably heard all about cryptocurrency, especially Bitcoin. Thus, you needn’t have to look far to hear about the soaring popularity of this topic, its terminology, and its growth in value.

Blockchain was regarded as the technology that would bring the dawn of a new era of justice and prosperity for mankind. It would democratize wealth. It would take the power away from the oligarchy and give it back to the people. It would protect the data of the people.

Blockchain started to be used as a payment solution without the middleman, namely Bitcoin. Then, people found out that blockchain has some other interesting properties.

First, it is transparent, meaning people can audit it to check whether there is money laundering going on or not. Second, to some extent, it gives users privacy, which can be used to avoid profiling.

Then, after Ethereum...

Blockchain technology

Most people know Bitcoin exists because of blockchain.

But what is blockchain?

It is an append-only database that consists of blocks that are linked by hashing. Here, each block contains many transactions of transferring value (but could be other things) between participants secured by cryptography; a consensus between many nodes that hold an identical database decides on which new block is to be appended next.

You don’t have to understand the definition at this point; those are a lot of words to chew on! First, I’ll explain blockchain so that you can adjust to this new knowledge as we move through this book.

Going back to the definition of blockchain, we can summarize the definition as an append-only database. Once you put something into the database, it cannot be changed; there is no undo button. We’ll talk about the ramifications of this feature in Chapter 2, Smart Contract Fundamentals. This definition entails many things and...

Cryptography

The most popular use of blockchain is to create cryptocurrency. As the word crypto is in cryptocurrency, you would expect that you need to master cryptography to become a blockchain programmer. That isn’t true. You only need to know two things about cryptography:

  • Private keys and public keys (asymmetric cryptography)
  • Hashing

We explained these previously. You don’t need to know how to design a hashing algorithm or private key and public key algorithm. You only need to get an intuitive understanding of how they work and the implications of these technologies.

Private keys and public keys enable decentralized accounts. In a normal application, you have a username and password. These two fields enable someone to access their account. However, having a private key and a public key enables someone to have an account in a decentralized manner.

For hashing, it is a one-way function, meaning that given an input, you can get the output easily...

The hashing function

Hashing is a function that takes an input of any length and turns it into a fixed-length output. To make this clearer, we can look at the following code example:

>>> import hashlib
>>> hashlib.sha256(b"hello").hexdigest()
'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
>>> hashlib.sha256(b"a").hexdigest()
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'
>>> hashlib.sha256(b"hellohellohellohello").hexdigest()
'25b0b104a66b6a2ad14f899d190b043e45442d29a3c4ce71da2547e37adc68a9'

As you can see, the length of the input can be 1, 5, or even 20 characters, but the output will always be the length of 64 hexadecimal numeric characters. The output looks scrambled, and there is no apparent link between the input and the output. However, if you give the same input, it will give the same output every time:

>>> hashlib.sha256(b...

Consensus

As we can see, the hashing function makes history tampering hard, but not too hard. Even if we have a blockchain that consists of 1000 blocks, it would be trivial to alter the content of the first block and change the 999 parent hashes on the other blocks with recent computers. So, to ensure that bad people cannot alter history (or at least make it hard), we must distribute this append-only database to everyone who wants to keep it (let’s call them miners). Say there are 10 miners. In this case, you cannot just alter the blockchain in your copy because the other nine miners would scold you, saying something like Hey, our records say history A but your record says B. In this case, the majority wins.

However, consensus is not just a case of choosing which blockchain has been chosen by the majority. The problem starts when we want to add a new block to the blockchain.

Where do we start? How do we do it?

The answer is that we broadcast. When we broadcast the candidate...

Coding on the blockchain

At the time of writing, the two most popular cryptocurrencies are Bitcoin and Ethereum.

Note

If you ask someone who knows a lot about cryptocurrencies a simple question, you may get the following answer: Bitcoin is just for sending money, but you can create a program on Ethereum. The program can be tokens, auctions, or escrow, among many other things. But that is a half-truth.

You can also create a program via Bitcoin. Usually, people call this program a script. It is necessary to provide a script in a Bitcoin transaction. A transaction in Bitcoin can be mundane, so if I want to send you 1 BTC (a unit of currency in Bitcoin) and your Bitcoin address is Z, I need to upload a script like this into the Bitcoin blockchain:

What's your public key? If the public key is hashed, does it equal Z? If
yes, could you provide your private key to prove that you own this public
key?

But it could be a little bit fancier. Let’s say you require at least...

Summary

In this chapter, we investigated the various technologies behind cryptocurrencies, such as Bitcoin and Ethereum. This technology allows us to decentralize how we store values or code. We also covered cryptography by using private and public keys to secure the integrity of data. Later, we learned about hash functions, proof of work, consensus, and the basic concepts of blockchain programming. The blockchain will be an important technology for keeping the truth. So, now is a good time to learn about this technology.

In the next chapter, we will learn about smart contracts, which are programs that live in Ethereum. A smart contract is different than a kind of program that lives on a server, such as an application written with Ruby on Rails, Laravel, or Django. The differences are more than just the syntax; the concept is radically different than a normal web application.

References

To learn more about the topics that were covered in this chapter, take a look at the following resources:

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Use the world's easiest programming language to build web3 applications
  • Write common smart contracts like decentralized exchanges, NFT marketplaces, and lending applications
  • Unlock deeper levels of insights with technologies relating to blockchain, such as IPFS and Layer 2
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

We are living in the age of decentralized fi nance and NFTs. People swap tokens on Uniswap, borrow assets from Aave, send payments with stablecoins, trade art NFTs on OpenSea, and more. To build applications of this kind, you need to know how to write smart contracts. This comprehensive guide will help you explore all the features of Vyper, a programming language designed to write smart contracts. You’ll also explore the web3.py library. As you progress, you’ll learn how to connect to smart contracts, read values, and create transactions. To make sure your foundational knowledge is strong enough, the book guides you through Ape Framework, which can help you create decentralized exchanges, NFT marketplaces, voting applications, and more. Each project provides invaluable insights and hands-on experience, equipping you with the skills you need to build real-world blockchain solutions. By the end of this book, you’ll be well versed with writing common Web3 applications such as a decentralized exchange, an NFT marketplace, a voting application, and more.

Who is this book for?

This blockchain book is for developers interested in understanding blockchain and smart contracts. It is suitable for both technology enthusiasts looking to explore blockchain technology and programmers who aspire to become smart contract engineers. Basic knowledge of GNU/Linux and Python programming is mandatory to get started with this book.

What you will learn

  • Understand blockchain and smart contracts
  • Learn how to write smart contracts with Vyper
  • Explore how to use the web3.py library and Ape Framework
  • Discover related technologies such as Layer 2 and IPFS
  • Gain a step-by-step guide to writing an automated market maker (AMM) decentralized exchange (DEX) smart contract
  • Build innovative, interactive, and token-gated Web3 NFT applications

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 28, 2024
Length: 436 pages
Edition : 2nd
Language : English
ISBN-13 : 9781805121367
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 28, 2024
Length: 436 pages
Edition : 2nd
Language : English
ISBN-13 : 9781805121367
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 100.97
React and React Native
€32.99
System Programming Essentials with Go
€31.99
Hands-On Blockchain for Python Developers
€35.99
Total 100.97 Stars icon

Table of Contents

26 Chapters
Part 1:Blockchain and Smart Contract Chevron down icon Chevron up icon
Chapter 1: Introduction to Blockchain Programming Chevron down icon Chevron up icon
Chapter 2: Smart Contract Fundamentals Chevron down icon Chevron up icon
Chapter 3: Using Vyper to Implement a Smart Contract Chevron down icon Chevron up icon
Part 2: Web3 and Ape Framework Chevron down icon Chevron up icon
Chapter 4: Using Web3.py to Interact with Smart Contracts Chevron down icon Chevron up icon
Chapter 5: Ape Framework Chevron down icon Chevron up icon
Chapter 6: Building a Practical Decentralized Application Chevron down icon Chevron up icon
Part 3: Graphical User Interface Applications Chevron down icon Chevron up icon
Chapter 7: Front-End Decentralized Application Chevron down icon Chevron up icon
Chapter 8: Cryptocurrency Wallet Chevron down icon Chevron up icon
Part 4: Related Technologies Chevron down icon Chevron up icon
Chapter 9: InterPlanetary: A Brave New File System Chevron down icon Chevron up icon
Chapter 10: Implementing a Decentralized Application Using IPFS Chevron down icon Chevron up icon
Chapter 11: Exploring Layer 2 Chevron down icon Chevron up icon
Part 5: Cryptocurrency and NFT Chevron down icon Chevron up icon
Chapter 12: Creating Tokens on Ethereum Chevron down icon Chevron up icon
Chapter 13: How to Create an NFT Chevron down icon Chevron up icon
Part 6: Writing Complex Smart Contracts Chevron down icon Chevron up icon
Chapter 14: Writing NFT Marketplace Smart Contracts Chevron down icon Chevron up icon
Chapter 15: Writing a Lending Vault Smart Contract Chevron down icon Chevron up icon
Chapter 16: Decentralized Exchange Chevron down icon Chevron up icon
Part 7: Building a Full-Stack Web3 Application Chevron down icon Chevron up icon
Chapter 17: Token-Gated Applications Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.