Smart Contract Development Using Python

Posted By : Suchit

Nov 27, 2023

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 means that once a smart contract is deployed, its code and the outcomes of its execution are recorded on the blockchain and cannot be altered by any party.

 

Suggested Read | A Definitive Guide to Smart Contract Development Tools

 

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.

 

Also, Discover | The Increasing Inevitability of Hybrid 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.

 

Before understanding the deployment process we need to understand how wallets are managed in Brownie. 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 <id>

 

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

 

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 <id>
  • 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 <network id>

 

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 note that transactions are not confirmed immediately on live blockchains the script will take some time to complete.

 

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)

 

Check It Out | Smart Contract Development with Hyperledger Fabric Blockchain

 

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. To truly become a smart contract expert, you'll need to continue exploring and learning. 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. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 18, 2024 at 02:53 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text