Fix bidder balance check for provider auction test

This commit is contained in:
IshaVenikar 2024-09-19 17:25:16 +05:30
parent e85c20f7ae
commit bc8c0e0040

View File

@ -6,6 +6,7 @@ import { Registry, Account, createBid } from './index';
import { getConfig } from './testing/helper'; import { getConfig } from './testing/helper';
import { DENOM } from './constants'; import { DENOM } from './constants';
import { Duration } from './proto/google/protobuf/duration'; import { Duration } from './proto/google/protobuf/duration';
import { Coin } from '@cosmjs/proto-signing';
jest.setTimeout(30 * 60 * 1000); jest.setTimeout(30 * 60 * 1000);
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
@ -34,7 +35,7 @@ const createAuctionTests = () => {
let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; let auctionCreatorAccount: { address: string, privateKey: string, bid?: any };
let bidderAccounts: { 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 () => { beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
@ -58,10 +59,7 @@ const createAuctionTests = () => {
}); });
test('Create vickrey auction', async () => { test('Create vickrey auction', async () => {
const minimumBid = { const minimumBid = coin('1000000', DENOM);
denom: 'alnt',
amount: '1000000'
};
const auction = await registry.createAuction({ const auction = await registry.createAuction({
commitsDuration, commitsDuration,
@ -79,16 +77,14 @@ const createAuctionTests = () => {
test('Commit bids.', async () => { test('Commit bids.', async () => {
for (let i = 0; i < 3; i++) { for (let i = 0; i < 3; i++) {
bidAmounts.push({ bidAmounts.push(coin((10000000 + (i * 500)).toString(), DENOM));
type: DENOM, bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].amount}${bidAmounts[i].denom}`);
quantity: (10000000 + (i * 500)).toString()
});
bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].quantity}${bidAmounts[i].type}`);
await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee);
} }
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('commit'); expect(auction.status).toEqual('commit');
expect(auction.bids.length).toEqual(3);
auction.bids.forEach((bid: any) => { auction.bids.forEach((bid: any) => {
expect(bid.status).toEqual('commit'); expect(bid.status).toEqual('commit');
}); });
@ -160,7 +156,7 @@ const createProviderAuctionTests = () => {
let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; let auctionCreatorAccount: { address: string, privateKey: string, bid?: any };
let bidderAccounts: { 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 () => { beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
@ -184,10 +180,7 @@ const createProviderAuctionTests = () => {
}); });
test('Create provider auction', async () => { test('Create provider auction', async () => {
const maxPrice = { const maxPrice = coin('100000000', DENOM);
denom: 'alnt',
amount: '100000000'
};
const auction = await registry.createProviderAuction({ const auction = await registry.createProviderAuction({
commitsDuration, commitsDuration,
@ -206,16 +199,14 @@ const createProviderAuctionTests = () => {
test('Commit bids.', async () => { test('Commit bids.', async () => {
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
bidAmounts.push({ bidAmounts.push(coin((100000 + (i * 500)).toString(), DENOM));
type: DENOM, bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].amount}${bidAmounts[i].denom}`);
quantity: (100000 + (i * 500)).toString()
});
bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${bidAmounts[i].quantity}${bidAmounts[i].type}`);
await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee);
} }
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('commit'); expect(auction.status).toEqual('commit');
expect(auction.bids.length).toEqual(3);
auction.bids.forEach((bid: any) => { auction.bids.forEach((bid: any) => {
expect(bid.status).toEqual('commit'); expect(bid.status).toEqual('commit');
}); });
@ -259,8 +250,9 @@ const createProviderAuctionTests = () => {
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('completed'); 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++) { for (let i = 0; i < bidWinners.length; i++) {
const winner = bidWinners[i]; const winner = bidWinners[i];
@ -275,21 +267,24 @@ const createProviderAuctionTests = () => {
const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10); const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10);
const expectedBalance = bidderInitialBalance + winningBidAmount; const expectedBalance = bidderInitialBalance + winningBidAmount;
// The actual balance would be less than expected balance as fees would also get deducted
expect(type).toBe(DENOM); 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]); const [bidderAccountObj] = await registry.getAccounts([bidder.address]);
expect(bidderAccountObj).toBeDefined(); expect(bidderAccountObj).toBeDefined();
expect(bidderAccountObj.address).toBe(bidder.address); expect(bidderAccountObj.address).toBe(bidder.address);
const [{ type, quantity }] = bidderAccountObj.balance; const [{ type, quantity }] = bidderAccountObj.balance;
const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10); // The balance would be less than initial balance as fees would get deducted
const expectedWinningBalance = bidderInitialBalance - winningBidAmount;
expect(type).toBe(DENOM); expect(type).toBe(DENOM);
expect(parseInt(quantity)).toBeLessThan(expectedWinningBalance); expect(parseInt(quantity)).toBeLessThan(bidderInitialBalance);
} }
const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]); const [creatorAccountObj] = await registry.getAccounts([auctionCreatorAccount.address]);
@ -299,8 +294,9 @@ const createProviderAuctionTests = () => {
const [{ type, quantity }] = creatorAccountObj.balance; const [{ type, quantity }] = creatorAccountObj.balance;
const totalWinningAmount = parseInt(auction.winnerPrice.quantity, 10) * bidWinners.length; 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(type).toBe(DENOM);
expect(parseInt(quantity)).toBeLessThan(expectedCreatorBalance); expect(parseInt(quantity)).toBeLessThan(expectedCreatorBalance);
}); });