1/5
## Set balance in MochaSin and TitanoBoa We are going to discuss some cheat codes in MochaSin and TitanoBoa. These codes will allow us to set the balance of a user and also pretend or prank a user. This is important if we want to test how other users with different amounts of money interact with our contract. We are going to call our `fund` function and actually send some money, but we need to make sure that whoever calls this `fund` function actually has money to do so. This is where we can start getting into the concept of cheat codes or functions that work with the PyEVM or local forked network in order to set up our tests better. For example, if we want to call `fund`, we'll need some money to actually have money. So, we can add this test: ```python def test_fund_with_money(coffee, account): account.transfer(coffee.address, to_wei(5, "ether")) assert coffee.balance() == to_wei(5, "ether") boa.env.set_balance(account.address, SEND_VALUE) coffee.fund(value=SEND_VALUE) # Assert amount_funded = coffee.funders(0) assert amount_funded == SEND_VALUE ``` We are going to call the `fund` function with money. In order to do this, we'll need to send some money to the account we want to interact with. Then, we can set the balance of the account with the `SEND_VALUE` amount. We can then go ahead and run this test: ```bash mox test ``` Or, we can run a specific test with: ```bash mox test -k test_fund_with_money ``` We are testing some basic functionality here, and I'm going to leave a lot of time for you to actually write a whole bunch of tests because you should get incredibly good at writing tests. AI's are very helpful at writing tests, and you should use AI's to help you write your tests. But, sometimes, AI's screw it up, and what's worse is if you write a test or AI writes a test, and it doesn't test what you want it to test, and it breaks. That will be very bad. So, let's keep going, and I'll leave you some time to write tests afterwards.
A practical guide to writing cheat codes in Vyper smart contracts - This lesson covers setting the balance of a user within a test environment using the Boa cheat code library. You will learn how to effectively test the interaction of your smart contract with users who have different amounts of money.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar