Learn Brownie - 10 Code Examples & CST Typing Practice Test
Brownie is an open-source Python-based framework for Ethereum smart contract development, testing, and deployment. It integrates with Ethereum networks and provides a full-featured environment for building decentralized applications (dApps).
View all 10 Brownie code examples →
Learn BROWNIE with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Brownie Minimal Smart Contract Deployment
# contracts/Counter.sol
pragma solidity ^0.8.0;
contract Counter {
uint256 public count = 0;
function increment() public { count += 1; }
}
# deploy.py
from brownie import Counter, accounts
def main():
acct = accounts[0]
counter = Counter.deploy({'from': acct})
counter.increment({'from': acct})
print('Counter value:', counter.count())
A minimal example showing how to deploy and interact with a simple Solidity smart contract using Brownie.
Brownie ERC20 Token Deployment
# contracts/MyToken.sol
pragma solidity ^0.8.0;
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20('MyToken','MTK') {
_mint(msg.sender, initialSupply);
}
}
# deploy.py
from brownie import MyToken, accounts
def main():
acct = accounts[0]
token = MyToken.deploy(1000*10**18, {'from': acct})
print('Token deployed at:', token.address)
Deploy a simple ERC20 token using Brownie.
Brownie Smart Contract Interaction Example
from brownie import Contract, accounts
def main():
acct = accounts[0]
counter = Contract('0xYourDeployedAddress')
counter.increment({'from': acct})
print('Counter value:', counter.count())
Interact with an already deployed contract using Brownie.
Brownie Event Listening Example
from brownie import Counter, accounts
def main():
acct = accounts[0]
counter = Counter[-1] # Last deployed instance
tx = counter.increment({'from': acct})
for event in tx.events:
print('Event emitted:', event)
Listen to events emitted by a smart contract using Brownie.
Brownie Funding Contract Example
# contracts/FundMe.sol
pragma solidity ^0.8.0;
contract FundMe {
uint256 public balance = 0;
function fund() public payable { balance += msg.value; }
}
# deploy.py
from brownie import FundMe, accounts
def main():
acct = accounts[0]
fund_me = FundMe.deploy({'from': acct})
fund_me.fund({'from': acct, 'value': 1000000000000000000}) # 1 ETH
print('Contract balance:', fund_me.balance())
Deploy and fund a smart contract with Ether using Brownie.
Brownie Multi-Account Transaction Example
from brownie import accounts
def main():
sender = accounts[0]
receiver = accounts[1]
sender.transfer(receiver, '1 ether')
print('Transferred 1 ETH from sender to receiver')
Send transactions between multiple accounts in Brownie.
Brownie Upgradeable Contract Example
# contracts/LogicV1.sol
pragma solidity ^0.8.0;
contract LogicV1 {
uint public x;
function setX(uint _x) public { x = _x; }
}
# deploy.py
from brownie import LogicV1, accounts
def main():
acct = accounts[0]
logic = LogicV1.deploy({'from': acct})
logic.setX(42, {'from': acct})
print('Value set in proxy logic:', logic.x())
Deploy an upgradeable proxy contract using Brownie.
Brownie Testing Smart Contracts Example
import pytest
from brownie import Counter, accounts
def test_increment():
acct = accounts[0]
counter = Counter.deploy({'from': acct})
counter.increment({'from': acct})
assert counter.count() == 1
Writing tests for a Solidity smart contract using Brownie.
Brownie Interaction with ERC721 NFT
# contracts/MyNFT.sol
pragma solidity ^0.8.0;
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
contract MyNFT is ERC721 {
constructor() ERC721('MyNFT','MNFT') {}
function mint(address to, uint256 tokenId) public { _mint(to, tokenId); }
}
# deploy.py
from brownie import MyNFT, accounts
def main():
acct = accounts[0]
nft = MyNFT.deploy({'from': acct})
nft.mint(acct, 1, {'from': acct})
print('Minted NFT ID 1 to account:', acct)
Deploy and interact with an ERC721 NFT smart contract using Brownie.
Brownie Gas Estimation Example
from brownie import Counter, accounts
def main():
acct = accounts[0]
counter = Counter.deploy({'from': acct})
gas_estimate = counter.increment.estimate_gas({'from': acct})
print('Estimated gas for increment():', gas_estimate)
Estimate gas usage for a smart contract function using Brownie.
Frequently Asked Questions about Brownie
What is Brownie?
Brownie is an open-source Python-based framework for Ethereum smart contract development, testing, and deployment. It integrates with Ethereum networks and provides a full-featured environment for building decentralized applications (dApps).
What are the primary use cases for Brownie?
Writing Ethereum smart contracts in Solidity. Testing contracts using Python-based frameworks. Deploying contracts to Ethereum testnets or mainnet. Automating interaction with deployed contracts. Integrating blockchain contracts with Python backend scripts
What are the strengths of Brownie?
Pythonic workflow makes blockchain development more accessible. Strong testing ecosystem with pytest. Easy deployment to multiple Ethereum networks. Good documentation and community support. Supports both local and remote Ethereum nodes
What are the limitations of Brownie?
Limited support for non-Ethereum blockchains. Primarily focused on Python developers. Requires familiarity with Solidity for contract writing. Complex DeFi protocols may require advanced configurations. Smaller ecosystem compared to JavaScript frameworks like Hardhat or Truffle
How can I practice Brownie typing speed?
CodeSpeedTest offers 10+ real Brownie code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.