-
Notifications
You must be signed in to change notification settings - Fork 19
/
TokenRules.sol
315 lines (267 loc) · 9.13 KB
/
TokenRules.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
pragma solidity ^0.5.0;
// Copyright 2019 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import "./EIP20TokenInterface.sol";
import "../organization/Organized.sol";
/**
* @notice Register of whitelisted rules that are allowed to initiate transfers
* from a token holder accounts.
*
* @dev TokenHolder.executeRule() function will execute any rule that are
* signed by an authorized and non-expired session key.
* However, only the rules, that are registered in TokenRules
* can initiate transfers of token from TokenHolder to other beneficiaries.
* TokenHolder is going to allow TokenRules as a spender before
* execution of the rule (amount is limited by spendingLimit registered
* during an authorizaiton of an session key.). TokenHolder will
* clear this allowance after execution.
* Before execution of transfers from TokenHolder.
* During a execution, rule can call TokenRules.executeTransfers()
* function only once.
*/
contract TokenRules is Organized {
/* Events */
event RuleRegistered(
string _ruleName,
address _ruleAddress
);
/* Structs */
struct TokenRule {
string ruleName;
address ruleAddress;
string ruleAbi;
}
/**
* RuleIndex struct is going to be used in 'rulesByAddress' and
* 'rulesByNameHash' mappings for pointing to the index with 'rules' array.
* Simple usage of uint256 in those mappings does not work, because
* for non existing rule name and address it defaults to 0 index,
* which is obviously wrong. Before accessing 'rules' array by index
* one should check 'exists' field of the struct.
*/
struct RuleIndex {
uint256 index;
bool exists;
}
/* Storage */
/** Contains all registered rule in the order of registration. */
TokenRule[] public rules;
/** Mapping from a rule address to the index in the `rules` array. */
mapping (address => RuleIndex) public rulesByAddress;
/** Mapping from a rule name hash to the index in the `rules` array. */
mapping (bytes32 => RuleIndex) public rulesByNameHash;
EIP20TokenInterface public token;
/**
* TokenHolder contract before a rule execution will set the flag
* on (true) for itself. TokenRules.executeTransfers will set the flag to
* off (false) for _from (TokenHolder) after execution. This will restrict
* a rule to make a call to TokenRules.executeTransfers only *once*.
*/
mapping (address => bool) public allowedTransfers;
bool public areDirectTransfersEnabled;
/* Modifiers */
modifier onlyRule {
require(
rulesByAddress[msg.sender].exists,
"Only registered rule is allowed to call."
);
_;
}
modifier directTransfersAreEnabled {
require(
areDirectTransfersEnabled,
"Direct transfers are not allowed."
);
_;
}
/* Special Functions */
/**
* @dev Function requires:
* - Token address is not null.
*/
constructor(
OrganizationInterface _organization,
EIP20TokenInterface _token
)
Organized(_organization)
public
{
require(address(_token) != address(0), "Token address is null.");
token = _token;
areDirectTransfersEnabled = true;
}
/* External Functions */
/**
* @dev Function requires:
* - Only worker can call.
* - Rule name is not empty.
* - Rule with the specified name does not exist.
* - Rule address is not null.
* - Rule with the specified address does not exist.
* - Rule abi is not empty.
*
* @param _ruleName The name of a rule to register.
* @param _ruleAddress The address of a rule to register.
* @param _ruleAbi The abi of the rule to register.
*/
function registerRule(
string calldata _ruleName,
address _ruleAddress,
string calldata _ruleAbi
)
external
onlyWorker
{
require(bytes(_ruleName).length != 0, "Rule name is empty.");
require(_ruleAddress != address(0), "Rule address is null.");
require(bytes(_ruleAbi).length != 0, "Rule ABI is empty.");
bytes32 ruleNameHash = keccak256(abi.encodePacked(_ruleName));
require(
!rulesByNameHash[ruleNameHash].exists,
"Rule with the specified name already exists."
);
require(
!rulesByAddress[_ruleAddress].exists,
"Rule with the specified address already exists."
);
TokenRule memory rule = TokenRule({
ruleName: _ruleName,
ruleAddress: _ruleAddress,
ruleAbi: _ruleAbi
});
RuleIndex memory ruleIndex = RuleIndex({
index: rules.length,
exists: true
});
rulesByAddress[_ruleAddress] = ruleIndex;
rulesByNameHash[ruleNameHash] = ruleIndex;
rules.push(rule);
emit RuleRegistered(_ruleName, _ruleAddress);
}
/** @dev See documentation for allowedTransfers storage variable. */
function allowTransfers()
external
{
allowedTransfers[msg.sender] = true;
}
/** @dev See documentation for allowedTransfers storage variable. */
function disallowTransfers()
external
{
allowedTransfers[msg.sender] = false;
}
/**
* @dev Transfers from the specified account to all beneficiary
* accounts corresponding amounts.
* Function requires:
* - Only registered rule can call.
*
* @param _from An address from which transfer is done.
* @param _transfersTo List of addresses to transfer.
* @param _transfersAmount List of amounts to transfer.
*/
function executeTransfers(
address _from,
address[] calldata _transfersTo,
uint256[] calldata _transfersAmount
)
external
onlyRule
{
_executeTransfers(_from, _transfersTo, _transfersAmount);
}
/**
* @notice Enables direct transfers from token rules.
*
* @dev Function requires:
* - Only organization worker is allowed to call.
*
* \see directTransfers()
*/
function enableDirectTransfers()
external
onlyWorker
{
areDirectTransfersEnabled = true;
}
/**
* @notice Disables direct transfers from token rules.
*
* @dev Function requires:
* - Only organization worker is allowed to call.
*
* \see directTransfers()
*/
function disableDirectTransfers()
external
onlyWorker
{
areDirectTransfersEnabled = false;
}
/**
* @dev Transfers from the caller's account to all beneficiary
* accounts corresponding amounts.
*
* @param _transfersTo List of addresses to transfer.
* @param _transfersAmount List of amounts to transfer.
*/
function directTransfers(
address[] calldata _transfersTo,
uint256[] calldata _transfersAmount
)
external
directTransfersAreEnabled
{
_executeTransfers(msg.sender, _transfersTo, _transfersAmount);
}
/* Private Functions */
/**
* @dev Transfers from the specified account to all beneficiary
* accounts corresponding amounts.
* Function requires:
* - An account from which (_from) transfer will be done should
* allow this transfer by calling to TokenRules.allowTransfers().
* TokenRules will set this allowance back, hence, only
* one call is allowed per execution session.
* - _transfersTo and _transfersAmount arrays length should match.
*
* @param _from An address from which transfer is done.
* @param _transfersTo List of addresses to transfer.
* @param _transfersAmount List of amounts to transfer.
*/
function _executeTransfers(
address _from,
address[] memory _transfersTo,
uint256[] memory _transfersAmount
)
private
{
require(
allowedTransfers[_from],
"Transfers from the address are not allowed."
);
require(
_transfersTo.length == _transfersAmount.length,
"'to' and 'amount' transfer arrays' lengths are not equal."
);
for(uint256 i = 0; i < _transfersTo.length; ++i) {
token.transferFrom(
_from,
_transfersTo[i],
_transfersAmount[i]
);
}
allowedTransfers[_from] = false;
}
}