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

Commit fcba8d0

Browse files
authored
Merge pull request #684 from near/view-state
View state
2 parents aa7642d + 728c299 commit fcba8d0

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

bin/near-cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ yargs // eslint-disable-line
235235
.command(require('../commands/dev-deploy'))
236236
.command(require('../commands/call'))
237237
.command(callViewFunction)
238+
.command(require('../commands/view-state'))
238239
.command(sendMoney)
239240
.command(clean)
240241
.command(stake)

commands/view-state.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const exitOnError = require('../utils/exit-on-error');
2+
const connect = require('../utils/connect');
3+
const { formatResponse } = require('../utils/inspect-response');
4+
5+
module.exports = {
6+
command: 'view-state <account-id> [prefix]',
7+
desc: 'View contract storage state',
8+
builder: (yargs) => yargs
9+
.option('prefix', {
10+
desc: 'Return keys only with given prefix.',
11+
type: 'string',
12+
default: ''
13+
14+
})
15+
.option('block-id', {
16+
desc: 'The block number OR the block hash (base58-encoded).',
17+
type: 'string',
18+
19+
})
20+
.option('finality', {
21+
desc: '`optimistic` uses the latest block recorded on the node that responded to your query,\n' +
22+
'`final` is for a block that has been validated on at least 66% of the nodes in the network',
23+
type: 'string',
24+
choices: ['optimistic', 'final'],
25+
26+
})
27+
.option('utf8', {
28+
desc: 'Decode keys and values as UTF-8 strings',
29+
type: 'boolean',
30+
default: false
31+
}),
32+
handler: exitOnError(viewState)
33+
};
34+
35+
async function viewState(options) {
36+
const { accountId, prefix, finality, blockId, utf8 } = options;
37+
const near = await connect(options);
38+
const account = await near.account(accountId);
39+
40+
let state = await account.viewState(prefix, { blockId, finality });
41+
if (utf8) {
42+
state = state.map(({ key, value}) => ({ key: key.toString('utf-8'), value: value.toString('utf-8') }));
43+
}
44+
console.log(formatResponse(state, options));
45+
}

test/test_account_creation.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ set -x
44
timestamp=$(date +%s)
55
testaccount=testaccount$timestamp.test.near
66

7-
RESULT=$(./bin/near create-account $testaccount --masterAccount test.far)
8-
echo $RESULT
9-
EXPECTED=".+New account doesn't share the same top-level account.+ "
10-
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
7+
ERROR=$(./bin/near create-account $testaccount --masterAccount test.far 2>&1 >/dev/null)
8+
echo $ERROR
9+
EXPECTED_ERROR=".+New account doesn't share the same top-level account.+ "
10+
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
1111
echo FAILURE Unexpected output creating account with different master account
1212
exit 1
1313
fi
1414

15-
RESULT=$(./bin/near create-account tooshortfortla --masterAccount test.far)
16-
echo $RESULT
17-
EXPECTED=".+Top-level accounts must be at least.+ "
18-
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
15+
ERROR=$(./bin/near create-account tooshortfortla --masterAccount test.far 2>&1 >/dev/null)
16+
echo $ERROR
17+
EXPECTED_ERROR=".+Top-level accounts must be at least.+ "
18+
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
1919
echo FAILURE Unexpected output when creating a short top-level account
2020
exit 1
2121
fi

test/test_deploy_init_contract.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ echo Creating account
1111

1212
echo Deploying contract without init method
1313
../bin/near deploy --accountId $testaccount --wasmFile ../test/res/fungible_token.wasm
14-
RESULT=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v | ../node_modules/.bin/strip-ansi)
15-
echo $RESULT
16-
EXPECTED=".+Fun token should be initialized before usage+"
17-
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
14+
ERROR=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v 2>&1 >/dev/null | ../node_modules/.bin/strip-ansi)
15+
echo $ERROR
16+
EXPECTED_ERROR=".+Fun token should be initialized before usage+"
17+
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
1818
echo FAILURE Expected message requiring initialization of contract
1919
exit 1
2020
else

utils/inspect-response.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ const prettyPrintResponse = (response, options) => {
3535
const prettyPrintError = (error, options) => {
3636
if (checkForAccDoesNotExist(error, options)) return;
3737

38-
console.log('An error occured');
39-
console.log(formatResponse(error));
38+
console.error('An error occured');
39+
console.error(error.stack);
40+
console.error(formatResponse(error));
4041
const txnId = getTxnIdFromError(error);
4142
if (txnId) {
4243
console.log(`We attempted to send transaction ${txnId} to NEAR, but something went wrong.`);
@@ -47,7 +48,7 @@ const prettyPrintError = (error, options) => {
4748

4849
const formatResponse = (response) => {
4950
return util.inspect(response, {
50-
showHidden: true,
51+
// showHidden: true,
5152
depth: null,
5253
colors: Boolean(process.stdout.isTTY && process.stdout.hasColors()),
5354
maxArrayLength: null

0 commit comments

Comments
 (0)