Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes and cleanup #79

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Small fixes and cleanup #79

wants to merge 12 commits into from

Conversation

harisang
Copy link
Contributor

@harisang harisang commented Oct 15, 2024

This PR addresses the following issues:

  • Currently, we only fetch prices for tokens for which there is a non-zero imbalance. Due to network/protocol fees being implicit here, this is not enough as we might end up with slippage on tokens whose raw token imbalance is zero. With this PR, we fetch a price for all tokens for which there was a transfer to or from the settlement contract.

  • When multiple txs happen at the same block, the write_prices function that writes price entries in the database sometimes attempts to write a row that already exists in the db (e..g, when WETH is traded in both txs). This PR proposes to first check if the entry exists, and only proceed with writing if the entry is non-existent in the db.

@harisang harisang requested a review from fhenneke October 15, 2024 23:35
Copy link
Contributor

@fhenneke fhenneke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments.

Functionality looks good.

src/helpers/database.py Outdated Show resolved Hide resolved
for token_address, imbalance in token_imbalances.items():
if imbalance != 0:
transaction_tokens.append((tx_hash, token_address))
for token_address in token_imbalances.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reorganize code here.

Instead of fetching token_imbalances in the beginning, and then having this loop here, both should be moved into a self.imbalances.get_transaction_tokens function. That function would then be called here. The function self.blockchain_data.get_transaction_tokens should then be deleted.

Copy link
Contributor Author

@harisang harisang Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is still not clear where and how the raw imbalances will be used, i decided to keep it separate and not part of a get_transaction_tokens function.

Rearranged a few things and modified the test as well (not sure if you like how it looks like), in order to check the current version of the code. Ended up removing the test as it wasn't easy to make it work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One reason for separating functionality into functions is that it makes it possible to test things.

Moving a function into the main body and removing tests is not ideal.

I can handle moving the function into token imbalances if you do not have the capacity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i am attempting to do this now

@@ -72,6 +72,7 @@ def tests_write_prices():
f"postgresql+psycopg://postgres:postgres@localhost:5432/mainnet"
)
db = Database(engine, "mainnet")
# list contains duplicate entry in order to test how this is handled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this a separate test test_write_prices_duplicates. Otherwise we might get stuck with complicated tests as in solver-rewards where we have one huge test case with lots of settlements. It then becomes difficult to add new cases and a failed test does not show why it failed.

tests/unit/test_database.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants