Skip to content

Commit

Permalink
Merge pull request #189 from nearprotocol/fix-tx-status
Browse files Browse the repository at this point in the history
Fix tx-status to handle account id better
  • Loading branch information
vgrichina committed Nov 16, 2019
2 parents 1c139dd + d7ce31b commit 8d414d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
15 changes: 1 addition & 14 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,6 @@ const clean = {
handler: exitOnError(main.clean)
};

// For transaction:
const txStatus = {
command: 'tx-status <hash>',
desc: 'lookup transaction status by hash',
builder: (yargs) => yargs
.option('hash', {
desc: 'base58-encoded hash',
type: 'string',
required: true
}),
handler: exitOnError(main.txStatus)
};

let config = require('../get-config')();
yargs // eslint-disable-line
.scriptName('near')
Expand Down Expand Up @@ -219,7 +206,7 @@ yargs // eslint-disable-line
.command(viewAccount)
.command(deleteAccount)
.command(keys)
.command(txStatus)
.command(require('../commands/tx-status'))
.command(build)
.command(deploy)
.command(scheduleFunctionCall)
Expand Down
37 changes: 37 additions & 0 deletions commands/tx-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const bs58 = require('bs58');

module.exports = {
command: 'tx-status <hash>',
desc: 'lookup transaction status by hash',
builder: (yargs) => yargs
.option('hash', {
desc: 'base58-encoded hash',
type: 'string',
required: true
}),
handler: exitOnError(async (argv) => {
const near = await connect(argv);

const hashParts = argv.hash.split(':');
let hash, accountId;
if (hashParts.length == 2) {
[accountId, hash] = hashParts;
} else if (hashParts.length == 1) {
[hash] = hashParts;
} else {
throw new Error('Unexpected transaction hash format');
}
accountId = accountId || argv.accountId || argv.masterAccount;

if (!accountId) {
throw new Error('Please specify account id, either as part of transaction hash or using --accountId flag.');
}

const status = await near.connection.provider.txStatus(bs58.decode(hash), accountId);
console.log(`Transaction ${accountId}:${hash}`);
console.log(inspectResponse(status));
})
};
20 changes: 3 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const fs = require('fs');
const util = require('util');
const yargs = require('yargs');
const bs58 = require('bs58');
const ncp = require('ncp').ncp;
ncp.limit = 16;
const rimraf = require('rimraf');
const readline = require('readline');
const URL = require('url').URL;
Expand All @@ -12,12 +11,7 @@ const { KeyPair, keyStores } = require('nearlib');
const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore;

const connect = require('./utils/connect');

ncp.limit = 16;

const inspectResponse = (response) => {
return util.inspect(response, { showHidden: false, depth: null, colors: true });
};
const inspectResponse = require('./utils/inspect-response');

// TODO: Fix promisified wrappers to handle error properly

Expand Down Expand Up @@ -152,12 +146,4 @@ exports.stake = async function(options) {
const account = await near.account(options.accountId);
const result = await account.stake(options.stakingKey, options.amount);
console.log(inspectResponse(result));
};

// For transaction:
exports.txStatus = async function(options) {
let near = await connect(options);
let status = await near.connection.provider.txStatus(bs58.decode(options.hash), options.accountId || options.masterAccount);
console.log(`Transaction ${options.hash}`);
console.log(inspectResponse(status));
};
};
5 changes: 5 additions & 0 deletions utils/inspect-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

const util = require('util');
module.exports = (response) => {
return util.inspect(response, { showHidden: false, depth: null, colors: true });
};

0 comments on commit 8d414d1

Please sign in to comment.