Zaps

high level

Example

zap, receipt
final amount = 21;
final lnurl = "opensats@vlt.ge";
final comment = "enjoy this zap from NDK";

ZapResponse response = await ndk.zaps.zap(
    nwcConnection: connection,
    lnurl: lnurl,
    comment: comment,
    amountSats: amount,
    fetchZapReceipt: true,
    signer: Bip340EventSigner(
        privateKey: key.privateKey, publicKey: key.publicKey),
    relays: ["wss://relay.damus.io"],
    pubKey:
        "787338757fc25d65cd929394d5e7713cf43638e8d259e8dcf5c73b834eb851f2",
    eventId:
        "906a0c5920b59e5754d0df5164bfea2a8d48ce5d73beaa1e854b3e6725e3288a");

if (response.payInvoiceResponse != null &&
    response.payInvoiceResponse!.preimage != null &&
    response.payInvoiceResponse!.preimage!.isNotEmpty) {
  print(
      "Payed $amount to $lnurl, preimage = ${response.payInvoiceResponse!.preimage}");

  print("Waiting for Zap Receipt...");
  ZapReceipt? receipt = await response.zapReceipt;
  if (receipt != null) {
    print("Receipt : $receipt");
  } else {
    print("No Receipt");
  }
}

await ndk.destroy();
receipts for profile
  }

  final dateTime = DateTime.fromMillisecondsSinceEpoch(paidAt * 1000).toLocal();
  final year = dateTime.year.toString().padLeft(4, '0');
  final month = dateTime.month.toString().padLeft(2, '0');
  final day = dateTime.day.toString().padLeft(2, '0');
  final hour = dateTime.hour.toString().padLeft(2, '0');
  final minute = dateTime.minute.toString().padLeft(2, '0');

  return "$year-$month-$day $hour:$minute";
}

void main() async {
  final ndk = Ndk.defaultConfig();

  print("fetching zap receipts for profile ");
  final profileReceipts = await ndk.zaps
      .fetchZappedReceipts(
        pubKey:
            "30782a8323b7c98b172c5a2af7206bb8283c655be6ddce11133611a03d5f1177",
      )
      .toList();

  // Sort profileReceipts by paidAt (created_at) in descending order
receipts for event
print("fetching zap receipts for single event ");
final receipts = await ndk.zaps
    .fetchZappedReceipts(
        pubKey:
            "787338757fc25d65cd929394d5e7713cf43638e8d259e8dcf5c73b834eb851f2",
        eventId:
            "906a0c5920b59e5754d0df5164bfea2a8d48ce5d73beaa1e854b3e6725e3288a")
    .toList();

// Sort eventReceipts by amountSats in descending order
receipts.sort((a, b) => (b.amountSats ?? 0).compareTo(a.amountSats ?? 0));

int eventSum = 0;
for (var receipt in receipts) {
  String? sender;
  if (receipt.sender != null) {
    Metadata? metadata = await ndk.metadata.loadMetadata(receipt.sender!);
    sender = metadata?.name;
  }
  print(
      "${sender != null ? "from $sender " : ""} ${receipt.amountSats} sats ${receipt.comment}");
  eventSum += receipt.amountSats ?? 0;
}
print("${receipts.length} receipts, total of $eventSum sats");

How to use

You need a nostr+walletconnect://... uri from your NWC wallet service provider.

see https://github.com/getAlby/awesome-nwc for more info how to get a wallet supporting NWC