Implement commands for record operations

This commit is contained in:
nabarun 2022-04-25 11:57:40 +05:30 committed by Ashwin Phatak
parent 977d9a8ffa
commit 97dbbf87c0
17 changed files with 1615 additions and 19 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules/
dist/*
config.yml

7
config.example.yml Normal file
View File

@ -0,0 +1,7 @@
services:
cns:
restEndpoint: 'http://localhost:1317'
gqlEndpoint: 'http://localhost:9473/api'
userKey:
bondId:
chainId: chibaclonk_9000-1

View File

@ -6,11 +6,17 @@
"author": "",
"license": "UNLICENSED",
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.182",
"@types/node": "^17.0.25",
"@types/yargs": "^17.0.10",
"typescript": "^4.6.3"
},
"dependencies": {
"chiba-clonk-client": "https://github.com/vulcanize/chiba-clonk-client.git#646e44c1d29bc312834604ada7088e955dbc2303",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"lodash-clean": "^2.2.3",
"yargs": "^17.4.1"
},
"scripts": {

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 record.';
export const handler = async (argv: Arguments) => {
const { id, config } = argv;
assert(id, 'Invalid Record 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.getRecordsByIds([id as string]);
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 } from '../../../util';
export const command = 'list';
export const desc = 'List records.';
export const builder = {
'bond-id': {
type: 'string'
},
type: {
type: 'string'
},
name: {
type: 'string'
},
all: {
type: 'boolean',
default: false
}
}
export const handler = async (argv: Arguments) => {
const { services: { cns: cnsConfig } } = getConfig(argv.config as string)
const { restEndpoint, gqlEndpoint, chainId } = getConnectionInfo(argv, cnsConfig);
const { type, name, bondId, all } = argv;
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.queryRecords({ bondId, type, name }, all as boolean);
console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -0,0 +1,44 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import path from 'path';
import yaml from 'js-yaml';
import fs from 'fs';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getGasAndFees, getConnectionInfo } from '../../../util';
export const command = 'publish';
export const desc = 'Register record.';
export const builder = {
'bond-id': {
type: 'string'
},
}
export const handler = async (argv: Arguments) => {
const { txKey, filename, verbose, config } = argv;
const { services: { cns: cnsConfig } } = getConfig(config as string)
const { restEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, cnsConfig);
assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(userKey, 'Invalid User Key.');
assert(bondId, 'Invalid Bond ID.');
assert(chainId, 'Invalid CNS Chain ID.');
let file = null;
if (filename) {
file = path.join(process.cwd(), filename as string);
} else {
file = 0; // stdin
}
const { record } = await yaml.load(fs.readFileSync(file, 'utf-8')) as any;
const registry = new Registry(restEndpoint, gqlEndpoint, chainId);
const fee = getGasAndFees(argv, cnsConfig);
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, txKey as string, fee);
console.log(verbose ? JSON.stringify(result, undefined, 2) : result.data);
}

View File

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

View File

@ -1,17 +0,0 @@
import yargs, { Arguments } from 'yargs';
export const command = 'start';
export const desc = 'Start local CNS.';
exports.builder = {
'log-file': {
type: 'string'
}
}
exports.handler = function (argv: Arguments) {
const { logFile } = argv;
console.log('option logFile', logFile)
}

View File

@ -0,0 +1,24 @@
import { Arguments } from 'yargs';
import assert from 'assert';
import { Registry } from 'chiba-clonk-client';
import { getConfig, getConnectionInfo } from '../../util';
export const command = 'status';
export const desc = 'Get CNS status.';
export const handler = async (argv: Arguments) => {
const { services: { cns } } = getConfig(argv.config as string)
const { restEndpoint, gqlEndpoint, chainId } = getConnectionInfo(argv, cns);
assert(restEndpoint, 'Invalid CNS REST endpoint.');
assert(gqlEndpoint, 'Invalid CNS GQL endpoint.');
assert(chainId, 'Invalid CNS Chain ID.');
console.log('parse argv', restEndpoint, gqlEndpoint, chainId)
const registry = new Registry(restEndpoint, gqlEndpoint, chainId);
// TODO: Implement getStatus method.
// const result = await registry.getStatus();
// console.log(JSON.stringify(result, undefined, 2));
}

View File

@ -5,5 +5,17 @@ export const command = 'cns';
export const desc = 'CNS tools';
exports.builder = (yargs: yargs.Argv) => {
return yargs.commandDir('cns-cmds')
return yargs
.options({
'user-key': { type: 'string' },
'tx-key': { type: 'string' },
'bond-id': { type: 'string' },
'chain-id': { type: 'string' },
'filename': { alias: 'f' },
'id': { type: 'string' },
'gas': { type: 'string' },
'fees': { type: 'string' }
})
.commandDir('cns-cmds')
.help()
}

1
src/decs.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module 'lodash-clean'

View File

@ -2,6 +2,21 @@ import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
yargs(hideBin(process.argv))
.options({
verbose: {
description: 'Verbose output',
demand: false,
default: false,
type: 'boolean',
alias: 'v'
},
config: {
alias: 'c',
default: 'config.yml',
describe: 'Config file path.',
type: 'string'
}
})
.commandDir('cmds')
.demandCommand()
.help()

16
src/util/common.ts Normal file
View File

@ -0,0 +1,16 @@
import { Arguments } from "yargs";
import clean from 'lodash-clean';
export const getConnectionInfo = (argv: Arguments, config: any) => {
const { server, userKey, bondId, txKey, chainId, fees, gas } = argv;
const result = {
...config,
...clean({ server, userKey, bondId, txKey, chainId }),
privateKey: txKey || userKey || config.userKey,
gas: String(gas || config.gas),
fees: String(fees || config.fees)
};
return result;
};

9
src/util/config.ts Normal file
View File

@ -0,0 +1,9 @@
import yaml from 'js-yaml'
import fs from 'fs'
import path from 'path'
export const getConfig = (configFilePath: string): any => {
const resolvedFilePath = path.resolve(process.cwd(), configFilePath);
const configFile = fs.readFileSync(resolvedFilePath, 'utf-8')
return yaml.load(configFile);
};

20
src/util/fees.ts Normal file
View File

@ -0,0 +1,20 @@
import assert from 'assert';
import { Arguments } from 'yargs';
export const parseGasAndFees = (gas: string, fees = '') => {
assert(gas, 'Invalid gas.');
const [{ amount, denom }] = fees.trim().split(',')
.map(fee => fee.trim().split(/(\d+)/))
.filter(([_, amount, denom]) => (denom && amount))
.map(([_, amount, denom]) => ({ denom, amount }));
return { amount, denom, gas };
};
export const getGasAndFees = (argv: Arguments, config: any = {}) => {
return parseGasAndFees(
String(argv.gas || config.gas),
String(argv.fees || config.fees)
);
};

3
src/util/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './fees';
export * from './config';
export * from './common';

1382
yarn.lock

File diff suppressed because it is too large Load Diff