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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// Deposit holds.
//
// While `hold:enabled` is set, every Lightning (and bolt12) deposit adds to
// the receiving account's `held:<id>`. Held sats can still be spent over
// Lightning or sent to another coinos account, but not withdrawn on-chain
// (bitcoin, liquid) or over Lightning into our own on-chain liquidity (the
// nodes in `hold:exit_nodes`). Spending held sats over Lightning releases
// that much of the hold. An internal transfer of held sats moves the hold to
// the receiver, so passing funds through a second account doesn't clear it.
// The check and the release happen inside the atomic debit script in
// lib/db.ts. A failed send that's reversed gets its hold back.
//
// The first `hold:free_sat` (default 50k) of Lightning deposits per account
// in any 24h aren't held at all, and holds older than `hold:expire_hours`
// (default 72h) are released by `holds.ts expire` (hourly cron) unless the
// account is frozen. Big recent deposits are what we want to look at; small
// ones were mostly false positives.
//
// Accounts with a hold are listed in the `held` set, and each account keeps a
// short log of what added to or lifted its hold (`held:log:<id>`). Review and
// lift holds with scripts/holds.ts.
import { db } from "$lib/db";
export const holdKey = (id: string) => `held:${id}`;
const LOG_MAX = 100;
// Add to a hold inside the caller's MULTI so the balance and the hold change
// together.
export const addHold = (m: any, id: string, amount: number, entry: Record<string, any>) => {
m.incrBy(holdKey(id), amount)
.sAdd("held", id)
.lPush(`held:log:${id}`, JSON.stringify({ at: Date.now(), amount, ...entry }))
.lTrim(`held:log:${id}`, 0, LOG_MAX - 1);
};
export const HOLD_FREE_SAT = 50_000;
export const HOLD_EXPIRE_HOURS = 72;
// How much of a Lightning deposit falls inside the account's daily free
// allowance. Counts the deposit against the allowance as a side effect.
export const holdAllowance = async (id: string, amount: number) => {
const cap = Number(await db.get("hold:free_sat") ?? HOLD_FREE_SAT);
if (!(cap > 0) || !(amount > 0)) return 0;
const k = `holdfree:${id}`;
const used = await db.incrBy(k, amount);
if (used === amount) await db.expire(k, 86400);
return Math.max(0, Math.min(amount, cap - (used - amount)));
};
export const logHold = async (id: string, entry: Record<string, any>) => {
await db
.multi()
.lPush(`held:log:${id}`, JSON.stringify({ at: Date.now(), ...entry }))
.lTrim(`held:log:${id}`, 0, LOG_MAX - 1)
.exec();
};
// Lightning destinations that are really our own on-chain liquidity (the
// swap/Ark node). Cached briefly: it's checked on every Lightning send.
let exitNodes = { at: 0, set: new Set<string>() };
export const isExitNode = async (pubkey?: string) => {
if (!pubkey) return false;
if (Date.now() - exitNodes.at > 60_000)
exitNodes = { at: Date.now(), set: new Set(await db.sMembers("hold:exit_nodes")) };
return exitNodes.set.has(pubkey);
};
export const heldMessage = (held: number, available: number) =>
`⚡️${held} of your balance came from recent Lightning deposits and can't be withdrawn on-chain right now. ` +
`You can still send it over Lightning. Available for on-chain withdrawal: ⚡️${available}.`;
|