@@ -8,14 +8,17 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
8
8
address
9
9
private constant UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
10
10
11
+ address private WETH_ADDRESS;
12
+
11
13
uint256 [] private _percentage;
12
14
13
15
constructor (uint256 [] memory percentage ) {
16
+ WETH_ADDRESS = IUniswapV2Router (UNISWAP_V2_ROUTER).WETH ();
14
17
assert (percentage.length == 2 );
15
18
_percentage = percentage;
16
19
}
17
20
18
- function percentage () public override view returns (uint256 [] memory ) {
21
+ function percentage () public override view returns (uint256 [] memory ) {
19
22
return _percentage;
20
23
}
21
24
@@ -28,20 +31,21 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
28
31
uint256 amount1 ,
29
32
address tokenAddress ,
30
33
uint256 tokenValue
31
- ) public override {
32
- require (
33
- _isValidPairToken (stableCoinAddress, tokenAddress),
34
- "Choosen token address is not in a valid pair "
35
- );
36
- _transferToMeAndCheckAllowance (
37
- tokenAddress,
38
- tokenValue,
39
- UNISWAP_V2_ROUTER
40
- );
34
+ ) public override payable {
35
+ if (tokenAddress != WETH_ADDRESS) {
36
+ _transferToMeAndCheckAllowance (
37
+ tokenAddress,
38
+ tokenValue,
39
+ UNISWAP_V2_ROUTER
40
+ );
41
+ }
42
+ uint256 realTokenValue = tokenAddress == WETH_ADDRESS
43
+ ? msg .value
44
+ : tokenValue;
41
45
uint256 stableCoinAmount = _swap (
42
46
tokenAddress,
43
47
stableCoinAddress,
44
- tokenValue ,
48
+ realTokenValue ,
45
49
address (this )
46
50
);
47
51
(uint256 return0 , uint256 return1 ) = IStableCoin (stableCoinAddress)
@@ -50,45 +54,7 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
50
54
stableCoinAddress,
51
55
pairIndex
52
56
);
53
- require (
54
- _isPumpOK (
55
- stableCoinAddress,
56
- tokenAddress,
57
- tokenValue,
58
- token0,
59
- return0,
60
- token1,
61
- return1,
62
- stableCoinAmount
63
- ),
64
- "Values are not coherent "
65
- );
66
- _flushToSender (token0, token1, stableCoinAddress);
67
- }
68
-
69
- function _isPumpOK (
70
- address stableCoinAddress ,
71
- address tokenAddress ,
72
- uint256 tokenValue ,
73
- address token0 ,
74
- uint256 return0 ,
75
- address token1 ,
76
- uint256 return1 ,
77
- uint256 stableCoinAmount
78
- ) private view returns (bool ) {
79
- IStableCoin stableCoin = IStableCoin (stableCoinAddress);
80
- uint256 cumulative = stableCoin.fromTokenToStable (
81
- tokenAddress,
82
- tokenValue
83
- );
84
- cumulative += stableCoin.fromTokenToStable (token0, return0);
85
- cumulative += stableCoin.fromTokenToStable (token1, return1);
86
- uint256 percentage = (cumulative * _percentage[0 ]) / _percentage[1 ];
87
- uint256 cumulativePlus = cumulative + percentage;
88
- uint256 cumulativeMinus = cumulative - percentage;
89
- return
90
- stableCoinAmount >= cumulativeMinus &&
91
- stableCoinAmount <= cumulativePlus;
57
+ _flushToSender (token0, token1, stableCoinAddress, tokenAddress);
92
58
}
93
59
94
60
//Earn dumping uSD - Means mint uSD then swap uSD for the chosen Uniswap Pool tokens
@@ -131,7 +97,7 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
131
97
msg .sender
132
98
);
133
99
}
134
- _flushToSender (token0, token1, stableCoinAddress);
100
+ _flushToSender (token0, token1, stableCoinAddress, address ( 0 ) );
135
101
}
136
102
137
103
function _transferTokens (
@@ -199,17 +165,23 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
199
165
function _flushToSender (
200
166
address token0 ,
201
167
address token1 ,
202
- address token2
168
+ address token2 ,
169
+ address token3
203
170
) private {
204
171
_flushToSender (token0);
205
172
_flushToSender (token1);
206
173
_flushToSender (token2);
174
+ _flushToSender (token3);
207
175
}
208
176
209
177
function _flushToSender (address tokenAddress ) private {
210
178
if (tokenAddress == address (0 )) {
211
179
return ;
212
180
}
181
+ if (tokenAddress == WETH_ADDRESS) {
182
+ payable (msg .sender ).transfer (address (this ).balance);
183
+ return ;
184
+ }
213
185
IERC20 token = IERC20 (tokenAddress);
214
186
uint256 balanceOf = token.balanceOf (address (this ));
215
187
if (balanceOf > 0 ) {
@@ -230,6 +202,15 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
230
202
address [] memory path = new address [](2 );
231
203
path[0 ] = tokenIn;
232
204
path[1 ] = tokenOut;
205
+ if (path[0 ] == WETH_ADDRESS) {
206
+ return
207
+ uniswapV2Router.swapExactETHForTokens {value: amountIn}(
208
+ uniswapV2Router.getAmountsOut (amountIn, path)[1 ],
209
+ path,
210
+ receiver,
211
+ block .timestamp + 1000
212
+ )[1 ];
213
+ }
233
214
return
234
215
uniswapV2Router.swapExactTokensForTokens (
235
216
amountIn,
@@ -239,23 +220,4 @@ contract UnifiedStableFarming is IUnifiedStableFarming {
239
220
block .timestamp + 1000
240
221
)[1 ];
241
222
}
242
-
243
- function _isValidPairToken (address stableCoinAddress , address tokenAddress )
244
- private
245
- view
246
- returns (bool )
247
- {
248
- address [] memory allowedPairs = IStableCoin (stableCoinAddress)
249
- .allowedPairs ();
250
- for (uint256 i = 0 ; i < allowedPairs.length ; i++ ) {
251
- IUniswapV2Pair pair = IUniswapV2Pair (allowedPairs[i]);
252
- if (pair.token0 () == tokenAddress) {
253
- return true ;
254
- }
255
- if (pair.token1 () == tokenAddress) {
256
- return true ;
257
- }
258
- }
259
- return false ;
260
- }
261
223
}
0 commit comments