Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
from fastapi import APIRouter, Depends, Request, Response, status
|
2026-07-21 10:26:25 +02:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
2026-07-26 21:44:39 +02:00
|
|
|
from app.api.errors import from_api_error, http_error
|
2026-07-21 10:26:25 +02:00
|
|
|
from app.auth.dependencies import get_current_user
|
|
|
|
|
from app.db.models import User
|
|
|
|
|
from app.db.session import get_session
|
|
|
|
|
from app.withdrawals.service import WithdrawalError, request_withdrawal
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/withdrawals", tags=["withdrawals"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WithdrawalRequest(BaseModel):
|
|
|
|
|
external_address: str
|
|
|
|
|
amount_sats: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WithdrawalResponse(BaseModel):
|
|
|
|
|
txid: str
|
|
|
|
|
amount_requested_sats: int
|
|
|
|
|
amount_sent_sats: int
|
|
|
|
|
status: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", response_model=WithdrawalResponse, status_code=status.HTTP_201_CREATED)
|
|
|
|
|
async def create_withdrawal(
|
|
|
|
|
body: WithdrawalRequest,
|
|
|
|
|
request: Request,
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
response: Response,
|
2026-07-21 10:26:25 +02:00
|
|
|
user: User = Depends(get_current_user),
|
|
|
|
|
session: AsyncSession = Depends(get_session),
|
|
|
|
|
) -> WithdrawalResponse:
|
|
|
|
|
listener = request.app.state.electrum_listener
|
|
|
|
|
if listener.client is None:
|
2026-07-26 21:44:39 +02:00
|
|
|
raise http_error(
|
|
|
|
|
status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
|
|
|
"network_unavailable",
|
|
|
|
|
"not connected to the network, try again shortly",
|
|
|
|
|
)
|
2026-07-21 10:26:25 +02:00
|
|
|
|
|
|
|
|
async with request.app.state.user_locks.acquire(user.id):
|
|
|
|
|
try:
|
|
|
|
|
withdrawal = await request_withdrawal(
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
session, listener.client, user, body.external_address, body.amount_sats, network=listener
|
2026-07-21 10:26:25 +02:00
|
|
|
)
|
|
|
|
|
except WithdrawalError as exc:
|
2026-07-27 00:31:24 +02:00
|
|
|
code = (
|
|
|
|
|
status.HTTP_502_BAD_GATEWAY # the network refused it, not the caller (B-07)
|
|
|
|
|
if exc.code == "broadcast_failed"
|
2026-09-29 16:26:01 +02:00
|
|
|
else status.HTTP_503_SERVICE_UNAVAILABLE # B-88: write lock not granted, nothing sent
|
|
|
|
|
if exc.code == "service_busy"
|
2026-07-27 00:31:24 +02:00
|
|
|
else status.HTTP_400_BAD_REQUEST
|
|
|
|
|
)
|
|
|
|
|
raise from_api_error(code, exc) from exc
|
2026-07-21 10:26:25 +02:00
|
|
|
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
if withdrawal.status == "building":
|
|
|
|
|
# B-76 (TX-20, WDR-16): uncertain broadcast — accepted for processing, the
|
|
|
|
|
# reconciler decides whether the transaction left.
|
|
|
|
|
response.status_code = status.HTTP_202_ACCEPTED
|
2026-07-21 10:26:25 +02:00
|
|
|
return WithdrawalResponse(
|
|
|
|
|
txid=withdrawal.txid,
|
|
|
|
|
amount_requested_sats=withdrawal.amount_requested_sats,
|
|
|
|
|
amount_sent_sats=withdrawal.amount_sent_sats,
|
|
|
|
|
status=withdrawal.status,
|
|
|
|
|
)
|