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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
pub use super::*;
pub use frame_support::pallet_prelude::*;
#[allow(unused_imports)]
use num_traits::float::FloatCore;
pub use scale_info::prelude::boxed::Box;
pub use sp_core::H256;
use sp_runtime::traits::{StaticLookup, Zero};
impl<T: Config> Pallet<T> {
pub fn approve_representative_role(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
let caller = ensure_signed(origin)?;
let mut representative = Roles::Pallet::<T>::get_pending_representatives(&who).unwrap();
Roles::RepApprovalList::<T>::remove(&who);
let who2 = T::Lookup::unlookup(who.clone());
let mut index = Roles::Pallet::<T>::rep_num();
let registered = Roles::RepresentativeLog::<T>::contains_key(&who);
if !registered {
representative.activated = true;
representative.assets_accounts.clear();
representative.assets_accounts.push(caller);
representative.index = index;
Roles::RepresentativeLog::<T>::insert(&who, representative);
Roles::AccountsRolesLog::<T>::insert(&who, Roles::Accounts::REPRESENTATIVE);
} else {
representative.assets_accounts.push(caller);
Roles::RepresentativeLog::<T>::mutate(&who, |val| {
*val = Some(representative);
})
}
let mut check0 = false;
let v = Ident::Pallet::<T>::registrars();
for i in v {
let reg = i.unwrap();
if reg.account == who.clone() {
check0 = true;
}
}
if !check0 {
let origin_root: OriginFor<T> = frame_system::RawOrigin::Root.into();
Ident::Pallet::<T>::add_registrar(origin_root, who2).ok();
let origin2: OriginFor<T> = RawOrigin::Signed(who).into();
Ident::Pallet::<T>::set_fields(origin2.clone(), index, Default::default()).ok();
let fee0 = Self::manage_bal_to_u128(T::RepFees::get()).unwrap();
let bals0 = BalanceType::<T>::convert_to_balance(fee0);
let fees = bals0.ident_bal;
Ident::Pallet::<T>::set_fee(origin2, index, fees).ok();
if !registered {
index += 1;
Roles::RepNumber::<T>::put(index);
}
}
Ok(())
}
pub fn revoke_representative_role(who: T::AccountId) -> DispatchResult {
Roles::RepresentativeLog::<T>::mutate(&who, |val| {
let mut val0 = val.clone().unwrap();
val0.activated = false;
*val = Some(val0);
});
Roles::AccountsRolesLog::<T>::remove(&who);
Ok(())
}
pub fn fetch_house(collection: T::NftCollectionId, item: T::NftItemId) -> Onboarding::Asset<T>{
Onboarding::Pallet::<T>::houses(collection, item).unwrap()
}
pub fn calculate_guaranty(collection: T::NftCollectionId, item: T::NftItemId) -> u128 {
let coeff = T::Guaranty::get() as u128;
let ror = T::RoR::get();
let price0 = Self::fetch_house(collection, item).price.unwrap();
let price1 = Self::onboarding_bal_to_u128(ror.mul_floor(price0)).unwrap();
let time = <T as Config>::Lease::get();
let rent = ((price1 as f64) / time as f64).round();
let amount: u128 = coeff * (rent as u128);
amount
}
pub fn guaranty_payment(
origin: OriginFor<T>,
from: T::AccountId,
collection: T::NftCollectionId,
item: T::NftItemId,
) -> DispatchResult {
let creator = ensure_signed(origin.clone())?;
let amount = Self::calculate_guaranty(collection, item);
let bals0 = BalanceType::<T>::convert_to_balance(amount);
let amount1 = bals0.payment_bal;
Payment::Pallet::<T>::request_payment(origin, from.clone(), amount1).ok();
let details = Payment::Pallet::<T>::get_payment_details(&from, &creator).unwrap();
GuarantyPayment::<T>::insert(from, creator, details);
Ok(())
}
pub fn owners_infos(asset_account: T::AccountId) -> Option<Share::Ownership<T>> {
let assets = Share::Virtual::<T>::iter_keys();
let mut infos = None;
for (i, j) in assets {
let ownership = Share::Pallet::<T>::virtual_acc(i, j).unwrap();
if asset_account.clone() == ownership.virtual_account {
infos = Some(ownership);
}
}
infos
}
pub fn vote_helper(share: u128, vote: bool) -> Option<Dem::Vote> {
match share {
50..=100 => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked1x }),
101..=150 => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked2x }),
151..=300 => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked3x }),
301..=350 => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked4x }),
351..=400 => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked5x }),
401.. => Some(Dem::Vote { aye: vote, conviction: Dem::Conviction::Locked6x }),
_ => None,
}
}
pub fn tenant_link_asset(
tenant: T::AccountId,
collection: T::NftCollectionId,
item: T::NftItemId,
asset_account: T::AccountId,
) -> DispatchResult {
let ror = T::RoR::get();
Roles::TenantLog::<T>::mutate(&tenant, |val| {
let mut val0 = val.clone().unwrap();
let price0 = Self::fetch_house(collection, item).price.unwrap();
let price1 = Self::onboarding_bal_to_u128(ror.mul_floor(price0)).unwrap();
let time = <T as Config>::Lease::get();
let rent0 = ((price1 as f64) / time as f64).round();
let rent1 = (rent0 as u128) * time as u128;
let now = <frame_system::Pallet<T>>::block_number();
let mut bals = BalanceType::<T>::convert_to_balance(rent0 as u128);
let rent = bals.roles_bal;
bals = BalanceType::<T>::convert_to_balance(rent1);
let year_rent = bals.roles_bal;
val0.rent = rent;
val0.asset_account = Some(asset_account);
val0.remaining_rent = year_rent;
val0.remaining_payments = time as u8;
val0.contract_start = now;
*val = Some(val0);
});
Onboarding::Houses::<T>::mutate(collection, item, |house| {
let mut house0 = house.clone().unwrap();
house0.tenants.push(tenant);
house0.max_tenants -= 1;
*house = Some(house0);
});
Ok(())
}
pub fn tenant_unlink_asset(
tenant: T::AccountId,
collection: T::NftCollectionId,
item: T::NftItemId,
) -> DispatchResult {
Roles::TenantLog::<T>::mutate(&tenant, |val| {
let mut val0 = val.clone().unwrap();
val0.asset_account = None;
*val = Some(val0);
});
Onboarding::Houses::<T>::mutate(collection, item, |house| {
let mut house0 = house.clone().unwrap();
house0.tenants.retain(|t| *t != tenant);
house0.max_tenants += 1;
*house = Some(house0);
});
Ok(())
}
pub fn create_proposal_hash_and_note(
caller: T::AccountId,
proposal_call: pallet::Call<T>,
) -> T::Hash {
let origin: <T as frame_system::Config>::Origin = RawOrigin::Signed(caller.clone()).into();
let proposal = Box::new(Self::get_formatted_call(proposal_call.into()));
let call = Call::<T>::execute_call_dispatch { account_id: caller, proposal };
let call_formatted = Self::get_formatted_call(call.into());
let call_dispatch = Box::new(call_formatted);
let proposal_hash = T::Hashing::hash_of(&call_dispatch);
let proposal_encoded: Vec<u8> = call_dispatch.encode();
Dem::Pallet::<T>::note_preimage(origin, proposal_encoded).ok();
proposal_hash
}
pub fn caller_can_vote(caller: &T::AccountId, ownership: Share::Ownership<T>) -> bool {
let owners = ownership.owners;
owners.contains(caller)
}
pub fn manage_bal_to_u128(input: BalanceOf<T>) -> Option<u128> {
input.try_into().ok()
}
pub fn assets_bal_to_u128(input: <T as Assetss::Config>::Balance) -> Option<u128> {
input.try_into().ok()
}
pub fn roles_bal_to_u128(input: Roles::BalanceOf<T>) -> Option<u128> {
input.try_into().ok()
}
pub fn onboarding_bal_to_u128(input: Onboarding::BalanceOf<T>) -> Option<u128> {
input.try_into().ok()
}
pub fn blocknumber_to_u128(input: BlockNumberFor<T>) -> Option<u128> {
input.try_into().ok()
}
pub fn get_formatted_call(call: <T as Config>::Call) -> <T as Config>::Call {
call
}
pub fn begin_block(now: T::BlockNumber) -> Weight {
let max_block_weight = Weight::from_ref_time(1000_u64);
if (now % <T as Config>::CheckPeriod::get()).is_zero() {
let indexes = ProposalsIndexes::<T>::iter();
for index in indexes {
let ref_infos: RefInfos<T> = Dem::Pallet::<T>::referendum_info(index.1).unwrap();
let b = match ref_infos {
pallet_democracy::ReferendumInfo::Finished { approved, end: _ } => {
(1, approved)
},
_ => (0, false),
};
if b.0 == 1 {
ProposalsLog::<T>::mutate(index.1, |val| {
let mut val0 = val.clone().unwrap();
if b.1 {
val0.vote_result = VoteResult::ACCEPTED
} else {
val0.vote_result = VoteResult::REJECTED
}
*val = Some(val0)
});
}
}
}
max_block_weight
}
pub fn finish_block(now: T::BlockNumber) -> Weight {
if (now % <T as Config>::CheckPeriod::get()).is_zero() {
let tenants = Roles::Pallet::<T>::tenant_list();
for i in tenants {
let tenant = Roles::Pallet::<T>::tenants(i).unwrap();
if tenant.asset_account.is_some() {
let time = <T as Config>::Lease::get();
let remaining_p = tenant.remaining_payments;
let contract_begin = tenant.contract_start;
let rent = Self::roles_bal_to_u128(tenant.rent).unwrap() * time as u128;
let rent_float = rent as f64;
let rent0 = Self::roles_bal_to_u128(tenant.rent).unwrap();
let total_blocks = <T as Config>::ContractLength::get();
let mut rpb = Self::blocknumber_to_u128(total_blocks).unwrap();
let mut rpb_float = rpb as f64;
rpb_float = (rent_float / rpb_float).round();
rpb = rpb_float as u128;
let blocks = Self::blocknumber_to_u128(now - contract_begin).unwrap();
let amount_due = blocks.saturating_mul(rpb);
let payed = (time as u128 - remaining_p as u128) * rent;
let asset_account = tenant.asset_account.clone().unwrap();
let asset_account_free_balance =
<T as Config>::Currency::free_balance(&asset_account);
let infos = Self::owners_infos(asset_account.clone()).unwrap();
if infos.rent_nbr > 0 {
let owners = infos.owners;
let bals0 = BalanceType::<T>::convert_to_balance(rent0);
let rent1 = bals0.manage_bal;
let token_id = infos.token_id;
let total_issuance = Assetss::Pallet::<T>::total_supply(token_id.into());
let total_issuance_float =
Self::assets_bal_to_u128(total_issuance).unwrap() as f64;
let maintenance = T::Maintenance::get() * rent1;
let distribute = rent1.saturating_sub(maintenance);
let distribute_float = (Self::manage_bal_to_u128(distribute).unwrap()
* infos.rent_nbr as u128) as f64;
debug_assert!(distribute > Zero::zero());
debug_assert!(distribute < rent1);
debug_assert!(maintenance < asset_account_free_balance);
let reservation =
<T as Config>::Currency::reserve(&asset_account, maintenance);
Self::deposit_event(Event::MaintenanceFeesPayment {
tenant: tenant.account_id.clone(),
when: now,
asset_account: tenant.asset_account.unwrap(),
amount: maintenance,
});
debug_assert!(reservation.is_ok());
for i in owners.clone() {
let share = Assetss::Pallet::<T>::balance(token_id.into(), &i);
let share_float = Self::assets_bal_to_u128(share).unwrap() as f64
/ total_issuance_float;
let amount_float = share_float * distribute_float;
let bals0 = BalanceType::<T>::convert_to_balance(amount_float as u128);
let amount = bals0.manage_bal;
<T as Config>::Currency::transfer(
&asset_account,
&i,
amount,
ExistenceRequirement::AllowDeath,
)
.ok();
}
Self::deposit_event(Event::RentDistributed {
owners,
amount: distribute,
when: now,
});
let ownership_infos = Share::Virtual::<T>::iter_keys();
for (i, j) in ownership_infos {
let infos = Share::Pallet::<T>::virtual_acc(i, j).unwrap();
if infos.virtual_account == asset_account {
Share::Virtual::<T>::mutate(i, j, |val| {
let mut val0 = val.clone().unwrap();
val0.rent_nbr = 0;
*val = Some(val0);
});
}
}
}
if payed < amount_due && (now % <T as Config>::RentCheck::get()).is_zero() {
let tenant_debt0 = amount_due - payed;
let bals0 = BalanceType::<T>::convert_to_balance(tenant_debt0);
let debt = bals0.manage_bal;
Self::deposit_event(Event::TenantDebt {
tenant: tenant.account_id,
debt,
when: now,
});
}
}
}
}
Weight::zero()
}
}