DeveloperBreeze

Smart Contracts Programming Tutorials, Guides & Best Practices

Explore 7+ expertly crafted smart contracts tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Writing an ERC-20 Token Contract with OpenZeppelin

Tutorial August 22, 2024
solidity

  • Open MyToken.sol in your code editor and add the following code:
     // SPDX-License-Identifier: MIT
     pragma solidity ^0.8.0;

     import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
     import "@openzeppelin/contracts/access/Ownable.sol";

     contract MyToken is ERC20, Ownable {
         constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) {
             _mint(msg.sender, initialSupply);
         }

         function mint(address to, uint256 amount) public onlyOwner {
             _mint(to, amount);
         }

         function burn(uint256 amount) public {
             _burn(msg.sender, amount);
         }
     }