Skip to content

Commit

Permalink
don't check "output vs input" tolerance if zero
Browse files Browse the repository at this point in the history
* 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 ChaosDAO-org#17
  • Loading branch information
spazcoin committed Feb 19, 2023
1 parent 4357091 commit 0be6efc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions bin/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 20 additions & 13 deletions subscrape/scrapers/moonbeam_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -715,23 +717,25 @@ 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):
self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} 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):
self.logger.warning(f"For transaction {tx_hash} with contract {contract_address} 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):
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 1 addition & 4 deletions tests/test_moonbeam_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0be6efc

Please sign in to comment.