Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
dc5ee9c
implementing V3 features. added methods to fetch on-chain pool data
KeremP Jan 18, 2022
1613ea1
feature: adding position miniting (WIP)
KeremP Jan 23, 2022
a331538
WIP testing V3 pool position minting
KeremP Feb 14, 2022
6191901
wip v3 features
KeremP Apr 24, 2022
ac3d112
Merge remote-tracking branch 'upstream/master' into v3-features
KeremP Jul 10, 2022
7b4aedf
feat: create v3 pool and mint/add liquidity position
KeremP Jul 11, 2022
c736d49
fix: add 0x0 address assert in get_pool_instance
KeremP Jul 11, 2022
99180a9
feat: close v3 liquidity position
KeremP Jul 13, 2022
760b50f
minor cleanups
KeremP Jul 13, 2022
99d1217
add get_tvl_in_pool to return total value locked in pool
KeremP Jul 15, 2022
c95e119
implementing V3 features. added methods to fetch on-chain pool data
KeremP Jan 18, 2022
62c80f2
feature: adding position miniting (WIP)
KeremP Jan 23, 2022
b32acc8
WIP testing V3 pool position minting
KeremP Feb 14, 2022
8cf2664
wip v3 features
KeremP Apr 24, 2022
99a16db
feat: create v3 pool and mint/add liquidity position
KeremP Jul 11, 2022
54e460e
fix: add 0x0 address assert in get_pool_instance
KeremP Jul 11, 2022
4151b5c
feat: close v3 liquidity position
KeremP Jul 13, 2022
6d66b28
minor cleanups
KeremP Jul 13, 2022
91b1dc0
add get_tvl_in_pool to return total value locked in pool
KeremP Jul 15, 2022
d1b24ea
Merge branch 'v3-features' of github.com:KeremP/uniswap-python into v…
KeremP Jul 15, 2022
088ebee
Merge remote-tracking branch 'upstream/master' into v3-features
KeremP Jul 15, 2022
b0fca2b
minor cleanups
KeremP Jul 15, 2022
30e9759
fixing minor errors afer rebase
KeremP Jul 15, 2022
6fc0680
cleanup asset amounts for tests
KeremP Jul 15, 2022
2f960ae
fix: ensure asset amounts are correct for v3 liquidity position tests
KeremP Jul 15, 2022
6ab2c51
add tests for TVL calculations. include method for fetching TVL from …
KeremP Jul 15, 2022
ed43b4b
add arbitrum subgraph endpoint
KeremP Jul 15, 2022
6f0ff5b
fix typo
KeremP Jul 15, 2022
30cce24
skip tvl tests for now
KeremP Jul 17, 2022
1e5d430
feat: get TVL on chain
KeremP Aug 18, 2022
2604d4c
minor cleanups
KeremP Aug 18, 2022
e6f972c
fix aribitrum multicall2 address. clean up TVL test
KeremP Aug 18, 2022
9468536
update get_liquidity_positions to take arbitrary address param
KeremP Aug 26, 2022
812bb87
fix: removed print in internal code
ErikBjare Aug 29, 2022
43aae85
Merge branch 'master' into v3-features
ErikBjare Aug 29, 2022
0175265
fix: fixed lint and bug in tests
ErikBjare Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 54 additions & 36 deletions tests/test_uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from uniswap.constants import ETH_ADDRESS, WETH9_ADDRESS
from uniswap.exceptions import InsufficientBalance
from uniswap.tokens import get_tokens
from uniswap.util import _str_to_addr, default_tick_range, _addr_to_str, _load_contract_erc20
from uniswap.util import (
_str_to_addr,
default_tick_range,
_addr_to_str,
_load_contract_erc20,
)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -197,10 +202,11 @@ def test_get_raw_price(self, client: Uniswap, tokens, token0, token1, fee):
@pytest.mark.parametrize(
"token0, token1, kwargs",
[
(weth, dai, {"fee": 500}),
]
("WETH", "DAI", {"fee": 500}),
],
)
def test_get_pool_instance(self, client, token0, token1, kwargs):
def test_get_pool_instance(self, client, tokens, token0, token1, kwargs):
token0, token1 = tokens[token0], tokens[token1]
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")
r = client.get_pool_instance(token0, token1, **kwargs)
Expand All @@ -209,10 +215,11 @@ def test_get_pool_instance(self, client, token0, token1, kwargs):
@pytest.mark.parametrize(
"token0, token1, kwargs",
[
(weth, dai, {"fee": 500}),
]
("WETH", "DAI", {"fee": 500}),
],
)
def test_get_pool_immutables(self, client, token0, token1, kwargs):
def test_get_pool_immutables(self, client, tokens, token0, token1, kwargs):
token0, token1 = tokens[token0], tokens[token1]
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")
pool = client.get_pool_instance(token0, token1, **kwargs)
Expand All @@ -223,10 +230,11 @@ def test_get_pool_immutables(self, client, token0, token1, kwargs):
@pytest.mark.parametrize(
"token0, token1, kwargs",
[
(weth, dai, {"fee": 500}),
]
("WETH", "DAI", {"fee": 500}),
],
)
def test_get_pool_state(self, client, token0, token1, kwargs):
def test_get_pool_state(self, client, tokens, token0, token1, kwargs):
token0, token1 = tokens[token0], tokens[token1]
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")
pool = client.get_pool_instance(token0, token1, **kwargs)
Expand All @@ -237,10 +245,13 @@ def test_get_pool_state(self, client, token0, token1, kwargs):
@pytest.mark.parametrize(
"amount0, amount1, token0, token1, kwargs",
[
(1, 10, weth, dai, {"fee":500}),
]
(1, 10, "WETH", "DAI", {"fee": 500}),
],
)
def test_mint_position(self, client, amount0, amount1, token0, token1, kwargs):
def test_mint_position(
self, client, tokens, amount0, amount1, token0, token1, kwargs
):
token0, token1 = tokens[token0], tokens[token1]
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")
pool = client.get_pool_instance(token0, token1, **kwargs)
Expand Down Expand Up @@ -289,10 +300,12 @@ def test_get_exchange_rate(
@pytest.mark.parametrize(
"token0, token1, amount0, amount1, qty, fee",
[
('DAI', 'USDC', ONE_ETH, ONE_USDC, ONE_ETH, 3000),
]
("DAI", "USDC", ONE_ETH, ONE_USDC, ONE_ETH, 3000),
],
)
def test_v3_deploy_pool_with_liquidity(self, client: Uniswap, tokens, token0, token1, amount0, amount1, qty, fee):
def test_v3_deploy_pool_with_liquidity(
self, client: Uniswap, tokens, token0, token1, amount0, amount1, qty, fee
):
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")

Expand All @@ -303,41 +316,49 @@ def test_v3_deploy_pool_with_liquidity(self, client: Uniswap, tokens, token0, to

print(pool.address)
# Ensuring client has sufficient balance of both tokens
eth_to_dai = client.make_trade(tokens['ETH'], tokens[token0], qty, client.address)
eth_to_dai_tx = client.w3.eth.wait_for_transaction_receipt(eth_to_dai, timeout=RECEIPT_TIMEOUT)
eth_to_dai = client.make_trade(
tokens["ETH"], tokens[token0], qty, client.address
)
eth_to_dai_tx = client.w3.eth.wait_for_transaction_receipt(
eth_to_dai, timeout=RECEIPT_TIMEOUT
)
assert eth_to_dai_tx["status"]
dai_to_usdc = client.make_trade(tokens[token0], tokens[token1], qty*10, client.address)
dai_to_usdc_tx = client.w3.eth.wait_for_transaction_receipt(dai_to_usdc, timeout=RECEIPT_TIMEOUT)
dai_to_usdc = client.make_trade(
tokens[token0], tokens[token1], qty * 10, client.address
)
dai_to_usdc_tx = client.w3.eth.wait_for_transaction_receipt(
dai_to_usdc, timeout=RECEIPT_TIMEOUT
)
assert dai_to_usdc_tx["status"]

balance_0 = client.get_token_balance(tokens[token0])
balance_1 = client.get_token_balance(tokens[token1])

assert balance_0 > amount0, f'Have: {balance_0} need {amount0}'
assert balance_1 > amount1, f'Have: {balance_1} need {amount1}'

assert balance_0 > amount0, f"Have: {balance_0} need {amount0}"
assert balance_1 > amount1, f"Have: {balance_1} need {amount1}"

min_tick, max_tick = default_tick_range(fee)
r = client.mint_liquidity(
pool,
amount0,
amount1,
tick_lower=min_tick,
tick_upper=max_tick,
deadline=2**64
pool,
amount0,
amount1,
tick_lower=min_tick,
tick_upper=max_tick,
deadline=2 ** 64,
)
assert r["status"]

position_balance = client.nonFungiblePositionManager.functions.balanceOf(_addr_to_str(client.address)).call()
position_balance = client.nonFungiblePositionManager.functions.balanceOf(
_addr_to_str(client.address)
).call()
assert position_balance > 0

position_array = client.get_liquidity_positions()
assert len(position_array) > 0


@pytest.mark.parametrize(
"deadline",
[(2**64)],
[(2 ** 64)],
)
def test_close_position(self, client: Uniswap, deadline):
if client.version != 3:
Expand All @@ -347,10 +368,7 @@ def test_close_position(self, client: Uniswap, deadline):
r = client.close_position(tokenId, deadline=deadline)
assert r["status"]

@pytest.mark.parametrize(
"token0, token1",
[("DAI", "USDC")]
)
@pytest.mark.parametrize("token0, token1", [("DAI", "USDC")])
def test_get_tvl_in_pool_on_chain(self, client: Uniswap, tokens, token0, token1):
if client.version != 3:
pytest.skip("Not supported in this version of Uniswap")
Expand Down
5 changes: 0 additions & 5 deletions uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from .types import AddressLike
from .token import ERC20Token
from .tokens import get_tokens
from .exceptions import InvalidToken, InsufficientBalance
from .util import (
_str_to_addr,
Expand All @@ -31,15 +30,12 @@
_load_contract_erc20,
chunks,
encode_sqrt_ratioX96,
get_max_tick,
is_same_address,
nearest_tick,
)
from .decorators import supports, check_approval
from .constants import (
MAX_TICK,
MAX_UINT_128,
MIN_TICK,
WETH9_ADDRESS,
_netid_to_name,
_factory_contract_addresses_v1,
Expand Down Expand Up @@ -1737,7 +1733,6 @@ def mint_position(self, pool: Contract, amount0: int, amount1: int) -> None:
MIN_TICK = -887272
MAX_TICK = -MIN_TICK

pool_sate = self.get_pool_state(pool)
pool_immutables = self.get_pool_immutables(pool)

token0 = pool_immutables["token0"]
Expand Down
4 changes: 2 additions & 2 deletions uniswap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import json
import math
import functools
from typing import Any, Dict, Generator, Iterable, Sequence, Union, List, Tuple
from typing import Any, Generator, Sequence, Union, List, Tuple

from web3 import Web3
from web3.exceptions import NameNotFound
from web3.contract import Contract

from .constants import MIN_TICK, MAX_TICK, _tick_spacing
from .types import AddressLike, Address, Contract
from .types import AddressLike, Address


def _str_to_addr(s: Union[AddressLike, str]) -> Address:
Expand Down