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
3 changed files with 27 additions and 38 deletions
Showing only changes of commit 11705f060b - Show all commits

View File

@ -1,31 +1,31 @@
import Long from 'long'; import Long from 'long';
import { coin } from '@cosmjs/amino';
import { Registry, Account, createBid } from './index'; import { Registry, Account, createBid } from './index';
import { getConfig } from './testing/helper'; import { getConfig } from './testing/helper';
import { DENOM, DURATION } from './constants'; import { DENOM } from './constants';
import { Duration } from './proto/google/protobuf/duration';
jest.setTimeout(30 * 60 * 1000); jest.setTimeout(30 * 60 * 1000);
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
const commitsDuration = { const duration = 60;
seconds: Long.fromNumber(DURATION),
const commitsDuration = Duration.fromPartial({
seconds: Long.fromNumber(duration),
nanos: 0 nanos: 0
}; });
const revealsDuration = Duration.fromPartial({
const revealsDuration = { seconds: Long.fromNumber(duration),
seconds: Long.fromNumber(DURATION),
nanos: 0 nanos: 0
}; });
const commitFee = { const commitFee = coin('1000', DENOM);
denom: DENOM, const revealFee = coin('1000', DENOM);
amount: '1000'
};
const revealFee = { const creatorInitialBalance = 1000000000000;
denom: DENOM, const bidderInitialBalance = 20000000;
amount: '1000'
};
const createAuctionTests = () => { const createAuctionTests = () => {
let registry: Registry; let registry: Registry;
@ -37,15 +37,13 @@ const createAuctionTests = () => {
let bidAmounts: { type: string, quantity: string }[] = []; let bidAmounts: { type: string, quantity: string }[] = [];
beforeAll(async () => { beforeAll(async () => {
console.log('Running vickrey auction tests');
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
// Create auction creator account // Create auction creator account
const mnenonic1 = Account.generateMnemonic(); const mnenonic1 = Account.generateMnemonic();
const auctionCreator = await Account.generateFromMnemonic(mnenonic1); const auctionCreator = await Account.generateFromMnemonic(mnenonic1);
await auctionCreator.init(); await auctionCreator.init();
await registry.sendCoins({ denom: DENOM, amount: '1000000000000', 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') };
}); });
@ -54,7 +52,7 @@ const createAuctionTests = () => {
const mnenonic = Account.generateMnemonic(); const mnenonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnenonic); const account = await Account.generateFromMnemonic(mnenonic);
await account.init(); await account.init();
await registry.sendCoins({ denom: DENOM, amount: '20000000', destinationAddress: account.address }, privateKey, fee); await registry.sendCoins({ denom: DENOM, amount: bidderInitialBalance.toString(), destinationAddress: account.address }, privateKey, fee);
bidderAccounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') }); bidderAccounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') });
} }
}); });
@ -70,8 +68,7 @@ const createAuctionTests = () => {
revealsDuration, revealsDuration,
commitFee, commitFee,
revealFee, revealFee,
minimumBid, minimumBid
signer: auctionCreatorAccount.address
}, },
auctionCreatorAccount.privateKey, fee); auctionCreatorAccount.privateKey, fee);
@ -98,7 +95,7 @@ const createAuctionTests = () => {
}); });
test('Wait for reveal phase.', (done) => { test('Wait for reveal phase.', (done) => {
const commitTime = DURATION * 1000; const commitTime = duration * 1000;
const waitTime = commitTime + (6 * 1000); const waitTime = commitTime + (6 * 1000);
setTimeout(done, waitTime); setTimeout(done, waitTime);
@ -125,7 +122,7 @@ const createAuctionTests = () => {
}); });
test('Wait for auction completion.', (done) => { test('Wait for auction completion.', (done) => {
const revealTime = DURATION * 1000; const revealTime = duration * 1000;
const waitTime = revealTime + (6 * 1000); const waitTime = revealTime + (6 * 1000);
setTimeout(done, waitTime); setTimeout(done, waitTime);
@ -136,13 +133,12 @@ const createAuctionTests = () => {
expect(auction.status).toEqual('completed'); expect(auction.status).toEqual('completed');
const highestBidder = bidderAccounts[bidderAccounts.length - 1]; const highestBidder = bidderAccounts[bidderAccounts.length - 1];
const secondHighestBidder = (bidderAccounts.length > 1 ? bidderAccounts[bidderAccounts.length - 2] : highestBidder); const secondHighestBidder = (bidderAccounts[bidderAccounts.length - 2]);
expect(auction.winnerAddresses[0]).toEqual(highestBidder.address); expect(auction.winnerAddresses[0]).toEqual(highestBidder.address);
expect(highestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerBids[0].quantity}${auction.winnerBids[0].type}`); expect(highestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerBids[0].quantity}${auction.winnerBids[0].type}`);
expect(secondHighestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`); expect(secondHighestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`);
const bidderInitialBalance = 20000000;
const winningPriceAmount = parseInt(auction.winnerPrice.quantity, 10); const winningPriceAmount = parseInt(auction.winnerPrice.quantity, 10);
const expectedBalance = (bidderInitialBalance - winningPriceAmount).toString(); const expectedBalance = (bidderInitialBalance - winningPriceAmount).toString();
@ -167,15 +163,13 @@ const createProviderAuctionTests = () => {
let bidAmounts: { type: string, quantity: string }[] = []; let bidAmounts: { type: string, quantity: string }[] = [];
beforeAll(async () => { beforeAll(async () => {
console.log('Running provider auction tests');
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
// Create auction creator account // Create auction creator account
const mnenonic1 = Account.generateMnemonic(); const mnenonic1 = Account.generateMnemonic();
const auctionCreator = await Account.generateFromMnemonic(mnenonic1); const auctionCreator = await Account.generateFromMnemonic(mnenonic1);
await auctionCreator.init(); await auctionCreator.init();
await registry.sendCoins({ denom: DENOM, amount: '1000000000000', 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') };
}); });
@ -184,7 +178,7 @@ const createProviderAuctionTests = () => {
const mnenonic = Account.generateMnemonic(); const mnenonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnenonic); const account = await Account.generateFromMnemonic(mnenonic);
await account.init(); await account.init();
await registry.sendCoins({ denom: DENOM, amount: '20000000', destinationAddress: account.address }, privateKey, fee); await registry.sendCoins({ denom: DENOM, amount: bidderInitialBalance.toString(), destinationAddress: account.address }, privateKey, fee);
bidderAccounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') }); bidderAccounts.push({ address: account.address, privateKey: account.privateKey.toString('hex') });
} }
}); });
@ -200,7 +194,6 @@ const createProviderAuctionTests = () => {
revealsDuration, revealsDuration,
commitFee, commitFee,
revealFee, revealFee,
signer: auctionCreatorAccount.address,
maxPrice, maxPrice,
numProviders: 3 numProviders: 3
}, },
@ -229,7 +222,7 @@ const createProviderAuctionTests = () => {
}); });
test('Wait for reveal phase.', (done) => { test('Wait for reveal phase.', (done) => {
const commitTime = DURATION * 1000; const commitTime = duration * 1000;
const waitTime = commitTime + (6 * 1000); const waitTime = commitTime + (6 * 1000);
setTimeout(done, waitTime); setTimeout(done, waitTime);
@ -256,7 +249,7 @@ const createProviderAuctionTests = () => {
}); });
test('Wait for auction completion.', (done) => { test('Wait for auction completion.', (done) => {
const revealTime = DURATION * 1000; const revealTime = duration * 1000;
const waitTime = revealTime + (6 * 1000); const waitTime = revealTime + (6 * 1000);
setTimeout(done, waitTime); setTimeout(done, waitTime);
@ -275,8 +268,6 @@ const createProviderAuctionTests = () => {
expect(winner.bid.reveal.bidAmount).toEqual(`${auction.winnerBids[i].quantity}${auction.winnerBids[i].type}`); expect(winner.bid.reveal.bidAmount).toEqual(`${auction.winnerBids[i].quantity}${auction.winnerBids[i].type}`);
const bidderInitialBalance = 20000000;
const [winnerAccountObj] = await registry.getAccounts([winner.address]); const [winnerAccountObj] = await registry.getAccounts([winner.address]);
expect(winnerAccountObj).toBeDefined(); expect(winnerAccountObj).toBeDefined();
expect(winnerAccountObj.address).toBe(winner.address); expect(winnerAccountObj.address).toBe(winner.address);
@ -296,7 +287,6 @@ const createProviderAuctionTests = () => {
const [{ type, quantity: currentBalance }] = creatorAccountObj.balance; const [{ type, quantity: currentBalance }] = creatorAccountObj.balance;
const creatorInitialBalance = 1000000000000;
const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10); const winningBidAmount = parseInt(auction.winnerPrice.quantity, 10);
const expectedBalance = creatorInitialBalance - (winningBidAmount * 3); const expectedBalance = creatorInitialBalance - (winningBidAmount * 3);

View File

@ -97,7 +97,7 @@ const auctionTests = (numBidders = 3) => {
const highestBidder = accounts[accounts.length - 1]; const highestBidder = accounts[accounts.length - 1];
const secondHighestBidder = (accounts.length > 1 ? accounts[accounts.length - 2] : highestBidder); const secondHighestBidder = (accounts.length > 1 ? accounts[accounts.length - 2] : highestBidder);
expect(auction.winnerAddress).toEqual(highestBidder.address); expect(auction.winnerAddresses[0]).toEqual(highestBidder.address);
expect(highestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerBid.quantity}${auction.winnerBid.type}`); expect(highestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerBid.quantity}${auction.winnerBid.type}`);
expect(secondHighestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`); expect(secondHighestBidder.bid.reveal.bidAmount).toEqual(`${auction.winnerPrice.quantity}${auction.winnerPrice.type}`);

View File

@ -1,3 +1,2 @@
export const DENOM = 'alnt'; export const DENOM = 'alnt';
export const DURATION = 60;
export const DEFAULT_GAS_ESTIMATION_MULTIPLIER = 2; export const DEFAULT_GAS_ESTIMATION_MULTIPLIER = 2;