Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions commands/view-state.js
Original file line number Diff line number Diff line change
@@ -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 <account-id> [prefix]',
Copy link
Collaborator

@volovyks volovyks Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantically it's the same as state. Should we rename it to view-contract-state, or simply contract-state? @mikedotexe @evgenykuzyakov @chadoh need your opinion here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have the namespaces proposed in near/NEPs#31, I would advocate for near contract state. But I doubt it makes sense to do that for now.

In the meantime, I don't have a strong preference. near view-state some-contract.near seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just roll as is then and makes sense to me to change to namespaces long term.

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));
}
16 changes: 8 additions & 8 deletions test/test_account_creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions test/test_deploy_init_contract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions utils/inspect-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand All @@ -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
Expand Down