2025-04-02 17:47:30 -05:00
|
|
|
import Bolt11Decoder from 'light-bolt11-decoder';
|
2024-04-01 18:13:38 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
export const getSatAmountFromInvoice = invoice => {
|
|
|
|
const decoded = Bolt11Decoder.decode(invoice);
|
|
|
|
return decoded.sections[2].value / 1000;
|
|
|
|
};
|
2024-08-13 14:42:36 -05:00
|
|
|
|
|
|
|
export const getTotalFromZaps = (zaps, event) => {
|
2025-04-02 17:47:30 -05:00
|
|
|
let total = 0;
|
|
|
|
let uniqueZaps = new Set();
|
|
|
|
zaps.forEach(zap => {
|
|
|
|
// If the zap matches the event or the parameterized event, then add the zap to the total
|
|
|
|
if (
|
|
|
|
(zap.tags.find(tag => tag[0] === 'e' && tag[1] === event.id) ||
|
|
|
|
zap.tags.find(
|
|
|
|
tag => tag[0] === 'a' && tag[1] === `${event.kind}:${event.pubkey}:${event.d}`
|
|
|
|
)) &&
|
|
|
|
!uniqueZaps.has(zap.id)
|
|
|
|
) {
|
|
|
|
uniqueZaps.add(zap.id);
|
|
|
|
const bolt11Tag = zap.tags.find(tag => tag[0] === 'bolt11');
|
|
|
|
const invoice = bolt11Tag ? bolt11Tag[1] : null;
|
|
|
|
if (invoice) {
|
|
|
|
const amount = getSatAmountFromInvoice(invoice);
|
|
|
|
total += amount;
|
2024-08-13 14:42:36 -05:00
|
|
|
}
|
2025-04-02 17:47:30 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return total;
|
|
|
|
};
|