use neo_devpack::prelude::*;
#[neo_contract]
pub struct HelloWorld {
greeting: NeoString,
counter: NeoInteger,
}
impl HelloWorld {
pub fn new() -> Self {
Self {
greeting: NeoString::from_str("Hello, Neo N3!"),
counter: NeoInteger::zero(),
}
}
#[neo_method]
pub fn get_greeting(&self) -> NeoResult<NeoString> {
Ok(self.greeting.clone())
}
#[neo_method]
pub fn set_greeting(&mut self, greeting: NeoString) -> NeoResult<()> {
self.greeting = greeting;
Ok(())
}
#[neo_method]
pub fn get_counter(&self) -> NeoResult<NeoInteger> {
Ok(self.counter.clone())
}
#[neo_method]
pub fn increment_counter(&mut self) -> NeoResult<NeoInteger> {
self.counter = self.counter.clone() + NeoInteger::one();
Ok(self.counter.clone())
}
#[neo_method]
pub fn say_hello(&self) -> NeoResult<NeoString> {
let message = NeoString::from_str(&format!(
"{} Counter: {}",
self.greeting.as_str(),
self.counter.as_i32_saturating()
));
Ok(message)
}
}
impl Default for HelloWorld {
fn default() -> Self {
Self::new()
}
}
pub fn deploy() -> NeoResult<()> {
let _contract = HelloWorld::new();
Ok(())
}
pub fn update() -> NeoResult<()> {
Ok(())
}
pub fn destroy() -> NeoResult<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hello_world_creation() {
let contract = HelloWorld::new();
assert_eq!(contract.get_greeting().unwrap().as_str(), "Hello, Neo N3!");
assert_eq!(contract.get_counter().unwrap().as_i32_saturating(), 0);
}
#[test]
fn test_hello_world_operations() {
let mut contract = HelloWorld::new();
let new_greeting = NeoString::from_str("Hello, Rust!");
contract.set_greeting(new_greeting.clone()).unwrap();
assert_eq!(contract.get_greeting().unwrap().as_str(), "Hello, Rust!");
contract.increment_counter().unwrap();
assert_eq!(contract.get_counter().unwrap().as_i32_saturating(), 1);
let hello = contract.say_hello().unwrap();
assert!(hello.as_str().contains("Hello, Rust!"));
assert!(hello.as_str().contains("Counter: 1"));
}
}
pub fn main() -> NeoResult<()> {
let contract = HelloWorld::new();
let message = contract.say_hello()?;
let event = NeoString::from_str("HelloWorld");
let state = NeoArray::from_vec(vec![NeoValue::String(message)]);
NeoRuntime::notify(&event, &state)?;
Ok(())
}