#21DaysSolidityChallenge Day 4: Fine-Tuning Your ERC-20 Token with Access Control in Solidity
🚀 Buy me a coffee! ☕ http://buymeacoffee.com/solidity
👋 Welcome to Day 4 of our Solidity Code Challenge series! In this challenge, we’ll take your ERC-20 token contract created on Day 3 and add an essential layer of security and control through access control mechanisms. You’ll create roles like “owner,” “minter,” and “pauser,” empowering you to manage and govern your token contract effectively.
Oh, this magical link is just sooo tempting! 🪄✨ Click away, my dear friend. 😉
🔐 Access Control in Smart Contracts: A Quick Overview
Before we delve into today’s challenge, let’s briefly explore the concept of access control in smart contracts:
- Access Control: Access control is the practice of restricting or permitting certain actions within a smart contract to specific addresses or roles. It helps manage who can perform critical functions in the contract.
Now, let’s get started with the challenge!
Step 1: Setting Up Your Development Environment
Ensure you have the following tools and accounts ready:
1. Ethereum Wallet: You’ll need an Ethereum wallet like MetaMask to interact with the Ethereum blockchain.
2. Solidity Compiler: Have the Solidity compiler (solc) installed on your computer or use online Solidity development environments like Remix.
3. Test Network: Choose a test network (e.g., Ropsten, Rinkeby, or Kovan) to deploy and test your token contract without using real Ether.
4. Integrated Development Environment (IDE): Consider using an IDE like Visual Studio Code with Solidity extensions for a smoother coding experience.
Step 2: Enhancing the Token Contract with Access Control
We’ll continue building upon your `MyToken.sol` contract from Day 3. In this step, you’ll add roles for “owner,” “minter,” and “pauser” and implement functions to grant and revoke access.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Pausable.sol";
contract MyToken is ERC20Pausable, Ownable {
constructor(uint256 initialSupply) ERC20("My Token", "MT") {
_mint(msg.sender, initialSupply * 10 ** uint256(decimals()));
}
function mint(address to, uint256 amount) public onlyMinter {
_mint(to, amount);
}
function burn(uint256 amount) public onlyOwner {
_burn(msg.sender, amount);
}
function grantMinterRole(address account) public onlyOwner {
_setupRole(DEFAULT_ADMIN_ROLE, account);
grantRole(MINTER_ROLE, account);
}
function revokeMinterRole(address account) public onlyOwner {
revokeRole(MINTER_ROLE, account);
}
function pause() public onlyPauser {
_pause();
}
function unpause() public onlyPauser {
_unpause();
}
}
In this updated contract:
- We import two essential Solidity libraries:
— `Ownable` from OpenZeppelin: This library provides basic authorization control functions and simplifies access control.
— `ERC20Pausable` from OpenZeppelin: This extension of the ERC-20 token contract includes pausable functionality.
- We inherit from `ERC20Pausable` and `Ownable`. This grants the contract access to access control and pausable functions.
- We create a constructor that accepts an initial supply and mints tokens to the contract deployer.
- We add a `mint` function, which allows addresses with the “MINTER_ROLE” to mint new tokens.
- A `burn` function allows the contract owner to burn (destroy) tokens.
- We include functions to grant and revoke the “MINTER_ROLE” to specific addresses.
- The `pause` and `unpause` functions enable the pauser role to pause and unpause token transfers when needed.
Step 3: Compiling the Enhanced Token Contract
Compile your enhanced ERC-20 token contract with the Solidity compiler. Use the following command in your terminal:
solc - bin - abi MyToken.sol
This command generates the bytecode (`MyToken.bin`) and ABI (`MyToken.abi`) needed for contract deployment.
Step 4: Deploying the Enhanced ERC-20 Token Contract
Deploy your enhanced ERC-20 token contract to a test network. Follow these steps:
1. Open your Ethereum wallet (e.g., MetaMask) and switch to the Ropsten network.
2. Acquire some test Ether for Ropsten from a faucet. You can find a faucet by searching “Ropsten faucet” online.
3. Deploy your contract using a tool like Remix or Truffle, or manually through a script.
- Go to Remix (https://remix.ethereum.org/).
- Click the “Solidity” tab.
- Create a new file, paste your enhanced contract code, and compile it.
- Switch to the “Deploy & Run Transactions” tab.
- Ensure your environment is set to “Injected Web3” (if you’re using MetaMask).
- Click the “Deploy” button.
4. Confirm the deployment in your wallet, and your contract will be deployed to the Ropsten network.
Step 5: Testing Access Control Mechanisms
Now that your contract is deployed, let’s test the access control mechanisms you’ve implemented.
Minting Tokens
1. In Remix, go to the “Deployed Contracts” section.
2. Find your deployed `MyToken` contract and click on it.
3. You will see the `mint` function under the contract details.
4. Enter an address to receive the minted tokens and specify the number of tokens to mint.
5. Click the “mint” button to execute the function.
Burning Tokens
1. In Remix, locate your deployed `MyToken` contract.
2. You will see the `burn` function under the contract details.
3. Enter the number of tokens to burn.
4. Click the “burn” button to execute the function.
Granting and Revoking Minter Role
1. In Remix, find your deployed `MyToken` contract.
2. You will see the `grantMinterRole` and `revokeMinterRole` functions under the contract details.
3. Use the `grantMinterRole` function to grant the minter role to another address.
4. Use the `revokeMinterRole` function to revoke the minter role from an address.
Pausing and Unpausing Token Transfers
1. In Remix, locate your deployed `MyToken` contract.
2. You will see the `pause` and `unpause` functions under the contract details.
3. Use the `pause` function to pause token transfers temporarily.
4. Use the `unpause` function to resume token transfers.
🎉 Congratulations! You’ve successfully added access control to your ERC-20 token contract and tested the access control mechanisms.
Conclusion 🌟
In this Day 4 challenge, you’ve fortified your ERC-20 token contract with access control features, enabling you to manage roles like “owner,” “minter,” and “pauser.” These controls enhance the security and governance of your token contract.
As you continue through the Solidity Code Challenge series, you’ll tackle more
complex concepts and build more advanced smart contracts. Keep up the great work, and explore the limitless possibilities of blockchain technology!
🚀 Happy coding! 🚀