Search code examples
rustwebassemblysmartcontractsnearprotocol

Initializing a LookupMap with near-sdk-rust


I am currently developing a contract where I want to make use of a LookupMap, however it's not clear to me how to initialize it. Here is the code:

// Just a structyre
pub struct Gift {
    url: String,
    n_tokens_required: usize,
    current_tokens: usize,
}

// Main contract code
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Voting { // TODO: Rename this class
    pub gifts: LookupMap<String, Vector<Gift>>,
    pub contract_owner: String,
}

impl Default for Voting {
    fn default() -> Self {
        Voting {gifts: LookupMap::new(), contract_owner: env::current_account_id()}
    }
}

So I want the gifts attribute of my contract to be a LookupMap with the signature LookupMap<String, Vector<Gift>>. How can I initialize it on my default function implementation?

When I try to do LookupMap::new(), it says that I need a parameter key_prefix, with the trait IntoStorageKey, however it's not clear to me what this parameter actually is.

Could anyone help me understand this better?

Thank you!


Solution

  • You will find your answer on this page https://www.near-sdk.io/contract-structure/collections