add utxos endpoint
This commit is contained in:
@@ -3,6 +3,7 @@ import asyncio
|
||||
from lnbits.settings import settings
|
||||
from lnbits.task_manager import OnchainAddressEvent
|
||||
from lnbits.utils.electrum import (
|
||||
UTXO,
|
||||
AddressResponse,
|
||||
Balance,
|
||||
BlockHeader,
|
||||
@@ -82,6 +83,12 @@ async def fetch_onchain_balance(onchain_address: str) -> AddressResponse:
|
||||
)
|
||||
|
||||
|
||||
async def fetch_utxos(onchain_address: str) -> list[UTXO]:
|
||||
scripthash = scripthash_from_address(onchain_address)
|
||||
async with _client() as client:
|
||||
return await client.listunspent(scripthash)
|
||||
|
||||
|
||||
def address_event_to_response(event: OnchainAddressEvent) -> AddressResponse:
|
||||
return AddressResponse(
|
||||
balance=Balance(confirmed=event.confirmed, unconfirmed=event.unconfirmed),
|
||||
|
||||
@@ -12,6 +12,7 @@ from lnbits.core.services.blockexplorer import (
|
||||
fetch_recent_blocks,
|
||||
fetch_tip,
|
||||
fetch_transaction,
|
||||
fetch_utxos,
|
||||
)
|
||||
from lnbits.decorators import check_access_token, check_user_exists
|
||||
from lnbits.settings import settings
|
||||
@@ -22,6 +23,7 @@ from lnbits.task_manager import (
|
||||
task_manager,
|
||||
)
|
||||
from lnbits.utils.electrum import (
|
||||
UTXO,
|
||||
AddressResponse,
|
||||
BlockHeader,
|
||||
BlockInfo,
|
||||
@@ -104,6 +106,18 @@ async def api_address(address: str) -> AddressResponse:
|
||||
raise HTTPException(HTTPStatus.SERVICE_UNAVAILABLE, detail=str(e)) from e
|
||||
|
||||
|
||||
@blockexplorer_router.get("/utxos/{address}", dependencies=[Depends(_check_api_access)])
|
||||
async def api_utxos(address: str) -> list[UTXO]:
|
||||
try:
|
||||
scripthash_from_address(address)
|
||||
except ValueError as e:
|
||||
raise HTTPException(HTTPStatus.BAD_REQUEST, detail=str(e)) from e
|
||||
try:
|
||||
return await fetch_utxos(address)
|
||||
except ElectrumError as e:
|
||||
raise HTTPException(HTTPStatus.SERVICE_UNAVAILABLE, detail=str(e)) from e
|
||||
|
||||
|
||||
# ---- WebSocket ----
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user