From ce1625d7f6580a6720a814cff13156ad9f03a3fa Mon Sep 17 00:00:00 2001 From: gsovereignty Date: Sat, 3 Aug 2024 21:32:16 +0800 Subject: [PATCH] problem: can't see how much Bitcoin is waiting to sponsor contributors --- src/components/BitcoinAssociations.svelte | 27 ++++++++++++++----- src/lib/event_helpers/rockets.ts | 2 ++ src/lib/stores/bitcoin.ts | 32 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/components/BitcoinAssociations.svelte b/src/components/BitcoinAssociations.svelte index 30222f5..50f18ea 100644 --- a/src/components/BitcoinAssociations.svelte +++ b/src/components/BitcoinAssociations.svelte @@ -1,11 +1,12 @@ @@ -32,13 +45,13 @@ - Sponsor - - Amount (Sats) + Sponsor + + Address - {#each rocket.BitcoinAssociations() as [pubkey, ba], _ (pubkey)} + {#each addresses as [pubkey, ba], _ (pubkey)}
@@ -55,7 +68,7 @@
{ba.Address} diff --git a/src/lib/event_helpers/rockets.ts b/src/lib/event_helpers/rockets.ts index 5d8a4ef..07d5b70 100644 --- a/src/lib/event_helpers/rockets.ts +++ b/src/lib/event_helpers/rockets.ts @@ -755,6 +755,7 @@ export class BitcoinAssociation { Pubkey: string; Address: string | undefined; Event: NDKEvent; + Balance:number; Validate(): boolean { let valid = true; if (this.Pubkey.length != 64) { @@ -767,6 +768,7 @@ export class BitcoinAssociation { } constructor(event?: NDKEvent) { + this.Balance = 0; if (event) { this.Pubkey = event.pubkey; this.Address = event.tagValue('onchain'); diff --git a/src/lib/stores/bitcoin.ts b/src/lib/stores/bitcoin.ts index 3afc493..e59b312 100644 --- a/src/lib/stores/bitcoin.ts +++ b/src/lib/stores/bitcoin.ts @@ -1,3 +1,4 @@ +import validate from 'bitcoin-address-validation'; import { get, writable } from 'svelte/store'; type BitcoinTip = { @@ -30,3 +31,34 @@ export async function getBitcoinTip() { } return null; } + +export async function getBalance(address: string): Promise { + return new Promise((resolve, reject) => { + if (!validate(address)) { + reject('invalid address'); + } else { + try { + fetch(`https://blockstream.info/api/address/${address}`) + .then((response) => { + if (!response.ok) { + reject('invalid response from server'); + } else { + response + .json() + .then((j) => { + let spent = parseInt(j.chain_stats.spent_txo_sum, 10); + let funded = parseInt(j.chain_stats.funded_txo_sum, 10); + resolve(funded - spent); + }) + .catch((x) => reject(x)); + } + }) + .catch((x) => { + reject(x); + }); + } catch { + reject('failed'); + } + } + }); +}