logo
logo
Sign in

Demystifying Smart Contract Development Using Python

avatar
arslan siddiqui
Demystifying Smart Contract Development Using Python

The rise of blockchain tech has totally changed scenarios of how we handle digital assets and trade value. It's like this whole new way of doing things where everything is decentralized, meaning it's all about trust, transparency, and top-notch security. At the heart of this transformative technology lies the concept of smart contracts. These are self-executing programs on a blockchain that can make agreements happen without any middlemen getting in the way. In this article, we give you a step-by-step guide to smart contract development using Python and Brownie.

 

Smart Contracts

 

Smart contracts automatically execute and enforce the terms of the contract written in the form of code when predefined conditions are met. Since they operate on a blockchain, they benefit from the immutability and decentralization of the underlying technology. This suggests that because a smart contract's code and execution outcomes are recorded on the blockchain, they cannot be altered once they are put into action.

 

Smart Contract Development

 

When starting smart contract development, developers have two popular options: Python and JavaScript. Both languages offer unique advantages, but let's focus on Python for now. 

 

Python, known for its versatility and widespread adoption, has become a compelling choice for smart contract development. Its clear and concise syntax, coupled with a rich set of libraries and an active community, makes it an ideal tool for both beginners and seasoned developers entering the world of blockchain technology. As we delve into smart contract development, Python will be our guide, providing an accessible and powerful entry point into the decentralized landscape.

 

For developing and testing smart contracts on Python, we have selected Brownie, a popular Python-based framework specifically designed for this purpose. Its ease of use and popularity among Python developers make it an ideal choice for streamlining smart contract development.

 

Prerequisites

 

  • python3 version 3.6 or greater
  • ganache-cli - version 6.12.2 or above

 

Steps Involved in Smart Contract Development using Python

 

Installation

 

To install Brownie the best way is to use pipx, which is similar to "JavaScript's npx" for Python.

 

pipx install eth-brownie

 

Once installation is complete, type "brownie" in the console to verify that it worked

 

Setting up a Project

 

To start using Brownie first you need to set up a project directory and Brownie's CLI tool provides this functionality via the following command:

 

$ brownie init

 

For now, we will use one of Brownie's features named Brownie mixes. Brownie mixes are ready-made templates that you can use as a starting point for your project or as a tutorial. You can find mixes here https://github.com/brownie-mix/

 

$ brownie bake token

 

After running this command a directory named token will be created. You can open it using any editor of your choice. There you will see the following folder structure:

 

 

The folders for the current scope of learning are the following:

 

  • contracts/: All smart contracts are created here
  • interfaces/: IAll smart contracts interfaces are created here
  • scripts/: Scripts for deployment and interaction with smart contracts
  • tests/: Scripts for testing the smart contracts developed

 

The following directories are used internally by Brownie to manage the project:

 

  • build/: Project data such as compiler artifacts and unit test results
  • reports/: JSON report files for use in the GUI

 

To Compile your Project:

 

$ brownie compile

 

You will see the following output:

 

Brownie - Python development framework for Ethereum


Compiling contracts...

Optimizer: Enabled  Runs: 200

- Token.sol...

- SafeMath.sol...

Brownie project has been compiled at token/build/contracts

 

We can change the compiler version and other settings by editing the config file i.e. brownie.config.yaml

 

Smart Contract Deployment using Scripts 


Once a project is ready to be deployed, Brownie can be used to handle the deployments.

 

Prior to comprehending the deployment procedure, we must grasp Brownie's wallet management system. The Accounts container (available as accounts or just a) allows you to access all your local accounts or wallets. Each individual account is represented by an Account object that can perform actions such as querying a balance or sending ETH.

 

To add a new account via private key:

 

$ brownie accounts new

 

Here is the name of the wallet that is going to be used. After running this command you will be asked to input the private key and password to access the account later. Then, the account will be available as .

 

Creating a Deployment Script

 

  • First of all, we need to create an Account object by unlocking the account using Accounts.load() function and passing the
  • Then we can write the code to deploy smart contracts using the Account loaded.

 

from brownie import Token, accounts


def main():

    acct = accounts.load('deployment_account')

    Token.deploy("My Real Token", "RLT", 18, 1e28, {'from': acct})


 

Running the deployment script

 

$ brownie run deploy.py --network

 

In order to execute your script in a live environment, you must include the --network flag in the command line. You can use network ids defined in brownie-confing.yaml

 

Please be aware that the script will take some time to finish and that transactions on live blockchains are not immediately confirmed.

 

Verifying Deployment Source Code

 

Brownie allows automatic source code verification for solidity contracts on all networks supported by etherscan. To verify a contract while deploying it, add the publish_source=True argument in the deploy function 

 

Token.deploy("My Real Token", "RLT", 18, 1e28, {'from': acct}, publish_source=True)

 

Verifying already deployed contracts is also supported. It works only if the compiler settings are identical to the deployed contracts

 

token = Token.at("0x114A107C1931de1d5023594B14fc19d077FC4dfD")

Token.publish_source(token)

 


 

Summing Up

 

This introductory guide has equipped you with the basic knowledge and tools to begin your smart contract development journey using Python and Brownie. Remember, this guide is just the beginning; it's like having a map to start your adventure. You will need to keep researching and learning if you want to become an authority on smart contracts. For your next step, you can checkout the official Brownie documentation.

 

Looking for smart contract development services? Connect with our smart contract developers to get started. 



collect
0
avatar
arslan siddiqui
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more