-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Daring logic prototype * Twitter posting fucntion for Microchain * Sending messages from general agent * First version * WIP * Fetching 1 tx instead of all at once * Tests added * Added DBManager so that we avoid opening/closing too many connections * Merges after PR * Changes before PR * Fixing CI * Fixed sql_handler test * Fixed test_messages_functions.py test * Fixed some more tests * Patching tests to not use DUNE_API_KEY in tests * Fixing test_messages_functions.py * Simple comment for triggering docker build * Update prediction_market_agent/agents/microchain_agent/messages_functions.py Co-authored-by: Peter Jung <[email protected]> * Update prediction_market_agent/agents/microchain_agent/messages_functions.py Co-authored-by: Peter Jung <[email protected]> * Unified fee property * Refactoring tests * New poetry file * Fixed mypy * Fixed circular import * Fixing pytest-docker * Update prediction_market_agent/db/blockchain_transaction_fetcher.py Co-authored-by: Peter Jung <[email protected]> * Update tests/agents/microchain/test_messages_functions.py Co-authored-by: Peter Jung <[email protected]> * Update tests/db/conftest.py Co-authored-by: Peter Jung <[email protected]> * Implementing PR comments * Improving tests * Implemented further PR comments * Fixing tests * Fixing incomplete refactoring * Removed unused variable --------- Co-authored-by: Peter Jung <[email protected]>
- Loading branch information
1 parent
c38d21d
commit 2691021
Showing
18 changed files
with
589 additions
and
221 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
prediction_market_agent/db/blockchain_message_table_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import typing as t | ||
|
||
from prediction_market_agent_tooling.gtypes import ChecksumAddress | ||
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes | ||
from sqlalchemy import ColumnElement | ||
from sqlmodel import col | ||
|
||
from prediction_market_agent.db.models import BlockchainMessage | ||
from prediction_market_agent.db.sql_handler import SQLHandler | ||
|
||
|
||
class BlockchainMessageTableHandler: | ||
def __init__( | ||
self, | ||
sqlalchemy_db_url: str | None = None, | ||
): | ||
self.sql_handler = SQLHandler( | ||
model=BlockchainMessage, sqlalchemy_db_url=sqlalchemy_db_url | ||
) | ||
|
||
def __build_consumer_column_filter( | ||
self, consumer_address: ChecksumAddress | ||
) -> ColumnElement[bool]: | ||
return col(BlockchainMessage.consumer_address) == consumer_address | ||
|
||
def fetch_latest_blockchain_message( | ||
self, consumer_address: ChecksumAddress | ||
) -> BlockchainMessage | None: | ||
query_filter = self.__build_consumer_column_filter(consumer_address) | ||
items: t.Sequence[ | ||
BlockchainMessage | ||
] = self.sql_handler.get_with_filter_and_order( | ||
query_filters=[query_filter], | ||
order_by_column_name=BlockchainMessage.block.key, # type: ignore[attr-defined] | ||
order_desc=True, | ||
limit=1, | ||
) | ||
return items[0] if items else None | ||
|
||
def fetch_all_transaction_hashes( | ||
self, consumer_address: ChecksumAddress | ||
) -> list[HexBytes]: | ||
query_filter = self.__build_consumer_column_filter(consumer_address) | ||
items: t.Sequence[ | ||
BlockchainMessage | ||
] = self.sql_handler.get_with_filter_and_order(query_filters=[query_filter]) | ||
tx_hashes = [HexBytes(i.transaction_hash) for i in items] | ||
return list(set(tx_hashes)) | ||
|
||
def save_multiple(self, items: t.Sequence[BlockchainMessage]) -> None: | ||
return self.sql_handler.save_multiple(items) |
89 changes: 89 additions & 0 deletions
89
prediction_market_agent/db/blockchain_transaction_fetcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import polars as pl | ||
import spice | ||
from eth_typing import ChecksumAddress | ||
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes | ||
from prediction_market_agent_tooling.tools.web3_utils import xdai_to_wei | ||
from web3 import Web3 | ||
|
||
from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import ( | ||
MicrochainAgentKeys, | ||
) | ||
from prediction_market_agent.agents.microchain_agent.utils import decompress_message | ||
from prediction_market_agent.db.blockchain_message_table_handler import ( | ||
BlockchainMessageTableHandler, | ||
) | ||
from prediction_market_agent.db.models import BlockchainMessage | ||
from prediction_market_agent.utils import APIKeys | ||
|
||
|
||
class BlockchainTransactionFetcher: | ||
def __init__(self) -> None: | ||
self.blockchain_table_handler = BlockchainMessageTableHandler() | ||
|
||
def unzip_message_else_do_nothing(self, data_field: str) -> str: | ||
"""We try decompressing the message, else we return the original data field.""" | ||
try: | ||
return decompress_message(HexBytes(data_field)) | ||
except: | ||
return data_field | ||
|
||
def fetch_unseen_transactions_df( | ||
self, consumer_address: ChecksumAddress | ||
) -> pl.DataFrame: | ||
keys = APIKeys() | ||
latest_blockchain_message = ( | ||
self.blockchain_table_handler.fetch_latest_blockchain_message( | ||
consumer_address | ||
) | ||
) | ||
min_block_number = ( | ||
0 if not latest_blockchain_message else latest_blockchain_message.block | ||
) | ||
# We order by block_time because it's used as partition on Dune. | ||
# We use >= for block because we might have lost transactions from the same block. | ||
# Additionally, processed tx_hashes are filtered out anyways. | ||
query = f'select * from gnosis.transactions where "to" = {Web3.to_checksum_address(consumer_address)} AND block_number >= {min_block_number} and value >= {xdai_to_wei(MicrochainAgentKeys().RECEIVER_MINIMUM_AMOUNT)} order by block_time asc' | ||
df = spice.query(query, api_key=keys.dune_api_key.get_secret_value()) | ||
|
||
existing_hashes = self.blockchain_table_handler.fetch_all_transaction_hashes( | ||
consumer_address=consumer_address | ||
) | ||
# Filter out existing hashes - hashes are by default lowercase | ||
df = df.filter(~pl.col("hash").is_in([i.hex() for i in existing_hashes])) | ||
return df | ||
|
||
def fetch_count_unprocessed_transactions( | ||
self, consumer_address: ChecksumAddress | ||
) -> int: | ||
df = self.fetch_unseen_transactions_df(consumer_address=consumer_address) | ||
return len(df) | ||
|
||
def fetch_one_unprocessed_blockchain_message_and_store_as_processed( | ||
self, consumer_address: ChecksumAddress | ||
) -> BlockchainMessage | None: | ||
""" | ||
Method for fetching oldest unprocessed transaction sent to the consumer address. | ||
After being fetched, it is stored in the DB as processed. | ||
""" | ||
df = self.fetch_unseen_transactions_df(consumer_address=consumer_address) | ||
if df.is_empty(): | ||
return None | ||
|
||
# We only want the oldest non-processed message. | ||
oldest_non_processed_message = df.row(0, named=True) | ||
blockchain_message = BlockchainMessage( | ||
consumer_address=consumer_address, | ||
transaction_hash=oldest_non_processed_message["hash"], | ||
value_wei=oldest_non_processed_message["value"], | ||
block=int(oldest_non_processed_message["block_number"]), | ||
sender_address=oldest_non_processed_message["from"], | ||
data_field=self.unzip_message_else_do_nothing( | ||
oldest_non_processed_message["data"] | ||
), | ||
) | ||
|
||
# Store here to avoid having to refresh after session was closed. | ||
item = blockchain_message.model_copy(deep=True) | ||
# mark unseen transaction as processed in DB | ||
self.blockchain_table_handler.save_multiple([blockchain_message]) | ||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.