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
4 changed files with 61 additions and 64 deletions
Showing only changes of commit daa4297e72 - Show all commits

View File

@ -1,22 +1,13 @@
import Long from 'long';
import { coin } from '@cosmjs/amino';
import { Coin } from '@cosmjs/proto-signing';
import { Registry, Account, createBid, INVALID_BID_ERROR } from './index'; import { Registry, Account, createBid, INVALID_BID_ERROR } from './index';
import { getConfig } from './testing/helper'; import { getConfig } from './testing/helper';
import { DENOM } 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 duration = 60; const duration = 60;
const commitsDuration = Duration.fromPartial({ seconds: Long.fromNumber(60) }); const commitFee = '1000';
const revealsDuration = Duration.fromPartial({ seconds: Long.fromNumber(60) }); const revealFee = '1000';
const commitFee = coin('1000', DENOM);
const revealFee = coin('1000', DENOM);
const creatorInitialBalance = 1000000000000; const creatorInitialBalance = 1000000000000;
const bidderInitialBalance = 20000000; const bidderInitialBalance = 20000000;
@ -31,7 +22,7 @@ const auctionTests = () => {
let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = []; let bidderAccounts: { address: string, privateKey: string, bid?: any }[] = [];
const numBidders = 3; const numBidders = 3;
let bidAmounts: Coin[] = []; let bidAmounts: string[] = [];
beforeAll(async () => { beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
@ -56,12 +47,13 @@ const auctionTests = () => {
}); });
test('Create a vickrey auction', async () => { test('Create a vickrey auction', async () => {
const minimumBid = coin('1000000', DENOM); const minimumBid = '1000000';
const auction = await registry.createAuction( const auction = await registry.createAuction(
{ {
commitsDuration, commitsDuration: duration.toString(),
revealsDuration, revealsDuration: duration.toString(),
denom: DENOM,
commitFee, commitFee,
revealFee, revealFee,
minimumBid minimumBid
@ -77,8 +69,8 @@ const auctionTests = () => {
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)); bidAmounts.push((lowestBidAmount + (i * 500)).toString());
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]}${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);
} }
@ -113,7 +105,7 @@ const auctionTests = () => {
expect(bid.status).toEqual('reveal'); expect(bid.status).toEqual('reveal');
}); });
const expectedBidAmounts = bidAmounts.map(bidAmount => { return { quantity: bidAmount.amount, type: bidAmount.denom }; }); const expectedBidAmounts = bidAmounts.map(bidAmount => { return { quantity: bidAmount.toString(), type: DENOM }; });
const actualBidAmounts = auction.bids.map((bid: any) => bid.bidAmount); const actualBidAmounts = auction.bids.map((bid: any) => bid.bidAmount);
expect(actualBidAmounts).toEqual(expect.arrayContaining(expectedBidAmounts)); expect(actualBidAmounts).toEqual(expect.arrayContaining(expectedBidAmounts));
@ -124,7 +116,7 @@ const auctionTests = () => {
const [{ type, quantity }] = bidderrAccountObj.balance; const [{ type, quantity }] = bidderrAccountObj.balance;
const actualBalance = parseInt(quantity); const actualBalance = parseInt(quantity);
const expectedBalance = bidderInitialBalance - parseInt(bidAmounts[i].amount); const expectedBalance = bidderInitialBalance - parseInt(bidAmounts[i]);
expect(type).toBe(DENOM); expect(type).toBe(DENOM);
expect(actualBalance).toBeLessThan(expectedBalance); expect(actualBalance).toBeLessThan(expectedBalance);
@ -163,7 +155,7 @@ const auctionTests = () => {
}); });
}; };
const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number) => { const providerAuctionTestsWithBids = (bidAmounts: number[], numProviders: number) => {
let registry: Registry; let registry: Registry;
let auctionId: string; let auctionId: string;
@ -172,10 +164,8 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
let sortedBidders: { address: string, privateKey: string, bid?: any }[] = []; let sortedBidders: { address: string, privateKey: string, bid?: any }[] = [];
let invalidBidderAddresses: string[] = []; let invalidBidderAddresses: string[] = [];
const bidAmounts: Coin[] = bidsAmount.map(amount => coin(amount.toString(), DENOM));
const numBidders = bidAmounts.length; const numBidders = bidAmounts.length;
const maxPrice = 10 * lowestBidAmount;
const maxPrice = coin((10 * lowestBidAmount).toString(), DENOM);
beforeAll(async () => { beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
@ -202,11 +192,12 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
test('Create a provider auction', async () => { test('Create a provider auction', async () => {
const auction = await registry.createProviderAuction( const auction = await registry.createProviderAuction(
{ {
commitsDuration, commitsDuration: duration.toString(),
revealsDuration, revealsDuration: duration.toString(),
denom: DENOM,
commitFee, commitFee,
revealFee, revealFee,
maxPrice, maxPrice: maxPrice.toString(),
numProviders numProviders
}, },
auctionCreatorAccount.privateKey, fee auctionCreatorAccount.privateKey, fee
@ -222,7 +213,7 @@ 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) - 200000; const expectedBalance = creatorInitialBalance - (maxPrice * numProviders) - 200000;
expect(type).toBe(DENOM); expect(type).toBe(DENOM);
expect(actualBalance).toBe(expectedBalance); expect(actualBalance).toBe(expectedBalance);
@ -230,7 +221,7 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
test('Commit bids.', async () => { test('Commit bids.', async () => {
for (let i = 0; i < numBidders; i++) { for (let i = 0; i < numBidders; i++) {
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]}${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);
} }
@ -239,7 +230,7 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
}); });
for (const bidder of sortedBidders) { for (const bidder of sortedBidders) {
if (parseInt(bidder.bid.reveal.bidAmount) > parseInt(maxPrice.amount)) { if (parseInt(bidder.bid.reveal.bidAmount) > maxPrice) {
invalidBidderAddresses.push(bidder.address); invalidBidderAddresses.push(bidder.address);
} }
} }
@ -284,7 +275,7 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
const [auction] = await registry.getAuctionsByIds([auctionId]); const [auction] = await registry.getAuctionsByIds([auctionId]);
expect(auction.status).toEqual('reveal'); expect(auction.status).toEqual('reveal');
const validBids = sortedBidders.map((bidder: any) => { const expectedBids = sortedBidders.map((bidder: any) => {
if (invalidBidderAddresses.includes(bidder.address)) { if (invalidBidderAddresses.includes(bidder.address)) {
return '0'; return '0';
} }
@ -292,7 +283,7 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
}); });
const actualBidAmounts = auction.bids.map((bid: any) => `${bid.bidAmount.quantity}${bid.bidAmount.type}`); const actualBidAmounts = auction.bids.map((bid: any) => `${bid.bidAmount.quantity}${bid.bidAmount.type}`);
expect(actualBidAmounts).toEqual(expect.arrayContaining(validBids)); expect(actualBidAmounts).toEqual(expect.arrayContaining(expectedBids));
}); });
test('Wait for auction completion.', (done) => { test('Wait for auction completion.', (done) => {
@ -308,7 +299,7 @@ const providerAuctionTestsWithBids = (bidsAmount: number[], numProviders: number
// Filter bidders to exclude bids exceeding maxPrice // Filter bidders to exclude bids exceeding maxPrice
const filteredBidders = sortedBidders.filter(bidder => { const filteredBidders = sortedBidders.filter(bidder => {
return parseInt(bidder.bid.reveal.bidAmount) <= parseInt(`${maxPrice.amount}${maxPrice.denom}`); return parseInt(bidder.bid.reveal.bidAmount) <= parseInt(`${maxPrice}${DENOM}`);
}); });
const numWinners = Math.min(filteredBidders.length, numProviders); const numWinners = Math.min(filteredBidders.length, numProviders);

View File

@ -23,8 +23,8 @@ import {
import { import {
MessageMsgCommitBid, MessageMsgCommitBid,
MessageMsgRevealBid, MessageMsgRevealBid,
MsgCreateProviderAuction, MessageCreateVickreyAuction,
MsgCreateVickreyAuction MessageCreateProviderAuction
} from './types/cerc/auction/message'; } from './types/cerc/auction/message';
import { MessageMsgSendCoins } from './types/cosmos/bank/message'; import { MessageMsgSendCoins } from './types/cosmos/bank/message';
import { MessageMsgOnboardParticipant } from './types/cerc/onboarding/message'; import { MessageMsgOnboardParticipant } from './types/cerc/onboarding/message';
@ -507,10 +507,11 @@ export class Registry {
{ {
commitsDuration, commitsDuration,
revealsDuration, revealsDuration,
denom,
commitFee, commitFee,
revealFee, revealFee,
minimumBid minimumBid
}: MsgCreateVickreyAuction, }: MessageCreateVickreyAuction,
privateKey: string, privateKey: string,
fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER
): Promise<MsgCreateAuctionResponse> { ): Promise<MsgCreateAuctionResponse> {
@ -523,10 +524,11 @@ export class Registry {
AUCTION_KIND_VICKREY, AUCTION_KIND_VICKREY,
commitsDuration, commitsDuration,
revealsDuration, revealsDuration,
denom,
commitFee, commitFee,
revealFee, revealFee,
minimumBid, minimumBid,
undefined, '',
0, 0,
fee fee
); );
@ -536,11 +538,12 @@ export class Registry {
{ {
commitsDuration, commitsDuration,
revealsDuration, revealsDuration,
denom,
commitFee, commitFee,
revealFee, revealFee,
maxPrice, maxPrice,
numProviders numProviders
}: MsgCreateProviderAuction, }: MessageCreateProviderAuction,
privateKey: string, privateKey: string,
fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER fee: StdFee | number = DEFAULT_GAS_ESTIMATION_MULTIPLIER
): Promise<MsgCreateAuctionResponse> { ): Promise<MsgCreateAuctionResponse> {
@ -553,9 +556,10 @@ export class Registry {
AUCTION_KIND_PROVIDER, AUCTION_KIND_PROVIDER,
commitsDuration, commitsDuration,
revealsDuration, revealsDuration,
denom,
commitFee, commitFee,
revealFee, revealFee,
undefined, '',
maxPrice, maxPrice,
numProviders, numProviders,
fee fee

View File

@ -1,3 +1,4 @@
import Long from 'long';
import { GeneratedType, OfflineSigner, Registry } from '@cosmjs/proto-signing'; import { GeneratedType, OfflineSigner, Registry } from '@cosmjs/proto-signing';
import { import {
@ -418,12 +419,13 @@ export class LaconicClient extends SigningStargateClient {
public async createAuction ( public async createAuction (
signer: string, signer: string,
kind: string, kind: string,
commitsDuration: Duration, commitsDuration: string,
revealsDuration: Duration, revealsDuration: string,
commitFee: Coin, denom: string,
revealFee: Coin, commitFee: string,
minimumBid: Coin | undefined, revealFee: string,
maxPrice: Coin | undefined, minimumBid: string,
maxPrice: string,
numProviders: number, numProviders: number,
fee: StdFee | 'auto' | number, fee: StdFee | 'auto' | number,
memo = '' memo = ''
@ -433,12 +435,12 @@ export class LaconicClient extends SigningStargateClient {
value: { value: {
signer, signer,
kind, kind,
commitsDuration, commitsDuration: Duration.fromPartial({ seconds: Long.fromString(commitsDuration) }),
revealsDuration, revealsDuration: Duration.fromPartial({ seconds: Long.fromString(revealsDuration) }),
commitFee, commitFee: Coin.fromPartial({ amount: commitFee, denom }),
revealFee, revealFee: Coin.fromPartial({ amount: revealFee, denom }),
minimumBid, minimumBid: Coin.fromPartial({ amount: minimumBid, denom }),
maxPrice, maxPrice: Coin.fromPartial({ amount: maxPrice, denom }),
numProviders numProviders
} }
}; };

View File

@ -1,8 +1,6 @@
import { EncodeObject, GeneratedType } from '@cosmjs/proto-signing'; import { EncodeObject, GeneratedType } from '@cosmjs/proto-signing';
import { Coin } from '@cosmjs/amino';
import { MsgCommitBidResponse, MsgCommitBid, MsgRevealBid, MsgRevealBidResponse, MsgCreateAuction, MsgCreateAuctionResponse } from '../../../proto/cerc/auction/v1/tx'; import { MsgCommitBidResponse, MsgCommitBid, MsgRevealBid, MsgRevealBidResponse, MsgCreateAuction, MsgCreateAuctionResponse } from '../../../proto/cerc/auction/v1/tx';
import { Duration } from '../../../proto/google/protobuf/duration';
export const typeUrlMsgCreateAuction = '/cerc.auction.v1.MsgCreateAuction'; export const typeUrlMsgCreateAuction = '/cerc.auction.v1.MsgCreateAuction';
export const typeUrlMsgCommitBid = '/cerc.auction.v1.MsgCommitBid'; export const typeUrlMsgCommitBid = '/cerc.auction.v1.MsgCommitBid';
@ -45,19 +43,21 @@ export interface MessageMsgRevealBid {
reveal: string, reveal: string,
} }
export interface MsgCreateVickreyAuction { export interface MessageCreateVickreyAuction {
commitsDuration: Duration; commitsDuration: string;
revealsDuration: Duration; revealsDuration: string;
commitFee: Coin; denom: string;
revealFee: Coin; commitFee: string;
minimumBid: Coin; revealFee: string;
minimumBid: string;
} }
export interface MsgCreateProviderAuction { export interface MessageCreateProviderAuction {
commitsDuration: Duration; commitsDuration: string;
revealsDuration: Duration; revealsDuration: string;
commitFee: Coin; denom: string;
revealFee: Coin; commitFee: string;
maxPrice: Coin; revealFee: string;
maxPrice: string;
numProviders: number; numProviders: number;
} }