-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrealunit.controller.ts
More file actions
458 lines (418 loc) · 18.1 KB
/
realunit.controller.ts
File metadata and controls
458 lines (418 loc) · 18.1 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
import {
BadRequestException,
Body,
Controller,
Get,
HttpStatus,
Param,
Post,
Put,
Query,
Res,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiAcceptedResponse,
ApiBadRequestResponse,
ApiBearerAuth,
ApiConflictResponse,
ApiExcludeEndpoint,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { Response } from 'express';
import { Config, Environment } from 'src/config/config';
import {
BrokerbotBuyPriceDto,
BrokerbotInfoDto,
BrokerbotPriceDto,
BrokerbotSharesDto,
} from 'src/integration/blockchain/realunit/dto/realunit-broker.dto';
import { Blockchain } from 'src/integration/blockchain/shared/enums/blockchain.enum';
import { GetJwt } from 'src/shared/auth/get-jwt.decorator';
import { IpGuard } from 'src/shared/auth/ip.guard';
import { JwtPayload } from 'src/shared/auth/jwt-payload.interface';
import { RoleGuard } from 'src/shared/auth/role.guard';
import { UserActiveGuard } from 'src/shared/auth/user-active.guard';
import { UserRole } from 'src/shared/auth/user-role.enum';
import { PdfBrand } from 'src/shared/utils/pdf.util';
import { PdfDto } from 'src/subdomains/core/buy-crypto/routes/buy/dto/pdf.dto';
import { UserService } from 'src/subdomains/generic/user/models/user/user.service';
import { BalancePdfService } from '../../balance/services/balance-pdf.service';
import { TxStatementType } from '../../payment/dto/transaction-helper/tx-statement-details.dto';
import { SwissQRService } from '../../payment/services/swiss-qr.service';
import { TransactionHelper } from '../../payment/services/transaction-helper';
import { RealUnitAdminQueryDto, RealUnitQuoteDto, RealUnitTransactionDto } from '../dto/realunit-admin.dto';
import {
RealUnitBalancePdfDto,
RealUnitMultiReceiptPdfDto,
RealUnitSingleReceiptPdfDto,
} from '../dto/realunit-pdf.dto';
import {
RealUnitEmailRegistrationDto,
RealUnitEmailRegistrationResponseDto,
RealUnitRegistrationDto,
RealUnitRegistrationResponseDto,
RealUnitRegistrationStatus,
} from '../dto/realunit-registration.dto';
import { RealUnitSellConfirmDto, RealUnitSellDto, RealUnitSellPaymentInfoDto } from '../dto/realunit-sell.dto';
import {
AccountHistoryDto,
AccountHistoryQueryDto,
AccountSummaryDto,
HistoricalPriceDto,
HistoricalPriceQueryDto,
HoldersDto,
HoldersQueryDto,
RealUnitBuyDto,
RealUnitPaymentInfoDto,
TimeFrame,
TokenInfoDto,
} from '../dto/realunit.dto';
import { RealUnitService } from '../realunit.service';
@ApiTags('Realunit')
@Controller('realunit')
export class RealUnitController {
constructor(
private readonly realunitService: RealUnitService,
private readonly balancePdfService: BalancePdfService,
private readonly userService: UserService,
private readonly transactionHelper: TransactionHelper,
private readonly swissQrService: SwissQRService,
) {}
@Get('account/:address')
@ApiOperation({
summary: 'Get account information',
description: 'Retrieves account information for a specific address on the Realunit protocol',
})
@ApiOkResponse({ type: AccountSummaryDto })
@ApiParam({ name: 'address', type: String })
async getAccountSummary(@Param('address') address: string): Promise<AccountSummaryDto> {
return this.realunitService.getAccount(address);
}
@Get('account/:address/history')
@ApiOperation({
summary: 'Get account history',
description: 'Retrieves a paginated transaction history for a specific address on the Realunit protocol',
})
@ApiOkResponse({ type: AccountHistoryDto })
@ApiParam({
name: 'address',
description: 'The wallet address to query',
})
async getAccountHistory(
@Param('address') address: string,
@Query() { first, after }: AccountHistoryQueryDto,
): Promise<AccountHistoryDto> {
return this.realunitService.getAccountHistory(address, first, after);
}
@Get('holders')
@ApiOperation({
summary: 'Get token holders',
description: 'Retrieves a paginated list of token holders on the Realunit protocol',
})
@ApiOkResponse({ type: HoldersDto })
async getHolders(@Query() { first, before, after }: HoldersQueryDto): Promise<HoldersDto> {
return this.realunitService.getHolders(first, before, after);
}
@Get('price/history')
@ApiOperation({
summary: 'Get historical prices',
description: 'Retrieves the historical prices of RealUnit token in multiple currencies (CHF, EUR, USD)',
})
@ApiOkResponse({ type: [HistoricalPriceDto] })
async getHistoricalPrice(@Query() { timeFrame }: HistoricalPriceQueryDto): Promise<HistoricalPriceDto[]> {
return this.realunitService.getHistoricalPrice(timeFrame ?? TimeFrame.WEEK);
}
@Get('price')
@ApiOperation({
summary: 'Get RealUnit price',
description: 'Retrieves the current price of RealUnit on the Realunit protocol',
})
@ApiOkResponse({ type: HistoricalPriceDto })
async getRealUnitPrice(): Promise<HistoricalPriceDto> {
return this.realunitService.getRealUnitPrice();
}
@Get('tokenInfo')
@ApiOperation({
summary: 'Get token info',
description: 'Retrieves the information of the RealUnit token',
})
@ApiOkResponse({ type: TokenInfoDto })
async getTokenInfo(): Promise<TokenInfoDto> {
return this.realunitService.getRealUnitInfo();
}
// --- Balance PDF Endpoint ---
@Post('balance/pdf')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
summary: 'Get balance report PDF',
description: 'Generates a PDF balance report for a specific address on Ethereum blockchain',
})
@ApiOkResponse({ type: PdfDto, description: 'Balance PDF report (base64 encoded)' })
async getBalancePdf(@Body() dto: RealUnitBalancePdfDto): Promise<PdfDto> {
const tokenBlockchain = [Environment.DEV, Environment.LOC].includes(Config.environment)
? Blockchain.SEPOLIA
: Blockchain.ETHEREUM;
const pdfData = await this.balancePdfService.generateBalancePdf(
{ ...dto, blockchain: tokenBlockchain },
PdfBrand.REALUNIT,
);
return { pdfData };
}
// --- Receipt PDF Endpoint ---
@Post('transactions/receipt/single')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
description: 'Generates a PDF receipt for a completed RealUnit transaction',
})
@ApiParam({ name: 'id', description: 'Transaction ID' })
@ApiOkResponse({ type: PdfDto, description: 'Receipt PDF (base64 encoded)' })
@ApiBadRequestResponse({ description: 'Transaction not found or not a RealUnit transaction' })
async generateReceipt(@GetJwt() jwt: JwtPayload, @Body() dto: RealUnitSingleReceiptPdfDto): Promise<PdfDto> {
const user = await this.userService.getUser(jwt.user, { userData: true });
const txStatementDetails = await this.transactionHelper.getTxStatementDetails(
user.userData.id,
dto.transactionId,
TxStatementType.RECEIPT,
);
if (!Config.invoice.currencies.includes(txStatementDetails.currency)) {
throw new BadRequestException('PDF receipt is only available for CHF and EUR transactions');
}
return { pdfData: await this.swissQrService.createTxStatement(txStatementDetails, PdfBrand.REALUNIT) };
}
@Post('transactions/receipt/multi')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
description: 'Generates a single PDF receipt for multiple completed RealUnit transactions',
})
@ApiOkResponse({ type: PdfDto, description: 'Receipt PDF (base64 encoded)' })
@ApiBadRequestResponse({ description: 'Transaction not found, currency mismatch, or not a RealUnit transaction' })
async generateMultiReceipt(@GetJwt() jwt: JwtPayload, @Body() dto: RealUnitMultiReceiptPdfDto): Promise<PdfDto> {
const user = await this.userService.getUser(jwt.user, { userData: true });
const txStatementDetails = await this.transactionHelper.getTxStatementDetailsMulti(
user.userData.id,
dto.transactionIds,
TxStatementType.RECEIPT,
);
if (txStatementDetails.length > 0 && !Config.invoice.currencies.includes(txStatementDetails[0].currency)) {
throw new BadRequestException('PDF receipt is only available for CHF and EUR transactions');
}
return { pdfData: await this.swissQrService.createMultiTxStatement(txStatementDetails, PdfBrand.REALUNIT) };
}
// --- Brokerbot Endpoints ---
@Get('brokerbot/info')
@ApiOperation({
summary: 'Get Brokerbot info',
description: 'Retrieves general information about the REALU Brokerbot (addresses, settings)',
})
@ApiOkResponse({ type: BrokerbotInfoDto })
async getBrokerbotInfo(): Promise<BrokerbotInfoDto> {
return this.realunitService.getBrokerbotInfo();
}
@Get('brokerbot/price')
@ApiOperation({
summary: 'Get current Brokerbot price',
description: 'Retrieves the current price per REALU share from the Brokerbot smart contract',
})
@ApiOkResponse({ type: BrokerbotPriceDto })
async getBrokerbotPrice(): Promise<BrokerbotPriceDto> {
return this.realunitService.getBrokerbotPrice();
}
@Get('brokerbot/buyPrice')
@ApiOperation({
summary: 'Get buy price for shares',
description: 'Calculates the total cost to buy a specific number of REALU shares (includes price increment)',
})
@ApiQuery({ name: 'shares', type: Number, description: 'Number of shares to buy' })
@ApiOkResponse({ type: BrokerbotBuyPriceDto })
async getBrokerbotBuyPrice(@Query('shares') shares: number): Promise<BrokerbotBuyPriceDto> {
return this.realunitService.getBrokerbotBuyPrice(Number(shares));
}
@Get('brokerbot/shares')
@ApiOperation({
summary: 'Get shares for amount',
description: 'Calculates how many REALU shares can be purchased for a given CHF amount',
})
@ApiQuery({ name: 'amount', type: String, description: 'Amount in CHF (e.g., "1000.50")' })
@ApiOkResponse({ type: BrokerbotSharesDto })
async getBrokerbotShares(@Query('amount') amount: string): Promise<BrokerbotSharesDto> {
return this.realunitService.getBrokerbotShares(amount);
}
// --- Buy Payment Info Endpoint ---
@Put('buy')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
summary: 'Get payment info for RealUnit buy',
description:
'Returns personal IBAN and payment details for purchasing REALU tokens. Requires KYC Level 30 and RealUnit registration.',
})
@ApiOkResponse({ type: RealUnitPaymentInfoDto })
@ApiBadRequestResponse({ description: 'KYC Level 30 required, registration missing, or address not on allowlist' })
async getPaymentInfo(@GetJwt() jwt: JwtPayload, @Body() dto: RealUnitBuyDto): Promise<RealUnitPaymentInfoDto> {
const user = await this.userService.getUser(jwt.user, { userData: { kycSteps: true, country: true } });
return this.realunitService.getPaymentInfo(user, dto);
}
@Put('buy/:id/confirm')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), IpGuard)
@ApiOkResponse()
async confirmBuy(@GetJwt() jwt: JwtPayload, @Param('id') id: string): Promise<void> {
await this.realunitService.confirmBuy(jwt.user, +id);
}
// --- Sell Payment Info Endpoints ---
@Put('sell')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
summary: 'Get payment info for RealUnit sell',
description:
'Returns EIP-7702 delegation data for gasless REALU transfer and fallback deposit info. Requires KYC Level 20 and RealUnit registration.',
})
@ApiOkResponse({ type: RealUnitSellPaymentInfoDto })
@ApiBadRequestResponse({ description: 'KYC Level 20 required or registration missing' })
async getSellPaymentInfo(
@GetJwt() jwt: JwtPayload,
@Body() dto: RealUnitSellDto,
): Promise<RealUnitSellPaymentInfoDto> {
const user = await this.userService.getUser(jwt.user, { userData: { kycSteps: true, country: true } });
return this.realunitService.getSellPaymentInfo(user, dto);
}
@Put('sell/:id/confirm')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
summary: 'Confirm RealUnit sell transaction',
description: 'Confirms the sell transaction with EIP-7702 signatures or manual transaction hash.',
})
@ApiParam({ name: 'id', description: 'Transaction request ID' })
@ApiOkResponse({ description: 'Transaction confirmed', schema: { properties: { txHash: { type: 'string' } } } })
@ApiBadRequestResponse({ description: 'Invalid transaction request or signatures' })
async confirmSell(
@GetJwt() jwt: JwtPayload,
@Param('id') id: string,
@Body() dto: RealUnitSellConfirmDto,
): Promise<{ txHash: string }> {
return this.realunitService.confirmSell(jwt.user, +id, dto);
}
// --- Registration Endpoints ---
@Get('register/status')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.USER), UserActiveGuard())
@ApiOperation({
summary: 'Check if wallet is registered for RealUnit',
description: 'Returns true if the connected wallet is registered for RealUnit, false otherwise',
})
@ApiOkResponse({ type: Boolean })
async isRegistered(@GetJwt() jwt: JwtPayload): Promise<boolean> {
const user = await this.userService.getUser(jwt.user, { userData: { kycSteps: true } });
return this.realunitService.hasRegistrationForWallet(user.userData, jwt.address);
}
@Post('register/email')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.ACCOUNT), UserActiveGuard())
@ApiOperation({
summary: 'Step 1: Register email for RealUnit',
description:
'First step of RealUnit registration. Checks if email exists in DFX system. If exists and merge is possible, sends merge confirmation email. Otherwise registers email and sets KYC Level 10.',
})
@ApiOkResponse({ type: RealUnitEmailRegistrationResponseDto })
@ApiBadRequestResponse({ description: 'Email does not match verified email' })
@ApiConflictResponse({ description: 'Account already exists and merge not possible' })
async registerEmail(
@GetJwt() jwt: JwtPayload,
@Body() dto: RealUnitEmailRegistrationDto,
): Promise<RealUnitEmailRegistrationResponseDto> {
const status = await this.realunitService.registerEmail(jwt.account, dto);
return { status };
}
@Post('register/complete')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.ACCOUNT), UserActiveGuard())
@ApiOperation({
summary: 'Step 2: Complete RealUnit registration',
description:
'Second step of RealUnit registration. Requires email registration to be completed. Validates personal data against DFX system and forwards to Aktionariat.',
})
@ApiOkResponse({ type: RealUnitRegistrationResponseDto })
@ApiAcceptedResponse({
type: RealUnitRegistrationResponseDto,
description: 'Registration accepted, manual review needed or forwarding to Aktionariat failed',
})
@ApiBadRequestResponse({
description: 'Invalid signature, wallet mismatch, email registration not completed, or data mismatch',
})
async completeRegistration(
@GetJwt() jwt: JwtPayload,
@Body() dto: RealUnitRegistrationDto,
@Res() res: Response,
): Promise<void> {
const status = await this.realunitService.completeRegistration(jwt.account, dto);
const response: RealUnitRegistrationResponseDto = {
status: status,
};
const statusCode = status === RealUnitRegistrationStatus.COMPLETED ? HttpStatus.CREATED : HttpStatus.ACCEPTED;
res.status(statusCode).json(response);
}
@Post('register')
@ApiBearerAuth()
@UseGuards(AuthGuard(), RoleGuard(UserRole.ACCOUNT), UserActiveGuard())
@ApiOperation({ summary: 'Register for RealUnit' })
@ApiOkResponse({ type: RealUnitRegistrationResponseDto, description: 'Registration completed successfully' })
@ApiAcceptedResponse({
type: RealUnitRegistrationResponseDto,
description: 'Registration accepted, pending manual review',
})
@ApiBadRequestResponse({ description: 'Invalid signature or wallet does not belong to user' })
async register(@GetJwt() jwt: JwtPayload, @Body() dto: RealUnitRegistrationDto, @Res() res: Response): Promise<void> {
const needsReview = await this.realunitService.register(jwt.account, dto);
const response: RealUnitRegistrationResponseDto = {
status: needsReview ? RealUnitRegistrationStatus.PENDING_REVIEW : RealUnitRegistrationStatus.COMPLETED,
};
res.status(needsReview ? HttpStatus.ACCEPTED : HttpStatus.OK).json(response);
}
// --- Admin Endpoints ---
@Get('admin/quotes')
@ApiBearerAuth()
@ApiExcludeEndpoint()
@ApiOperation({ summary: 'Get RealUnit quotes' })
@ApiOkResponse({ type: [RealUnitQuoteDto], description: 'List of open RealUnit requests (quotes)' })
@UseGuards(AuthGuard(), RoleGuard(UserRole.ADMIN), UserActiveGuard())
async getAdminQuotes(@Query() { limit, offset }: RealUnitAdminQueryDto): Promise<RealUnitQuoteDto[]> {
return this.realunitService.getAdminQuotes(limit, offset);
}
@Get('admin/transactions')
@ApiBearerAuth()
@ApiExcludeEndpoint()
@ApiOperation({ summary: 'Get RealUnit transactions' })
@ApiOkResponse({ type: [RealUnitTransactionDto], description: 'List of completed RealUnit transactions' })
@UseGuards(AuthGuard(), RoleGuard(UserRole.ADMIN), UserActiveGuard())
async getAdminTransactions(@Query() { limit, offset }: RealUnitAdminQueryDto): Promise<RealUnitTransactionDto[]> {
return this.realunitService.getAdminTransactions(limit, offset);
}
@Put('admin/quotes/:id/confirm-payment')
@ApiBearerAuth()
@ApiExcludeEndpoint()
@ApiOperation({ summary: 'Confirm payment received for a open RealUnit request (quote)' })
@ApiParam({ name: 'id', description: 'Transaction request ID' })
@ApiOkResponse({ description: 'Payment confirmed and shares allocated' })
@UseGuards(AuthGuard(), RoleGuard(UserRole.ADMIN), UserActiveGuard())
async confirmPaymentReceived(@Param('id') id: string): Promise<void> {
await this.realunitService.confirmPaymentReceived(+id);
}
@Put('admin/registration/:kycStepId/forward')
@ApiBearerAuth()
@ApiExcludeEndpoint()
@UseGuards(AuthGuard(), RoleGuard(UserRole.ADMIN), UserActiveGuard())
async forwardRegistration(@Param('kycStepId') kycStepId: string): Promise<void> {
await this.realunitService.forwardRegistrationToAktionariat(+kycStepId);
}
}