Skip to content
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

Proxy errors #73

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.34] - 2025-01-27
### Updated
- [Exchanges] handle proxy errors

## [1.2.33] - 2025-01-22
### Updated
- [Binance] fix binance futures check
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# trading-backend [1.2.33](https://github.com/Drakkar-Software/trading-backend/tree/master/CHANGELOG.md)
# trading-backend [1.2.34](https://github.com/Drakkar-Software/trading-backend/tree/master/CHANGELOG.md)
[![PyPI](https://img.shields.io/pypi/v/trading-backend.svg)](https://pypi.python.org/pypi/trading-backend/)
[![Downloads](https://pepy.tech/badge/trading-backend/month)](https://pepy.tech/project/trading-backend)
[![Github-Action-CI](https://github.com/Drakkar-Software/trading-backend/workflows/trading-backend-CI/badge.svg)](https://github.com/Drakkar-Software/trading-backend/actions)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from setuptools import find_packages
from setuptools import setup

VERSION = "1.2.33"
VERSION = "1.2.34"
PROJECT_NAME = "trading-backend"

PACKAGES = find_packages(exclude=["tests"])
Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ def add_headers(self, headers_dict):
for header_key, header_value in headers_dict.items():
self.client.headers[header_key] = header_value

def raise_or_prefix_proxy_error_if_relevant(self, cause_error, raised_error):
raise cause_error from (raised_error or cause_error)


class ExchangeManager:
def __init__(self, is_margin=False, is_future=False, is_sandboxed=False):
Expand Down
3 changes: 2 additions & 1 deletion trading_backend/exchanges/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ async def _inner_is_valid_account(self) -> (bool, str):
if self._exchange.exchange_manager.is_future and not self._exchange.exchange_manager.is_sandboxed:
# no way to check on futures accounts (raising AgentCode is not exist)
return True, None
details = await self._get_account_referral_details()
with self.error_describer():
details = await self._get_account_referral_details()
if not details.get("rebateWorking", False):
ref_id = details.get("referrerId", None)
if ref_id is not None:
Expand Down
14 changes: 12 additions & 2 deletions trading_backend/exchanges/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import ccxt
import contextlib

import trading_backend.errors
import trading_backend.enums
Expand Down Expand Up @@ -73,7 +74,8 @@ async def _inner_cancel_order(self):
async def _get_api_key_rights_using_order(self) -> list[trading_backend.enums.APIKeyRights]:
rights = [trading_backend.enums.APIKeyRights.READING]
try:
await self._inner_cancel_order()
with self.error_describer():
await self._inner_cancel_order()
except ccxt.AuthenticationError as err:
if "permission" in str(err).lower():
# does not have trading permission
Expand All @@ -92,7 +94,8 @@ async def _get_api_key_rights_using_order(self) -> list[trading_backend.enums.AP
async def _get_api_key_rights(self) -> list[trading_backend.enums.APIKeyRights]:
# default implementation: fetch portfolio and don't check
# todo implementation for each exchange as long as ccxt does not support it in unified api
await self._exchange.connector.client.fetch_balance()
with self.error_describer():
await self._exchange.connector.client.fetch_balance()
return [
trading_backend.enums.APIKeyRights.READING,
trading_backend.enums.APIKeyRights.SPOT_TRADING,
Expand Down Expand Up @@ -173,3 +176,10 @@ def _get_id(self):
if self._exchange.exchange_manager.is_margin:
return self.MARGIN_ID
return self.SPOT_ID

@contextlib.contextmanager
def error_describer(self):
try:
yield
except (ccxt.ExchangeNotAvailable, ccxt.AuthenticationError) as err:
self._exchange.connector.raise_or_prefix_proxy_error_if_relevant(err, None)
Loading