From 46a6389d589d69a306fb2690dfca108f45159cc2 Mon Sep 17 00:00:00 2001 From: nabarun Date: Mon, 25 Apr 2022 15:06:45 +0530 Subject: [PATCH] Implement commands for account and token operations --- package.json | 2 +- src/cmds/cns-cmds/account-cmds/get.ts | 28 +++++++++++++++++ src/cmds/cns-cmds/account.ts | 10 ++++++ src/cmds/cns-cmds/record.ts | 1 + src/cmds/cns-cmds/tokens-cmds/send.ts | 44 +++++++++++++++++++++++++++ src/cmds/cns-cmds/tokens.ts | 10 ++++++ src/cmds/cns.ts | 2 ++ yarn.lock | 4 +-- 8 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/cmds/cns-cmds/account-cmds/get.ts create mode 100644 src/cmds/cns-cmds/account.ts create mode 100644 src/cmds/cns-cmds/tokens-cmds/send.ts create mode 100644 src/cmds/cns-cmds/tokens.ts diff --git a/package.json b/package.json index bd61f46..f306526 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "typescript": "^4.6.3" }, "dependencies": { - "chiba-clonk-client": "https://github.com/vulcanize/chiba-clonk-client.git#646e44c1d29bc312834604ada7088e955dbc2303", + "chiba-clonk-client": "https://github.com/vulcanize/chiba-clonk-client.git#cf7e47cde72e6807244bae7101aeb25342fc5588", "js-yaml": "^4.1.0", "lodash": "^4.17.21", "lodash-clean": "^2.2.3", diff --git a/src/cmds/cns-cmds/account-cmds/get.ts b/src/cmds/cns-cmds/account-cmds/get.ts new file mode 100644 index 0000000..ab86588 --- /dev/null +++ b/src/cmds/cns-cmds/account-cmds/get.ts @@ -0,0 +1,28 @@ +import { Arguments } from 'yargs'; +import assert from 'assert'; +import { Account, Registry } from 'chiba-clonk-client'; + +import { getConfig, getConnectionInfo } from '../../../util'; + +export const command = 'get'; + +export const desc = 'Get account.'; + +export const handler = async (argv: Arguments) => { + let address = argv.address as string; + + 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(chainId, 'Invalid CNS Chain ID.'); + + if (!address && privateKey) { + address = new Account(Buffer.from(privateKey, 'hex')).getCosmosAddress(); + } + + const registry = new Registry(restEndpoint, gqlEndpoint, chainId); + const result = await registry.getAccounts([address]); + + console.log(JSON.stringify(result, undefined, 2)); +} diff --git a/src/cmds/cns-cmds/account.ts b/src/cmds/cns-cmds/account.ts new file mode 100644 index 0000000..58babe1 --- /dev/null +++ b/src/cmds/cns-cmds/account.ts @@ -0,0 +1,10 @@ +import yargs from 'yargs'; + +export const command = 'account'; + +export const desc = 'Account operations.'; + +exports.builder = (yargs: yargs.Argv) => { + return yargs.commandDir('account-cmds') + .demandCommand(); +} diff --git a/src/cmds/cns-cmds/record.ts b/src/cmds/cns-cmds/record.ts index 56eb660..90050df 100644 --- a/src/cmds/cns-cmds/record.ts +++ b/src/cmds/cns-cmds/record.ts @@ -6,4 +6,5 @@ export const desc = 'Record operations.'; exports.builder = (yargs: yargs.Argv) => { return yargs.commandDir('record-cmds') + .demandCommand() } diff --git a/src/cmds/cns-cmds/tokens-cmds/send.ts b/src/cmds/cns-cmds/tokens-cmds/send.ts new file mode 100644 index 0000000..1025710 --- /dev/null +++ b/src/cmds/cns-cmds/tokens-cmds/send.ts @@ -0,0 +1,44 @@ +import { Arguments } from 'yargs'; +import assert from 'assert'; +import { Account, Registry } from 'chiba-clonk-client'; + +import { getConfig, getConnectionInfo, getGasAndFees } from '../../../util'; + +export const command = 'send'; + +export const desc = 'Send tokens.'; + +export const builder = { + type: { + type: 'string' + }, + quantity: { + type: 'string' + } +} + +export const handler = async (argv: Arguments) => { + const destinationAddress = argv.address as string; + const denom = argv.type as string; + const amount = argv.quantity as string; + + assert(destinationAddress, 'Invalid Address.'); + 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 account = new Account(Buffer.from(privateKey, 'hex')); + const fromAddress = account.formattedCosmosAddress; + + const registry = new Registry(restEndpoint, gqlEndpoint, chainId); + const fee = getGasAndFees(argv, cnsConfig); + await registry.sendCoins({ denom, amount, destinationAddress }, privateKey, fee); + const result = await registry.getAccounts([fromAddress, destinationAddress]); + console.log(JSON.stringify(result, undefined, 2)); +} diff --git a/src/cmds/cns-cmds/tokens.ts b/src/cmds/cns-cmds/tokens.ts new file mode 100644 index 0000000..7c82de6 --- /dev/null +++ b/src/cmds/cns-cmds/tokens.ts @@ -0,0 +1,10 @@ +import yargs from 'yargs'; + +export const command = 'tokens'; + +export const desc = 'Tokens operations.'; + +exports.builder = (yargs: yargs.Argv) => { + return yargs.commandDir('tokens-cmds') + .demandCommand(); +} diff --git a/src/cmds/cns.ts b/src/cmds/cns.ts index 9958305..3255ad0 100644 --- a/src/cmds/cns.ts +++ b/src/cmds/cns.ts @@ -13,9 +13,11 @@ exports.builder = (yargs: yargs.Argv) => { 'chain-id': { type: 'string' }, 'filename': { alias: 'f' }, 'id': { type: 'string' }, + 'address': { type: 'string' }, 'gas': { type: 'string' }, 'fees': { type: 'string' } }) .commandDir('cns-cmds') + .demandCommand() .help() } diff --git a/yarn.lock b/yarn.lock index 72e8def..0f8eaea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -714,9 +714,9 @@ chalk@^2.4.1: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -"chiba-clonk-client@https://github.com/vulcanize/chiba-clonk-client.git#646e44c1d29bc312834604ada7088e955dbc2303": +"chiba-clonk-client@https://github.com/vulcanize/chiba-clonk-client.git#cf7e47cde72e6807244bae7101aeb25342fc5588": version "0.1.0" - resolved "https://github.com/vulcanize/chiba-clonk-client.git#646e44c1d29bc312834604ada7088e955dbc2303" + resolved "https://github.com/vulcanize/chiba-clonk-client.git#cf7e47cde72e6807244bae7101aeb25342fc5588" dependencies: "@cosmjs/amino" "^0.28.1" "@cosmjs/crypto" "^0.28.1"