From 63e627caf64dccefd9b9b4c9ef062dd6c81af4cc Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Mon, 23 Sep 2024 16:39:59 +0530 Subject: [PATCH] Check exact bidder balances in provider auctions --- README.md | 2 +- src/auction.test.ts | 58 +++++++++++++++++++++++------------ src/authority-auction.test.ts | 12 ++++++-- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b63533f..59c8ca8 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Follow these steps to run the tests: - Run tests: ```bash - yarn test:auctions + yarn test:authority-auctions ``` - Run the tests for record and authority expiry diff --git a/src/auction.test.ts b/src/auction.test.ts index 242727d..6577338 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -6,6 +6,7 @@ jest.setTimeout(30 * 60 * 1000); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const duration = 60; +const txFees = 200000; const commitFee = '1000'; const revealFee = '1000'; @@ -115,10 +116,10 @@ const auctionTests = () => { const [{ type, quantity }] = bidderrAccountObj.balance; const actualBalance = parseInt(quantity); - const expectedBalance = bidderInitialBalance - parseInt(bidAmounts[i]) - 400000 - parseInt(commitFee) - parseInt(revealFee); - expect(type).toBe(DENOM); + const expectedBalance = bidderInitialBalance - parseInt(bidAmounts[i]) - (2 * txFees) - parseInt(commitFee) - parseInt(revealFee); expect(actualBalance).toBe(expectedBalance); + expect(type).toBe(DENOM); } }); @@ -147,14 +148,14 @@ const auctionTests = () => { const [winnerAccountObj] = await registry.getAccounts([highestBidder.address]); expect(winnerAccountObj).toBeDefined(); - // Balance should be less than bidder's initial balance - winning price as tx fees also gets deducted const [{ type, quantity }] = winnerAccountObj.balance; + const actualBalance = parseInt(quantity); + + expect(actualBalance).toBe(bidderInitialBalance - winningPriceAmount - (2 * txFees) - parseInt(commitFee)); expect(type).toBe(DENOM); - expect(parseInt(quantity)).toBe(bidderInitialBalance - winningPriceAmount - 400000 - parseInt(commitFee)); }); }; -// TODO: Check exact balances by subtracting the fees const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number) => { let registry: Registry; let auctionId: string; @@ -213,10 +214,10 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number const [{ type, quantity }] = creatorAccountObj.balance; const actualBalance = parseInt(quantity); - const expectedBalance = creatorInitialBalance - (maxPrice * numProviders) - 200000; - expect(type).toBe(DENOM); + const expectedBalance = creatorInitialBalance - (maxPrice * numProviders) - txFees; expect(actualBalance).toBe(expectedBalance); + expect(type).toBe(DENOM); }); test('Commit bids.', async () => { @@ -324,11 +325,9 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number const [{ type, quantity }] = winnerAccountObj.balance; const actualBalance = parseInt(quantity); - // Balance should be more than bidder's initial balance but - // less than initial balance + winning price as tx fees also get deducted + const expectedBidderBalance = bidderInitialBalance + winningBidAmount - (2 * txFees) - parseInt(commitFee); + expect(actualBalance).toBe(expectedBidderBalance); expect(type).toBe(DENOM); - expect(actualBalance).toBeGreaterThan(bidderInitialBalance); - expect(actualBalance).toBeLessThan(bidderInitialBalance + winningBidAmount); } // Check balances of non-winners @@ -339,26 +338,45 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number const [{ type, quantity }] = bidderAccountObj.balance; const actualBalance = parseInt(quantity); - // Balance should be less than the initial balance as fees would get deducted + let expectedBidderBalance: number; + // Remove 'alnt' from bidAmount for comparison + const sanitizedBidAmount = parseInt(bidder.bid.reveal.bidAmount.toString().replace(/\D/g, ''), 10); + if (sanitizedBidAmount < maxPrice) { + expectedBidderBalance = bidderInitialBalance - (2 * txFees) - parseInt(commitFee); + } else { + // Bid is invalid, reveal fees are not returned + expectedBidderBalance = bidderInitialBalance - (2 * txFees) - parseInt(commitFee) - parseInt(revealFee); + } + + expect(actualBalance).toBe(expectedBidderBalance); expect(type).toBe(DENOM); - expect(actualBalance).toBeLessThan(bidderInitialBalance); } 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; - - // Check whether the balance after deducting locked amount is less than the actual balance + const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount - txFees; + expect(actualBalance).toBe(expectedCreatorBalance); expect(type).toBe(DENOM); - expect(parseInt(quantity)).toBeLessThan(expectedCreatorBalance); }); }; +const providerAuctionTests = () => { + const bids1 = [10002000, 10009000, 10006000, 10004000]; + // Number of providers is less than number of bidders + describe('Auction with numProviders < numBidders', () => providerAuctionTestsWithBids(bids1, 3)); + // Number of providers is greater than number of bidders + describe('Auction numProviders > numBidders', () => providerAuctionTestsWithBids(bids1, 5)); + + // With bid greater than max price + const bids2 = [10002000, 10009000, 100060000, 10004000]; + describe('Auction with a bid greater than the max price', () => providerAuctionTestsWithBids(bids2, 5)); +}; + describe('Vickrey Auction', () => auctionTests()); -const randomBids = [10002000, 10009000, 10006000, 100040000]; -describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 3)); -describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 5)); +describe('Provider Auction', () => providerAuctionTests()); diff --git a/src/authority-auction.test.ts b/src/authority-auction.test.ts index 190347e..1f7f37e 100644 --- a/src/authority-auction.test.ts +++ b/src/authority-auction.test.ts @@ -5,6 +5,8 @@ import { DENOM } from './constants'; jest.setTimeout(30 * 60 * 1000); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); +const duration = 60; + const auctionTests = (numBidders = 3) => { let registry: Registry; @@ -65,7 +67,10 @@ const auctionTests = (numBidders = 3) => { }); test('Wait for reveal phase.', (done) => { - setTimeout(done, 60 * 1000); + const commitTime = duration * 1000; + const waitTime = commitTime + (6 * 1000); + + setTimeout(done, waitTime); }); test('Reveal bids.', async () => { @@ -87,7 +92,10 @@ const auctionTests = (numBidders = 3) => { }); test('Wait for auction completion.', (done) => { - setTimeout(done, 60 * 1000); + const revealTime = duration * 1000; + const waitTime = revealTime + (6 * 1000); + + setTimeout(done, waitTime); }); test('Check auction winner, authority owner and status.', async () => {