Implement commands for bond operations

This commit is contained in:
nabarun 2022-04-25 17:01:57 +05:30 committed by Ashwin Phatak
parent 46a6389d58
commit 1133ac774c
7 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util';
export const command = 'cancel';
export const desc = 'Cancel bond.';
export const handler = async (argv: Arguments) => {
const id = argv.id as string
assert(id, 'Invalid Bond ID.');
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 result = await registry.cancelBond({ id }, privateKey, fee);
console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -0,0 +1,39 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util';
export const command = 'create';
export const desc = 'Create bond.';
export const builder = {
type: {
type: 'string'
},
quantity: {
type: 'string'
}
}
export const handler = async (argv: Arguments) => {
const { config, verbose } = argv;
const denom = argv.type as string;
const amount = argv.quantity as string;
assert(denom, 'Invalid Type.');
assert(amount, 'Invalid Quantity.');
const { services: { cns: cnsConfig } } = getConfig(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 result = await registry.createBond({ denom, amount }, privateKey, fee);
console.log(verbose ? JSON.stringify(result, undefined, 2) : result.data);
}

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';
export const desc = 'Get bond.';
export const handler = async (argv: Arguments) => {
const { id, config } = argv;
console.assert(id, 'Bond Id is required.');
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.getBondsByIds([id as string]);
console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -0,0 +1,29 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo } from '../../../util';
export const command = 'list';
export const desc = 'List bonds.';
export const builder = {
owner: {
type: 'string'
}
}
export const handler = async (argv: Arguments) => {
const { services: { cns: cnsConfig } } = getConfig(argv.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 { owner } = argv;
const result = await registry.queryBonds({ owner });
console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -0,0 +1,40 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util';
export const command = 'refill';
export const desc = 'Refill bond.';
export const builder = {
type: {
type: 'string'
},
quantity: {
type: 'string'
}
}
export const handler = async (argv: Arguments) => {
const denom = argv.type as string;
const amount = argv.quantity as string;
const id = argv.id as string
assert(id, 'Invalid Bond ID.');
assert(denom, 'Invalid Type.');
assert(amount, 'Invalid Quantity.');
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 result = await registry.refillBond({ id, denom, amount }, privateKey, fee);
console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -0,0 +1,40 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util';
export const command = 'withdraw';
export const desc = 'Withdraw funds from bond.';
export const builder = {
type: {
type: 'string'
},
quantity: {
type: 'string'
}
}
export const handler = async (argv: Arguments) => {
const denom = argv.type as string;
const amount = argv.quantity as string;
const id = argv.id as string
assert(id, 'Invalid Bond ID.');
assert(denom, 'Invalid Type.');
assert(amount, 'Invalid Quantity.');
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 result = await registry.withdrawBond({ id, denom, amount }, privateKey, fee);
console.log(JSON.stringify(result, undefined, 2));
}

10
src/cmds/cns-cmds/bond.ts Normal file
View File

@ -0,0 +1,10 @@
import yargs from 'yargs';
export const command = 'bond';
export const desc = 'Bonds operations.';
exports.builder = (yargs: yargs.Argv) => {
return yargs.commandDir('bond-cmds')
.demandCommand();
}