Skip to content

Commit

Permalink
feat: 매수 로직 구현
Browse files Browse the repository at this point in the history
Co-authored-by: 이승관 <[email protected]>
  • Loading branch information
SeongHyeon0409 and SeungGwan123 committed Nov 18, 2024
1 parent 74ee20f commit aca3e14
Show file tree
Hide file tree
Showing 16 changed files with 636 additions and 239 deletions.
2 changes: 2 additions & 0 deletions packages/server/common/upbit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export const UPBIT_WEBSOCKET_URL = 'wss://api.upbit.com/websocket/v1';
export const UPBIT_RESTAPI_URL = 'https://api.upbit.com/v1/market/all?is_details=true'
export const UPBIT_CURRENT_PRICE_URL = 'https://api.upbit.com/v1/ticker?'
export const UPBIT_CURRENT_ORDERBOOK_URL = 'https://api.upbit.com/v1/orderbook?'
export const UPBIT_IMAGE_URL = "https://static.upbit.com/logos/"

export const UPBIT_WEBSOCKET_CONNECTION_TIME = 3000
export const UPBIT_UPDATED_COIN_LIST_TIME = 86400000
export const UPBIT_UPDATED_COIN_INFO_TIME = 1000
export const UPBIT_UPDATED_ORDER_INFO_TIME = 1000

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/account/account.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
@Entity()
export class Account {
@PrimaryGeneratedColumn()
accountId: string;
id: string;

@Column('double')
KRW: number;
Expand Down
56 changes: 30 additions & 26 deletions packages/server/src/account/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@ import { User } from 'src/auth/user.entity';

@Injectable()
export class AccountRepository extends Repository<Account> {
constructor(private dataSource: DataSource) {
super(Account, dataSource.createEntityManager());
}
async createAccountForAdmin(adminUser: User) {
const account = new Account();
account.KRW = 300000000;
account.USDT = 300000;
account.BTC = 10;
account.user = adminUser;
await this.save(account);
console.log('admin 계정에 Account가 성공적으로 생성되었습니다.');
}
async getMyMoney(user, moneyType: string) {
const account = await this.findOne({
where: { user: { id: user.userId } },
});
constructor(private dataSource: DataSource) {
super(Account, dataSource.createEntityManager());
}
async createAccountForAdmin(adminUser: User) {
const account = new Account();
account.KRW = 300000000;
account.USDT = 300000;
account.BTC = 10;
account.user = adminUser;
await this.save(account);
console.log('admin 계정에 Account가 성공적으로 생성되었습니다.');
}
async getMyMoney(user, moneyType: string) {
const account = await this.findOne({
where: { user: { id: user.userId } },
});

if (!account[moneyType]) return 0;
return account[moneyType];
}
async successBuy(buyDto, accountBalance, accountId, queryRunner) {
const updateData = {
accountId: accountId,
[buyDto.typeGiven]: accountBalance,
};
if (!account[moneyType]) return 0;
return account[moneyType];
}
async updateAccountCurrency(typeGiven, accountBalance, accountId, queryRunner) {
try{
const updateData = {
id: accountId,
[typeGiven]: accountBalance,
};

await queryRunner.manager.save(Account, updateData);
}
await queryRunner.manager.save(Account, updateData);
}catch(error){
console.log(error)
}
}
}
2 changes: 1 addition & 1 deletion packages/server/src/asset/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Asset {
@Column('double')
price: number;

@Column()
@Column('double')
quantity: number;

@CreateDateColumn({ type: 'timestamp' })
Expand Down
26 changes: 22 additions & 4 deletions packages/server/src/asset/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ export class AssetRepository extends Repository<Asset> {
constructor(private dataSource: DataSource) {
super(Asset, dataSource.createEntityManager());
}
async createAsset(buyDto, currentCoinPrice, account, queryRunner) {
async createAsset(buyDto, price, quantity, queryRunner) {
try {
const { typeReceived, receivedAmount } = buyDto;
const { typeReceived, account } = buyDto;
const asset = new Asset();
asset.assetName = typeReceived;
asset.price = currentCoinPrice;
asset.quantity = receivedAmount;
asset.price = price;
asset.quantity = quantity;
asset.account = account;
await queryRunner.manager.save(Asset, asset);
} catch (e) {
console.log(e);
}
}

async updateAsset(asset, queryRunner) {
try {
await queryRunner.manager.save(Asset, asset);
} catch (e) {
console.log(e);
}
}
async getAsset(account, assetName){
try {
return await this.createQueryBuilder('asset')
.where('asset.assetName = :assetName', { assetName })
.andWhere('asset.account = :accountId', { accountId: account.id })
.getOne();
} catch (error) {
console.error('Error fetching asset:', error);
throw new Error('Failed to fetch asset');
}
}
}
2 changes: 1 addition & 1 deletion packages/server/src/trade-history/trade-history.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class TradeHistory {
@Column('double')
price: number;

@Column()
@Column('double')
quantity: number;

@CreateDateColumn({ type: 'timestamp' })
Expand Down
Loading

0 comments on commit aca3e14

Please sign in to comment.