aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-04 23:13:33 -0800
committerAdam Soltys <[email protected]>2026-02-06 09:00:09 -0800
commit1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb (patch)
tree960318969072afa63536e9429c27fa40ca2919d3 /lib/db.ts
parentc0481268da25eed1f25281508b5c4777d26c2b03 (diff)
downloadcoinos-server-1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb.tar.gz
coinos-server-1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb.zip
tigerbeetle
Diffstat (limited to 'lib/db.ts')
-rw-r--r--lib/db.ts67
1 files changed, 1 insertions, 66 deletions
diff --git a/lib/db.ts b/lib/db.ts
index 91770824..5ebe38b5 100644
--- a/lib/db.ts
+++ b/lib/db.ts
@@ -1,75 +1,10 @@
import config from "$config";
import { err, warn } from "$lib/logging";
import { fail, sleep } from "$lib/utils";
-import { createClient, defineScript } from "redis";
-
-const DEBIT = `
-local balanceKey = KEYS[1]
-local creditKey = KEYS[2]
-local insufficientString = KEYS[3];
-local amount = tonumber(ARGV[1])
-local tip = tonumber(ARGV[2])
-local fee = tonumber(ARGV[3])
-local ourfee = tonumber(ARGV[4])
-local frozen = tonumber(ARGV[5])
-
-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
-
-if balance - frozen < amount + tip + fee + ourfee then
- return {err = insufficientString .. ' ⚡️' .. balance - frozen .. ' / ' .. amount + tip + fee + ourfee}
-end
-
-redis.call('decrby', creditKey, tostring(math.floor(covered)))
-redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee)))
-
-return ourfee
-`;
-
-const REVERSE = `
-local paymentKey = KEYS[1]
-local balanceKey = KEYS[2]
-local creditKey = KEYS[3]
-local hashKey = KEYS[4]
-local paymentsKey = KEYS[5]
-local pid = KEYS[6]
-
-local total = tonumber(ARGV[1])
-local credit = tonumber(ARGV[2])
-local hash = ARGV[3]
-
-if redis.call('exists', paymentKey) == 1 then
- redis.call('del', paymentKey)
- redis.call('srem', 'pending', hash)
- redis.call('incrby', balanceKey, total)
- redis.call('incrby', creditKey, credit)
- redis.call('del', hashKey)
- redis.call('lrem', paymentsKey, 0, pid)
- redis.call('lrem', 'payments', 0, pid)
- return pid
-else
- error("Payment has already been reversed" .. paymentKey)
-end
-`;
-
-const debit = defineScript({
- NUMBER_OF_KEYS: 3,
- SCRIPT: DEBIT,
- transformArguments: (...args) => args.map((a) => a.toString()),
-});
-
-const reverse = defineScript({
- NUMBER_OF_KEYS: 6,
- SCRIPT: REVERSE,
- transformArguments: (...args) => args.map((a) => a.toString()),
-});
+import { createClient } from "redis";
export const db = createClient({
url: config.db,
- scripts: { debit, reverse },
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 5000),
},