Brownie
Using Brownie to Deploy To Phron
Introduction
Brownie is an Ethereum development environment that helps Python developers manage and automate the recurring tasks inherent to building smart contracts and DApps. Brownie can directly interact with Phron's Ethereum API so it can also be used to deploy smart contracts on Phron.
This guide will cover how to use Brownie to compile, deploy, and interact with Ethereum smart contracts on the Phron TestNet.
Please note that Brownie is no longer actively maintained. You can check out Ape as an alternative Python Ethereum development environment.
Checking Prerequisites
To get started, you will need the following:
Have MetaMask installed and connected to Phron
Have an account with funds. You can get DEV tokens for testing on Phron once every 24 hours from the Phron Faucet.
To test out the examples in this guide on Phron, you will need to have your own endpoint and API key, which you can get from one of the supported Endpoint Providers
For this guide, Python version 3.9.10, pip version 22.0.3, and pipx version 1.0.0 were used.
Creating a Brownie Project
You will need to install Brownie and create a Brownie project if you don't already have one. You can choose to either create an empty project or use a Brownie mix, which is essentially a template to build your project on. For this example, you can create an empty project. You can get started by completing the following steps:
Create a directory for your project
If you don't already have
pipx
installed, go ahead and install itInstall Brownie using
pipx
, which is used to run executables installed locally in your project. Brownie will be installed into a virtual environment and be available directly from the command line
NoteA common error while installing Brownie on Ubuntu is:
This can be resolved by using the following command:
Create a project
Your Brownie project should contain the following empty directories:
build - for project data such as contract artifacts from compilation
contracts - to store the smart contract files
interfaces - for smart contract interfaces that are required for your project
reports - for JSON report files for use in the Brownie GUI
scripts - where Python scripts used for deploying contracts or other automated tasks will live
tests - to store Python scripts for testing your project. Brownie uses the
pytest
framework for unit testing
Another important file to note that is not included in an empty project is the brownie-config.yaml
configuration file. The configuration file is optional and comes in handy when customizing specific settings such as a default network, compiler version and settings, and more.
Network Configuration
To deploy to a Phron network, you'll need to add and configure the network. Network configurations in Brownie are added from the command line. Brownie can be used with both development and live environments.
Phron are supported out of the box with Brownie as of version 1.19.3. To view the complete list of supported networks, you can run the following command:
If you're looking to deploy a contract to a Phron development node you'll need to add the network configurations. Under the hood, Brownie uses Ganache for development environments. However, since a Phron development node acts as your own personal development environment, Ganache isn't needed. Therefore, you can configure a development node as a "live" network.
To add Phron development node configurations, you can run the following command:
If you successfully added the network, you'll see a success message along with the network details in the terminal.
To deploy to a Phron network, or run tests on a specific network, you can specify the network by appending the following to the given command:
If you would like to set a default network, you can do so by adding the following snippet to the brownie-config.yaml
configuration file:
NoteKeep in mind that the
brownie-config.yaml
file isn't automatically created, you can optionally create it yourself.
Setting your Networks RPC URLs
It is recommended that you override the default Brownie RPC URLs to your own RPC endpoint or the public Phron network endpoints. You can override the default Brownie RPC URL for each network as follows:
Account Configuration
Before you can deploy a contract, you'll need to configure your account, which is also done from the command line. To add a new account you can run:
Make sure to replace INSERT_ACCOUNT_NAME
with your name of choice. For this example, alice
will be used as the account name.
You'll be prompted to enter in your private key and a password to encrypt the account with. If the account was successfully configured, you'll see your account address printed to the terminal.
The Contract File
Next you can create a contract inside of the contracts
directory. The smart contract that you'll deploy as an example will be called Box
, it will let you store a value that can be retrieved later. You can create a Box.sol
contract by running the following command:
Open the file and add the following contract to it:
Compiling Solidity
To compile the contract you can simply run:
NoteThe first time you compile your contracts it may take longer than usual while the
solc
binary is installed.
After compilation, you'll find the build artifacts in the build/contracts
directory. The artifacts contain the bytecode and metadata of the contract, which are .json
files. The build
directory should already be in the .gitignore
file but if it's not, it’s a good idea to add it there.
If you want to specify the compiler version or compilation settings, you can do so in the brownie-config.yaml
file. Please note that if you haven't already created this file, you will need to do so. Then you can specify the compiler like so:
NoteYou can view the list of EVM versions supported by Brownie in their documentation.
Your contracts will only be compiled again if Brownie notices that a change has been made. To force a new compilation, you can run:
Deploying the Contract
In order to deploy the Box.sol
smart contract, you will need to write a simple deployment script. You can create a new file under the scripts
directory and name it deploy.py
:
Next, you need to write your deployment script. To get started start, take the following steps:
Import the
Box
contract and theaccounts
module frombrownie
Load your account using
accounts.load()
which decrypts a keystore file and returns the account information for the given account nameUse the
deploy
method that exists within this instance to instantiate the smart contract specifying thefrom
account and thegas_limit
You can now deploy the Box.sol
contract using the run
command and specifying the network:
After a few seconds, the contract is deployed, and you should see the address in the terminal.
Congratulations, your contract is live! Save the address, as you will use it to interact with this contract instance in the next step.
Interacting with the Contract
You can interact with contracts using the Brownie console for quick debugging and testing or you can also write a script to interact.
Using Brownie Console
To interact with your newly deployed contract, you can launch the Brownie console
by running:
The contract instance will automatically be accessible from the console. It will be wrapped in a ContractContainer
which also enables you to deploy new contract instances. To access the deployed contract you can use Box[0]
. To call the store
method and set the value to 5
, you can take the following steps:
Create a variable for the contract
Call the
store
method using your account and set the value to5
Enter the password for your account
The transaction will be signed by your account and broadcasted to the network. Now, you can retrieve the value by taking these steps:
Call the
retrieve
methodEnter your password
You should see 5
or the value you have stored initially.
Using a Script
You can also write a script to interact with your newly deployed contract. To get started, you can create a new file in the scripts
directory:
Next, you need to write your script that will store and then retrieve a value. To get started, take the following steps:
Import the
Box
contract and theaccounts
module frombrownie
Load your account using
accounts.load()
which decrypts a keystore file and returns the account information for the given account nameCreate a variable for the
Box
contractUse the
store
andretrieve
functions to store a value and then retrieve it and print it to the console
To run the script, you can use the following command:
You'll need to enter the password for Alice to send the transaction to update the stored value. Once the transaction goes through, you should see a transaction hash and a value of 5
printed to the console.
Congratulations, you have successfully deployed and interacted with a contract using Brownie!
This tutorial is for educational purposes only. As such, any contracts or code created in this tutorial should not be used in production.The information presented herein has been provided by third parties and is made available solely for general information purposes. Phron does not endorse any project listed and described on the Phron Doc Website (https://docs.Phron.ai/). Phron does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Phron disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Phron. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Phron has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Phron harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.
Last updated