1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import config from "$config";
import {
CashuMint,
CashuWallet,
getDecodedToken,
getEncodedToken,
} from "@cashu/cashu-ts";
import { g, s } from "$lib/db";
let wallet = new CashuWallet(new CashuMint(config.mintUrl));
let enc = (proofs) =>
getEncodedToken({
token: [{ mint: config.mintUrl, proofs }],
});
let dec = (token) => getDecodedToken(token).token[0];
export async function melt(user, amount) {
let { proofs } = dec(await g(`cash:${user.id}`));
let { proofs: current } = dec(await g(`cash`));
let { send, returnChange } = await wallet.send(amount, proofs);
await s(`cash`, enc([...current, ...send]));
await s(`cash:${user.id}`, enc(returnChange));
}
export async function mint(user, amount) {
let { proofs } = dec(await g(`cash`));
let { proofs: current } = dec(await g(`cash:${user.id}`));
let { send, returnChange } = await wallet.send(amount, proofs);
await s(`cash:${user.id}`, enc([...current, ...send]));
await s(`cash`, enc(returnChange));
}
export async function balance(user) {
let { proofs } = dec(await g(`cash:${user.id}`));
return proofs.reduce((a, b) => a + b.amount, 0);
}
|