Add methods for creating auctions and add auction tests #28

Merged
nabarun merged 32 commits from deep-stack/registry-sdk:iv-create-auction-test into main 2024-09-25 13:21:05 +00:00
2 changed files with 40 additions and 14 deletions
Showing only changes of commit 233063881c - Show all commits

View File

@ -22,6 +22,7 @@ const creatorInitialBalance = 1000000000000;
const bidderInitialBalance = 20000000; const bidderInitialBalance = 20000000;
const lowestBidAmount = 10000000; const lowestBidAmount = 10000000;
// TODO: Check exact balances by subtracting the fees
const auctionTests = () => { const auctionTests = () => {
let registry: Registry; let registry: Registry;
let auctionId: string; let auctionId: string;
@ -168,6 +169,8 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
let auctionCreatorAccount: { address: string, privateKey: string }; let auctionCreatorAccount: { address: string, privateKey: string };
let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = [];
let sortedBidders: { address: string, privateKey: string, bid?: any }[] = [];
let invalidBidderAddresses: string[] = [];
const bidAmounts: Coin[] = bidsAmount.map(amount => coin(amount.toString(), DENOM)); const bidAmounts: Coin[] = bidsAmount.map(amount => coin(amount.toString(), DENOM));
const numBidders = bidAmounts.length; const numBidders = bidAmounts.length;
@ -219,10 +222,10 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
const [{ type, quantity }] = creatorAccountObj.balance; const [{ type, quantity }] = creatorAccountObj.balance;
const actualBalance = parseInt(quantity); const actualBalance = parseInt(quantity);
const expectedBalance = creatorInitialBalance - (parseInt(maxPrice.amount) * numProviders); const expectedBalance = creatorInitialBalance - (parseInt(maxPrice.amount) * numProviders) - 200000;
expect(type).toBe(DENOM); expect(type).toBe(DENOM);
expect(actualBalance).toBe(expectedBalance - 200000); expect(actualBalance).toBe(expectedBalance);
}); });
test('Commit bids.', async () => { test('Commit bids.', async () => {
@ -231,6 +234,16 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
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);
} }
sortedBidders = bidderAccounts.slice().sort((a, b) => {
return parseInt(a.bid.reveal.bidAmount) - parseInt(b.bid.reveal.bidAmount);
});
for (const bidder of sortedBidders) {
if (parseInt(bidder.bid.reveal.bidAmount) > parseInt(maxPrice.amount)) {
invalidBidderAddresses.push(bidder.address);
}
}
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(numBidders); expect(auction.bids.length).toEqual(numBidders);
@ -251,20 +264,35 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
expect(auction.status).toEqual('reveal'); expect(auction.status).toEqual('reveal');
for (let i = 0; i < numBidders; i++) { for (let i = 0; i < numBidders; i++) {
await registry.revealBid({ auctionId, reveal: bidderAccounts[i].bid.revealString }, bidderAccounts[i].privateKey, fee); try {
await registry.revealBid(
{ auctionId, reveal: bidderAccounts[i].bid.revealString },
bidderAccounts[i].privateKey,
fee
);
} catch (error: any) {
if (invalidBidderAddresses.includes(bidderAccounts[i].address)) {
expect(error.toString()).toContain(INVALID_BID_ERROR);
} else {
throw error;
}
}
} }
}); });
test('Check bids are revealed', async () => { test('Check bids are revealed', async () => {
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('reveal'); expect(auction.status).toEqual('reveal');
auction.bids.forEach((bid: any) => {
expect(bid.status).toEqual('reveal'); const validBids = sortedBidders.map((bidder: any) => {
if (invalidBidderAddresses.includes(bidder.address)) {
return '0';
}
return bidder.bid.reveal.bidAmount;
}); });
const expectedBidAmounts = bidAmounts.map(bidAmount => { return { quantity: bidAmount.amount, type: bidAmount.denom }; }); const actualBidAmounts = auction.bids.map((bid: any) => `${bid.bidAmount.quantity}${bid.bidAmount.type}`);
const actualBidAmounts = auction.bids.map((bid: any) => bid.bidAmount); expect(actualBidAmounts).toEqual(expect.arrayContaining(validBids));
expect(actualBidAmounts).toEqual(expect.arrayContaining(expectedBidAmounts));
}); });
test('Wait for auction completion.', (done) => { test('Wait for auction completion.', (done) => {
@ -278,12 +306,9 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('completed'); expect(auction.status).toEqual('completed');
const sortedBidders = bidderAccounts.slice().sort((a, b) => { // Filter bidders to exclude bids exceeding maxPrice
return parseInt(a.bid.reveal.bidAmount) - parseInt(b.bid.reveal.bidAmount);
});
const filteredBidders = sortedBidders.filter(bidder => { const filteredBidders = sortedBidders.filter(bidder => {
return parseInt(bidder.bid.reveal.bidAmount) <= parseInt(maxPrice.amount); return parseInt(bidder.bid.reveal.bidAmount) <= parseInt(`${maxPrice.amount}${maxPrice.denom}`);
}); });
const numWinners = Math.min(filteredBidders.length, numProviders); const numWinners = Math.min(filteredBidders.length, numProviders);
@ -343,6 +368,6 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
}; };
describe('Vickrey Auction', () => auctionTests()); describe('Vickrey Auction', () => auctionTests());
const randomBids = [10002000, 10009000, 10006000, 10004000]; const randomBids = [10002000, 10009000, 10006000, 100040000];
describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 3)); describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 3));
describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 5)); describe('Provider Auction', () => providerAuctionTestsWithBids(randomBids, 5));

View File

@ -16,6 +16,7 @@ export interface MsgOnboardParticipantEncodeObject extends EncodeObject {
} }
export const ONBOARDING_DISABLED_ERROR = 'Onboarding is disabled'; export const ONBOARDING_DISABLED_ERROR = 'Onboarding is disabled';
export const INVALID_BID_ERROR = 'Bid is higher than max price';
interface ethPayload { interface ethPayload {
address: string address: string