Skip to content

Commit

Permalink
Merge pull request #5 from Ludium-Official/winnerBuy
Browse files Browse the repository at this point in the history
Winner buy
  • Loading branch information
mmingyeomm authored Oct 31, 2024
2 parents 4453d26 + e214a38 commit 8a6d162
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 136 deletions.
26 changes: 17 additions & 9 deletions backend/src/auction-log/auction-log.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Get, Post, HttpException, HttpStatus, Body, ValidationPipe, Param } from '@nestjs/common';
import { AuctionLogRepository } from './auction-log.repostiory';
import { ItemRepository } from '../item/item.repository';
import { AuctionLogDto } from './dto/auction-log.dto';
import { AuctionLogDto, AuctionLogverifyDto } from './dto/auction-log.dto';
import { AuctionLog } from './auction-log.entity';
import { AuctionLogService } from './auction-log.service';

Expand Down Expand Up @@ -48,15 +48,23 @@ constructor(
}
}

@Post('merklemap')
async getMerkleMap(@Body('itemId') itemId: number) {
const merkleMap = await this.auctionLogService.getMerkleMapForItem(itemId);
const serializedMap = await this.auctionLogService.serializeMerkleMap(merkleMap);

console.log(serializedMap.length)
@Post('merklemap')
async getMerkleMap(@Body('itemId') itemId: number) {
const merkleMap = await this.auctionLogService.getMerkleMapForItem(itemId);
const serializedMap = await this.auctionLogService.serializeMerkleMap(merkleMap);
console.log(serializedMap.length)

return serializedMap;
}
return serializedMap;
}

@Post('verify')
async verifyBid(@Body() AuctionLogverifyDto: AuctionLogverifyDto) {
console.log('auction-log / verify ')
return this.auctionLogService.verifyBid(AuctionLogverifyDto);
}



}

Expand Down
7 changes: 7 additions & 0 deletions backend/src/auction-log/auction-log.repostiory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export class AuctionLogRepository extends Repository<AuctionLog>{
return this.findOne({ where: { id } });
}

async findByItemIdAndKey(itemId: number, key: number): Promise<AuctionLog | null> {
return this.createQueryBuilder('auctionLog')
.leftJoinAndSelect('auctionLog.item', 'item')
.where('item.id = :itemId', { itemId })
.andWhere('auctionLog.key = :key', { key })
.getOne();
}



Expand Down
22 changes: 21 additions & 1 deletion backend/src/auction-log/auction-log.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Injectable, NotFoundException } from "@nestjs/common";
import { Field, MerkleMap, MerkleTree} from "o1js";
import { AuctionLogRepository } from "./auction-log.repostiory";
import {
Expand All @@ -7,6 +7,7 @@ import {
bigintFromBase64,
bigintToBase64,
} from "zkcloudworker";
import { AuctionLogverifyDto } from "./dto/auction-log.dto";

@Injectable()
export class AuctionLogService {
Expand Down Expand Up @@ -66,8 +67,27 @@ export class AuctionLogService {

return JSON.stringify(serialized);
}

async verifyBid(AuctionLogverifyDto: AuctionLogverifyDto) {
console.log(AuctionLogverifyDto.itemId)
console.log(AuctionLogverifyDto.key)

const auctionLog = await this.auctionLogRepository.findByItemIdAndKey(AuctionLogverifyDto.itemId, AuctionLogverifyDto.key);

if (!auctionLog) {
throw new NotFoundException('No bid found with the provided key');
}

// Get the highest bid for this item to determine if this is the winning bid

return {
bidAmount: auctionLog.bidAmount,
bidTime: auctionLog.bidTime,
transactionHash: auctionLog.transactionHash,
bidUser: auctionLog.bidUser,
};
}



// Add other methods to interact with the Item contract as needed
Expand Down
7 changes: 7 additions & 0 deletions backend/src/auction-log/dto/auction-log.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export class AuctionLogDto {

transactionHash: string;
}


export class AuctionLogverifyDto{
itemId: number;

key: number;
}
Empty file.
3 changes: 3 additions & 0 deletions backend/src/auction-winner/auction-winner.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class AuctionWinner {
@PrimaryGeneratedColumn()
id: number;

@Column()
winningKey: number;

@Column('decimal', { precision: 10, scale: 2 })
winningBid: number;

Expand Down
1 change: 1 addition & 0 deletions backend/src/auction-winner/auction-winner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class AuctionWinnerService {
private async storeWinner(auction: Item, highestBid: AuctionLog) {
const winner = new AuctionWinner();
winner.auctionItem = auction;
winner.winningKey = highestBid.key;
winner.winningBid = highestBid.bidAmount;
winner.winnerAddress = highestBid.bidUser;
winner.winTime = new Date();
Expand Down
13 changes: 7 additions & 6 deletions backend/src/item/item.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ItemService } from './item.service';
import { MerkleMap } from 'o1js';
import { CreateItemDto } from './dto/create-item.dto';


@Controller('items')
export class ItemsController {
constructor(private readonly itemService: ItemService) {}
Expand Down Expand Up @@ -89,13 +90,13 @@ export class ItemsController {

@Get('/finished')
async getFinishedAuctions() {
try {
const finishedAuctions = await this.itemService.getFinishedAuctions();
return finishedAuctions;
} catch (error) {
throw new HttpException('Failed to fetch running auctions', HttpStatus.INTERNAL_SERVER_ERROR);
}
try {
const finishedAuctions = await this.itemService.getFinishedAuctions();
return finishedAuctions;
} catch (error) {
throw new HttpException('Failed to fetch finished auctions', HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Get('/:id')
async getAuctionDetails(@Param('id') id: number) {
Expand Down
7 changes: 6 additions & 1 deletion backend/src/item/item.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export class ItemRepository extends Repository<Item>{
super(Item, dataSource.createEntityManager());
}
async findById(id: number): Promise<Item | null> {
return this.findOne({ where: { id } });
return this.findOne({
where: { id },
relations: {
auctionWinner: true
}
});
}


Expand Down
11 changes: 5 additions & 6 deletions backend/src/item/item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export class ItemService {
}
}


async getRunningAuctions() {
const currentTime = new Date();
return this.itemRepository.find({
Expand All @@ -118,15 +117,15 @@ export class ItemService {
}
});
}

async getFinishedAuctions() {
const currentTime = new Date();
return this.itemRepository.find({
const currentDate = new Date();
return await this.itemRepository.find({
where: {
endTime: LessThan(currentTime)
endTime: LessThan(currentDate)
},
relations: ['auctionWinner'], // Include the winner relation
order: {
endTime: 'ASC'
endTime: 'DESC'
}
});
}
Expand Down
Loading

0 comments on commit 8a6d162

Please sign in to comment.