From 617a49fa1249e466b1f144d58701d379d882e510 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 19 Aug 2024 19:31:00 -0500 Subject: [PATCH 1/4] Add gettx command and have send command return the tx hash. --- src/cmds/registry-cmds/tokens-cmds/gettx.ts | 54 +++++++++++++++++++++ src/cmds/registry-cmds/tokens-cmds/send.ts | 37 ++++++++++++-- 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/cmds/registry-cmds/tokens-cmds/gettx.ts diff --git a/src/cmds/registry-cmds/tokens-cmds/gettx.ts b/src/cmds/registry-cmds/tokens-cmds/gettx.ts new file mode 100644 index 0000000..61df54d --- /dev/null +++ b/src/cmds/registry-cmds/tokens-cmds/gettx.ts @@ -0,0 +1,54 @@ +import { Arguments } from 'yargs'; +import assert from 'assert'; +import { Account, Registry } from '@cerc-io/registry-sdk'; + +import { getConfig, getConnectionInfo, queryOutput } from '../../../util'; +import { IndexedTx } from '@cosmjs/stargate/build/stargateclient'; + +export const command = 'gettx'; + +export const desc = 'Get token transfer tx info.'; + +export const builder = { + hash: { + type: 'string' + } +}; + +export const handler = async (argv: Arguments) => { + const hash = argv.hash as string; + + assert(hash, 'Invalid tx hash.'); + + const { services: { registry: registryConfig } } = getConfig(argv.config as string); + const { rpcEndpoint, gqlEndpoint, privateKey, chainId } = getConnectionInfo(argv, registryConfig); + assert(rpcEndpoint, 'Invalid registry RPC endpoint.'); + assert(gqlEndpoint, 'Invalid registry GQL endpoint.'); + assert(privateKey, 'Invalid Transaction Key.'); + assert(chainId, 'Invalid registry Chain ID.'); + + const account = new Account(Buffer.from(privateKey, 'hex')); + await account.init(); + + const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId); + const laconicClient = await registry.getLaconicClient(account); + + const txResponse: IndexedTx | null = await laconicClient.getTx(hash); + if (txResponse) { + const transfer = txResponse.events.find(e => e.type === 'transfer' ? e.attributes.find(a => a.key === 'msg_index') : null); + const output = { + hash: txResponse.hash, + height: txResponse.height, + index: txResponse.txIndex, + code: txResponse.code, + log: txResponse.rawLog, + sender: transfer?.attributes.find(a => a.key === 'sender')?.value, + recipient: transfer?.attributes.find(a => a.key === 'recipient')?.value, + amount: transfer?.attributes.find(a => a.key === 'amount')?.value, + raw: Buffer.from(txResponse.tx).toString('hex').toUpperCase() + }; + queryOutput(output, argv.output); + } else { + queryOutput(null, argv.output); + } +}; diff --git a/src/cmds/registry-cmds/tokens-cmds/send.ts b/src/cmds/registry-cmds/tokens-cmds/send.ts index 0aa6dbe..e11733f 100644 --- a/src/cmds/registry-cmds/tokens-cmds/send.ts +++ b/src/cmds/registry-cmds/tokens-cmds/send.ts @@ -3,6 +3,7 @@ import assert from 'assert'; import { Account, Registry } from '@cerc-io/registry-sdk'; import { getConfig, getConnectionInfo, getGasAndFees, queryOutput } from '../../../util'; +import { DeliverTxResponse } from '@cosmjs/stargate'; export const command = 'send'; @@ -38,8 +39,38 @@ export const handler = async (argv: Arguments) => { const fromAddress = account.address; const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId); + const laconicClient = await registry.getLaconicClient(account); const fee = getGasAndFees(argv, registryConfig); - await registry.sendCoins({ denom, amount, destinationAddress }, privateKey, fee); - const result = await registry.getAccounts([fromAddress, destinationAddress]); - queryOutput(result, argv.output); + + const txResponse: DeliverTxResponse = await laconicClient.sendTokens( + account.address, + destinationAddress, + [ + { + denom, + amount + } + ], + fee); + + assert(txResponse.code === 0, `TX Failed - Hash: ${txResponse.transactionHash}, Code: ${txResponse.code}, Message: ${txResponse.rawLog}`); + + const transfer = txResponse.events.find(e => e.type === 'transfer' ? e.attributes.find(a => a.key === 'msg_index') : null); + const accountResponse = await registry.getAccounts([fromAddress, destinationAddress]); + + const output = { + tx: { + hash: txResponse.transactionHash, + height: txResponse.height, + index: txResponse.txIndex, + code: txResponse.code, + log: txResponse.rawLog, + sender: transfer?.attributes.find(a => a.key === 'sender')?.value, + recipient: transfer?.attributes.find(a => a.key === 'recipient')?.value, + amount: transfer?.attributes.find(a => a.key === 'amount')?.value + }, + balances: accountResponse + }; + + queryOutput(output, argv.output); }; -- 2.45.2 From 3a02e91413aa8b8adf8b805354c362c108071f28 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 19 Aug 2024 21:52:33 -0500 Subject: [PATCH 2/4] tokens send test --- src/cmds/registry-cmds/tokens-cmds/send.ts | 2 +- test/cli.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cmds/registry-cmds/tokens-cmds/send.ts b/src/cmds/registry-cmds/tokens-cmds/send.ts index e11733f..7523ccf 100644 --- a/src/cmds/registry-cmds/tokens-cmds/send.ts +++ b/src/cmds/registry-cmds/tokens-cmds/send.ts @@ -69,7 +69,7 @@ export const handler = async (argv: Arguments) => { recipient: transfer?.attributes.find(a => a.key === 'recipient')?.value, amount: transfer?.attributes.find(a => a.key === 'amount')?.value }, - balances: accountResponse + accounts: accountResponse }; queryOutput(output, argv.output); diff --git a/test/cli.test.ts b/test/cli.test.ts index 8718bb2..44b71ea 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -217,8 +217,8 @@ describe('Test laconic CLI commands', () => { getAccountObj({ address: testAccount2, balance: sendAmount }) ]; - expect(outputObj.length).toEqual(2); - expect(outputObj).toMatchObject(expectedAccounts); + expect(outputObj.accounts.length).toEqual(2); + expect(outputObj.accounts).toMatchObject(expectedAccounts); }); }); -- 2.45.2 From b0fd95cf114b5b46c6e8dfaecdc2dd8b0b0c2c35 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 19 Aug 2024 21:56:15 -0500 Subject: [PATCH 3/4] Check tx details --- test/cli.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/cli.test.ts b/test/cli.test.ts index 44b71ea..e3d715d 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -217,6 +217,10 @@ describe('Test laconic CLI commands', () => { getAccountObj({ address: testAccount2, balance: sendAmount }) ]; + expect(outputObj.tx.code).toEqual(0); + expect(outputObj.tx.amount).toEqual(`${sendAmount}${TOKEN_TYPE}`); + expect(outputObj.tx.sender).toEqual(testAccount); + expect(outputObj.tx.recipient).toEqual(testAccount2); expect(outputObj.accounts.length).toEqual(2); expect(outputObj.accounts).toMatchObject(expectedAccounts); }); -- 2.45.2 From 7708406771bad5db5aee54c2b81d1f8a00d6703c Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 19 Aug 2024 22:00:05 -0500 Subject: [PATCH 4/4] gettx test --- test/cli.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/cli.test.ts b/test/cli.test.ts index e3d715d..5d84875 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -224,6 +224,22 @@ describe('Test laconic CLI commands', () => { expect(outputObj.accounts.length).toEqual(2); expect(outputObj.accounts).toMatchObject(expectedAccounts); }); + test('laconic registry tokens gettx --hash ', async () => { + const sendAmount = 1000000000; + + const sendResult = spawnSync('laconic', ['registry', 'tokens', 'send', '--address', testAccount2, '--type', TOKEN_TYPE, '--quantity', sendAmount.toString()]); + const sendOutput = checkResultAndRetrieveOutput(sendResult); + expect(sendOutput.tx.code).toEqual(0); + + const gettxResult = spawnSync('laconic', ['registry', 'tokens', 'gettx', '--hash', sendOutput.tx.hash]); + const gettxOutput = checkResultAndRetrieveOutput(gettxResult); + + expect(gettxOutput.hash).toEqual(sendOutput.tx.hash); + expect(gettxOutput.code).toEqual(0); + expect(gettxOutput.amount).toEqual(`${sendAmount}${TOKEN_TYPE}`); + expect(gettxOutput.sender).toEqual(testAccount); + expect(gettxOutput.recipient).toEqual(testAccount2); + }); }); describe('Record operations', () => { -- 2.45.2