Problem Statement
You are tasked with creating a custom ERC-20 token named \"EduCoin\" on the Ethereum blockchain. Your EduCoin should have the following features:
1. The total supply of EduCoin is limited to 1,000,000 tokens.
2. Implement functions to mint new tokens and burn existing ones, accessible only by a designated minter role.
3. Ensure that the contract can handle edge cases such as attempting to transfer more tokens than an account holds or burning more tokens than are available in total supply.
Concepts
- ERC-20 standard
- smart contracts
- token minting and burning
Constraints
- Use Solidity version ^0.8.0
- Implement access control for minting and burning functions
Security Notes
- Be cautious of reentrancy attacks
- Ensure proper validation checks to prevent underflow and overflow in token operations
Solutions
Java 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 // Define a role for minting and burning tokens\n address public minter;\n\n // Constructor to initialize the token with name, symbol, total supply, and set the initial minter\n constructor() ERC20(\"EduCoin\", \"EDU\") {\n _mint(msg.sender, 1000000 * 10 ** decimals()); // Minting initial tokens to the contract creator\n minter = msg.sender; // Setting the contract creator as the minter initially\n }\n\n // Modifier to restrict functions only for the minter role\n modifier onlyMinter() {\n require(msg.sender == minter, \"Caller is not the minter\");\n _;\n }\n\n // Function to mint new tokens, accessible only by the minter\n function mint(address to, uint256 amount) external onlyMinter {\n require(totalSupply() + amount <= 1000000 * 10 ** decimals(), \"Cannot exceed total supply\"); // Ensure total supply limit is not exceeded\n _mint(to, amount);\n }\n\n // Function to burn tokens, accessible only by the minter\n function burn(uint256 amount) external onlyMinter {\n require(amount <= balanceOf(minter), \"Cannot burn more than owned\"); // Ensure that the burner doesn't burn more tokens than they own\n _burn(msg.sender, amount);\n }\n\n // Function to change the minter role to another address\n function setMinter(address newMinter) external onlyOwner {\n require(newMinter != address(0), \"New minter is the zero address\");\n minter = newMinter;\n }\n}\n This smart contract implements a custom ERC-20 token named EduCoin on the Ethereum blockchain. It uses OpenZeppelin's ERC20 and Ownable contracts for standard functionality and ownership management.
The contract initializes with a total supply of 1,000,000 EduCoins and sets the contract creator as both the owner and the minter. The minter has special privileges to mint new tokens or burn existing ones, but these actions are restricted using access control modifiers.
Security considerations include preventing reentrancy attacks by leveraging OpenZeppelin's safe transfer functions, and ensuring that token operations do not cause underflows or overflows, which is naturally handled by Solidity 0.8.x due to its built-in checks for arithmetic operations.\nThe contract includes a modifier onlyMinter to ensure that minting and burning functionalities can only be performed by the designated minter. Additionally, there are validation checks in place before minting new tokens (to prevent exceeding total supply) and before burning tokens (to ensure the burner does not burn more than they own).
Ownership of the contract is managed via OpenZeppelin's Ownable contract, allowing the owner to change the minter role to another address, thus providing flexibility in managing token operations.