From 0be6efc3c600f7dcf0ccdf8f1d804fc8292b3817 Mon Sep 17 00:00:00 2001 From: spazcoin Date: Sat, 18 Feb 2023 22:29:47 -0600 Subject: [PATCH] don't check "output vs input" tolerance if zero * print filename:lineno in log messages for better debugging * don't check "output vs input" tolerance if tx request values were zero. * unit test fix: only use lowercase account numbers 1hrs contributes to #17 --- .github/workflows/github-actions.yaml | 4 ++-- bin/scrape.py | 6 ++--- subscrape/scrapers/moonbeam_scraper.py | 33 ++++++++++++++++---------- tests/test_moonbeam_scraper.py | 5 +--- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/.github/workflows/github-actions.yaml b/.github/workflows/github-actions.yaml index bc282c9..5d954f4 100644 --- a/.github/workflows/github-actions.yaml +++ b/.github/workflows/github-actions.yaml @@ -9,7 +9,7 @@ jobs: strategy: matrix: # python-version: ["3.7", "3.8", "3.9", "3.10"] - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.10"] steps: - uses: actions/checkout@v3 @@ -21,7 +21,7 @@ jobs: run: | python -m pip install --upgrade pip pip install flake8 pytest - if [ -f PipRequirements.txt ]; then pip install -Ur PipRequirements.txt; fi + pip install -Ur PipRequirements.txt - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names diff --git a/bin/scrape.py b/bin/scrape.py index e8f9ac3..9498e8c 100644 --- a/bin/scrape.py +++ b/bin/scrape.py @@ -2,7 +2,6 @@ import json import logging from pathlib import Path -import platform import sys sys.path.append(str(Path(__file__).resolve().parent.parent)) import subscrape @@ -15,11 +14,12 @@ async def main(): Will call `scraper_factory()` to retrieve the proper scraper for a chain. If `_version` in the config does not match the current version, a warning is logged. """ - logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') + logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s (%(filename)s:%(lineno)s)') # httpx with asyncio can cause an "unclosed transport" error on Windows. A workaround is to set a different loop # policy. See https://github.com/encode/httpx/issues/914 - if platform.system() == 'Windows': + # and https://github.com/encode/httpx/issues/914#issuecomment-622586610 + if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'): asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # load config diff --git a/subscrape/scrapers/moonbeam_scraper.py b/subscrape/scrapers/moonbeam_scraper.py index fd6e5d4..7a60106 100644 --- a/subscrape/scrapers/moonbeam_scraper.py +++ b/subscrape/scrapers/moonbeam_scraper.py @@ -511,15 +511,17 @@ async def __decode_token_swap_tx(self, account, transaction, contract_method_nam # validate that the exact amounts are somewhat similar to the contract input values # (to make sure we're matching up the right values). input_tolerance = requested_input_quantity_float * 0.2 # 20% each side - if (exact_amount_in_float > requested_input_quantity_float + input_tolerance) \ - or (exact_amount_in_float < requested_input_quantity_float - input_tolerance): + if amount_in != 0 \ + and ((exact_amount_in_float > requested_input_quantity_float + input_tolerance) + or (exact_amount_in_float < requested_input_quantity_float - input_tolerance)): self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} method" f" {contract_method_name}, expected log decoded input quantity" f" {exact_amount_in_float} to be within 20% of the requested tx input quantity" f" {requested_input_quantity_float} but it's not.") output_tolerance = requested_output_quantity_float * 0.2 # 20% each side - if (exact_amount_out_float > requested_output_quantity_float + output_tolerance) \ - or (exact_amount_out_float < requested_output_quantity_float - output_tolerance): + if amount_out != 0 \ + and ((exact_amount_out_float > requested_output_quantity_float + output_tolerance) + or (exact_amount_out_float < requested_output_quantity_float - output_tolerance)): self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} method" f" {contract_method_name}, expected log decoded output quantity" f" {exact_amount_out_float} to be within 20% of the requested tx output quantity" @@ -715,7 +717,8 @@ async def __decode_add_liquidity_tx(self, account, transaction, contract_method_ # validate that the exact amounts are somewhat similar to the contract input values # (to make sure we're matching up the right values). - if 'requested_input_a_quantity_float' in locals() and requested_input_a_quantity_float is not None: + if 'requested_input_a_quantity_float' in locals() and requested_input_a_quantity_float is not None \ + and amount_in_a != 0: input_a_tolerance = exact_amount_in_a_float * 0.2 # 20% each side if (exact_amount_in_a_float > requested_input_a_quantity_float + input_a_tolerance) \ or (exact_amount_in_a_float < requested_input_a_quantity_float - input_a_tolerance): @@ -723,7 +726,8 @@ async def __decode_add_liquidity_tx(self, account, transaction, contract_method_ f" '{contract_method_name}', expected log decoded LP input A quantity" f" {exact_amount_in_a_float} to be within 20% of the requested tx input quantity" f" {requested_input_a_quantity_float} but it's not.") - if 'requested_input_b_quantity_float' in locals() and requested_input_b_quantity_float is not None: + if 'requested_input_b_quantity_float' in locals() and requested_input_b_quantity_float is not None \ + and amount_in_b != 0: input_b_tolerance = exact_amount_in_b_float * 0.2 # 20% each side if (exact_amount_in_b_float > requested_input_b_quantity_float + input_b_tolerance) \ or (exact_amount_in_b_float < requested_input_b_quantity_float - input_b_tolerance): @@ -731,7 +735,7 @@ async def __decode_add_liquidity_tx(self, account, transaction, contract_method_ f" '{contract_method_name}', expected log decoded LP input B quantity" f" {exact_amount_in_b_float} to be within 20% of the requested tx input quantity" f" {requested_input_b_quantity_float} but it's not.") - if 'amount_out' in locals() and amount_out is not None: # if variable 'amount_out' has been defined + if 'amount_out' in locals() and amount_out is not None and amount_out != 0: # if 'amount_out' has been defined output_tolerance = exact_amount_out_float * 0.2 # 20% each side if (exact_amount_out_float > amount_out + output_tolerance) \ or (exact_amount_out_float < amount_out - output_tolerance): @@ -890,22 +894,25 @@ async def __decode_remove_liquidity_tx(self, account, transaction, contract_meth # validate that the exact amounts are somewhat similar to the contract input values # (to make sure we're matching up the right values). input_tolerance = exact_amount_in_float * 0.2 # 20% each side - if (exact_amount_in_float > requested_input_quantity_float + input_tolerance) \ - or (exact_amount_in_float < requested_input_quantity_float - input_tolerance): + if input_liquidity_amount != 0 \ + and ((exact_amount_in_float > requested_input_quantity_float + input_tolerance) + or (exact_amount_in_float < requested_input_quantity_float - input_tolerance)): self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} method" f" '{contract_method_name}', expected log decoded LP input quantity" f" {exact_amount_in_float} to be within 20% of the requested tx input quantity" f" {requested_input_quantity_float} but it's not.") output_tolerance = exact_amount_out_a_float * 0.2 # 20% each side - if (exact_amount_out_a_float > requested_output_a_quantity_float + output_tolerance) \ - or (exact_amount_out_a_float < requested_output_a_quantity_float - output_tolerance): + if amount_out_a != 0 \ + and ((exact_amount_out_a_float > requested_output_a_quantity_float + output_tolerance) + or (exact_amount_out_a_float < requested_output_a_quantity_float - output_tolerance)): self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} method" f" '{contract_method_name}', expected log decoded LP output A quantity" f" {exact_amount_out_a_float} to be within 20% of the requested tx output quantity" f" {requested_output_a_quantity_float} but it's not.") output_tolerance = exact_amount_out_b_float * 0.2 # 20% each side - if (exact_amount_out_b_float > requested_output_b_quantity_float + output_tolerance) \ - or (exact_amount_out_b_float < requested_output_b_quantity_float - output_tolerance): + if amount_out_b != 0 \ + and ((exact_amount_out_b_float > requested_output_b_quantity_float + output_tolerance) + or (exact_amount_out_b_float < requested_output_b_quantity_float - output_tolerance)): self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} method" f" '{contract_method_name}', expected log decoded LP output B quantity" f" {exact_amount_out_b_float} to be within 20% of the requested tx output quantity" diff --git a/tests/test_moonbeam_scraper.py b/tests/test_moonbeam_scraper.py index e3f12a6..189eb14 100644 --- a/tests/test_moonbeam_scraper.py +++ b/tests/test_moonbeam_scraper.py @@ -68,7 +68,7 @@ async def test__no_entries_returned(): @pytest.mark.asyncio async def test__decode_token_swap_tx_Solarbeam__swapExactTokensForTokens(): # also testing: swap transaction on Solarbeam DEX. filter range on "timeStamp" - test_acct = "0xBa4123F4b2da090aeCef69Fd0946D42Ecd4C788E" + test_acct = "0xba4123f4b2da090aecef69fd0946d42ecd4c788e" config = { "moonriver": { "account_transactions": { @@ -234,9 +234,6 @@ async def test__decode_token_swap_tx_Huckleberry__swapExactTokensForTokensSuppor logging.info(f"begin 'test__decode_token_swap_tx_Huckleberry__swapExactTokensForTokensSupportingFeeOnTransferTokens'" f" scraping at {time.strftime('%X')}") items_scraped = await subscrape.scrape(config) - logging.info('Note: for this specific unit test, subscrape will emit a warning like "expected log decoded output' - ' quantity 3.02e-06 to be within 20% of the requested tx output quantity 0.0 but its not."' - ' Evidently the original contract call set amountOutMin=0 so this is expected behavior.') assert len(items_scraped) >= 1 transactions = _get_archived_transactions_from_json(test_acct, 'moonriver') transaction_found = False