source neardev/dev-account.env
export CONTRACT_NAME=$CONTRACT_NAME
near view-state $CONTRACT_NAME --finality final
Getting only keys:
// file: view_state_keys.js
const nearAPI = require('near-api-js')
const { connect, keyStores } = nearAPI
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(__dirname);
const config = {
keyStore,
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
explorerUrl: 'https://explorer.testnet.near.org',
}
async function main () {
const near = await connect(config)
const response = await near.connection.provider.query({
request_type: 'view_state',
finality: 'final',
account_id: process.env.CONTRACT_NAME,
prefix_base64: '',
})
console.log(JSON.stringify({
// TODO add calc size of data for limit burning 200TGas for one call on contract
keys: response.values.map(it => it.key)
}))
}
main().catch(reason => {
console.error(reason)
})
for more info see near/core-contracts#171
use near_sdk::json_types::Base64VecU8;
#[near_bindgen]
impl Contract {
#[private]
#[init(ignore_state)]
pub fn clean(keys: Vec<Base64VecU8>) {
for key in keys.iter() {
env::storage_remove(&key.0);
}
}
}
Build and deploy contract:
near deploy ${CONTRACT_NAME} out/main.wasm
near --accountId $CONTRACT_NAME call $CONTRACT_NAME clean --base64 "$(node view_state_keys.js | base64 -w0)" --gas 300000000000000
For other people landing here: you can prevent the error State of contract _____.near is too large to be viewed when calling view-state via the JSON RPC API if you select an alternative RPC provider from this list (also works for testnet if you replace testnet with mainnet in some of the URLs): docs.near.org/api/rpc/providers I was successful with OOMNIA: endpoints.omniatech.io/v1/near/testnet/public