Skip to content

Instantly share code, notes, and snippets.

@ink-playground-gists
Created November 16, 2022 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ink-playground-gists/20784008ec376f27dae9fa198a3a892d to your computer and use it in GitHub Desktop.
Save ink-playground-gists/20784008ec376f27dae9fa198a3a892d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![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