-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAOtoken.sol
138 lines (102 loc) · 4.76 KB
/
DAOtoken.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
pragma solidity ^0.8.10;
// SPDX-License-Identifier: MIT
contract DAOtoken {
string public name; // token name
string public symbol; // token symbol
uint8 public decimals;
uint256 public totalSupply;
address public owner;
uint256 public maxSupply;
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address=> uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed _owner, address indexed _spender, uint256 value);
event EcoBurn(address indexed from, uint256 value);
event Mint(address indexed to, uint256 value);
event OwnershipTransferred(address indexed _from, address indexed _to);
constructor() {
decimals = 18;
totalSupply = 1000000*10**uint256(decimals);
maxSupply = 1000000*10**uint256(decimals);
owner = msg.sender;
balanceOf[owner] = totalSupply;
name = "CraftStake";
symbol = "CRS";
}
function _transfer(address _from, address _to, uint256 _value) internal {
require(_to != address(0), "Address can't be empty.");
require(balanceOf[_from] >= _value, "You are trying to spend more than you have.");
require(balanceOf[_to] + _value >= balanceOf[_to]);
uint previousBalances = balanceOf[_from] + balanceOf[_to];
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
function transfer(address _to, uint256 _value) public returns(bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
// spend 3rd party's approved token amount
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(allowance[_from][msg.sender] >= _value, "You can't spend more than allowed");
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
// approve 3rd party to spend/ecoburn your tokens
function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
// disappear from blockchain supply forever
function ecoburn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value, "You can't burn more than you have.");
balanceOf[msg.sender] -= _value;
totalSupply -= _value;
emit EcoBurn(msg.sender, _value);
return true;
}
// eco burn tokens of 3rd party
function ecoburnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value, "You can't burn more than you have.");
require(allowance[_from][msg.sender] >= _value, "You can't burn more than you are allowed.");
balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value;
totalSupply -= _value;
emit EcoBurn(_from, _value);
return true;
}
// generate new tokens (only owner can do it)
function mint(uint256 _value) public returns (bool success) {
require(msg.sender == owner, "Only owner can mint.");
require(totalSupply + _value <= maxSupply, "Minted token supply can't exceed max fixed supply.");
balanceOf[owner] += _value;
totalSupply += _value;
emit Mint(owner, _value);
return true;
}
// generate new tokens to 3rd party (only owner can do it)
function mintTo(address _to, uint256 _value) public returns (bool success) {
require(msg.sender == owner, "Only owner can mint.");
require(_to != address(0), "You can't mint to empty address.");
require(totalSupply + _value <= maxSupply);
balanceOf[_to] += _value;
totalSupply += _value;
emit Mint(_to, _value);
return true;
}
// owner assigns new owner
function transferOwnership(address newOwner) public returns (bool success) {
require(newOwner != address(0), "You can't transfer ownership to empty address.");
require(newOwner != owner, "New owner is the same as old owner.");
require(msg.sender == owner, "Only owner can transfer ownership");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
return success;
}
function getAddressBalance(address myAddress) public view returns (uint256) {
return balanceOf[myAddress];
}
}