Skip to content

Commit

Permalink
Tidy up workaround fixes (#1467)
Browse files Browse the repository at this point in the history
* function get_pair_timeframe_source_from_contract

* use get_pair_timeframe_source_from_contract

* formatting

* tests

* test fix

* Fix pair
  • Loading branch information
trizin authored Jul 29, 2024
1 parent ecf07fc commit 6853021
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 20 deletions.
20 changes: 20 additions & 0 deletions pdr_backend/subgraph/info725.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}")
9 changes: 4 additions & 5 deletions pdr_backend/subgraph/subgraph_feed_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]:
Expand Down
9 changes: 4 additions & 5 deletions pdr_backend/subgraph/subgraph_pending_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions pdr_backend/subgraph/subgraph_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]
Expand Down
5 changes: 2 additions & 3 deletions pdr_backend/subgraph/subgraph_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = (
Expand Down
50 changes: 50 additions & 0 deletions pdr_backend/subgraph/test/test_info725.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
2 changes: 1 addition & 1 deletion pdr_backend/subgraph/test/test_subgraph_feed_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/subgraph/test/test_subgraph_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pdr_backend/subgraph/test/test_subgraph_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 6853021

Please sign in to comment.