|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
import { Registry, Account, createBid, INVALID_BID_ERROR, RELEASE_FUNDS_ERROR, OWNER_MISMATCH_ERROR } from './index';
|
|
|
|
|
import { checkAccountBalance, createTestAccounts, getConfig } from './testing/helper';
|
|
|
|
|
import { Registry, Account, createBid } from './index';
|
|
|
|
|
import { createTestAccounts, getConfig } from './testing/helper';
|
|
|
|
|
import { DENOM } from './constants';
|
|
|
|
|
import { INVALID_BID_ERROR, OWNER_MISMATCH_ERROR, RELEASE_FUNDS_ERROR } from './types/cerc/auction/message';
|
|
|
|
|
|
|
|
|
|
jest.setTimeout(30 * 60 * 1000);
|
|
|
|
|
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
|
|
|
@ -105,13 +106,9 @@ const auctionTests = () => {
|
|
|
|
|
|
|
|
|
|
// Check that the bid amounts are locked after reveal phase
|
|
|
|
|
for (let i = 0; i < numBidders; i++) {
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, bidderAccounts[i].address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
// The bid amount, commit fee, reveal fee and tx fees (for commit and reveal txs) will be deducted from the bidder's acoount
|
|
|
|
|
const expectedBalance = bidderInitialBalance - parseInt(bidAmounts[i]) - (2 * txFees) - parseInt(commitFee) - parseInt(revealFee);
|
|
|
|
|
expect(actualBalance).toBe(expectedBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, bidderAccounts[i].address, expectedBalance);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -137,12 +134,9 @@ const auctionTests = () => {
|
|
|
|
|
|
|
|
|
|
const winningPriceAmount = parseInt(auction.winnerPrice.quantity);
|
|
|
|
|
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, highestBidder.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
// The winning price will get deducted after auction completion
|
|
|
|
|
expect(actualBalance).toBe(bidderInitialBalance - winningPriceAmount - (2 * txFees) - parseInt(commitFee));
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
const expectedBalance = bidderInitialBalance - winningPriceAmount - (2 * txFees) - parseInt(commitFee);
|
|
|
|
|
await checkAccountBalance(registry, highestBidder.address, expectedBalance);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -201,14 +195,9 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
auctionId = auction.auction?.id || '';
|
|
|
|
|
expect(auction.auction?.status).toEqual('commit');
|
|
|
|
|
|
|
|
|
|
// Check that the total locked amount is deducted from the creator's account
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, auctionCreatorAccount.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
// The locked amount and tx fees are deducted from the auction creator's account
|
|
|
|
|
const expectedBalance = creatorInitialBalance - (maxPrice * numProviders) - txFees;
|
|
|
|
|
expect(actualBalance).toBe(expectedBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, auctionCreatorAccount.address, expectedBalance);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Commit bids.', async () => {
|
|
|
|
@ -301,14 +290,10 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
const expectedWinningPrice = winningBidders[numWinners - 1].bid.reveal.bidAmount;
|
|
|
|
|
expect(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`).toEqual(expectedWinningPrice);
|
|
|
|
|
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, auctionCreatorAccount.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
// The total winning amount will get deducted and the leftover locked amount is returned
|
|
|
|
|
const totalWinningAmount = parseInt(auction.winnerPrice.quantity) * winningBidders.length;
|
|
|
|
|
const expectedCreatorBalance = creatorInitialBalance - totalWinningAmount - txFees;
|
|
|
|
|
expect(actualBalance).toBe(expectedCreatorBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, auctionCreatorAccount.address, expectedCreatorBalance);
|
|
|
|
|
|
|
|
|
|
// Funds should not be given to winners on auction completion
|
|
|
|
|
for (let i = 0; i < winningBidders.length; i++) {
|
|
|
|
@ -317,12 +302,8 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
expect(auction.winnerAddresses[i]).toEqual(bidWinner.address);
|
|
|
|
|
expect(`${auction.winnerBids[i].quantity}${auction.winnerBids[i].type}`).toEqual(bidWinner.bid.reveal.bidAmount);
|
|
|
|
|
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, bidWinner.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
const expectedBidderBalance = bidderInitialBalance - (2 * txFees) - parseInt(commitFee);
|
|
|
|
|
expect(actualBalance).toBe(expectedBidderBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, bidWinner.address, expectedBidderBalance);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -347,25 +328,18 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
expect(auctionObj.auction?.fundsReleased).toBe(true);
|
|
|
|
|
|
|
|
|
|
// Auction winners get the winning amount after funds are released
|
|
|
|
|
const expectedBidderBalance = bidderInitialBalance + winningBidAmount - (2 * txFees) - parseInt(commitFee);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, bidWinner.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
const expectedBidderBalance = bidderInitialBalance + winningBidAmount - (2 * txFees) - parseInt(commitFee);
|
|
|
|
|
expect(actualBalance).toBe(expectedBidderBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, bidWinner.address, expectedBidderBalance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check balances of non-winners
|
|
|
|
|
for (const bidder of losingBidders) {
|
|
|
|
|
const { type, quantity } = await checkAccountBalance(registry, bidder.address);
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
let expectedBidderBalance: number;
|
|
|
|
|
if (invalidBidderAddress !== bidder.address) {
|
|
|
|
|
expectedBidderBalance = bidderInitialBalance - (2 * txFees) - parseInt(commitFee);
|
|
|
|
@ -374,8 +348,7 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
expectedBidderBalance = bidderInitialBalance - (2 * txFees) - parseInt(commitFee) - parseInt(revealFee);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(actualBalance).toBe(expectedBidderBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
await checkAccountBalance(registry, bidder.address, expectedBidderBalance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Funds cannot be released twice
|
|
|
|
@ -387,14 +360,23 @@ const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const checkAccountBalance = async (registry: Registry, address: string, expectedBalance: number): Promise<void> => {
|
|
|
|
|
const [winnerAccountObj] = await registry.getAccounts([address]);
|
|
|
|
|
const [{ type, quantity }] = winnerAccountObj.balance;
|
|
|
|
|
const actualBalance = parseInt(quantity);
|
|
|
|
|
|
|
|
|
|
expect(actualBalance).toBe(expectedBalance);
|
|
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const providerAuctionTests = () => {
|
|
|
|
|
// With a bid amount greater than the max price
|
|
|
|
|
const bids1 = [10002000, 10009000, 10006000 * 10, 10004000];
|
|
|
|
|
const bids = [10002000, 10009000, 10006000 * 10, 10004000];
|
|
|
|
|
|
|
|
|
|
// Number of providers is less than number of bidders
|
|
|
|
|
describe('Auction with numProviders < numBidders', () => providerAuctionTestsWithBids(bids1, 3));
|
|
|
|
|
describe('Auction with numProviders < numBidders', () => providerAuctionTestsWithBids(bids, 3));
|
|
|
|
|
// Number of providers is greater than number of bidders
|
|
|
|
|
describe('Auction numProviders > numBidders', () => providerAuctionTestsWithBids(bids1, 5));
|
|
|
|
|
describe('Auction numProviders > numBidders', () => providerAuctionTestsWithBids(bids, 5));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('Vickrey Auction', () => auctionTests());
|
|
|
|
|