problem: can't see how much Bitcoin is waiting to sponsor contributors

This commit is contained in:
gsovereignty
2024-08-03 21:32:16 +08:00
parent 70145ff297
commit ce1625d7f6
3 changed files with 54 additions and 7 deletions

View File

@@ -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;
@@ -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">
@@ -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">
@@ -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>

View File

@@ -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');

View File

@@ -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<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');
}
}
});
}