diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 94e28d1c..66111f9f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,14 +6,33 @@ on: pull_request: branches: [ master ] +env: + # Public key for PRs, plz don't abuse + PROVIDER_MAINNET: ${{ secrets.MAINNET_PROVIDER || 'https://mainnet.infura.io/v3/42ffb4f2549c4a5fa3b5d6db70f6fad1' }} + PROVIDER_ARBITRUM: 'https://rpc.ankr.com/arbitrum' + PROVIDER_XDAI: 'https://rpc.ankr.com/gnosis' + jobs: test: - name: test (v${{ matrix.uniswap-version }}) + name: test (v${{ matrix.uniswap-version }}, ${{ matrix.network }}) runs-on: ubuntu-latest strategy: fail-fast: false matrix: uniswap-version: [1, 2, 3] + network: ["mainnet"] + include: + - network: arbitrum + uniswap-version: 3 + #include: + # - network: xdai + # uniswap-version: 3 + #include: + # - network: optimism + # uniswap-version: 3 + #include: + # - network: polygon + # uniswap-version: 3 steps: - uses: actions/checkout@v2 @@ -35,12 +54,14 @@ jobs: run: | python -m pip install --upgrade pip poetry poetry config virtualenvs.in-project true + - name: Set up cache uses: actions/cache@v2 id: cache with: path: .venv key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }} + - name: Ensure cache is healthy if: steps.cache.outputs.cache-hit == 'true' run: timeout 10s poetry run pip --version || rm -rf .venv @@ -48,13 +69,16 @@ jobs: - name: Install dependencies run: | poetry install - npm install -g ganache@7.0.1 + npm install -g ganache@7.3.2 + - name: Test env: - PROVIDER: ${{ secrets.MAINNET_PROVIDER }} + # Use the secret if available, otherwise fallback to the public key + PROVIDER: ${{ ((matrix.network == 'mainnet') && env.PROVIDER_MAINNET) || ((matrix.network == 'arbitrum') && env.PROVIDER_ARBITRUM) }} UNISWAP_VERSION: ${{ matrix.uniswap-version }} run: | make test + - name: "Upload coverage to Codecov" uses: codecov/codecov-action@v1 with: @@ -76,16 +100,19 @@ jobs: id: full-python-version run: | echo ::set-output name=version::$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info[:3]))") + - name: Set up poetry run: | python -m pip install --upgrade pip poetry poetry config virtualenvs.in-project true + - name: Set up cache uses: actions/cache@v2 id: cache with: path: .venv key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }} + - name: Ensure cache is healthy if: steps.cache.outputs.cache-hit == 'true' run: timeout 10s poetry run pip --version || rm -rf .venv @@ -94,6 +121,7 @@ jobs: run: | python -m pip install --upgrade pip poetry poetry install + - name: Typecheck run: | make typecheck diff --git a/Makefile b/Makefile index ac1ebe70..aa3e4d58 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: test typecheck lint precommit docs test: - poetry run pytest -v --tb=line --maxfail=4 --cov=uniswap --cov-report html --cov-report term --cov-report xml + poetry run pytest -v --maxfail=10 --cov=uniswap --cov-report html --cov-report term --cov-report xml typecheck: poetry run mypy --pretty @@ -11,7 +11,7 @@ lint: format: black uniswap - + format-abis: npx prettier --write --parser=json uniswap/assets/*/*.abi diff --git a/tests/test_uniswap.py b/tests/test_uniswap.py index c8511a1f..f5265755 100644 --- a/tests/test_uniswap.py +++ b/tests/test_uniswap.py @@ -15,6 +15,7 @@ from uniswap.constants import ETH_ADDRESS from uniswap.exceptions import InsufficientBalance from uniswap.util import _str_to_addr +from uniswap.tokens import get_tokens logger = logging.getLogger(__name__) @@ -47,12 +48,17 @@ def client(request, web3: Web3, ganache: GanacheInstance): ) +@pytest.fixture(scope="function", params=UNISWAP_VERSIONS) +def tokens(client: Uniswap): + return get_tokens(client.netname) + + @pytest.fixture(scope="module") def test_assets(client: Uniswap): """ Buy some DAI and USDC to test with. """ - tokens = client._get_token_addresses() + tokens = get_tokens(client.netname) for token_name, amount in [("DAI", 100 * 10 ** 18), ("USDC", 100 * 10 ** 6)]: token_addr = tokens[token_name] @@ -68,7 +74,7 @@ def test_assets(client: Uniswap): def web3(ganache: GanacheInstance): w3 = Web3(Web3.HTTPProvider(ganache.provider, request_kwargs={"timeout": 30})) if 1 != int(w3.net.version): - raise Exception("PROVIDER was not a mainnet provider, which the tests require") + logger.warning("PROVIDER was not a mainnet provider, which the tests require") return w3 @@ -114,26 +120,15 @@ def does_not_raise(): yield -# TODO: Change pytest.param(..., mark=pytest.mark.xfail) to the expectation/raises method -@pytest.mark.usefixtures("client", "web3") -class TestUniswap(object): - ONE_ETH = 10 ** 18 - ONE_USDC = 10 ** 6 +ONE_ETH = 10 ** 18 +ONE_USDC = 10 ** 6 - ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" +ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" - # TODO: Detect mainnet vs rinkeby and set accordingly, like _get_token_addresses in the Uniswap class - # For Mainnet testing (with `ganache --fork` as per the ganache fixture) - eth = "0x0000000000000000000000000000000000000000" - weth = Web3.toChecksumAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2") - bat = Web3.toChecksumAddress("0x0D8775F648430679A709E98d2b0Cb6250d2887EF") - dai = Web3.toChecksumAddress("0x6b175474e89094c44da98b954eedeac495271d0f") - usdc = Web3.toChecksumAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48") - # For Rinkeby - # eth = "0x0000000000000000000000000000000000000000" - # bat = "0xDA5B056Cfb861282B4b59d29c9B395bcC238D29B" - # dai = "0x2448eE2641d78CC42D7AD76498917359D961A783" +# TODO: Change pytest.param(..., mark=pytest.mark.xfail) to the expectation/raises method +@pytest.mark.usefixtures("client", "web3") +class TestUniswap(object): # ------ Exchange ------------------------------------------------------------------ def test_get_fee_maker(self, client: Uniswap): @@ -152,20 +147,19 @@ def test_get_fee_taker(self, client: Uniswap): @pytest.mark.parametrize( "token0, token1, qty, kwargs", [ - (eth, bat, ONE_ETH, {}), - (bat, eth, ONE_ETH, {}), - (eth, dai, ONE_ETH, {}), - (dai, eth, ONE_ETH, {}), - (eth, bat, 2 * ONE_ETH, {}), - (bat, eth, 2 * ONE_ETH, {}), - (weth, dai, ONE_ETH, {}), - (dai, weth, ONE_ETH, {}), - (dai, usdc, ONE_ETH, {"fee": 500}), - pytest.param(eth, "btc", ONE_ETH, {}, marks=pytest.mark.xfail), - pytest.param("btc", eth, ONE_ETH, {}, marks=pytest.mark.xfail), + ("ETH", "UNI", ONE_ETH, {}), + ("UNI", "ETH", ONE_ETH, {}), + ("ETH", "DAI", ONE_ETH, {}), + ("DAI", "ETH", ONE_ETH, {}), + ("ETH", "UNI", 2 * ONE_ETH, {}), + ("UNI", "ETH", 2 * ONE_ETH, {}), + ("WETH", "DAI", ONE_ETH, {}), + ("DAI", "WETH", ONE_ETH, {}), + ("DAI", "USDC", ONE_ETH, {"fee": 500}), ], ) - def test_get_price_input(self, client, token0, token1, qty, kwargs): + def test_get_price_input(self, client, tokens, token0, token1, qty, kwargs): + token0, token1 = tokens[token0], tokens[token1] if client.version == 1 and ETH_ADDRESS not in [token0, token1]: pytest.skip("Not supported in this version of Uniswap") r = client.get_price_input(token0, token1, qty, **kwargs) @@ -174,55 +168,58 @@ def test_get_price_input(self, client, token0, token1, qty, kwargs): @pytest.mark.parametrize( "token0, token1, qty, kwargs", [ - (eth, bat, ONE_ETH, {}), - (bat, eth, ONE_ETH, {}), - (eth, dai, ONE_ETH, {}), - (dai, eth, ONE_ETH, {}), - (eth, bat, 2 * ONE_ETH, {}), - (bat, eth, 2 * ONE_ETH, {}), - (weth, dai, ONE_ETH, {}), - (dai, weth, ONE_ETH, {}), - (dai, usdc, ONE_USDC, {"fee": 500}), - pytest.param(eth, "btc", ONE_ETH, {}, marks=pytest.mark.xfail), - pytest.param("btc", eth, ONE_ETH, {}, marks=pytest.mark.xfail), + ("ETH", "UNI", ONE_ETH, {}), + ("UNI", "ETH", ONE_ETH // 100, {}), + ("ETH", "DAI", ONE_ETH, {}), + ("DAI", "ETH", ONE_ETH, {}), + ("ETH", "UNI", 2 * ONE_ETH, {}), + ("WETH", "DAI", ONE_ETH, {}), + ("DAI", "WETH", ONE_ETH, {}), + ("DAI", "USDC", ONE_USDC, {"fee": 500}), ], ) - def test_get_price_output(self, client, token0, token1, qty, kwargs): + def test_get_price_output(self, client, tokens, token0, token1, qty, kwargs): + token0, token1 = tokens[token0], tokens[token1] if client.version == 1 and ETH_ADDRESS not in [token0, token1]: pytest.skip("Not supported in this version of Uniswap") r = client.get_price_output(token0, token1, qty, **kwargs) assert r # ------ ERC20 Pool ---------------------------------------------------------------- - @pytest.mark.parametrize("token", [(bat), (dai)]) + @pytest.mark.parametrize("token", [("UNI"), ("DAI")]) def test_get_ex_eth_balance( self, client: Uniswap, + tokens, token, ): if not client.version == 1: - pytest.skip("Tested method only supported on Uniswap v1") - r = client.get_ex_eth_balance(token) + pytest.skip("Only supported on Uniswap v1") + r = client.get_ex_eth_balance(tokens[token]) assert r - @pytest.mark.parametrize("token", [(bat), (dai)]) + @pytest.mark.parametrize("token", [("UNI"), ("DAI")]) def test_get_ex_token_balance( self, client: Uniswap, + tokens, token, ): if not client.version == 1: - pytest.skip("Tested method only supported on Uniswap v1") - r = client.get_ex_token_balance(token) + pytest.skip("Only supported on Uniswap v1") + r = client.get_ex_token_balance(tokens[token]) assert r - @pytest.mark.parametrize("token", [(bat), (dai)]) - def get_exchange_rate( + @pytest.mark.parametrize("token", [("UNI"), ("DAI")]) + def test_get_exchange_rate( self, client: Uniswap, + tokens, token, ): - r = client.get_exchange_rate(token) + if not client.version == 1: + pytest.skip("Only supported on Uniswap v1") + r = client.get_exchange_rate(tokens[token]) assert r # ------ Liquidity ----------------------------------------------------------------- @@ -230,12 +227,12 @@ def get_exchange_rate( @pytest.mark.parametrize( "token, max_eth", [ - (bat, 0.00001 * ONE_ETH), - (dai, 0.00001 * ONE_ETH), - pytest.param("btc", ONE_ETH, marks=pytest.mark.xfail), + ("UNI", 0.00001 * ONE_ETH), + ("DAI", 0.00001 * ONE_ETH), ], ) - def test_add_liquidity(self, client: Uniswap, web3: Web3, token, max_eth): + def test_add_liquidity(self, client: Uniswap, tokens, web3: Web3, token, max_eth): + token = tokens[token] r = client.add_liquidity(token, max_eth) tx = web3.eth.wait_for_transaction_receipt(r, timeout=RECEIPT_TIMEOUT) assert tx["status"] @@ -244,16 +241,16 @@ def test_add_liquidity(self, client: Uniswap, web3: Web3, token, max_eth): @pytest.mark.parametrize( "token, max_token, expectation", [ - (bat, 0.00001 * ONE_ETH, does_not_raise()), - (dai, 0.00001 * ONE_ETH, does_not_raise()), - ("btc", ONE_ETH, pytest.raises(NameNotFound)), + ("UNI", 0.00001 * ONE_ETH, does_not_raise()), + ("DAI", 0.00001 * ONE_ETH, does_not_raise()), ], ) def test_remove_liquidity( - self, client: Uniswap, web3: Web3, token, max_token, expectation + self, client: Uniswap, web3: Web3, tokens, token, max_token, expectation ): + token = tokens[token] with expectation: - r = client.remove_liquidity(token, max_token) + r = client.remove_liquidity(tokens[token], max_token) tx = web3.eth.wait_for_transaction_receipt(r) assert tx["status"] @@ -262,21 +259,21 @@ def test_remove_liquidity( "input_token, output_token, qty, recipient, expectation", [ # ETH -> Token - (eth, dai, ONE_ETH, None, does_not_raise), + ("ETH", "DAI", ONE_ETH, None, does_not_raise), # Token -> Token - (dai, usdc, ONE_ETH, None, does_not_raise), + ("DAI", "USDC", ONE_ETH, None, does_not_raise), # Token -> ETH - (usdc, eth, 100 * ONE_USDC, None, does_not_raise), - # (eth, bat, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), - # (bat, eth, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), - # (dai, bat, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), - (dai, "btc", ONE_ETH, None, lambda: pytest.raises(NameNotFound)), + ("USDC", "ETH", 100 * ONE_USDC, None, does_not_raise), + # ("ETH", "UNI", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), + # ("UNI", "ETH", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), + # ("DAI", "UNI", 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise), ], ) def test_make_trade( self, client: Uniswap, web3: Web3, + tokens, test_assets, input_token, output_token, @@ -284,6 +281,7 @@ def test_make_trade( recipient, expectation, ): + input_token, output_token = tokens[input_token], tokens[output_token] if client.version == 1 and ETH_ADDRESS not in [input_token, output_token]: pytest.skip( "Not supported in this version of Uniswap, or at least no liquidity" @@ -297,36 +295,36 @@ def test_make_trade( # TODO: Checks for ETH, taking gas into account bal_in_after = client.get_token_balance(input_token) - if input_token != self.eth: + if input_token != tokens["ETH"]: assert bal_in_before - qty == bal_in_after @pytest.mark.parametrize( "input_token, output_token, qty, recipient, expectation", [ # ETH -> Token - (eth, dai, 10 ** 18, None, does_not_raise), + ("ETH", "DAI", 10 ** 18, None, does_not_raise), # Token -> Token - (dai, usdc, ONE_USDC, None, does_not_raise), + ("DAI", "USDC", ONE_USDC, None, does_not_raise), # Token -> ETH - (dai, eth, 10 ** 16, None, does_not_raise), + ("DAI", "ETH", 10 ** 16, None, does_not_raise), # FIXME: These should probably be uncommented eventually - # (eth, bat, int(0.000001 * ONE_ETH), ZERO_ADDRESS), - # (bat, eth, int(0.000001 * ONE_ETH), ZERO_ADDRESS), - # (dai, bat, int(0.000001 * ONE_ETH), ZERO_ADDRESS), + # ("ETH", "UNI", int(0.000001 * ONE_ETH), ZERO_ADDRESS), + # ("UNI", "ETH", int(0.000001 * ONE_ETH), ZERO_ADDRESS), + # ("DAI", "UNI", int(0.000001 * ONE_ETH), ZERO_ADDRESS), ( - dai, - eth, + "DAI", + "ETH", 10 * 10 ** 18, None, lambda: pytest.raises(InsufficientBalance), ), - (dai, "btc", ONE_ETH, None, lambda: pytest.raises(NameNotFound)), ], ) def test_make_trade_output( self, client: Uniswap, web3: Web3, + tokens, test_assets, input_token, output_token, @@ -334,6 +332,7 @@ def test_make_trade_output( recipient, expectation, ): + input_token, output_token = tokens[input_token], tokens[output_token] if client.version == 1 and ETH_ADDRESS not in [input_token, output_token]: pytest.skip( "Not supported in this version of Uniswap, or at least no liquidity" @@ -347,5 +346,5 @@ def test_make_trade_output( # TODO: Checks for ETH, taking gas into account balance_after = client.get_token_balance(output_token) - if output_token != self.eth: + if output_token != tokens["ETH"]: assert balance_before + qty == balance_after diff --git a/uniswap/cli.py b/uniswap/cli.py index 633ab91c..76e18dd8 100644 --- a/uniswap/cli.py +++ b/uniswap/cli.py @@ -4,17 +4,23 @@ import click from dotenv import load_dotenv from web3 import Web3 +from typing import Optional from .uniswap import Uniswap, AddressLike, _str_to_addr from .token import BaseToken -from .tokens import tokens +from .tokens import get_tokens from .constants import ETH_ADDRESS logger = logging.getLogger(__name__) +# Global used in _coerce_to_checksum to look up tokens +_uni: Optional[Uniswap] = None + def _coerce_to_checksum(addr: str) -> str: + assert _uni + tokens = get_tokens(_uni.netname) if not addr.startswith("0x"): if addr.upper() in tokens: return tokens[addr.upper()] @@ -33,7 +39,7 @@ def _coerce_to_checksum(addr: str) -> str: @click.option( "--version", type=click.Choice(["1", "2", "3"]), - default=os.getenv("UNISWAP_VERSION", "2"), + default=os.getenv("UNISWAP_VERSION", "3"), ) @click.pass_context def main(ctx: click.Context, verbose: bool, version: str) -> None: @@ -43,6 +49,8 @@ def main(ctx: click.Context, verbose: bool, version: str) -> None: ctx.ensure_object(dict) ctx.obj["VERBOSE"] = verbose ctx.obj["UNISWAP"] = Uniswap(None, None, version=int(version)) + global _uni + _uni = ctx.obj["UNISWAP"] @main.command() @@ -100,7 +108,7 @@ def token(ctx: click.Context, token: AddressLike) -> None: def tokendb(ctx: click.Context, metadata: bool) -> None: """List known token addresses""" uni: Uniswap = ctx.obj["UNISWAP"] - for symbol, addr in tokens.items(): + for symbol, addr in get_tokens(uni.netname).items(): if metadata and addr != "0x0000000000000000000000000000000000000000": data = uni.get_token(_str_to_addr(addr)) assert data.symbol.lower() == symbol.lower() diff --git a/uniswap/tokens.py b/uniswap/tokens.py index ed5ee60b..551b77ed 100644 --- a/uniswap/tokens.py +++ b/uniswap/tokens.py @@ -1,10 +1,10 @@ from typing import Dict from web3 import Web3 -from web3.types import ChecksumAddress +from eth_typing.evm import ChecksumAddress -tokens: Dict[str, ChecksumAddress] = { +tokens_mainnet: Dict[str, ChecksumAddress] = { k: Web3.toChecksumAddress(v) for k, v in { "ETH": "0x0000000000000000000000000000000000000000", @@ -25,3 +25,29 @@ "BAT": "0xDA5B056Cfb861282B4b59d29c9B395bcC238D29B", }.items() } + +tokens_arbitrum: Dict[str, ChecksumAddress] = { + k: Web3.toChecksumAddress(v) + for k, v in { + "ETH": "0x0000000000000000000000000000000000000000", + "WETH": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1", + "DAI": "0xda10009cbd5d07dd0cecc66161fc93d7c9000da1", + "USDC": "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8", + "UNI": "0xfa7f8980b0f1e64a2062791cc3b0871572f1f7f0", + }.items() +} + + +def get_tokens(netname: str) -> Dict[str, ChecksumAddress]: + """ + Returns a dict with addresses for tokens for the current net. + Used in testing. + """ + if netname == "mainnet": + return tokens_mainnet + elif netname == "rinkeby": + return tokens_rinkeby + elif netname == "arbitrum": + return tokens_arbitrum + else: + raise Exception(f"Unknown net '{netname}'") diff --git a/uniswap/uniswap.py b/uniswap/uniswap.py index ccc5639a..c43c3284 100644 --- a/uniswap/uniswap.py +++ b/uniswap/uniswap.py @@ -2,24 +2,22 @@ import time import logging import functools -from typing import List, Any, Optional, Union, Tuple, Dict, Iterable +from typing import List, Any, Optional, Union, Tuple, Iterable from web3 import Web3 -from web3.eth import Contract -from web3.contract import ContractFunction +from web3.contract import Contract, ContractFunction from web3.exceptions import BadFunctionCallOutput, ContractLogicError from web3.types import ( TxParams, Wei, - Address, - ChecksumAddress, Nonce, - HexBytes, ) +from eth_typing.evm import Address, ChecksumAddress +from hexbytes import HexBytes from .types import AddressLike from .token import ERC20Token -from .tokens import tokens, tokens_rinkeby +from .tokens import get_tokens from .exceptions import InvalidToken, InsufficientBalance from .util import ( _str_to_addr, @@ -1212,6 +1210,15 @@ def get_token(self, address: AddressLike, abi_name: str = "erc20") -> ERC20Token """ # FIXME: This function should always return the same output for the same input # and would therefore benefit from caching + if address == "0x0000000000000000000000000000000000000000": + # This isn't exactly right, but for all intents and purposes, + # ETH is treated as a ERC20 by Uniswap. + return ERC20Token( + address=address, + name="ETH", + symbol="ETH", + decimals=18, + ) token_contract = _load_contract(self.w3, abi_name, address=address) try: _name = token_contract.functions.name().call() @@ -1224,11 +1231,11 @@ def get_token(self, address: AddressLike, abi_name: str = "erc20") -> ERC20Token raise InvalidToken(address) try: name = _name.decode() - except: + except Exception: name = _name try: symbol = _symbol.decode() - except: + except Exception: symbol = _symbol return ERC20Token(symbol, address, name, decimals) @@ -1256,7 +1263,7 @@ def get_raw_price( token_out = self.get_weth_address() if self.version == 2: - params: Iterable[Union[ChecksumAddress,Optional[int]]] = [ + params: Iterable[Union[ChecksumAddress, Optional[int]]] = [ self.w3.toChecksumAddress(token_in), self.w3.toChecksumAddress(token_out), ] @@ -1364,20 +1371,6 @@ def get_fee_taker(self) -> float: """Get the taker fee.""" return 0.003 - # ------ Test utilities ------------------------------------------------------------ - - def _get_token_addresses(self) -> Dict[str, ChecksumAddress]: - """ - Returns a dict with addresses for tokens for the current net. - Used in testing. - """ - if self.netname == "mainnet": - return tokens - elif self.netname == "rinkeby": - return tokens_rinkeby - else: - raise Exception(f"Unknown net '{self.netname}'") - # ---- Old v1 utils ---- @supports([1])