Last active
May 31, 2022 18:36
-
-
Save kalloc/f2c6ac09922788abc5954e063f2ba338 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | |
use near_sdk::collections::{LookupMap, UnorderedSet}; | |
use near_sdk::json_types::Base64VecU8; | |
use near_sdk::{near_bindgen, BorshStorageKey, PanicOnDefault, env}; | |
#[near_bindgen] | |
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)] | |
pub struct Contract { | |
key1: LookupMap<u8, UnorderedSet<u8>>, | |
key2: LookupMap<u8, UnorderedSet<u8>>, | |
} | |
#[derive(BorshSerialize, BorshStorageKey)] | |
enum StorageKey1 { | |
ForKey1 { key: u8 }, | |
Key1, | |
ForKey2 { key: u8 }, | |
Key2, | |
} | |
#[near_bindgen] | |
impl Contract { | |
#[init] | |
pub fn init() -> Self { | |
let mut this = Self { | |
key1: LookupMap::new(StorageKey1::Key1), | |
key2: LookupMap::new(StorageKey1::Key2), | |
}; | |
this.write1(1, 1); | |
this.write2(1, 1); | |
this.write1(1, 2); | |
this.write2(1, 2); | |
this.write1(1, 3); | |
this.write1(2, 1); | |
this.write2(2, 1); | |
this.write1(6, 2); | |
this.write2(7, 2); | |
this.write1(7, 3); | |
this | |
} | |
pub fn write1(&mut self, key: u8, value: u8) { | |
let mut set = if let Some(set) = self.key1.get(&key) { | |
set | |
} else { | |
UnorderedSet::new(StorageKey1::ForKey1 { key }) | |
}; | |
set.insert(&value); | |
env::log_str(format!("Set length {}", set.try_to_vec().unwrap().len()).as_str()); | |
env::log_str(format!("Insert {} into {} to {:#?}", value, key, set).as_str()); | |
self.key1.insert(&key, &set); | |
} | |
pub fn read1(&self, key: u8) -> Vec<u8> { | |
self.key1.get(&key).unwrap().to_vec() | |
} | |
pub fn write2(&mut self, key: u8, value: u8) { | |
let mut set = if let Some(set) = self.key2.get(&key) { | |
set | |
} else { | |
UnorderedSet::new(StorageKey1::ForKey2 { key }) | |
}; | |
set.insert(&value); | |
env::log_str(format!("Set length {}", set.try_to_vec().unwrap().len()).as_str()); | |
env::log_str(format!("Insert {} into {} to {:#?}", value, key, set).as_str()); | |
self.key2.insert(&key, &set); | |
} | |
pub fn read2(&self, key: u8) -> Vec<u8> { | |
self.key2.get(&key).unwrap().to_vec() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment