add utxos endpoint
This commit is contained in:
@@ -3,6 +3,7 @@ import asyncio
|
|||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.task_manager import OnchainAddressEvent
|
from lnbits.task_manager import OnchainAddressEvent
|
||||||
from lnbits.utils.electrum import (
|
from lnbits.utils.electrum import (
|
||||||
|
UTXO,
|
||||||
AddressResponse,
|
AddressResponse,
|
||||||
Balance,
|
Balance,
|
||||||
BlockHeader,
|
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:
|
def address_event_to_response(event: OnchainAddressEvent) -> AddressResponse:
|
||||||
return AddressResponse(
|
return AddressResponse(
|
||||||
balance=Balance(confirmed=event.confirmed, unconfirmed=event.unconfirmed),
|
balance=Balance(confirmed=event.confirmed, unconfirmed=event.unconfirmed),
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from lnbits.core.services.blockexplorer import (
|
|||||||
fetch_recent_blocks,
|
fetch_recent_blocks,
|
||||||
fetch_tip,
|
fetch_tip,
|
||||||
fetch_transaction,
|
fetch_transaction,
|
||||||
|
fetch_utxos,
|
||||||
)
|
)
|
||||||
from lnbits.decorators import check_access_token, check_user_exists
|
from lnbits.decorators import check_access_token, check_user_exists
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
@@ -22,6 +23,7 @@ from lnbits.task_manager import (
|
|||||||
task_manager,
|
task_manager,
|
||||||
)
|
)
|
||||||
from lnbits.utils.electrum import (
|
from lnbits.utils.electrum import (
|
||||||
|
UTXO,
|
||||||
AddressResponse,
|
AddressResponse,
|
||||||
BlockHeader,
|
BlockHeader,
|
||||||
BlockInfo,
|
BlockInfo,
|
||||||
@@ -104,6 +106,18 @@ async def api_address(address: str) -> AddressResponse:
|
|||||||
raise HTTPException(HTTPStatus.SERVICE_UNAVAILABLE, detail=str(e)) from e
|
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 ----
|
# ---- WebSocket ----
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user