forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarTransactionService.js
More file actions
584 lines (501 loc) · 19.5 KB
/
Copy pathstellarTransactionService.js
File metadata and controls
584 lines (501 loc) · 19.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
const {
TransactionBuilder,
Operation,
Networks,
BASE_FEE,
Keypair,
} = require('stellar-sdk');
const { server: _serverModule } = require('../../blockchain/stellarService');
const { recordTransaction: _recordTransactionModule } = require('../db/transactionRepository');
const { getConfig, getRequiredConfig } = require('./configService');
const logger = require('../lib/logger');
// ---------------------------------------------------------------------------
// Dependency injection seam — override in tests via stellarTxService._deps
// ---------------------------------------------------------------------------
const _deps = {
server: _serverModule,
recordTransaction: _recordTransactionModule,
};
// Convenience accessors used throughout the module so that tests that inject
// via _deps automatically affect all internal calls.
function getServer() { return _deps.server; }
function getRecordTransaction() { return _deps.recordTransaction; }
// ---------------------------------------------------------------------------
// Network configuration — selected via STELLAR_NETWORK env var
// ---------------------------------------------------------------------------
const NETWORK_PASSPHRASE =
getConfig('STELLAR_NETWORK', 'testnet') === 'mainnet'
? Networks.PUBLIC
: Networks.TESTNET;
const DEFAULT_TIMEOUT = 180;
const FEE_BUMP_MULTIPLIER = 2;
const MAX_FEE_BUMP_ATTEMPTS = 3;
const STUCK_RESULT_CODES = [
'tx_bad_seq',
'tx_insufficient_fee',
'tx_too_late',
];
// ---------------------------------------------------------------------------
// Error helper
// ---------------------------------------------------------------------------
function createError(message, status, code) {
const err = new Error(message);
err.status = status;
err.code = code;
return err;
}
// ---------------------------------------------------------------------------
// Core: submit(operation, signers, options)
// ---------------------------------------------------------------------------
/**
* Builds, signs, and submits a Stellar transaction.
*
* Flow:
* 1. Fetch fresh sequence number from Horizon via loadAccount
* 2. Build transaction with the provided operation(s)
* 3. Sign with all provided signers
* 4. Submit to Horizon
* 5. If the transaction is stuck (bad_seq, insufficient_fee, too_late),
* automatically retry with a fee-bump transaction
* 6. Parse the result and store in DB
*
* @param {object} params
* @param {string} params.sourceAddress - Source account public key
* @param {import('stellar-sdk').xdr.Operation[]} params.operations - One or more Stellar operations
* @param {import('stellar-sdk').Keypair[]} params.signers - Keypairs to sign with
* @param {object} [params.options]
* @param {number} [params.options.timeout=180] - Transaction timeout in seconds
* @param {string} [params.options.memo] - Memo text
* @param {string} [params.options.feeSourceSecret] - Secret key for fee-bump fee source (if different)
* @param {object} [params.options.metadata] - Metadata to store alongside the DB record
* @param {string} [params.options.txType='transfer'] - Transaction type for DB classification
* @param {string} [params.options.amount] - Amount for DB record
* @param {string} [params.options.fromWallet] - From wallet for DB record
* @param {string} [params.options.toWallet] - To wallet for DB record
* @param {number} [params.options.merchantId] - Merchant ID for DB record
* @param {number} [params.options.campaignId] - Campaign ID for DB record
* @param {number} [params.options.userId] - User ID for DB record
* @returns {Promise<{ txHash: string, ledger: number, status: string, resultXdr: string }>}
*/
async function submit({ sourceAddress, operations, signers, options = {} }) {
if (!sourceAddress) {
throw createError('sourceAddress is required', 400, 'validation_error');
}
if (!operations || (Array.isArray(operations) && operations.length === 0)) {
throw createError('At least one operation is required', 400, 'validation_error');
}
if (!signers || (Array.isArray(signers) && signers.length === 0)) {
throw createError('At least one signer is required', 400, 'validation_error');
}
const ops = Array.isArray(operations) ? operations : [operations];
const signerList = Array.isArray(signers) ? signers : [signers];
// 1. Fetch fresh sequence number from Horizon
const account = await getServer().loadAccount(sourceAddress);
// 2. Build transaction
const timeout = options.timeout || DEFAULT_TIMEOUT;
let builder = new TransactionBuilder(account, {
fee: BASE_FEE,
networkPassphrase: NETWORK_PASSPHRASE,
});
for (const op of ops) {
builder = builder.addOperation(op);
}
if (options.memo) {
builder = builder.addMemo(require('stellar-sdk').Memo.text(options.memo));
}
builder = builder.setTimeout(timeout);
const transaction = builder.build();
// 3. Sign with all signers
for (const signer of signerList) {
transaction.sign(signer);
}
// 4. Submit with automatic fee-bump retry for stuck transactions
const result = await submitWithFeeBumpRetry(transaction, {
...options,
sourceAddress,
operations: ops,
signers: signerList,
});
// 5. Parse and store result in DB
await storeTransactionResult(result, options);
return result;
}
// ---------------------------------------------------------------------------
// Fee bump for stuck transactions
// ---------------------------------------------------------------------------
/**
* Submits a transaction, and if it's stuck (bad_seq, insufficient_fee, too_late),
* retries with a fee-bump transaction up to MAX_FEE_BUMP_ATTEMPTS times.
*
* On each fee-bump attempt the sequence number is re-fetched via loadAccount so
* that a stale sequence from a prior attempt never causes an infinite retry loop.
*
* @param {import('stellar-sdk').Transaction} transaction - Initial signed transaction
* @param {object} options
* @param {string} options.sourceAddress - Source account public key
* @param {import('stellar-sdk').xdr.Operation[]} options.operations
* @param {import('stellar-sdk').Keypair[]} options.signers
* @param {string} [options.feeSourceSecret]
* @param {number} [options.timeout]
* @param {string} [options.memo]
* @returns {Promise<{ txHash: string, ledger: number, status: string, resultXdr: string }>}
*/
async function submitWithFeeBumpRetry(transaction, options = {}) {
let lastError;
// ── First attempt: submit the original transaction ──────────────────────
// submitHorizonTransaction handles tx_bad_seq inline by refreshing the
// sequence number and re-submitting the inner transaction once. All other
// stuck codes (tx_insufficient_fee, tx_too_late) fall through to the
// fee-bump loop below.
try {
const horizonResult = await submitHorizonTransaction(transaction, {
options,
refreshSequenceAndRebuildOnce: async () => {
return refreshAndRebuildTransaction({
sourceAddress: options.sourceAddress,
operations: options.operations,
signers: options.signers,
memo: options.memo,
timeout: options.timeout,
});
},
});
return {
txHash: horizonResult.hash,
ledger: horizonResult.ledger,
status: 'submitted',
resultXdr: horizonResult.result_xdr,
_raw: horizonResult,
};
} catch (err) {
lastError = err;
const resultCodes = extractResultCodes(err);
// Also check err.code — submitHorizonTransaction wraps Horizon errors and
// puts the original Horizon code in err.code.
const allCodes = resultCodes.length > 0 ? resultCodes : (err.code ? [err.code] : []);
const isStuck = STUCK_RESULT_CODES.some((code) => allCodes.includes(code));
// Non-stuck error — propagate immediately without any fee-bump retry.
if (!isStuck) {
throw err;
}
}
// ── Fee-bump retry loop ──────────────────────────────────────────────────
// Each attempt:
// 1. Re-fetch the sequence number from Horizon (avoids stale-seq loops).
// 2. Rebuild + re-sign the inner transaction with the fresh sequence.
// 3. Wrap in a fee-bump with an exponentially increasing fee.
// 4. Submit the fee-bump.
//
// We iterate exactly MAX_FEE_BUMP_ATTEMPTS times (1-indexed for clarity).
const feeSourceSecret =
options.feeSourceSecret || getRequiredConfig('FEE_SOURCE_SECRET');
const feeSourceKeypair = Keypair.fromSecret(feeSourceSecret);
for (let attempt = 1; attempt <= MAX_FEE_BUMP_ATTEMPTS; attempt++) {
try {
// Step 1 & 2: always re-fetch sequence before each fee-bump attempt.
const freshInnerTx = await refreshAndRebuildTransaction({
sourceAddress: options.sourceAddress,
operations: options.operations,
signers: options.signers,
memo: options.memo,
timeout: options.timeout,
});
// Step 3: build fee-bump with doubled fee per attempt.
const bumpedFee = String(
parseInt(BASE_FEE, 10) * FEE_BUMP_MULTIPLIER * attempt,
);
const feeBumpTx = TransactionBuilder.buildFeeBumpTransaction(
feeSourceKeypair,
bumpedFee,
freshInnerTx,
NETWORK_PASSPHRASE,
);
feeBumpTx.sign(feeSourceKeypair);
// Step 4: submit.
const horizonResult = await getServer().submitTransaction(feeBumpTx);
return {
txHash: horizonResult.hash,
ledger: horizonResult.ledger,
status: 'submitted',
resultXdr: horizonResult.result_xdr,
_raw: horizonResult,
};
} catch (err) {
lastError = err;
const resultCodes = extractResultCodes(err);
const allCodes = resultCodes.length > 0 ? resultCodes : (err.code ? [err.code] : []);
const isStuck = STUCK_RESULT_CODES.some((code) => allCodes.includes(code));
logger.warn(
`[stellarTransactionService] Fee-bump attempt ${attempt}/${MAX_FEE_BUMP_ATTEMPTS} failed`,
{ codes: allCodes, isStuck },
);
// If this error is not a stuck code, stop retrying immediately.
if (!isStuck) {
break;
}
// If we've exhausted all attempts, fall through to the throw below.
}
}
// All attempts exhausted — throw a typed, catchable error.
const resultCodes = extractResultCodes(lastError);
const allLastCodes = resultCodes.length > 0 ? resultCodes : (lastError.code ? [lastError.code] : []);
throw createError(
`Transaction submission failed after ${MAX_FEE_BUMP_ATTEMPTS} fee-bump attempts: ${
allLastCodes.join(', ') || lastError.message
}`,
400,
'tx_submission_failed',
);
}
/**
* Submits a fee-bump transaction to Horizon.
*
* @param {import('stellar-sdk').FeeBumpTransaction} feeBumpTx
* @returns {Promise<{ txHash: string, ledger: number, status: string, resultXdr: string }>}
*/
async function submitFeeBumpTransaction(feeBumpTx) {
try {
const horizonResult = await getServer().submitTransaction(feeBumpTx);
return {
txHash: horizonResult.hash,
ledger: horizonResult.ledger,
status: 'submitted',
resultXdr: horizonResult.result_xdr,
_raw: horizonResult,
};
} catch (err) {
const resultCodes = extractResultCodes(err);
throw createError(
`Fee-bump submission failed: ${resultCodes.join(', ') || err.message}`,
400,
'tx_fee_bump_failed',
);
}
}
/**
* Explicitly submits a fee-bump for a previously submitted stuck transaction.
*
* @param {object} params
* @param {string} params.innerTxXDR - The XDR of the original (stuck) transaction
* @param {string} params.feeSourceSecret - Secret key of the account paying the fee
* @param {string} [params.baseFee] - Base fee for the fee-bump (default: 2x current fee)
* @returns {Promise<{ txHash: string, ledger: number, status: string, resultXdr: string }>}
*/
async function submitFeeBump({ innerTxXDR, feeSourceSecret, baseFee }) {
if (!innerTxXDR) {
throw createError('innerTxXDR is required', 400, 'validation_error');
}
if (!feeSourceSecret) {
throw createError('feeSourceSecret is required', 400, 'validation_error');
}
const feeSourceKeypair = Keypair.fromSecret(feeSourceSecret);
const innerTx = TransactionBuilder.fromXDR(innerTxXDR, NETWORK_PASSPHRASE);
const effectiveBaseFee = baseFee || String(parseInt(BASE_FEE, 10) * FEE_BUMP_MULTIPLIER);
const feeBumpTx = TransactionBuilder.buildFeeBumpTransaction(
feeSourceKeypair,
effectiveBaseFee,
innerTx,
NETWORK_PASSPHRASE,
);
feeBumpTx.sign(feeSourceKeypair);
return submitFeeBumpTransaction(feeBumpTx);
}
// ---------------------------------------------------------------------------
// Result parsing and DB storage
// ---------------------------------------------------------------------------
/**
* Extracts Horizon result codes from a submission error.
*
* @param {Error} err
* @returns {string[]}
*/
function extractResultCodes(err) {
try {
const extras = err?.response?.data?.extras;
if (!extras) return [];
const codes = extras.result_codes || {};
const allCodes = [];
if (Array.isArray(codes.transaction)) allCodes.push(...codes.transaction);
if (Array.isArray(codes.operations)) allCodes.push(...codes.operations);
// Some Horizon variants may return a flat transaction error code.
if (typeof extras.result_code === 'string') allCodes.push(extras.result_code);
return allCodes;
} catch {
return [];
}
}
function extractHorizonResponseBody(err) {
try {
return err?.response?.data ?? null;
} catch {
return null;
}
}
function isHorizonTimeout(err) {
const msg = `${err?.message || ''}`.toLowerCase();
const code = `${err?.code || ''}`.toLowerCase();
return (
msg.includes('timeout') ||
msg.includes('timed out') ||
code.includes('etimedout') ||
code.includes('timeout')
);
}
function isInsufficientBalance(err) {
const codes = extractResultCodes(err);
return codes.some((c) => c === 'insufficient_balance' || c === 'tx_insufficient_balance');
}
function findPrimaryHorizonCode(err) {
const codes = extractResultCodes(err);
if (codes?.length) return codes[0];
// Fallback to extras/result codes string if available
return err?.response?.data?.extras?.result_codes?.transaction?.[0] || err?.response?.data?.title || err?.response?.data?.type || 'horizon_error';
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function refreshAndRebuildTransaction({ sourceAddress, operations, signers, memo, timeout }) {
if (!sourceAddress || !operations || !signers) return null;
const account = await getServer().loadAccount(sourceAddress);
let builder = new TransactionBuilder(account, {
fee: BASE_FEE,
networkPassphrase: NETWORK_PASSPHRASE,
});
const ops = Array.isArray(operations) ? operations : [operations];
for (const op of ops) {
builder = builder.addOperation(op);
}
if (memo) {
builder = builder.addMemo(require('stellar-sdk').Memo.text(memo));
}
builder = builder.setTimeout(timeout || DEFAULT_TIMEOUT);
const tx = builder.build();
const signerList = Array.isArray(signers) ? signers : [signers];
for (const signer of signerList) {
tx.sign(signer);
}
return tx;
}
async function submitHorizonTransaction(transaction, { refreshSequenceAndRebuildOnce }) {
const maxTimeoutRetries = 3;
let timeoutAttempt = 0;
// Guard: only refresh sequence once per submitHorizonTransaction invocation.
let didSequenceRefresh = false;
// Attempt loop only for timeout errors and a single tx_bad_seq refresh.
while (true) {
try {
return await getServer().submitTransaction(transaction);
} catch (err) {
const horizonBody = extractHorizonResponseBody(err);
logger.error('[stellarTransactionService] Horizon submission error', {
message: err?.message,
code: findPrimaryHorizonCode(err),
horizonBody,
});
const codes = extractResultCodes(err);
// tx_bad_seq => refresh sequence number and retry once.
if (
codes.includes('tx_bad_seq') &&
!didSequenceRefresh &&
typeof refreshSequenceAndRebuildOnce === 'function'
) {
const rebuiltTx = await refreshSequenceAndRebuildOnce();
if (rebuiltTx) {
didSequenceRefresh = true;
transaction = rebuiltTx;
continue;
}
}
// Acceptance criteria: insufficient_balance => map to 400.
if (isInsufficientBalance(err)) {
const code = findPrimaryHorizonCode(err);
const e = createError('Insufficient balance', 400, code);
e.horizonBody = horizonBody;
throw e;
}
// Acceptance criteria: timeout => retry up to 3 times with exponential backoff.
if (isHorizonTimeout(err) && timeoutAttempt < maxTimeoutRetries - 1) {
const backoffMs = Math.pow(2, timeoutAttempt) * 500; // 0.5s, 1s, 2s
timeoutAttempt += 1;
await sleep(backoffMs);
continue;
}
// Acceptance criteria: all other Horizon errors => 503 with original error code.
const originalCode = findPrimaryHorizonCode(err);
const e = createError('Horizon error', 503, originalCode);
e.horizonBody = horizonBody;
throw e;
}
}
}
/**
* Parses a successful Horizon submission result into a structured object.
*
* @param {object} horizonResult - Raw Horizon response
* @returns {{ txHash: string, ledger: number, status: string, resultXdr: string, successful: boolean }}
*/
function parseTransactionResult(horizonResult) {
return {
txHash: horizonResult.hash,
ledger: horizonResult.ledger,
status: horizonResult.successful ? 'completed' : 'failed',
resultXdr: horizonResult.result_xdr || null,
successful: horizonResult.successful,
};
}
/**
* Stores the transaction result in the database.
*
* @param {{ txHash: string, ledger: number, status: string, resultXdr: string }} result
* @param {object} options
*/
async function storeTransactionResult(result, options = {}) {
try {
await getRecordTransaction()({
txHash: result.txHash,
txType: options.txType || 'transfer',
amount: options.amount || '0',
fromWallet: options.fromWallet || null,
toWallet: options.toWallet || null,
merchantId: options.merchantId || null,
campaignId: options.campaignId || null,
userId: options.userId || null,
stellarLedger: result.ledger,
status: result.status === 'submitted' ? 'completed' : result.status,
metadata: {
...options.metadata,
resultXdr: result.resultXdr,
},
});
} catch (dbErr) {
// Log but don't fail the response — the tx was already submitted on-chain
logger.error('[stellarTransactionService] Failed to store transaction result:', dbErr.message);
}
}
// ---------------------------------------------------------------------------
// Fetch current sequence number (exposed for external consumers)
// ---------------------------------------------------------------------------
/**
* Fetches the current sequence number for a Stellar account from Horizon.
*
* @param {string} publicKey - Stellar public key
* @returns {Promise<string>} Sequence number as a string
*/
async function getSequenceNumber(publicKey) {
const account = await getServer().loadAccount(publicKey);
return account.sequence;
}
module.exports = {
submit,
submitFeeBump,
submitFeeBumpTransaction,
parseTransactionResult,
extractResultCodes,
getSequenceNumber,
storeTransactionResult,
NETWORK_PASSPHRASE,
STUCK_RESULT_CODES,
FEE_BUMP_MULTIPLIER,
MAX_FEE_BUMP_ATTEMPTS,
_deps,
};