1/5
## DSC Engine Intro This lesson will introduce the DSC Engine, a component of a decentralized stablecoin system. We'll begin by creating a new contract in the _src_ folder named _dsc_engine.vy_. We will be creating an ERC20 standard contract that defines the core functionality of the DSC Engine. We'll copy and paste the following code into the new file. ```python # pragma version 0.4.1 @license MIT @author You @title DSC Engine @notice This contract is the engine of the Decentralized Stable Coin. It is responsible for the minting and burning of the stablecoin. @notice Collateral: Exogenous (WETH, WBTC, etc...) @notice Minting (Stability) Mechanism: Decentralized (Algorithmic) @notice Value (Relative Stability): Anchored (Pegged to USD) @notice Collateral Type: Crypto # from snekmate.tokens import erc20 from snekmate.auth import ownable as ow # initializes: ow initializes: erc20(ownable=ow) # exports: ( erc20.IERC20, erc20.burn_from, erc20.mint, erc20.set_minter, ow.owner, ow.owner_set, ow.transfer_ownership ) # NAME: constant(String[25]) = "Decentralized Stable Coin" SYMBOL: constant(String[5]) = "DSC" DECIMALS: constant(uint8) = 18 EIP_712_VERSION: constant(String[20]) = "1" ``` We'll create a new header titled _EXTERNAL FUNCTIONS_. We'll also create the _@deploy_ constructor function. ```python # # EXTERNAL FUNCTIONS # @deploy def __init__(): pass ``` In the _@deploy_ constructor function we are going to take in a list of token addresses. ```python @deploy def __init__(): token_addresses: address[2], ``` If we wanted this to be more advanced, we could use a dynamic array or other data structures. However, we are just going to keep things simple. We will only support two types of collateral. ```python @deploy def __init__(): token_addresses: address[2], price_feed_addresses: address[2], ``` We will also need the address of the DSC token. ```python @deploy def __init__(): token_addresses: address[2], price_feed_addresses: address[2], dsc_address: address ``` We'll create a new header titled _STATE VARIABLES_. We'll also define _DSC_ as a public immutable address. ```python # # STATE VARIABLES # DSC: public(immutable) i_decentralized_stable_coin, ``` Next, we'll define _COLLATERAL_TOKENS_ as a public immutable address array of size 2. ```python # # STATE VARIABLES # DSC: public(immutable) i_decentralized_stable_coin, COLLATERAL_TOKENS: public(immutable) address[2], ``` We'll also define _token_to_price_feed_ as a public hashmap that maps token addresses to price feeds. ```python # # STATE VARIABLES # DSC: public(immutable) i_decentralized_stable_coin, COLLATERAL_TOKENS: public(immutable) address[2], # # Storage # token_to_price_feed: public(HashMap[address, address]) ``` Lastly, we'll set our state variables. ```python # # STATE VARIABLES # DSC: public(immutable) i_decentralized_stable_coin, COLLATERAL_TOKENS: public(immutable) address[2], # # Storage # token_to_price_feed: public(HashMap[address, address]) # # EXTERNAL FUNCTIONS # @deploy def __init__(): token_addresses: address[2], price_feed_addresses: address[2], dsc_address: address DSC = i_decentralized_stable_coin.dsc_address COLLATERAL_TOKENS = token_addresses self.token_to_price_feed[token_addresses[0]] = price_feed_addresses[0] self.token_to_price_feed[token_addresses[1]] = price_feed_addresses[1] ``` We'll create a new file titled _i_decentralized_stable_coin.vy_. We'll copy and paste the following code into this file. ```python # pragma version 0.4.1 @license MIT @author You @title i_decentralized_stablecoin @notice # @external def burn_from(owner: address, amount: uint256): pass # @external def mint(owner: address, amount: uint256): pass ``` Finally, we'll import the _i_decentralized_stable_coin_ interface. ```python # pragma version 0.4.1 @license MIT @author You @title DSC Engine @notice This contract is the engine of the Decentralized Stable Coin. It is responsible for the minting and burning of the stablecoin. @notice Collateral: Exogenous (WETH, WBTC, etc...) @notice Minting (Stability) Mechanism: Decentralized (Algorithmic) @notice Value (Relative Stability): Anchored (Pegged to USD) @notice Collateral Type: Crypto # from snekmate.tokens import erc20 from snekmate.auth import ownable as ow from interfaces import i_decentralized_stable_coin # initializes: ow initializes: erc20(ownable=ow) implements: i_decentralized_stable_coin # exports: ( erc20.IERC20, erc20.burn_from, erc20.mint, erc20.set_minter, ow.owner, ow.owner_set, ow.transfer_ownership ) # NAME: constant(String[25]) = "Decentralized Stable Coin" SYMBOL: constant(String[5]) = "DSC" DECIMALS: constant(uint8) = 18 EIP_712_VERSION: constant(String[20]) = "1" ``` Now, we can compile our contract. ```bash mox compile ``` This concludes the DSC Engine Intro lesson.
A comprehensive guide to creating a smart contract for a decentralized stablecoin with collateralized value. The lesson walks through setting up the contract's structure, including its stability mechanism, and provides insights on best practices in building a secure and efficient DeFi applications functionality, and implementing the necessary interfaces for interacting with a stablecoin and its collateral.
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