1/5
## Minting DSC In the previous lesson, we added code to our fuzzer to mint and redeem collateral, but that code wasn't actually minting any DSC. To make the code more effective, we need to add a way to actually mint DSC. Let's start by adding a new rule to our fuzzer to generate a random user and mint some DSC. We'll use the `st.integers` strategy to generate a random user seed between 0 and `USERS_SIZE - 1`, and we'll also define a rule for a random amount of DSC to mint, using `st.integers` with a min value of 0 and a max value of `MAX_DEPOSIT_SIZE`. ```python @rule user_seed=st.integers(min_value=0, max_value=USERS_SIZE - 1) ``` ```python @rule amount=st.integers(min_value=0, max_value=MAX_DEPOSIT_SIZE) ``` Next, we'll define a new function called `mint_dsc`, which will take the user seed, the amount of DSC to mint, and the collateral seed as parameters. ```python def mint_dsc(self, user_seed, amount, collateral_seed): ``` We'll use the `boa.env.prank` context manager to impersonate the user, and then call the `mint_dsc` function on the DSC engine. ```python user = self.users[user_seed] with boa.env.prank(user): self.dsce.mint_dsc(amount) ``` Now, let's run our fuzzer and see what happens. ```bash python3 -k stablecoin_fuzzer ``` We get the following error message: ``` DSCEngine: Health factor broken ``` This error is happening because our fuzzer is trying to mint DSC without first providing the user with enough collateral. To fix this issue, we need to add a try/except block around the `mint_dsc` function. If we encounter a `BoaError`, we'll check if the error message contains the phrase "Health factor broken". If it does, we'll call the internal `get_token_amount_from_usd` function on the DSC engine to calculate the amount of collateral needed, and then deposit the collateral into the user's account. ```python def mint_dsc(self, user_seed, amount, collateral_seed): user = self.users[user_seed] with boa.env.prank(user): try: self.dsce.mint_dsc(amount) except BoaError as e: if "DSCEngine: Health factor broken" in str(e.stack_trace.vm_error): collateral = self._get_collateral_from_seed(collateral_seed) amount = self.dsce.get_token_amount_from_usd(collateral.address, amount) self.dsce.deposit_collateral(collateral.address, amount) if amount == 0: self.mint_and_deposit(collateral_seed, user_seed, amount) ``` The `get_token_amount_from_usd` function is an external view on the DSC engine that takes the token address, the user's address, and the USD amount to deposit, and returns the amount of the token required to represent the given USD amount. Now, let's run our fuzzer again. This time, we should see that the fuzzer passes without encountering the health factor broken error. ```bash python3 -k stablecoin_fuzzer ``` As we can see, the fuzzer passes. This is because we have implemented a mechanism to handle the health factor broken error, and it's now possible to successfully mint DSC through our fuzzer.
A comprehensive guide to adding a mint DSC function in a Stablecoin fuzzer. The lesson covers the basics of writing a mint function with a try/catch block, handling errors, and using a view function for calculations.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
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 21min
Duration: 1h 58min
Duration: 2h 55min
Duration: 1h 55min
Duration: 46min
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
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