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 2 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
47 changes: 47 additions & 0 deletions commands/view-state.js
Original file line number Diff line number Diff line change
@@ -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 <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));
}
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