/
UniswapExchange.sol
613 lines (559 loc) · 26.7 KB
/
UniswapExchange.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
pragma solidity ^0.5.0;
import "../tokens/ERC20.sol";
import "../interfaces/IERC20.sol";
import "../interfaces/IUniswapFactory.sol";
import "../interfaces/IUniswapExchange.sol";
contract UniswapExchange is ERC20 {
/***********************************|
| Variables && Events |
|__________________________________*/
// Variables
bytes32 public name; // Uniswap V1
bytes32 public symbol; // UNI-V1
uint256 public decimals; // 18
IERC20 token; // address of the ERC20 token traded on this contract
IUniswapFactory factory; // interface for the factory that created this contract
// Events
event TokenPurchase(address indexed buyer, uint256 indexed eth_sold, uint256 indexed tokens_bought);
event EthPurchase(address indexed buyer, uint256 indexed tokens_sold, uint256 indexed eth_bought);
event AddLiquidity(address indexed provider, uint256 indexed eth_amount, uint256 indexed token_amount);
event RemoveLiquidity(address indexed provider, uint256 indexed eth_amount, uint256 indexed token_amount);
/***********************************|
| Constsructor |
|__________________________________*/
/**
* @dev This function acts as a contract constructor which is not currently supported in contracts deployed
* using create_with_code_of(). It is called once by the factory during contract creation.
*/
function setup(address token_addr) public {
require(
address(factory) == address(0) && address(token) == address(0) && token_addr != address(0),
"INVALID_ADDRESS"
);
factory = IUniswapFactory(msg.sender);
token = IERC20(token_addr);
name = 0x556e697377617020563100000000000000000000000000000000000000000000;
symbol = 0x554e492d56310000000000000000000000000000000000000000000000000000;
decimals = 18;
}
/***********************************|
| Exchange Functions |
|__________________________________*/
/**
* @notice Convert ETH to Tokens.
* @dev User specifies exact input (msg.value).
* @dev User cannot specify minimum output or deadline.
*/
function () external payable {
ethToTokenInput(msg.value, 1, block.timestamp, msg.sender, msg.sender);
}
/**
* @dev Pricing function for converting between ETH && Tokens.
* @param input_amount Amount of ETH or Tokens being sold.
* @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
* @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
* @return Amount of ETH or Tokens bought.
*/
function getInputPrice(uint256 input_amount, uint256 input_reserve, uint256 output_reserve) public view returns (uint256) {
require(input_reserve > 0 && output_reserve > 0, "INVALID_VALUE");
uint256 input_amount_with_fee = input_amount.mul(997);
uint256 numerator = input_amount_with_fee.mul(output_reserve);
uint256 denominator = input_reserve.mul(1000).add(input_amount_with_fee);
return numerator / denominator;
}
/**
* @dev Pricing function for converting between ETH && Tokens.
* @param output_amount Amount of ETH or Tokens being bought.
* @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
* @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
* @return Amount of ETH or Tokens sold.
*/
function getOutputPrice(uint256 output_amount, uint256 input_reserve, uint256 output_reserve) public view returns (uint256) {
require(input_reserve > 0 && output_reserve > 0);
uint256 numerator = input_reserve.mul(output_amount).mul(1000);
uint256 denominator = (output_reserve.sub(output_amount)).mul(997);
return (numerator / denominator).add(1);
}
function ethToTokenInput(uint256 eth_sold, uint256 min_tokens, uint256 deadline, address buyer, address recipient) private returns (uint256) {
require(deadline >= block.timestamp && eth_sold > 0 && min_tokens > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 tokens_bought = getInputPrice(eth_sold, address(this).balance.sub(eth_sold), token_reserve);
require(tokens_bought >= min_tokens);
require(token.transfer(recipient, tokens_bought));
emit TokenPurchase(buyer, eth_sold, tokens_bought);
return tokens_bought;
}
/**
* @notice Convert ETH to Tokens.
* @dev User specifies exact input (msg.value) && minimum output.
* @param min_tokens Minimum Tokens bought.
* @param deadline Time after which this transaction can no longer be executed.
* @return Amount of Tokens bought.
*/
function ethToTokenSwapInput(uint256 min_tokens, uint256 deadline) public payable returns (uint256) {
return ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, msg.sender);
}
/**
* @notice Convert ETH to Tokens && transfers Tokens to recipient.
* @dev User specifies exact input (msg.value) && minimum output
* @param min_tokens Minimum Tokens bought.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output Tokens.
* @return Amount of Tokens bought.
*/
function ethToTokenTransferInput(uint256 min_tokens, uint256 deadline, address recipient) public payable returns(uint256) {
require(recipient != address(this) && recipient != address(0));
return ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, recipient);
}
function ethToTokenOutput(uint256 tokens_bought, uint256 max_eth, uint256 deadline, address payable buyer, address recipient) private returns (uint256) {
require(deadline >= block.timestamp && tokens_bought > 0 && max_eth > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_sold = getOutputPrice(tokens_bought, address(this).balance.sub(max_eth), token_reserve);
// Throws if eth_sold > max_eth
uint256 eth_refund = max_eth.sub(eth_sold);
if (eth_refund > 0) {
buyer.transfer(eth_refund);
}
require(token.transfer(recipient, tokens_bought));
emit TokenPurchase(buyer, eth_sold, tokens_bought);
return eth_sold;
}
/**
* @notice Convert ETH to Tokens.
* @dev User specifies maximum input (msg.value) && exact output.
* @param tokens_bought Amount of tokens bought.
* @param deadline Time after which this transaction can no longer be executed.
* @return Amount of ETH sold.
*/
function ethToTokenSwapOutput(uint256 tokens_bought, uint256 deadline) public payable returns(uint256) {
return ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, msg.sender);
}
/**
* @notice Convert ETH to Tokens && transfers Tokens to recipient.
* @dev User specifies maximum input (msg.value) && exact output.
* @param tokens_bought Amount of tokens bought.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output Tokens.
* @return Amount of ETH sold.
*/
function ethToTokenTransferOutput(uint256 tokens_bought, uint256 deadline, address recipient) public payable returns (uint256) {
require(recipient != address(this) && recipient != address(0));
return ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, recipient);
}
function tokenToEthInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline, address buyer, address payable recipient) private returns (uint256) {
require(deadline >= block.timestamp && tokens_sold > 0 && min_eth > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_bought = getInputPrice(tokens_sold, token_reserve, address(this).balance);
uint256 wei_bought = eth_bought;
require(wei_bought >= min_eth);
recipient.transfer(wei_bought);
require(token.transferFrom(buyer, address(this), tokens_sold));
emit EthPurchase(buyer, tokens_sold, wei_bought);
return wei_bought;
}
/**
* @notice Convert Tokens to ETH.
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_eth Minimum ETH purchased.
* @param deadline Time after which this transaction can no longer be executed.
* @return Amount of ETH bought.
*/
function tokenToEthSwapInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline) public returns (uint256) {
return tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, msg.sender);
}
/**
* @notice Convert Tokens to ETH && transfers ETH to recipient.
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_eth Minimum ETH purchased.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @return Amount of ETH bought.
*/
function tokenToEthTransferInput(uint256 tokens_sold, uint256 min_eth, uint256 deadline, address payable recipient) public returns (uint256) {
require(recipient != address(this) && recipient != address(0));
return tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, recipient);
}
function tokenToEthOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline, address buyer, address payable recipient) private returns (uint256) {
require(deadline >= block.timestamp && eth_bought > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 tokens_sold = getOutputPrice(eth_bought, token_reserve, address(this).balance);
// tokens sold is always > 0
require(max_tokens >= tokens_sold);
recipient.transfer(eth_bought);
require(token.transferFrom(buyer, address(this), tokens_sold));
emit EthPurchase(buyer, tokens_sold, eth_bought);
return tokens_sold;
}
/**
* @notice Convert Tokens to ETH.
* @dev User specifies maximum input && exact output.
* @param eth_bought Amount of ETH purchased.
* @param max_tokens Maximum Tokens sold.
* @param deadline Time after which this transaction can no longer be executed.
* @return Amount of Tokens sold.
*/
function tokenToEthSwapOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline) public returns (uint256) {
return tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, msg.sender);
}
/**
* @notice Convert Tokens to ETH && transfers ETH to recipient.
* @dev User specifies maximum input && exact output.
* @param eth_bought Amount of ETH purchased.
* @param max_tokens Maximum Tokens sold.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @return Amount of Tokens sold.
*/
function tokenToEthTransferOutput(uint256 eth_bought, uint256 max_tokens, uint256 deadline, address payable recipient) public returns (uint256) {
require(recipient != address(this) && recipient != address(0));
return tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, recipient);
}
function tokenToTokenInput(
uint256 tokens_sold,
uint256 min_tokens_bought,
uint256 min_eth_bought,
uint256 deadline,
address buyer,
address recipient,
address payable exchange_addr)
private returns (uint256)
{
require(deadline >= block.timestamp && tokens_sold > 0 && min_tokens_bought > 0 && min_eth_bought > 0);
require(exchange_addr != address(this) && exchange_addr != address(0));
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_bought = getInputPrice(tokens_sold, token_reserve, address(this).balance);
uint256 wei_bought = eth_bought;
require(wei_bought >= min_eth_bought);
require(token.transferFrom(buyer, address(this), tokens_sold));
uint256 tokens_bought = IUniswapExchange(exchange_addr).ethToTokenTransferInput.value(wei_bought)(min_tokens_bought, deadline, recipient);
emit EthPurchase(buyer, tokens_sold, wei_bought);
return tokens_bought;
}
/**
* @notice Convert Tokens (token) to Tokens (token_addr).
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_tokens_bought Minimum Tokens (token_addr) purchased.
* @param min_eth_bought Minimum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param token_addr The address of the token being purchased.
* @return Amount of Tokens (token_addr) bought.
*/
function tokenToTokenSwapInput(
uint256 tokens_sold,
uint256 min_tokens_bought,
uint256 min_eth_bought,
uint256 deadline,
address token_addr)
public returns (uint256)
{
address payable exchange_addr = factory.getExchange(token_addr);
return tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (token_addr) && transfers
* Tokens (token_addr) to recipient.
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_tokens_bought Minimum Tokens (token_addr) purchased.
* @param min_eth_bought Minimum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @param token_addr The address of the token being purchased.
* @return Amount of Tokens (token_addr) bought.
*/
function tokenToTokenTransferInput(
uint256 tokens_sold,
uint256 min_tokens_bought,
uint256 min_eth_bought,
uint256 deadline,
address recipient,
address token_addr)
public returns (uint256)
{
address payable exchange_addr = factory.getExchange(token_addr);
return tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr);
}
function tokenToTokenOutput(
uint256 tokens_bought,
uint256 max_tokens_sold,
uint256 max_eth_sold,
uint256 deadline,
address buyer,
address recipient,
address payable exchange_addr)
private returns (uint256)
{
require(deadline >= block.timestamp && (tokens_bought > 0 && max_eth_sold > 0));
require(exchange_addr != address(this) && exchange_addr != address(0));
uint256 eth_bought = IUniswapExchange(exchange_addr).getEthToTokenOutputPrice(tokens_bought);
uint256 token_reserve = token.balanceOf(address(this));
uint256 tokens_sold = getOutputPrice(eth_bought, token_reserve, address(this).balance);
// tokens sold is always > 0
require(max_tokens_sold >= tokens_sold && max_eth_sold >= eth_bought);
require(token.transferFrom(buyer, address(this), tokens_sold));
uint256 eth_sold = IUniswapExchange(exchange_addr).ethToTokenTransferOutput.value(eth_bought)(tokens_bought, deadline, recipient);
emit EthPurchase(buyer, tokens_sold, eth_bought);
return tokens_sold;
}
/**
* @notice Convert Tokens (token) to Tokens (token_addr).
* @dev User specifies maximum input && exact output.
* @param tokens_bought Amount of Tokens (token_addr) bought.
* @param max_tokens_sold Maximum Tokens (token) sold.
* @param max_eth_sold Maximum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param token_addr The address of the token being purchased.
* @return Amount of Tokens (token) sold.
*/
function tokenToTokenSwapOutput(
uint256 tokens_bought,
uint256 max_tokens_sold,
uint256 max_eth_sold,
uint256 deadline,
address token_addr)
public returns (uint256)
{
address payable exchange_addr = factory.getExchange(token_addr);
return tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (token_addr) && transfers
* Tokens (token_addr) to recipient.
* @dev User specifies maximum input && exact output.
* @param tokens_bought Amount of Tokens (token_addr) bought.
* @param max_tokens_sold Maximum Tokens (token) sold.
* @param max_eth_sold Maximum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @param token_addr The address of the token being purchased.
* @return Amount of Tokens (token) sold.
*/
function tokenToTokenTransferOutput(
uint256 tokens_bought,
uint256 max_tokens_sold,
uint256 max_eth_sold,
uint256 deadline,
address recipient,
address token_addr)
public returns (uint256)
{
address payable exchange_addr = factory.getExchange(token_addr);
return tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (exchange_addr.token).
* @dev Allows trades through contracts that were not deployed from the same factory.
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_tokens_bought Minimum Tokens (token_addr) purchased.
* @param min_eth_bought Minimum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param exchange_addr The address of the exchange for the token being purchased.
* @return Amount of Tokens (exchange_addr.token) bought.
*/
function tokenToExchangeSwapInput(
uint256 tokens_sold,
uint256 min_tokens_bought,
uint256 min_eth_bought,
uint256 deadline,
address payable exchange_addr)
public returns (uint256)
{
return tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (exchange_addr.token) && transfers
* Tokens (exchange_addr.token) to recipient.
* @dev Allows trades through contracts that were not deployed from the same factory.
* @dev User specifies exact input && minimum output.
* @param tokens_sold Amount of Tokens sold.
* @param min_tokens_bought Minimum Tokens (token_addr) purchased.
* @param min_eth_bought Minimum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @param exchange_addr The address of the exchange for the token being purchased.
* @return Amount of Tokens (exchange_addr.token) bought.
*/
function tokenToExchangeTransferInput(
uint256 tokens_sold,
uint256 min_tokens_bought,
uint256 min_eth_bought,
uint256 deadline,
address recipient,
address payable exchange_addr)
public returns (uint256)
{
require(recipient != address(this));
return tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (exchange_addr.token).
* @dev Allows trades through contracts that were not deployed from the same factory.
* @dev User specifies maximum input && exact output.
* @param tokens_bought Amount of Tokens (token_addr) bought.
* @param max_tokens_sold Maximum Tokens (token) sold.
* @param max_eth_sold Maximum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param exchange_addr The address of the exchange for the token being purchased.
* @return Amount of Tokens (token) sold.
*/
function tokenToExchangeSwapOutput(
uint256 tokens_bought,
uint256 max_tokens_sold,
uint256 max_eth_sold,
uint256 deadline,
address payable exchange_addr)
public returns (uint256)
{
return tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr);
}
/**
* @notice Convert Tokens (token) to Tokens (exchange_addr.token) && transfers
* Tokens (exchange_addr.token) to recipient.
* @dev Allows trades through contracts that were not deployed from the same factory.
* @dev User specifies maximum input && exact output.
* @param tokens_bought Amount of Tokens (token_addr) bought.
* @param max_tokens_sold Maximum Tokens (token) sold.
* @param max_eth_sold Maximum ETH purchased as intermediary.
* @param deadline Time after which this transaction can no longer be executed.
* @param recipient The address that receives output ETH.
* @param exchange_addr The address of the exchange for the token being purchased.
* @return Amount of Tokens (token) sold.
*/
function tokenToExchangeTransferOutput(
uint256 tokens_bought,
uint256 max_tokens_sold,
uint256 max_eth_sold,
uint256 deadline,
address recipient,
address payable exchange_addr)
public returns (uint256)
{
require(recipient != address(this));
return tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr);
}
/***********************************|
| Getter Functions |
|__________________________________*/
/**
* @notice Public price function for ETH to Token trades with an exact input.
* @param eth_sold Amount of ETH sold.
* @return Amount of Tokens that can be bought with input ETH.
*/
function getEthToTokenInputPrice(uint256 eth_sold) public view returns (uint256) {
require(eth_sold > 0);
uint256 token_reserve = token.balanceOf(address(this));
return getInputPrice(eth_sold, address(this).balance, token_reserve);
}
/**
* @notice Public price function for ETH to Token trades with an exact output.
* @param tokens_bought Amount of Tokens bought.
* @return Amount of ETH needed to buy output Tokens.
*/
function getEthToTokenOutputPrice(uint256 tokens_bought) public view returns (uint256) {
require(tokens_bought > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_sold = getOutputPrice(tokens_bought, address(this).balance, token_reserve);
return eth_sold;
}
/**
* @notice Public price function for Token to ETH trades with an exact input.
* @param tokens_sold Amount of Tokens sold.
* @return Amount of ETH that can be bought with input Tokens.
*/
function getTokenToEthInputPrice(uint256 tokens_sold) public view returns (uint256) {
require(tokens_sold > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_bought = getInputPrice(tokens_sold, token_reserve, address(this).balance);
return eth_bought;
}
/**
* @notice Public price function for Token to ETH trades with an exact output.
* @param eth_bought Amount of output ETH.
* @return Amount of Tokens needed to buy output ETH.
*/
function getTokenToEthOutputPrice(uint256 eth_bought) public view returns (uint256) {
require(eth_bought > 0);
uint256 token_reserve = token.balanceOf(address(this));
return getOutputPrice(eth_bought, token_reserve, address(this).balance);
}
/**
* @return Address of Token that is sold on this exchange.
*/
function tokenAddress() public view returns (address) {
return address(token);
}
/**
* @return Address of factory that created this exchange.
*/
function factoryAddress() public view returns (address) {
return address(factory);
}
/***********************************|
| Liquidity Functions |
|__________________________________*/
/**
* @notice Deposit ETH && Tokens (token) at current ratio to mint UNI tokens.
* @dev min_liquidity does nothing when total UNI supply is 0.
* @param min_liquidity Minimum number of UNI sender will mint if total UNI supply is greater than 0.
* @param max_tokens Maximum number of tokens deposited. Deposits max amount if total UNI supply is 0.
* @param deadline Time after which this transaction can no longer be executed.
* @return The amount of UNI minted.
*/
function addLiquidity(uint256 min_liquidity, uint256 max_tokens, uint256 deadline) public payable returns (uint256) {
require(deadline > block.timestamp && max_tokens > 0 && msg.value > 0, 'UniswapExchange#addLiquidity: INVALID_ARGUMENT');
uint256 total_liquidity = _totalSupply;
if (total_liquidity > 0) {
require(min_liquidity > 0);
uint256 eth_reserve = address(this).balance.sub(msg.value);
uint256 token_reserve = token.balanceOf(address(this));
uint256 token_amount = (msg.value.mul(token_reserve) / eth_reserve).add(1);
uint256 liquidity_minted = msg.value.mul(total_liquidity) / eth_reserve;
require(max_tokens >= token_amount && liquidity_minted >= min_liquidity);
_balances[msg.sender] = _balances[msg.sender].add(liquidity_minted);
_totalSupply = total_liquidity.add(liquidity_minted);
require(token.transferFrom(msg.sender, address(this), token_amount));
emit AddLiquidity(msg.sender, msg.value, token_amount);
emit Transfer(address(0), msg.sender, liquidity_minted);
return liquidity_minted;
} else {
require(address(factory) != address(0) && address(token) != address(0) && msg.value >= 1000000000, "INVALID_VALUE");
require(factory.getExchange(address(token)) == address(this));
uint256 token_amount = max_tokens;
uint256 initial_liquidity = address(this).balance;
_totalSupply = initial_liquidity;
_balances[msg.sender] = initial_liquidity;
require(token.transferFrom(msg.sender, address(this), token_amount));
emit AddLiquidity(msg.sender, msg.value, token_amount);
emit Transfer(address(0), msg.sender, initial_liquidity);
return initial_liquidity;
}
}
/**
* @dev Burn UNI tokens to withdraw ETH && Tokens at current ratio.
* @param amount Amount of UNI burned.
* @param min_eth Minimum ETH withdrawn.
* @param min_tokens Minimum Tokens withdrawn.
* @param deadline Time after which this transaction can no longer be executed.
* @return The amount of ETH && Tokens withdrawn.
*/
function removeLiquidity(uint256 amount, uint256 min_eth, uint256 min_tokens, uint256 deadline) public returns (uint256, uint256) {
require(amount > 0 && deadline > block.timestamp && min_eth > 0 && min_tokens > 0);
uint256 total_liquidity = _totalSupply;
require(total_liquidity > 0);
uint256 token_reserve = token.balanceOf(address(this));
uint256 eth_amount = amount.mul(address(this).balance) / total_liquidity;
uint256 token_amount = amount.mul(token_reserve) / total_liquidity;
require(eth_amount >= min_eth && token_amount >= min_tokens);
_balances[msg.sender] = _balances[msg.sender].sub(amount);
_totalSupply = total_liquidity.sub(amount);
msg.sender.transfer(eth_amount);
require(token.transfer(msg.sender, token_amount));
emit RemoveLiquidity(msg.sender, eth_amount, token_amount);
emit Transfer(msg.sender, address(0), amount);
return (eth_amount, token_amount);
}
}