Skip to content

Commit 4b62161

Browse files
committed
Initial contract
1 parent 04609c6 commit 4b62161

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "tutorial4"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[lib]
9+
crate-type = ["cdylib", "rlib"]
10+
11+
[dependencies]
12+
near-sdk = "3.1.0"
13+
near-contract-standards = "4.0.0-pre.3"
14+
15+
[profile.release]
16+
codegen-units = 1
17+
# Tell `rustc` to optimize for small code size.
18+
opt-level = "z"
19+
lto = true
20+
debug = false
21+
panic = "abort"
22+
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
23+
overflow-checks = true

src/info.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
2+
use near_sdk::serde::{Serialize, Deserialize};
3+
4+
5+
#[derive(BorshDeserialize, BorshSerialize)]
6+
#[derive(Serialize, Deserialize)]
7+
#[serde(crate = "near_sdk::serde")]
8+
pub struct InformationV1 {
9+
pub name: String,
10+
pub age: u8
11+
}

src/lib.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
2+
use near_sdk::{near_bindgen,env, Balance, PanicOnDefault, AccountId,BorshStorageKey};
3+
use near_sdk::collections::UnorderedMap;
4+
use near_sdk::json_types::U128;
5+
6+
use info::*;
7+
mod info;
8+
9+
#[derive(BorshStorageKey, BorshSerialize)]
10+
pub enum StorageKeys {
11+
List
12+
}
13+
14+
15+
#[near_bindgen]
16+
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)]
17+
pub struct CountContract{
18+
pub count_num: usize,
19+
pub balance_send: Balance,
20+
pub info: UnorderedMap<AccountId,InformationV1>,
21+
22+
}
23+
24+
#[near_bindgen]
25+
impl CountContract{
26+
#[init]
27+
pub fn new() -> Self{
28+
Self{
29+
count_num: 0,
30+
balance_send: 0,
31+
info: UnorderedMap::new(StorageKeys::List),
32+
33+
}
34+
}
35+
36+
pub fn increment_count(&mut self, count: usize) {
37+
self.internal_increment_count(count);
38+
}
39+
40+
pub fn get_count(&self) -> usize {
41+
self.count_num
42+
}
43+
44+
#[payable]
45+
pub fn deposit(&mut self) {
46+
let deposit: Balance = env::attached_deposit();
47+
self.balance_send += deposit;
48+
}
49+
50+
pub fn add_info(&mut self,name: String, age: u8){
51+
let info = InformationV1 { name, age};
52+
53+
self.info.insert(&env::predecessor_account_id(), &info);
54+
}
55+
56+
57+
pub fn get_info(&self, account_id: AccountId) -> Option<InformationV1>{
58+
self.info.get(&account_id)
59+
}
60+
61+
62+
pub fn get_all_info(&self) -> Vec<InformationV1>{
63+
self.info
64+
.values_as_vector()
65+
.to_vec()
66+
}
67+
}
68+
69+
impl CountContract{
70+
pub fn internal_increment_count(&mut self, count: usize){
71+
self.count_num += count;
72+
}
73+
}

0 commit comments

Comments
 (0)