Implement ERC20

This commit is contained in:
Aadi Desai 2023-02-06 23:12:29 +00:00
parent 59b4c9c14e
commit 1c93831b80

View file

@ -4,33 +4,82 @@ pragma solidity ^0.8.17;
import "./interfaces/IERC20.sol"; import "./interfaces/IERC20.sol";
contract ERC20 is IERC20Metadata { contract ERC20 is IERC20Metadata {
constructor (string memory name, string memory symbol) {} mapping(address => uint256) private _balances;
address _owner;
string _name;
string _symbol;
uint256 _totalSupply;
uint8 _decimals = 18;
mapping(address => mapping(address => uint256)) _approvals;
function name() external view returns (string memory) {} constructor(string memory newName, string memory newSymbol) {
_owner = msg.sender;
_name = newName;
_symbol = newSymbol;
}
function symbol() external view returns (string memory) {} function name() external view returns (string memory) {
return _name;
}
function decimals() external view returns (uint8) {} function symbol() external view returns (string memory) {
return _symbol;
}
function totalSupply() external view returns (uint256) {} function decimals() external view returns (uint8) {
return _decimals;
}
function balanceOf(address account) external view returns (uint256) {} function totalSupply() external view returns (uint256) {
return _totalSupply;
}
function transfer(address to, uint256 amount) external returns (bool) {} function balanceOf(address account) external view returns (uint256) {
return _balances[account];
}
function transfer(address to, uint256 amount) external returns (bool) {
require(_balances[msg.sender] >= amount, "insufficient balance");
_balances[msg.sender] -= amount;
_balances[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
function allowance(address owner, address spender) function allowance(address owner, address spender)
external external
view view
returns (uint256) returns (uint256)
{} {
return _approvals[owner][spender];
}
function approve(address spender, uint256 amount) external returns (bool) {} function approve(address spender, uint256 amount) external returns (bool) {
_approvals[msg.sender][spender] += amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom( function transferFrom(
address from, address from,
address to, address to,
uint256 amount uint256 amount
) external returns (bool) {} ) external returns (bool) {
require(_balances[from] >= amount, "insufficient balance");
require(
_approvals[from][msg.sender] >= amount,
"insufficient allowance"
);
_balances[from] -= amount;
_balances[to] += amount;
_approvals[from][msg.sender] -= amount;
emit Transfer(from, to, amount);
return true;
}
function mint(address to, uint256 amount) external {} function mint(address to, uint256 amount) external {
require(msg.sender == _owner, "only minter can mint");
_balances[to] += amount;
_totalSupply += amount;
}
} }