-
Notifications
You must be signed in to change notification settings - Fork 4
/
InvestorCapValidator.sol
204 lines (183 loc) · 5.47 KB
/
InvestorCapValidator.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
pragma solidity 0.4.24;
import "openzeppelin-solidity/contracts/lifecycle/Pausable.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "../interfaces/IModuleContract.sol";
import "../inheritables/TransferValidator.sol";
import "../interfaces/IERC20.sol";
/**
* @title Transfer Validator that checks if the trade will increase the investorCount above a max number of investors
*/
contract InvestorCapValidator is TransferValidator, IModuleContract, Pausable {
using SafeMath for uint;
/*----------- Constants -----------*/
bytes32 public constant moduleName = "InvestorCapValidator";
/*----------- Globals -----------*/
mapping(address => uint) internal investorCounts_;
uint internal investorCap_;
/*----------- Events -----------*/
event LogInvestorCountOverride(
address indexed sender,
address indexed investment,
uint newInvestorCount
);
event LogSetInvestorCap(
address indexed sender,
uint newInvestorCap
);
/**
* @dev Constructor for contract
* @param _investorCap maximum number of investors a token can have
*/
constructor(
uint _investorCap
) public {
investorCap_ = _investorCap;
}
/*----------- Validator Methods -----------*/
/**
* @dev Validate whether a trade will change the number of investors to a number above the Cap
* @param _token address token we are checking
* @param _to address The address which you want to transfer to
* @param _from address The Address which you want to transfer from
* @param _amount uint256 The Amount of tokens being transferred
* @return bool
*/
function canSend(address _token, address _from, address _to, uint256 _amount)
external
returns(bool)
{
IERC20 token = IERC20(_token);
uint toBalance = token.balanceOf(_to);
uint fromBalance = token.balanceOf(_from);
return validateAndUpdateCount(toBalance, fromBalance, _amount, msg.sender);
}
/*----------- Internal Methods -----------*/
/**
* @dev Validates and updates the balances
* @param toBalance balance of to address
* @param fromBalance balance of from address
* @param amount amount to be traded
* @return bool
*/
function validateAndUpdateCount(
uint toBalance,
uint fromBalance,
uint amount,
address investment
)
internal
returns (bool)
{
bool toIsNew = (toBalance == 0);
bool fromIsLeaving = (fromBalance.sub(amount) == 0);
if (toIsNew && !fromIsLeaving) {
if (investorCounts_[investment].add(1) > investorCap_) {
return false;
}
investorCounts_[investment] = investorCounts_[investment].add(1);
return true;
}
if (!toIsNew && fromIsLeaving) {
investorCounts_[investment] = investorCounts_[investment].sub(1);
return true;
}
return true;
}
/*----------- Setter Methods -----------*/
/**
* @dev Sets InvestorCount,
* Allowing the owner to "catch up" on trades that occured before module
* was added
* @param investment address of investment for which investors are being tracked
* @param _investorCount current number of investors
* @return uint newInvestorCount
*/
function overrideInvestorCount(
address investment,
uint _investorCount
)
external
onlyOwner
whenNotPaused
returns (uint newInvestorCount)
{
investorCounts_[investment] = _investorCount;
emit LogInvestorCountOverride(msg.sender, investment, investorCounts_[investment]);
return investorCounts_[investment];
}
/**
* @dev Sets InvestorCap,
* @param _investorCap maximum number of investors
* @return uint newInvestorCap
*/
function setInvestorCap(
uint _investorCap
)
external
onlyOwner
whenNotPaused
returns (uint newInvestorCap)
{
investorCap_ = _investorCap;
emit LogSetInvestorCap(msg.sender, investorCap_);
return investorCap_;
}
/*----------- Getter Methods -----------*/
/**
* @dev Returns the name of the validator
* @return bytes32
*/
function getName()
external
view
returns(bytes32)
{
return moduleName;
}
/**
* @dev Returns the type of the validator
* @return uint8
*/
function getType()
external
view
returns(uint8)
{
return moduleType;
}
/**
* @dev Returns the investorCap of the validator
* @return uint
*/
function investorCap()
external
view
returns(uint)
{
return investorCap_;
}
/**
* @dev Returns the investorCount of the validator
* @param investment address of investment for which to return the investorCount
* @return uint
*/
function investorCount(address investment)
external
view
returns(uint)
{
return investorCounts_[investment];
}
/**
* @dev Returns the investorCap - investorCount of the validator
* @param investment address of investment for which to return the remaining investors
* @return uint
*/
function getRemainingInvestors(address investment)
external
view
returns(uint)
{
return (investorCap_ - investorCounts_[investment]);
}
}