diff options
| -rw-r--r-- | lib/db.ts | 37 | ||||
| -rw-r--r-- | lib/locations.ts | 2 | ||||
| -rw-r--r-- | lib/payments.ts | 18 | ||||
| -rw-r--r-- | routes/lnurl.ts | 3 |
4 files changed, 46 insertions, 14 deletions
@@ -1,9 +1,9 @@ import config from "$config"; -import { fail, wait, sleep } from "$lib/utils"; +import { fail, sleep } from "$lib/utils"; import { createClient, defineScript } from "redis"; -import { warn, l, err } from "$lib/logging"; +import { warn, err } from "$lib/logging"; -let SCRIPT = ` +let DEBIT = ` local balanceKey = KEYS[1] local creditKey = KEYS[2] local amount = tonumber(ARGV[1]) @@ -27,15 +27,42 @@ redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee return ourfee `; +let REVERSE = ` +local paymentKey = KEYS[1] +local balanceKey = KEYS[2] +local creditKey = KEYS[3] +local hashKey = KEYS[4] + +local total = tonumber(ARGV[1]) +local credit = tonumber(ARGV[2]) + +local payment = cjson.decode(redis.call('get', paymentKey)) + +if payment ~= nil then + redis.call('incrby', balanceKey, total) + redis.call('incrby', creditKey, credit) + redis.call('del', paymentKey) + redis.call('del', hashKey) +end + +return paymentKey +`; + let debit = defineScript({ NUMBER_OF_KEYS: 2, - SCRIPT, + SCRIPT: DEBIT, + transformArguments: (...args) => args.map((a) => a.toString()), +}); + +let reverse = defineScript({ + NUMBER_OF_KEYS: 4, + SCRIPT: REVERSE, transformArguments: (...args) => args.map((a) => a.toString()), }); export let db = createClient({ url: config.db, - scripts: { debit }, + scripts: { debit, reverse }, socket: { reconnectStrategy: (retries) => Math.min(retries * 50, 5000), }, diff --git a/lib/locations.ts b/lib/locations.ts index 4122c25a..560d5259 100644 --- a/lib/locations.ts +++ b/lib/locations.ts @@ -8,7 +8,7 @@ export let getLocations = async () => { let previous = await g("locations"); let since = await g("locations:since"); if (!since) since = "2022-09-19T00:00:00Z"; - if (Date.now() - new Date(since).getTime() < 60000) return; + if (Date.now() - new Date(since).getTime() < 60000) return; let locations: Array<any> = await got( `https://api.btcmap.org/v2/elements?updated_since=${since}`, diff --git a/lib/payments.ts b/lib/payments.ts index a073bec7..1f1497ec 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -809,14 +809,18 @@ let finalize = async (r, p) => { }; let reverse = async (p) => { - await db.del(`payment:${p.hash}`); - await db.del(`payment:${p.id}`); - - let total = Math.abs(p.amount); + let total = Math.abs(p.amount) + p.fee; let ourfee = p.ourfee || 0; let credit = Math.round(total * config.fee) - ourfee; - await db.incrBy(`balance:${p.uid}`, total + p.fee + ourfee); - await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit); - await db.lRem(`${p.uid}:payments`, 1, p.id); + let k = await db.reverse( + `payment:${p.id}`, + `balance:${p.uid}`, + `credit:${types.lightning}:${p.uid}`, + `payment:${p.hash}`, + total, + credit, + ); + + warn("reversed", k); }; diff --git a/routes/lnurl.ts b/routes/lnurl.ts index 59fc6ba1..fec516c1 100644 --- a/routes/lnurl.ts +++ b/routes/lnurl.ts @@ -69,6 +69,7 @@ export default { maxSendable, metadata, nostrPubkey: serverPubkey, + commentAllowed: 512, callback: `${URL}/api/lnurl/${id}`, tag: "payRequest", }); @@ -81,7 +82,7 @@ export default { async lnurl(req, res) { let { params: { id }, - query: { amount, nostr }, + query: { amount, comment, nostr }, } = req; try { let uid = await g(`lnurl:${id}`); |
