Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
Status
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen};
use near_sdk::collections::UnorderedMap;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
records: UnorderedMap<String, String>,
}
#[near_bindgen]
impl StatusMessage {
pub fn set_status(&mut self, message: String) {
env::log(b"A");
let account_id = env::signer_account_id();
self.records.insert(&account_id, &message);
}
pub fn get_status(&self, account_id: String) -> Option<String> {
env::log(b"A");
return self.records.get(&account_id);
}
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use super::*;
use near_sdk::MockedBlockchain;
use near_sdk::{testing_env, VMContext};
fn get_context(input: Vec<u8>, is_view: bool) -> VMContext {
VMContext {
current_account_id: "alice_near".to_string(),
signer_account_id: "bob_near".to_string(),
signer_account_pk: vec![0, 1, 2],
predecessor_account_id: "carol_near".to_string(),
input,
block_index: 0,
block_timestamp: 0,
account_balance: 0,
account_locked_balance: 0,
storage_usage: 0,
attached_deposit: 0,
prepaid_gas: 10u64.pow(18),
random_seed: vec![0, 1, 2],
is_view,
output_data_receivers: vec![],
epoch_height: 0,
}
}
#[test]
fn set_get_message() {
let context = get_context(vec![], false);
testing_env!(context);
let mut contract = StatusMessage::default();
contract.set_status("hello".to_string());
assert_eq!(
"hello".to_string(),
contract.get_status("bob_near".to_string()).unwrap()
);
}
#[test]
fn get_nonexistent_message() {
let context = get_context(vec![], true);
testing_env!(context);
let contract = StatusMessage::default();
assert_eq!(None, contract.get_status("francis.near".to_string()));
}
}
use near_sdk::{env, near_bindgen};
use near_sdk::collections::UnorderedMap;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct StatusMessage {
records: UnorderedMap<String, String>,
}
#[near_bindgen]
impl StatusMessage {
pub fn set_status(&mut self, message: String) {
env::log(b"A");
let account_id = env::signer_account_id();
self.records.insert(&account_id, &message);
}
pub fn get_status(&self, account_id: String) -> Option<String> {
env::log(b"A");
return self.records.get(&account_id);
}
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use super::*;
use near_sdk::MockedBlockchain;
use near_sdk::{testing_env, VMContext};
fn get_context(input: Vec<u8>, is_view: bool) -> VMContext {
VMContext {
current_account_id: "alice_near".to_string(),
signer_account_id: "bob_near".to_string(),
signer_account_pk: vec![0, 1, 2],
predecessor_account_id: "carol_near".to_string(),
input,
block_index: 0,
block_timestamp: 0,
account_balance: 0,
account_locked_balance: 0,
storage_usage: 0,
attached_deposit: 0,
prepaid_gas: 10u64.pow(18),
random_seed: vec![0, 1, 2],
is_view,
output_data_receivers: vec![],
epoch_height: 0,
}
}
#[test]
fn set_get_message() {
let context = get_context(vec![], false);
testing_env!(context);
let mut contract = StatusMessage::default();
contract.set_status("hello".to_string());
assert_eq!(
"hello".to_string(),
contract.get_status("bob_near".to_string()).unwrap()
);
}
#[test]
fn get_nonexistent_message() {
let context = get_context(vec![], true);
testing_env!(context);
let contract = StatusMessage::default();
assert_eq!(None, contract.get_status("francis.near".to_string()));
}
}
near login
near deploy --wasmFile res/status_message.wasm --accountId YOUR_CONTRACT_ACCOUNT_NAME
near call YOUR_CONTRACT_ACCOUNT_NAME set_status '{"message": "aloha friend"}' --accountId YOUR_ACCOUNT_NAME
near view YOUR_CONTRACT_ACCOUNT_NAME get_status '{"account_id": "YOUR_ACCOUNT_NAME"}'