Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Fix Zap Error #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/components/Posts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getEventHash, getSignature, nip19, SimplePool } from 'nostr-tools';
import { fetchInvoice, getProfileMetadata, getZapEndpoint } from '../ZapHelper';
import {
fetchInvoice,
getProfileMetadata,
getZapEndpoint,
listenForZapReceipt,
} from '../ZapHelper';
import { Link } from 'react-router-dom';
import React, { useEffect, useState } from 'react';
import ZapModal from '../ZapHelper/ZapModal';
Expand Down Expand Up @@ -138,7 +143,7 @@ export async function upvotePost(noteId, userPublicKey) {
}
}

export const sendNewZaps = async (postId, opPubKey, sats = 11) => {
export const sendNewZaps = async (postId, opPubKey, sats = 2) => {
console.log('Sending zaps');
const pubKey = opPubKey;
let relays = [
Expand All @@ -147,9 +152,17 @@ export const sendNewZaps = async (postId, opPubKey, sats = 11) => {
'wss://nos.lol',
'wss://nostr.bitcoiner.social',
];
const encodedNoteId = nip19.noteEncode(postId);

console.log('Fetching profile metadata for publicKey:', pubKey);
let userDetails = await getProfileMetadata(pubKey);
console.log('User details:', userDetails);

console.log('Fetching Zap endpoint for userDetails:', userDetails);
let zapEndpoint = await getZapEndpoint(userDetails);
console.log('Zap endpoint:', zapEndpoint);

console.log('Fetching invoice');
let encodedNoteId = nip19.noteEncode(postId);
let invoice = await fetchInvoice({
zapEndpoint: zapEndpoint,
amount: sats * 1000,
Expand All @@ -158,7 +171,11 @@ export const sendNewZaps = async (postId, opPubKey, sats = 11) => {
noteId: encodedNoteId,
normalizedRelays: relays,
});
console.log('Invoice:', invoice);

let zapUrl = 'lightning:' + invoice;
console.log('Zap URL:', zapUrl);

window.location.assign(zapUrl);
};

Expand Down Expand Up @@ -279,7 +296,9 @@ function Posts(props) {

const handleConfirm = value => {
const postId = props.note.id;
console.log(postId);
let opPubKey = props.note.pubkey;
console.log(opPubKey);
console.log(`Processing value: ${value}`);

sendNewZaps(postId, opPubKey, value);
Expand All @@ -293,6 +312,20 @@ function Posts(props) {
}
}, [postCreatedAt]);

useEffect(() => {
const relays = [
'wss://relay.nostr.band',
'wss://purplepag.es',
'wss://relay.damus.io',
];
const invoice =
'lnbc50n1pnzz7azpp5hj3mln4qaz7fv2t7w2yqdsuawqn5ztjqv9z94rzx3z644lcvg28qhp5n4c2qkxxva69yh95kmy05ltpvwpgnthq8jk033c75tu3hwxhrn3qcqzzsxqyz5vqsp5c4j58uqnfgkmc0xugg6xvvvkjsmmwfxdujrq6ahuyxh8fhkk2k7s9qyyssq0lq00gmmj6hksws4mzlecw258na4tduavuyjvys52hkz902egl3pts85jrpcgwghgy3ua3ut94xcfx6hfrq4wrr6r0pgundzs2chedcq3jtu9l:lnbc110n1pnzzuqmpp58fza0jpqgkk9prremdwyl7xds2hz4qzpdva4afzd7gc5a58tmw8qhp59wnyet69g9q5juej43g43xra33fqhgfza6p90dpspfc9urg8eagqcqzzsxqyz5vqsp5vzrly506plwkd09c2whahsf7erlgg8j488w2u367qnc2v4ws9szq9qyyssq40uds0hgyxqh3wagaka55d07gff6t3crwjat65gzcze3ngyzwkz96tqdtdvntyh0hm05p9844zheg043cem87348s50t38slgdxfc0sq77e9ygp6u59c0a6pj2u0636t6f3vf9k9eue0c7hakara9sjslup9a4pshp5xpyytuef9p89lk8k7atu4lequ97nftmdzjmuvyz45va6h396p78qcqzzsxqyz5vqsp5ycv0d5xwqc3vdy0067kysyttzq4qku52hcf9lnxca5k4w5m33d5s9qyyssqljeyf4p4uzzmfgdljzzqhwwm9j866xwwasv9ls9h5nngrcudwyfqd22whtec4z250285llhrt7f3zv4jrkuasvr0t9ahr8k4n59uqyqpld9gw3';
const closeListener = listenForZapReceipt({ relays, invoice });
return () => {
closeListener();
};
}, []);

useEffect(() => {
const localLikeCount = getLocalLikeCountForPost(props.note.id);
setVotesCount(props.note.voteCount + localLikeCount);
Expand Down
4 changes: 4 additions & 0 deletions src/components/ZapHelper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getProfileMetadata = async authorId => {
kinds: [0],
});
} catch (error) {
alert('not able to find user profile');
throw new Error('failed to fetch user profile :(');
} finally {
pool.close(relays);
Expand All @@ -37,9 +38,12 @@ export const getZapEndpoint = async profileMetadata => {
const zapEndpoint = await nip57.getZapEndpoint(profileMetadata);

if (!zapEndpoint) {
alert('not able to find user end point');
throw new Error('failed to retrieve zap endpoint :(');
}

console.log('Zap Endpoint:', zapEndpoint); // Log the Zap endpoint

return zapEndpoint;
};

Expand Down
Loading