problem: zap amount is not validated

This commit is contained in:
gsovereignty
2024-08-03 23:28:02 +08:00
parent 1361504a74
commit 0d95d62140
5 changed files with 59 additions and 54 deletions

View File

@@ -21,6 +21,8 @@
let desc: string; let desc: string;
let image: string; let image: string;
let o = false;
function publish(ndk: NDKSvelte) { function publish(ndk: NDKSvelte) {
if (!ndk.signer) { if (!ndk.signer) {
throw new Error('no ndk signer found'); throw new Error('no ndk signer found');
@@ -42,12 +44,13 @@
console.log(e.rawEvent()); console.log(e.rawEvent());
e.publish().then((x) => { e.publish().then((x) => {
console.log(x); console.log(x);
goto(`${base}/rockets/${getRocketURL(e)}`); o = false;
goto(`${base}/rockets/${getRocketURL(rocketEvent)}`);
}); });
} }
</script> </script>
<Dialog.Root> <Dialog.Root bind:open={o}>
<Dialog.Trigger class={buttonVariants({ variant: 'default' })} <Dialog.Trigger class={buttonVariants({ variant: 'default' })}
>Propose a New Product</Dialog.Trigger >Propose a New Product</Dialog.Trigger
> >

View File

@@ -11,19 +11,23 @@
import { requestProvider } from 'webln'; import { requestProvider } from 'webln';
import QrCodeSvg from './QrCodeSvg.svelte'; import QrCodeSvg from './QrCodeSvg.svelte';
import CopyButton from './CopyButton.svelte'; import CopyButton from './CopyButton.svelte';
import type { RocketProduct } from '@/event_helpers/rockets';
export let product: NDKEvent; export let product: NDKEvent;
export let rocketProduct: RocketProduct | undefined;
export let rocket: NDKEvent; export let rocket: NDKEvent;
let invoice: string | null; let invoice: string | null;
async function zap() { async function zap() {
const z = new NDKZap({ ndk: $ndk, zappedEvent: rocket, zappedUser: rocket.author }); if (rocketProduct) {
invoice = await z.createZapRequest( const z = new NDKZap({ ndk: $ndk, zappedEvent: rocket, zappedUser: rocket.author });
1000, invoice = await z.createZapRequest(
`Purchase of ${product.getMatchingTags('name')[0][1]} from ${rocket.dTag}`, rocketProduct.Price * 1000,
[['product', product.id]] `Purchase of ${product.getMatchingTags('name')[0][1]} from ${rocket.dTag}`,
); [['product', product.id]]
);
}
} }
async function payWithWebLn() { async function payWithWebLn() {
@@ -44,33 +48,37 @@
let open: boolean; let open: boolean;
</script> </script>
<Dialog.Root bind:open> {#if rocketProduct}
<Dialog.Trigger class={buttonVariants({ variant: 'default' })}>Buy Now</Dialog.Trigger> <Dialog.Root bind:open>
<Dialog.Content class="sm:max-w-[425px]"> <Dialog.Trigger class={buttonVariants({ variant: 'default' })}
<Dialog.Header> >Buy Now for {rocketProduct.Price} sats</Dialog.Trigger
<Dialog.Title >
>Buy {product.getMatchingTags('name')[0][1]} from {rocket.dTag} now!</Dialog.Title <Dialog.Content class="sm:max-w-[425px]">
> <Dialog.Header>
{#if !currentUser} <Dialog.Title
<Alert.Root> >Buy {product.getMatchingTags('name')[0][1]} from {rocket.dTag} now!</Dialog.Title
<Terminal class="h-4 w-4" /> >
<Alert.Title>Heads up!</Alert.Title> {#if !currentUser}
<Alert.Description <Alert.Root>
>You need a nostr signing extension to use Nostrocket!</Alert.Description <Terminal class="h-4 w-4" />
> <Alert.Title>Heads up!</Alert.Title>
</Alert.Root> <Alert.Description
>You need a nostr signing extension to use Nostrocket!</Alert.Description
>
</Alert.Root>
{/if}
<Dialog.Description>Pay now with Lightning</Dialog.Description>
</Dialog.Header>
{#if invoice}
<QrCodeSvg content={invoice} />
<div class="flex gap-2">
<Input bind:value={invoice} readonly />
<CopyButton text={invoice} />
</div>
<Button on:click={payWithWebLn}>Pay with WebLN</Button>
{:else}
<Button on:click={zap}>Create invoice</Button>
{/if} {/if}
<Dialog.Description>Pay now with Lightning</Dialog.Description> </Dialog.Content>
</Dialog.Header> </Dialog.Root>
{#if invoice} {/if}
<QrCodeSvg content={invoice} />
<div class="flex gap-2">
<Input bind:value={invoice} readonly />
<CopyButton text={invoice} />
</div>
<Button on:click={payWithWebLn}>Pay with WebLN</Button>
{:else}
<Button on:click={zap}>Create invoice</Button>
{/if}
</Dialog.Content>
</Dialog.Root>

View File

@@ -3,6 +3,7 @@
import type { NDKEvent } from '@nostr-dev-kit/ndk'; import type { NDKEvent } from '@nostr-dev-kit/ndk';
import AddProductToRocket from './AddProductToRocket.svelte'; import AddProductToRocket from './AddProductToRocket.svelte';
import PayNow from './PayNow.svelte'; import PayNow from './PayNow.svelte';
import { Rocket } from '@/event_helpers/rockets';
export let product: NDKEvent; export let product: NDKEvent;
export let rocket: NDKEvent; export let rocket: NDKEvent;
@@ -34,14 +35,8 @@
return test == 3; return test == 3;
} }
function includedInRocket(): boolean { function includedInRocket(rocket:Rocket, product:NDKEvent): boolean {
let included = false; return Boolean(rocket.Products().get(product.id))
for (let p of rocket.getMatchingTags('product')) {
if (p[1].split(':')[0] == product.id) {
included = true;
}
}
return included;
} }
</script> </script>
@@ -71,10 +66,10 @@
/> />
{/if} {/if}
<Card.Footer class="flex justify-center pt-2"> <Card.Footer class="flex justify-center pt-2">
{#if !includedInRocket()} {#if !includedInRocket(new Rocket(rocket), product)}
<AddProductToRocket {product} {rocket} /> <AddProductToRocket {product} {rocket} />
{:else} {:else}
<PayNow {product} {rocket} /> <PayNow {product} rocketProduct={new Rocket(rocket).Products().get(product.id)} {rocket} />
{/if} {/if}
</Card.Footer> </Card.Footer>
</Card.Root> </Card.Root>

View File

@@ -8,8 +8,6 @@
export let rocket: NDKEvent; export let rocket: NDKEvent;
export let unratifiedZaps = 0; export let unratifiedZaps = 0;
let products = new Rocket(rocket).Products()
</script> </script>
<Card.Root class="sm:col-span-3"> <Card.Root class="sm:col-span-3">
@@ -18,7 +16,7 @@
<Card.Description></Card.Description> <Card.Description></Card.Description>
</Card.Header> </Card.Header>
<Card.Content class="grid grid-cols-1 gap-2"> <Card.Content class="grid grid-cols-1 gap-2">
{#each products as product (product.ID)} {#each new Rocket(rocket).Products() as [id, product] (id)}
<div> <div>
<ProductCardFromId {rocket} productID={product.ID}> <ProductCardFromId {rocket} productID={product.ID}>
<ProductPurchases bind:unratifiedZaps={unratifiedZaps} {rocket} {product} /> <ProductPurchases bind:unratifiedZaps={unratifiedZaps} {rocket} {product} />

View File

@@ -67,10 +67,11 @@ export class Rocket {
} }
return ''; return '';
} }
Products(): RocketProduct[] { Products(): Map<string, RocketProduct> {
let _products: RocketProduct[] = []; let _products = new Map<string, RocketProduct>()
for (let p of this.Event.getMatchingTags('product')) { for (let p of this.Event.getMatchingTags('product')) {
_products.push(new RocketProduct(p)); let rp = new RocketProduct(p)
_products.set(rp.ID, rp);
} }
return _products; return _products;
} }
@@ -521,7 +522,7 @@ export class ZapPurchase {
return true; return true;
} }
let product = this.ProductFromRocket(rocket); let product = this.ProductFromRocket(rocket);
if (product && this.Amount >= product.Price) { if (product && this.Amount/1000 >= product.Price) {
return true; return true;
} }
return false; return false;