Skip to content

Commit

Permalink
feat: save txids into extra
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Aug 28, 2024
1 parent ef5eb5e commit 4b1d712
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ async def fetch_onchain_balance(onchain_address: str) -> OnchainBalance:
res = await client.get(f"{settings.mempool_url}/api/address/{onchain_address}")
res.raise_for_status()
data = res.json()
# TODO: add txids to OnchainBalance
print(data)
txids = [] # type: ignore
confirmed = data["chain_stats"]["funded_txo_sum"]
unconfirmed = data["mempool_stats"]["funded_txo_sum"]
return OnchainBalance(confirmed=confirmed, unconfirmed=unconfirmed)
return OnchainBalance(confirmed=confirmed, unconfirmed=unconfirmed, txids=txids)


async def fetch_onchain_config_network(api_key: str) -> str:
Expand Down
1 change: 1 addition & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ class SatsPayTheme(BaseModel):
class OnchainBalance(BaseModel):
confirmed: int
unconfirmed: int
txids: list[str]
21 changes: 19 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,32 @@ async def wait_for_onchain():
await _handle_ws_message(address, data)


def sum_outputs(address: str, vouts):
def sum_outputs(address: str, vouts) -> int:
return sum(
[vout["value"] for vout in vouts if vout.get("scriptpubkey_address") == address]
)


def sum_transactions(address: str, txs):
def sum_transactions(address: str, txs) -> int:
return sum([sum_outputs(address, tx["vout"]) for tx in txs])


def get_txids(address: str, data) -> list[str]:
confirmed_txs = data.get("confirmed", [])
confirmed_txids = [
vout["txid"]
for vout in confirmed_txs
if vout.get("scriptpubkey_address") == address
]
mempool_txs = data.get("mempool", [])
mempool_txids = [
vout["txid"]
for vout in mempool_txs
if vout.get("scriptpubkey_address") == address
]
return confirmed_txids + mempool_txids


async def _handle_ws_message(address: str, data: dict):
charge = await get_charge_by_onchain_address(address)
assert charge, f"Charge with address `{address}` does not exist."
Expand All @@ -104,6 +120,7 @@ async def _handle_ws_message(address: str, data: dict):
charge.balance = confirmed_balance
charge.pending = unconfirmed_balance
charge.paid = charge.balance >= charge.amount
charge.add_extra({"txids": get_txids(address, data)})
await send_success_websocket(charge)
if charge.paid:
logger.success(f"Charge {charge.id} onchain paid.")
Expand Down

0 comments on commit 4b1d712

Please sign in to comment.