use near_sdk::{env, near, AccountId, PanicOnDefault}; #[near(contract_state)] #[derive(PanicOnDefault)] pub struct Registry { owner_id: AccountId, entries: Vec<(AccountId, String)>, } #[near] impl Registry { #[init] pub fn new(owner_id: AccountId) -> Self { Self { owner_id, entries: Vec::new() } } pub fn set(&mut self, account_id: AccountId, value: String) { self.assert_owner(); if value.len() > 256 { env::panic_str("Value too long"); } if let Some(entry) = self.entries.iter_mut().find(|(id, _)| id == &account_id) { entry.1 = value; } else { self.entries.push((account_id, value)); } } pub fn get(&self, account_id: AccountId) -> Option { self.entries.iter().find(|(id, _)| id == &account_id).map(|(_, value)| value.clone()) } pub fn remove(&mut self, account_id: AccountId) -> bool { self.assert_owner(); if let Some(index) = self.entries.iter().position(|(id, _)| id == &account_id) { self.entries.swap_remove(index); true } else { false } } pub fn count(&self) -> u32 { self.entries.len() as u32 } fn assert_owner(&self) { if env::predecessor_account_id() != self.owner_id { env::panic_str("Owner only"); } } }