#[derive(BorshSerialize, BorshDeserialize)]
pub struct NotesDs {
pub own: Vec<String>,
pub shared: UnorderedMap<AccountId,Vec<String>>,
}
impl NotesDs{
pub fn new() -> Self {
assert!(env::state_read::<Self>().is_none(), "Already initialized");
Self {
own: Vec:: new(),
shared: UnorderedMap::new(b"w".to_vec()),
}
}
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Note {
pub note_list : UnorderedMap<AccountId,NotesDs>,
}
impl Default for Note {
fn default() -> Self {
// Check incase the contract is not initialized
env::panic(b"The contract is not initialized.")
}
}
#[near_bindgen]
impl Note {
/// Init attribute used for instantiation.
#[init]
pub fn new() -> Self {
assert!(env::state_read::<Self>().is_none(), "Already initialized");
Self {
note_list: UnorderedMap::new(b"h".to_vec()),
}
}
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}
}
Problem Defination
This causes an error
error[E0277]: the trait bound NotesDs: Serialize is not satisfied
#[near_bindgen] - the trait Serialize is not implemented for 'NotesDs'
note: required because of the requirements on the impl of Serialize for Vec<NotesDs> note: required by a bound in to_vec
Cargo.toml
[package]
name = "test101"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = "3.1.0"
serde_json = "1.0.2"
[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = true
I tried adding Serialize and Deserialize to all of my structs and enums.
The main problem is coming when the return value is of the type NotesDs, the functions where it is not the return type are working fine.
I even tried initializing the custom defined struct in a separate file and importing it. But I still get the error.
You implemented Borsh serialization on the NotesDs
struct using #[derive(BorshSerialize, BorshDeserialize)]
, and you're returning NotesDs
from this contract function:
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}
The issue is that near-sdk
defaults to JSON serialization. To use Borsh serialization for the return type, annotate the function with #[result_serializer(borsh)]
:
#[result_serializer(borsh)]
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}