From 76a591ab6db38500d4ffded99938e2fdc8a0897b Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 19 Jul 2024 17:20:31 -0300 Subject: [PATCH] feat: create getZapSplits function --- src/utils/zap_split.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/utils/zap_split.ts diff --git a/src/utils/zap_split.ts b/src/utils/zap_split.ts new file mode 100644 index 00000000..68aabc8a --- /dev/null +++ b/src/utils/zap_split.ts @@ -0,0 +1,35 @@ +import { NSchema as n, NStore } from '@nostrify/nostrify'; +import { isNumberFrom1To100 } from '@/utils.ts'; + +type Pubkey = string; +type ExtraMessage = string; +/** Number from 1 to 100, stringified. */ +type splitPercentages = string; + +type DittoZapSplits = { + [key: Pubkey]: [splitPercentages, ExtraMessage]; +}; + +/** Gets zap splits from NIP-78 in DittoZapSplits format. */ +export async function getZapSplits(store: NStore, pubkey: string): Promise { + const zapSplits: DittoZapSplits = {}; + + const [event] = await store.query([{ + authors: [pubkey], + kinds: [30078], + '#d': ['pub.ditto.zapSplits'], + limit: 1, + }]); + if (!event) return {}; + + for (const tag of event.tags) { + if ( + tag[0] === 'p' && n.id().safeParse(tag[1]).success && + isNumberFrom1To100(tag[2]) + ) { + zapSplits[tag[1]] = [tag[2], tag[3] ?? '']; + } + } + + return zapSplits; +}