Skip to content

Instantly share code, notes, and snippets.

@noemk2
Created July 2, 2022 22:05
Show Gist options
  • Select an option

  • Save noemk2/0b38269e8913c8e6a5d3411387bc289d to your computer and use it in GitHub Desktop.

Select an option

Save noemk2/0b38269e8913c8e6a5d3411387bc289d to your computer and use it in GitHub Desktop.
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{log, near_bindgen};
// Define the default message
const DEFAULT_MESSAGE: &str = "Hello";
// Define the contract structure
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Contract {
message: String,
}
// Define the default, which automatically initializes the contract
impl Default for Contract {
fn default() -> Self {
Self {
message: DEFAULT_MESSAGE.to_string(),
}
}
}
// Implement the contract structure
#[near_bindgen]
impl Contract {
// Public method - returns the greeting saved, defaulting to DEFAULT_MESSAGE
pub fn get_greeting(&self) -> String {
return self.message.clone();
}
// Public method - accepts a greeting, such as "howdy", and records it
pub fn set_greeting(&mut self, message: String) {
// Use env::log to record logs permanently to the blockchain!
log!("Saving greeting {}", message);
self.message = message;
}
}
/*
* The rest of this file holds the inline tests for the code above
* Learn more about Rust tests: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_default_greeting() {
let contract = Contract::default();
// this test did not call set_greeting so should return the default "Hello" greeting
assert_eq!(contract.get_greeting(), "Hello".to_string());
}
#[test]
fn set_then_get_greeting() {
let mut contract = Contract::default();
contract.set_greeting("howdy".to_string());
assert_eq!(contract.get_greeting(), "howdy".to_string());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment