-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1442b02
commit 86508af
Showing
10 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from typing import Union | ||
|
||
from dipdup.datasources.bcd.datasource import BcdDatasource | ||
from dipdup.datasources.coinbase.datasource import CoinbaseDatasource | ||
from dipdup.datasources.tzkt.datasource import TzktDatasource | ||
|
||
DatasourceT = Union[TzktDatasource, BcdDatasource] | ||
DatasourceT = Union[TzktDatasource, BcdDatasource, CoinbaseDatasource] |
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
Empty file.
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,60 @@ | ||
import logging | ||
from datetime import datetime, timedelta, timezone | ||
from typing import Any, Dict, List, Tuple | ||
|
||
from aiolimiter import AsyncLimiter | ||
|
||
from dipdup.datasources.coinbase.models import CandleData, CandleInterval | ||
from dipdup.datasources.proxy import DatasourceRequestProxy | ||
|
||
CANDLES_REQUEST_LIMIT = 300 | ||
REST_API_URL = 'https://api.pro.coinbase.com' | ||
WEBSOCKET_API_URL = 'wss://ws-feed.pro.coinbase.com' | ||
|
||
|
||
class CoinbaseDatasource: | ||
def __init__(self, cache: bool) -> None: | ||
self._logger = logging.getLogger('dipdup.coinbase') | ||
self._proxy = DatasourceRequestProxy( | ||
cache=cache, | ||
ratelimiter=AsyncLimiter(max_rate=10, time_period=1), | ||
) | ||
|
||
async def close_session(self) -> None: | ||
await self._proxy.close_session() | ||
|
||
async def run(self) -> None: | ||
pass | ||
|
||
async def resync(self) -> None: | ||
pass | ||
|
||
async def get_oracle_prices(self) -> Dict[str, Any]: | ||
return await self._proxy.http_request( | ||
'get', | ||
url=f'{REST_API_URL}/oracle', | ||
) | ||
|
||
async def get_candles(self, since: datetime, until: datetime, interval: CandleInterval, ticker: str = 'XTZ-USD') -> List[CandleData]: | ||
candles = [] | ||
for _since, _until in self._split_candle_requests(since, until, interval): | ||
candles_json = await self._proxy.http_request( | ||
'get', | ||
url=f'{REST_API_URL}/products/{ticker}/candles', | ||
params={ | ||
'start': _since.replace(tzinfo=timezone.utc).isoformat(), | ||
'end': _until.replace(tzinfo=timezone.utc).isoformat(), | ||
'granularity': interval.seconds, | ||
}, | ||
) | ||
candles += [CandleData.from_json(c) for c in candles_json] | ||
return sorted(candles, key=lambda c: c.timestamp) | ||
|
||
def _split_candle_requests(self, since: datetime, until: datetime, interval: CandleInterval) -> List[Tuple[datetime, datetime]]: | ||
request_interval_limit = timedelta(seconds=interval.seconds * CANDLES_REQUEST_LIMIT) | ||
request_intervals = [] | ||
while since + request_interval_limit < until: | ||
request_intervals.append((since, since + request_interval_limit)) | ||
since += request_interval_limit | ||
request_intervals.append((since, until)) | ||
return request_intervals |
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,47 @@ | ||
from datetime import datetime, timezone | ||
from decimal import Decimal | ||
from enum import Enum | ||
from typing import List, Union | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
|
||
class CandleInterval(Enum): | ||
ONE_MINUTE = 'ONE_MINUTE' | ||
FIVE_MINUTES = 'FIVE_MINUTES' | ||
FIFTEEN_MINUTES = 'FIFTEEN_MINUTES' | ||
ONE_HOUR = 'ONE_HOUR' | ||
SIX_HOURS = 'SIX_HOURS' | ||
ONE_DAY = 'ONE_DAY' | ||
|
||
@property | ||
def seconds(self) -> int: | ||
return { | ||
CandleInterval.ONE_MINUTE: 60, | ||
CandleInterval.FIVE_MINUTES: 300, | ||
CandleInterval.FIFTEEN_MINUTES: 900, | ||
CandleInterval.ONE_HOUR: 3600, | ||
CandleInterval.SIX_HOURS: 21600, | ||
CandleInterval.ONE_DAY: 86400, | ||
}[self] | ||
|
||
|
||
@dataclass | ||
class CandleData: | ||
timestamp: datetime | ||
low: Decimal | ||
high: Decimal | ||
open: Decimal | ||
close: Decimal | ||
volume: Decimal | ||
|
||
@classmethod | ||
def from_json(cls, json: List[Union[int, float]]) -> 'CandleData': | ||
return CandleData( | ||
timestamp=datetime.fromtimestamp(json[0], tz=timezone.utc), | ||
low=Decimal(str(json[1])), | ||
high=Decimal(str(json[2])), | ||
open=Decimal(str(json[3])), | ||
close=Decimal(str(json[4])), | ||
volume=Decimal(str(json[5])), | ||
) |
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