Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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
€8.99 €28.99
eBook Jun 2024 436 pages 2nd Edition
eBook
€8.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
€8.99 €28.99
eBook Jun 2024 436 pages 2nd Edition
eBook
€8.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781805121688
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 28, 2024
Length: 436 pages
Edition : 2nd
Language : English
ISBN-13 : 9781805121688
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
Banner background image

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.