1/5
We've just built the ability for a user to deposit collateral, and now we're going to dive into the minting of the Decentralized Stable Coin! It's very important to ensure that there's always at least 2:1 ratio of collateral to the amount of DSC that's being minted. The DSC engine is responsible for managing that ratio, so let's take a look at the code. Here's the `mintDSC` function: ```javascript @external def mint_dsc(amount: uint256): pass ``` This function allows the user to specify the amount of DSC they want to mint. Before the actual minting happens, we need to check the user's collateral. So, let's create a function that calculates the user's health factor: ```javascript @internal def health_factor(user: address) -> uint256: total_dsc_minted: uint256 = 0 total_collateral_value_usd: uint256 = 0 (total_dsc_minted, total_collateral_value_usd) = self.get_account_information(user) return self.calculate_health_factor(total_dsc_minted, total_collateral_value_usd) ``` The `health_factor` function calculates the user's health factor based on the ratio of their total collateral value in USD and the amount of DSC they've already minted. Now, we need a function to calculate the health factor: ```javascript @internal def calculate_health_factor(total_dsc_minted: uint256, total_collateral_value_usd: uint256) -> uint256: if total_dsc_minted == 0: return MAX_HEALTH_FACTOR # What's the ratio of DSC minted to collateral value? collateral_adjusted_for_threshold: uint256 = (total_collateral_value_usd + LIQUIDATION_THRESHOLD) // LIQUIDATION_PRECISION return (collateral_adjusted_for_threshold * PRECISION) // total_dsc_minted ``` This function calculates the health factor based on the total DSC minted and the collateral value in USD. Finally, let's take a look at how we actually mint the DSC: ```javascript @internal def _mint_dsc(amount_dsc_to_mint: uint256): assert amount_dsc_to_mint > 0, "DSCEngine: Needs more than zero" self.user_to_dsc_minted[msg.sender] += amount_dsc_to_mint self.revert_if_health_factor_broken(msg.sender) extcall dsc.mint(msg.sender, amount_dsc_to_mint) ``` The `_mint_dsc` function makes sure that the amount of DSC being minted is above 0, it then updates the amount of DSC minted for the user, and finally it calls the DSC's mint function! We've now created the code for minting the DSC. Now, we will make sure that only the DSC engine is able to mint our decentralized stablecoin. We'll start by going to our decentralized stablecoin contract. ```javascript @deploy def init(): erc20.init() ``` Right when we deploy, we're going to set the minter to be the DSC engine, and we're going to set the owner to be the DSC engine as well. ```javascript @deploy def init(): erc20.init() erc20.set_minter(dsc_engine) erc20.ow_transfer_ownership(dsc_engine) ``` This ensures that only the DSC engine can mint DSC, and it can also transfer ownership of the DSC to another address!
A technical walkthrough of minting DSC and its relation to the health factor. This lesson explains how to deposit collateral for minting DSC, how to mint DSC based on the collateral deposited, and how the health factor is calculated based on the amount of DSC minted and the total USD value of collateral deposited.
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