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
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]
#![allow(clippy::upper_case_acronyms)]
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub use pallet_asset_management as Assets;
pub use pallet_identity as Ident;
pub use pallet_nft as Nft;
pub use pallet_payment as Payment;
pub use pallet_roles as Roles;
pub use pallet_share_distributor as Share;
mod functions;
mod types;
pub use functions::*;
pub use types::*;
pub mod weights;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config:
frame_system::Config
+ Assets::Config
+ Ident::Config
+ Roles::Config
+ Nft::Config
+ Payment::Config
{
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type WeightInfo: WeightInfo;
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
}
#[pallet::storage]
#[pallet::getter(fn infos)]
pub type Tenants<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, RegisteredTenant<T>, OptionQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
GuarantyDepositPayment {
tenant: T::AccountId,
when: BlockNumberOf<T>,
asset_account: T::AccountId,
amount: Payment::BalanceOf<T>,
},
AssetRequested { tenant: T::AccountId, when: BlockNumberOf<T>, asset_account: T::AccountId },
RentPayment {
tenant: T::AccountId,
when: BlockNumberOf<T>,
asset_account: T::AccountId,
amount: Roles::BalanceOf<T>,
remaining: Roles::BalanceOf<T>,
},
}
#[pallet::error]
pub enum Error<T> {
NoneValue,
StorageOverflow,
NotAnAsset,
NotATenant,
NotARepresentative,
AssetNotLinked,
NotAValidPayment,
NoRentToPay,
TenantAssetNotLinked,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn pay_rent(origin: OriginFor<T>) -> DispatchResult {
let tenant_account = ensure_signed(origin.clone())?;
let tenant = Roles::Pallet::<T>::tenants(tenant_account.clone()).unwrap();
ensure!(tenant.asset_account.is_some(), Error::<T>::TenantAssetNotLinked);
ensure!(tenant.remaining_payments > 0, Error::<T>::NoRentToPay);
Self::rent_helper(tenant_account.clone()).ok();
let now = <frame_system::Pallet<T>>::block_number();
Self::deposit_event(Event::RentPayment {
tenant: tenant_account,
when: now,
asset_account: tenant.asset_account.unwrap(),
amount: tenant.rent,
remaining: tenant.remaining_rent,
});
Ok(())
}
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn request_asset(
origin: OriginFor<T>,
info: Box<IdentityInfo<T::MaxAdditionalFields>>,
asset_type: Nft::PossibleCollections,
asset_id: T::NftItemId,
) -> DispatchResult {
let caller = ensure_signed(origin.clone())?;
ensure!(Roles::TenantLog::<T>::contains_key(caller.clone()), Error::<T>::NotATenant);
let collection_id: T::NftCollectionId = asset_type.value().into();
let ownership = Share::Pallet::<T>::virtual_acc(collection_id, asset_id);
ensure!(ownership.is_some(), Error::<T>::NotAnAsset);
let virtual_account = ownership.unwrap().virtual_account;
let vacancy = Assets::Pallet::<T>::fetch_house(collection_id,asset_id).max_tenants;
ensure!(vacancy > 0, Assets::Error::<T>::MaximumNumberOfTenantsReached);
if !Tenants::<T>::contains_key(caller.clone()) {
RegisteredTenant::<T>::new(
caller.clone(),
info.clone(),
Some(virtual_account.clone()),
)
.ok();
} else {
let mut val0 = Self::infos(&caller).unwrap();
Tenants::<T>::mutate(&caller, |val| {
val0.asset_requested = Some(virtual_account.clone());
*val = Some(val0);
});
}
Self::request_helper(origin.clone(), virtual_account.clone(), info).ok();
let now = <frame_system::Pallet<T>>::block_number();
Self::deposit_event(Event::AssetRequested {
tenant: caller,
when: now,
asset_account: virtual_account,
});
Ok(())
}
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn pay_guaranty_deposit(
origin: OriginFor<T>,
asset_type: Nft::PossibleCollections,
asset_id: T::NftItemId,
) -> DispatchResult {
let caller = ensure_signed(origin.clone())?;
ensure!(Roles::TenantLog::<T>::contains_key(&caller), Error::<T>::NotATenant);
let collection_id: T::NftCollectionId = asset_type.value().into();
let ownership = Share::Pallet::<T>::virtual_acc(collection_id, asset_id);
ensure!(ownership.is_some(), Error::<T>::NotAnAsset);
let virtual_account = ownership.unwrap().virtual_account;
ensure!(
Assets::GuarantyPayment::<T>::contains_key(&caller, &virtual_account),
Error::<T>::NotAValidPayment
);
let payment_infos = Assets::Pallet::<T>::guaranty(&caller, &virtual_account).unwrap();
let status = payment_infos.state;
ensure!(
status == Payment::PaymentState::PaymentRequested,
Error::<T>::NotAValidPayment
);
Self::payment_helper(origin, virtual_account.clone(), collection_id, asset_id).ok();
let now = <frame_system::Pallet<T>>::block_number();
Self::deposit_event(Event::GuarantyDepositPayment {
tenant: caller,
when: now,
asset_account: virtual_account,
amount: payment_infos.amount,
});
Ok(())
}
}
}