-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderingLatch.sol
366 lines (286 loc) · 12.8 KB
/
OrderingLatch.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
pragma solidity ^0.5.1;
// This token latch uses buy and sell orders to operate
// Follows the Tr100c protocol
contract ERC20 {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract ERC20TokenLatch {
uint64 trade_increment = 1;
uint public fee; // fee for trades
address payable public owner;
address payable public latched_contract;
mapping(uint32 => address payable) public buy_order_owners;
mapping(uint32 => uint256) public buy_order_qty;
mapping(uint32 => uint64) public buy_order_price;
uint32 public num_buy_orders = 0;
uint32 public max_buy_price_idx;
mapping(uint32 => address payable) public sell_order_owners;
mapping(uint32 => uint256) public sell_order_qty;
mapping(uint32 => uint64) public sell_order_price;
uint32 public num_sell_orders = 0;
uint32 public min_sell_price_idx;
function rmBuyOrder(uint32 idx) private {
buy_order_owners[idx]=buy_order_owners[num_buy_orders];
buy_order_qty[idx]=buy_order_qty[num_buy_orders];
buy_order_price[idx]=buy_order_price[num_buy_orders];
num_buy_orders--;
if(max_buy_price_idx==idx){
max_buy_price_idx=0;
for(uint32 i=1;i<num_buy_orders;i++){
if(buy_order_price[max_buy_price_idx]<buy_order_price[i])max_buy_price_idx=i;
}
}
}
function rmSellOrder(uint32 idx) private {
sell_order_owners[idx]=sell_order_owners[num_sell_orders];
sell_order_qty[idx]=sell_order_qty[num_sell_orders];
sell_order_price[idx]=sell_order_price[num_sell_orders];
num_sell_orders--;
if(min_sell_price_idx==idx){
min_sell_price_idx=0;
for(uint32 i=1;i<num_sell_orders;i++){
if(sell_order_price[min_sell_price_idx]>sell_order_price[i])min_sell_price_idx=i;
}
}
}
function addBuyOrder(address payable adr, uint256 qty, uint64 price) private {
buy_order_owners[num_buy_orders] = adr;
buy_order_qty[num_buy_orders] = qty;
buy_order_price[num_buy_orders] = price;
if(price>buy_order_price[max_buy_price_idx])max_buy_price_idx = num_buy_orders;
if(num_buy_orders==0)max_buy_price_idx = 0;
num_buy_orders++;
}
function addSellOrder(address payable adr, uint256 qty, uint64 price) private {
sell_order_owners[num_sell_orders] = adr;
sell_order_qty[num_sell_orders] = qty;
sell_order_price[num_sell_orders] = price;
if(price<sell_order_price[min_sell_price_idx])min_sell_price_idx = num_sell_orders;
if(num_sell_orders==0)min_sell_price_idx = 0;
num_sell_orders++;
}
function maxBuyPrice() public view returns (uint64 price){
return buy_order_price[max_buy_price_idx];
}
function minSellPrice() public view returns (uint64 price){
return sell_order_price[min_sell_price_idx];
}
function getPrice() public view returns (uint64){
if(num_sell_orders==0){
if(num_buy_orders==0)return 1000;
else return maxBuyPrice();
}else if(num_buy_orders==0) return minSellPrice();
return (minSellPrice()+maxBuyPrice())/2;
}
constructor(address payable latch) public {
latched_contract=latch;
owner = msg.sender;
fee = .0001 ether;
}
function balanceOf(address tokenOwner) public view returns (uint balance){
return ERC20(latched_contract).balanceOf(tokenOwner);
}
function totalSupply() public view returns (uint){
return ERC20(latched_contract).totalSupply();
}
function transfer(address target, uint qty) private{
ERC20(latched_contract).transfer(target, qty);
}
function getFee() public view returns (uint){
return fee;
}
function getBuyPrice() public view returns (uint64){
if(num_buy_orders>0)return maxBuyPrice()+trade_increment;
else return getPrice();
}
function getSellPrice() public view returns (uint64){
if(num_sell_orders>0)return minSellPrice()-trade_increment;
else return getPrice();
}
function getSellReturn(uint amount) public view returns (uint){ // ether for selling amount tokens
// computing fees for selling is difficult and expensive, so I'm not doing it. Not worth it.
return (getSellPrice()*amount)/10000;
}
function getBuyCost(uint amount) public view returns (uint){ // ether cost for buying amount tokens
return ((amount*getBuyPrice())/10000) + fee;
}
function buy(uint tokens)public payable{
uint cost = getBuyCost(tokens);
require(msg.value>=cost);
// handle fee and any extra funds
msg.sender.transfer(msg.value-cost);
owner.transfer(fee);
// get info needed for trading
uint64 buy_price = getBuyPrice();
uint left = tokens;
// now try to fulfill the order
for(uint32 i=0;i<num_sell_orders;i++){
if(buy_price<minSellPrice())
break; // cannot fulfill order because there is not a sell order that would satisfy
if(sell_order_price[i]<=buy_price){
// we can trade some!
if(sell_order_qty[i]>left){
// we can trade all!
sell_order_qty[i]-=left;
sell_order_owners[i].transfer((sell_order_price[i]*left)/10000);
transfer(msg.sender, left);
// send the owner any extra funds
owner.transfer(((buy_price-sell_order_price[i])*left)/10000);
// order fully fulfilled
return;
}else{
// will complete a single sell order, but buy order will have some left over
uint qty = sell_order_qty[i];
left-=qty;
sell_order_owners[i].transfer((sell_order_price[i]*qty)/10000);
transfer(msg.sender, qty);
// send the owner any extra funds
owner.transfer(((buy_price-sell_order_price[i])*qty)/10000);
// delete the order that was completed
rmSellOrder(i);
}
}
}
// if we are here then some of the order is left. Place the order in the queue.
addBuyOrder(msg.sender, left, buy_price);
}
function sell(uint tokens)public{
require(ERC20(latched_contract).allowance(msg.sender, address(this))>=tokens);
// handle fee and any extra funds
ERC20(latched_contract).transferFrom(msg.sender,address(this),tokens);
// get info needed for trading
uint64 sell_price = getSellPrice();
uint left = tokens;
// now try to fulfill the order
for(uint32 i=0;i<num_buy_orders;i++){
if(sell_price>maxBuyPrice())
break; // cannot fulfill order because there is not a buy order that would satisfy
if(buy_order_price[i]>=sell_price){
// we can trade some!
if(buy_order_qty[i]>left){
// we can trade all!
buy_order_qty[i]-=left;
transfer(buy_order_owners[i],left);
msg.sender.transfer((sell_price*left)/10000);
// send the owner any extra funds
owner.transfer(((buy_order_price[i]-sell_price)*left)/10000);
// order fully fulfilled
return;
}else{
// will complete a single sell order, but buy order will have some left over
uint qty = buy_order_qty[i];
left-=qty;
transfer(buy_order_owners[i],qty);
msg.sender.transfer((sell_price*qty)/10000);
// send the owner any extra funds
owner.transfer(((buy_order_price[i]-sell_price)*qty)/10000);
// delete the order that was completed
rmBuyOrder(i);
}
}
}
// if we are here then some of the order is left. Place the order in the queue.
addSellOrder(msg.sender, left, sell_price);
}
function placeBuyOrder(uint tokens, uint64 price10000) public payable{
uint cost = fee + ((tokens*price10000)/10000);
require(msg.value>=cost);
// handle fee and any extra funds
msg.sender.transfer(msg.value-cost);
owner.transfer(fee);
uint left = tokens;
// now try to fulfill the order
for(uint32 i=0;i<num_sell_orders;i++){
if(price10000<minSellPrice())
break; // cannot fulfill order because there is not a sell order that would satisfy
if(sell_order_price[i]<=price10000){
// we can trade some!
if(sell_order_qty[i]>left){
// we can trade all!
sell_order_qty[i]-=left;
sell_order_owners[i].transfer((sell_order_price[i]*left)/10000);
transfer(msg.sender, left);
// send the owner any extra funds
owner.transfer(((price10000-sell_order_price[i])*left)/10000);
// order fully fulfilled
return;
}else{
// will complete a single sell order, but buy order will have some left over
uint qty = sell_order_qty[i];
left-=qty;
sell_order_owners[i].transfer((sell_order_price[i]*qty)/10000);
transfer(msg.sender, qty);
// send the owner any extra funds
owner.transfer(((price10000-sell_order_price[i])*qty)/10000);
// delete the order that was completed
rmSellOrder(i);
}
}
}
// if we are here then some of the order is left. Place the order in the queue.
addBuyOrder(msg.sender, left, price10000);
}
function placeSellOrder(uint tokens, uint64 price10000) public{
require(ERC20(latched_contract).allowance(msg.sender, address(this))>=tokens);
// handle fee and any extra funds
ERC20(latched_contract).transferFrom(msg.sender,address(this),tokens);
// get info needed for trading
uint left = tokens;
// now try to fulfill the order
for(uint32 i=0;i<num_buy_orders;i++){
if(price10000>maxBuyPrice())
break; // cannot fulfill order because there is not a buy order that would satisfy
if(buy_order_price[i]>=price10000){
// we can trade some!
if(buy_order_qty[i]>left){
// we can trade all!
buy_order_qty[i]-=left;
transfer(buy_order_owners[i],left);
msg.sender.transfer((price10000*left)/10000);
// send the owner any extra funds
owner.transfer(((buy_order_price[i]-price10000)*left)/10000);
// order fully fulfilled
return;
}else{
// will complete a single sell order, but buy order will have some left over
uint qty = buy_order_qty[i];
left-=qty;
transfer(buy_order_owners[i],qty);
msg.sender.transfer((price10000*qty)/10000);
// send the owner any extra funds
owner.transfer(((buy_order_price[i]-price10000)*qty)/10000);
// delete the order that was completed
rmBuyOrder(i);
}
}
}
// if we are here then some of the order is left. Place the order in the queue.
addSellOrder(msg.sender, left, price10000);
}
function canBuy(uint amount) public pure returns (bool possible){ // returns true if this amount of token can be bought - does not account for Ethereum account balance
return true;
}
function canSell(uint amount) public pure returns (bool possible){ // returns true if this amount of token can be sold - does not account for token account balance
return true;
}
function get_tradable() public view returns (uint){
return ERC20(latched_contract).totalSupply();
}
function setFee(uint new_fee) public{
require(msg.sender==owner);
fee=new_fee;
}
function destroy() public {
require(msg.sender==owner);
require(address(this).balance<0.1 ether);
require(ERC20(latched_contract).balanceOf(address(this))==0);
selfdestruct(msg.sender);
}
}