-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDecentralizedExchanges_0x72f60eca0db6811274215694129661151f97982e.sol
289 lines (242 loc) · 9.23 KB
/
DecentralizedExchanges_0x72f60eca0db6811274215694129661151f97982e.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
pragma solidity ^0.4.24;
// File: openzeppelin-solidity\contracts\math\SafeMath.sol
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
// File: openzeppelin-solidity\contracts\token\ERC20\ERC20Basic.sol
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* See https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
// File: openzeppelin-solidity\contracts\token\ERC20\ERC20.sol
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender)
public view returns (uint256);
function transferFrom(address from, address to, uint256 value)
public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
// File: openzeppelin-solidity\contracts\token\ERC20\SafeERC20.sol
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure.
* To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
function safeTransfer(ERC20Basic token, address to, uint256 value) internal {
require(token.transfer(to, value));
}
function safeTransferFrom(
ERC20 token,
address from,
address to,
uint256 value
)
internal
{
require(token.transferFrom(from, to, value));
}
function safeApprove(ERC20 token, address spender, uint256 value) internal {
require(token.approve(spender, value));
}
}
// File: contracts\DecentralizedExchanges2.sol
contract SpecialERC20 {
function transfer(address to, uint256 value) public;
}
contract DecentralizedExchanges {
using SafeMath for uint;
using SafeERC20 for ERC20;
string public name = "DecentralizedExchanges";
event Order(bytes32 hash);
event Trade(bytes32 hash, address seller, address token, uint amount, address purchaser, uint eth);
event Cancel(bytes32 hash, uint amount, bool isSell);
struct OrderInfo {
bool isSell;
bool isSpecialERC20;
uint eth;
uint amount;
uint expires;
uint nonce;
uint createdAt;
uint fill;
address token;
address[] limitUser;
address owner;
}
mapping (bytes32 => OrderInfo) public orderInfos;
mapping (address => bytes32[]) public userOrders;
function getOrderInfo(bytes32 hash) public view returns (bool, uint, address, uint, uint, uint, address[], uint, address, uint, bool) {
OrderInfo storage info = orderInfos[hash];
return (info.isSell, info.eth, info.token, info.amount, info.expires, info.nonce, info.limitUser, info.createdAt, info.owner, info.fill, info.isSpecialERC20);
}
// 创建买单,用eth买token
function createPurchaseOrder(bool isSpecialERC20, uint eth, address token, uint amount, uint expires, address[] seller, uint nonce) payable public {
require(msg.value >= eth);
bytes32 hash = sha256(abi.encodePacked(this, eth, token, amount, expires, seller, nonce, msg.sender, now));
orderInfos[hash] = OrderInfo(false, isSpecialERC20, eth, amount, expires, nonce, now, 0, token, seller, msg.sender);
for (uint i = 0; i < userOrders[msg.sender].length; i++) {
require(userOrders[msg.sender][i] != hash);
}
userOrders[msg.sender].push(hash);
emit Order(hash);
}
// 创建卖单,卖token得eth
function createSellOrder(bool isSpecialERC20, address token, uint amount, uint eth, uint expires, address[] purchaser, uint nonce) public {
ERC20(token).safeTransferFrom(msg.sender, this, amount);
bytes32 hash = sha256(abi.encodePacked(this, eth, token, amount, expires, purchaser, nonce, msg.sender, now));
orderInfos[hash] = OrderInfo(true, isSpecialERC20, eth, amount, expires, nonce, now, 0, token, purchaser, msg.sender);
for (uint i = 0; i < userOrders[msg.sender].length; i++) {
require(userOrders[msg.sender][i] != hash);
}
userOrders[msg.sender].push(hash);
emit Order(hash);
}
function cancelOrder(bytes32 hash) public {
OrderInfo storage info = orderInfos[hash];
require(info.owner == msg.sender);
if (info.isSell) {
if (info.fill < info.amount) {
uint amount = info.amount;
uint remain = amount - info.fill;
info.fill = info.amount;
if (info.isSpecialERC20) {
SpecialERC20(info.token).transfer(msg.sender, remain);
} else {
ERC20(info.token).transfer(msg.sender, remain);
}
emit Cancel(hash, remain, info.isSell);
} else {
emit Cancel(hash, 0, info.isSell);
}
} else {
if (info.fill < info.eth) {
uint eth = info.eth;
remain = eth - info.fill;
info.fill = info.eth;
msg.sender.transfer(eth);
emit Cancel(hash, remain, info.isSell);
} else {
emit Cancel(hash, 0, info.isSell);
}
}
}
// 卖token,针对创建的买单
function sell(bytes32 hash, uint amount) public {
OrderInfo storage info = orderInfos[hash];
bool find = false;
if (info.limitUser.length > 0) {
for (uint i = 0; i < info.limitUser.length; i++) {
if (info.limitUser[i] == msg.sender) {
find = true;
break;
}
}
require(find);
}
// 确保订单还有剩余eth
require(info.fill < info.eth);
require(info.expires >= now);
uint remain = info.eth - info.fill;
uint remainAmount = remain.mul(info.amount).div(info.eth);
uint tradeAmount = remainAmount < amount ? remainAmount : amount;
// token从卖家转到合约
ERC20(info.token).safeTransferFrom(msg.sender, this, tradeAmount);
uint total = info.eth.mul(tradeAmount).div(info.amount);
msg.sender.transfer(total);
// token从合约转到买家
ERC20(info.token).transfer(info.owner, tradeAmount);
info.fill = info.fill.add(total);
emit Trade(hash, msg.sender, info.token, tradeAmount, info.owner, total);
}
// 买token,针对创建的卖单
function purchase(bytes32 hash, uint amount) payable public {
OrderInfo storage info = orderInfos[hash];
bool find = false;
if (info.limitUser.length > 0) {
for (uint i = 0; i < info.limitUser.length; i++) {
if (info.limitUser[i] == msg.sender) {
find = true;
break;
}
}
require(find);
}
// 确保订单还有剩余token
require(info.fill < info.amount);
require(info.expires >= now);
uint remainAmount = info.amount - info.fill;
uint tradeAmount = remainAmount < amount ? remainAmount : amount;
uint total = info.eth.mul(tradeAmount).div(info.amount);
require(msg.value >= total);
if (msg.value > total) { // 多余的eth转回去
msg.sender.transfer(msg.value - total);
}
info.owner.transfer(total);
if (info.isSpecialERC20) {
SpecialERC20(info.token).transfer(msg.sender, tradeAmount);
} else {
ERC20(info.token).transfer(msg.sender, tradeAmount);
}
info.fill = info.fill.add(tradeAmount);
emit Trade(hash, info.owner, info.token, tradeAmount, msg.sender, total);
}
}