From 6853021954c89c38a125a060fb0e91f4690e2ed8 Mon Sep 17 00:00:00 2001 From: trizin <25263018+trizin@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:57:47 +0300 Subject: [PATCH] Tidy up workaround fixes (#1467) * function get_pair_timeframe_source_from_contract * use get_pair_timeframe_source_from_contract * formatting * tests * test fix * Fix pair --- pdr_backend/subgraph/info725.py | 20 ++++++++ .../subgraph/subgraph_feed_contracts.py | 9 ++-- .../subgraph/subgraph_pending_slots.py | 9 ++-- pdr_backend/subgraph/subgraph_predictions.py | 5 +- .../subgraph/subgraph_subscriptions.py | 5 +- pdr_backend/subgraph/test/test_info725.py | 50 +++++++++++++++++++ .../test/test_subgraph_feed_contracts.py | 2 +- .../test/test_subgraph_predictions.py | 4 +- .../test/test_subgraph_subscriptions.py | 2 +- 9 files changed, 86 insertions(+), 20 deletions(-) diff --git a/pdr_backend/subgraph/info725.py b/pdr_backend/subgraph/info725.py index 249f07751..f1a23ad80 100644 --- a/pdr_backend/subgraph/info725.py +++ b/pdr_backend/subgraph/info725.py @@ -7,6 +7,8 @@ from enforce_typing import enforce_types from web3 import Web3 +from pdr_backend.util.constants import WHITELIST_FEEDS_MAINNET + @enforce_types def key_to_key725(key: str): @@ -90,3 +92,21 @@ def info725_to_info(info725: list) -> Dict[str, Optional[str]]: break return info + + +def get_pair_timeframe_source_from_contract(contract): + if contract["token"]["nft"]: + info725 = contract["token"]["nft"]["nftData"] + info = info725_to_info(info725) # {"pair": "ETH/USDT", } + pair = info["pair"] + timeframe = info["timeframe"] + source = info["source"] + return (pair, timeframe, source) + + if contract["id"] in WHITELIST_FEEDS_MAINNET: + pair = contract["token"]["name"] + timeframe = "5m" if int(contract["secondsPerEpoch"]) == 300 else "1h" + source = "binance" # true for all mainnet contracts + return (pair, timeframe, source) + + raise Exception(f"Could not get pair, timeframe, source from contract: {contract}") diff --git a/pdr_backend/subgraph/subgraph_feed_contracts.py b/pdr_backend/subgraph/subgraph_feed_contracts.py index b3e94bb89..2bd912656 100644 --- a/pdr_backend/subgraph/subgraph_feed_contracts.py +++ b/pdr_backend/subgraph/subgraph_feed_contracts.py @@ -8,6 +8,7 @@ from enforce_typing import enforce_types from pdr_backend.subgraph.core_subgraph import query_subgraph +from pdr_backend.subgraph.info725 import get_pair_timeframe_source_from_contract from pdr_backend.subgraph.subgraph_feed import SubgraphFeed from pdr_backend.util.constants import WHITELIST_FEEDS_MAINNET @@ -75,11 +76,9 @@ def query_feed_contracts( if not contract_list: break for contract in contract_list: - pair = contract["token"]["name"] - timeframe = "5m" if int(contract["secondsPerEpoch"]) == 300 else "1h" - source = "binance" # fix me - if None in (pair, timeframe, source): - continue + pair, timeframe, source = get_pair_timeframe_source_from_contract( + contract + ) # filter out unwanted if not contract["token"]["nft"]: diff --git a/pdr_backend/subgraph/subgraph_pending_slots.py b/pdr_backend/subgraph/subgraph_pending_slots.py index e22836bcd..4ea25f3ed 100644 --- a/pdr_backend/subgraph/subgraph_pending_slots.py +++ b/pdr_backend/subgraph/subgraph_pending_slots.py @@ -9,6 +9,7 @@ from pdr_backend.cli.arg_feeds import ArgFeeds from pdr_backend.contract.slot import Slot from pdr_backend.subgraph.core_subgraph import query_subgraph +from pdr_backend.subgraph.info725 import get_pair_timeframe_source_from_contract from pdr_backend.subgraph.subgraph_feed import SubgraphFeed from pdr_backend.util.constants import WHITELIST_FEEDS_MAINNET from pdr_backend.util.time_types import UnixTimeS @@ -86,11 +87,9 @@ def get_pending_slots( continue contract = slot["predictContract"] - pair = contract["token"]["name"] - timeframe = "5m" if int(contract["secondsPerEpoch"]) == 300 else "1h" - source = "binance" # fix me - if None in (pair, timeframe, source): - continue + pair, timeframe, source = get_pair_timeframe_source_from_contract( + contract + ) assert pair, "need a pair" assert timeframe, "need a timeframe" diff --git a/pdr_backend/subgraph/subgraph_predictions.py b/pdr_backend/subgraph/subgraph_predictions.py index a843401f5..03f79c62a 100644 --- a/pdr_backend/subgraph/subgraph_predictions.py +++ b/pdr_backend/subgraph/subgraph_predictions.py @@ -11,6 +11,7 @@ from pdr_backend.lake.prediction import Prediction from pdr_backend.subgraph.core_subgraph import query_subgraph +from pdr_backend.subgraph.info725 import get_pair_timeframe_source_from_contract from pdr_backend.util.networkutil import get_subgraph_url from pdr_backend.util.time_types import UnixTimeS @@ -133,9 +134,7 @@ def fetch_filtered_predictions( for prediction_sg_dict in data: contract = prediction_sg_dict["slot"]["predictContract"] - pair = contract["token"]["name"] - timeframe = "5m" if int(contract["secondsPerEpoch"]) == 300 else "1h" - source = "binance" # fix me + pair, timeframe, source = get_pair_timeframe_source_from_contract(contract) timestamp = UnixTimeS(int(prediction_sg_dict["timestamp"])) slot = UnixTimeS(int(prediction_sg_dict["slot"]["slot"])) user = prediction_sg_dict["user"]["id"] diff --git a/pdr_backend/subgraph/subgraph_subscriptions.py b/pdr_backend/subgraph/subgraph_subscriptions.py index 563eac1e1..747d295a4 100644 --- a/pdr_backend/subgraph/subgraph_subscriptions.py +++ b/pdr_backend/subgraph/subgraph_subscriptions.py @@ -10,6 +10,7 @@ from pdr_backend.lake.subscription import Subscription from pdr_backend.subgraph.core_subgraph import query_subgraph +from pdr_backend.subgraph.info725 import get_pair_timeframe_source_from_contract from pdr_backend.util.networkutil import get_subgraph_url from pdr_backend.util.time_types import UnixTimeS @@ -105,9 +106,7 @@ def fetch_filtered_subscriptions( for subscription_sg_dict in data: contract = subscription_sg_dict["predictContract"] - pair = contract["token"]["name"] - timeframe = "5m" if int(contract["secondsPerEpoch"]) == 300 else "1h" - source = "binance" # fix me + pair, timeframe, source = get_pair_timeframe_source_from_contract(contract) timestamp = UnixTimeS(int(subscription_sg_dict["timestamp"])) tx_id = subscription_sg_dict["txId"] last_price_value = ( diff --git a/pdr_backend/subgraph/test/test_info725.py b/pdr_backend/subgraph/test/test_info725.py index 85d122d4a..288594e86 100644 --- a/pdr_backend/subgraph/test/test_info725.py +++ b/pdr_backend/subgraph/test/test_info725.py @@ -6,6 +6,7 @@ from web3 import Web3 from pdr_backend.subgraph.info725 import ( + get_pair_timeframe_source_from_contract, info725_to_info, info_to_info725, key_to_key725, @@ -87,3 +88,52 @@ def test_info_to_info725__missingkey(): {"key": key_to_key725("source"), "value": None}, ] assert info_to_info725(info) == info725 + + +@enforce_types +def test_get_pair_timeframe_source_from_contract_nft_data(): + contract_data = { + "token": { + "nft": { + "nftData": [ + { + "key": key_to_key725("pair"), + "value": value_to_value725("ETH/USDT"), + }, + { + "key": key_to_key725("timeframe"), + "value": value_to_value725("5m"), + }, + { + "key": key_to_key725("source"), + "value": value_to_value725("binance"), + }, + ] + } + } + } + + pair, timeframe, source = get_pair_timeframe_source_from_contract(contract_data) + assert pair == "ETH/USDT" + assert timeframe == "5m" + assert source == "binance" + + +@enforce_types +def test_get_pair_timeframe_source_from_contract_nft_data__missingkey(): + contract = { + "id": "0x3fb744c3702ff2237fc65f261046ead36656f3bc", + "secondsPerEpoch": "300", + "token": { + "id": "0x3fb744c3702ff2237fc65f261046ead36656f3bc", + "name": "BTC/USDT", + "symbol": "BTC/USDT", + "nft": None, + }, + } + + pair, timeframe, source = get_pair_timeframe_source_from_contract(contract) + + assert pair == "BTC/USDT" + assert timeframe == "5m" + assert source == "binance" diff --git a/pdr_backend/subgraph/test/test_subgraph_feed_contracts.py b/pdr_backend/subgraph/test/test_subgraph_feed_contracts.py index 2f013c9cd..f0db4058f 100644 --- a/pdr_backend/subgraph/test/test_subgraph_feed_contracts.py +++ b/pdr_backend/subgraph/test/test_subgraph_feed_contracts.py @@ -70,7 +70,7 @@ def test_query_feed_contracts__fullchain(monkeypatch): assert feed.seconds_per_subscription == 700 assert feed.trueval_submit_timeout == 5 assert feed.owner == "0xowner1" - assert feed.pair == "Name:contract1" + assert feed.pair == "BTC/USDT" assert feed.timeframe == "5m" assert feed.source == "binance" assert feed.seconds_per_epoch == 5 * 60 diff --git a/pdr_backend/subgraph/test/test_subgraph_predictions.py b/pdr_backend/subgraph/test/test_subgraph_predictions.py index 9e976a1d5..a08030765 100644 --- a/pdr_backend/subgraph/test/test_subgraph_predictions.py +++ b/pdr_backend/subgraph/test/test_subgraph_predictions.py @@ -22,7 +22,7 @@ # pylint: disable=line-too-long ID="0x18f54cc21b7a2fdd011bea06bba7801b280e3151-1698527100-0xd2a24cb4ff2584bad80ff5f109034a891c3d88dd", contract="0x18f54cc21b7a2fdd011bea06bba7801b280e3151", - pair="ADA/USDT", + pair="ADA-USDT", timeframe="5m", predvalue=True, stake=0.050051425480971974, @@ -131,7 +131,7 @@ def test_fetch_filtered_predictions(mock_query_subgraph): assert len(predictions) == 1000 assert isinstance(predictions[0], Prediction) assert predictions[0].user == "0xd2a24cb4ff2584bad80ff5f109034a891c3d88dd" - assert predictions[0].pair == "ADA-USDT" + assert predictions[0].pair == "ADA/USDT" assert predictions[0].contract == "0x18f54cc21b7a2fdd011bea06bba7801b280e3151" assert predictions[0].truevalue is False assert predictions[0].predvalue is True diff --git a/pdr_backend/subgraph/test/test_subgraph_subscriptions.py b/pdr_backend/subgraph/test/test_subgraph_subscriptions.py index 5f0de4c58..ac141965b 100644 --- a/pdr_backend/subgraph/test/test_subgraph_subscriptions.py +++ b/pdr_backend/subgraph/test/test_subgraph_subscriptions.py @@ -99,7 +99,7 @@ def test_fetch_filtered_subscriptions(mock_query_subgraph): assert len(subscriptions) == 1 assert isinstance(subscriptions[0], Subscription) assert subscriptions[0].user == "0x2433e002ed10b5d6a3d8d1e0c5d2083be9e37f1d" - assert subscriptions[0].pair == "ADA-USDT" + assert subscriptions[0].pair == "ADA/USDT" assert mock_query_subgraph.call_count == 1