problem: can't see how much btc is waiting to invest in a rocket

This commit is contained in:
gsovereignty
2024-08-13 11:02:52 +08:00
parent 98836eefac
commit af8084f402
5 changed files with 146 additions and 109 deletions

View File

@@ -0,0 +1,122 @@
<script lang="ts">
import * as Card from '@/components/ui/card';
import * as Table from '@/components/ui/table';
import { BitcoinAssociation, Rocket } from '@/event_helpers/rockets';
import { getCuckPrice } from '@/helpers';
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, onMount } from 'svelte';
import { derived, writable } from 'svelte/store';
export let rocket: Rocket;
let cuckprice = 0;
let associations = writable(new Map<string, BitcoinAssociation>());
onMount(() => {
getCuckPrice().then((price) => {
if (price) cuckprice = price;
});
associations.set(rocket.BitcoinAssociations());
$associations.forEach((a) => {
if (a.Address) {
getBalance(a.Address)
.then((v) => {
a.Balance = v;
associations.update((existing) => {
existing.set(a.Address!, a);
return existing;
});
})
.catch((err) => {
console.log(err);
});
}
});
});
let amounts = derived(associations, ($associations) => {
let amounts = new Map<string, number>();
for (let [_, a] of $associations) {
if (a.Balance > 0) {
let existing = amounts.get(a.Pubkey);
if (!existing) {
existing = 0;
}
existing += a.Balance;
amounts.set(a.Pubkey, existing);
}
}
return amounts;
});
let total = derived(amounts, ($amounts) => {
let t = 0;
for (let [_, a] of $amounts) {
t += a;
}
return t;
});
</script>
<Card.Root class="sm:col-span-3">
<Card.Header class="px-7">
<Card.Title>Sponsors</Card.Title>
<Card.Description
>These people want to sponsor Contributors working on {rocket.Name()}.</Card.Description
>
</Card.Header>
<Card.Content>
<Table.Root>
<Table.Header>
<Table.Row>
<Table.Head class="w-[200px]">Sponsor</Table.Head>
<Table.Head class="hidden text-left md:table-cell">Amount (Sats)</Table.Head>
<Table.Head class="hidden text-left md:table-cell">CuckLoserBucks</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each $amounts as [npub, amount], _ (npub)}
<Table.Row>
<Table.Cell>
<div class="flex flex-nowrap">
<Avatar
ndk={$ndk}
pubkey={npub}
class="h-10 w-10 flex-none rounded-full object-cover"
/>
<Name
ndk={$ndk}
pubkey={npub}
class="hidden max-w-32 truncate p-2 md:inline-block"
/>
</div>
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
{amount.toLocaleString()}
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
${Math.floor((amount / 100000000) * cuckprice).toLocaleString()}
</Table.Cell>
</Table.Row>
{/each}
{#if $total > 0}
<Table.Row
class=" bg-violet-200 hover:bg-violet-300 dark:bg-violet-950 dark:hover:bg-violet-900"
>
<Table.Cell>TOTAL</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
{$total.toLocaleString()}
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
${Math.floor(($total / 100000000) * cuckprice).toLocaleString()}
</Table.Cell>
</Table.Row>
{/if}
</Table.Body>
</Table.Root>
</Card.Content>
</Card.Root>

View File

@@ -1,86 +0,0 @@
<script lang="ts">
import * as Card from '@/components/ui/card';
import * as Table from '@/components/ui/table';
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, onMount } from 'svelte';
export let rocket: Rocket;
let _associationRequests = $ndk.storeSubscribe(
[{ '#a': [`31108:${rocket.Event.author.pubkey}:${rocket.Name()}`], kinds: [1413 as NDKKind] }],
{
subId: `${rocket.Name()}_bitcoin_associations`
}
);
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.Address!, a);
addresses = addresses;
})
.catch((err) => {
console.log(err);
});
}
});
});
</script>
<Card.Root class="sm:col-span-3">
<Card.Header class="px-7">
<Card.Title>Registered Bitcoin Addresses</Card.Title>
<Card.Description
>These people have registered a Bitcoin address and want to sponsor Contributors working on {rocket.Name()}</Card.Description
>
</Card.Header>
<Card.Content>
<Table.Root>
<Table.Header>
<Table.Row>
<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 addresses as [address, ba], _ (address)}
<Table.Row>
<Table.Cell>
<div class="flex flex-nowrap">
<Avatar
ndk={$ndk}
pubkey={ba.Pubkey}
class="h-10 w-10 flex-none rounded-full object-cover"
/>
<Name
ndk={$ndk}
pubkey={ba.Pubkey}
class="hidden max-w-32 truncate p-2 md:inline-block"
/>
</div>
</Table.Cell>
<Table.Cell class="hidden text-left md:table-cell">
{ba.Balance.toLocaleString()}
</Table.Cell>
<Table.Cell class="table-cell">{ba.Address}</Table.Cell>
</Table.Row>
{/each}
</Table.Body>
</Table.Root>
</Card.Content>
</Card.Root>

View File

@@ -12,12 +12,11 @@
import Todo from './Todo.svelte';
import UpdateMission from './UpdateMission.svelte';
import { Rocket } from '@/event_helpers/rockets';
import BitcoinAssociations from './BitcoinAssociations.svelte';
import BitcoinAssociations from './AssociatedBitcoinAddresses.svelte';
export let rocket: NDKEvent;
$: unratifiedZaps = new Map<string, number>()
$: unratifiedZaps = new Map<string, number>();
</script>
<div class="flex flex-col sm:gap-4">
@@ -44,7 +43,7 @@
<ProposedProducts {rocket} />
<MeritRequests rocket={new Rocket(rocket)} />
<BitcoinAssociations rocket={new Rocket(rocket)}/>
<BitcoinAssociations rocket={new Rocket(rocket)} />
<Card.Root class="sm:col-span-3">
<Card.Header class="pb-3">
<Card.Title>Actions</Card.Title>
@@ -62,10 +61,6 @@
<Card.Footer></Card.Footer>
</Card.Root>
<Todo
text={[
'delete rocket (if current user is rocket creator) - publish deletion request'
]}
/>
<Todo text={['delete rocket (if current user is rocket creator) - publish deletion request']} />
</main>
</div>

View File

@@ -82,17 +82,24 @@ export function formatSats(sats: number): string {
}
}
export async function getCuckPrice(): Promise<number | Error> {
try {
export async function getCuckPrice(): Promise<number> {
return new Promise((resolve, reject) => {
var url = 'https://api.coindesk.com/v1/bpi/currentprice.json';
var symbol = 'USD';
const data = await fetch(url);
const json = await data.json();
const cuckPrice = parseFloat(json.bpi[symbol].rate.replace(/,/g, ''));
return cuckPrice;
} catch (e) {
return e as Error;
}
fetch(url)
.then((r) => {
r.json()
.then((j) => {
resolve(parseFloat(j.bpi[symbol].rate.replace(/,/g, '')));
})
.catch((e) => {
reject(e as Error);
});
})
.catch((e) => {
reject(e as Error);
});
});
}
export async function parseProblem(problem: string) {

View File

@@ -7,20 +7,19 @@ import { browser } from '$app/environment';
const _ndk = new NDKSvelte({
explicitRelayUrls: [
'wss://purplepag.es',
'wss://relay.higlighter.com',
'wss://relay.nostr.band',
'wss://nos.lol',
'wss://relay.nostrocket.org',
'wss://nostr.mutinywallet.com',
'wss://relay.damus.io'
],
enableOutboxModel: false,
enableOutboxModel: false
//clientNip89: "31990:fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52:1716498133442",
});
//we need to check for browser environment before calling window because svelte is slightly retarded when used client side only
// if (browser && window.indexedDB) {
// _ndk.cacheAdapter = new NDKCacheAdapterDexie({ dbName: 'gulag' });
// }
if (browser && window.indexedDB) {
_ndk.cacheAdapter = new NDKCacheAdapterDexie({ dbName: 'gulag' });
}
export const ndk = writable(_ndk);