-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
executable file
·274 lines (260 loc) · 9.51 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
// PackedLayout
#[ink::contract]
mod chocolate {
use ink_storage::traits::{PackedLayout, SpreadAllocate, SpreadLayout};
use ink_storage::Mapping;
// Ref: https://github.com/paritytech/ink/blob/master/examples/mother/Cargo.toml
use ink_prelude::vec::Vec;
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[derive(Default, SpreadAllocate)]
#[ink(storage)]
pub struct Chocolate {
project_index: u32,
/// Stores a project from id in storage
projects: Mapping<u32, Project>,
// Multivalued keys would allow wider scope for reviews
/// Index in reviews_projects_list arr. -> struct
reviews: Mapping<u32, Review>,
/// Accountid + projectId
reviews_projects_list: Vec<(AccountId, u32)>,
}
#[derive(
Debug,
PartialEq,
scale::Encode,
scale::Decode,
Clone,
SpreadLayout,
PackedLayout,
SpreadAllocate,
)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink_storage::traits::StorageLayout,)
)]
pub struct ReviewProject {
review_id: u32,
project_id: u32,
}
#[derive(
Debug,
PartialEq,
scale::Encode,
scale::Decode,
Clone,
SpreadLayout,
PackedLayout,
SpreadAllocate,
Default,
)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink_storage::traits::StorageLayout,)
)]
pub struct Review {
id: u32,
body: Vec<u8>,
rating: u32,
owner: AccountId,
}
#[derive(
Debug,
PartialEq,
scale::Encode,
scale::Decode,
Clone,
SpreadLayout,
PackedLayout,
SpreadAllocate,
Default,
)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink_storage::traits::StorageLayout,)
)]
pub struct Project {
review_count: u32,
rating_sum: u32,
owner: AccountId,
meta: Vec<u8>,
name: Vec<u8>,
}
/// Errors that can occur upon calling this contract.
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
pub enum Error {
/// Returned if the name already exists upon registration.
ProjectAlreadyExists,
/// Returned if caller is not owner while required to.
ReviewAlreadyExists,
/// Queried project does not exist
ProjectDoesNotExist,
/// Queried Review does not exist
ReviewDoesNotExist,
}
/// Type alias for the contract's result type.
pub type Result<T> = core::result::Result<T, Error>;
impl Chocolate {
/// Constructor that initializes the contract;
#[ink(constructor)]
pub fn new() -> Self {
ink_lang::utils::initialize_contract(|_| {})
}
/// Constructor that initializes the `bool` value to `false`.
///
/// Constructors can delegate to other constructors.
#[ink(constructor)]
pub fn default() -> Self {
ink_lang::utils::initialize_contract(|_| {})
}
/// A message that can be called on instantiated contracts.
/// This one flips the value of the stored `bool` from `true`
/// to `false` and vice versa.
#[ink(message)]
pub fn flip(&mut self) -> Result<()> {
self.add_project("CHOC".bytes().collect(), Default::default())
}
/// Simply returns the current value of our `project`.
#[ink(message)]
pub fn get_project(&self, id: u32) -> Result<Project> {
let maybe_project = self.projects.get(id);
match maybe_project {
None => Err(Error::ProjectDoesNotExist),
Some(project) => Ok(project),
}
}
#[ink(message)]
pub fn get_review(&self, project_id: u32, user: AccountId) -> Result<Review> {
let maybe_index = self
.reviews_projects_list
.iter()
.position(|s| s.0.eq(&user) && s.1.eq(&project_id));
match maybe_index {
None => Err(Error::ReviewDoesNotExist),
Some(index) => {
let as_key: u32 = index.try_into().expect("Should fit");
let maybe_review = self.reviews.get(as_key);
let review = maybe_review.expect("Valid keys should have review entries");
Ok(review)
}
}
}
#[ink(message)]
pub fn add_project(&mut self, name: Vec<u8>, meta: Vec<u8>) -> Result<()> {
let caller = self.env().caller();
let index = self.project_index;
let project = Project {
owner: caller,
name,
meta,
..Default::default()
};
self.projects.insert(index, &project);
self.project_index += 1;
Ok(())
}
#[ink(message)]
pub fn add_review(&mut self, body: Vec<u8>, rating: u32, project_id: u32) -> Result<()> {
let caller = self.env().caller();
let maybe_project = self.projects.get(project_id);
match maybe_project {
None => Err(Error::ProjectDoesNotExist),
Some(mut project) => {
let key = (caller.clone(), project_id);
match self.reviews_projects_list.binary_search(&key) {
Err(index) => {
let as_key_t: u32 =
index.try_into().expect("Vec should not exceed u32 size??"); // not sure.
project.rating_sum += rating;
project.review_count += 1;
let review = Review {
owner: caller,
body,
rating,
id: as_key_t,
};
self.reviews_projects_list.insert(index, key);
self.projects.insert(project_id, &project);
self.reviews.insert(as_key_t, &review);
Ok(())
}
Ok(_) => Err(Error::ReviewAlreadyExists),
}
}
}
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// Imports `ink_lang` so we can use `#[ink::test]`.
use ink_lang as ink;
fn default_accounts() -> ink_env::test::DefaultAccounts<ink_env::DefaultEnvironment> {
ink_env::test::default_accounts::<Environment>()
}
fn set_next_caller(caller: AccountId) {
ink_env::test::set_caller::<Environment>(caller);
}
/// We test if the default constructor does its job.
#[ink::test]
fn default_works() {
let chocolate = Chocolate::default();
assert_eq!(chocolate.get_project(0), Err(Error::ProjectDoesNotExist));
}
/// We test a simple use case of our contract. Adds a default project 0.
#[ink::test]
fn it_works() {
let default_accounts = default_accounts();
set_next_caller(default_accounts.alice);
let mut chocolate = Chocolate::new();
assert_eq!(chocolate.get_project(0), Err(Error::ProjectDoesNotExist));
chocolate.flip().expect("Should add project 0");
assert_eq!(
chocolate.get_project(0),
Ok(Project {
owner: default_accounts.alice,
name: "CHOC".to_owned().into_bytes(),
meta: Default::default(),
..Default::default()
})
);
assert_eq!(chocolate.project_index, 1);
}
#[ink::test]
fn it_works_review() {
let default_accounts = default_accounts();
set_next_caller(default_accounts.alice);
let mut chocolate = Chocolate::new();
assert_eq!(
chocolate.get_review(0, default_accounts.alice.clone()),
Err(Error::ReviewDoesNotExist)
);
chocolate.flip().expect("Should add test project 0");
chocolate
.add_review(Default::default(), 10, 0)
.expect("Adding review should succeed");
let maybe_key = chocolate
.reviews_projects_list
.iter()
.position(|s| s.0.eq(&default_accounts.alice) && s.1.eq(&0));
assert_eq!(maybe_key, Some(0)); // first key array.
assert_eq!(
chocolate.get_review(0, default_accounts.alice),
Ok(Review {
owner: default_accounts.alice,
body: Default::default(),
rating: 10,
id: maybe_key.unwrap().try_into().expect("Should fit"),
})
)
}
}
}