-
Notifications
You must be signed in to change notification settings - Fork 1
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
CERT 7649 Allow dynamic price feed providers #20
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
162918e
CERT 7649 Allow dynamic price feed providers
nivcertora 8552c77
Address comments
nivcertora 7e938bb
Fix
nivcertora 7de0a7a
New design + add cache to prevent api dealy
nivcertora 9ae90f5
Merge 7de0a7a523c9406a6f4a6f6d3d0e98570b4b8b06 into 23cef171762b95d14…
nivcertora b6b0116
Address comments
nivcertora 87f3dd3
Auto change version.
nivcertora 1441b95
Update Readme
nivcertora 9b39541
Merge branch 'niv/CERT-7649-Support-Different-providers' of github.co…
nivcertora e7bd5f0
Address comments
nivcertora 69acf55
Merge e7bd5f0dc7bd41c17b21c3d0bdd11fc1dc3eb81f into 23cef171762b95d14…
nivcertora b4a2bbb
Auto change version.
nivcertora 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
**/local* | ||
build/ | ||
*.egg-info | ||
CustomerClones/ | ||
CustomerClones/ | ||
**cache** |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .chainlink_api import ChainLinkAPI | ||
from .chronicle_api import ChronicleAPI | ||
from .price_feed_utils import PriceFeedData, PriceFeedProvider, PriceFeedProviderBase | ||
|
||
all = [ChainLinkAPI, ChronicleAPI] | ||
all = [ChainLinkAPI, ChronicleAPI, PriceFeedData, PriceFeedProvider, PriceFeedProviderBase] |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import StrEnum | ||
from typing import Optional | ||
from pydantic import BaseModel, Field | ||
from pathlib import Path | ||
import json | ||
import requests | ||
|
||
from Quorum.utils.chain_enum import Chain | ||
|
||
class PriceFeedProvider(StrEnum): | ||
""" | ||
Enumeration for supported price feed providers. | ||
""" | ||
CHAINLINK = 'Chainlink' | ||
CHRONICLE = 'Chronicle' | ||
|
||
class PriceFeedData(BaseModel): | ||
name: Optional[str] | ||
pair: Optional[str | list] | ||
address: str = Field(..., alias='contractAddress') | ||
proxy_address: Optional[str] = Field(None, alias='proxyAddress') | ||
|
||
class Config: | ||
allow_population_by_field_name = True # Allows population using field names | ||
extra = 'ignore' # Ignores extra fields not defined in the model | ||
|
||
|
||
class PriceFeedProviderBase(ABC): | ||
""" | ||
PriceFeedProviderBase is an abstract base class for price feed providers. | ||
It defines the interface for fetching price feed data for different blockchain networks. | ||
|
||
Attributes: | ||
cache_dir (Path): The directory path to store the fetched price feed data. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.cache_dir = Path(__file__).parent / "cache" / self.get_name().value | ||
self.cache_dir.mkdir(parents=True, exist_ok=True) | ||
self.session = requests.Session() | ||
self.memory: dict[Chain, dict[str, PriceFeedData]] = {} | ||
|
||
def get_feeds(self, chain: Chain) -> dict[str, PriceFeedData]: | ||
""" | ||
Get price feed data for a given blockchain network from the cache. | ||
|
||
Args: | ||
chain (Chain): The blockchain network to fetch price feed data for. | ||
|
||
Returns: | ||
dict[str, PriceFeedData]: A dictionary mapping the contract address of the price feed to the price feed data. | ||
""" | ||
cache_file = self.cache_dir / f"{chain.value}.json" | ||
if cache_file.exists(): | ||
with open(cache_file, 'r') as file: | ||
data: dict = json.load(file) | ||
self.memory[chain] = {addr: PriceFeedData(**feed) for addr, feed in data.items()} | ||
else: | ||
if chain not in self.memory: | ||
self.memory[chain] = self._get_price_feeds_info(chain) | ||
with open(cache_file, 'w') as file: | ||
json.dump({addr: feed.dict() for addr, feed in self.memory[chain].items()}, file) | ||
return self.memory[chain] | ||
|
||
@abstractmethod | ||
def _get_price_feeds_info(self, chain: Chain) -> dict[str, PriceFeedData]: | ||
pass | ||
|
||
@abstractmethod | ||
def get_name(self) -> PriceFeedProvider: | ||
pass |
Oops, something went wrong.
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.
I may be missing something here but price feed data isn't updating? I mean I see you never update the cache only create it or read from it.
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.
Create is the update, the same as it worked before