Skip to content

Commit

Permalink
Add reactions to configuration to allow different zaps values. Update…
Browse files Browse the repository at this point in the history
… ZAP_VALUE to DEFAULT_ZAP_VALUE.
  • Loading branch information
silenceway committed Jul 23, 2023
1 parent 23a92eb commit d7b3921
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Modify the environment variables file:
- USER_SECRET_KEY : Your hex secret key
- NOSTR_RELAYS : A list of relays comma separated
- LNDHUB_URL : LNDHub server
- ZAP_VALUE : Zap amount
- DEFAULT_ZAP_VALUE : Default zap amount, empty if you only want specific reactions zaps.
- REACTION_VALUE_* : Reaction zap value. Emoji or string and value. Sample: 🤙|21 - +|1

* There is a known bug with yarn build in nostr-relaypool library

Expand Down
16 changes: 13 additions & 3 deletions src/actions/doPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ import { finishEvent, nip57, type Event as NostrEvent } from 'nostr-tools';
import fetch from 'node-fetch';

import { pool } from '..';
import { nostrRelays, publicKey, secretKey, zapValue } from '../constants/config';
import { nostrRelays, reactionsValues, secretKey, zapValue } from '../constants/config';
import { getProfile } from './profile';
import { connectToLndhubApi } from '../utils/lndhubapi';
import { saveZap, zapExists } from '../utils/database';

export const doPayment = async (event: NostrEvent) => {
const { content } = event;
const isReaction = reactionsValues.filter(reaction => reaction.type === content);
let amount = 0;

if (isReaction.length) {
console.log(event);
amount = isReaction[0].value;
} else {
amount = parseInt(zapValue);
}

const authorTag = event.tags.filter((tag: string[]) => tag[0] === 'p');
const noteTag = event.tags.filter((tag: string[]) => tag[0] === 'e');
const author = authorTag.length ? authorTag[0][1] : '';
const note = noteTag.length ? noteTag[0][1] : '';

if (!zapExists(note)) {
if (!zapExists(note) && amount && author) {
console.log(`Starting payment to ${note}.`);
const amount = parseInt(zapValue);
const invoice = await createInvoice(author, note, amount);
const lndhub = await connectToLndhubApi();

Expand Down
2 changes: 1 addition & 1 deletion src/actions/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const getMetadata = async (authorId: string, kind: Kind = Kind.Metadata)
return metadata;
};

export const extractProfileMetadataContent = (profileMetadata: any) => JSON.parse(profileMetadata.content);
export const extractProfileMetadataContent = (profileMetadata: any) => profileMetadata?.content && JSON.parse(profileMetadata.content) || {};

export const getProfile = async (authorId: string): Promise<any> => {
const metadataPromise = getMetadata(authorId);
Expand Down
22 changes: 20 additions & 2 deletions src/constants/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import dotenv from 'dotenv';
import { nip19 } from 'nostr-tools';
import { nip19 } from 'nostr-tools';

import { Reaction } from 'src/interfaces/config';

dotenv.config();

const nPubKey = process.env.USER_PUBLIC_KEY;
const serverRelays = process.env.NOSTR_RELAYS;

export const secretKey = process.env.USER_SECRET_KEY ?? '';
export const zapValue = process.env.ZAP_VALUE ?? "0";
export const zapValue = process.env.DEFAULT_ZAP_VALUE ?? "0";
export const publicKey = nip19.decode(nPubKey ?? '');
export const nostrRelays = serverRelays?.split(',') || [];

const reactions: Reaction[] = [];

Object.keys(process.env).forEach((element) => {
if (element.startsWith('REACTION_VALUE') && !!process.env[element]) {
const value = process.env[element]?.split('|');

if (value?.[0]) {
const reaction = { type: value[0], value: parseInt(value[1]) };
reactions.push(reaction);
}
}
});

export const reactionsValues = reactions;

4 changes: 4 additions & 0 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Reaction {
type: string;
value: number;
}

0 comments on commit d7b3921

Please sign in to comment.