DEV Community

ilija
ilija

Posted on • Updated on

Web3 backend and smart contract development for Python developers Musical NFTs part 4: Smart contract development with Brownie

From dependency for smart contract part we will need Brownie framework. This is Python based smart contract development framework. (there is new kid in the block ApeWorX framework which will be considered for some other articles in this seria)

Important thing to notice here is that often conflict can emerge between frameworks and libraries we use for our backend development and the one we use for smart contract development. That is why we need to create separate virtual environment only for smart contract development process.

In our case this process involves writing smart contracts in Solidity, testing them with Pytest and deployment to Polygon Mumbai test-net with Brownie. Ones we have our contract deployed on Polygon Mumbai we will go back to Django and plug our contracts to apps views to handle communication between client and smart contracts.

But first let's deactivate current Django virtual environment, make new smart contract folder, create new Python virtual environment and install few dependencies.

# deactivate virtual environment
$deactivate

#create and cd into smart-contract folder
$md smart-contracts && cd smart-contracts

# create and activate new python virtual environment 
$python -m venv env && source env/bin/activate

# pip install eth-brownie
$pip install eth-brownie

# create new local requirements.txt
$pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Restart your console and type brownie to check if installation was done properly

$brownie
Brownie v1.19.3 - Python development framework for Ethereum

Usage:  brownie <command> [<args>...] [options <args>]

Commands:
init               Initialize a new brownie project
bake               Initialize from a brownie-mix template
pm                 Install and manage external packages
compile            Compile the contract source files
console            Load the console
test               Run test cases in the tests/ folder
run                Run a script in the scripts/ folder
accounts           Manage local accounts
networks           Manage network settings
gui                Load the GUI to view opcodes and test coverage
analyze            Find security vulnerabilities using the MythX API

Options:
--help -h          Display this message
--version          Show version and exit

Type 'brownie <command> --help' for specific options and more information about
each command.
Enter fullscreen mode Exit fullscreen mode

Now let's install Ganache-cli (for more information go to https://github.com/trufflesuite/ganache). Ganache is Ethereum simulator for your local machine (ConSensys will end active development of Ganache and in future we will start to use other local blockchain emulators. But for now let`s keep Ganache
Consensys Announces the Sunset of Truffle and Ganache and New HardHat Partnership

To install Ganche on your local machine you need to have & install:

Node.js >=v16.0 and npm >=7.10.0

Ones you have this successfully installed then type to command line:

$ npm install ganache --global
Enter fullscreen mode Exit fullscreen mode

To confirm that you have Ganache type

$ ganche
Ganache CLI v6.12.1 (ganache-core: 2.13.1)

Available Accounts
==================
(0) 0xe261e26aECcE52b3788Fac9625896FFbc6bb4424 (100 ETH)
(1) 0xcE16e8eb8F4BF2E65BA9536C07E305b912BAFaCF (100 ETH)
(2) 0x02f1c4C93AFEd946Cce5Ad7D34354A150bEfCFcF (100 ETH)
(3) 0x0B75F0b70076Fab3F18F94700Ecaf3B00fE528E7 (100 ETH)
(4) 0x7194d1F1d43c2c58302BB61a224D41B649e65C93 (100 ETH)
(5) 0xC9A2d92c5913eDEAd9a7C936C96631F0F2241063 (100 ETH)
(6) 0xD79BcDE5Cb11cECD1dfC6685B65690bE5b6a611e (100 ETH)
(7) 0xb6D080353f40dEcA2E67108087c356d3A1AfcD64 (100 ETH)
(8) 0x31A064DeeaD74DE7B9453beB4F780416D8859d3b (100 ETH)
(9) 0x37524a360a40C682F201Fb011DB7bbC8c8A247c6 (100 ETH)

Private Keys
==================
(0) 0x7f109a9e3b0d8ecfba9cc23a3614433ce0fa7ddcc80f2a8f10b222179a5a80d6
(1) 0x6ec1f2e7d126a74a1d2ff9e1c5d90b92378c725e506651ff8bb8616a5c724628
(2) 0xb4d7f7e82f61d81c95985771b8abf518f9328d019c36849d4214b5f995d13814
(3) 0x941536648ac10d5734973e94df413c17809d6cc5e24cd11e947e685acfbd12ae
(4) 0x5829cf333ef66b6bdd34950f096cb24e06ef041c5f63e577b4f3362309125863
(5) 0x8fc4bffe2b40b2b7db7fd937736c4575a0925511d7a0a2dfc3274e8c17b41d20
(6) 0xb6c10e2baaeba1fa4a8b73644db4f28f4bf0912cceb6e8959f73bb423c33bd84
(7) 0xfe8875acb38f684b2025d5472445b8e4745705a9e7adc9b0485a05df790df700
(8) 0xbdc6e0a69f2921a78e9af930111334a41d3fab44653c8de0775572c526feea2d
(9) 0x3e215c3d2a59626a669ed04ec1700f36c05c9b216e592f58bbfd3d8aa6ea25f9

HD Wallet
==================
Mnemonic:      candy maple velvet cake sugar cream honey rich smooth crumble sweet treat
Base HD Path:  m/44'/60'/0'/0/{account_index}

Default Gas Price
==================
20000000000

Gas Limit
==================
6721975

Call Gas Limit
==================
9007199254740991

Listening on 127.0.0.1:8545
Enter fullscreen mode Exit fullscreen mode

Now lets intialize new project with Brownie inside smart-contract folder. Intialization of new project need to be done inside empty folder.

$md brownie_musical_nfts && cd brownie_musical_nfts
$brownie init
Brownie v1.19.3 - Python development framework for Ethereum

SUCCESS: A new Brownie project has been initialized at /home/code/my_tutorials/musical_nft_thumbnails/smart-contracts/brownie_musical_nfts
Enter fullscreen mode Exit fullscreen mode

We will create one test smart contract and try to compile to see if everyting works well. Inside contract folder create nft_package.sol file.

// SPDX-License-Identifier: MIT

pragma solidity 0.8.13;

contract NftPackage {

    uint256 public test = 10;

}
Enter fullscreen mode Exit fullscreen mode

Compiling newly created test smart contract

brownie compile
Brownie v1.19.3 - Python development framework for Ethereum

Compiling contracts...
Solc version: 0.8.13
Optimizer: Enabled  Runs: 200
EVM Version: Istanbul
Generating build data...
- NftPackage

Project has been compiled. Build artifacts saved at /home/ilija/code/my_tutorials/musical_nft_thumbnails/smart-contracts/brownie_musical_nfts/build/contracts
Enter fullscreen mode Exit fullscreen mode

Now we have fully functional Brownie project and we can move to writing our smart contract.

Top comments (0)