chore: ruff py313, conftest unification, audit log, app factory comune
- pyproject.toml: ruff target-version py311 → py313 (auto-fix 42 lint warnings via UP rules); aggiunto consider_namespace_packages = true che risolve la collisione conftest tra servizi e permette di lanciare pytest sull'intera suite cross-servizio. - mcp_common.audit: nuovo helper audit_write_op() con logger dedicato mcp.audit. Wirato su tutti i write endpoint di deribit, bybit, alpaca e hyperliquid (place_order, place_combo_order, cancel_*, set_*, close_*, transfer_*, switch_*, amend_*) con principal + target + payload non-sensibile + result summarizzato. - mcp_common.app_factory: ExchangeAppSpec + run_exchange_main() centralizza il boilerplate dei __main__.py (configure_root_logging, fail_fast_if_missing, summarize, load creds, resolve_environment, load token store, uvicorn). I 4 __main__.py exchange ridotti da ~60 LOC ognuno a ~25 LOC dichiarativi. mcp_common.env_validation promosso da mcp_deribit (mantenuto re-export shim per back-compat test_env_validation). - 8 test nuovi (4 audit + 4 app_factory). Suite full: 450/450 verdi. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,56 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
from mcp_common.auth import load_token_store_from_files
|
||||
from mcp_common.environment import resolve_environment
|
||||
from mcp_common.logging import configure_root_logging
|
||||
from mcp_common.app_factory import ExchangeAppSpec, run_exchange_main
|
||||
|
||||
from mcp_hyperliquid.client import HyperliquidClient
|
||||
from mcp_hyperliquid.server import create_app
|
||||
|
||||
|
||||
configure_root_logging() # CER-P5-009
|
||||
|
||||
def main():
|
||||
wallet_file = os.environ["HYPERLIQUID_WALLET_FILE"]
|
||||
with open(wallet_file) as f:
|
||||
creds = json.load(f)
|
||||
|
||||
env_info = resolve_environment(
|
||||
creds,
|
||||
env_var="HYPERLIQUID_TESTNET",
|
||||
flag_key="testnet",
|
||||
exchange="hyperliquid",
|
||||
default_base_url_live="https://api.hyperliquid.xyz",
|
||||
default_base_url_testnet="https://api.hyperliquid-testnet.xyz",
|
||||
)
|
||||
|
||||
client = HyperliquidClient(
|
||||
SPEC = ExchangeAppSpec(
|
||||
exchange="hyperliquid",
|
||||
creds_env_var="HYPERLIQUID_WALLET_FILE",
|
||||
env_var="HYPERLIQUID_TESTNET",
|
||||
flag_key="testnet",
|
||||
default_base_url_live="https://api.hyperliquid.xyz",
|
||||
default_base_url_testnet="https://api.hyperliquid-testnet.xyz",
|
||||
default_port=9012,
|
||||
build_client=lambda creds, env_info: HyperliquidClient(
|
||||
wallet_address=creds["wallet_address"],
|
||||
private_key=creds["private_key"],
|
||||
testnet=(env_info.environment == "testnet"),
|
||||
api_wallet_address=creds.get("api_wallet_address"),
|
||||
)
|
||||
),
|
||||
build_app=create_app,
|
||||
)
|
||||
|
||||
token_store = load_token_store_from_files(
|
||||
core_token_file=os.environ.get("CORE_TOKEN_FILE"),
|
||||
observer_token_file=os.environ.get("OBSERVER_TOKEN_FILE"),
|
||||
)
|
||||
app = create_app(
|
||||
client=client,
|
||||
token_store=token_store,
|
||||
creds=creds,
|
||||
env_info=env_info,
|
||||
)
|
||||
uvicorn.run(
|
||||
app,
|
||||
log_config=None, # CER-P5-009: delega al root JSON logger
|
||||
host=os.environ.get("HOST", "0.0.0.0"),
|
||||
port=int(os.environ.get("PORT", "9012")),
|
||||
)
|
||||
|
||||
def main():
|
||||
run_exchange_main(SPEC)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -3,15 +3,16 @@ from __future__ import annotations
|
||||
import os
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from mcp_common.audit import audit_write_op
|
||||
from mcp_common.auth import Principal, TokenStore, require_principal
|
||||
from mcp_common.mcp_bridge import mount_mcp_endpoint
|
||||
from mcp_common.environment import EnvironmentInfo
|
||||
from mcp_hyperliquid.leverage_cap import enforce_leverage as _enforce_leverage
|
||||
from mcp_hyperliquid.leverage_cap import get_max_leverage
|
||||
from mcp_common.mcp_bridge import mount_mcp_endpoint
|
||||
from mcp_common.server import build_app
|
||||
from pydantic import BaseModel, field_validator, model_validator
|
||||
|
||||
from mcp_hyperliquid.client import HyperliquidClient
|
||||
from mcp_hyperliquid.leverage_cap import enforce_leverage as _enforce_leverage
|
||||
from mcp_hyperliquid.leverage_cap import get_max_leverage
|
||||
|
||||
# --- Body models ---
|
||||
|
||||
@@ -305,7 +306,7 @@ def create_app(
|
||||
):
|
||||
_check(principal, core=True)
|
||||
_enforce_leverage(body.leverage, creds=creds, exchange="hyperliquid")
|
||||
return await client.place_order(
|
||||
result = await client.place_order(
|
||||
instrument=body.instrument,
|
||||
side=body.side,
|
||||
amount=body.amount,
|
||||
@@ -313,34 +314,67 @@ def create_app(
|
||||
price=body.price,
|
||||
reduce_only=body.reduce_only,
|
||||
)
|
||||
audit_write_op(
|
||||
principal=principal, action="place_order", exchange="hyperliquid",
|
||||
target=body.instrument,
|
||||
payload={"side": body.side, "amount": body.amount, "type": body.type,
|
||||
"price": body.price, "reduce_only": body.reduce_only,
|
||||
"leverage": body.leverage},
|
||||
result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/cancel_order", tags=["writes"])
|
||||
async def t_cancel_order(
|
||||
body: CancelOrderReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.cancel_order(body.order_id, body.instrument)
|
||||
result = await client.cancel_order(body.order_id, body.instrument)
|
||||
audit_write_op(
|
||||
principal=principal, action="cancel_order", exchange="hyperliquid",
|
||||
target=body.order_id, payload={"instrument": body.instrument}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/set_stop_loss", tags=["writes"])
|
||||
async def t_set_sl(
|
||||
body: SetStopLossReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.set_stop_loss(body.instrument, body.stop_price, body.size)
|
||||
result = await client.set_stop_loss(body.instrument, body.stop_price, body.size)
|
||||
audit_write_op(
|
||||
principal=principal, action="set_stop_loss", exchange="hyperliquid",
|
||||
target=body.instrument,
|
||||
payload={"stop_price": body.stop_price, "size": body.size},
|
||||
result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/set_take_profit", tags=["writes"])
|
||||
async def t_set_tp(
|
||||
body: SetTakeProfitReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.set_take_profit(body.instrument, body.tp_price, body.size)
|
||||
result = await client.set_take_profit(body.instrument, body.tp_price, body.size)
|
||||
audit_write_op(
|
||||
principal=principal, action="set_take_profit", exchange="hyperliquid",
|
||||
target=body.instrument,
|
||||
payload={"tp_price": body.tp_price, "size": body.size},
|
||||
result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/close_position", tags=["writes"])
|
||||
async def t_close_position(
|
||||
body: ClosePositionReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.close_position(body.instrument)
|
||||
result = await client.close_position(body.instrument)
|
||||
audit_write_op(
|
||||
principal=principal, action="close_position", exchange="hyperliquid",
|
||||
target=body.instrument, payload={}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
# ───── MCP endpoint (/mcp) — bridge verso /tools/* ─────
|
||||
port = int(os.environ.get("PORT", "9012"))
|
||||
|
||||
Reference in New Issue
Block a user