ERC20 smart contract with mint & burn functionality and full Foundry unit tests.
Built with Solidity ^0.8.0 and OpenZeppelin.
File: src/FrogCoin.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract FrogCoin is ERC20, Ownable {
constructor() ERC20("FrogCoin", "FROG") {}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function burn(address from, uint256 amount) public onlyOwner {
_burn(from, amount);
}
}- ERC20 standard based on OpenZeppelin
mint: only owner can mint tokensburn: only owner can burn tokens from any address- Access control via
Ownable
File: test/FrogCoin.t.sol
test_InitialSupplyAndDetails→ verifies name, symbol, decimals, totalSupplytest_Mint_Success→ owner can mint tokenstest_Mint_RevertWhenNotOwner→ non-owner cannot minttest_Burn_Success→ burn tokens successfullytest_Burn_RevertWhenNotOwner→ revert if non-owner tries to burntest_Burn_RevertInsufficientBalance→ revert if trying to burn too muchtest_Transfer_Success→ standard ERC20 transfer works
Make sure you have Foundry installed.
# Initialize project
forge init frog-coin
# Install dependencies
forge install OpenZeppelin/[email protected]
# Run tests
forge testMIT