Problem Statement
You are tasked with developing a simple ERC20 token contract for a new cryptocurrency called EduCoin. The contract should allow users to mint tokens, transfer them between addresses, and check balances. Additionally, implement functionality that allows the contract owner to burn tokens from any user's account as an administrative feature. Ensure the contract is secure against common attacks such as reentrancy. Your solution must include comprehensive comments explaining each function and its purpose.
Concepts
- Solidity
- ERC20 Token Standard
- Reentrancy Attack
Constraints
- Use Solidity version ^0.8.0
- Adhere to ERC20 token standard specifications
- Implement a mint function accessible only by the contract owner
Security Notes
- Be cautious of reentrancy vulnerabilities, especially in the transfer function
- Ensure proper access control for administrative actions like burning tokens
Solutions
Python Solution
pragma solidity ^0.8.0;\n\nimport \"@openzeppelin/contracts/token/ERC20/ERC20.sol\";\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\n\ncontract EduCoin is ERC20, Ownable {\n // Constructor to initialize the token with a name and symbol\n constructor() ERC20(\"EduCoin\", \"EDU\") {}\n\n // Function to mint new tokens. Only callable by the contract owner.\n function mint(address _to, uint256 _amount) public onlyOwner {\n _mint(_to, _amount);\n }\n\n // Function to burn tokens from a specific address. Only callable by the contract owner.\n function burnFrom(address _from, uint256 _amount) public onlyOwner {\n _burn(_from, _amount);\n }\n}\n This Solidity smart contract implements an ERC20 token named EduCoin using OpenZeppelin's secure and well-tested libraries. The contract inherits from the ERC20 class for standard token functionalities like transferring and checking balances, and Ownable to manage ownership of the contract.\nThe constructor initializes the token with a name (EduCoin) and symbol (EDU). The mint function allows the contract owner to create new tokens and send them to any specified address, ensuring that only the owner can add supply to the system. This is crucial for maintaining control over token distribution.\nAdditionally, the burnFrom function enables the contract owner to destroy a specific amount of tokens from any given address. This administrative feature might be useful in scenarios such as recovering accidentally sent tokens or removing compromised accounts. The use of onlyOwner modifier ensures that these sensitive operations can only be performed by the designated contract owner, enhancing security.\nBy leveraging OpenZeppelin's ERC20 and Ownable implementations, this contract benefits from built-in protections against common vulnerabilities like reentrancy attacks. These libraries are widely audited and trusted in the blockchain community, making them a reliable choice for smart contract development.