Add command to create an auction and add auction CLI tests #83

Merged
nabarun merged 13 commits from deep-stack/laconic-registry-cli:iv-create-auction-cmds into main 2024-09-25 13:57:56 +00:00
3 changed files with 54 additions and 26 deletions
Showing only changes of commit b18314eef4 - Show all commits

View File

@ -2,10 +2,11 @@ import { Arguments } from 'yargs';
import assert from 'assert'; import assert from 'assert';
import Long from 'long'; import Long from 'long';
import { Account, DENOM, Registry } from '@cerc-io/registry-sdk'; import { Registry } from '@cerc-io/registry-sdk';
import { MsgCreateAuctionResponse } from '@cerc-io/registry-sdk/dist/proto/cerc/auction/v1/tx';
import { getConfig, getConnectionInfo, getGasAndFees, getGasPrice, txOutput } from '../../../util'; import { getConfig, getConnectionInfo, getGasAndFees, getGasPrice, txOutput } from '../../../util';
import { MsgCreateAuctionResponse } from '@cerc-io/registry-sdk/dist/proto/cerc/auction/v1/tx'; import { AUCTION_KIND_VICKREY, AUCTION_KIND_PROVIDER } from '../../../../test/helpers';
export const command = 'create'; export const command = 'create';
@ -13,28 +14,40 @@ export const desc = 'Create auction.';
export const builder = { export const builder = {
kind: { kind: {
type: 'string' type: 'string',
describe: 'Type of auction (vickrey | provider)'
}, },
'commits-duration': { 'commits-duration': {
type: 'number' type: 'number',
describe: 'Duration for commits phase in seconds'
}, },
'reveals-duration': { 'reveals-duration': {
type: 'number' type: 'number',
describe: 'Duration for reveals phase in seconds'
},
denom: {
type: 'string',
describe: 'Denom'
}, },
'commit-fee': { 'commit-fee': {
type: 'string' type: 'number',
describe: 'Fee for committing to the auction'
}, },
'reveal-fee': { 'reveal-fee': {
type: 'string' type: 'number',
describe: 'Fee for revealing bids in the auction'
}, },
'minimum-bid': { 'minimum-bid': {
type: 'string' type: 'number',
describe: 'Minimum bid amount (only for vickrey auction)'
}, },
'max-price': { 'max-price': {
type: 'string' type: 'number',
describe: 'Maximum price (only for provider auction)'
}, },
'num-providers': { 'num-providers': {
type: 'string' type: 'number',
describe: 'Number of providers (only for provider auction)'
} }
}; };
@ -50,29 +63,44 @@ export const handler = async (argv: Arguments) => {
seconds: Long.fromNumber(argv.revealsDuration as number), seconds: Long.fromNumber(argv.revealsDuration as number),
nanos: 0 nanos: 0
}; };
const denom = argv.denom as string;
const commitFee = { const commitFee = {
denom: DENOM, denom: denom,
amount: argv.commitFee as string amount: argv.commitFee as string
}; };
const revealFee = { const revealFee = {
denom: DENOM, denom: denom,
amount: argv.revealFee as string amount: argv.revealFee as string
}; };
const minimumBid = { const minimumBid = {
denom: DENOM, denom: denom,
amount: argv.minimumBid as string amount: argv.minimumBid as string
}; };
const maxPrice = { const maxPrice = {
denom: DENOM, denom: denom,
amount: argv.maxPrice as string amount: argv.maxPrice as string
}; };
const numProviders = argv.numProviders as number; const numProviders = argv.numProviders as number;
assert(kind, 'Invalid auction kind.'); const validKinds = [AUCTION_KIND_VICKREY, AUCTION_KIND_PROVIDER];
assert(commitsDuration, 'Invalid commits duration.'); assert(validKinds.includes(kind), 'Invalid auction kind.');
assert(revealsDuration, 'Invalid reveals duration.');
assert(commitFee, 'Invalid commit fee.'); if (kind === AUCTION_KIND_VICKREY) {
assert(revealFee, 'Invalid reveal fee.'); assert(commitsDuration, 'Invalid commits duration.');
assert(revealsDuration, 'Invalid reveals duration.');
assert(commitFee, 'Invalid commit fee.');
assert(revealFee, 'Invalid reveal fee.');
assert(!argv.maxPrice, 'Max price should not be provided for vickrey auction.');
assert(!argv.numProviders, 'Num providers should not be provided for vickrey auction.');
} else if (kind === AUCTION_KIND_PROVIDER) {
assert(commitsDuration, 'Invalid commits duration.');
assert(revealsDuration, 'Invalid reveals duration.');
assert(commitFee, 'Invalid commit fee.');
assert(revealFee, 'Invalid reveal fee.');
assert(maxPrice, 'Invalid max price.');
assert(numProviders, 'Invalid number of providers.');
assert(!argv.minimumBid, 'Minimum bid should not be provided for provider auction.');
}
const { services: { registry: registryConfig } } = getConfig(config as string); const { services: { registry: registryConfig } } = getConfig(config as string);
const { rpcEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, registryConfig); const { rpcEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, registryConfig);
@ -81,20 +109,16 @@ export const handler = async (argv: Arguments) => {
assert(privateKey, 'Invalid Transaction Key.'); assert(privateKey, 'Invalid Transaction Key.');
assert(chainId, 'Invalid registry Chain ID.'); assert(chainId, 'Invalid registry Chain ID.');
const account = new Account(Buffer.from(privateKey, 'hex'));
await account.init();
const signer = account.address;
const gasPrice = getGasPrice(argv, registryConfig); const gasPrice = getGasPrice(argv, registryConfig);
const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice }); const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice });
const fee = getGasAndFees(argv, registryConfig); const fee = getGasAndFees(argv, registryConfig);
let result: MsgCreateAuctionResponse; let result: MsgCreateAuctionResponse;
if (kind === 'vickrey') { if (kind === AUCTION_KIND_VICKREY) {
result = await registry.createAuction({ commitsDuration, revealsDuration, commitFee, revealFee, minimumBid, signer }, privateKey, fee); result = await registry.createAuction({ commitsDuration, revealsDuration, commitFee, revealFee, minimumBid }, privateKey, fee);
} else { } else {
result = await registry.createProviderAuction({ commitsDuration, revealsDuration, commitFee, revealFee, signer, maxPrice, numProviders }, privateKey, fee); result = await registry.createProviderAuction({ commitsDuration, revealsDuration, commitFee, revealFee, maxPrice, numProviders }, privateKey, fee);
} }
const jsonString = `{"auctionId":"${result.auction?.id}"}`; const jsonString = `{"auctionId":"${result.auction?.id}"}`;

View File

@ -1,5 +1,6 @@
import { Arguments } from 'yargs'; import { Arguments } from 'yargs';
import assert from 'assert'; import assert from 'assert';
import { Registry } from '@cerc-io/registry-sdk'; import { Registry } from '@cerc-io/registry-sdk';
import { getConfig, getConnectionInfo, getGasAndFees, getGasPrice, txOutput } from '../../../util'; import { getConfig, getConnectionInfo, getGasAndFees, getGasPrice, txOutput } from '../../../util';

View File

@ -16,6 +16,9 @@ export const AUCTION_FEES = {
export const AUCTION_COMMIT_DURATION = 60; // 60s export const AUCTION_COMMIT_DURATION = 60; // 60s
export const AUCTION_REVEAL_DURATION = 60; // 60s export const AUCTION_REVEAL_DURATION = 60; // 60s
export const AUCTION_KIND_VICKREY = 'vickrey';
export const AUCTION_KIND_PROVIDER = 'provider';
export function checkResultAndRetrieveOutput (result: SpawnSyncReturns<Buffer>): any { export function checkResultAndRetrieveOutput (result: SpawnSyncReturns<Buffer>): any {
expect(result.status).toBe(0); expect(result.status).toBe(0);