This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |