forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayoutService.js
More file actions
549 lines (466 loc) · 15.5 KB
/
Copy pathpayoutService.js
File metadata and controls
549 lines (466 loc) · 15.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// services/payoutService.js
import mongoose from "mongoose";
import * as StellarSdk from "@stellar/stellar-sdk";
import EducatorBalance from "../models/EducatorBalance.js";
import LedgerEntry from "../models/LedgerEntry.js";
import PayoutBatch from "../models/PayoutBatch.js";
import Transaction from "../models/Transaction.js";
import User from "../models/User.js";
import {
toStroops,
fromStroops,
hasUsdcTrustline,
submitTransaction,
verifyPaymentOperations,
getExplorerUrl,
server,
USDC,
networkPassphrase,
PLATFORM_WALLET_PUBLIC_KEY,
} from "./stellar/stellarService.js";
import logger from "../config/logger.js";
/**
* Record earnings for an educator upon transaction confirmation (Idempotent per stellarTxHash)
* @param {Object} transaction - Confirmed Transaction document
* @param {Object} [options] - Options with session
* @returns {Promise<Object>} - Result of balance credit
*/
export const recordSaleEarnings = async (transaction, { session } = {}) => {
if (transaction.status !== "confirmed") {
return { success: false, reason: "Transaction not confirmed" };
}
const txRef = transaction.stellarTxHash || transaction._id.toString();
// Idempotency check: verify if a sale ledger entry already exists for this txRef
const existingEntry = await LedgerEntry.findOne({
txRef,
type: "sale",
}).session(session || null);
if (existingEntry) {
logger.info(`Sale earnings for transaction ${txRef} already recorded. Skipping.`);
return { success: true, idempotentSkipped: true };
}
if (!transaction.creator) {
return { success: false, reason: "Transaction has no creator" };
}
const netAmountStr = transaction.platformFee?.creatorAmount || transaction.amount;
const amountStroops = toStroops(netAmountStr);
const settlementMode = transaction.settlement || "direct";
let balance = await EducatorBalance.findOne({
educator: transaction.creator,
}).session(session || null);
if (!balance) {
balance = new EducatorBalance({
educator: transaction.creator,
owedStroops: "0",
settledStroops: "0",
});
}
if (settlementMode === "platform_collect") {
const currentOwed = BigInt(balance.owedStroops || "0");
balance.owedStroops = (currentOwed + amountStroops).toString();
} else {
const currentSettled = BigInt(balance.settledStroops || "0");
balance.settledStroops = (currentSettled + amountStroops).toString();
}
await balance.save({ session: session || null });
const ledgerEntry = new LedgerEntry({
educator: transaction.creator,
type: "sale",
txRef,
amount: netAmountStr,
amountStroops: amountStroops.toString(),
settlement: settlementMode,
});
await ledgerEntry.save({ session: session || null });
logger.info(
`Recorded ${settlementMode} sale earnings of ${netAmountStr} USDC (${amountStroops} stroops) for educator ${transaction.creator}`
);
return { success: true, balance, ledgerEntry };
};
/**
* Build a batch payout plan and (if dryRun=false) create unsigned Stellar XDRs
* @param {Object} params
* @param {string[]} [params.educatorIds] - Optional target educator IDs
* @param {string|number} [params.minAmount="0"] - Minimum owed amount threshold
* @param {boolean} [params.dryRun=false] - If true, return plan without DB writes
* @param {string} [params.adminId] - Operator user ID
* @returns {Promise<Object>} - Payout build result
*/
export const buildPayoutBatch = async ({
educatorIds,
minAmount = "0",
dryRun = false,
adminId,
} = {}) => {
const minStroops = toStroops(minAmount);
let candidateEducators = [];
if (Array.isArray(educatorIds) && educatorIds.length > 0) {
candidateEducators = await User.find({ _id: { $in: educatorIds } });
} else {
const balances = await EducatorBalance.find({ owedStroops: { $ne: "0" } });
const userIds = balances.map((b) => b.educator);
candidateEducators = await User.find({ _id: { $in: userIds } });
}
const recipients = [];
const skipped = [];
for (const user of candidateEducators) {
const balance = await EducatorBalance.findOne({ educator: user._id });
const owedStroops = BigInt(balance?.owedStroops || "0");
if (owedStroops <= 0n || owedStroops < minStroops) {
skipped.push({
educatorId: user._id,
name: user.name,
reason: "below minimum",
});
continue;
}
if (!user.stellarWallet?.publicKey) {
skipped.push({
educatorId: user._id,
name: user.name,
reason: "no wallet",
});
continue;
}
const trustlineOk = await hasUsdcTrustline(user.stellarWallet.publicKey);
if (!trustlineOk) {
skipped.push({
educatorId: user._id,
name: user.name,
reason: "no USDC trustline",
});
continue;
}
recipients.push({
educator: user._id,
name: user.name,
wallet: user.stellarWallet.publicKey,
amount: fromStroops(owedStroops),
stroops: owedStroops.toString(),
});
}
const totalStroopsBigInt = recipients.reduce(
(acc, r) => acc + BigInt(r.stroops),
0n
);
const totalAmount = fromStroops(totalStroopsBigInt);
if (dryRun) {
return {
dryRun: true,
totalRecipients: recipients.length,
totalAmount,
totalStroops: totalStroopsBigInt.toString(),
recipients,
skipped,
};
}
if (recipients.length === 0) {
return {
success: false,
message: "No eligible educators for payout",
recipients: [],
skipped,
};
}
const batchId = new mongoose.Types.ObjectId().toString();
const platformWalletKey =
process.env.PLATFORM_WALLET_PUBLIC_KEY || PLATFORM_WALLET_PUBLIC_KEY;
if (!platformWalletKey) {
throw new Error("PLATFORM_WALLET_PUBLIC_KEY environment variable is not set");
}
let sourceAccount;
try {
sourceAccount = await server.loadAccount(platformWalletKey);
} catch (error) {
// Offline fallback for unit tests or network errors
sourceAccount = new StellarSdk.Account(platformWalletKey, "1000");
}
// Chunk recipients into groups of at most 100 payment ops
const CHUNK_SIZE = 100;
const chunkedRecipients = [];
for (let i = 0; i < recipients.length; i += CHUNK_SIZE) {
chunkedRecipients.push(recipients.slice(i, i + CHUNK_SIZE));
}
const chunks = [];
const memoText = `DNB-PAYOUT-${batchId.slice(-16)}`;
for (let cIdx = 0; cIdx < chunkedRecipients.length; cIdx++) {
const chunkRecipients = chunkedRecipients[cIdx];
const builder = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: StellarSdk.BASE_FEE,
networkPassphrase,
});
for (const rec of chunkRecipients) {
builder.addOperation(
StellarSdk.Operation.payment({
destination: rec.wallet,
asset: USDC,
amount: rec.amount,
})
);
}
const tx = builder
.addMemo(StellarSdk.Memo.text(memoText))
.setTimeout(300)
.build();
chunks.push({
chunkIndex: cIdx,
xdr: tx.toXDR(),
hash: tx.hash().toString("hex"),
});
}
const payoutBatch = new PayoutBatch({
batchId,
recipients,
totalAmount,
totalStroops: totalStroopsBigInt.toString(),
chunks,
status: "built",
createdBy: adminId,
});
await payoutBatch.save();
return {
success: true,
batchId,
status: "built",
totalRecipients: recipients.length,
totalAmount,
totalStroops: totalStroopsBigInt.toString(),
recipients,
skipped,
chunks,
};
};
/**
* Submit signed batch XDRs to Stellar, verify payments on-chain, and settle balances atomically
* @param {Object} params
* @param {string} params.batchId - Payout batch ID
* @param {string[]|string} params.signedXdrs - Signed XDRs (array or string)
* @returns {Promise<Object>} - Submission and settlement result
*/
export const submitPayoutBatch = async ({ batchId, signedXdrs }) => {
const xdrs = Array.isArray(signedXdrs) ? signedXdrs : [signedXdrs];
const payoutBatch = await PayoutBatch.findOne({ batchId });
if (!payoutBatch || !["built", "failed"].includes(payoutBatch.status)) {
throw new Error("Payout batch not found or already processed");
}
if (xdrs.length !== payoutBatch.chunks.length) {
throw new Error(
`Expected ${payoutBatch.chunks.length} signed XDR(s), but received ${xdrs.length}`
);
}
// 1. Submit and verify each chunk on Stellar
const CHUNK_SIZE = 100;
for (let cIdx = 0; cIdx < xdrs.length; cIdx++) {
const signedXdr = xdrs[cIdx];
const chunkRecipients = payoutBatch.recipients.slice(
cIdx * CHUNK_SIZE,
(cIdx + 1) * CHUNK_SIZE
);
let submitResult;
try {
submitResult = await submitTransaction(signedXdr);
} catch (stellarError) {
payoutBatch.status = "failed";
payoutBatch.failureReason = `Submission failed for chunk ${cIdx}: ${stellarError.message}`;
await payoutBatch.save();
// Revert to built status as required by spec so batch can be fixed/resubmitted
payoutBatch.status = "built";
await payoutBatch.save();
throw new Error(`Stellar submission failed: ${stellarError.message}`);
}
const expectedPayments = chunkRecipients.map((rec) => ({
destination: rec.wallet,
amount: rec.amount,
}));
const verification = await verifyPaymentOperations(
submitResult.hash,
expectedPayments
);
if (!verification.verified) {
const reason = `Verification failed for chunk ${cIdx}: ${verification.reason}`;
payoutBatch.status = "failed";
payoutBatch.failureReason = reason;
await payoutBatch.save();
// Revert to built status as required by spec
payoutBatch.status = "built";
await payoutBatch.save();
throw new Error(reason);
}
// Save hash back to chunk if verification passed
payoutBatch.chunks[cIdx].hash = submitResult.hash;
}
// 2. All chunks submitted & verified successfully -> Move owed to settled atomically
const session = await mongoose.startSession();
session.startTransaction();
try {
const now = new Date();
for (const rec of payoutBatch.recipients) {
const recStroops = BigInt(rec.stroops);
const balance = await EducatorBalance.findOne({
educator: rec.educator,
}).session(session);
if (balance) {
const currentOwed = BigInt(balance.owedStroops || "0");
const currentSettled = BigInt(balance.settledStroops || "0");
const newOwed = currentOwed >= recStroops ? currentOwed - recStroops : 0n;
const newSettled = currentSettled + recStroops;
balance.owedStroops = newOwed.toString();
balance.settledStroops = newSettled.toString();
balance.lastPayoutAt = now;
await balance.save({ session });
}
const ledgerEntry = new LedgerEntry({
educator: rec.educator,
type: "payout",
txRef: batchId,
amount: rec.amount,
amountStroops: rec.stroops,
});
await ledgerEntry.save({ session });
}
payoutBatch.status = "confirmed";
await payoutBatch.save({ session });
await session.commitTransaction();
logger.info(`Payout batch ${batchId} successfully submitted, verified, and settled.`);
return {
success: true,
batchId,
status: "confirmed",
totalRecipients: payoutBatch.recipients.length,
totalAmount: payoutBatch.totalAmount,
};
} catch (error) {
await session.abortTransaction();
payoutBatch.status = "built";
payoutBatch.failureReason = error.message;
await payoutBatch.save();
throw error;
} finally {
session.endSession();
}
};
/**
* Get educator balance details
* @param {string} educatorId
* @returns {Promise<Object>}
*/
export const getEducatorBalance = async (educatorId) => {
const balance = await EducatorBalance.findOne({ educator: educatorId });
const owedStroops = BigInt(balance?.owedStroops || "0");
const settledStroops = BigInt(balance?.settledStroops || "0");
const lifetimeStroops = owedStroops + settledStroops;
return {
owed: fromStroops(owedStroops),
settled: fromStroops(settledStroops),
lifetime: fromStroops(lifetimeStroops),
owedStroops: owedStroops.toString(),
settledStroops: settledStroops.toString(),
lastPayoutAt: balance?.lastPayoutAt || null,
};
};
/**
* Get educator per-sale statement
* @param {string} educatorId
* @param {Object} queryParams - { from, to }
* @returns {Promise<Array>}
*/
export const getEducatorStatement = async (educatorId, { from, to } = {}) => {
const query = {
creator: educatorId,
status: "confirmed",
};
if (from || to) {
query.createdAt = {};
if (from) query.createdAt.$gte = new Date(from);
if (to) query.createdAt.$lte = new Date(to);
}
const transactions = await Transaction.find(query).sort({ createdAt: -1 });
return transactions.map((tx) => {
const gross = tx.amount;
const fee = tx.platformFee?.platformAmount || "0";
const net = tx.platformFee?.creatorAmount || tx.amount;
return {
transactionId: tx._id,
date: tx.confirmedAt || tx.createdAt,
itemTitle: tx.itemTitle,
itemType: tx.itemType,
gross,
fee,
net,
settlement: tx.settlement || "direct",
stellarTxHash: tx.stellarTxHash,
};
});
};
/**
* Get educator payout batch history
* @param {string} educatorId
* @returns {Promise<Array>}
*/
export const getEducatorHistory = async (educatorId) => {
const batches = await PayoutBatch.find({
"recipients.educator": educatorId,
status: "confirmed",
}).sort({ updatedAt: -1 });
return batches.map((b) => {
const recipientInfo = b.recipients.find(
(r) => r.educator.toString() === educatorId.toString()
);
const hashes = b.chunks.map((c) => c.hash).filter(Boolean);
return {
batchId: b.batchId,
date: b.updatedAt,
amount: recipientInfo?.amount || "0",
hashes,
explorerUrls: hashes.map((h) => getExplorerUrl(h)),
};
});
};
/**
* Recompute educator balances strictly from LedgerEntry records
* @returns {Promise<Object>}
*/
export const recalculateBalancesFromLedger = async () => {
const entries = await LedgerEntry.find().sort({ createdAt: 1 });
const storedBalances = await EducatorBalance.find();
const computed = {};
for (const entry of entries) {
const edId = entry.educator.toString();
if (!computed[edId]) {
computed[edId] = { owedStroops: 0n, settledStroops: 0n };
}
const amtStroops = BigInt(entry.amountStroops || "0");
if (entry.type === "sale") {
if (entry.settlement === "platform_collect") {
computed[edId].owedStroops += amtStroops;
} else {
computed[edId].settledStroops += amtStroops;
}
} else if (entry.type === "payout") {
computed[edId].owedStroops -= amtStroops;
computed[edId].settledStroops += amtStroops;
}
}
let isExact = true;
const auditReport = [];
for (const sb of storedBalances) {
const edId = sb.educator.toString();
const comp = computed[edId] || { owedStroops: 0n, settledStroops: 0n };
const storedOwed = BigInt(sb.owedStroops || "0");
const storedSettled = BigInt(sb.settledStroops || "0");
const matchOwed = storedOwed === comp.owedStroops;
const matchSettled = storedSettled === comp.settledStroops;
if (!matchOwed || !matchSettled) {
isExact = false;
}
auditReport.push({
educatorId: edId,
storedOwed: storedOwed.toString(),
computedOwed: comp.owedStroops.toString(),
storedSettled: storedSettled.toString(),
computedSettled: comp.settledStroops.toString(),
match: matchOwed && matchSettled,
});
}
return { isExact, auditReport };
};