From c67961869b5e2fbd532b4cf2de56f6cf4206daa5 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Tue, 20 Aug 2024 03:19:48 +0000 Subject: [PATCH] 927: Add gettx command and have send command return the tx hash. (#78) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To support https://git.vdb.to/cerc-io/stack-orchestrator/issues/927, add a new token command `gettx` to inspect a previous transaction, and adjust the output of `send` to include the tx details. For example: ``` ❯ laconic registry tokens send --address laconic1yqpc7cyfetpgmqtkk0ukevugeaau9p0cwmjlsu --type alnt --quantity 1000 { "tx": { "hash": "977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC", "height": 343369, "index": 0, "code": 0, "log": "", "sender": "laconic14wc07wa3r7rppru43g9jxkzhqnhmvfm4dgey6s", "recipient": "laconic1yqpc7cyfetpgmqtkk0ukevugeaau9p0cwmjlsu", "amount": "1000alnt" }, "accounts": [ { "address": "laconic14wc07wa3r7rppru43g9jxkzhqnhmvfm4dgey6s", "pubKey": "AvOh0Hdjj5/YKMTPEm/oLgvpg4gIP1vB4d1NhBMq6/+B", "number": 3, "sequence": 91, "balance": [ { "type": "alnt", "quantity": 1.2899999999709944e+22 } ] }, { "address": "laconic1yqpc7cyfetpgmqtkk0ukevugeaau9p0cwmjlsu", "pubKey": "A7XYVHLemQYUjXe6VnSDlcyzLnpdJ8CE8zvDiZtgxqnT", "number": 1, "sequence": 1, "balance": [ { "type": "alnt", "quantity": 1.289999999991e+22 } ] } ] } ``` ``` ❯ laconic registry tokens gettx --hash 977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC { "hash": "977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC", "height": 343369, "index": 0, "code": 0, "log": "", "sender": "laconic14wc07wa3r7rppru43g9jxkzhqnhmvfm4dgey6s", "recipient": "laconic1yqpc7cyfetpgmqtkk0ukevugeaau9p0cwmjlsu", "amount": "1000alnt", "raw": "0A91010A8E010A1C2F636F736D6F732E62616E6B2E763162657461312E4D736753656E64126E0A2E6C61636F6E696331347763303777613372377270707275343367396A786B7A68716E686D76666D34646765793673122E6C61636F6E6963317971706337637966657470676D71746B6B30756B657675676561617539703063776D6A6C73751A0C0A04616C6E7412043130303012680A500A460A1F2F636F736D6F732E63727970746F2E736563703235366B312E5075624B657912230A2102F3A1D077638F9FD828C4CF126FE82E0BE98388083F5BC1E1DD4D84132AEBFF8112040A020801185A12140A0E0A04616C6E7412063430303030301080B5181A4088DF7BA4B63EA68E185AB2887C9EC29EBC4158874BC037816B8494AD36D3B2433B5223CECC336D4624BB7FEF4DBB4A8B5F4707ACD8E55443312009E9473DF821" } ``` Reviewed-on: https://git.vdb.to/cerc-io/laconic-registry-cli/pulls/78 Reviewed-by: David Boreham Co-authored-by: Thomas E Lackey Co-committed-by: Thomas E Lackey --- src/cmds/registry-cmds/tokens-cmds/gettx.ts | 54 +++++++++++++++++++++ src/cmds/registry-cmds/tokens-cmds/send.ts | 37 ++++++++++++-- test/cli.test.ts | 24 ++++++++- 3 files changed, 110 insertions(+), 5 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..7523ccf 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 + }, + accounts: accountResponse + }; + + queryOutput(output, argv.output); }; diff --git a/test/cli.test.ts b/test/cli.test.ts index 8718bb2..5d84875 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -217,8 +217,28 @@ describe('Test laconic CLI commands', () => { getAccountObj({ address: testAccount2, balance: sendAmount }) ]; - expect(outputObj.length).toEqual(2); - expect(outputObj).toMatchObject(expectedAccounts); + 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); + }); + 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); }); });