Learn BROWNIE with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.