-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fixes data collection for market liquidity removal by using subgraph instead of filtering on-chain
events
#42
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import json | ||
import random | ||
from abc import ABC | ||
from string import Template | ||
from typing import Any, Dict, Generator, List, Optional, Set, Tuple, Type, cast | ||
|
||
from packages.valory.connections.openai.connection import ( | ||
|
@@ -112,6 +113,35 @@ | |
|
||
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" | ||
|
||
CONDITIONS_QUERY = Template( | ||
"""{ | ||
conditions(where:{id_in:$conditions}){ | ||
id, | ||
oracle, | ||
questionId, | ||
outcomeSlotCount | ||
} | ||
} | ||
""" | ||
) | ||
|
||
QUESTIONS_QUERY = Template( | ||
"""{ | ||
questions(where:{id_in:$conditions}){ | ||
id, | ||
openingTimestamp | ||
} | ||
}""" | ||
) | ||
|
||
|
||
def to_content(query: str) -> bytes: | ||
"""Convert the given query string to payload content, i.e., add it under a `queries` key and convert it to bytes.""" | ||
finalized_query = {"query": query} | ||
encoded_query = json.dumps(finalized_query, sort_keys=True).encode("utf-8") | ||
|
||
return encoded_query | ||
|
||
|
||
def parse_date_timestring(string: str) -> Optional[datetime.datetime]: | ||
"""Parse and return a datetime string.""" | ||
|
@@ -364,11 +394,12 @@ def get_markets_to_removal_ts( | |
condition_id_to_market = {} | ||
for market in markets: | ||
for condition_id in market["condition_ids"]: | ||
condition_id_to_market[condition_id] = market | ||
condition_id_to_market[f"0x{condition_id.hex()}"] = market | ||
condition_ids = list(condition_id_to_market.keys()) | ||
condition_preparations = yield from self._get_condition_preparation_events( | ||
condition_ids, from_block | ||
condition_ids=condition_ids | ||
) | ||
self.context.logger.info(f"Conditions: {condition_preparations}") | ||
if condition_preparations is None: | ||
# something went wrong | ||
return None | ||
|
@@ -390,7 +421,8 @@ def get_markets_to_removal_ts( | |
question_ids = [ | ||
preparation["question_id"] for preparation in condition_preparations | ||
] | ||
questions = yield from self._get_questions(question_ids, from_block) | ||
self.context.logger.info(f"Questions: {question_ids}") | ||
questions = yield from self._get_questions(question_ids=question_ids) | ||
if questions is None: | ||
# something went wrong | ||
return None | ||
|
@@ -474,51 +506,36 @@ def _get_markets_with_funds( | |
return markets_with_liq | ||
|
||
def _get_condition_preparation_events( | ||
self, | ||
condition_ids: List[bytes], | ||
from_block: int, | ||
self, condition_ids: List[str] | ||
) -> Generator[None, None, Optional[List[Dict[str, Any]]]]: | ||
"""Get condition preparation events.""" | ||
contract_api_response = yield from self.get_contract_api_response( | ||
performative=ContractApiMessage.Performative.GET_STATE, | ||
contract_address=self.params.conditional_tokens_contract, | ||
contract_id=str(ConditionalTokensContract.contract_id), | ||
contract_callable="get_condition_preparation_events", | ||
condition_ids=condition_ids, | ||
from_block=from_block, | ||
) | ||
if contract_api_response.performative != ContractApiMessage.Performative.STATE: | ||
self.context.logger.error( | ||
f"Failed to get condition preparation events: {contract_api_response}" | ||
) | ||
return None | ||
condition_preparation_events = cast( | ||
Optional[List[Dict[str, Any]]], | ||
contract_api_response.state.body.get("data", []), | ||
query = CONDITIONS_QUERY.substitute(conditions=json.dumps(condition_ids)) | ||
response = yield from self.get_http_response( | ||
content=to_content(query), | ||
**self.context.omen_subgraph.get_spec(), | ||
) | ||
data = json.loads(response.body.decode()) | ||
condition_preparation_events = data["data"]["conditions"] | ||
for condition in condition_preparation_events: | ||
condition["condition_id"] = condition.pop("id") | ||
condition["question_id"] = condition.pop("questionId") | ||
condition["outcome_slot_count"] = condition.pop("outcomeSlotCount") | ||
return condition_preparation_events | ||
|
||
def _get_questions( | ||
self, question_ids: List[str], from_block: int | ||
self, question_ids: List[str] | ||
) -> Generator[None, None, Optional[List[Dict[str, Any]]]]: | ||
"""Get question ids.""" | ||
contract_api_response = yield from self.get_contract_api_response( | ||
performative=ContractApiMessage.Performative.GET_STATE, | ||
contract_address=self.params.realitio_contract, | ||
contract_id=str(RealtioContract.contract_id), | ||
contract_callable="get_question_events", | ||
question_ids=question_ids, | ||
from_block=from_block, | ||
) | ||
if contract_api_response.performative != ContractApiMessage.Performative.STATE: | ||
self.context.logger.error( | ||
f"Failed to get question: {contract_api_response}" | ||
) | ||
return None | ||
questions = cast( | ||
Optional[List[Dict[str, Any]]], | ||
contract_api_response.state.body.get("data", []), | ||
query = QUESTIONS_QUERY.substitute(conditions=json.dumps(question_ids)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
response = yield from self.get_http_response( | ||
content=to_content(query), | ||
**self.context.omen_subgraph.get_spec(), | ||
) | ||
data = json.loads(response.body.decode()) | ||
questions = data["data"]["questions"] | ||
for condition in questions: | ||
condition["question_id"] = condition.pop("id") | ||
condition["opening_ts"] = int(condition.pop("openingTimestamp")) | ||
return questions | ||
|
||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to add a retry mechanism here, but first let's test if this works or not