Contract 1: GameToken (ERC20)

Purpose: ERC20 token serving as the native game currency

Key Features:

  • Role-based access control (MINTER_ROLE, BURNER_ROLE)

  • Genesis mint of 1,000,000 tokens

  • Controlled minting/burning for game economy

contract GameToken is ERC20, AccessControl {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

What's happening here?

  • Creates a custom ERC20 token that inherits from OpenZeppelin's ERC20 and AccessControl

  • Defines two roles using keccak256 hashing: MINTER_ROLE and BURNER_ROLE

  • These roles control who can create or destroy tokens

constructor() ERC20("GameFi Token", "GAME") {
    _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
    _grantRole(MINTER_ROLE, msg.sender);
    
    // Genesis mint
    _mint(msg.sender, 1000000 * 10**18);
}

What's happening here?

  • Constructor sets token name to "GameFi Token" and symbol to "GAME"

  • Gives the deployer admin and minter roles

  • Mints 1 million initial tokens (multiplied by 10^18 for 18 decimal places)

function mint(address to, uint256 amount) external onlyRole(MINTER_ROLE) {
    _mint(to, amount);
}

function burn(address from, uint256 amount) external onlyRole(BURNER_ROLE) {
    _burn(from, amount);
}

What's happening here?

  • mint: Only addresses with MINTER_ROLE can create new tokens

  • burn: Only addresses with BURNER_ROLE can destroy tokens

  • This creates a controlled token economy where supply can be managed

Last updated