Skip to content

Commit

Permalink
Add view-state command
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Jan 29, 2021
1 parent df866f7 commit 5c6db6a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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]',
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));
}

0 comments on commit 5c6db6a

Please sign in to comment.