-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to view_state()
with Complex Types?
#256
Comments
view_state()
with Complex Types?
What I ended up doing was grabbing the elements of the Does anyone know of a better approach? let state_items = contract.view_state(None).await?;
let state = state_items.get(b"STATE".as_slice()).unwrap();
let contract_state = MyContract::try_from_slice(&state)?;
let state_items = contract.view_state(Some(b"blacklist")).await?;
let mut blacklist_state: HashSet<AccountId> = HashSet::new();
for val in 0..contract_state.blacklist.len() { // len() works
let state = state_items.get(&[b"blackliste".as_slice(), &val.to_le_bytes()].concat()).unwrap();
blacklist_state.insert(AccountId::try_from_slice(&state).unwrap());
}
println!("{:#?}", blacklist_state.clone());
// can use blacklist_state.contains() now |
@sinh3ck I think this is because you need to enable the |
@ChaoticTempest Thanks for your input. I noticed that on the |
@sinh3ck sorry, I thought I commented on this a while back, but calling methods like If you still want to continue with viewing these collection types, you'll have to grab the prefix from |
@ChaoticTempest I'm glad I brought this up then. I guess it's more like a feature request now. It would be nice to grab the state and view the data in a more automated fashion. Even considering that simple types can be grabbed right away, while the big problem is with collections and nested collections. Thanks for your help! |
According to the documentation and examples provided in this repo, the way to view the state of the contract is similar to this:
Printing the
owner_id
works just fine. However the last line, when attempting to print a complex type like an UnorderedSetto_vec()
, panics with the following message:I have also tried the following, but it also panics with the same message:
From what I gather, the panic is occurring on line 15 of the backtrace
near_sdk::collections::vector::Vector<T>::get_raw
when the call tostorage_read()
with the correctlookup_key
is returningNone
.What's the correct way of printing this UnorderedSet's elements?
If there's a way, could you include it on one of the examples in this repo?
Thanks in advance.
The text was updated successfully, but these errors were encountered: