aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-25 09:25:18 -0700
committerAdam Soltys <[email protected]>2026-11-25 09:25:18 -0700
commit6d5f69a23b8e52513825465a06911d67702edec3 (patch)
treeeab409c6d3394c37f9b8b790e7f3e7f8b9ac4301 /lib/db.ts
parent00799bb5ed16d3f2514ba68938e479110a1412a9 (diff)
downloadcoinos-server-6d5f69a23b8e52513825465a06911d67702edec3.tar.gz
coinos-server-6d5f69a23b8e52513825465a06911d67702edec3.zip
payments: hold Lightning deposits back from on-chain withdrawal
While hold:enabled is set, Lightning deposits above a daily free allowance are held: they can still be spent over Lightning or sent internally, but not withdrawn on-chain or over Lightning into our own on-chain liquidity. The check and release run inside the atomic debit script, internal transfers carry the hold to the receiver, and old holds expire via scripts/holds.ts. The listener notes which peer each deposit arrived from for review. sendOnchain now refuses frozen accounts before building the transaction or locking its inputs, so a frozen account retrying withdrawals cannot hold hot-wallet UTXOs and make other sends fail with locked outputs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6SEnsbshZrUDtTsxAX
Diffstat (limited to 'lib/db.ts')
-rw-r--r--lib/db.ts25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/db.ts b/lib/db.ts
index e03fb810..f3edba11 100644
--- a/lib/db.ts
+++ b/lib/db.ts
@@ -12,20 +12,39 @@ local tip = tonumber(ARGV[2])
local fee = tonumber(ARGV[3])
local ourfee = tonumber(ARGV[4])
local frozen = tonumber(ARGV[5])
+-- Optional deposit hold (see lib/hold.ts). Held sats can leave over Lightning
+-- or internally but not on-chain; spending them releases the hold, and the
+-- amount released is returned so the receiver of an internal send inherits it.
+local heldKey = ARGV[6]
+local onchain = ARGV[7] == '1'
+local held = 0
+if heldKey and heldKey ~= '' then held = tonumber(redis.call('get', heldKey) or '0') end
local balance = tonumber(redis.call('get', balanceKey) or '0')
local credit = tonumber(redis.call('get', creditKey) or '0')
local covered = math.min(credit, ourfee)
ourfee = ourfee - covered
+local total = amount + tip + fee + ourfee
-if balance - frozen < amount + tip + fee + ourfee then
- return {err = insufficientString .. ' ⚡️' .. balance - frozen .. ' / ' .. amount + tip + fee + ourfee}
+if balance - frozen < total then
+ return {err = insufficientString .. ' ⚡️' .. balance - frozen .. ' / ' .. total}
+end
+if onchain and held > 0 and balance - math.max(frozen, held) < total then
+ return {err = 'HELD:' .. held .. ':' .. math.max(0, balance - math.max(frozen, held))}
end
redis.call('decrby', creditKey, tostring(math.floor(covered)))
-redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee)))
+redis.call('decrby', balanceKey, tostring(math.floor(total)))
+if heldKey and heldKey ~= '' then
+ local used = 0
+ if not onchain and held > 0 then
+ used = math.min(held, math.floor(total))
+ redis.call('decrby', heldKey, tostring(used))
+ end
+ return {ourfee, used}
+end
return ourfee
`;