Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
address Daryl comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ailisp authored and Mike Purvis committed Jun 28, 2021
1 parent 54261db commit 3c863fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
33 changes: 18 additions & 15 deletions commands/view-state.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { formatResponse } = require('../utils/inspect-response');
const yargs = require('yargs');

module.exports = {
command: 'view-state <account-id> [prefix]',
Expand All @@ -10,7 +11,6 @@ module.exports = {
desc: 'Return keys only with given prefix.',
type: 'string',
default: ''

})
.option('block-id', {
desc: 'The block number OR the block hash (base58-encoded).',
Expand All @@ -27,6 +27,21 @@ module.exports = {
desc: 'Decode keys and values as UTF-8 strings',
type: 'boolean',
default: false
})
.conflicts('block-id', 'finality')
.check((argv, _) => {
if (!argv.finality && !argv.blockId) {
throw new Error('Must provide either --finality or --blockId');
}
return true;
})
.coerce({
blockId: (blockId) => {
if (blockId && !isNaN(blockId)) {
return Number(blockId);
}
return blockId;
}
}),
handler: exitOnError(viewState)
};
Expand All @@ -35,21 +50,9 @@ async function viewState(options) {
const { accountId, prefix, finality, blockId, utf8 } = options;
const near = await connect(options);
const account = await near.account(accountId);
if (finality && blockId) {
console.error('Only one of --finality and --blockId can be provided');
process.exit(1);
} else if (!finality && !blockId) {
console.error('Must provide either --finality or --blockId');
process.exit(1);
}

// near-api-js takes block_id instead of blockId
let block_id = blockId;
if (blockId && !isNaN(Number(blockId))) {
// If block id is a number (still string as command line args), it must be convert to JavaScript Number
// for near-api-js.
block_id = Number(blockId);
}
let state = await account.viewState(prefix, { block_id, finality });
let state = await account.viewState(prefix, { block_id: blockId, finality });
if (utf8) {
state = state.map(({ key, value}) => ({ key: key.toString('utf-8'), value: value.toString('utf-8') }));
} else {
Expand Down
20 changes: 17 additions & 3 deletions test/test_account_operations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,32 @@ if [[ ! "$RESULT" == $EXPECTED ]]; then
exit 1
fi

echo Get account storage --blockId $BLOCK_HASH --finality optimistic should fail
EXPECTED="Only one of --finality and --blockId can be provided"
set +e

echo Get account storage --blockId $BLOCK_HASH --finality optimistic should fail
EXPECTED="Arguments block-id and finality are mutually exclusive"
./bin/near view-state $testaccount --blockId $BLOCK_HASH --finality optimistic 2> ${testaccount}.stderr
if [[ ! $? == 1 ]]; then
echo view-state should fail given both blockId and finality
exit 1
fi
if [[ ! $(cat ${testaccount}.stderr) == $EXPECTED ]]; then
if [[ ! $(cat ${testaccount}.stderr) =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near view-state
exit 1
fi

echo Get account storage without one of blockId or finality should fail
EXPECTED="Must provide either --finality or --blockId"
./bin/near view-state $testaccount 2> ${testaccount}.stderr
if [[ ! $? == 1 ]]; then
echo view-state should fail without one of blockId or finality should fail
exit 1
fi
if [[ ! $(cat ${testaccount}.stderr) =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near view-state
exit 1
fi

set -e


Expand Down

0 comments on commit 3c863fe

Please sign in to comment.