Reader small image

You're reading from  Hands-On Blockchain for Python Developers

Product typeBook
Published inFeb 2019
Reading LevelExpert
PublisherPackt
ISBN-139781788627856
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Arjuna Sky Kok
Arjuna Sky Kok
author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok

Right arrow

Interacting with Smart Contracts Using Web3

In this chapter, you're going to learn how to connect to a smart contract programmatically. Here, you'll use the Python programming language to execute a method in a smart contract. To achieve that, you will use the web3.py library. In the previous chapter, you built a smart contract and deployed it to the Ethereum blockchain. You also used Vyper to write a smart contract. To interact with that smart contract, you fired up the Truffle console and typed in a number of commands. Those commands are sent to the smart contract in the blockchain. Depending on what command you type in, this could either read the state of the smart contract or change it. In this chapter, you are going to move beyond the Truffle console.

This chapter will cover the following topics:

  • Introduction to decentralized applications
  • Geth
  • Understanding the...

Introduction to decentralized applications

You'll build a program using Python to execute methods in a smart contract programmatically, and we call this program a decentralized application. So, there's a smart contract, and there's a decentralized application. A smart contract written with the Vyper or Solidity programming languages lives in an Ethereum blockchain. This means that if you deploy your smart contract to the Ethereum production blockchain, the bytecode of your smart contract is written in every Ethereum node. So, if we have 10,000 Ethereum nodes in this world, your smart contract is duplicated 10,000 times.

However, a decentralized application doesn't live in Ethereum blockchain. It lives in your computer, in your neighbor's computer, in a cloud, but it does not live on the blockchain, and it does not have to be duplicated all over the world...

Geth

Go Ethereum (Geth) is an implementation of the Ethereum protocol written in Go. You can use Geth to sync an Ethereum node, or even build a private Ethereum blockchain. If you want to be a miner, this is a software that you would use. Your Ethereum node is a gateway and a part of Ethereum blockchain. Your program with the web3 library requires the Ethereum node to be able to interact with a smart contract that lives inside the blockchain.

Using Ganache is all fine and dandy. But Ganache is a fake blockchain. There are no miners, so it's hard to simulate some situations that we would encounter on real Ethereum blockchain. As a result, let's step up our game. We don't need to use Ethereum production blockchain now, but we can use something in between development and production blockchain—the Rinkeby network. If the Ethereum production blockchain is akin...

Understanding the web3.py library

Now, let's write a decentralized application with this library. The simplest decentralized application script would be sending money from one account to another. Name the script send_money_ganache.py:

from web3 import Web3, HTTPProvider


w3 = Web3(HTTPProvider('http://localhost:7545'))

private_key = '59e31694256f71b8d181f47fc67914798c4b96990e835fc1407bf4673ead30e2'

transaction = {
'to': Web3.toChecksumAddress('0x9049386D4d5808e0Cd9e294F2aA3d70F01Fbf0C5'),
'value': w3.toWei('1', 'ether'),
'gas': 100000,
'gasPrice': w3.toWei('1', 'gwei'),
'nonce': 0
}

signed = w3.eth.account.signTransaction(transaction, private_key)
tx = w3.eth.sendRawTransaction(signed.rawTransaction)

Before you execute this script, launch Ganache first. After...

Interacting with smart contracts using web3.py

You've sent ethers using a Python script with the web3 library in Ganache and the Rinkeby network. Now, let's create a script to interact with a smart contract. But before doing that, you need to learn how to launch a smart contract with geth and a Python script with the web3 library. Previously, in Chapter 3, Implementing Smart Contracts Using Vyper, you launched a smart contract using Truffle.

Launching a smart contract with Geth

In the next section, we are going to connect to a smart contract with web3. Here's how to deploy a smart contract to the Rinkeby blockchain:

$ ./geth --rinkeby --verbosity 0 console

In the geth console, list all of your accounts with...

Summary

In this chapter, you learned how to install a web3 library. This is the library that is designed to connect to a smart contract. On top of that, you learned how to run an Ethereum node on the Rinkeby network. You configured web3 to connect to the Ethereum blockchain on the Rinkeby network. You also learned how to tell web3 to connect to an Ethereum testing network, such as Ganache. In addition, you created a script to send ethers from one account to another. Finally, you created a script to execute methods on a smart contract, either to read the value of public variables or to change the state of the smart contract.

In the next chapter, you are going to use a smart contract development framework called Populus that takes care of the manual work you've undertaken in relation to a smart contract, such as compiling the code and deploying it. In addition, the Populus...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019Publisher: PacktISBN-13: 9781788627856
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok