Skip to content

Commit

Permalink
fix: 매도/매수 로직 수정
Browse files Browse the repository at this point in the history
Co-authored-by: 이승관 <[email protected]>
  • Loading branch information
SeongHyeon0409 and SeungGwan123 committed Nov 29, 2024
1 parent 4c9e10d commit bb80cce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
53 changes: 27 additions & 26 deletions packages/server/src/account/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class AccountRepository extends Repository<Account> {

async updateAccountCurrency(
typeGiven: string,
accountBalance: number,
change: number,
accountId: number,
queryRunner: QueryRunner,
): Promise<void> {
Expand All @@ -60,7 +60,32 @@ export class AccountRepository extends Repository<Account> {
await queryRunner.manager
.createQueryBuilder()
.update(Account)
.set({ [typeGiven]: accountBalance })
.set({ [typeGiven]: `${typeGiven} + ${change}` })
.where('id = :id', { id: accountId })
.execute();

this.logger.log(`계정 통화 업데이트 완료: accountId=${accountId}`);
} catch (error) {
this.logger.error(
`계정 통화 업데이트 실패: ${error.message}`,
error.stack,
);
throw error;
}
}
async updateAccountAvailableCurrency(
change: number,
accountId: number,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(
`계정 통화 업데이트 시작: accountId=${accountId}, type=availableKRW`,
);
try {
await queryRunner.manager
.createQueryBuilder()
.update(Account)
.set({ availableKRW: () => `availableKRW + ${change}` })
.where('id = :id', { id: accountId })
.execute();

Expand Down Expand Up @@ -104,31 +129,7 @@ export class AccountRepository extends Repository<Account> {
return account.availableKRW;
}

async updateAccountAvailableCurrency(
change: number,
accountId: number,
queryRunner: QueryRunner,
): Promise<void> {
this.logger.log(
`계정 통화 업데이트 시작: accountId=${accountId}, type=availableKRW`,
);
try {
await queryRunner.manager
.createQueryBuilder()
.update(Account)
.set({ availableKRW: () => `availableKRW + ${change}` })
.where('id = :id', { id: accountId })
.execute();

this.logger.log(`계정 통화 업데이트 완료: accountId=${accountId}`);
} catch (error) {
this.logger.error(
`계정 통화 업데이트 실패: ${error.message}`,
error.stack,
);
throw error;
}
}

async updateAccountBTC(
id: number,
Expand Down
15 changes: 2 additions & 13 deletions packages/server/src/trade/trade-ask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,15 @@ export class AskService extends TradeAskBidService implements OnModuleInit {
);
}

const change = formatQuantity(
account[askDto.typeReceived] + buyData.price * buyData.quantity,
);
const change = formatQuantity(buyData.price * buyData.quantity);

await this.accountRepository.updateAccountCurrency(
askDto.typeReceived,
change,
account.id,
queryRunner,
);
const availableChange = formatQuantity(
account.availableKRW + buyData.price * buyData.quantity,
);

await this.accountRepository.updateAccountCurrency(
askDto.typeReceived,
change,
account.id,
queryRunner,
);
const availableChange = formatQuantity(buyData.price * buyData.quantity);

await this.accountRepository.updateAccountCurrency(
'availableKRW',
Expand Down
20 changes: 10 additions & 10 deletions packages/server/src/trade/trade-bid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
const userAccount = await this.accountRepository.validateUserAccount(
user.userId,
);
const accountBalance = await this.checkCurrencyBalance(
bidDto,
userAccount,
);
// const accountBalance = await this.checkCurrencyBalance(
// bidDto,
// userAccount,
// );
const { receivedPrice, receivedAmount } = bidDto;

await this.accountRepository.updateAccountCurrency(
'availableKRW',
accountBalance,
-formatQuantity(receivedPrice * receivedAmount),
userAccount.id,
queryRunner,
);
Expand Down Expand Up @@ -278,18 +279,17 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
(bidDto.receivedPrice - buyData.price) * buyData.quantity,
);

const returnChange = formatQuantity(
userAccount[typeGiven] - buyData.price * buyData.quantity,
);
const returnChange = formatQuantity(buyData.price * buyData.quantity);

await this.accountRepository.updateAccountCurrency(
typeGiven,
returnChange,
-returnChange,
account.id,
queryRunner,
);

await this.accountRepository.updateAccountAvailableCurrency(
await this.accountRepository.updateAccountCurrency(
'availableKRW',
change,
account.id,
queryRunner,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/trade/trade.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class TradeService {

private calculateAccountBalance(trade: any, userAccount: any): number {
return parseFloat(
(trade.price * trade.quantity + userAccount.availableKRW).toFixed(
(trade.price * trade.quantity).toFixed(
8,
),
);
Expand Down

0 comments on commit bb80cce

Please sign in to comment.