-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledgerBalanceCache.ts
More file actions
257 lines (230 loc) · 6.82 KB
/
ledgerBalanceCache.ts
File metadata and controls
257 lines (230 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Balance cache helpers for fast bid validation and UI reads.
import type { RedisClient } from "./storage/redis.js";
import type { LedgerEntryType } from "./storage/mongoSchemas.js";
export type BalanceCacheSnapshot = {
userId: string;
currency: string;
available: number;
held: number;
spent: number;
current: number;
updatedAt: Date;
};
type BalanceDelta = {
availableDelta: number;
heldDelta: number;
spentDelta: number;
};
const balanceCacheTtlSeconds = 3600;
const balanceIdempotencyTtlSeconds = 86400;
const balanceDeltaScript = `
local balanceKey = KEYS[1]
local idempotencyKey = KEYS[2]
local ttlSeconds = tonumber(ARGV[1])
local idempotencyTtl = tonumber(ARGV[2])
local availableDelta = tonumber(ARGV[3]) or 0
local heldDelta = tonumber(ARGV[4]) or 0
local spentDelta = tonumber(ARGV[5]) or 0
local currentDelta = tonumber(ARGV[6]) or 0
local userId = ARGV[7]
local currency = ARGV[8]
local updatedAt = ARGV[9]
if idempotencyKey and idempotencyKey ~= "" then
local inserted = redis.call("SETNX", idempotencyKey, updatedAt)
if inserted == 0 then
return 0
end
if idempotencyTtl and idempotencyTtl > 0 then
redis.call("EXPIRE", idempotencyKey, idempotencyTtl)
end
end
if userId and userId ~= "" then
redis.call("HSET", balanceKey, "userId", userId)
end
if currency and currency ~= "" then
redis.call("HSET", balanceKey, "currency", currency)
end
redis.call("HINCRBYFLOAT", balanceKey, "available", availableDelta)
redis.call("HINCRBYFLOAT", balanceKey, "held", heldDelta)
redis.call("HINCRBYFLOAT", balanceKey, "spent", spentDelta)
redis.call("HINCRBYFLOAT", balanceKey, "current", currentDelta)
redis.call("HSET", balanceKey, "updatedAt", updatedAt)
if ttlSeconds and ttlSeconds > 0 then
redis.call("EXPIRE", balanceKey, ttlSeconds)
end
return 1
`;
type BalanceScriptRedis = RedisClient & {
applyBalanceDelta?: (
keyCount: number,
balanceKey: string,
idempotencyKey: string,
ttlSeconds: string,
idempotencyTtl: string,
availableDelta: string,
heldDelta: string,
spentDelta: string,
currentDelta: string,
userId: string,
currency: string,
updatedAt: string
) => Promise<number>;
};
function ensureBalanceScript(redis: RedisClient): BalanceScriptRedis {
const client = redis as BalanceScriptRedis;
if (!client.applyBalanceDelta) {
client.defineCommand("applyBalanceDelta", {
numberOfKeys: 2,
lua: balanceDeltaScript
});
}
return client;
}
export function buildBalanceCacheKey(userId: string, currency: string): string {
return `balance:${userId}:${currency}`;
}
export function buildBalanceIdempotencyKey(idempotencyKey: string): string {
return `balance:idemp:${idempotencyKey}`;
}
export async function readBalanceCache(
redis: RedisClient,
userId: string,
currency: string
): Promise<BalanceCacheSnapshot | null> {
const key = buildBalanceCacheKey(userId, currency);
let data: Record<string, string> = {};
try {
data = await redis.hgetall(key);
} catch {
return null;
}
if (!data || Object.keys(data).length === 0) {
return null;
}
const available = parseNumber(data.available);
const held = parseNumber(data.held);
const spent = parseNumber(data.spent);
const current = parseNumber(data.current);
const updatedAt = parseDate(data.updatedAt);
const resolvedUserId = data.userId ?? userId;
const resolvedCurrency = data.currency ?? currency;
if (
available === null ||
held === null ||
spent === null ||
current === null ||
!updatedAt
) {
return null;
}
return {
userId: resolvedUserId,
currency: resolvedCurrency,
available,
held,
spent,
current,
updatedAt
};
}
export async function writeBalanceCache(
redis: RedisClient,
balance: BalanceCacheSnapshot,
ttlSeconds = balanceCacheTtlSeconds
): Promise<void> {
const key = buildBalanceCacheKey(balance.userId, balance.currency);
const pipeline = redis.multi();
pipeline.hset(key, {
userId: balance.userId,
currency: balance.currency,
available: balance.available.toString(),
held: balance.held.toString(),
spent: balance.spent.toString(),
current: balance.current.toString(),
updatedAt: balance.updatedAt.toISOString()
});
if (ttlSeconds > 0) {
pipeline.expire(key, ttlSeconds);
}
await pipeline.exec();
}
export async function applyBalanceDelta(
redis: RedisClient,
input: {
userId: string;
currency: string;
entryType: LedgerEntryType;
amount: number;
idempotencyKey: string;
updatedAt?: Date;
},
ttlSeconds = balanceCacheTtlSeconds,
idempotencyTtlSeconds = balanceIdempotencyTtlSeconds
): Promise<boolean> {
const deltas = resolveBalanceDelta(input.entryType, input.amount);
if (!deltas) {
return false;
}
const updatedAt = input.updatedAt ?? new Date();
const balanceKey = buildBalanceCacheKey(input.userId, input.currency);
const idempotencyKey = buildBalanceIdempotencyKey(input.idempotencyKey);
const client = ensureBalanceScript(redis);
const currentDelta = deltas.availableDelta + deltas.heldDelta;
if (!client.applyBalanceDelta) {
throw new Error("Balance delta script not loaded");
}
const applied = await client.applyBalanceDelta(
2,
balanceKey,
idempotencyKey,
ttlSeconds.toString(),
idempotencyTtlSeconds.toString(),
deltas.availableDelta.toString(),
deltas.heldDelta.toString(),
deltas.spentDelta.toString(),
currentDelta.toString(),
input.userId,
input.currency,
updatedAt.toISOString()
);
return applied === 1;
}
function resolveBalanceDelta(entryType: LedgerEntryType, amount: number): BalanceDelta | null {
if (!Number.isFinite(amount) || amount <= 0) {
return null;
}
switch (entryType) {
case "deposit_confirmed":
return { availableDelta: amount, heldDelta: 0, spentDelta: 0 };
case "hold_created":
return { availableDelta: -amount, heldDelta: amount, spentDelta: 0 };
case "hold_released":
return { availableDelta: amount, heldDelta: -amount, spentDelta: 0 };
case "hold_captured":
return { availableDelta: 0, heldDelta: -amount, spentDelta: amount };
case "withdrawal_requested":
return { availableDelta: -amount, heldDelta: amount, spentDelta: 0 };
case "withdrawal_confirmed":
return { availableDelta: 0, heldDelta: -amount, spentDelta: amount };
case "withdrawal_failed":
return { availableDelta: amount, heldDelta: -amount, spentDelta: 0 };
case "withdrawal_broadcasted":
return null;
default:
return null;
}
}
function parseNumber(value?: string): number | null {
if (!value) {
return null;
}
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
function parseDate(value?: string): Date | null {
if (!value) {
return null;
}
const parsed = new Date(value);
return Number.isNaN(parsed.getTime()) ? null : parsed;
}