Skip to content

Commit

Permalink
Standardize Bybit endpoint error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 7, 2024
1 parent 751ba25 commit 2d118fa
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 7 deletions.
1 change: 1 addition & 0 deletions nautilus_trader/adapters/bybit/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class BybitTransactionType(Enum):
@unique
class BybitEndpointType(Enum):
NONE = "NONE"
ASSET = "ASSET"
MARKET = "MARKET"
ACCOUNT = "ACCOUNT"
TRADE = "TRADE"
Expand Down
4 changes: 3 additions & 1 deletion nautilus_trader/adapters/bybit/endpoints/account/fee_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ async def get(self, params: BybitFeeRateGetParams) -> BybitFeeRateResponse:
try:
return self._get_resp_decoder.decode(raw)
except Exception as e:
raise RuntimeError(f"Failed to decode response fee rate response: {raw!s}") from e
raise RuntimeError(
f"Failed to decode response from {self.url_path}: {raw.decode()}",
) from e
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ async def get(self, params: PositionInfoGetParams) -> BybitPositionResponseStruc
try:
return self._get_resp_decoder.decode(raw)
except Exception as e:
decoded_raw = raw.decode("utf-8")
raise RuntimeError(
f"Failed to decode response position info response: {decoded_raw}",
f"Failed to decode response from {self.url_path}: {raw.decode()}",
) from e
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ async def get(self, params: BybitWalletBalanceGetParams) -> BybitWalletBalanceRe
try:
return self._get_resp_decoder.decode(raw)
except Exception as e:
decoded_raw = raw.decode("utf-8")
raise RuntimeError(
f"Failed to decode response wallet balance response: {decoded_raw}",
f"Failed to decode response from {self.url_path}: {raw.decode()}",
) from e
51 changes: 51 additions & 0 deletions nautilus_trader/adapters/bybit/endpoints/asset/coin_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------

import msgspec

from nautilus_trader.adapters.bybit.common.enums import BybitEndpointType
from nautilus_trader.adapters.bybit.endpoints.endpoint import BybitHttpEndpoint
from nautilus_trader.adapters.bybit.http.client import BybitHttpClient
from nautilus_trader.adapters.bybit.schemas.asset.coin_info import BybitCoinInfoResponse
from nautilus_trader.core.nautilus_pyo3 import HttpMethod


class BybitCoinInfoGetParams(msgspec.Struct, omit_defaults=True, frozen=False):
coin: str | None = None


class BybitCoinInfoEndpoint(BybitHttpEndpoint):
def __init__(
self,
client: BybitHttpClient,
base_endpoint: str,
) -> None:
self.http_method = HttpMethod.GET
url_path = base_endpoint + "/asset/coin/query-info"
super().__init__(
client=client,
endpoint_type=BybitEndpointType.ASSET,
url_path=url_path,
)
self._get_resp_decoder = msgspec.json.Decoder(BybitCoinInfoResponse)

async def get(self, params: BybitCoinInfoGetParams) -> BybitCoinInfoResponse:
raw = await self._method(self.http_method, params)
try:
return self._get_resp_decoder.decode(raw)
except Exception as e:
raise RuntimeError(
f"Failed to decode response from {self.url_path}: {raw.decode()}",
) from e
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async def get(self) -> BybitServerTimeResponse:
try:
return self._get_resp_decoder.decode(raw)
except Exception as e:
decoder_raw = raw.decode("utf-8")
raise RuntimeError(
f"Failed to decode response server time response: {decoder_raw}",
f"Failed to decode response from {self.url_path}: {raw.decode()}",
) from e
14 changes: 14 additions & 0 deletions nautilus_trader/adapters/bybit/schemas/asset/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------
50 changes: 50 additions & 0 deletions nautilus_trader/adapters/bybit/schemas/asset/coin_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------

from typing import Any

import msgspec as msgspec


class BybitCoinChainInfo(msgspec.Struct):
confirmation: str
chainType: str
withdrawFee: str
depositMin: str
withdrawMin: str
chain: str
chainDeposit: str
chainWithdraw: str
minAccuracy: str
withdrawPercentageFee: str


class BybitCoinInfo(msgspec.Struct):
name: str
coin: str
remainAmount: str
chains: list[BybitCoinChainInfo]


class BybitCoinInfoResult(msgspec.Struct):
rows: list[BybitCoinInfo]


class BybitCoinInfoResponse(msgspec.Struct):
retCode: int
retMsg: str
result: BybitCoinInfoResult
retExtInfo: dict[str, Any]
time: int

0 comments on commit 2d118fa

Please sign in to comment.