-
-
Save ink-playground-gists/20784008ec376f27dae9fa198a3a892d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![cfg_attr(not(feature = "std"), no_std)] | |
use ink_lang as ink; | |
#[ink::contract] | |
mod mycontract { | |
use ink_storage::traits::{SpreadAllocate, SpreadLayout}; | |
#[derive(Debug, SpreadLayout, SpreadAllocate, Default)] | |
#[cfg_attr( | |
feature = "std", | |
derive( | |
scale_info::TypeInfo, | |
ink_storage::traits::StorageLayout | |
) | |
)] | |
pub struct Contract { | |
map: ink_storage::Mapping<AccountId, u32> | |
} | |
#[ink(storage)] | |
#[derive(SpreadAllocate)] | |
pub struct MyContract { | |
// Store a mapping from AccountIds to a u32 | |
map2: Contract, | |
} | |
impl MyContract { | |
#[ink(constructor)] | |
pub fn new() -> Self { | |
// This call is required in order to correctly initialize the | |
// `Mapping`s of our contract. | |
// ink_lang::utils::initialize_contract(|contract: &mut Self| { | |
// let caller = Self::env().caller(); | |
// contract.map.insert(&caller, &count); | |
// }) | |
Self { map2: Default::default() } | |
} | |
// #[ink(constructor)] | |
// pub fn default() -> Self { | |
// Even though we're not explicitly initializing the `Mapping`, | |
// we still need to call this | |
// ink_lang::utils::initialize_contract(|_| {}) | |
// } | |
// Grab the number at the caller's AccountID, if it exists | |
#[ink(message)] | |
pub fn get(&self) -> u32 { | |
let caller = Self::env().caller(); | |
// self.map2.get(&caller).unwrap_or_default() | |
self.map2.map.get(&caller).unwrap_or_default() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment