Skip to content

Commit

Permalink
Merge pull request hummingbot#6868 from hummingbot/fix/mexc-api-update
Browse files Browse the repository at this point in the history
Fix/Fix MEXC failed to connect exchange
  • Loading branch information
nikspz authored Feb 23, 2024
2 parents d81a878 + 8f78f48 commit 748fce0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async def _request_order_book_snapshot(self, trading_pair: str) -> Dict[str, Any
params=params,
method=RESTMethod.GET,
throttler_limit_id=CONSTANTS.SNAPSHOT_PATH_URL,
headers={"Content-Type": "application/json"}
)

return data
Expand Down
2 changes: 1 addition & 1 deletion hummingbot/connector/exchange/mexc/mexc_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def add_auth_to_params(self,
return request_params

def header_for_authentication(self) -> Dict[str, str]:
return {"X-MEXC-APIKEY": self.api_key}
return {"X-MEXC-APIKEY": self.api_key, "Content-Type": "application/json"}

def _generate_signature(self, params: Dict[str, Any]) -> str:

Expand Down
28 changes: 22 additions & 6 deletions hummingbot/connector/exchange/mexc/mexc_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def supported_order_types(self):
return [OrderType.LIMIT, OrderType.LIMIT_MAKER, OrderType.MARKET]

async def get_all_pairs_prices(self) -> List[Dict[str, str]]:
pairs_prices = await self._api_get(path_url=CONSTANTS.TICKER_BOOK_PATH_URL)
pairs_prices = await self._api_get(path_url=CONSTANTS.TICKER_BOOK_PATH_URL, headers={"Content-Type": "application/json"})
return pairs_prices

def _is_request_exception_related_to_time_synchronizer(self, request_exception: Exception):
Expand Down Expand Up @@ -396,7 +396,8 @@ async def _update_order_fills_from_trades(self):
tasks.append(self._api_get(
path_url=CONSTANTS.MY_TRADES_PATH_URL,
params=params,
is_auth_required=True))
is_auth_required=True,
headers={"Content-Type": "application/json"}))

self.logger().debug(f"Polling for order fills of {len(tasks)} trading pairs.")
results = await safe_gather(*tasks, return_exceptions=True)
Expand Down Expand Up @@ -473,7 +474,8 @@ async def _all_trade_updates_for_order(self, order: InFlightOrder) -> List[Trade
"orderId": exchange_order_id
},
is_auth_required=True,
limit_id=CONSTANTS.MY_TRADES_PATH_URL)
limit_id=CONSTANTS.MY_TRADES_PATH_URL,
headers={"Content-Type": "application/json"})

for trade in all_fills_response:
exchange_order_id = str(trade["orderId"])
Expand Down Expand Up @@ -505,7 +507,8 @@ async def _request_order_status(self, tracked_order: InFlightOrder) -> OrderUpda
params={
"symbol": trading_pair,
"origClientOrderId": tracked_order.client_order_id},
is_auth_required=True)
is_auth_required=True,
headers={"Content-Type": "application/json"})

new_state = CONSTANTS.ORDER_STATE[updated_order_data["status"]]

Expand All @@ -525,7 +528,8 @@ async def _update_balances(self):

account_info = await self._api_get(
path_url=CONSTANTS.ACCOUNTS_PATH_URL,
is_auth_required=True)
is_auth_required=True,
headers={"Content-Type": "application/json"})

balances = account_info["balances"]
for balance_entry in balances:
Expand Down Expand Up @@ -556,7 +560,19 @@ async def _get_last_traded_price(self, trading_pair: str) -> float:
resp_json = await self._api_request(
method=RESTMethod.GET,
path_url=CONSTANTS.TICKER_PRICE_CHANGE_PATH_URL,
params=params
params=params,
headers={"Content-Type": "application/json"}
)

return float(resp_json["lastPrice"])

async def _make_network_check_request(self):
await self._api_get(path_url=self.check_network_request_path, headers={"Content-Type": "application/json"})

async def _make_trading_rules_request(self) -> Any:
exchange_info = await self._api_get(path_url=self.trading_rules_request_path, headers={"Content-Type": "application/json"})
return exchange_info

async def _make_trading_pairs_request(self) -> Any:
exchange_info = await self._api_get(path_url=self.trading_pairs_request_path, headers={"Content-Type": "application/json"})
return exchange_info
2 changes: 2 additions & 0 deletions hummingbot/connector/exchange_py_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ async def _api_request(
is_auth_required: bool = False,
return_err: bool = False,
limit_id: Optional[str] = None,
headers: Optional[Dict[str, Any]] = None,
**kwargs,
) -> Dict[str, Any]:

Expand All @@ -910,6 +911,7 @@ async def _api_request(
is_auth_required=is_auth_required,
return_err=return_err,
throttler_limit_id=limit_id if limit_id else path_url,
headers=headers,
)

return request_result
Expand Down
1 change: 1 addition & 0 deletions hummingbot/core/web_assistant/rest_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def execute_request_and_get_response(

local_headers = {
"Content-Type": ("application/json" if method != RESTMethod.GET else "application/x-www-form-urlencoded")}

local_headers.update(headers)

data = json.dumps(data) if data is not None else data
Expand Down
2 changes: 1 addition & 1 deletion test/hummingbot/connector/exchange/mexc/test_mexc_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ def test_rest_authenticate(self):
hashlib.sha256).hexdigest()
self.assertEqual(now * 1e3, configured_request.params["timestamp"])
self.assertEqual(expected_signature, configured_request.params["signature"])
self.assertEqual({"X-MEXC-APIKEY": self._api_key}, configured_request.headers)
self.assertEqual({"X-MEXC-APIKEY": self._api_key, "Content-Type": "application/json"}, configured_request.headers)

0 comments on commit 748fce0

Please sign in to comment.