diff --git a/chainhook-subscriber/src/models/cancel-bid.model.ts b/chainhook-subscriber/src/models/cancel-auction.model.ts similarity index 92% rename from chainhook-subscriber/src/models/cancel-bid.model.ts rename to chainhook-subscriber/src/models/cancel-auction.model.ts index dbc2197..2ff567f 100644 --- a/chainhook-subscriber/src/models/cancel-bid.model.ts +++ b/chainhook-subscriber/src/models/cancel-auction.model.ts @@ -1,7 +1,7 @@ import { Action } from "./action.model"; import { Highestbidder } from "./highest-bidder.model"; -export interface CancelBid { +export interface CancelAuction { action: Action; 'auction-id': Action; 'end-block': Action; diff --git a/chainhook-subscriber/src/modules/auctionhistory/services/cancel.auction.service.ts b/chainhook-subscriber/src/modules/auctionhistory/services/cancel.auction.service.ts index 9b5d20b..7722bbd 100644 --- a/chainhook-subscriber/src/modules/auctionhistory/services/cancel.auction.service.ts +++ b/chainhook-subscriber/src/modules/auctionhistory/services/cancel.auction.service.ts @@ -1,10 +1,11 @@ import { Inject, Injectable } from "@nestjs/common"; -import { StartAuction } from "src/models"; +import { CancelAuction, StartAuction } from "src/models"; import { AuctionEntityRepository } from "src/modules/auctions/repositories/auction.repository"; import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auction.repository.provider"; import { Transactional } from "src/common/transaction/transaction"; import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook"; import { AuctionEntity } from "src/modules/auctions/entities/auction.entity"; +import { AuctionStatus } from "src/modules/auctions/entities/auction.status"; @Injectable() export class CancelAuctionService { @@ -14,7 +15,7 @@ export class CancelAuctionService { ) {} @Transactional() - public async startAuction(startAuctionModel: StartAuction): Promise { + public async cancelAuction(cancelAuctionModel: CancelAuction): Promise { runOnTransactionRollback((cb) => console.log('Rollback error ' + cb.message), @@ -22,9 +23,12 @@ export class CancelAuctionService { runOnTransactionComplete((_) => console.log('Transaction Complete')); - const auction = await this.auctionRepository.findOneOrFail({auctionId: Number(startAuctionModel["auction-id"].value)}) - - auction.status = startAuctionModel["auction-status"].value; + const auction = await this.auctionRepository.findOneOrFail({ + where: { auctionId: Number(cancelAuctionModel["auction-id"].value)}, + relations: ['bids'], + }); + + auction.status = AuctionStatus.CANCELLED; await this.auctionRepository.save(auction); } diff --git a/chainhook-subscriber/src/modules/auctionhistory/services/place.bid.service.ts b/chainhook-subscriber/src/modules/auctionhistory/services/place.bid.service.ts new file mode 100644 index 0000000..5b8732f --- /dev/null +++ b/chainhook-subscriber/src/modules/auctionhistory/services/place.bid.service.ts @@ -0,0 +1,44 @@ +import { Inject, Injectable } from "@nestjs/common"; +import { PlaceBid, StartAuction } from "src/models"; +import { AuctionEntityRepository } from "src/modules/auctions/repositories/auction.repository"; +import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auction.repository.provider"; +import { Transactional } from "src/common/transaction/transaction"; +import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook"; +import { AuctionBidEntity } from "src/modules/auctions/entities/auction.bid.entity"; +import { AuctionBidEntityRepositoryToken } from "src/modules/auctions/providers/auction.bid.repository.provider"; +import { AuctionBidEntityRepository } from "src/modules/auctions/repositories/auction.bid.entity.repository"; + +@Injectable() +export class PlaceBidService { + constructor( + @Inject(AuctionEntityRepositoryToken) + private auctionRepository: AuctionEntityRepository, + @Inject(AuctionBidEntityRepositoryToken) + private auctionBidRepository: AuctionBidEntityRepository + ) {} + + @Transactional() + public async placeBid(placeBidModel: PlaceBid): Promise { + + runOnTransactionRollback((cb) => + console.log('Rollback error ' + cb.message), + ); + + runOnTransactionComplete((_) => console.log('Transaction Complete')); + + const auction = await this.auctionRepository.findOneOrFail({ + where: { auctionId: Number(placeBidModel["auction-id"].value)}, + relations: ['bids'], + }); + + const bid = new AuctionBidEntity(); + bid.amount = Number(placeBidModel.bid.value); + bid.bidder = placeBidModel.bidder.value; + + await this.auctionBidRepository.save(bid); + + (auction.bids || []).push(bid); + + await this.auctionRepository.save(auction); + } +} \ No newline at end of file diff --git a/chainhook-subscriber/src/modules/auctionhistory/services/start.auction.service.ts b/chainhook-subscriber/src/modules/auctionhistory/services/start.auction.service.ts index 8063218..e76e84b 100644 --- a/chainhook-subscriber/src/modules/auctionhistory/services/start.auction.service.ts +++ b/chainhook-subscriber/src/modules/auctionhistory/services/start.auction.service.ts @@ -5,6 +5,7 @@ import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auc import { Transactional } from "src/common/transaction/transaction"; import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook"; import { AuctionEntity } from "src/modules/auctions/entities/auction.entity"; +import { AuctionStatus } from "src/modules/auctions/entities/auction.status"; @Injectable() export class StartAuctionService { @@ -25,7 +26,7 @@ export class StartAuctionService { const auction = new AuctionEntity(); auction.auctionId = Number(startAuctionModel["auction-id"].value); - auction.status = startAuctionModel["auction-status"].value; + auction.status = AuctionStatus.OPEN; auction.endBlock = startAuctionModel["end-block"].value; auction.highestBid = startAuctionModel["highest-bid"].value; auction.highestBidder = startAuctionModel["highest-bidder"].value.value;