From 5c6db6a2a136659872d6349d7f0057a4fd2360a6 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Fri, 29 Jan 2021 01:07:10 -0800 Subject: [PATCH 1/4] Add view-state command --- bin/near-cli.js | 1 + commands/view-state.js | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 commands/view-state.js 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..cbe66ded --- /dev/null +++ b/commands/view-state.js @@ -0,0 +1,47 @@ +const exitOnError = require('../utils/exit-on-error'); +const connect = require('../utils/connect'); +const { formatResponse } = require('../utils/inspect-response'); +const { utils } = require('near-api-js'); + + +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)); +} From 52b4fe2c7986eedf72f7b965158387e118479b14 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Fri, 29 Jan 2021 01:07:36 -0800 Subject: [PATCH 2/4] WIP: fix response formatting --- utils/inspect-response.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 From cea08c116f36bffc210cdfa24b390f10c00819f2 Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 3 Mar 2021 12:07:43 +0200 Subject: [PATCH 3/4] lint fix --- commands/view-state.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commands/view-state.js b/commands/view-state.js index cbe66ded..fec99443 100644 --- a/commands/view-state.js +++ b/commands/view-state.js @@ -1,8 +1,6 @@ const exitOnError = require('../utils/exit-on-error'); const connect = require('../utils/connect'); const { formatResponse } = require('../utils/inspect-response'); -const { utils } = require('near-api-js'); - module.exports = { command: 'view-state [prefix]', @@ -41,7 +39,7 @@ async function viewState(options) { 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') })) + state = state.map(({ key, value}) => ({ key: key.toString('utf-8'), value: value.toString('utf-8') })); } console.log(formatResponse(state, options)); } From 728c299c2b7583bc7129239a63f94908e8de60b5 Mon Sep 17 00:00:00 2001 From: Serhii Volovyk Date: Wed, 3 Mar 2021 14:25:17 +0200 Subject: [PATCH 4/4] tests fixed --- test/test_account_creation.sh | 16 ++++++++-------- test/test_deploy_init_contract.sh | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) 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