aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-16 15:33:22 -0700
committerAdam Soltys <[email protected]>2024-10-16 15:33:22 -0700
commite0ea06df4572c5183c5a4fd1697ca59c2614672c (patch)
treebd42b8194aa3485964beaef5ec2f8708e506503a /lib/db.ts
parent4501538630af9d6026fe5661da0386d1b5d65008 (diff)
downloadcoinos-server-e0ea06df4572c5183c5a4fd1697ca59c2614672c.tar.gz
coinos-server-e0ea06df4572c5183c5a4fd1697ca59c2614672c.zip
atomic payment reversal
Diffstat (limited to 'lib/db.ts')
-rw-r--r--lib/db.ts37
1 files changed, 32 insertions, 5 deletions
diff --git a/lib/db.ts b/lib/db.ts
index 4f113a1d..82fa2cec 100644
--- a/lib/db.ts
+++ b/lib/db.ts
@@ -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),
},