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
5 changed files with 181 additions and 47 deletions
Showing only changes of commit d27f9ae43d - Show all commits

View File

@ -660,26 +660,29 @@ Reassociate records (switch bond):
laconic registry bond records reassociate --old-bond-id 5c40abd336ae1561f2a1b55be73b12f5a083080bf879b4c9288d182d238badb0 --new-bond-id 3e11c61f179897e4b12e9b63de35d36f88ac146755e7a28ce0bcdd07cf3a03ae
```
Create `provider` auction:
Create a `provider` auction:
```bash
laconic registry auction create --kind provider --commits-duration 60 --reveals-duration 60 --commit-fee 1000 --reveal-fee 1000 --max-price 100000 --num-providers 2
laconic registry auction create --kind provider --commits-duration 60 --reveals-duration 60 --denom alnt --commit-fee 1000 --reveal-fee 1000 --max-price 100000 --num-providers 1
{"auctionId":"73c5fa4b91bb973641ccbb6901a8404745fb8793c95485b00d5a791e6b6c1630"}
# Set auction id in a variable
AUCTION=
```
Commit an auction bid:
```bash
laconic registry auction bid commit 73c5fa4b91bb973641ccbb6901a8404745fb8793c95485b00d5a791e6b6c1630 25000000 alnt
laconic registry auction bid commit $AUCTION 25000 alnt
{"reveal_file":"./out/bafyreiai5upey4562ont54pe7m3buiphtd6n3q2vr5lxdcj3gpyklbbgvy.json"}
{"reveal_file":"/home/user/laconic-registry-cli/out/bafyreiai5upey4562ont54pe7m3buiphtd6n3q2vr5lxdcj3gpyklbbgvy.json"}
```
Reveal an auction bid:
```bash
laconic registry auction bid reveal b66b74048fc360de6a926123b760e6485276d90ad2274b5386c02664cd04bace ./out/bafyreifjkhiakayvvaasnsw7ufaax54ncow4xuycqnox7hxay34c6yod7a.json
laconic registry auction bid reveal $AUCTION /home/user/laconic-registry-cli/out/bafyreiai5upey4562ont54pe7m3buiphtd6n3q2vr5lxdcj3gpyklbbgvy.json
{"success": true}
```
@ -687,7 +690,7 @@ laconic registry auction bid reveal b66b74048fc360de6a926123b760e6485276d90ad227
Check the auction state on completion:
```bash
laconic registry auction get b66b74048fc360de6a926123b760e6485276d90ad2274b5386c02664cd04bace
laconic registry auction get $AUCTION
[
{
@ -716,18 +719,18 @@ laconic registry auction get b66b74048fc360de6a926123b760e6485276d90ad2274b5386c
"winnerBids": [
{
"type": "alnt",
"quantity": 26000
"quantity": 25000
}
],
"winnerPrice": {
"type": "alnt",
"quantity": 26000
"quantity": 25000
},
"maxPrice": {
"type": "alnt",
"quantity": 100000
},
"numProviders": 2,
"numProviders": 1,
"bids": [
{
"bidderAddress": "laconic13qrlfkgl02wgwpw0n4j8kswygwnukphy92249r",
@ -745,7 +748,7 @@ laconic registry auction get b66b74048fc360de6a926123b760e6485276d90ad2274b5386c
},
"bidAmount": {
"type": "alnt",
"quantity": 26000
"quantity": 25000
}
}
]

View File

@ -16,19 +16,19 @@ export const desc = 'Create auction.';
export const builder = {
kind: {
type: 'string',
describe: 'Type of auction (vickrey | provider)'
describe: 'Auction kind (vickrey | provider)'
},
'commits-duration': {
type: 'number',
describe: 'Duration for commits phase in seconds'
describe: 'Duration for bid commit phase in seconds'
},
'reveals-duration': {
type: 'number',
describe: 'Duration for reveals phase in seconds'
describe: 'Duration for bid reveal phase in seconds'
},
denom: {
type: 'string',
describe: 'Denom'
describe: 'Denom to use'
},
'commit-fee': {
type: 'number',
@ -36,19 +36,21 @@ export const builder = {
},
'reveal-fee': {
type: 'number',
describe: 'Fee for revealing bids in the auction'
describe: 'Fee for revealing a bid in the auction'
},
'minimum-bid': {
type: 'number',
default: 0,
describe: 'Minimum bid amount (only for vickrey auction)'
},
'max-price': {
type: 'number',
describe: 'Maximum price (only for provider auction)'
default: 0,
describe: 'Max acceptable bid price (only for provider auction)'
},
'num-providers': {
type: 'number',
describe: 'Number of providers (only for provider auction)'
describe: 'Number ofdesired providers (only for provider auction)'
}
};
@ -56,14 +58,24 @@ export const handler = async (argv: Arguments) => {
const { config } = argv;
const kind = argv.kind as string;
const commitsDuration = Duration.fromPartial({
seconds: Long.fromNumber(argv.commitsDuration as number),
nanos: 0
});
const revealsDuration = Duration.fromPartial({
seconds: Long.fromNumber(argv.revealsDuration as number),
nanos: 0
});
if (kind === AUCTION_KIND_VICKREY) {
assert(argv.minimumBid, 'Invalid minimum bid.');
assert(!argv.maxPrice, `Max price can only be used with ${AUCTION_KIND_PROVIDER} auction.`);
assert(!argv.numProviders, `Num providers can only be used with ${AUCTION_KIND_PROVIDER} auction.`);
} else if (kind === AUCTION_KIND_PROVIDER) {
assert(argv.maxPrice, 'Invalid max price.');
assert(argv.numProviders, 'Invalid num providers.');
assert(!argv.minimumBid, `Minimum bid can only be used with ${AUCTION_KIND_VICKREY} auction.`);
}
assert(argv.commitsDuration, 'Invalid commits duration.');
assert(argv.revealsDuration, 'Invalid reveals duration.');
assert(argv.commitFee, 'Invalid commit fee.');
assert(argv.revealFee, 'Invalid reveal fee.');
const commitsDuration = Duration.fromPartial({ seconds: Long.fromNumber(argv.commitsDuration as number) });
const revealsDuration = Duration.fromPartial({ seconds: Long.fromNumber(argv.revealsDuration as number) });
const denom = argv.denom as string;
const commitFee = coin(argv.commitFee as string, denom);
const revealFee = coin(argv.revealFee as string, denom);
@ -71,25 +83,8 @@ export const handler = async (argv: Arguments) => {
const maxPrice = coin(argv.maxPrice as string, denom);
const numProviders = argv.numProviders as number;
const validKinds = [AUCTION_KIND_VICKREY, AUCTION_KIND_PROVIDER];
assert(validKinds.includes(kind), 'Invalid auction kind.');
if (kind === AUCTION_KIND_VICKREY) {
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 validAuctionKinds = [AUCTION_KIND_VICKREY, AUCTION_KIND_PROVIDER];
assert(validAuctionKinds.includes(kind), `Invalid auction kind, has to be one of ${validAuctionKinds}.`);
const { services: { registry: registryConfig } } = getConfig(config as string);
const { rpcEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, registryConfig);

View File

@ -1,4 +1,4 @@
## Run CLI tests
# Run CLI tests
* Follow the project `Setup` and `Account Setup` from root [README](./../README.md)

View File

@ -2,6 +2,8 @@ import fs from 'fs';
import assert from 'assert';
import { spawnSync } from 'child_process';
import { AUCTION_KIND_VICKREY } from '@cerc-io/registry-sdk';
import {
CHAIN_ID,
TOKEN_TYPE,
@ -16,7 +18,8 @@ import {
getAuthorityObj,
getAuctionObj,
getBidObj,
updateGasAndFeesConfig
updateGasAndFeesConfig,
AUCTION_STATUS
} from './helpers';
describe('Test laconic CLI commands', () => {
@ -369,7 +372,7 @@ describe('Test laconic CLI commands', () => {
});
});
describe('Auction operations', () => {
describe('Authority auction operations', () => {
const bidAmount = 25000000;
let bidRevealFilePath: string;
@ -587,6 +590,129 @@ describe('Test laconic CLI commands', () => {
});
});
describe('Vickrey Auction operations', () => {
const commitFee = 1000;
const revealFee = 1000;
const minimumBid = 100000;
const bidAmount = 25000000;
let bidRevealFilePath: string;
test('laconic registry auction create --kind <kind> --commits-duration <commits_duration> --reveals-duration <reveals_duration> --commit-fee <commit_fee> --reveal-fee <reveal_fee> --minimum-bid <minimum_bid>', async () => {
const createAuctionResult = spawnSync('laconic', [
'registry',
'auction',
'create',
'--kind', AUCTION_KIND_VICKREY,
'--commits-duration', AUCTION_COMMIT_DURATION.toString(),
'--reveals-duration', AUCTION_REVEAL_DURATION.toString(),
'--denom', TOKEN_TYPE,
'--commit-fee', commitFee.toString(),
'--reveal-fee', revealFee.toString(),
'--minimum-bid', minimumBid.toString()
]);
const outputObj = checkResultAndRetrieveOutput(createAuctionResult);
expect(outputObj).toHaveProperty('auctionId');
testAuctionId = outputObj.auctionId;
const getAuctionResult = spawnSync('laconic', ['registry', 'auction', 'get', '--id', testAuctionId]);
const auctionOutputObj = checkResultAndRetrieveOutput(getAuctionResult);
const expectedAuctionObjPartial = {
kind: AUCTION_KIND_VICKREY,
status: AUCTION_STATUS.COMMIT,
ownerAddress: testAccount,
commitFee: { quantity: commitFee },
revealFee: { quantity: revealFee },
minimumBid: { quantity: minimumBid },
winnerAddresses: [],
winnerBids: [],
maxPrice: { quantity: 0 },
numProviders: 0,
bids: []
};
expect(auctionOutputObj[0]).toMatchObject(expectedAuctionObjPartial);
});
test('laconic registry auction bid commit <auction_id> <quantity> <type>', async () => {
const result = spawnSync('laconic', ['registry', 'auction', 'bid', 'commit', testAuctionId, bidAmount.toString(), TOKEN_TYPE]);
const outputObj = checkResultAndRetrieveOutput(result);
// Expected output
expect(outputObj.reveal_file).toBeDefined();
bidRevealFilePath = outputObj.reveal_file;
});
test('laconic registry auction bid reveal <auction_id> <file_path>', async () => {
// Wait for auction commits duration (60s)
await delay(AUCTION_COMMIT_DURATION * 1000);
let auctionResult = spawnSync('laconic', ['registry', 'auction', 'get', testAuctionId]);
let auctionOutputObj = checkResultAndRetrieveOutput(auctionResult);
const expectedAuctionObjPartial = {
status: AUCTION_STATUS.REVEAL,
ownerAddress: testAccount,
winnerAddresses: [],
winnerBids: [],
bids: [{
bidderAddress: testAccount,
status: AUCTION_STATUS.COMMIT,
bidAmount: { quantity: 0 }
}]
};
expect(auctionOutputObj[0]).toMatchObject(expectedAuctionObjPartial);
// Reveal bid
const result = spawnSync('laconic', ['registry', 'auction', 'bid', 'reveal', testAuctionId, bidRevealFilePath]);
const outputObj = checkResultAndRetrieveOutput(result);
// Expected output
expect(outputObj).toEqual({ success: true });
const revealObject = JSON.parse(fs.readFileSync(bidRevealFilePath, 'utf8'));
expect(revealObject).toMatchObject({
chainId: CHAIN_ID,
auctionId: testAuctionId,
bidderAddress: testAccount,
bidAmount: `${bidAmount}${TOKEN_TYPE}`
});
auctionResult = spawnSync('laconic', ['registry', 'auction', 'get', testAuctionId]);
auctionOutputObj = checkResultAndRetrieveOutput(auctionResult);
const expectedAuctionObjPartialOnBidReveal = {
status: AUCTION_STATUS.REVEAL,
winnerAddresses: [],
bids: [{
bidderAddress: testAccount,
status: AUCTION_STATUS.REVEAL,
bidAmount: { quantity: bidAmount }
}]
};
expect(auctionOutputObj[0]).toMatchObject(expectedAuctionObjPartialOnBidReveal);
}, (AUCTION_COMMIT_DURATION + 5) * 1000);
test('laconic registry auction get <auction_id>', async () => {
// Wait for auction reveals duration (60s)
await delay(AUCTION_REVEAL_DURATION * 1000);
const auctionResult = spawnSync('laconic', ['registry', 'auction', 'get', testAuctionId]);
const auctionOutputObj = checkResultAndRetrieveOutput(auctionResult);
const expectedAuctionObjPartial = {
status: AUCTION_STATUS.COMPLETED,
ownerAddress: testAccount,
winnerAddresses: [testAccount],
winnerBids: [{ quantity: bidAmount }],
winnerPrice: { quantity: bidAmount }
};
expect(auctionOutputObj[0]).toMatchObject(expectedAuctionObjPartial);
}, (AUCTION_COMMIT_DURATION + 5) * 1000);
});
describe('Gas and fees config', () => {
const bondAmount = 1000;

View File

@ -8,6 +8,12 @@ import { getConfig } from '../src/util';
export const CHAIN_ID = 'laconic_9000-1';
export const TOKEN_TYPE = 'alnt';
export enum AUCTION_STATUS {
COMMIT = 'commit',
REVEAL = 'reveal',
COMPLETED = 'completed'
}
export const AUCTION_FEES = {
commit: 1000000,
reveal: 1000000,
@ -17,6 +23,10 @@ export const AUCTION_COMMIT_DURATION = 60; // 60s
export const AUCTION_REVEAL_DURATION = 60; // 60s
export function checkResultAndRetrieveOutput (result: SpawnSyncReturns<Buffer>): any {
if (result.status !== 0) {
console.log('stderr', result.stderr.toString().trim());
}
expect(result.status).toBe(0);
const errorOutput = result.stderr.toString().trim();