mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
checkpoint: implement nutzap redemption history (kind 7376)
This commit is contained in:
parent
d61f0d1d4b
commit
f7e49cd5ec
1 changed files with 31 additions and 5 deletions
|
|
@ -546,16 +546,27 @@ export const swapNutzapsToWalletController: AppController = async (c) => {
|
||||||
nutzapsFilter.since = nutzapHistory.created_at;
|
nutzapsFilter.since = nutzapHistory.created_at;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mintsToProofs: { [key: string]: { proofs: Proof[]; redeemed: string[][] } } = {};
|
||||||
|
|
||||||
const nutzaps = await store.query([nutzapsFilter], { signal });
|
const nutzaps = await store.query([nutzapsFilter], { signal });
|
||||||
|
|
||||||
const mintsToProofs: { [key: string]: Proof[] } = {};
|
|
||||||
nutzaps.forEach(async (event) => {
|
nutzaps.forEach(async (event) => {
|
||||||
try {
|
try {
|
||||||
const { mint, proofs }: { mint: string; proofs: Proof[] } = JSON.parse( // TODO: create a merge request in nostr tools or Nostrify to do this in a nice way?
|
const { mint, proofs }: { mint: string; proofs: Proof[] } = JSON.parse( // TODO: create a merge request in nostr tools or Nostrify to do this in a nice way?
|
||||||
await nip44.decrypt(pubkey, event.content),
|
await nip44.decrypt(pubkey, event.content),
|
||||||
);
|
);
|
||||||
if (typeof mint === 'string') {
|
if (typeof mint === 'string') {
|
||||||
mintsToProofs[mint] = [...(mintsToProofs[mint] || []), ...proofs];
|
mintsToProofs[mint].proofs = [...(mintsToProofs[mint].proofs || []), ...proofs];
|
||||||
|
mintsToProofs[mint].redeemed = [
|
||||||
|
...(mintsToProofs[mint].redeemed || []),
|
||||||
|
[
|
||||||
|
'e', // nutzap event that has been redeemed
|
||||||
|
event.id,
|
||||||
|
Conf.relay,
|
||||||
|
'redeemed',
|
||||||
|
],
|
||||||
|
['p', event.pubkey], // pubkey of the author of the 9321 event (nutzap sender)
|
||||||
|
];
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// do nothing, for now... (maybe print errors)
|
// do nothing, for now... (maybe print errors)
|
||||||
|
|
@ -563,12 +574,12 @@ export const swapNutzapsToWalletController: AppController = async (c) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const mint of Object.keys(mintsToProofs)) {
|
for (const mint of Object.keys(mintsToProofs)) {
|
||||||
const token = getEncodedToken({ mint, proofs: mintsToProofs[mint] }, { version: 3 });
|
const token = getEncodedToken({ mint, proofs: mintsToProofs[mint].proofs }, { version: 3 });
|
||||||
|
|
||||||
const cashuWallet = new CashuWallet(new CashuMint(mint));
|
const cashuWallet = new CashuWallet(new CashuMint(mint));
|
||||||
const receiveProofs = await cashuWallet.receive(token);
|
const receiveProofs = await cashuWallet.receive(token);
|
||||||
|
|
||||||
await createEvent({
|
const unspentProofs = await createEvent({
|
||||||
kind: 7375,
|
kind: 7375,
|
||||||
content: await nip44.encrypt(
|
content: await nip44.encrypt(
|
||||||
pubkey,
|
pubkey,
|
||||||
|
|
@ -579,7 +590,22 @@ export const swapNutzapsToWalletController: AppController = async (c) => {
|
||||||
),
|
),
|
||||||
}, c);
|
}, c);
|
||||||
|
|
||||||
// TODO: create the 7376 history kind, reemded marker, etc
|
const amount = receiveProofs.reduce((accumulator, current) => {
|
||||||
|
return accumulator + current.amount;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
await createEvent({
|
||||||
|
kind: 7376,
|
||||||
|
content: await nip44.encrypt(
|
||||||
|
pubkey,
|
||||||
|
JSON.stringify([
|
||||||
|
['direction', 'in'],
|
||||||
|
['amount', amount],
|
||||||
|
['e', unspentProofs.id, Conf.relay, 'created'],
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
tags: mintsToProofs[mint].redeemed,
|
||||||
|
}, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json(201);
|
return c.json(201);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue