forked from Emmy123222/Stellar-MicroPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturretsController.js
More file actions
269 lines (251 loc) · 8.28 KB
/
Copy pathturretsController.js
File metadata and controls
269 lines (251 loc) · 8.28 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
/**
* src/controllers/turretsController.js
* HTTP handlers for Turrets txFunctions deployment and monitoring.
*/
"use strict";
const turretsService = require("../services/turretsService");
/**
* @typedef {object} TxFunctionDeployment
* @property {string} id
* @property {string} ownerPublicKey
* @property {"dca"|"stop_loss"|"escrow_release"} type
* @property {"active"|"paused"|"completed"} status
* @property {object} config - Normalized txFunction config for the given type
* @property {string} deploymentHash
* @property {string} signedChallengeXDR
* @property {string} createdAt
* @property {number} createdAtMs
* @property {string} nextRunAt
* @property {string|null} lastExecutedAt
* @property {string|null} lastCheckedAt
* @property {number|null} lastObservedPriceUsd
* @property {string|null} lastError
*/
/**
* @typedef {object} ExecutionHistoryEntry
* @property {string} id
* @property {string} deploymentId
* @property {"created"|"executed"|"error"|"status"} status
* @property {string} message
* @property {object|null} result
* @property {string} createdAt
*/
/**
* @typedef {object} AuditLogEntry
* @property {string} id
* @property {string} action
* @property {string} actor
* @property {string} deploymentId
* @property {object} details
* @property {string} timestamp
*/
/**
* POST /api/turrets/challenge
* Build an unsigned challenge transaction the owner must sign to authorize deployment.
*
* @param {object} req - Express request
* @param {object} req.body
* @param {string} req.body.ownerPublicKey - Owner's Stellar public key (G...)
* @param {"dca"|"stop_loss"|"escrow_release"} req.body.type - txFunction type
* @param {object} req.body.config - Type-specific configuration (validated/normalized server-side)
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: { challengeXDR: string, deploymentHash: string,
* normalizedConfig: object, networkPassphrase: string } }`
*/
async function createChallenge(req, res, next) {
try {
const { ownerPublicKey, type, config } = req.body;
const data = await turretsService.createSigningChallenge({ ownerPublicKey, type, config });
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* POST /api/turrets/deploy
* Deploy a signed txFunction after verifying the owner's signature.
*
* @param {object} req - Express request
* @param {object} req.body
* @param {string} req.body.ownerPublicKey - Owner's Stellar public key (G...)
* @param {"dca"|"stop_loss"|"escrow_release"} req.body.type - txFunction type
* @param {object} req.body.config - Type-specific configuration
* @param {string} req.body.deploymentHash - Hash returned by the challenge step
* @param {string} req.body.signedChallengeXDR - Challenge transaction signed by the owner
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} 201 JSON: `{ success: true, data: TxFunctionDeployment }`
*/
function deploy(req, res, next) {
try {
const { ownerPublicKey, type, config, deploymentHash, signedChallengeXDR } = req.body;
const data = turretsService.deployTxFunction({
ownerPublicKey,
type,
config,
deploymentHash,
signedChallengeXDR,
});
res.status(201).json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* GET /api/turrets?ownerPublicKey=<publicKey>
* List deployed txFunctions, optionally filtered by owner.
*
* @param {object} req - Express request
* @param {object} req.query
* @param {string} [req.query.ownerPublicKey] - Filter to deployments owned by this public key
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: TxFunctionDeployment[] }`
*/
function list(req, res, next) {
try {
const ownerPublicKey = req.query.ownerPublicKey;
const data = turretsService.listDeployments(ownerPublicKey);
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* GET /api/turrets/:id
* Get a single txFunction deployment by ID.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.id - Deployment ID
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: TxFunctionDeployment }`, or 404 (via next)
* when the deployment doesn't exist
*/
function getOne(req, res, next) {
try {
const { id } = req.params;
const data = turretsService.getDeployment(id);
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* GET /api/turrets/:id/history
* Get execution history for a txFunction deployment.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.id - Deployment ID
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: ExecutionHistoryEntry[] }`, or 404 (via next)
* when the deployment doesn't exist
*/
function getHistory(req, res, next) {
try {
const { id } = req.params;
const page = parseInt(req.query.page, 10) || 1;
const limit = parseInt(req.query.limit, 10) || 10;
turretsService.getDeployment(id);
const history = turretsService.getExecutionHistory(id);
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
const paginatedData = history.slice(startIndex, endIndex);
res.json({
success: true,
data: paginatedData,
pagination: {
total: history.length,
page,
limit,
pages: Math.ceil(history.length / limit)
}
});
} catch (err) {
next(err);
}
}
/**
* POST /api/turrets/:id/pause
* Pause an active txFunction deployment.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.id - Deployment ID
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: TxFunctionDeployment }`
*/
function pause(req, res, next) {
try {
const { id } = req.params;
const actor = req.user?.publicKey; // From JWT auth middleware
const data = turretsService.setDeploymentStatus(id, "paused", actor);
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* POST /api/turrets/:id/resume
* Resume a paused txFunction deployment.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.id - Deployment ID
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: TxFunctionDeployment }`
*/
function resume(req, res, next) {
try {
const { id } = req.params;
const actor = req.user?.publicKey; // From JWT auth middleware
const data = turretsService.setDeploymentStatus(id, "active", actor);
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
/**
* GET /api/turrets/audit-log
* Get audit log entries, optionally filtered.
*
* @param {object} req - Express request
* @param {object} req.query
* @param {string} [req.query.actor] - Filter by actor's Stellar public key
* @param {string} [req.query.deploymentId] - Filter by deployment ID
* @param {string} [req.query.action] - Filter by action name (e.g. "deploy", "paused")
* @param {string} [req.query.limit] - Max entries to return
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {void} JSON: `{ success: true, data: AuditLogEntry[] }`
*/
function getAuditLog(req, res, next) {
try {
const { actor, deploymentId, action, limit } = req.query;
const filters = {};
if (actor) filters.actor = actor;
if (deploymentId) filters.deploymentId = deploymentId;
if (action) filters.action = action;
if (limit) filters.limit = parseInt(limit, 10);
const data = turretsService.getAuditLog(filters);
res.json({ success: true, data });
} catch (err) {
next(err);
}
}
module.exports = {
createChallenge,
deploy,
list,
getOne,
getHistory,
pause,
resume,
getAuditLog,
};