/
rgy.sol
82 lines (64 loc) · 1.88 KB
/
rgy.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
pragma solidity >=0.4.22 <0.6.0;
contract RGYx {
address private creator;
mapping (address => uint256) public balance;
address[] public shareholders;
address public developer;
string public name;
uint256 genesisShares;
uint256 private shares;
uint256 public cost;
bool public availableToTransfer = false;
event ShareSold(address _to, uint256 _how);
event AvailableToTransferChange(bool _available);
constructor (string memory _name, address _developer, uint256 _shares, uint256 _cost) public {
creator = msg.sender;
name = _name;
genesisShares = _shares;
shares = _shares;
cost = _cost;
developer = _developer;
}
function sellShares(address to, uint256 how ) public returns (uint256) {
require(msg.sender == creator, "you are not Renvestgy");
require(shares >= how, "insuficent founds");
balance[to] += how;
shares -= how;
require(shares >= 0, "invalid sell");
uint256 available = shares/genesisShares*100;
if (available < 20) {
availableToTransfer = true;
emit AvailableToTransferChange(availableToTransfer);
}
// ! existance check
bool exist = false;
// * consume a lot of gas
for (uint i = 0; i<shareholders.length; i++) {
if (shareholders[i] == to) {
exist = true;
break;
}
}
if (!exist) {
shareholders.push(to);
}
emit ShareSold(to, how);
return shares;
}
function getAvailableShares() public view returns (uint256) {
return shares;
}
function getSoldShares() public view returns (uint256) {
uint256 total;
for (uint i = 0; i<shareholders.length; i++) {
total += balance[shareholders[i]];
}
return total;
}
function getGenesis() public view returns (uint256) {
return genesisShares;
}
function getTotalShareholders() public view returns (uint256) {
return shareholders.length;
}
}