/
Indexer.sol
308 lines (264 loc) · 10.1 KB
/
Indexer.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
/*
Copyright 2020 Swap Holdings 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.
*/
pragma solidity 0.5.12;
pragma experimental ABIEncoderV2;
import "./Index.sol";
import "@airswap/indexer/contracts/interfaces/IIndexer.sol";
import "@airswap/indexer/contracts/interfaces/ILocatorWhitelist.sol";
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
/**
* @title Indexer: A Collection of Index contracts by Token Pair
*/
contract Indexer is IIndexer, Ownable {
// Token to be used for staking (ERC-20)
IERC20 public stakingToken;
// Mapping of signer token to sender token to protocol type to index
mapping (address => mapping (address => mapping (bytes2 => Index))) public indexes;
// The whitelist contract for checking whether a peer is whitelisted per peer type
mapping (bytes2 => address) public locatorWhitelists;
// Mapping of token address to boolean
mapping (address => bool) public tokenBlacklist;
/**
* @notice Contract Constructor
* @param indexerStakingToken address
*/
constructor(
address indexerStakingToken
) public {
stakingToken = IERC20(indexerStakingToken);
}
/**
* @notice Modifier to check an index exists
*/
modifier indexExists(address signerToken, address senderToken, bytes2 protocol) {
require(indexes[signerToken][senderToken][protocol] != Index(0),
"INDEX_DOES_NOT_EXIST");
_;
}
/**
* @notice Set the address of an ILocatorWhitelist to use
* @dev Allows removal of locatorWhitelist by passing 0x0
* @param protocol bytes2 Protocol type for locators
* @param newLocatorWhitelist address Locator whitelist
*/
function setLocatorWhitelist(
bytes2 protocol,
address newLocatorWhitelist
) external onlyOwner {
locatorWhitelists[protocol] = newLocatorWhitelist;
}
/**
* @notice Create an Index (List of Locators for a Token Pair)
* @dev Deploys a new Index contract and stores the address. If the Index already
* @dev exists, returns its address, and does not emit a CreateIndex event
* @param signerToken address Signer token for the Index
* @param senderToken address Sender token for the Index
* @param protocol bytes2 Protocol type for locators in Index
*/
function createIndex(
address signerToken,
address senderToken,
bytes2 protocol
) external returns (address) {
// If the Index does not exist, create it.
if (indexes[signerToken][senderToken][protocol] == Index(0)) {
// Create a new Index contract for the token pair.
indexes[signerToken][senderToken][protocol] = new Index();
emit CreateIndex(signerToken, senderToken, protocol, address(indexes[signerToken][senderToken][protocol]));
}
// Return the address of the Index contract.
return address(indexes[signerToken][senderToken][protocol]);
}
/**
* @notice Add a Token to the Blacklist
* @param token address Token to blacklist
*/
function addTokenToBlacklist(
address token
) external onlyOwner {
if (!tokenBlacklist[token]) {
tokenBlacklist[token] = true;
emit AddTokenToBlacklist(token);
}
}
/**
* @notice Remove a Token from the Blacklist
* @param token address Token to remove from the blacklist
*/
function removeTokenFromBlacklist(
address token
) external onlyOwner {
if (tokenBlacklist[token]) {
tokenBlacklist[token] = false;
emit RemoveTokenFromBlacklist(token);
}
}
/**
* @notice Set an Intent to Trade
* @dev Requires approval to transfer staking token for sender
*
* @param signerToken address Signer token of the Index being staked
* @param senderToken address Sender token of the Index being staked
* @param protocol bytes2 Protocol type for locator in Intent
* @param stakingAmount uint256 Amount being staked
* @param locator bytes32 Locator of the staker
*/
function setIntent(
address signerToken,
address senderToken,
bytes2 protocol,
uint256 stakingAmount,
bytes32 locator
) external indexExists(signerToken, senderToken, protocol) {
// If whitelist set, ensure the locator is valid.
if (locatorWhitelists[protocol] != address(0)) {
require(ILocatorWhitelist(locatorWhitelists[protocol]).has(locator),
"LOCATOR_NOT_WHITELISTED");
}
// Ensure neither of the tokens are blacklisted.
require(!tokenBlacklist[signerToken] && !tokenBlacklist[senderToken],
"PAIR_IS_BLACKLISTED");
bool notPreviouslySet = (indexes[signerToken][senderToken][protocol].getLocator(msg.sender) == bytes32(0));
if (notPreviouslySet) {
// Only transfer for staking if stakingAmount is set.
if (stakingAmount > 0) {
// Transfer the stakingAmount for staking.
require(stakingToken.transferFrom(msg.sender, address(this), stakingAmount),
"UNABLE_TO_STAKE");
}
// Set the locator on the index.
indexes[signerToken][senderToken][protocol].setLocator(msg.sender, stakingAmount, locator);
emit Stake(msg.sender, signerToken, senderToken, protocol, stakingAmount);
} else {
uint256 oldStake = indexes[signerToken][senderToken][protocol].getScore(msg.sender);
_updateIntent(msg.sender, signerToken, senderToken, protocol, stakingAmount, locator, oldStake);
}
}
/**
* @notice Unset an Intent to Trade
* @dev Users are allowed to unstake from blacklisted indexes
*
* @param signerToken address Signer token of the Index being unstaked
* @param senderToken address Sender token of the Index being staked
* @param protocol bytes2 Protocol type for locators in Intent
*/
function unsetIntent(
address signerToken,
address senderToken,
bytes2 protocol
) external {
_unsetIntent(msg.sender, signerToken, senderToken, protocol);
}
/**
* @notice Get the locators of those trading a token pair
* @dev Users are allowed to unstake from blacklisted indexes
*
* @param signerToken address Signer token of the trading pair
* @param senderToken address Sender token of the trading pair
* @param protocol bytes2 Protocol type for locators in Intent
* @param cursor address Address to start from
* @param limit uint256 Total number of locators to return
* @return bytes32[] List of locators
* @return uint256[] List of scores corresponding to locators
* @return address The next cursor to provide for pagination
*/
function getLocators(
address signerToken,
address senderToken,
bytes2 protocol,
address cursor,
uint256 limit
) external view returns (
bytes32[] memory locators,
uint256[] memory scores,
address nextCursor
) {
// Ensure neither token is blacklisted.
if (tokenBlacklist[signerToken] || tokenBlacklist[senderToken]) {
return (new bytes32[](0), new uint256[](0), address(0));
}
// Ensure the index exists.
if (indexes[signerToken][senderToken][protocol] == Index(0)) {
return (new bytes32[](0), new uint256[](0), address(0));
}
return indexes[signerToken][senderToken][protocol].getLocators(cursor, limit);
}
/**
* @notice Gets the Stake Amount for a User
* @param user address User who staked
* @param signerToken address Signer token the user staked on
* @param senderToken address Sender token the user staked on
* @param protocol bytes2 Protocol type for locators in Intent
* @return uint256 Amount the user staked
*/
function getStakedAmount(
address user,
address signerToken,
address senderToken,
bytes2 protocol
) public view returns (uint256 stakedAmount) {
if (indexes[signerToken][senderToken][protocol] == Index(0)) {
return 0;
}
// Return the score, equivalent to the stake amount.
return indexes[signerToken][senderToken][protocol].getScore(user);
}
function _updateIntent(
address user,
address signerToken,
address senderToken,
bytes2 protocol,
uint256 newAmount,
bytes32 newLocator,
uint256 oldAmount
) internal {
// If the new stake is bigger, collect the difference.
if (oldAmount < newAmount) {
// Note: SafeMath not required due to the inequality check above
require(stakingToken.transferFrom(user, address(this), newAmount - oldAmount),
"UNABLE_TO_STAKE");
}
// If the old stake is bigger, return the excess.
if (newAmount < oldAmount) {
// Note: SafeMath not required due to the inequality check above
require(stakingToken.transfer(user, oldAmount - newAmount));
}
// Update their intent.
indexes[signerToken][senderToken][protocol].updateLocator(user, newAmount, newLocator);
emit Stake(user, signerToken, senderToken, protocol, newAmount);
}
/**
* @notice Unset intents and return staked tokens
* @param user address Address of the user who staked
* @param signerToken address Signer token of the trading pair
* @param senderToken address Sender token of the trading pair
* @param protocol bytes2 Protocol type for locators in Intent
*/
function _unsetIntent(
address user,
address signerToken,
address senderToken,
bytes2 protocol
) internal indexExists(signerToken, senderToken, protocol) {
// Get the score for the user.
uint256 score = indexes[signerToken][senderToken][protocol].getScore(user);
// Unset the locator on the index.
indexes[signerToken][senderToken][protocol].unsetLocator(user);
if (score > 0) {
// Return the staked tokens. Reverts on failure.
require(stakingToken.transfer(user, score));
}
emit Unstake(user, signerToken, senderToken, protocol, score);
}
}