This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathSetToken.sol
299 lines (253 loc) Β· 8.8 KB
/
SetToken.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
/*
Copyright 2018 Set Labs Inc.
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.
*/
pragma solidity 0.4.24;
import { DetailedERC20 } from "zeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
import { ERC20 } from "zeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import { StandardToken } from "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
import { ISetFactory } from "./interfaces/ISetFactory.sol";
/**
* @title SetToken
* @author Set Protocol
*
* Implementation of the basic {Set} token.
*/
contract SetToken is
StandardToken,
DetailedERC20
{
using SafeMath for uint256;
/* ============ Constants ============ */
string constant COMPONENTS_INPUT_MISMATCH = "Components and units must be the same length.";
string constant COMPONENTS_MISSING = "Components must not be empty.";
string constant INVALID_COMPONENT_UNIT = "Unit declarations must be non-zero.";
string constant INVALID_COMPONENT_ADDRESS = "Components must have non-zero address.";
string constant INVALID_NATURAL_UNIT = "Natural unit does not work with component decimals.";
string constant INVALID_SENDER = "Sender is not permitted to perform this function.";
string constant UNITS_MISSING = "Units must not be empty.";
string constant ZERO_QUANTITY = "Quantity must be greater than zero.";
/* ============ Structs ============ */
struct Component {
address address_;
uint unit_;
}
/* ============ State Variables ============ */
uint public naturalUnit;
Component[] public components;
// Mapping of componentHash to isComponent
mapping(bytes32 => bool) internal isComponent;
// Address of the Factory contract that created the SetToken
address public factory;
/* ============ Modifiers ============ */
modifier isCore() {
require(
msg.sender == ISetFactory(factory).core(),
INVALID_SENDER
);
_;
}
// Confirm that all inputs for creating a set are valid
modifier areValidCreationParameters(address[] _components, uint[] _units) {
// Confirm an empty _components array is not passed
require(
_components.length > 0,
COMPONENTS_MISSING
);
// Confirm an empty _quantities array is not passed
require(
_units.length > 0,
UNITS_MISSING
);
// Confirm there is one quantity for every token address
require(
_components.length == _units.length,
COMPONENTS_INPUT_MISMATCH
);
_;
}
modifier isPositive(uint _quantity) {
require(
_quantity > 0,
ZERO_QUANTITY
);
_;
}
modifier isValidDestination(address _to) {
require(_to != address(0));
require(_to != address(this));
_;
}
/* ============ Constructor ============ */
/**
* Constructor function for {Set} token
*
* As looping operations are expensive, checking for duplicates will be on the onus of the application developer
*
* @param _components address[] A list of component address which you want to include
* @param _units uint[] A list of quantities in gWei of each component (corresponds to the {Set} of _components)
* @param _naturalUnit uint The minimum multiple of Sets that can be issued or redeeemed
* @param _name string The Set's name
* @param _symbol string the Set's symbol
*/
constructor(
address _factory,
address[] _components,
uint[] _units,
uint _naturalUnit,
string _name,
string _symbol
)
public
DetailedERC20(_name, _symbol, 18)
isPositive(_naturalUnit)
areValidCreationParameters(_components, _units)
{
// NOTE: It will be the onus of developers to check whether the addressExists
// are in fact ERC20 addresses
uint8 minDecimals = 18;
uint8 currentDecimals;
for (uint16 i = 0; i < _units.length; i++) {
// Check that all units are non-zero. Negative numbers will underflow
uint currentUnits = _units[i];
require(
currentUnits > 0,
INVALID_COMPONENT_UNIT
);
// Check that all addresses are non-zero
address currentComponent = _components[i];
require(
currentComponent != address(0),
INVALID_COMPONENT_ADDRESS
);
// Figure out which of the components has the minimum decimal value
if (currentComponent.call(bytes4(keccak256("decimals()")))) {
currentDecimals = DetailedERC20(currentComponent).decimals();
minDecimals = currentDecimals < minDecimals ? currentDecimals : minDecimals;
} else {
// If one of the components does not implement decimals, we assume the worst
// and set minDecimals to 0
minDecimals = 0;
}
// Check the component has not already been added
require(!tokenIsComponent(currentComponent));
// add component to isComponent mapping
isComponent[keccak256(abi.encodePacked(currentComponent))] = true;
components.push(Component({
address_: currentComponent,
unit_: currentUnits
}));
}
// This is the minimum natural unit possible for a Set with these components.
require(
_naturalUnit >= uint(10) ** (18 - minDecimals),
INVALID_NATURAL_UNIT
);
factory = _factory;
naturalUnit = _naturalUnit;
}
/* ============ Public Functions ============ */
/*
* Mint set token for given address.
* Can only be called by authorized contracts.
*
* @param _issuer The address of the issuing account
* @param _quantity The number of sets to attribute to issuer
*/
function mint(
address _issuer,
uint _quantity
)
external
isCore
{
// Update token balance of the issuer
balances[_issuer] = balances[_issuer].add(_quantity);
// Update the total supply of the set token
totalSupply_ = totalSupply_.add(_quantity);
}
/*
* Burn set token for given address.
* Can only be called by authorized contracts.
*
* @param _from The address of the redeeming account
* @param _quantity The number of sets to burn from redeemer
*/
function burn(
address _from,
uint _quantity
)
external
isCore
isPositive(_quantity)
{
require(balances[_from] >= _quantity);
balances[_from] = balances[_from].sub(_quantity);
totalSupply_ = totalSupply_.sub(_quantity);
emit Transfer(_from, address(0), _quantity);
}
/* ============ Getters ============ */
function getComponents()
public
view
returns(address[])
{
address[] memory componentAddresses = new address[](components.length);
for (uint16 i = 0; i < components.length; i++) {
componentAddresses[i] = components[i].address_;
}
return componentAddresses;
}
function getUnits()
public
view
returns(uint[])
{
uint[] memory units = new uint[](components.length);
for (uint16 i = 0; i < components.length; i++) {
units[i] = components[i].unit_;
}
return units;
}
/* ============ Transfer Overrides ============ */
function transfer(
address _to,
uint256 _value
)
public
isValidDestination(_to)
returns (bool)
{
return super.transfer(_to, _value);
}
function transferFrom(
address _from,
address _to,
uint256 _value
)
public
isValidDestination(_to)
returns (bool)
{
return super.transferFrom(_from, _to, _value);
}
/* ============ Private Helpers ============ */
function tokenIsComponent(
address _tokenAddress
)
view
internal
returns (bool)
{
return isComponent[keccak256(abi.encodePacked(_tokenAddress))];
}
}