Fix default 'account get' output. #77

Merged
telackey merged 4 commits from telackey/getaccount into main 2024-08-20 19:01:25 +00:00
5 changed files with 165 additions and 31 deletions
Showing only changes of commit 2c20c8e8f9 - Show all commits

View File

@ -141,7 +141,18 @@ Send tokens:
```bash
$ laconic registry tokens send --address laconic15za32wly5exgcrt2zfr8php4ya49n5y7masu7k --type alnt --quantity 1000000000
[
{
"tx": {
"hash": "977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC",
"height": 343369,
"index": 0,
"code": 0,
"log": "",
"sender": "laconic1pmuxrcnuhhf8qdllzuf2ctj2tnwwcg6yswqnyd",
"recipient": "laconic15za32wly5exgcrt2zfr8php4ya49n5y7masu7k",
"amount": "1000000000alnt"
},
"accounts": [
{
"address": "laconic1pmuxrcnuhhf8qdllzuf2ctj2tnwwcg6yswqnyd",
"pubKey": "A68/q7/xazFzNj+rrvE07ALxkMgqw1ugL35VECkWAYvt",
@ -166,7 +177,25 @@ $ laconic registry tokens send --address laconic15za32wly5exgcrt2zfr8php4ya49n5y
}
]
}
]
]
}
```
Get token TX details:
```bash
$ laconic registry tokens gettx --hash 977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC
{
"hash": "977152CBE474613E1BBAFEF286F12134829FAF3C9E7C8349149DE3E687B816FC",
"height": 343369,
"index": 0,
"code": 0,
"log": "",
"sender": "laconic1pmuxrcnuhhf8qdllzuf2ctj2tnwwcg6yswqnyd",
"recipient": "laconic15za32wly5exgcrt2zfr8php4ya49n5y7masu7k",
"amount": "1000000000alnt",
"raw": "0A91010A8E010A1C2F636F736D6F732E62616E6B2E763162657461312E4D736753656E64126E0A2E6C61636F6E696331347763303777613372377270707275343367396A786B7A68716E686D76666D34646765793673122E6C61636F6E6963317971706337637966657470676D71746B6B30756B657675676561617539703063776D6A6C73751A0C0A04616C6E7412043130303012680A500A460A1F2F636F736D6F732E63727970746F2E736563703235366B312E5075624B657912230A2102F3A1D077638F9FD828C4CF126FE82E0BE98388083F5BC1E1DD4D84132AEBFF8112040A020801185A12140A0E0A04616C6E7412063430303030301080B5181A4088DF7BA4B63EA68E185AB2887C9EC29EBC4158874BC037816B8494AD36D3B2433B5223CECC336D4624BB7FEF4DBB4A8B5F4707ACD8E55443312009E9473DF821"
}
```
Create record (generic):

View File

@ -1,6 +1,6 @@
{
"name": "@cerc-io/laconic-registry-cli",
"version": "0.2.4",
"version": "0.2.5",
"main": "index.js",
"repository": "git@github.com:cerc-io/laconic-registry-cli.git",
"author": "",

View File

@ -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);
}
};

View File

@ -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);
};

View File

@ -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 <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);
});
});