Skip to content

Commit

Permalink
problem: can't see how much Bitcoin is waiting to sponsor contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Aug 3, 2024
1 parent 70145ff commit ce1625d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/components/BitcoinAssociations.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import * as Card from '@/components/ui/card';
import * as Table from '@/components/ui/table';
import { Rocket } from '@/event_helpers/rockets';
import { BitcoinAssociation, Rocket } from '@/event_helpers/rockets';
import { ndk } from '@/ndk';
import { getBalance } from '@/stores/bitcoin';
import { NDKKind } from '@nostr-dev-kit/ndk';
import { Avatar, Name } from '@nostr-dev-kit/ndk-svelte-components';
import { onDestroy } from 'svelte';
import { onDestroy, onMount } from 'svelte';
export let rocket: Rocket;
Expand All @@ -19,6 +20,18 @@
onDestroy(() => {
_associationRequests?.unsubscribe();
});
let addresses = new Map<string, BitcoinAssociation>()
onMount(()=>{
addresses = rocket.BitcoinAssociations()
addresses.forEach(a => {
if (a.Address) {
getBalance(a.Address).then(v=>{a.Balance = v; addresses.set(a.Pubkey, a); addresses = addresses})
}
})
})
</script>

<Card.Root class="sm:col-span-3">
Expand All @@ -32,13 +45,13 @@
<Table.Root>
<Table.Header>
<Table.Row>
<Table.Head>Sponsor</Table.Head>
<Table.Head class="hidden text-left md:table-cell">Address</Table.Head>
<Table.Head class="table-cell">Amount (Sats)</Table.Head>
<Table.Head class="w-[200px]">Sponsor</Table.Head>
<Table.Head class="hidden text-left md:table-cell">Amount (Sats)</Table.Head>
<Table.Head class="table-cell">Address</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each rocket.BitcoinAssociations() as [pubkey, ba], _ (pubkey)}
{#each addresses as [pubkey, ba], _ (pubkey)}
<Table.Row>
<Table.Cell>
<div class="flex flex-nowrap">
Expand All @@ -55,7 +68,7 @@
</div>
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
{0}
{ba.Balance}
</Table.Cell>
<Table.Cell class="table-cell">{ba.Address}</Table.Cell>

Expand Down
2 changes: 2 additions & 0 deletions src/lib/event_helpers/rockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -767,6 +768,7 @@ export class BitcoinAssociation {
}

constructor(event?: NDKEvent) {
this.Balance = 0;
if (event) {
this.Pubkey = event.pubkey;
this.Address = event.tagValue('onchain');
Expand Down
32 changes: 32 additions & 0 deletions src/lib/stores/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import validate from 'bitcoin-address-validation';
import { get, writable } from 'svelte/store';

type BitcoinTip = {
Expand Down Expand Up @@ -30,3 +31,34 @@ export async function getBitcoinTip() {
}
return null;
}

export async function getBalance(address: string): Promise<number> {
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');
}
}
});
}

0 comments on commit ce1625d

Please sign in to comment.