diff options
Diffstat (limited to 'lib/db.ts')
| -rw-r--r-- | lib/db.ts | 25 |
1 files changed, 22 insertions, 3 deletions
@@ -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 `; |
