In the last lesson, we deployed our LearnCoin contract to the Goerli Testnet. In this lesson, we’ll explore in detail the functionalities of the LearnCoin contract and explain what each part does.
This is the full contract of our LearnCoin
Python
// SPDX-License-Identifier: None
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Snapshot.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20FlashMint.sol";
contract LearnCoin is ERC20, ERC20Burnable, ERC20Snapshot, AccessControl, Pausable, ERC20Permit, ERC20Votes, ERC20FlashMint {
bytes32 public constant SNAPSHOT_ROLE = keccak256("SNAPSHOT_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
constructor() ERC20("Learn Coin", "LC") ERC20Permit("Learn Coin") {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(SNAPSHOT_ROLE, msg.sender);
_grantRole(PAUSER_ROLE, msg.sender);
_mint(msg.sender, 21000000 * 10 ** decimals());
}
function snapshot() public onlyRole(SNAPSHOT_ROLE) {
_snapshot();
}
function pause() public onlyRole(PAUSER_ROLE) {
_pause();
}
function unpause() public onlyRole(PAUSER_ROLE) {
_unpause();
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override(ERC20, ERC20Snapshot)
{
super._beforeTokenTransfer(from, to, amount);
}
// The following functions are overrides required by Solidity.
function _afterTokenTransfer(address from, address to, uint256 amount)
internal
override(ERC20, ERC20Votes)
{
super._afterTokenTransfer(from, to, amount);
}
function _mint(address to, uint256 amount)
internal
override(ERC20, ERC20Votes)
{
super._mint(to, amount);
}
function _burn(address account, uint256 amount)
internal
override(ERC20, ERC20Votes)
{
super._burn(account, amount);
}
}
The contract you deployed is an ERC20 token contract named LearnCoin. It includes several features from OpenZeppelin’s contract library, such as burnable tokens, snapshots, access control, pausable tokens, permit functionality, votes, and flash mint.
The import statements at the beginning of the contract pull in code from OpenZeppelin’s contract library:
ERC20.sol is the base contract for ERC20 tokens.ERC20Burnable.sol adds the ability for token holders to destroy their own tokens.ERC20Snapshot.sol enables creating snapshots of token balances.AccessControl.sol is a contract module for managing access to certain functionality.Pausable.sol adds the ability to pause and unpause token transfers.ERC20Permit.sol enables holders to spend user’s tokens via permits.ERC20Votes.sol adds voting capabilities to the token.ERC20FlashMint.sol allows for flash minting of tokens.The LearnCoin contract inherits from the imported OpenZeppelin contracts. This means it will have all the methods and properties from those contracts.
SNAPSHOT_ROLE and PAUSER_ROLE are constant values (created using the keccak256 hash function) that represent specific roles for access control within the contract.
When the LearnCoin contract is deployed, the constructor function is called. It sets the token’s name and symbol, grants the deploying account (msg.sender) the admin, snapshot, and pauser roles, and mints an initial supply of tokens to the deploying account.
snapshot, pause, and unpause are functions that allow accounts with the corresponding roles to perform certain actions. snapshot enables a snapshot to be made, pause and unpause allow to stop and restart token transfers.
The _beforeTokenTransfer, _afterTokenTransfer, _mint, and _burn functions are internal functions that are overridden from the parent contracts. They implement custom logic for the LearnCoin contract.
_beforeTokenTransfer: This function is called before any transfer of tokens. It ensures that token transfers are not paused._afterTokenTransfer: This function is called after any transfer of tokens. It’s an empty function here, but can be used for custom logic that should happen right after transfers._mint: This function creates new tokens and assigns them to a given account._burn: This function destroys tokens from a given account.This was a quick overview of your LearnCoin contract. With the contract deployed, you’re now ready to interact with it, which we’ll cover in the next lesson.