Created
July 18, 2023 00:47
-
-
Save thurendous/29e3935b3e33fd5b35f498a06915bcd0 to your computer and use it in GitHub Desktop.
This is an example of how to create a stablecoin in solidity. There is some useful comments in the beginning of the code also.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
// This is considered an Exogenous, Decentralized, Anchored (pegged), Crypto Collateralized low volitility coin | |
// Layout of Contract: | |
// version | |
// imports | |
// errors | |
// interfaces, libraries, contracts | |
// Type declarations | |
// State variables | |
// Events | |
// Modifiers | |
// Functions | |
// Layout of Functions: | |
// constructor | |
// receive function (if exists) | |
// fallback function (if exists) | |
// external | |
// public | |
// internal | |
// private | |
// view & pure functions | |
pragma solidity 0.8.19; | |
import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol"; | |
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | |
/* | |
* @title DecentralizedStableCoin | |
* @author Patrick Collins | |
* Collateral: Exogenous | |
* Minting (Stability Mechanism): Decentralized (Algorithmic) | |
* Value (Relative Stability): Anchored (Pegged to USD) | |
* Collateral Type: Crypto | |
* | |
* This is the contract meant to be owned by DSCEngine. It is a ERC20 token that can be minted and burned by the DSCEngine smart contract. | |
*/ | |
contract DecentralizedStableCoin is ERC20Burnable, Ownable { | |
error DecentralizedStableCoin__AmountMustBeMoreThanZero(); | |
error DecentralizedStableCoin__BurnAmountExceedsBalance(); | |
error DecentralizedStableCoin__NotZeroAddress(); | |
/* | |
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner as a parameter. | |
For example: | |
constructor() ERC20("DecentralizedStableCoin", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {} | |
Related code changes can be viewed in this commit: | |
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60 | |
*/ | |
constructor() ERC20("DecentralizedStableCoin", "DSC") {} | |
function burn(uint256 _amount) public override onlyOwner { | |
uint256 balance = balanceOf(msg.sender); | |
if (_amount <= 0) { | |
revert DecentralizedStableCoin__AmountMustBeMoreThanZero(); | |
} | |
if (balance < _amount) { | |
revert DecentralizedStableCoin__BurnAmountExceedsBalance(); | |
} | |
super.burn(_amount); | |
} | |
function mint(address _to, uint256 _amount) external onlyOwner returns (bool) { | |
if (_to == address(0)) { | |
revert DecentralizedStableCoin__NotZeroAddress(); | |
} | |
if (_amount <= 0) { | |
revert DecentralizedStableCoin__AmountMustBeMoreThanZero(); | |
} | |
_mint(_to, _amount); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment