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 those who encounter the same problem after me, here are some solutions:
#[init(ignore_state)]
on top of theclean()
function. You can still call into the function without this..js
file in the same directly as where you run thenear
CLI, the.js
file is not part of the smart contract that gets deployed, it's something to be called locallybase64
implementation is different from the one in Linux, there's no-w0
parameter and by default it generates the same output as when you attach-w0
in Linux (reference: https://stackoverflow.com/questions/46463027/base64-doesnt-have-w-option-in-mac)I ran into the issue when following the NFT example on: https://docs.near.org/tutorials/nfts/minting-nfts#minting-your-nfts. I wasn't satisfied with the default NFT contract metadata and wanted to update it but got the error that the contract has already been initialized. This post helped me to clear the state and I was able to generate the NFT contract metadata again. Thanks!