Index

Rust

  1. Guessing Game
  2. Common Programming Concepts
    1. Variables and Mutability
    2. Data Types
    3. Function
    4. Control Flow
  3. Understanding Ownership
    1. References and Borrowing
    2. The Slice Type
  4. Using Structs
    1. An Example Program Using Structs
    2. Method Syntax
  5. Enums and Pattern Matching
    1. The match Control Flow Operator
    2. Concise Control Flow with if let
  6. Managing Growing Projects with Packages, Crates, and Modules
    1. Defining Modules to Control Scope and Privacy
    2. Paths for Referring to an Item in the Module Tree
    3. Bringing Paths into Scope with the use Keyword
    4. Separating Modules into Different Files
  7. Common Collections
    1. Storing UTF-8 Encoded Text with Strings
    2. Storing Keys with Associated Values in Hash Maps
  8. Error Handling
    1. Unrecoverable Errors with panic!
    2. Recoverable Errors with Result
  9. Generic Types, Traits, and Lifetimes
    1. Traits: Defining Shared Behavior
    2. Generics Rust by Example
      1. Functions
      2. Implementation
  10. Writing Automated Tests
  11. Object Oriented Programming
  12. Adding dependancies
  13. Option Take
  14. RefCell
  15. mem
  16. Data Structure
    1. Linked List
    2. Binary search tree
    3. N-ary Sum tree
  17. Recipe
    1. Semi colon
    2. Calling rust from python
    3. Default
    4. Crytocurrency With rust
    5. Function chaining
    6. Question Mark Operator
    7. Tests with println
    8. lib and bin
    9. Append vector to hash map
    10. Random Number
    11. uuid4
    12. uwrap and option
  18. Blockchain with Rust
  19. Near Protocol
    1. Startup code
    2. Couter
    3. Status
    4. Avrit
  20. Actix-web

Near Protocol


One to many Relationship
use near_sdk::collections::Map;
use near_sdk::collections::Vector;


#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProfileDetails {
    profile_tags: Map<String, ProductList>,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize, Debug)]
pub struct Product {
    product_name: String,
    product_details: String,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProductList {
    products: Vector<Product>,
}

#[near_bindgen]
impl ProfileDetails {
    pub fn set_profile(&mut self, product_name: String, product_details: String) {
        let account_id = String::from("amiyatulu.test");
        println!("{}", product_name);
        let p = Product {
            product_name,
            product_details,
        };
        let id = account_id.clone().into_bytes();
        let mut id_products = ProductList {
            products: Vector::new(id),
        };
        id_products.products.push(&p);
        self.profile_tags.insert(&account_id, &id_products);
    }

    pub fn push_product_to_profile(&mut self, product_name: String, product_details: String) {
        let account_id = String::from("amiyatulu.test");
        let p = Product {
            product_name,
            product_details,
        };
        let my_products_option = self.profile_tags.get(&account_id);
        match my_products_option {
            Some(mut my_products) => {
                my_products.products.push(&p);
                self.profile_tags.insert(&account_id, &my_products);
                println!("Hello myproducts push");
            }
            None => println!("Can't get the profile tag"),
        }
    }

    pub fn get_product_list(&mut self) {
        let account_id = String::from("amiyatulu.test");
        let my_products_option = self.profile_tags.get(&account_id);
        match my_products_option {
            Some(my_products) => {
                let data = my_products.products.get(0).unwrap();
                println!("{:?}", data);
                let data2 = my_products.products.get(1).unwrap();
                println!("{:?}", data2);
            }
            None => println!("Can't get the profile tag"),
        }
    }
}


#Test
#[test]
    fn profile() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = ProfileDetails::default();
        contract.set_profile("Cell biology".to_string(), "DNA".to_string());
        contract.push_product_to_profile("Mathematics".to_string(), "Set Theory".to_string());
        contract.get_product_list();
    }



Total Code:
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::Map;
use near_sdk::collections::Vector;
use near_sdk::{env, near_bindgen};
use serde::{Deserialize, Serialize};

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[derive(Serialize, Deserialize, Debug)]
pub struct TextMessage {
    text: String,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Welcome {
    records: Map<String, String>,
}

#[near_bindgen]
impl Welcome {
    pub fn set_greeting(&mut self, message: String) {
        let account_id = env::signer_account_id();
        self.records.insert(&account_id, &message);
    }

    pub fn welcome(&self, account_id: String) -> TextMessage {
        match self.records.get(&account_id) {
            None => {
                env::log(b"Using default message.");
                return TextMessage {
                    text: format!("Hello {}", account_id),
                };
            }
            _ => {
                return TextMessage {
                    text: format!("{} {}"self.records.get(&account_id).unwrap(), account_id),
                }
            }
        }
    }
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProfileDetails {
    profile_tags: Map<String, ProductList>,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize, Debug)]
pub struct Product {
    product_name: String,
    product_details: String,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProductList {
    products: Vector<Product>,
}

#[near_bindgen]
impl ProfileDetails {
    pub fn set_profile(&mut self, product_name: String, product_details: String) {
        let account_id = String::from("amiyatulu.test");
        println!("{}", product_name);
        let p = Product {
            product_name,
            product_details,
        };
        let id = account_id.clone().into_bytes();
        let mut id_products = ProductList {
            products: Vector::new(id),
        };
        id_products.products.push(&p);
        self.profile_tags.insert(&account_id, &id_products);
    }

    pub fn push_product_to_profile(&mut self, product_name: String, product_details: String) {
        let account_id = String::from("amiyatulu.test");
        let p = Product {
            product_name,
            product_details,
        };
        let my_products_option = self.profile_tags.get(&account_id);
        match my_products_option {
            Some(mut my_products) => {
                my_products.products.push(&p);
                self.profile_tags.insert(&account_id, &my_products);
                println!("Hello myproducts push");
            }
            None => println!("Can't get the profile tag"),
        }
    }

    pub fn get_product_list(&mut self) {
        let account_id = String::from("amiyatulu.test");
        let my_products_option = self.profile_tags.get(&account_id);
        match my_products_option {
            Some(my_products) => {
                let data = my_products.products.get(0).unwrap();
                println!("{:?}", data);
                let data2 = my_products.products.get(1).unwrap();
                println!("{:?}", data2);
            }
            None => println!("Can't get the profile tag"),
        }
    }
}

#[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![012],
            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![012],
            is_view,
            epoch_height: 0,
            output_data_receivers: vec![],
        }
    }

    #[test]
    fn set_get_message() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = Welcome::default();
        contract.set_greeting("howdy".to_string());
        assert_eq!(
            "howdy bob_near".to_string(),
            contract.welcome("bob_near".to_string()).text
        );
    }

    #[test]
    fn get_nonexistent_message() {
        let context = get_context(vec![], true);
        testing_env!(context);
        let contract = Welcome::default();
        println!(
            "Hello World {:?}",
            contract.welcome("francis.near".to_string())
        );
        assert_eq!(
            "Hello francis.near".to_string(),
            contract.welcome("francis.near".to_string()).text
        );
    }

    #[test]
    fn profile() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = ProfileDetails::default();
        contract.set_profile("Cell biology".to_string(), "DNA".to_string());
        contract.push_product_to_profile("Mathematics".to_string(), "Set Theory".to_string());
        contract.get_product_list();
    }
}



New code:
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::{Map, Set};
use near_sdk::{env, near_bindgen};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[derive(Serialize, Deserialize, Debug)]
pub struct TextMessage {
    text: String,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Welcome {
    records: Map<String, String>,
}


#[near_bindgen]
impl Welcome {
    pub fn set_greeting(&mut self, message: String) {
        let account_id = env::signer_account_id();
        self.records.insert(&account_id, &message);
    }

    pub fn welcome(&self, account_id: String) -> TextMessage {
        match self.records.get(&account_id) {
            None => {
                env::log(b"Using default message.");
                return TextMessage {
                    text: format!("Hello {}", account_id),
                };
            }
            _ => {
                return TextMessage {
                    text: format!("{} {}"self.records.get(&account_id).unwrap(), account_id),
                }
            }
        }
    }
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Profile {
    profile_map: Map<String, ProfileDetails>,
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProductMap {
    product_map: Map<String, Product>,
}


#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize, Debug)]
pub struct Product {
    product_tag: String,
    product_details: String, //IPFS Hash
}

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProfileDetails {
    profile_hash: String, //IPFS Hash
    products: Set<ProductMap>,
}

#[near_bindgen]
impl Profile {
    pub fn set_profile(&mut self, product_tag: String, product_details: String) {
        let seed = env::random_seed();
        println!("{:?}", seed);
        let account_id = String::from("amiyatulu.test");
        println!("{}", product_tag);
        let p = Product {
            product_tag,
            product_details,
        };
        let id = Uuid::new_v4().to_string().into_bytes();
        let mut p_map = Map::new(id);
        let id2 =  Uuid::new_v4().to_string().into_bytes();
        p_map.insert(&"productid124243".to_string(), &p);
        let pmap = ProductMap { product_map: p_map };
        let mut id_products = ProfileDetails {
            products: Set::new(id2),
            profile_hash: "IPFSHASH".to_string(),
        };
        id_products.products.insert(&pmap);
        self.profile_map.insert(&account_id, &id_products);
    }

    pub fn push_product_to_profile(&mut self, product_tag: String, product_details: String) {
        let account_id = String::from("amiyatulu.test");
        let p = Product {
            product_tag,
            product_details,
        };
        let id = Uuid::new_v4().to_string().into_bytes();
        let mut p_map = Map::new(id);
        p_map.insert(&"productidabc".to_string(), &p);
        let pmap = ProductMap { product_map: p_map };
        let profile_details_option = self.profile_map.get(&account_id);
        match profile_details_option {
            Some(mut profile_details) => {
                // let productmapset = profile_details.products.to_vec();
                // for i in 0..productmapset.len() {
                //     println!("{:?}", productmapset[i].product_map.to_vec());
                // }
                profile_details.products.insert(&pmap);
                self.profile_map.insert(&account_id, &profile_details);

            }
            None => println!("Can't get profile details"),
        }
    }

    pub fn get_product_list(&mut self) {
        let account_id = String::from("amiyatulu.test");
        let my_products_option = self.profile_map.get(&account_id);
        match my_products_option {
            Some(my_products) => {
                let data = my_products.products.to_vec();
                for i in 0..data.len() {
                    println!("{:?}", data[i].product_map.to_vec());
                }
            }
            None => println!("Can't get the profile tag"),
        }
    }
}

#[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![012],
            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![012],
            is_view,
            epoch_height: 0,
            output_data_receivers: vec![],
        }
    }

    #[test]
    fn set_get_message() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = Welcome::default();
        contract.set_greeting("howdy".to_string());
        assert_eq!(
            "howdy bob_near".to_string(),
            contract.welcome("bob_near".to_string()).text
        );
    }

    #[test]
    fn get_nonexistent_message() {
        let context = get_context(vec![], true);
        testing_env!(context);
        let contract = Welcome::default();
        println!(
            "Hello World {:?}",
            contract.welcome("francis.near".to_string())
        );
        assert_eq!(
            "Hello francis.near".to_string(),
            contract.welcome("francis.near".to_string()).text
        );
    }

    #[test]
    fn profile() {
        let context = get_context(vec![], false);
        testing_env!(context);
        let mut contract = Profile::default();
        contract.set_profile("Cell biology".to_string(), "DNA".to_string());
        contract.push_product_to_profile("Mathematics".to_string(), "Set Theory".to_string());
        contract.get_product_list();
    }
}