ERC20.sol [Code Snippet]
Implements the basic token functionality.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


import "./IERC20.sol";


contract ERC20 is IERC20 {

  mapping(address => uint256) private _balances;

  mapping(address => mapping(address => uint256)) private _allowances;

  uint256 private _totalSupply;

  string private _name;

  string private _symbol;


  constructor(string memory name_, string memory symbol_) {

    _name = name_;

    _symbol = symbol_;

  }


  function totalSupply() public view override returns (uint256) {

    return _totalSupply;

  }


  function balanceOf(address account) public view override returns (uint256) {

    return _balances[account];

  }


  function transfer(address recipient, uint256 amount) public override returns (bool) {

    _transfer(msg.sender, recipient, amount);

    return true;

  }


  // Internal transfer function with balance checks.

  function _transfer(address sender, address recipient, uint256 amount) internal {

    require(sender != address(0), "ERC20: transfer from the zero address");

    require(recipient != address(0), "ERC20: transfer to the zero address");

    require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");

     

    _balances[sender] -= amount;

    _balances[recipient] += amount;

    // Emit Transfer event (omitted for brevity)

  }


  // Additional functions (approve, transferFrom, etc.) omitted.

}


Share by: