-
Notifications
You must be signed in to change notification settings - Fork 5
/
MintableToken.sol
110 lines (92 loc) · 2.8 KB
/
MintableToken.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
pragma solidity 0.4.24;
import "./StandardToken.sol";
import "./Ownable.sol";
/**
* @title Mintable token
* @dev Standard token with minting
* @dev Based on https://github.com/OpenZeppelin/zeppelin-solidity
*/
contract MintableToken is StandardToken, Ownable {
bool public mintingFinished;
uint256 public cap;
event Mint(address indexed to, uint256 amount);
event MintFinished();
modifier onlyMinting() {
require(!mintingFinished, "Minting is already finished");
_;
}
modifier onlyNotExceedingCap(uint256 amount) {
require(_totalSupply.add(amount) <= cap, "Total supply must not exceed cap");
_;
}
constructor(uint256 _cap) public {
cap = _cap;
}
/**
* @dev Creates new tokens for the given address
* @param to The address that will receive the minted tokens.
* @param amount The amount of tokens to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mint(address to, uint256 amount)
public
onlyOwner
onlyMinting
onlyValidAddress(to)
onlyNotExceedingCap(amount)
returns (bool)
{
mintImpl(to, amount);
return true;
}
/**
* @dev Creates new tokens for the given addresses
* @param addresses The array of addresses that will receive the minted tokens.
* @param amounts The array of amounts of tokens to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mintMany(address[] addresses, uint256[] amounts)
public
onlyOwner
onlyMinting
onlyNotExceedingCap(sum(amounts))
returns (bool)
{
require(
addresses.length == amounts.length,
"Addresses array must be the same size as amounts array"
);
for (uint256 i = 0; i < addresses.length; i++) {
require(addresses[i] != address(0), "Address cannot be zero");
mintImpl(addresses[i], amounts[i]);
}
return true;
}
/**
* @dev Stops minting new tokens.
* @return True if the operation was successful.
*/
function finishMinting()
public
onlyOwner
onlyMinting
returns (bool)
{
mintingFinished = true;
emit MintFinished();
return true;
}
function mintImpl(address to, uint256 amount) private {
_totalSupply = _totalSupply.add(amount);
_balanceOf[to] = _balanceOf[to].add(amount);
emit Mint(to, amount);
emit Transfer(address(0), to, amount);
}
function sum(uint256[] arr) private pure returns (uint256) {
uint256 aggr = 0;
for (uint256 i = 0; i < arr.length; i++) {
aggr = aggr.add(arr[i]);
}
return aggr;
}
}