1/5
## Pranking and Impersonating in Solidity Tests Sometimes when we're writing tests, we want to pretend to be somebody else, or we want to prank. Our account, the default account, is going to be who deployed this address. But we should probably check that the non-owner cannot withdraw. We will need to get some other random user. We can assign a value to `RANDOM_USER`: ```python RANDOM_USER = boa.env.generate_address('non-owner') ``` Now we can write a test called `test_non_owner_cannot_withdraw`: ```python def test_non_owner_cannot_withdraw(coffee, account): # Arrange boa.env.set_balance(account.address, SEND_VALUE) coffee.fund(value=SEND_VALUE) # Act with boa.prank(RANDOM_USER): boa.reverts('You are not the owner!', coffee.withdraw()) ``` We are arranging by funding the account. Then, we are pranking a random user, and we are reverting. Let's test it out with a terminal command: ```bash mox test ``` This is how you pretend to be some other user. This is how you set the current user to somebody else random. And this is how you revert. Let's keep going. We will check to see if it doesn't work. Let's also check to see how it does work. We can do a test called `test_owner_can_withdraw`. ```python def test_owner_can_withdraw(coffee, account): # Arrange boa.env.set_balance(account.address, SEND_VALUE) coffee.fund(value=SEND_VALUE) # Act with boa.env.prank(coffee.OWNER): coffee.withdraw() # Assert assert coffee.funders() == boa.env.get_balance(coffee.OWNER) == 0 ``` Usually, just to make sure, I like to also prank that I am actually the owner here. So, I might do something like `boa.env.set_balance` and then `coffee.OWNER`. We then do `coffee.fund(value=SEND_VALUE)`. Finally, we assert that `boa.env.get_balance(coffee.address)` is equal to zero. Let's test this out. ```bash mox test ``` We ran into an error "unsupported type". Let's scroll up and see what line it's mad at me about. It looks like it's on the `assert` line about getting the balance of `coffee.address`. Let's change the `assert` line, to instead be: ```python assert coffee.funders() == 0 ``` Let's run `mox test` again and see if it passes.
A comprehensive guide to pranking in Vyper smart contract testing with Boa. The lesson explores the 'boa.env.prank()' function, enabling you to impersonate other users and test scenarios involving unauthorized access to functions like withdraw.
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