2023-03-19 14:06:56 +00:00
|
|
|
from typing import Any, AsyncIterator, Dict, Optional
|
2023-03-17 19:44:00 +00:00
|
|
|
from uuid import uuid4
|
2021-01-02 11:54:40 +00:00
|
|
|
|
2023-06-01 05:03:07 +00:00
|
|
|
from fastapi import Depends, HTTPException
|
2022-01-22 06:11:59 +00:00
|
|
|
|
2023-06-02 07:50:40 +00:00
|
|
|
from freqtrade.constants import Config
|
2022-02-25 06:40:49 +00:00
|
|
|
from freqtrade.enums import RunMode
|
2021-10-16 15:57:51 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2023-03-17 19:44:00 +00:00
|
|
|
from freqtrade.persistence.models import _request_id_ctx_var
|
2023-05-21 07:08:52 +00:00
|
|
|
from freqtrade.rpc.api_server.webserver_bgwork import ApiBG
|
2021-01-02 12:12:49 +00:00
|
|
|
from freqtrade.rpc.rpc import RPC, RPCException
|
2021-01-02 11:54:40 +00:00
|
|
|
|
2020-12-25 12:08:25 +00:00
|
|
|
from .webserver import ApiServer
|
|
|
|
|
|
|
|
|
2021-01-02 12:12:49 +00:00
|
|
|
def get_rpc_optional() -> Optional[RPC]:
|
|
|
|
if ApiServer._has_rpc:
|
|
|
|
return ApiServer._rpc
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-03-19 14:06:56 +00:00
|
|
|
async def get_rpc() -> Optional[AsyncIterator[RPC]]:
|
2021-01-02 12:12:49 +00:00
|
|
|
_rpc = get_rpc_optional()
|
|
|
|
if _rpc:
|
2023-03-17 19:44:00 +00:00
|
|
|
request_id = str(uuid4())
|
|
|
|
ctx_token = _request_id_ctx_var.set(request_id)
|
2022-08-06 13:08:13 +00:00
|
|
|
Trade.rollback()
|
2023-03-17 19:44:00 +00:00
|
|
|
try:
|
|
|
|
yield _rpc
|
|
|
|
finally:
|
|
|
|
Trade.session.remove()
|
|
|
|
_request_id_ctx_var.reset(ctx_token)
|
|
|
|
|
2021-01-02 12:12:49 +00:00
|
|
|
else:
|
2024-05-12 14:51:11 +00:00
|
|
|
raise RPCException("Bot is not in the correct state")
|
2020-12-25 12:08:25 +00:00
|
|
|
|
|
|
|
|
2021-01-02 11:54:40 +00:00
|
|
|
def get_config() -> Dict[str, Any]:
|
2020-12-25 12:08:25 +00:00
|
|
|
return ApiServer._config
|
2020-12-27 14:24:49 +00:00
|
|
|
|
|
|
|
|
2021-01-02 11:54:40 +00:00
|
|
|
def get_api_config() -> Dict[str, Any]:
|
2024-05-12 14:51:11 +00:00
|
|
|
return ApiServer._config["api_server"]
|
2022-01-22 06:11:59 +00:00
|
|
|
|
|
|
|
|
2023-06-02 07:50:40 +00:00
|
|
|
def _generate_exchange_key(config: Config) -> str:
|
|
|
|
"""
|
|
|
|
Exchange key - used for caching the exchange object.
|
|
|
|
"""
|
|
|
|
return f"{config['exchange']['name']}_{config.get('trading_mode', 'spot')}"
|
|
|
|
|
|
|
|
|
2022-01-22 06:11:59 +00:00
|
|
|
def get_exchange(config=Depends(get_config)):
|
2023-06-02 07:50:40 +00:00
|
|
|
exchange_key = _generate_exchange_key(config)
|
|
|
|
if not (exchange := ApiBG.exchanges.get(exchange_key)):
|
2022-01-22 06:11:59 +00:00
|
|
|
from freqtrade.resolvers import ExchangeResolver
|
2024-05-12 14:51:11 +00:00
|
|
|
|
|
|
|
exchange = ExchangeResolver.load_exchange(config, validate=False, load_leverage_tiers=False)
|
2023-06-02 07:50:40 +00:00
|
|
|
ApiBG.exchanges[exchange_key] = exchange
|
|
|
|
return exchange
|
2022-02-25 06:40:49 +00:00
|
|
|
|
|
|
|
|
2022-11-15 03:27:45 +00:00
|
|
|
def get_message_stream():
|
|
|
|
return ApiServer._message_stream
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
|
2022-02-25 06:40:49 +00:00
|
|
|
def is_webserver_mode(config=Depends(get_config)):
|
2024-05-12 14:51:11 +00:00
|
|
|
if config["runmode"] != RunMode.WEBSERVER:
|
|
|
|
raise HTTPException(status_code=503, detail="Bot is not in the correct state.")
|
2022-02-25 06:40:49 +00:00
|
|
|
return None
|