diff --git a/bin/near-cli.js b/bin/near-cli.js index 224918dc..6c3d27b4 100644 --- a/bin/near-cli.js +++ b/bin/near-cli.js @@ -235,6 +235,7 @@ yargs // eslint-disable-line .command(require('../commands/dev-deploy')) .command(require('../commands/call')) .command(callViewFunction) + .command(require('../commands/view-state')) .command(sendMoney) .command(clean) .command(stake) diff --git a/commands/view-state.js b/commands/view-state.js new file mode 100644 index 00000000..fec99443 --- /dev/null +++ b/commands/view-state.js @@ -0,0 +1,45 @@ +const exitOnError = require('../utils/exit-on-error'); +const connect = require('../utils/connect'); +const { formatResponse } = require('../utils/inspect-response'); + +module.exports = { + command: 'view-state [prefix]', + desc: 'View contract storage state', + builder: (yargs) => yargs + .option('prefix', { + desc: 'Return keys only with given prefix.', + type: 'string', + default: '' + + }) + .option('block-id', { + desc: 'The block number OR the block hash (base58-encoded).', + type: 'string', + + }) + .option('finality', { + desc: '`optimistic` uses the latest block recorded on the node that responded to your query,\n' + + '`final` is for a block that has been validated on at least 66% of the nodes in the network', + type: 'string', + choices: ['optimistic', 'final'], + + }) + .option('utf8', { + desc: 'Decode keys and values as UTF-8 strings', + type: 'boolean', + default: false + }), + handler: exitOnError(viewState) +}; + +async function viewState(options) { + const { accountId, prefix, finality, blockId, utf8 } = options; + const near = await connect(options); + const account = await near.account(accountId); + + let state = await account.viewState(prefix, { blockId, finality }); + if (utf8) { + state = state.map(({ key, value}) => ({ key: key.toString('utf-8'), value: value.toString('utf-8') })); + } + console.log(formatResponse(state, options)); +} diff --git a/test/test_account_creation.sh b/test/test_account_creation.sh index cf1d1e09..e598d032 100755 --- a/test/test_account_creation.sh +++ b/test/test_account_creation.sh @@ -4,18 +4,18 @@ set -x timestamp=$(date +%s) testaccount=testaccount$timestamp.test.near -RESULT=$(./bin/near create-account $testaccount --masterAccount test.far) -echo $RESULT -EXPECTED=".+New account doesn't share the same top-level account.+ " -if [[ ! "$RESULT" =~ $EXPECTED ]]; then +ERROR=$(./bin/near create-account $testaccount --masterAccount test.far 2>&1 >/dev/null) +echo $ERROR +EXPECTED_ERROR=".+New account doesn't share the same top-level account.+ " +if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then echo FAILURE Unexpected output creating account with different master account exit 1 fi -RESULT=$(./bin/near create-account tooshortfortla --masterAccount test.far) -echo $RESULT -EXPECTED=".+Top-level accounts must be at least.+ " -if [[ ! "$RESULT" =~ $EXPECTED ]]; then +ERROR=$(./bin/near create-account tooshortfortla --masterAccount test.far 2>&1 >/dev/null) +echo $ERROR +EXPECTED_ERROR=".+Top-level accounts must be at least.+ " +if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then echo FAILURE Unexpected output when creating a short top-level account exit 1 fi diff --git a/test/test_deploy_init_contract.sh b/test/test_deploy_init_contract.sh index 473edb14..c4ccdfb6 100755 --- a/test/test_deploy_init_contract.sh +++ b/test/test_deploy_init_contract.sh @@ -11,10 +11,10 @@ echo Creating account echo Deploying contract without init method ../bin/near deploy --accountId $testaccount --wasmFile ../test/res/fungible_token.wasm -RESULT=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v | ../node_modules/.bin/strip-ansi) -echo $RESULT -EXPECTED=".+Fun token should be initialized before usage+" -if [[ ! "$RESULT" =~ $EXPECTED ]]; then +ERROR=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v 2>&1 >/dev/null | ../node_modules/.bin/strip-ansi) +echo $ERROR +EXPECTED_ERROR=".+Fun token should be initialized before usage+" +if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then echo FAILURE Expected message requiring initialization of contract exit 1 else diff --git a/utils/inspect-response.js b/utils/inspect-response.js index 60b807a3..942b46cf 100644 --- a/utils/inspect-response.js +++ b/utils/inspect-response.js @@ -35,8 +35,9 @@ const prettyPrintResponse = (response, options) => { const prettyPrintError = (error, options) => { if (checkForAccDoesNotExist(error, options)) return; - console.log('An error occured'); - console.log(formatResponse(error)); + console.error('An error occured'); + console.error(error.stack); + console.error(formatResponse(error)); const txnId = getTxnIdFromError(error); if (txnId) { console.log(`We attempted to send transaction ${txnId} to NEAR, but something went wrong.`); @@ -47,7 +48,7 @@ const prettyPrintError = (error, options) => { const formatResponse = (response) => { return util.inspect(response, { - showHidden: true, + // showHidden: true, depth: null, colors: Boolean(process.stdout.isTTY && process.stdout.hasColors()), maxArrayLength: null