130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
|
|
import { BlockchainIntegrationService } from '../../application/services/blockchain.service';
|
|
import { CurrentUser, CurrentUserPayload } from '../../shared/decorators/current-user.decorator';
|
|
import { AdminOnly } from '../../shared/guards/jwt-auth.guard';
|
|
import { AssetType, WithdrawStatus } from '@prisma/client';
|
|
import Decimal from 'decimal.js';
|
|
|
|
class CreateWithdrawRequestDto {
|
|
assetType: AssetType;
|
|
amount: string;
|
|
toAddress: string;
|
|
}
|
|
|
|
class BindAddressDto {
|
|
kavaAddress: string;
|
|
}
|
|
|
|
class CreateSwapRequestDto {
|
|
fromAsset: AssetType;
|
|
toAsset: AssetType;
|
|
fromAmount: string;
|
|
minToAmount: string;
|
|
}
|
|
|
|
@ApiTags('Blockchain')
|
|
@Controller('blockchain')
|
|
@ApiBearerAuth()
|
|
export class BlockchainController {
|
|
constructor(private readonly blockchainService: BlockchainIntegrationService) {}
|
|
|
|
// ==================== User Operations ====================
|
|
|
|
@Get('my/address')
|
|
@ApiOperation({ summary: '获取我的绑定地址' })
|
|
async getMyAddress(@CurrentUser() user: CurrentUserPayload) {
|
|
return this.blockchainService.getUserAddressBinding(user.accountSequence);
|
|
}
|
|
|
|
@Post('my/address')
|
|
@ApiOperation({ summary: '绑定KAVA地址' })
|
|
async bindAddress(
|
|
@CurrentUser() user: CurrentUserPayload,
|
|
@Body() dto: BindAddressDto,
|
|
) {
|
|
await this.blockchainService.bindUserAddress(user.accountSequence, dto.kavaAddress);
|
|
return { success: true, kavaAddress: dto.kavaAddress };
|
|
}
|
|
|
|
@Get('my/withdrawals')
|
|
@ApiOperation({ summary: '获取我的提现记录' })
|
|
@ApiQuery({ name: 'status', required: false })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'offset', required: false, type: Number })
|
|
async getMyWithdrawals(
|
|
@CurrentUser() user: CurrentUserPayload,
|
|
@Query('status') status?: WithdrawStatus,
|
|
@Query('limit') limit?: number,
|
|
@Query('offset') offset?: number,
|
|
) {
|
|
return this.blockchainService.getUserWithdrawRequests(user.accountSequence, {
|
|
status,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
});
|
|
}
|
|
|
|
@Post('my/withdraw')
|
|
@ApiOperation({ summary: '创建提现请求' })
|
|
async createWithdrawRequest(
|
|
@CurrentUser() user: CurrentUserPayload,
|
|
@Body() dto: CreateWithdrawRequestDto,
|
|
) {
|
|
return this.blockchainService.createWithdrawRequest(
|
|
user.accountSequence,
|
|
dto.assetType,
|
|
new Decimal(dto.amount),
|
|
dto.toAddress,
|
|
);
|
|
}
|
|
|
|
@Post('my/swap')
|
|
@ApiOperation({ summary: '创建DEX Swap请求' })
|
|
async createSwapRequest(
|
|
@CurrentUser() user: CurrentUserPayload,
|
|
@Body() dto: CreateSwapRequestDto,
|
|
) {
|
|
return this.blockchainService.createSwapRequest(
|
|
user.accountSequence,
|
|
dto.fromAsset,
|
|
dto.toAsset,
|
|
new Decimal(dto.fromAmount),
|
|
new Decimal(dto.minToAmount),
|
|
);
|
|
}
|
|
|
|
// ==================== Admin Operations ====================
|
|
|
|
@Get('withdrawals/:id')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '获取提现请求详情' })
|
|
async getWithdrawRequest(@Param('id') id: string) {
|
|
return this.blockchainService.getWithdrawRequest(id);
|
|
}
|
|
|
|
@Post('withdrawals/:id/approve')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '审批提现请求' })
|
|
async approveWithdraw(
|
|
@Param('id') id: string,
|
|
@CurrentUser() user: CurrentUserPayload,
|
|
) {
|
|
return this.blockchainService.approveWithdrawRequest(id, user.userId);
|
|
}
|
|
|
|
@Post('withdrawals/:id/execute')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '执行链上提现' })
|
|
async executeWithdraw(@Param('id') id: string) {
|
|
return this.blockchainService.executeWithdraw(id);
|
|
}
|
|
|
|
@Post('withdrawals/:id/confirm')
|
|
@AdminOnly()
|
|
@ApiOperation({ summary: '确认提现完成' })
|
|
async confirmWithdraw(@Param('id') id: string) {
|
|
return this.blockchainService.confirmWithdrawComplete(id);
|
|
}
|
|
}
|