927: Add gettx command and have send command return the tx hash. #78
54
src/cmds/registry-cmds/tokens-cmds/gettx.ts
Normal file
54
src/cmds/registry-cmds/tokens-cmds/gettx.ts
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
@ -3,6 +3,7 @@ import assert from 'assert';
|
|||||||
import { Account, Registry } from '@cerc-io/registry-sdk';
|
import { Account, Registry } from '@cerc-io/registry-sdk';
|
||||||
|
|
||||||
import { getConfig, getConnectionInfo, getGasAndFees, queryOutput } from '../../../util';
|
import { getConfig, getConnectionInfo, getGasAndFees, queryOutput } from '../../../util';
|
||||||
|
import { DeliverTxResponse } from '@cosmjs/stargate';
|
||||||
|
|
||||||
export const command = 'send';
|
export const command = 'send';
|
||||||
|
|
||||||
@ -38,8 +39,38 @@ export const handler = async (argv: Arguments) => {
|
|||||||
const fromAddress = account.address;
|
const fromAddress = account.address;
|
||||||
|
|
||||||
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
|
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
|
||||||
|
const laconicClient = await registry.getLaconicClient(account);
|
||||||
const fee = getGasAndFees(argv, registryConfig);
|
const fee = getGasAndFees(argv, registryConfig);
|
||||||
await registry.sendCoins({ denom, amount, destinationAddress }, privateKey, fee);
|
|
||||||
const result = await registry.getAccounts([fromAddress, destinationAddress]);
|
const txResponse: DeliverTxResponse = await laconicClient.sendTokens(
|
||||||
queryOutput(result, argv.output);
|
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);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user