mirror of
https://github.com/Kevsl/crypto-exchange-api.git
synced 2025-07-09 06:00:12 +02:00
optimized trade
This commit is contained in:
parent
28e061722a
commit
93f86101b7
@ -1,5 +1,11 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
import {
|
||||||
|
IsCreditCard,
|
||||||
|
IsEmail,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsOptional,
|
||||||
|
IsString,
|
||||||
|
} from 'class-validator';
|
||||||
export class AuthRegisterDto {
|
export class AuthRegisterDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
type: String,
|
type: String,
|
||||||
|
@ -103,7 +103,7 @@ export class CryptoService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const newCryptoValue = crypto.value * 1.1;
|
const newCryptoValue = crypto.value * 1.1;
|
||||||
await this.prisma.crypto.update({
|
return this.prisma.crypto.update({
|
||||||
where: {
|
where: {
|
||||||
id: dto.id_crypto,
|
id: dto.id_crypto,
|
||||||
},
|
},
|
||||||
|
@ -1,48 +1,12 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsDecimal, IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
export class TradeDto {
|
export class TradeDto {
|
||||||
@ApiProperty({
|
|
||||||
type: String,
|
|
||||||
description: 'UUID of the user that hold tokens before trade',
|
|
||||||
example: '121212-DSDZ1-21212DJDZ-31313',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
id_giver: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
type: String,
|
|
||||||
description: 'UUID of the user that will receive tokens after trade',
|
|
||||||
example: '121212-DSDZ1-21212DJDZ-31313',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
id_receiver: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
type: String,
|
|
||||||
description: 'UUID of the crypto traded',
|
|
||||||
example: '121212-DSDZ1-21212DJDZ-31313',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
id_crypto: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
type: Number,
|
|
||||||
description: 'Amount of tokens traded ',
|
|
||||||
example: 2,
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsDecimal()
|
|
||||||
amount_traded: number;
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
type: String,
|
type: String,
|
||||||
description: 'Offer UUID ',
|
description: 'Offer UUID ',
|
||||||
example: '121212-DSDZ1-21212DJDZ-31313',
|
example: '121212-DSDZ1-21212DJDZ-31313',
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsDecimal()
|
@IsString()
|
||||||
id_offer: string;
|
id_offer: string;
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,24 @@ export class TradeService {
|
|||||||
async createTrade(userId: string, dto: TradeDto) {
|
async createTrade(userId: string, dto: TradeDto) {
|
||||||
await checkUserHasAccount(userId);
|
await checkUserHasAccount(userId);
|
||||||
|
|
||||||
|
const offer = await this.prisma.offer.findUnique({
|
||||||
|
where: {
|
||||||
|
id: dto.id_offer,
|
||||||
|
},
|
||||||
|
});
|
||||||
const crypto = await this.prisma.crypto.findFirst({
|
const crypto = await this.prisma.crypto.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: dto.id_crypto,
|
id: offer.id_crypto,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const buyer = await this.prisma.user.findFirst({
|
const buyer = await this.prisma.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: dto.id_receiver,
|
id: offer.id_user,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const price = crypto.value * dto.amount_traded;
|
|
||||||
|
const price = crypto.value * offer.amount;
|
||||||
if (buyer.dollarAvailables < price) {
|
if (buyer.dollarAvailables < price) {
|
||||||
throw new ForbiddenException(
|
throw new ForbiddenException(
|
||||||
`Acqueror ${buyer.pseudo} doesnt have enough money to make this trade`,
|
`Acqueror ${buyer.pseudo} doesnt have enough money to make this trade`,
|
||||||
@ -54,26 +61,26 @@ export class TradeService {
|
|||||||
|
|
||||||
const asset = await this.prisma.userHasCrypto.findFirst({
|
const asset = await this.prisma.userHasCrypto.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id_crypto: dto.id_crypto,
|
id_crypto: offer.id_crypto,
|
||||||
id_user: dto.id_giver,
|
id_user: offer.id_user,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!asset || asset.amount < dto.amount_traded) {
|
if (!asset || asset.amount < offer.amount) {
|
||||||
throw new ForbiddenException(`Seller doesnt have enough ${crypto.name} `);
|
throw new ForbiddenException(`Seller doesnt have enough ${crypto.name} `);
|
||||||
}
|
}
|
||||||
|
|
||||||
const trade = await this.prisma.trade.create({
|
const trade = await this.prisma.trade.create({
|
||||||
data: {
|
data: {
|
||||||
id_giver: dto.id_giver,
|
id_giver: offer.id_user,
|
||||||
id_receiver: dto.id_receiver,
|
id_receiver: userId,
|
||||||
id_crypto: dto.id_crypto,
|
id_crypto: offer.id_crypto,
|
||||||
amount_traded: dto.amount_traded,
|
amount_traded: offer.amount,
|
||||||
id_offer: dto.id_offer,
|
id_offer: offer.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const newBalanceGiver = (asset.amount -= dto.amount_traded);
|
const newBalanceGiver = (asset.amount -= offer.amount);
|
||||||
await this.prisma.userHasCrypto.update({
|
await this.prisma.userHasCrypto.update({
|
||||||
where: {
|
where: {
|
||||||
id: asset.id,
|
id: asset.id,
|
||||||
@ -85,21 +92,21 @@ export class TradeService {
|
|||||||
|
|
||||||
const receiverAssets = await this.prisma.userHasCrypto.findFirst({
|
const receiverAssets = await this.prisma.userHasCrypto.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id_user: dto.id_receiver,
|
id_user: userId,
|
||||||
id_crypto: dto.id_crypto,
|
id_crypto: offer.id_crypto,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!receiverAssets) {
|
if (!receiverAssets) {
|
||||||
await this.prisma.userHasCrypto.create({
|
await this.prisma.userHasCrypto.create({
|
||||||
data: {
|
data: {
|
||||||
id_user: dto.id_receiver,
|
id_user: userId,
|
||||||
id_crypto: dto.id_crypto,
|
id_crypto: offer.id_crypto,
|
||||||
amount: dto.amount_traded,
|
amount: offer.amount,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const newBalanceReceiver = receiverAssets.amount + dto.amount_traded;
|
const newBalanceReceiver = receiverAssets.amount + offer.amount;
|
||||||
await this.prisma.userHasCrypto.update({
|
await this.prisma.userHasCrypto.update({
|
||||||
where: {
|
where: {
|
||||||
id: receiverAssets.id,
|
id: receiverAssets.id,
|
||||||
@ -125,12 +132,17 @@ export class TradeService {
|
|||||||
|
|
||||||
await this.prisma.user.update({
|
await this.prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id: dto.id_receiver,
|
id: userId,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
dollarAvailables: prevAmount - price,
|
dollarAvailables: prevAmount - price,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
await this.prisma.offer.delete({
|
||||||
|
where: {
|
||||||
|
id: offer.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
return trade;
|
return trade;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user