-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.sol
76 lines (62 loc) · 3.07 KB
/
base.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// ERC20 standard interface
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract MyToken is IERC20 {
string public name = "MyToken"; // Token name
string public symbol = "MTK"; // Token symbol
uint8 public decimals = 18; // Decimal places
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
constructor(uint256 initialSupply) {
_totalSupply = initialSupply * (10 ** uint256(decimals)); // Set the total supply with decimals
_balances[msg.sender] = _totalSupply; // Assign all tokens to the contract creator
emit Transfer(address(0), msg.sender, _totalSupply); // Emit transfer event from the zero address
}
// Total supply of the token
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
// Balance of a specific account
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
// Transfer tokens to a specified address
function transfer(address recipient, uint256 amount) public override returns (bool) {
require(_balances[msg.sender] >= amount, "Insufficient balance");
_balances[msg.sender] -= amount;
_balances[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
// Allowance given by the token owner to a spender
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
// Approve an address to spend tokens on behalf of the owner
function approve(address spender, uint256 amount) public override returns (bool) {
_allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
// Transfer tokens from one address to another using allowance
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
require(_balances[sender] >= amount, "Insufficient balance");
require(_allowances[sender][msg.sender] >= amount, "Allowance exceeded");
_balances[sender] -= amount;
_balances[recipient] += amount;
_allowances[sender][msg.sender] -= amount;
emit Transfer(sender, recipient, amount);
return true;
}
}