Posted By : Suchit
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 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
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
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
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:
The following directories are used internally by Brownie to manage the project:
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
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>.
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.
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
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.
November 21, 2024 at 12:59 pm
Your comment is awaiting moderation.