-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
executable file
·253 lines (225 loc) · 7.28 KB
/
Copy pathlib.rs
File metadata and controls
executable file
·253 lines (225 loc) · 7.28 KB
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
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]
#[macro_export]
macro_rules! ensure {
( $x:expr, $y:expr $(,)? ) => {{
if !$x {
return Err($y.into());
}
}};
}
#[openbrush::contract]
mod house_bidding {
use ink::{
prelude::{string::String, vec, vec::Vec},
storage::Mapping,
};
use openbrush::{
contracts::psp34::{Id, *},
traits::{Storage, ZERO_ADDRESS},
};
pub type HouseId = i32;
pub type BidderId = i32;
fn zero_address() -> AccountId {
[0; 32].into()
}
#[derive(scale::Decode, scale::Encode, Eq, PartialEq)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub enum HouseError {
HouseNotFound,
CantBidFurther,
StillBidding,
CantBidTwice,
ValueTooSmall,
BiddingLimitNotFulfill,
LowBidPriceThanPreviouse,
}
/// Bidder struct
#[derive(scale::Decode, scale::Encode, Eq, PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct Bidder {
bidder_id: BidderId,
bidder_account: AccountId,
bidder_amount: Balance,
}
impl Default for Bidder {
fn default() -> Self {
Bidder {
bidder_id: Default::default(),
bidder_account: zero_address(),
bidder_amount: Default::default(),
}
}
}
/// House struct
#[derive(scale::Decode, scale::Encode, Eq, PartialEq)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct House {
house_id: HouseId,
house_owner: AccountId,
house_title: String,
house_description: String,
rooms: i32,
special_features: Vec<String>,
initial_price: Balance,
bidder: Vec<Bidder>,
max_bid_price: Balance,
winner: AccountId,
}
impl Default for House {
fn default() -> Self {
House {
house_id: Default::default(),
house_owner: zero_address(),
house_title: Default::default(),
house_description: Default::default(),
rooms: Default::default(),
special_features: Vec::new(),
initial_price: Default::default(),
bidder: Vec::new(),
max_bid_price: Default::default(),
winner: zero_address(),
}
}
}
#[ink(storage)]
#[derive(Storage)]
pub struct HouseBidding {
owner: AccountId,
house_id: HouseId,
bidder_id: BidderId,
house: Mapping<HouseId, House>,
#[storage_field]
psp34: psp34::Data,
}
impl Default for HouseBidding {
fn default() -> Self {
HouseBidding {
owner: ZERO_ADDRESS.into(),
house_id: Default::default(),
bidder_id: Default::default(),
house: Mapping::default(),
psp34: Default::default(),
}
}
}
impl HouseBidding {
#[ink(constructor)]
pub fn new() -> Self {
let instance = Self::default();
instance
}
#[ink(message)]
pub fn mint_house(
&mut self,
house_title: String,
house_description: String,
rooms: i32,
initial_price: Balance,
special_features: Vec<String>,
) -> Result<(), HouseError> {
let house_owner = self.env().caller();
let house_id = self.next_house_id();
let house = House {
house_id,
house_owner,
house_title,
house_description,
rooms,
special_features,
initial_price,
bidder: vec![],
max_bid_price: 0,
winner: zero_address(),
};
self.house.insert(&house_id, &house);
Ok(())
}
#[ink(message, payable)]
pub fn bid(&mut self, house_id: HouseId) -> Result<(), HouseError> {
let caller = self.env().caller();
let bidder_id = self.next_bidder_id();
let bidder_amount = self.env().transferred_value();
match self.house.get(&house_id) {
None => return Err(HouseError::HouseNotFound),
Some(mut house) => {
ensure!(
bidder_amount >= house.initial_price,
HouseError::ValueTooSmall
);
for bid in house.bidder.clone() {
ensure!(
bidder_amount > bid.bidder_amount,
HouseError::LowBidPriceThanPreviouse
);
if bid.bidder_account == caller {
return Err(HouseError::CantBidTwice);
}
}
let bidder = Bidder {
bidder_id,
bidder_account: caller,
bidder_amount,
};
let bidder_len = house.bidder.len() as i32;
ensure!(bidder_len < 5, HouseError::CantBidFurther);
house.bidder.push(bidder);
self.house.insert(&house_id, &house);
}
};
Ok(())
}
#[ink(message)]
pub fn get_winner(&mut self, house_id: HouseId) -> Result<(), HouseError> {
match self.house.get(&house_id) {
None => return Err(HouseError::HouseNotFound),
Some(mut house) => {
if house.bidder.len() == 5 {
for bid in house.bidder.clone() {
if bid.bidder_amount > house.max_bid_price {
house.max_bid_price = bid.bidder_amount;
house.winner = bid.bidder_account;
self.house.insert(&house_id, &house);
} else {
return Err(HouseError::StillBidding);
}
}
} else {
return Err(HouseError::BiddingLimitNotFulfill);
}
}
};
Ok(())
}
#[ink(message)]
pub fn get_house(&self) -> Vec<House> {
let mut house_vec: Vec<House> = Vec::new();
for id in 0..self.house_id {
match self.house.get(&id) {
None => (),
Some(house) => house_vec.push(house),
}
}
house_vec
}
pub fn next_house_id(&mut self) -> HouseId {
let id = self.house_id;
self.house_id += 1;
id
}
pub fn next_bidder_id(&mut self) -> HouseId {
let id = self.bidder_id;
self.bidder_id += 1;
id
}
}
}