Skip to content

Commit c3220c3

Browse files
authored
Merge pull request #19 from flmel/update_rust_sdk
chore: update rust_sdk
2 parents c2c20dc + b5c429e commit c3220c3

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

contract-rs/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "contract-rs"
3-
description = "cargo-near-new-project-description"
3+
description = "Coin Flip Examples"
44
version = "0.1.0"
55
edition = "2021"
66
# TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project.
@@ -13,10 +13,10 @@ crate-type = ["cdylib", "rlib"]
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]
16-
near-sdk = { version = "5.0.0", features = ["legacy"] }
16+
near-sdk = { version = "5.1.0", features = ["legacy"] }
1717

1818
[dev-dependencies]
19-
near-sdk = { version = "5.0.0", features = ["unit-testing"] }
19+
near-sdk = { version = "5.1.0", features = ["unit-testing"] }
2020
near-workspaces = { version = "0.10.0", features = ["unstable"] }
2121
tokio = { version = "1.12.0", features = ["full"] }
2222
serde_json = "1"
@@ -29,4 +29,4 @@ lto = true
2929
debug = false
3030
panic = "abort"
3131
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
32-
overflow-checks = true
32+
overflow-checks = true

contract-rs/src/lib.rs

+33-36
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
// Find all our documentation at https://docs.near.org
2-
use near_sdk::borsh::{BorshSerialize, BorshDeserialize};
3-
use near_sdk::env::{self, log_str};
4-
use near_sdk::{near_bindgen, AccountId, BorshStorageKey};
52
use near_sdk::collections::UnorderedMap;
3+
use near_sdk::env::{self, log_str};
4+
use near_sdk::{near, AccountId, BorshStorageKey};
65

7-
#[derive(BorshSerialize, BorshStorageKey)]
8-
#[borsh(crate = "near_sdk::borsh")]
6+
#[near(serializers = [borsh])]
7+
#[derive(BorshStorageKey)]
98
enum StorageKey {
109
Points,
1110
}
1211

1312
pub(crate) fn simulate_coin_flip() -> String {
14-
// Here we get a first byte of a random seed
15-
let random_seed = *env::random_seed().get(0).unwrap() as i8;
13+
// Here we get a first byte of a random seed
14+
let random_seed = *env::random_seed().get(0).unwrap() as i8;
1615

17-
// If a first byte is EVEN we choose heads, otherwise tails
18-
if let 0 = random_seed % 2 {
19-
return "heads".to_string()
20-
} else {
21-
return "tails".to_string()
22-
};
16+
// If a first byte is EVEN we choose heads, otherwise tails
17+
if let 0 = random_seed % 2 {
18+
return "heads".to_string();
19+
} else {
20+
return "tails".to_string();
21+
};
2322
}
2423

2524
// Define the contract structure
26-
#[near_bindgen]
27-
#[derive(BorshDeserialize, BorshSerialize)]
28-
#[borsh(crate = "near_sdk::borsh")]
25+
#[near(contract_state)]
2926
pub struct Contract {
3027
points: UnorderedMap<AccountId, u8>,
3128
}
@@ -34,42 +31,42 @@ pub struct Contract {
3431
impl Default for Contract {
3532
fn default() -> Self {
3633
Self {
37-
points: UnorderedMap::new(StorageKey::Points)
34+
points: UnorderedMap::new(StorageKey::Points),
3835
}
3936
}
4037
}
4138

4239
// Implement the contract structure
43-
#[near_bindgen]
40+
#[near]
4441
impl Contract {
4542
/*
4643
Flip a coin. Pass in the side (heads or tails) and a random number will be chosen
4744
indicating whether the flip was heads or tails. If you got it right, you get a point.
4845
*/
4946
pub fn flip_coin(&mut self, player_guess: String) -> String {
50-
// Check who called the method
51-
let player: AccountId = env::predecessor_account_id();
52-
log_str(&format!("{player} chose {player_guess}"));
47+
// Check who called the method
48+
let player: AccountId = env::predecessor_account_id();
49+
log_str(&format!("{player} chose {player_guess}"));
50+
51+
// Simulate a Coin Flip
52+
let outcome = simulate_coin_flip();
5353

54-
// Simulate a Coin Flip
55-
let outcome = simulate_coin_flip();
56-
57-
// Get the current player points
58-
let mut player_points = self.points.get(&player).unwrap_or(0);
54+
// Get the current player points
55+
let mut player_points = self.points.get(&player).unwrap_or(0);
5956

60-
// Check if their guess was right and modify the points accordingly
61-
if outcome.eq(&player_guess) {
62-
player_points = player_points + 1;
63-
} else {
64-
player_points = player_points.saturating_sub(1);
65-
};
57+
// Check if their guess was right and modify the points accordingly
58+
if outcome.eq(&player_guess) {
59+
player_points = player_points + 1;
60+
} else {
61+
player_points = player_points.saturating_sub(1);
62+
};
6663

67-
log_str(&format!("player_points: {player_points}"));
64+
log_str(&format!("player_points: {player_points}"));
6865

69-
// Store the new points
70-
self.points.insert(&player, &player_points);
66+
// Store the new points
67+
self.points.insert(&player, &player_points);
7168

72-
return outcome;
69+
return outcome;
7370
}
7471

7572
// View how many points a specific player has

0 commit comments

Comments
 (0)