Update provider auction test to accept unsorted bid amounts

This commit is contained in:
IshaVenikar 2024-09-20 14:09:15 +05:30
parent 5c3408279f
commit 570c9bdb3d

View File

@ -42,9 +42,8 @@ const auctionTests = () => {
await registry.sendCoins({ denom: DENOM, amount: creatorInitialBalance.toString(), destinationAddress: auctionCreator.address }, privateKey, fee);
auctionCreatorAccount = { address: auctionCreator.address, privateKey: auctionCreator.privateKey.toString('hex') };
});
test('Setup bidder accounts', async () => {
// Create bidder accounts
for (let i = 0; i < numBidders; i++) {
const mnenonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnenonic);
@ -159,20 +158,21 @@ const auctionTests = () => {
// Balance should be less than bidder's initial balance - winning price as tx fees also gets deducted
const [{ type, quantity }] = winnerAccountObj.balance;
expect(type).toBe(DENOM);
expect(parseInt(quantity)).toBeLessThan(bidderInitialBalance - winningPriceAmount);
expect(parseInt(quantity)).toBeLessThan(bidderInitialBalance - winningPriceAmount - 400000);
});
};
const providerAuctionTests = () => {
const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number) => {
let registry: Registry;
let auctionId: string;
let auctionCreatorAccount: { address: string, privateKey: string, bid?: any };
let auctionCreatorAccount: { address: string, privateKey: string };
let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = [];
const numBidders = 4;
const numProviders = 3;
let bidAmounts: Coin[] = [];
const bidAmounts: Coin[] = bidsAmount.map(amount => coin(amount.toString(), DENOM));
const numBidders = bidAmounts.length;
const maxPrice = coin((10 * lowestBidAmount).toString(), DENOM);
beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
@ -184,9 +184,8 @@ const providerAuctionTests = () => {
await registry.sendCoins({ denom: DENOM, amount: creatorInitialBalance.toString(), destinationAddress: auctionCreator.address }, privateKey, fee);
auctionCreatorAccount = { address: auctionCreator.address, privateKey: auctionCreator.privateKey.toString('hex') };
});
test('Setup bidder accounts', async () => {
// Create bidder accounts
for (let i = 0; i < numBidders; i++) {
const mnenonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnenonic);
@ -198,8 +197,6 @@ const providerAuctionTests = () => {
});
test('Create a provider auction', async () => {
const maxPrice = coin((10 * lowestBidAmount).toString(), DENOM);
const auction = await registry.createProviderAuction(
{
commitsDuration,
@ -225,12 +222,11 @@ const providerAuctionTests = () => {
const expectedBalance = creatorInitialBalance - (parseInt(maxPrice.amount) * numProviders);
expect(type).toBe(DENOM);
expect(actualBalance).toBeLessThan(expectedBalance);
expect(actualBalance).toBe(expectedBalance - 200000);
});
test('Commit bids.', async () => {
for (let i = 0; i < numBidders; i++) {
bidAmounts.push(coin((lowestBidAmount + (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);
}
@ -282,16 +278,20 @@ const providerAuctionTests = () => {
const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('completed');
const bidWinners = bidderAccounts.slice(0, numProviders);
const bidLosers = bidderAccounts.slice(numProviders, numBidders);
const sortedBidders = bidderAccounts.slice().sort((a, b) => {
return parseInt(a.bid.reveal.bidAmount) - parseInt(b.bid.reveal.bidAmount);
});
const winningBidders = sortedBidders.slice(0, numProviders);
const losingBidders = sortedBidders.slice(numProviders);
// Check winner price is equal to highest winning bid
const winningBidAmount = parseInt(auction.winnerPrice.quantity);
expect(auction.winnerPrice.quantity).toEqual(bidAmounts[numProviders - 1].amount);
// Check balances of auction winners
for (let i = 0; i < bidWinners.length; i++) {
const bidWinner = bidWinners[i];
for (let i = 0; i < winningBidders.length; i++) {
const bidWinner = winningBidders[i];
expect(auction.winnerAddresses[i]).toEqual(bidWinner.address);
expect(`${auction.winnerBids[i].quantity}${auction.winnerBids[i].type}`).toEqual(bidWinner.bid.reveal.bidAmount);
@ -303,22 +303,23 @@ const providerAuctionTests = () => {
const actualBalance = parseInt(quantity);
// Balance should be more than bidder's initial balance but
// less than initial balance + winning price as tx fees also gets deducted
// less than initial balance + winning price as tx fees also get deducted
expect(type).toBe(DENOM);
expect(actualBalance).toBeGreaterThan(bidderInitialBalance);
expect(actualBalance).toBeLessThan(bidderInitialBalance + winningBidAmount);
}
// Check balances of non-winners
for (const bidder of bidLosers) {
for (const bidder of losingBidders) {
const [bidderAccountObj] = await registry.getAccounts([bidder.address]);
expect(bidderAccountObj).toBeDefined();
const [{ type, quantity }] = bidderAccountObj.balance;
const actualBalance = parseInt(quantity);
// Balance should be less than initial balance as fees would get deducted
// Balance should be less than the initial balance as fees would get deducted
expect(type).toBe(DENOM);
expect(parseInt(quantity)).toBeLessThan(bidderInitialBalance);
expect(actualBalance).toBeLessThan(bidderInitialBalance);
}
const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]);
@ -326,18 +327,16 @@ const providerAuctionTests = () => {
const [{ type, quantity }] = creatorAccountObj.balance;
const totalWinningAmount = parseInt(auction.winnerPrice.quantity) * bidWinners.length;
const totalWinningAmount = parseInt(auction.winnerPrice.quantity) * winningBidders.length;
const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount;
// The balance would be less than expected balance
expect(type).toBe(DENOM);
expect(parseInt(quantity)).toBeLessThan(expectedCreatorBalance);
// Check whether the balance after deducting locked amount is less than the actual balance
const amountAfterDeduction = creatorInitialBalance - (parseInt(maxPrice.amount) * numProviders);
expect(parseInt(quantity)).toBeLessThan(amountAfterDeduction);
expect(type).toBe(DENOM);
expect(parseInt(quantity)).toBe(expectedCreatorBalance);
});
};
describe('Vickrey Auction', () => auctionTests());
describe('Provider Auction', () => providerAuctionTests());
const randomBids = [10002000, 10009000, 10006000, 10004000];
describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 3));
describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 5));