1

How to create a mapping of a vector of struct, also how to assign value and get the value in this data structure. I didn't find the documentation on the official doc. Is there any downside to using such a data structure in the context of smart contract?

1 Answer 1

1

Instead of an array of structs, you can use a vector of structs to add on mapping. Here is an example:

list_of_proposal: Mapping<AccountId, Vec<Proposal>>

pub struct Proposal{
....
...
}

// Get list_of_proposal (vector of structs)

pub fn get_proposal(&self) -> Vec<Proposal> 
{  
let proposal =
self.list_of_proposals(&self.env.caller());  
proposal 
.... 
}

//For add a new proposal

pub fn set_proposal(&self)
{
let vec = Vec::new();
let new_proposal = Proposal
{
...
...
}
vec.push(new_proposal);
self.list_of_proposal.insert(&vec);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.