From 60a2174a53656985a13f097b35f701696d2cd2c2 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Mon, 23 Sep 2024 18:05:27 +0530 Subject: [PATCH] Add test for checking balances after provider funds are released --- src/auction.test.ts | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/auction.test.ts b/src/auction.test.ts index 6577338..d3fcbd8 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -163,6 +163,7 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number let auctionCreatorAccount: { address: string, privateKey: string }; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; let sortedBidders: { address: string, privateKey: string, bid?: any }[] = []; + let filteredBidders: { address: string, privateKey: string, bid?: any }[] = []; let invalidBidderAddresses: string[] = []; const numBidders = bidAmounts.length; @@ -294,24 +295,47 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number setTimeout(done, waitTime); }); - test('Check auction winner, status, and all bidder balances.', async () => { + test('Check auction status, winners and creator balance.', async () => { const [auction] = await registry.getAuctionsByIds([auctionId]); expect(auction.status).toEqual('completed'); // Filter bidders to exclude bids exceeding maxPrice - const filteredBidders = sortedBidders.filter(bidder => { + filteredBidders = sortedBidders.filter(bidder => { return parseInt(bidder.bid.reveal.bidAmount) <= parseInt(`${maxPrice}${DENOM}`); }); const numWinners = Math.min(filteredBidders.length, numProviders); const winningBidders = filteredBidders.slice(0, numWinners); - const losingBidders = bidderAccounts.filter(bidder => !winningBidders.includes(bidder)); // Check winner price is equal to highest winning bid - const winningBidAmount = parseInt(auction.winnerPrice.quantity); const expectedWinningPrice = winningBidders[numWinners - 1].bid.reveal.bidAmount; expect(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`).toEqual(expectedWinningPrice); + const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]); + expect(creatorAccountObj).toBeDefined(); + + const [{ type, quantity }] = creatorAccountObj.balance; + const actualBalance = parseInt(quantity); + + // Check auction creator balance + const totalWinningAmount = parseInt(auction.winnerPrice.quantity) * winningBidders.length; + const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount - txFees; + expect(actualBalance).toBe(expectedCreatorBalance); + expect(type).toBe(DENOM); + }); + + test('Release funds and check provider balances.', async () => { + const [auction] = await registry.getAuctionsByIds([auctionId]); + + const numWinners = Math.min(filteredBidders.length, numProviders); + const winningBidders = filteredBidders.slice(0, numWinners); + const losingBidders = bidderAccounts.filter(bidder => !winningBidders.includes(bidder)); + + const winningBidAmount = parseInt(auction.winnerPrice.quantity); + + // Release funds + await registry.releaseFunds({ auctionId }, auctionCreatorAccount.privateKey, fee); + // Check balances of auction winners for (let i = 0; i < winningBidders.length; i++) { const bidWinner = winningBidders[i]; @@ -351,18 +375,6 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number expect(actualBalance).toBe(expectedBidderBalance); expect(type).toBe(DENOM); } - - const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]); - expect(creatorAccountObj).toBeDefined(); - - const [{ type, quantity }] = creatorAccountObj.balance; - const actualBalance = parseInt(quantity); - - // Check auction creator balance - const totalWinningAmount = parseInt(auction.winnerPrice.quantity) * winningBidders.length; - const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount - txFees; - expect(actualBalance).toBe(expectedCreatorBalance); - expect(type).toBe(DENOM); }); };