2022-10-30 19:11:07 +00:00
|
|
|
from pydantic import ValidationError
|
|
|
|
|
2022-10-30 10:57:53 +00:00
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
|
|
|
|
class BaseExchange(object):
|
|
|
|
def __init__(self, account):
|
|
|
|
name = self.__class__.__name__
|
|
|
|
self.account = account
|
|
|
|
self.log = logs.get_logger(name)
|
|
|
|
self.client = None
|
|
|
|
|
2022-10-30 19:11:07 +00:00
|
|
|
self.set_schema()
|
2022-10-30 10:57:53 +00:00
|
|
|
self.connect()
|
|
|
|
|
2022-10-30 19:11:07 +00:00
|
|
|
def set_schema(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-10-30 10:57:53 +00:00
|
|
|
def connect(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-10-30 19:11:07 +00:00
|
|
|
def call(self, method, *args, **kwargs) -> (bool, dict):
|
|
|
|
|
|
|
|
if hasattr(self.client, method):
|
|
|
|
try:
|
|
|
|
response = getattr(self.client, method)(*args, **kwargs)
|
|
|
|
if isinstance(response, list):
|
|
|
|
response = {"itemlist": response}
|
|
|
|
if method not in self.schema:
|
|
|
|
self.log.error(f"Method cannot be validated: {method}")
|
|
|
|
self.log.debug(f"Response: {response}")
|
|
|
|
return (False, f"Method cannot be validated: {method}")
|
|
|
|
return (True, self.schema[method](**response).dict())
|
|
|
|
except ValidationError as e:
|
|
|
|
self.log.error(f"Could not validate response: {e}")
|
|
|
|
return (False, e)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.error(f"Error calling {method}: {e}")
|
|
|
|
return (False, e)
|
|
|
|
else:
|
|
|
|
return (False, "No such method")
|
|
|
|
|
2022-10-30 11:21:48 +00:00
|
|
|
def get_account(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-10-30 10:57:53 +00:00
|
|
|
def get_supported_assets(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_balance(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_market_value(self, symbol):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def post_trade(self, trade):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_trade(self, trade_id):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def update_trade(self, trade):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def cancel_trade(self, trade_id):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_position_info(self, asset_id):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_all_positions(self):
|
|
|
|
raise NotImplementedError
|