Add commands for auction operations

This commit is contained in:
2022-04-27 15:32:46 +05:30
committed by Ashwin Phatak
parent 7f3b07b394
commit 11482ab5c8
8 changed files with 169 additions and 0 deletions
@@ -0,0 +1,49 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import path from 'path';
import { Account, createBid, Registry } from 'chiba-clonk-client';
import { ensureDir } from 'fs-extra';
import fs from 'fs';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util';
const OUT_DIR = 'out';
export const command = 'commit [auction-id] [quantity] [type]';
export const desc = 'Commit auction bid.';
export const handler = async (argv: Arguments) => {
const auctionId = argv.auctionId as string;
const quantity = argv.quantity as string;
const denom = argv.type as string;
assert(auctionId, 'Invalid auction ID.');
assert(quantity, 'Invalid token quantity.');
assert(denom, 'Invalid token type.');
const { services: { cns: cnsConfig } } = getConfig(argv.config as string)
const { restEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, cnsConfig);
assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(privateKey, 'Invalid Transaction Key.');
assert(chainId, 'Invalid CNS Chain ID.');
const account = new Account(Buffer.from(privateKey, 'hex'));
const bidderAddress = account.formattedCosmosAddress;
const bidAmount = `${quantity}${denom}`;
const { reveal, commitHash } = await createBid(chainId, auctionId, bidderAddress, bidAmount);
// Save reveal file.
const outDirPath = path.join(process.cwd(), OUT_DIR);
const revealFilePath = path.join(outDirPath, `${commitHash}.json`);
await ensureDir(outDirPath);
fs.writeFileSync(revealFilePath, JSON.stringify(reveal, undefined, 2));
const registry = new Registry(restEndpoint, gqlEndpoint, chainId);
const fee = getGasAndFees(argv, cnsConfig);
const result = await registry.commitBid({ auctionId, commitHash }, privateKey, fee);
console.log(JSON.stringify(result, undefined, 2));
console.log(`\nReveal file: ${revealFilePath}`);
}
@@ -0,0 +1,32 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import path from 'path';
import { Registry } from 'chiba-clonk-client';
import fs from 'fs';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../../util';
export const command = 'reveal [auction-id] [file-path]';
export const desc = 'Reveal auction bid.';
export const handler = async (argv: Arguments) => {
const auctionId = argv.auctionId as string;
const filePath = argv.filePath as string;
assert(auctionId, 'Invalid auction ID.');
assert(filePath, 'Invalid reveal file path.');
const { services: { cns: cnsConfig } } = getConfig(argv.config as string)
const { restEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, cnsConfig);
assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(privateKey, 'Invalid Transaction Key.');
assert(chainId, 'Invalid CNS Chain ID.');
const registry = new Registry(restEndpoint, gqlEndpoint, chainId);
const fee = getGasAndFees(argv, cnsConfig);
const reveal = fs.readFileSync(path.resolve(filePath));
const result = await registry.revealBid({ auctionId, reveal: reveal.toString('hex') }, privateKey, fee);
console.log(JSON.stringify(result, undefined, 2));
}
+15
View File
@@ -0,0 +1,15 @@
import yargs from 'yargs';
export const command = 'bid';
export const desc = 'Auction bid operations.';
exports.builder = (yargs: yargs.Argv) => {
return yargs.options({
'auction-id': { type: 'string' },
'type': { type: 'string' },
'quantity': { type: 'string' },
'file-path': { type: 'string' }
}).commandDir('bid-cmds')
.demandCommand();
}
+25
View File
@@ -0,0 +1,25 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo } from '../../../util';
export const command = 'get [id]';
export const desc = 'Get auction information.';
export const handler = async (argv: Arguments) => {
const { id, config } = argv;
assert(id, 'Invalid auction ID.');
const { services: { cns: cnsConfig } } = getConfig(config as string)
const { restEndpoint, gqlEndpoint, chainId } = getConnectionInfo(argv, cnsConfig);
assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(chainId, 'Invalid CNS Chain ID.');
const registry = new Registry(restEndpoint, gqlEndpoint, chainId);
const result = await registry.getAuctionsByIds([id as string]);
console.log(JSON.stringify(result, undefined, 2));
}
+10
View File
@@ -0,0 +1,10 @@
import yargs from 'yargs';
export const command = 'auction';
export const desc = 'Auction operations.';
exports.builder = (yargs: yargs.Argv) => {
return yargs.commandDir('auction-cmds')
.demandCommand();
}