How to deploy a smart contract with Brownie?

Smart contract development is one of the top priorities in the blockchain and web3 market right now. Most of the libraries in the domain of smart contract development include libraries based on JavaScript. The Brownie smart contract development framework presents a completely new framework for developing and testing smart contracts. It serves as a top competitor to JavaScript libraries such as Hardhat, web3.js, Truffle and ethers.js. 

On top of it, Brownie also features a unique distinction in the fact that it has been developed with Python programming language. The Brownie Python relationship is evident in the foundations of the framework, which have been developed over the web3.py Python library. Do you want to learn how to create and deploy smart contracts with the help of Brownie? The following post offers a detailed guide on deploying your smart contract with Brownie.

Curious to understand the complete smart contract development? Enroll Now in Smart Contracts Development Course!

What is Brownie?

Python has proved as one of the most popular programming languages with versatile features. Researchers have been using the programming language for running test models, while developers utilize the language for heavy production environments. Brownie is a smart contract development framework developed on top of Python. The answers to “What is Brownie smart contract?” might have you confused about the confectionery items everyone loves. However, Brownie can serve some sweet results in helping you with smart contract development and testing. 

The Brownie framework also supports Vyper and Solidity contracts. Furthermore, it also addresses contract testing requirements through pytest. On top of it, the contract testing through pytest also involves trace-based coverage evaluation. Developers can use hypotheses in Brownie for property-based and stateful testing. 

Furthermore, Brownie also empowers developers with debugging tools such as custom error strings and python-style tracebacks. In addition, the in-build console with Brownie supports faster project interactions. Brownie also provides compatibility with ethPM packages, which can easier and secure management of smart contract systems.

Important Highlights of Brownie

The importance of Brownie for smart contract development is another important factor you must consider before using the framework. You can find out the value of the Brownie Web3 connection in the important features of Brownie. For example, the in-built console for project interactions makes it different from other frameworks. Brownie supports smart contracts coded in Solidity as well as Vyper, thereby helping it capture a major share of the smart contract development market. 

Another important reason to deploy Brownie smart contract refers to the advantages of Python, which carry forward to Brownie. On top of it, Brownie also provides better flexibility for deploying local blockchains. Most important of all, Brownie can provide immediate notifications about failed tests to users. 

Certified web3 professional program

Basics of Using Brownie

Before you find out the ways to use Brownie, you need to understand the fundamental requirements. The answers to “How do you deploy a Brownie smart contract?” assume that you have prior knowledge of smart contract development and Python programming language. Here is an overview of the basic steps in using Brownie for creating and deploying smart contracts. 

The foremost step in using Brownie refers to the initialization of a new project. You can create a new project in Brownie by creating an empty folder and typing the command, 

$ brownie init

The best thing about figuring out answers to “What is Brownie smart contract?” is the opportunity to use Brownie mixes. The Brownie mixes are simple smart contract templates that can help you in building smart contracts. You can use a token mix, the most fundamental ERC-20 implementation, for learning about deploying smart contracts. Here is a command for loading a smart contract template in Brownie.

$ brownie bake token

The command would help in creating a subdirectory “token/” followed by downloading the template project in it.

  • Structure of the Brownie Project

The next important highlight in the basics of Brownie framework would point at the structure of projects. You can find four crucial elements in the structure of a Brownie project. The elements include contract sources, interface sources, scripts for project testing and scripts for project interaction and deployment. 

In addition, the structure of the Brownie project also includes the “build/” and “reports/” directories. Brownie can use the directories for project management, and developers should not edit or delete the files from these directories. The “build/” directory includes project data like unit test results and compiler artifacts. The “reports/” directory contains the JSON report files required for the GUI.

Another crucial aspect of a Brownie python smart contract development project refers to project compilation. You can use the following command for compilation of your Brownie project.

$ brownie compile

Developers can also edit the config file to change the compiler version alongside optimization settings. It is important to note that Brownie would automatically compile the new or modified source files upon loading. Therefore, you do not have to run the compiler manually every time. 

The guide to using Brownie also dives into significance of the in-built console. It is a useful tool among answers to “How do you deploy a brownie smart contract?” by providing prospects for direct interactions with smart contracts on non-local chains. A console is also a promising tool for beginners to learn about the different functionalities of Brownie. You can find noticeable similarities between the console and a regular Python interpreter. Developers can load the console directly from within a project directory by using the following command.

$ brownie console

Brownie can help in supporting contract compilation through the console alongside starting the local RPC client. In addition, Brownie also provides a command prompt that can help you interact with the Brownie network and leverage its functionalities through the Brownie API.

You can understand Brownie Web3 usage by reflecting on the use of ‘accounts’ for facilitating access to local accounts. It is a list-like object featuring the ‘Account’ objects which have the capability of initiating transactions. 

Developers can find a ‘ContractContainer’ object in Brownie for all deployable contracts within a Brownie project. The objects are list-like objects which help in deploying new contracts. Deployment of a contract returns the ‘Contract’ object, which you can use for interacting with the contract. 

Subsequently, the ‘Contract’ object goes into the ‘ContractContainer’ object. The ‘Contract’ objects feature class methods that help in executing calls and transactions. You can use the ‘ContractCall.info’ method for viewing contracts which includes NatSpec documentation in the contract source.

Start your journey to become a smart contract developer or architect with an in-depth overview of smart contract fundamentals with Smart Contracts Skill Path

The journey of learning methods to deploy Brownie smart contract would also draw attention toward debugging transactions. You can use the “TransactionReceipt” object to obtain all the significant information regarding a transaction alongside the different methods for supporting the debugging process. 

For example, you can rely on ‘TransactionReceipt.events’ for evaluation of the events which did not work. On top of it, Brownie also offers functionalities for inspection of the transaction trace alongside information for which a transaction was canceled.

The next significant highlight for a smart contract developer working on Brownie would point at scripts. You can use scripts in the Brownie smart contract development and testing framework for automation of interactions with contracts and contract deployment. For example, you can use “from brownie import *” at the start of a script for accessing objects, just like in a console. You can store the “main” function of a script in the ‘scripts/’ folder and type the following command to execute the function. 

$ brownie run [script name]

Developers interested in learning about the uses of Brownie for smart contract development and testing should also know about testing projects. Brownie leverages the ‘pytest’ framework for facilitating contract testing. Developers have to store their tests in the “tests/” folder. You can run the full suite of tests by using the following command,

$ brownie test

Another important highlight of testing in the Brownie framework points to pytest fixtures. The fixtures help developers interact with the Brownie project and support the testing process. Developers can use fixtures by adding an argument featuring the same name with the inputs of the test function. 

During the testing process, you can encounter a “VirtualMachineError” exception for reverted transactions. Developers can create assertions around the exception by using ‘brownie.reverts’ in the role of a context manager. You can also include strings as arguments in optional cases. It is also important to match the error string by a transaction for successful testing. 

The guide for using Brownie web3 development and testing framework also involves an emphasis on test isolation. You can address test isolation through pytest fixtures such as ‘module_isolation’ and ‘fn_isolation.’ The ‘module_isolation’ fixture helps in resetting the local chain prior to and after module completion, thereby providing a clean environment.

On top of it, the ‘fn_isolation’ fixture involves taking a snapshot of the chain prior to running every test. After completing the test, the fixture would revert the chain to the previous state. As a result, developers can provide the definition for a common state in each test, thereby avoiding repetitive transactions. 

If you are new to smart contracts, you might not be sure of its capability. Check the detailed  guide Now on Smart Contract Use Cases

Example of Deploying Smart Contract with Brownie

The detailed overview of all the essential methods for working with Brownie for developing and deploying smart contracts proves how the framework offers a flexible tool for smart contract creation. Let us take a deep dive into the practical use of Brownie with an example of deploying a template project. 

  • Install the Important Dependencies

The first thing you need to use Brownie is the understanding of Brownie Python dependency. Brownie has been developed over Python3, thereby implying the need for installing Python3 before you start using the framework. You can download and install Python3 from the official website of Python or use the following command in the terminal.

python3 –V

You should also create a project directory and make it the current working directory by using the following commands.

mkdir brownieDemo

cd brownieDemo

Another important dependency for using the Brownie web3 development and testing framework is the Python package manager, i.e., pip. You can use the following command for installing Brownie through pip. 

pip3 install eth-brownie

Finally, you can complete the dependency installation by obtaining the token mix template with the following command. 

brownie bake token
  • Navigating the File Structure

Developers can start the process to deploy Brownie smart contract by opening the token directory in the text editor. Find the main contract file or “Token.sol” and write the desired contract logic. You can find the ‘token.py’ script in the ‘scripts/’ folder of the file structure for deploying the contract and introducing necessary modifications.

  • Booting the Ethereum Node

Rather than running a personal node, you can deploy the smart contract with Brownie on Ropsten testnet. On the other hand, developers can also opt for node service providers. The HTTP URL of the node provider helps in setting up the node endpoint with Brownie. In addition, you would also require test ETH on the concerned test network for paying the gas fees required to validate tests. 

The final step in the use of Brownie framework focuses on deployment of the contract. You can start the deployment process with compilation of the contract. You can use the ‘scripts/’ directory to access the contract logic, where you can make the necessary changes. After completing the necessary changes, you can use the deployment script to deploy the contract. Here is the command you can use for deploying a contract with Brownie.

brownie run token.py --network customnetworkname

The prompt could also ask users for the password created during creation of the account. Following the execution of the deployment command, you will receive the transaction hash. Upon confirmation of the transaction, Brownie would return to the address on which the contract had been deployed.

Want to know the real-world examples of smart contracts and understand how you can use it for your business? Check the presentation Now on Examples Of Smart Contracts

Conclusion 

The basic guide on “How do you deploy a Brownie smart contract?” prove the capabilities of Brownie for facilitating comprehensive testing. Brownie is a powerful Python-based framework that helps in developing, testing and deploying smart contracts. It features an in-built console that facilitates faster interaction with contracts. In addition, Brownie also includes the flexibility of using the ‘pytest’ framework alongside predefined fixtures. Start learning more about Brownie and deploy your own smart contract on the platform now.   

Unlock your career with 101 Blockchains' Learning Programs

*Disclaimer: The article should not be taken as, and is not intended to provide any investment advice. Claims made in this article do not constitute investment advice and should not be taken as such. 101 Blockchains shall not be responsible for any loss sustained by any person who relies on this article. Do your own research!

Source link

Check Also

Best Web3 Development Tools for Blockchain Developers

Web3 development has turned into a complex industry with the rise of diverse types of …