From a647fed60ad3f254525ed080ee527cc13b19e490 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Mon, 16 Sep 2024 18:12:30 +0530 Subject: [PATCH] Add test for service provider auctions --- package.json | 2 +- src/auction.test.ts | 163 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 138 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 05311b5..5d9632f 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "scripts": { "test": "jest --runInBand --verbose --testPathPattern=src", "test:authority-auctions": "TEST_AUCTIONS_ENABLED=1 jest --runInBand --verbose src/authority-auction.test.ts", - "test:auction": "TEST_AUCTIONS_ENABLED=1 jest --runInBand --verbose src/auction.test.ts", + "test:auctions": "TEST_AUCTIONS_ENABLED=1 jest --runInBand --verbose src/auction.test.ts", "test:nameservice-expiry": "TEST_NAMESERVICE_EXPIRY=1 jest --runInBand --verbose src/nameservice-expiry.test.ts", "test:onboarding": "ONBOARDING_ENABLED=1 jest --runInBand --verbose src/onboarding.test.ts", "build": "tsc", diff --git a/src/auction.test.ts b/src/auction.test.ts index 2ca8486..2ef8d08 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -9,37 +9,37 @@ const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; +const commitsDuration = { + seconds: Long.fromNumber(60), + nanos: 0 +}; + +const revealsDuration = { + seconds: Long.fromNumber(60), + nanos: 0 +}; + +const commitFee = { + denom: DENOM, + amount: '1000' +}; + +const revealFee = { + denom: DENOM, + amount: '1000' +}; + const createAuctionTests = () => { let registry: Registry; - const commitsDuration = { - seconds: Long.fromNumber(60), - nanos: 0 - }; - - const revealsDuration = { - seconds: Long.fromNumber(60), - nanos: 0 - }; - - const commitFee = { - denom: DENOM, - amount: '1000' - }; - - const revealFee = { - denom: DENOM, - amount: '1000' - }; - let auctionId: string; beforeAll(async () => { - console.log('Running auction tests'); + console.log('Running vickrey auction tests'); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); - // Create auction creator account and a bidder account + // Create auction creator account const mnenonic1 = Account.generateMnemonic(); const auctionCreator = await Account.generateFromMnemonic(mnenonic1); await auctionCreator.init(); @@ -57,7 +57,7 @@ const createAuctionTests = () => { } }); - test('Create auction', async () => { + test('Create vickrey auction', async () => { const minimumBid = { denom: 'alnt', amount: '1000000' @@ -89,7 +89,10 @@ const createAuctionTests = () => { }); test('Wait for reveal phase.', (done) => { - setTimeout(done, 60 * 1000); + const commitTime = 60 * 1000; + const waitTime = commitTime + (6 * 1000); + + setTimeout(done, waitTime); }); test('Reveal bids.', async () => { @@ -111,7 +114,10 @@ const createAuctionTests = () => { }); test('Wait for auction completion.', (done) => { - setTimeout(done, 60 * 1000); + const revealTime = 60 * 1000; + const waitTime = revealTime + (6 * 1000); + + setTimeout(done, waitTime); }); test('Check auction winner, authority owner and status.', async () => { @@ -127,7 +133,112 @@ const createAuctionTests = () => { }); }; -const createSPAuctionTests = () => {}; +const createSPAuctionTests = () => { + let registry: Registry; + let auctionId: string; + + let auctionCreatorAccount: { address: string, privateKey: string, bid?: any }; + let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; + + beforeAll(async () => { + console.log('Running service provider auction tests'); + + registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); + + // Create auction creator account + const mnenonic1 = Account.generateMnemonic(); + const auctionCreator = await Account.generateFromMnemonic(mnenonic1); + await auctionCreator.init(); + await registry.sendCoins({ denom: DENOM, amount: '1000000000000', destinationAddress: auctionCreator.address }, privateKey, fee); + auctionCreatorAccount = { address: auctionCreator.address, privateKey: auctionCreator.privateKey.toString('hex') }; + }); + + test('Setup bidder accounts', async () => { + for (let i = 0; i < 4; i++) { + const mnenonic = Account.generateMnemonic(); + const account = await Account.generateFromMnemonic(mnenonic); + await account.init(); + await registry.sendCoins({ denom: DENOM, amount: '20000000', destinationAddress: account.address }, privateKey, fee); + bidderAccounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') }); + } + }); + + test('Create service provider auction', async () => { + const maxPrice = { + denom: 'alnt', + amount: '100000000' + }; + + const auction = await registry.createAuction({ + commitsDuration, + revealsDuration, + commitFee, + revealFee, + minimumBid: undefined, + maxPrice, + kind: 'service_provider', + numProviders: 3, + signer: auctionCreatorAccount.address + }, + auctionCreatorAccount.privateKey, fee); + + expect(auction.auction?.id).toBeDefined(); + auctionId = auction.auction?.id || ''; + expect(auction.auction?.status).toEqual('commit'); + }); + + test('Commit bids.', async () => { + for (let i = 0; i < 4; i++) { + bidderAccounts[i].bid = await createBid(chainId, auctionId, bidderAccounts[i].address, `${100000 + (i * 500)}${DENOM}`); + await registry.commitBid({ auctionId, commitHash: bidderAccounts[i].bid.commitHash }, bidderAccounts[i].privateKey, fee); + } + }); + + test('Wait for reveal phase.', (done) => { + const commitTime = 60 * 1000; + const waitTime = commitTime + (6 * 1000); + + setTimeout(done, waitTime); + }); + + test('Reveal bids.', async () => { + const [auction] = await registry.getAuctionsByIds([auctionId]); + expect(auction.status).toEqual('reveal'); + + for (let i = 0; i < 4; i++) { + await registry.revealBid({ auctionId, reveal: bidderAccounts[i].bid.revealString }, bidderAccounts[i].privateKey, fee); + } + }); + + test('Check bids are revealed', async () => { + const [auction] = await registry.getAuctionsByIds([auctionId]); + expect(auction.status).toEqual('reveal'); + + auction.bids.forEach((i: number, bid: any) => { + expect(bid.status).toEqual('reveal'); + }); + }); + + test('Wait for auction completion.', (done) => { + const revealTime = 60 * 1000; + const waitTime = revealTime + (6 * 1000); + + setTimeout(done, waitTime); + }); + + test('Check auction winner, authority owner and status.', async () => { + const [auction] = await registry.getAuctionsByIds([auctionId]); + expect(auction.status).toEqual('completed'); + + const bidWinners = bidderAccounts.slice(0, 3); + + bidWinners.forEach((winner, index) => { + expect(auction.winnerAddresses[index]).toEqual(winner.address); + expect(bidWinners[index].bid.reveal.bidAmount).toEqual(`${auction.winnerBids[index].quantity}${auction.winnerBids[index].type}`); + }); + expect(bidWinners[bidWinners.length - 1].bid.reveal.bidAmount).toEqual(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`); + }); +}; describe('Vickrey Auction', () => createAuctionTests()); describe('Service provider Auction', () => createSPAuctionTests());