problem: can't reason about zap reciepts

This commit is contained in:
gsovereignty
2024-07-07 21:50:32 +08:00
parent dce69618d0
commit b63746a672

View File

@@ -1,7 +1,7 @@
<script lang="ts">
import { page } from '$app/stores';
import { ndk } from '@/ndk';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { NDKEvent, type NDKTag } from '@nostr-dev-kit/ndk';
import type { ExtendedBaseType, NDKEventStore } from '@nostr-dev-kit/ndk-svelte';
import { onDestroy } from 'svelte';
import { derived, type Readable } from 'svelte/store';
@@ -19,6 +19,7 @@
let rocketEvents: NDKEventStore<NDKEvent> | undefined;
let latestRocketEvent: Readable<ExtendedBaseType<NDKEvent> | undefined>;
let zaps: Readable<ExtendedBaseType<NDKEvent>[]>;
let candidateProducts: Readable<ExtendedBaseType<NDKEvent>[]>;
onDestroy(() => {
@@ -35,8 +36,10 @@
if (rName && rPubkey) {
//the user wants the latest valid state of this rocket
rocketEvents = $ndk.storeSubscribe(
[{ '#d': [rName], authors: [rPubkey], kinds: [31108 as number] },
{ '#a': [`31108:${rPubkey}:${rName}`]}],
[
{ '#d': [rName], authors: [rPubkey], kinds: [31108 as number] },
{ '#a': [`31108:${rPubkey}:${rName}`] }
],
{ subId: rName }
);
}
@@ -45,9 +48,9 @@
if (rocketEvents) {
latestRocketEvent = derived(rocketEvents, ($events) => {
if (rocketEvents) {
let sorted = $events.filter(e=>{
return e.kind == 31108;
})
let sorted = $events.filter((e) => {
return e.kind == 31108;
});
sorted = sorted.toSorted((a, b) => {
return a.created_at - b.created_at;
});
@@ -56,18 +59,75 @@
return undefined;
});
if ($latestRocketEvent) {
candidateProducts = derived(rocketEvents, ($events) =>{
return $events.filter(e=>{
for (let p of $latestRocketEvent.getMatchingTags("product")) {
if ($latestRocketEvent) {
candidateProducts = derived(rocketEvents, ($events) => {
return $events.filter((e) => {
for (let p of $latestRocketEvent.getMatchingTags('product')) {
if (p[1].includes(e.id)) {
return false
return false;
}
}
return e.kind == 1908;
})
})
}
return e.kind == 1908;
});
});
zaps = derived(rocketEvents, ($events) => {
return $events.filter((e) => {
return e.kind == 9735;
});
});
}
}
}
function getMapOfProducts(rocket: NDKEvent): Map<string, NDKTag> {
let productIDs = new Map<string, NDKTag>();
for (let product of rocket.getMatchingTags('product')) {
if (product.length > 1 && product[1].split(':') && product[1].split(':').length > 0) {
productIDs.set(product[1].split(':')[0], product);
}
}
return productIDs
}
//todo: check that this zap is not already included in the payment JSON for the product
//todo: list purchases on the rocket page (from product tags, as well as zap receipts that aren't yet included). Deduct total products available if not 0.
//todo: make the page flash or something and show each time someone buys the product.
//todo: split this out so that we can consume it for the payment page too (so that we know if there are really products left or they're all sold)
function getZapData(zap: NDKEvent) {
let desc = zap.getMatchingTags('description');
if (desc && desc.length == 1) {
let request = new NDKEvent($ndk, JSON.parse(desc[0][1]));
console.log(request.author.pubkey);
let eTags = request.getMatchingTags('e');
let zappedProductIsLegit = false;
let productPrice = 0;
if (eTags && eTags.length > 0) {
for (let etag of eTags) {
if (etag.length > 1) {
if ($latestRocketEvent && getMapOfProducts($latestRocketEvent)) {
let products = getMapOfProducts($latestRocketEvent)
let thisProduct = products.get(etag[1])
if (thisProduct) {
zappedProductIsLegit = true
productPrice = parseInt(thisProduct[1].split(":")[1], 10)
}
}
}
}
}
console.log(zappedProductIsLegit)
if (!zappedProductIsLegit) {
console.log(request.rawEvent())
}
let amount = request.getMatchingTags('amount');
if (amount && amount.length == 1) {
if (amount[0].length == 2) {
let amountInt = parseInt(amount[0][1], 10);
console.log(amountInt >= productPrice);
}
}
}
}
@@ -83,12 +143,21 @@
'modify relevant data and republish event according to https://github.com/nostrocket/NIPS/blob/main/31108.md and https://github.com/nostrocket/NIPS/blob/main/MSBR334000.md '
]}
/>
{#if candidateProducts && $candidateProducts}
<Subheading title="Product Candidates" />
<CreateNewProduct rocketEvent={$latestRocketEvent} />
{#each $candidateProducts as r}<ProductCard rocket={$latestRocketEvent} product={r} />{/each}
{/if}
{#if candidateProducts && $candidateProducts}
<Subheading title="Product Candidates" />
<CreateNewProduct rocketEvent={$latestRocketEvent} />
{#each $candidateProducts as r}<ProductCard rocket={$latestRocketEvent} product={r} />{/each}
{/if}
{#if zaps && $zaps}
{#each $zaps as z}<p
on:click={() => {
getZapData(z);
}}
>
{z.id}
</p>{/each}
{/if}
{:else}
IGNITION: {rIgnitionOrActual} <br />
NAME: {rName} <br />