From bc8c0e0040d1576bfcb0456f0e0d2a7f1fb407c4 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Thu, 19 Sep 2024 17:25:16 +0530 Subject: [PATCH] Fix bidder balance check for provider auction test --- src/auction.test.ts | 50 +++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/auction.test.ts b/src/auction.test.ts index 445cabe..b373db3 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -6,6 +6,7 @@ import { Registry, Account, createBid } from './index'; import { getConfig } from './testing/helper'; import { DENOM } from './constants'; import { Duration } from './proto/google/protobuf/duration'; +import { Coin } from '@cosmjs/proto-signing'; jest.setTimeout(30 * 60 * 1000); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); @@ -34,7 +35,7 @@ const createAuctionTests = () => { let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; - let bidAmounts: { type: string, quantity: string }[] = []; + let bidAmounts: Coin[] = []; beforeAll(async () => { registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); @@ -58,10 +59,7 @@ const createAuctionTests = () => { }); test('Create vickrey auction', async () => { - const minimumBid = { - denom: 'alnt', - amount: '1000000' - }; + const minimumBid = coin('1000000', DENOM); const auction = await registry.createAuction({ commitsDuration, @@ -79,16 +77,14 @@ const createAuctionTests = () => { test('Commit bids.', async () => { for (let i = 0; i < 3; i++) { - bidAmounts.push({ - type: DENOM, - quantity: (10000000 + (i * 500)).toString() - }); - bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].quantity}${bidAmounts[i].type}`); + bidAmounts.push(coin((10000000 + (i * 500)).toString(), DENOM)); + bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].amount}${bidAmounts[i].denom}`); await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); } const [auction] = await registry.getAuctionsByIds([auctionId]); expect(auction.status).toEqual('commit'); + expect(auction.bids.length).toEqual(3); auction.bids.forEach((bid: any) => { expect(bid.status).toEqual('commit'); }); @@ -160,7 +156,7 @@ const createProviderAuctionTests = () => { let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; - let bidAmounts: { type: string, quantity: string }[] = []; + let bidAmounts: Coin[] = []; beforeAll(async () => { registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); @@ -184,10 +180,7 @@ const createProviderAuctionTests = () => { }); test('Create provider auction', async () => { - const maxPrice = { - denom: 'alnt', - amount: '100000000' - }; + const maxPrice = coin('100000000', DENOM); const auction = await registry.createProviderAuction({ commitsDuration, @@ -206,16 +199,14 @@ const createProviderAuctionTests = () => { test('Commit bids.', async () => { for (let i = 0; i < 4; i++) { - bidAmounts.push({ - type: DENOM, - quantity: (100000 + (i * 500)).toString() - }); - bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].quantity}${bidAmounts[i].type}`); + bidAmounts.push(coin((100000 + (i * 500)).toString(), DENOM)); + bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].amount}${bidAmounts[i].denom}`); await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); } const [auction] = await registry.getAuctionsByIds([auctionId]); expect(auction.status).toEqual('commit'); + expect(auction.bids.length).toEqual(3); auction.bids.forEach((bid: any) => { expect(bid.status).toEqual('commit'); }); @@ -259,8 +250,9 @@ const createProviderAuctionTests = () => { const [auction] = await registry.getAuctionsByIds([auctionId]); expect(auction.status).toEqual('completed'); - const bidWinners = bidderAccounts.slice(0, 3); // Assuming top 3 are winners + const bidWinners = bidderAccounts.slice(0, 3); + // Check balances of auction winners for (let i = 0; i < bidWinners.length; i++) { const winner = bidWinners[i]; @@ -275,21 +267,24 @@ const createProviderAuctionTests = () => { const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10); const expectedBalance = bidderInitialBalance + winningBidAmount; + // The actual balance would be less than expected balance as fees would also get deducted expect(type).toBe(DENOM); - expect(parseInt(quantity)).toBeLessThan(expectedBalance); + expect(expectedBalance).toBeLessThan(parseInt(quantity)); } - for (const bidder of bidderAccounts) { + // Check balances of non-winners + for (let i = 3; i < bidderAccounts.length; i++) { + const bidder = bidderAccounts[i]; + const [bidderAccountObj] = await registry.getAccounts([bidder.address]); expect(bidderAccountObj).toBeDefined(); expect(bidderAccountObj.address).toBe(bidder.address); const [{ type, quantity }] = bidderAccountObj.balance; - const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10); - const expectedWinningBalance = bidderInitialBalance - winningBidAmount; + // The balance would be less than initial balance as fees would get deducted expect(type).toBe(DENOM); - expect(parseInt(quantity)).toBeLessThan(expectedWinningBalance); + expect(parseInt(quantity)).toBeLessThan(bidderInitialBalance); } const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]); @@ -299,8 +294,9 @@ const createProviderAuctionTests = () => { const [{ type, quantity }] = creatorAccountObj.balance; const totalWinningAmount = parseInt(auction.winnerPrice.quantity, 10) * bidWinners.length; - const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount; + const expectedCreatorBalance = creatorInitialBalance + totalWinningAmount; + // The balance would be less than expected balance expect(type).toBe(DENOM); expect(parseInt(quantity)).toBeLessThan(expectedCreatorBalance); });