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
Showing only changes of commit 570c9bdb3d - Show all commits

View File

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