forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontractEventRepository.js
More file actions
164 lines (151 loc) · 3.91 KB
/
Copy pathcontractEventRepository.js
File metadata and controls
164 lines (151 loc) · 3.91 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
const { query } = require('./index');
/**
* Records a contract event for audit logging.
* Requirements: #182
*
* @param {object} params
* @param {string} params.contractId
* @param {string} params.eventType - 'mint' | 'claim' | 'stake' | 'unstake'
* @param {object} params.eventData
* @param {string} [params.transactionHash]
* @param {number} [params.ledgerSequence]
* @returns {Promise<object>} The inserted event row
*/
async function recordContractEvent({
contractId,
eventType,
eventData,
transactionHash,
ledgerSequence,
}) {
const result = await query(
`INSERT INTO contract_events
(contract_id, event_type, event_data, transaction_hash, ledger_sequence)
VALUES ($1, $2, $3, $4, $5)
RETURNING *`,
[contractId, eventType, JSON.stringify(eventData), transactionHash, ledgerSequence]
);
return result.rows[0];
}
/**
* Marks a contract event as processed.
* Requirements: #182
*
* @param {number} eventId
* @returns {Promise<object>}
*/
async function markEventProcessed(eventId) {
const result = await query(
`UPDATE contract_events
SET status = 'processed', processed_at = NOW()
WHERE id = $1
RETURNING *`,
[eventId]
);
return result.rows[0];
}
/**
* Marks a contract event as failed and increments retry count.
* Requirements: #182
*
* @param {number} eventId
* @param {string} errorMessage
* @returns {Promise<object>}
*/
async function markEventFailed(eventId, errorMessage) {
const result = await query(
`UPDATE contract_events
SET status = 'failed',
error_message = $2,
retry_count = retry_count + 1
WHERE id = $1
RETURNING *`,
[eventId, errorMessage]
);
return result.rows[0];
}
/**
* Gets pending contract events that need to be retried.
* Requirements: #182
*
* @param {number} maxRetries - Maximum retry count before giving up
* @returns {Promise<object[]>}
*/
async function getPendingEvents(maxRetries = 5) {
const result = await query(
`SELECT * FROM contract_events
WHERE status = 'pending'
OR (status = 'failed' AND retry_count < $1)
ORDER BY created_at ASC`,
[maxRetries]
);
return result.rows;
}
/**
* Gets contract events with pagination and filtering.
* Requirements: #182
*
* @param {object} params
* @param {string} [params.contractId]
* @param {string} [params.eventType]
* @param {number} params.page
* @param {number} params.limit
* @returns {Promise<{data: object[], total: number, page: number, limit: number}>}
*/
async function getContractEvents({ contractId, eventType, page = 1, limit = 20 }) {
const offset = (page - 1) * limit;
const conditions = [];
const params = [];
let paramIndex = 1;
if (contractId) {
conditions.push(`contract_id = $${paramIndex++}`);
params.push(contractId);
}
if (eventType) {
conditions.push(`event_type = $${paramIndex++}`);
params.push(eventType);
}
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
// Get total count
const countResult = await query(
`SELECT COUNT(*) as total FROM contract_events ${whereClause}`,
params
);
const total = parseInt(countResult.rows[0].total, 10);
// Get paginated data
const dataResult = await query(
`SELECT * FROM contract_events
${whereClause}
ORDER BY created_at DESC
LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
[...params, limit, offset]
);
return {
data: dataResult.rows,
total,
page,
limit,
};
}
/**
* Gets a contract event by ID.
* Requirements: #182
*
* @param {number} eventId
* @returns {Promise<object|null>}
*/
async function getContractEventById(eventId) {
const result = await query(
'SELECT * FROM contract_events WHERE id = $1',
[eventId]
);
return result.rows[0] || null;
}
module.exports = {
recordContractEvent,
markEventProcessed,
markEventFailed,
getPendingEvents,
getContractEvents,
getContractEventById,
};