Skip to content

Commit

Permalink
Add namings for different feed objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Dec 20, 2023
1 parent 99ff441 commit f25c7cc
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 167 deletions.
8 changes: 4 additions & 4 deletions pdr_backend/models/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pdr_backend.util.timeframestr import Timeframe


class Feed(StrMixin): # pylint: disable=too-many-instance-attributes
class SubgraphFeed(StrMixin): # pylint: disable=too-many-instance-attributes
@enforce_types
def __init__(
self,
Expand Down Expand Up @@ -54,7 +54,7 @@ def __str__(self) -> str:


@enforce_types
def print_feeds(feeds: Dict[str, Feed], label: Optional[str] = None):
def print_feeds(feeds: Dict[str, SubgraphFeed], label: Optional[str] = None):
label = label or "feeds"
print(f"{len(feeds)} {label}:")
if not feeds:
Expand All @@ -76,10 +76,10 @@ def _rnd_eth_addr() -> str:


@enforce_types
def mock_feed(timeframe_str: str, exchange_str: str, pair_str: str) -> Feed:
def mock_feed(timeframe_str: str, exchange_str: str, pair_str: str) -> SubgraphFeed:
addr = _rnd_eth_addr()
name = f"Feed {addr} {pair_str}|{exchange_str}|{timeframe_str}"
feed = Feed(
feed = SubgraphFeed(
name=name,
address=addr,
symbol=f"SYM: {addr}",
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/models/slot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pdr_backend.models.feed import Feed
from pdr_backend.models.feed import SubgraphFeed


class Slot:
def __init__(self, slot_number: int, feed: Feed):
def __init__(self, slot_number: int, feed: SubgraphFeed):
self.slot_number = slot_number
self.feed = feed
4 changes: 2 additions & 2 deletions pdr_backend/models/test/test_feed.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from enforce_typing import enforce_types

from pdr_backend.models.feed import Feed, mock_feed, print_feeds
from pdr_backend.models.feed import SubgraphFeed, mock_feed, print_feeds


@enforce_types
def test_feed():
feed = Feed(
feed = SubgraphFeed(
"Contract Name",
"0x12345",
"SYM:TEST",
Expand Down
6 changes: 3 additions & 3 deletions pdr_backend/models/test/test_slot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pdr_backend.models.slot import Slot
from pdr_backend.models.feed import Feed
from pdr_backend.models.feed import SubgraphFeed


def test_slot_initialization():
feed = Feed(
feed = SubgraphFeed(
"Contract Name",
"0x12345",
"test",
Expand All @@ -20,4 +20,4 @@ def test_slot_initialization():

assert slot.slot_number == slot_number
assert slot.feed == feed
assert isinstance(slot.feed, Feed)
assert isinstance(slot.feed, SubgraphFeed)
21 changes: 12 additions & 9 deletions pdr_backend/ppss/data_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import numpy as np
from enforce_typing import enforce_types

from pdr_backend.models.feed import Feed as FeedMixin
from pdr_backend.util.feedstr import Feed, Feeds, verify_feeds_strs
from pdr_backend.models.feed import SubgraphFeed
from pdr_backend.util.feedstr import ArgFeed, ArgFeeds
from pdr_backend.util.listutil import remove_dups
from pdr_backend.util.pairstr import unpack_pair_str
from pdr_backend.util.timeframestr import Timeframe, verify_timeframe_str
Expand All @@ -17,7 +17,8 @@ def __init__(self, d: dict):

# test inputs
verify_timeframe_str(self.timeframe)
verify_feeds_strs(self.predict_feeds_strs)
ArgFeeds.from_strs(self.predict_feeds_strs) # test that it's valid

if not (0 < self.test_n < np.inf): # pylint: disable=superfluous-parens
raise ValueError(f"test_n={self.test_n}, must be >0 and <inf")

Expand Down Expand Up @@ -63,12 +64,12 @@ def timeframe_m(self) -> int:
return Timeframe(self.timeframe).m

@property
def predict_feeds(self) -> Feeds:
def predict_feeds(self) -> ArgFeeds:
"""
Return list of Feed(exchange_str, signal_str, pair_str)
E.g. [Feed("binance", "open", "ADA/USDT"), ...]
"""
return Feeds.from_strs(self.predict_feeds_strs)
return ArgFeeds.from_strs(self.predict_feeds_strs)

@property
def pair_strs(self) -> set:
Expand All @@ -81,7 +82,7 @@ def exchange_strs(self) -> str:
return remove_dups([feed.exchange for feed in self.predict_feeds])

@property
def predict_feed(self) -> Feed:
def predict_feed(self) -> ArgFeed:
"""
Return Feed(exchange_str, signal_str, pair_str)
E.g. Feed("binance", "open", "ADA/USDT")
Expand Down Expand Up @@ -123,7 +124,9 @@ def filter_feeds_s(self) -> str:
return f"{self.timeframe} {self.predict_feeds_strs}"

@enforce_types
def filter_feeds(self, cand_feeds: Dict[str, FeedMixin]) -> Dict[str, FeedMixin]:
def filter_feeds(
self, cand_feeds: Dict[str, SubgraphFeed]
) -> Dict[str, SubgraphFeed]:
"""
@description
Filter to feeds that fit self.predict_feeds'
Expand All @@ -139,10 +142,10 @@ def filter_feeds(self, cand_feeds: Dict[str, FeedMixin]) -> Dict[str, FeedMixin]
(self.timeframe, feed.exchange, feed.pair) for feed in self.predict_feeds
]

final_feeds: Dict[str, FeedMixin] = {}
final_feeds: Dict[str, SubgraphFeed] = {}
found_tups = set() # to avoid duplicates
for feed in cand_feeds.values():
assert isinstance(feed, FeedMixin)
assert isinstance(feed, SubgraphFeed)
feed_tup = (feed.timeframe, feed.source, feed.pair)
if feed_tup in allowed_tups and feed_tup not in found_tups:
final_feeds[feed.address] = feed
Expand Down
11 changes: 5 additions & 6 deletions pdr_backend/ppss/data_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from enforce_typing import enforce_types

from pdr_backend.ppss.data_pp import DataPP
from pdr_backend.util.feedstr import Feeds, verify_feeds_strs
from pdr_backend.util.feedstr import ArgFeeds
from pdr_backend.util.timeutil import pretty_timestr, timestr_to_ut


Expand All @@ -31,11 +31,10 @@ def __init__(self, d: dict):
)
assert 0 < self.max_n_train
assert 0 < self.autoregressive_n < np.inf
verify_feeds_strs(self.input_feeds_strs)

# save self.exchs_dict
self.exchs_dict: dict = {} # e.g. {"binance" : ccxt.binance()}
feeds = Feeds.from_strs(self.input_feeds_strs)
feeds = ArgFeeds.from_strs(self.input_feeds_strs)
for feed in feeds:
exchange_class = getattr(ccxt, feed.exchange)
self.exchs_dict[feed.exchange] = exchange_class()
Expand Down Expand Up @@ -110,9 +109,9 @@ def n_input_feeds(self) -> int:
return len(self.input_feeds)

@property
def input_feeds(self) -> Feeds:
"""Return list of Feed(exchange_str, signal_str, pair_str)"""
return Feeds.from_strs(self.input_feeds_strs)
def input_feeds(self) -> ArgFeeds:
"""Return list of ArgFeed(exchange_str, signal_str, pair_str)"""
return ArgFeeds.from_strs(self.input_feeds_strs)

@property
def exchange_pair_tups(self) -> Set[Tuple[str, str]]:
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/ppss/ppss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enforce_typing import enforce_types
import yaml

from pdr_backend.models.feed import Feed, mock_feed
from pdr_backend.models.feed import SubgraphFeed, mock_feed
from pdr_backend.ppss.data_pp import DataPP
from pdr_backend.ppss.data_ss import DataSS
from pdr_backend.ppss.dfbuyer_ss import DFBuyerSS
Expand Down Expand Up @@ -82,7 +82,7 @@ def mock_feed_ppss(
pair,
network: Optional[str] = None,
tmpdir=None,
) -> Tuple[Feed, PPSS]:
) -> Tuple[SubgraphFeed, PPSS]:
feed = mock_feed(timeframe, exchange, pair)
ppss = mock_ppss(timeframe, [f"{exchange} c {pair}"], network, tmpdir)
return (feed, ppss)
Expand Down
12 changes: 6 additions & 6 deletions pdr_backend/ppss/test/test_data_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pdr_backend.models.feed import mock_feed
from pdr_backend.ppss.data_pp import DataPP, mock_data_pp
from pdr_backend.util.feedstr import Feed
from pdr_backend.util.feedstr import ArgFeed
from pdr_backend.util.mathutil import sole_value


Expand All @@ -25,10 +25,10 @@ def test_data_pp_1feed():
# derivative properties
assert isinstance(pp.timeframe_ms, int) # test more below
assert isinstance(pp.timeframe_m, int) # ""
assert pp.predict_feeds == [Feed("kraken", "high", "ETH/USDT")]
assert pp.predict_feeds == [ArgFeed("kraken", "high", "ETH/USDT")]
assert pp.pair_strs == ["ETH/USDT"]
assert pp.exchange_strs == ["kraken"]
assert pp.predict_feed == Feed("kraken", "high", "ETH/USDT")
assert pp.predict_feed == ArgFeed("kraken", "high", "ETH/USDT")
assert pp.exchange_str == "kraken"
assert pp.signal_str == "high"
assert pp.pair_str == "ETH/USDT"
Expand All @@ -55,9 +55,9 @@ def test_data_pp_3feeds():

# derivative properties
assert pp.predict_feeds == [
Feed("kraken", "high", "ETH/USDT"),
Feed("binance", "open", "BTC/USDT"),
Feed("binance", "high", "BTC/USDT"),
ArgFeed("kraken", "high", "ETH/USDT"),
ArgFeed("binance", "open", "BTC/USDT"),
ArgFeed("binance", "high", "BTC/USDT"),
]
assert pp.pair_strs == ["ETH/USDT", "BTC/USDT"]
assert pp.exchange_strs == ["kraken", "binance"]
Expand Down
12 changes: 6 additions & 6 deletions pdr_backend/ppss/test/test_data_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pdr_backend.ppss.data_pp import DataPP
from pdr_backend.ppss.data_ss import DataSS
from pdr_backend.util.feedstr import Feed, Feeds
from pdr_backend.util.feedstr import ArgFeed, ArgFeeds
from pdr_backend.util.timeutil import timestr_to_ut

_D = {
Expand Down Expand Up @@ -37,12 +37,12 @@ def test_data_ss_basic():
# derivative properties
assert ss.st_timestamp == timestr_to_ut("2023-06-18")
assert ss.fin_timestamp == timestr_to_ut("2023-06-21")
assert ss.input_feeds == Feeds(
assert ss.input_feeds == ArgFeeds(
[
Feed("kraken", "high", "ETH/USDT"),
Feed("kraken", "close", "ETH/USDT"),
Feed("binanceus", "high", "ETH/USDT"),
Feed("binanceus", "high", "TRX/DAI"),
ArgFeed("kraken", "high", "ETH/USDT"),
ArgFeed("kraken", "close", "ETH/USDT"),
ArgFeed("binanceus", "high", "ETH/USDT"),
ArgFeed("binanceus", "high", "TRX/DAI"),
]
)
assert ss.exchange_pair_tups == set(
Expand Down
16 changes: 9 additions & 7 deletions pdr_backend/ppss/web3_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from eth_account.signers.local import LocalAccount
from web3 import Web3

from pdr_backend.models.feed import Feed
from pdr_backend.models.feed import SubgraphFeed
from pdr_backend.models.slot import Slot
from pdr_backend.subgraph.subgraph_pending_slots import get_pending_slots
from pdr_backend.subgraph.subgraph_feed_contracts import query_feed_contracts
Expand Down Expand Up @@ -86,21 +86,21 @@ def w3(self) -> Optional[Web3]:
# --------------------------------
# onchain feed data
@enforce_types
def query_feed_contracts(self) -> Dict[str, Feed]:
def query_feed_contracts(self) -> Dict[str, SubgraphFeed]:
"""
@description
Gets all feeds, only filtered by self.owner_addrs
@return
feeds -- dict of [feed_addr] : Feed
feeds -- dict of [feed_addr] : SubgraphFeed
"""
feeds = query_feed_contracts(
subgraph_url=self.subgraph_url,
owners_string=self.owner_addrs,
)
# postconditions
for feed in feeds.values():
assert isinstance(feed, Feed)
assert isinstance(feed, SubgraphFeed)
return feeds

@enforce_types
Expand Down Expand Up @@ -174,7 +174,7 @@ def mock_web3_pp(network: str) -> Web3PP:


@enforce_types
def inplace_mock_feedgetters(web3_pp, feed: Feed):
def inplace_mock_feedgetters(web3_pp, feed: SubgraphFeed):
from pdr_backend.models.predictoor_contract import ( # pylint: disable=import-outside-toplevel
mock_predictoor_contract,
)
Expand All @@ -186,13 +186,15 @@ def inplace_mock_feedgetters(web3_pp, feed: Feed):


@enforce_types
def inplace_mock_query_feed_contracts(web3_pp: Web3PP, feed: Feed):
def inplace_mock_query_feed_contracts(web3_pp: Web3PP, feed: SubgraphFeed):
web3_pp.query_feed_contracts = Mock()
web3_pp.query_feed_contracts.return_value = {feed.address: feed}


@enforce_types
def inplace_mock_get_contracts(web3_pp: Web3PP, feed: Feed, predictoor_contract):
def inplace_mock_get_contracts(
web3_pp: Web3PP, feed: SubgraphFeed, predictoor_contract
):
from pdr_backend.models.predictoor_contract import ( # pylint: disable=import-outside-toplevel
PredictoorContract,
)
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/publisher/publish_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pdr_backend.ppss.web3_pp import Web3PP
from pdr_backend.publisher.publish_asset import publish_asset
from pdr_backend.util.contract import get_address
from pdr_backend.util.feedstr import Feeds
from pdr_backend.util.feedstr import ArgFeeds
from pdr_backend.util.pairstr import unpack_pair_str
from pdr_backend.util.timeframestr import Timeframe

Expand Down Expand Up @@ -38,7 +38,7 @@ def publish_assets(web3_pp: Web3PP, publisher_ss: PublisherSS):
raise ValueError(web3_pp.network)

for timeframe_str in timeframe_strs:
feeds = Feeds.from_strs(feeds_strs)
feeds = ArgFeeds.from_strs(feeds_strs)
for feed in feeds:
base_str, quote_str = unpack_pair_str(feed.pair)
publish_asset(
Expand Down
13 changes: 7 additions & 6 deletions pdr_backend/subgraph/subgraph_feed_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from enforce_typing import enforce_types

from pdr_backend.models.feed import Feed
from pdr_backend.models.feed import SubgraphFeed
from pdr_backend.subgraph.core_subgraph import query_subgraph
from pdr_backend.subgraph.info725 import info725_to_info

Expand All @@ -14,7 +14,7 @@
def query_feed_contracts(
subgraph_url: str,
owners_string: Optional[str] = None,
) -> Dict[str, Feed]:
) -> Dict[str, SubgraphFeed]:
"""
@description
Query the chain for prediction feed contracts.
Expand All @@ -24,15 +24,15 @@ def query_feed_contracts(
owners -- E.g. filter to "0x123,0x124". If None or "", allow all
@return
feeds -- dict of [feed_addr] : Feed
feeds -- dict of [feed_addr] : SubgraphFeed
"""
owners = None
if owners_string:
owners = owners_string.lower().split(",")

chunk_size = 1000 # max for subgraph = 1000
offset = 0
feeds: Dict[str, Feed] = {}
feeds: Dict[str, SubgraphFeed] = {}

while True:
query = """
Expand Down Expand Up @@ -84,7 +84,7 @@ def query_feed_contracts(
continue

# ok, add this one
feed = Feed(
feed = SubgraphFeed(
name=contract["token"]["name"],
address=contract["id"],
symbol=contract["token"]["symbol"],
Expand Down Expand Up @@ -116,5 +116,6 @@ def query_feed_contracts(

# postconditions
for feed in feeds.values():
assert isinstance(feed, Feed)
assert isinstance(feed, SubgraphFeed)

return feeds
Loading

0 comments on commit f25c7cc

Please sign in to comment.