problem: we are not including the right igntion and parent tags

This commit is contained in:
gsovereignty
2024-07-07 15:29:26 +08:00
parent 75206af3fa
commit dce69618d0
7 changed files with 151 additions and 48 deletions

View File

@@ -1,51 +1,92 @@
<script lang="ts">
import { Rocket } from 'lucide-svelte';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { Button, buttonVariants } from '$lib/components/ui/button/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
import { Input } from '$lib/components/ui/input/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import { ndk } from '@/ndk';
import Todo from './Todo.svelte';
import { currentUser } from '@/stores/session';
import { Terminal } from 'lucide-svelte';
import * as Alert from '@/components/ui/alert';
import type NDKSvelte from '@nostr-dev-kit/ndk-svelte';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { getRocketURL } from '@/helpers';
import { ndk } from '@/ndk';
import { currentUser } from '@/stores/session';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { Terminal } from 'lucide-svelte';
import Todo from './Todo.svelte';
export let product:NDKEvent;
export let rocket:NDKEvent;
export let product: NDKEvent;
export let rocket: NDKEvent;
let price:number = 0;
let max:number = 0
let price: number = 0;
let max: number = 0;
function updateIgnitionAndParentTag(rocket: NDKEvent) {
let existingIgnition = rocket.getMatchingTags('ignition');
//let existingParent = rocket.getMatchingTags("parent")
removeIgnitionAndParentTag(rocket);
if (existingIgnition.length > 1) {
throw new Error('too many ignition tags!');
}
if (existingIgnition.length == 0) {
rocket.tags.push(['ignition', rocket.id]);
}
if (existingIgnition.length == 1) {
if (existingIgnition[0][1].length == 64) {
rocket.tags.push(existingIgnition[0]);
}
if (existingIgnition[0][1] == 'this') {
rocket.tags.push(['ignition', rocket.id]);
}
}
rocket.tags.push(['parent', rocket.id]);
}
function removeIgnitionAndParentTag(rocket: NDKEvent) {
let existing = [];
for (let t of rocket.tags) {
existing.push(t);
}
rocket.tags = [];
for (let t of existing) {
if (t[0] !== 'ignition' && t[0] !== 'parent') {
rocket.tags.push(t);
}
}
}
function publish() {
if (!$ndk.signer) {
throw new Error('no ndk signer found');
}
rocket.ndk = $ndk
rocket.ndk = $ndk;
let author = $currentUser;
if (!author) {
throw new Error('no current user');
}
if (rocket.author.pubkey != author.pubkey) {
console.log(rocket.author, author)
throw new Error('you are not the creator of this rocket');
}
rocket.created_at = Math.floor(new Date().getTime() / 1000);
//todo validate d tag
rocket.tags.push(["product", `${product.id}:${price}:${rocket.created_at}:${max}`, "wss://relay.nostrocket.org", JSON.stringify([])])
rocket.publish().then((x)=>{
console.log(x)
goto(`${base}/rockets/${getRocketURL(rocket)}`)
})
if (rocket.author.pubkey != author.pubkey) {
console.log(rocket.author, author);
throw new Error('you are not the creator of this rocket');
}
rocket.created_at = Math.floor(new Date().getTime() / 1000);
//todo validate d tag
rocket.tags.push([
'product',
`${product.id}:${price}:${rocket.created_at}:${max}`,
'wss://relay.nostrocket.org',
JSON.stringify([])
]);
updateIgnitionAndParentTag(rocket)
rocket.publish().then((x) => {
console.log(x);
goto(`${base}/products`);
});
}
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: 'default' })}>Make Available for Purchase</Dialog.Trigger>
<Dialog.Trigger class={buttonVariants({ variant: 'default' })}
>Make Available for Purchase</Dialog.Trigger
>
<Dialog.Content class="sm:max-w-[425px]">
<Dialog.Header>
<Dialog.Title>Make this product available to customers</Dialog.Title>
@@ -65,14 +106,24 @@
<Label for="price" class="text-right">Price</Label>
<Input bind:value={price} id="price" placeholder="Price in sats" class="col-span-3" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<div class="grid grid-cols-4 items-center gap-4">
<Label for="max" class="text-right">Max Available</Label>
<Input bind:value={max} id="max" placeholder="Maximum number that can be sold" class="col-span-3" />
<Input
bind:value={max}
id="max"
placeholder="Maximum number that can be sold"
class="col-span-3"
/>
</div>
</div>
<Todo text={['validate input is a number']} />
<Dialog.Footer>
<Button on:click={()=>{publish()}} type="submit">Publish</Button>
<Button
on:click={() => {
publish();
}}
type="submit">Publish</Button
>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>

View File

@@ -42,7 +42,7 @@
console.log(e.rawEvent())
e.publish().then((x) => {
console.log(x);
//goto(`${base}/rockets/${getRocketURL(e)}`);
goto(`${base}/rockets/${getRocketURL(e)}`);
});
}
</script>

View File

@@ -31,6 +31,8 @@
//todo validate d tag
e.tags.push(["d", name])
e.tags.push(["ruleset", "334000"])
e.tags.push(["ignition", "this"])
e.tags.push(["parent", "this"])
e.publish().then((x)=>{
console.log(x)
goto(`${base}/rockets/${getRocketURL(e)}`)

View File

@@ -1,31 +1,42 @@
<script lang="ts">
import { Rocket } from 'lucide-svelte';
import { Button, buttonVariants } from '$lib/components/ui/button/index.js';
import { buttonVariants } from '$lib/components/ui/button/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
import { Input } from '$lib/components/ui/input/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import { ndk } from '@/ndk';
import Todo from './Todo.svelte';
import { currentUser } from '@/stores/session';
import { Terminal } from 'lucide-svelte';
import * as Alert from '@/components/ui/alert';
import type NDKSvelte from '@nostr-dev-kit/ndk-svelte';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { getRocketURL } from '@/helpers';
import { ndk } from '@/ndk';
import { currentUser } from '@/stores/session';
import { NDKEvent, NDKZap } from '@nostr-dev-kit/ndk';
import { Terminal } from 'lucide-svelte';
import Todo from './Todo.svelte';
import { requestProvider } from 'webln';
export let product:NDKEvent;
export let rocket:NDKEvent;
function zap() {
let z = new NDKZap({ndk:$ndk, zappedEvent:rocket, zappedUser: rocket.author})
z.createZapRequest(1000, `Purchase of ${product.getMatchingTags("name")[0][1]} from ${rocket.dTag}`, [["e", product.id]]).then(invoice=>{
if (invoice) {
requestProvider().then((webln)=>{
webln.sendPayment(invoice).then((response)=>{
if (response && response.preimage) {
console.log(response.preimage)
}
})
});
}
})
}
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: 'default' })}>Buy Now</Dialog.Trigger>
<Dialog.Content class="sm:max-w-[425px]">
<Dialog.Header>
<Dialog.Title>Buy {product.getMatchingTags("name")[0][1]} from {rocket.dTag} now!</Dialog.Title>
<Dialog.Title
>Buy {product.getMatchingTags('name')[0][1]} from {rocket.dTag} now!</Dialog.Title
>
{#if !currentUser}
<Alert.Root>
<Terminal class="h-4 w-4" />
@@ -37,9 +48,10 @@
{/if}
<Dialog.Description>Pay now with Lightning</Dialog.Description>
</Dialog.Header>
<Todo text={["generate zap request and get invoice"]} />
<Dialog.Footer>
<Todo text={['generate zap request and get invoice']} />
<Dialog.Footer>
<a href="#" on:click={zap}>test</a>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>

View File

@@ -32,7 +32,7 @@
//warn user that this information is probably out of date and let them reroute to get the latest
}
if (rIgnitionOrActual.length == 64 && rName && rPubkey) {
if (rName && rPubkey) {
//the user wants the latest valid state of this rocket
rocketEvents = $ndk.storeSubscribe(
[{ '#d': [rName], authors: [rPubkey], kinds: [31108 as number] },