|
| 1 | +from async_timeout import timeout |
1 | 2 | import pytest |
2 | 3 | import os |
3 | 4 | import subprocess |
|
11 | 12 | from web3 import Web3 |
12 | 13 | from web3.exceptions import NameNotFound |
13 | 14 |
|
14 | | -from uniswap import Uniswap |
15 | | -from uniswap.constants import ETH_ADDRESS |
| 15 | +from uniswap import Uniswap, token |
| 16 | +from uniswap.constants import ETH_ADDRESS, WETH9_ADDRESS |
16 | 17 | from uniswap.exceptions import InsufficientBalance |
17 | | -from uniswap.util import _str_to_addr |
18 | 18 | from uniswap.tokens import get_tokens |
| 19 | +from uniswap.util import _str_to_addr, default_tick_range, _addr_to_str, _load_contract_erc20 |
19 | 20 |
|
20 | 21 |
|
21 | 22 | logger = logging.getLogger(__name__) |
@@ -193,6 +194,60 @@ def test_get_raw_price(self, client: Uniswap, tokens, token0, token1, fee): |
193 | 194 | r = client.get_raw_price(token0, token1, fee=fee) |
194 | 195 | assert r |
195 | 196 |
|
| 197 | + @pytest.mark.parametrize( |
| 198 | + "token0, token1, kwargs", |
| 199 | + [ |
| 200 | + (weth, dai, {"fee": 500}), |
| 201 | + ] |
| 202 | + ) |
| 203 | + def test_get_pool_instance(self, client, token0, token1, kwargs): |
| 204 | + if client.version != 3: |
| 205 | + pytest.skip("Not supported in this version of Uniswap") |
| 206 | + r = client.get_pool_instance(token0, token1, **kwargs) |
| 207 | + assert r |
| 208 | + |
| 209 | + @pytest.mark.parametrize( |
| 210 | + "token0, token1, kwargs", |
| 211 | + [ |
| 212 | + (weth, dai, {"fee": 500}), |
| 213 | + ] |
| 214 | + ) |
| 215 | + def test_get_pool_immutables(self, client, token0, token1, kwargs): |
| 216 | + if client.version != 3: |
| 217 | + pytest.skip("Not supported in this version of Uniswap") |
| 218 | + pool = client.get_pool_instance(token0, token1, **kwargs) |
| 219 | + r = client.get_pool_immutables(pool) |
| 220 | + print(r) |
| 221 | + assert r |
| 222 | + |
| 223 | + @pytest.mark.parametrize( |
| 224 | + "token0, token1, kwargs", |
| 225 | + [ |
| 226 | + (weth, dai, {"fee": 500}), |
| 227 | + ] |
| 228 | + ) |
| 229 | + def test_get_pool_state(self, client, token0, token1, kwargs): |
| 230 | + if client.version != 3: |
| 231 | + pytest.skip("Not supported in this version of Uniswap") |
| 232 | + pool = client.get_pool_instance(token0, token1, **kwargs) |
| 233 | + r = client.get_pool_state(pool) |
| 234 | + print(r) |
| 235 | + assert r |
| 236 | + |
| 237 | + @pytest.mark.parametrize( |
| 238 | + "amount0, amount1, token0, token1, kwargs", |
| 239 | + [ |
| 240 | + (1, 10, weth, dai, {"fee":500}), |
| 241 | + ] |
| 242 | + ) |
| 243 | + def test_mint_position(self, client, amount0, amount1, token0, token1, kwargs): |
| 244 | + if client.version != 3: |
| 245 | + pytest.skip("Not supported in this version of Uniswap") |
| 246 | + pool = client.get_pool_instance(token0, token1, **kwargs) |
| 247 | + r = client.mint_position(pool, amount0, amount1) |
| 248 | + print(r) |
| 249 | + assert r |
| 250 | + |
196 | 251 | # ------ ERC20 Pool ---------------------------------------------------------------- |
197 | 252 | @pytest.mark.parametrize("token", [("UNI"), ("DAI")]) |
198 | 253 | def test_get_ex_eth_balance( |
@@ -231,6 +286,80 @@ def test_get_exchange_rate( |
231 | 286 | assert r |
232 | 287 |
|
233 | 288 | # ------ Liquidity ----------------------------------------------------------------- |
| 289 | + @pytest.mark.parametrize( |
| 290 | + "token0, token1, amount0, amount1, qty, fee", |
| 291 | + [ |
| 292 | + ('DAI', 'USDC', ONE_ETH, ONE_USDC, ONE_ETH, 3000), |
| 293 | + ] |
| 294 | + ) |
| 295 | + def test_v3_deploy_pool_with_liquidity(self, client: Uniswap, tokens, token0, token1, amount0, amount1, qty, fee): |
| 296 | + if client.version != 3: |
| 297 | + pytest.skip("Not supported in this version of Uniswap") |
| 298 | + |
| 299 | + try: |
| 300 | + pool = client.create_pool_instance(tokens[token0], tokens[token1], fee) |
| 301 | + except Exception: |
| 302 | + pool = client.get_pool_instance(tokens[token0], tokens[token1], fee) |
| 303 | + |
| 304 | + print(pool.address) |
| 305 | + # Ensuring client has sufficient balance of both tokens |
| 306 | + eth_to_dai = client.make_trade(tokens['ETH'], tokens[token0], qty, client.address) |
| 307 | + eth_to_dai_tx = client.w3.eth.wait_for_transaction_receipt(eth_to_dai, timeout=RECEIPT_TIMEOUT) |
| 308 | + assert eth_to_dai_tx["status"] |
| 309 | + dai_to_usdc = client.make_trade(tokens[token0], tokens[token1], qty*10, client.address) |
| 310 | + dai_to_usdc_tx = client.w3.eth.wait_for_transaction_receipt(dai_to_usdc, timeout=RECEIPT_TIMEOUT) |
| 311 | + assert dai_to_usdc_tx["status"] |
| 312 | + |
| 313 | + balance_0 = client.get_token_balance(tokens[token0]) |
| 314 | + balance_1 = client.get_token_balance(tokens[token1]) |
| 315 | + |
| 316 | + assert balance_0 > amount0, f'Have: {balance_0} need {amount0}' |
| 317 | + assert balance_1 > amount1, f'Have: {balance_1} need {amount1}' |
| 318 | + |
| 319 | + |
| 320 | + min_tick, max_tick = default_tick_range(fee) |
| 321 | + r = client.mint_liquidity( |
| 322 | + pool, |
| 323 | + amount0, |
| 324 | + amount1, |
| 325 | + tick_lower=min_tick, |
| 326 | + tick_upper=max_tick, |
| 327 | + deadline=2**64 |
| 328 | + ) |
| 329 | + assert r["status"] |
| 330 | + |
| 331 | + position_balance = client.nonFungiblePositionManager.functions.balanceOf(_addr_to_str(client.address)).call() |
| 332 | + assert position_balance > 0 |
| 333 | + |
| 334 | + position_array = client.get_liquidity_positions() |
| 335 | + assert len(position_array) > 0 |
| 336 | + |
| 337 | + |
| 338 | + @pytest.mark.parametrize( |
| 339 | + "deadline", |
| 340 | + [(2**64)], |
| 341 | + ) |
| 342 | + def test_close_position(self, client: Uniswap, deadline): |
| 343 | + if client.version != 3: |
| 344 | + pytest.skip("Not supported in this version of Uniswap") |
| 345 | + position_array = client.get_liquidity_positions() |
| 346 | + tokenId = position_array[0] |
| 347 | + r = client.close_position(tokenId, deadline=deadline) |
| 348 | + assert r["status"] |
| 349 | + |
| 350 | + @pytest.mark.parametrize( |
| 351 | + "token0, token1", |
| 352 | + [("DAI", "USDC")] |
| 353 | + ) |
| 354 | + def test_get_tvl_in_pool_on_chain(self, client: Uniswap, tokens, token0, token1): |
| 355 | + if client.version != 3: |
| 356 | + pytest.skip("Not supported in this version of Uniswap") |
| 357 | + |
| 358 | + pool = client.get_pool_instance(tokens[token0], tokens[token1]) |
| 359 | + tvl_0, tvl_1 = client.get_tvl_in_pool(pool) |
| 360 | + assert tvl_0 > 0 |
| 361 | + assert tvl_1 > 0 |
| 362 | + |
234 | 363 | @pytest.mark.skip |
235 | 364 | @pytest.mark.parametrize( |
236 | 365 | "token, max_eth", |
@@ -271,7 +400,7 @@ def test_remove_liquidity( |
271 | 400 | # Token -> Token |
272 | 401 | ("DAI", "USDC", ONE_ETH, None, does_not_raise), |
273 | 402 | # Token -> ETH |
274 | | - ("USDC", "ETH", 100 * ONE_USDC, None, does_not_raise), |
| 403 | + ("USDC", "ETH", ONE_USDC, None, does_not_raise), |
275 | 404 | # ("ETH", "UNI", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), |
276 | 405 | # ("UNI", "ETH", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), |
277 | 406 | # ("DAI", "UNI", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), |
@@ -310,19 +439,19 @@ def test_make_trade( |
310 | 439 | "input_token, output_token, qty, recipient, expectation", |
311 | 440 | [ |
312 | 441 | # ETH -> Token |
313 | | - ("ETH", "DAI", 10 ** 18, None, does_not_raise), |
| 442 | + ("ETH", "DAI", ONE_ETH, None, does_not_raise), |
314 | 443 | # Token -> Token |
315 | 444 | ("DAI", "USDC", ONE_USDC, None, does_not_raise), |
316 | 445 | # Token -> ETH |
317 | | - ("DAI", "ETH", 10 ** 16, None, does_not_raise), |
| 446 | + ("DAI", "ETH", 100 * ONE_USDC, None, does_not_raise), |
318 | 447 | # FIXME: These should probably be uncommented eventually |
319 | 448 | # ("ETH", "UNI", int(0.000001 * ONE_ETH), ZERO_ADDRESS), |
320 | 449 | # ("UNI", "ETH", int(0.000001 * ONE_ETH), ZERO_ADDRESS), |
321 | 450 | # ("DAI", "UNI", int(0.000001 * ONE_ETH), ZERO_ADDRESS), |
322 | 451 | ( |
323 | 452 | "DAI", |
324 | 453 | "ETH", |
325 | | - 10 * 10 ** 18, |
| 454 | + 10 * ONE_ETH, |
326 | 455 | None, |
327 | 456 | lambda: pytest.raises(InsufficientBalance), |
328 | 457 | ), |
|
0 commit comments