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
//1) create VA from nft collection & item Id's --> Done
//2) create tokens
//3) Use onboarding do_buy
//4) transfer tokens to owners
use super::*;
use enum_iterator::all;
#[allow(unused_imports)]
use num_traits::float::FloatCore;
use sp_runtime::{traits::SaturatedConversion, FixedPointNumber, FixedU128};

impl<T: Config> Pallet<T> {
	///The function below create a virtual account from the NFT collection and item id's
	pub fn virtual_account(
		collection_id: T::NftCollectionId,
		item_id: T::NftItemId,
	) -> DispatchResult {
		//Create virtual account
		let text0 = format!("{collection_id:?}_{:?}_account", item_id.clone());
		let bytes = text0.as_bytes();
		let array: &[u8; 8] = &bytes[0..8].try_into().unwrap();
		let account: T::AccountId = PalletId(*array).into_account_truncating();

		//Store account inside storage
		Ownership::<T>::new(collection_id, item_id, account.clone()).ok();
		Owners::<T>::new(account.clone()).ok();

		//The virtual account needs some initial funds to pay for asset creation fees
		//These funds could be provided by the FairSquare FeesAccount maintained in the
		//Onboarding pallet.
		let fees_account = Onboarding::Pallet::<T>::account_id();
		let fees = T::Fees::get();
		//Ensure that we have enough money in the fees_account
		let balance = <T as pallet::Config>::Currency::free_balance(&fees_account);
		ensure!(fees < balance, Error::<T>::NotEnoughFees);

		let res = <T as pallet::Config>::Currency::transfer(
			&fees_account,
			&account,
			fees,
			ExistenceRequirement::AllowDeath,
		);
		debug_assert!(res.is_ok());

		Ok(())
	}

	///This function executes all actions relatives to nft transfer from the seller to the virtual
	/// account
	pub fn nft_transaction(
		collection_id: T::NftCollectionId,
		item_id: T::NftItemId,
		virtual_id: T::AccountId,
	) -> DispatchResult {
		//Get collection
		let collection_vec = all::<Nft::PossibleCollections>().collect::<Vec<_>>();
		let _infos = Onboarding::Houses::<T>::get(collection_id, item_id).unwrap();
		let mut coll_id = Nft::PossibleCollections::HOUSES;
		for i in collection_vec.iter() {
			let val: T::NftCollectionId = i.value().into();
			if val == collection_id {
				coll_id = *i;
			}
		}
		//Execute NFT and money transfer
		Onboarding::Pallet::do_buy(coll_id, item_id, virtual_id, _infos).ok();

		Ok(())
	}

	///Collect contributors to the bid, and their shares
	pub fn owner_and_shares(
		collection_id: T::NftCollectionId,
		item_id: T::NftItemId,
		_total_tokens: <T as Assets::Config>::Balance,
	) -> Vec<(T::AccountId, u128)> {
		//Get owners and their reserved contribution to the bid

		let reservation_infos =
			HousingFund::Reservations::<T>::get((collection_id, item_id)).unwrap();
		let vec0 = reservation_infos.contributions;
		let price = reservation_infos.amount;
		let virtual_acc = Self::virtual_acc(collection_id, item_id).unwrap().virtual_account;

		let mut vec = Vec::new();
		for i in vec0.iter() {
			let price0 = Self::hfund_bal_to_u128(price).unwrap();
			let contribution0 = Self::hfund_bal_to_u128(i.1).unwrap();

			let price1 = Self::balance_to_f64_option0(price).unwrap();
			let contribution1 = Self::balance_to_f64_option0(i.1).unwrap();
			let frac = (contribution1 / price1).round();
			let mut share = FixedU128::saturating_from_rational(contribution0, price0)
				.saturating_mul_int(1000u128);
			let fl = share as f64;
			if (fl + 0.5) < frac {
				share += 1;
			}

			debug_assert!(share < 1000);
			debug_assert!(share > 0);

			vec.push((i.0.clone(), share));
			//Update Virtual_account storage
			Virtual::<T>::mutate(collection_id, item_id, |val| {
				let mut val0 = val.clone().unwrap();
				val0.owners.push(i.0.clone());
				*val = Some(val0);
			});
			//Update owners in Tokens storage
			Tokens::<T>::mutate(&virtual_acc, |val| {
				let amount: <T as Assets::Config>::Balance =
					share.saturated_into::<<T as Assets::Config>::Balance>();
				let mut val0 = val.clone().unwrap();
				val0.owners.push((i.0.clone(), amount));
				*val = Some(val0);
			});
		}
		vec
	}

	///Create 1000 Ownership tokens owned by a virtual account
	pub fn create_tokens(
		origin: OriginFor<T>,
		collection_id: T::NftCollectionId,
		item_id: T::NftItemId,
		account: T::AccountId,
	) -> DispatchResult {
		//Get token class Id:
		ensure!(Virtual::<T>::get(collection_id, item_id).is_some(), Error::<T>::InvalidValue);
		let token_id = Virtual::<T>::get(collection_id, item_id).unwrap().token_id;
		let to = T::Lookup::unlookup(account.clone());
		TokenId::<T>::mutate(|val| {
			let val0 = *val;
			*val = val0 + 1;
		});

		//Create token class
		let res = Assets::Pallet::<T>::force_create(
			origin.clone(),
			token_id.into(),
			to.clone(),
			true,
			One::one(),
		);
		debug_assert!(res.is_ok());

		//Set class metadata
		let token_name = format!("FairOwner_nbr{:?}", token_id.clone()).as_bytes().to_vec();
		let token_symbol = format!("FO{:?}", token_id.clone()).as_bytes().to_vec();
		let decimals = 1;
		Assets::Pallet::<T>::force_set_metadata(
			origin,
			token_id.into(),
			token_name,
			token_symbol,
			decimals,
			false,
		)
		.ok();

		//mint 1000 tokens
		let res0 = Assets::Pallet::<T>::mint(
			RawOrigin::Signed(account.clone()).into(),
			token_id.into(),
			to,
			Self::u32_to_balance_option(1000).unwrap(),
		);
		debug_assert!(res0.is_ok());

		//Update supply in Tokens storage
		Tokens::<T>::mutate(account, |val| {
			let mut val0 = val.clone().unwrap();
			val0.supply = Assets::Pallet::<T>::total_supply(token_id.into());
			*val = Some(val0);
		});

		Ok(())
	}

	///Distribute the ownership tokens to the group of new owners
	pub fn distribute_tokens(
		account: T::AccountId,
		collection_id: T::NftCollectionId,
		item_id: T::NftItemId,
	) -> DispatchResult {
		ensure!(Virtual::<T>::get(collection_id, item_id).is_some(), Error::<T>::InvalidValue);
		let token_id = Virtual::<T>::get(collection_id, item_id).unwrap().token_id;
		let total_tokens = Assets::Pallet::<T>::total_supply(token_id.into());
		debug_assert!(total_tokens == Self::u32_to_balance_option(1000).unwrap());
		let shares = Self::owner_and_shares(collection_id, item_id, total_tokens);

		let from = T::Lookup::unlookup(account.clone());
		let origin: OriginFor<T> = RawOrigin::Signed(account).into();

		for share in shares.iter() {
			let amount0 = share.clone().1;
			debug_assert!(amount0 > 0);
			let amount: <T as Assets::Config>::Balance =
				share.clone().1.saturated_into::<<T as Assets::Config>::Balance>();
			debug_assert!(!amount.clone().is_zero());
			let to = T::Lookup::unlookup(share.clone().0);
			Assets::Pallet::<T>::force_transfer(
				origin.clone(),
				token_id.into(),
				from.clone(),
				to,
				amount,
			)
			.ok();
		}

		Ok(())
	}

	// Conversion of u32 to Balance
	pub fn u32_to_balance_option(input: u32) -> Option<T::Balance> {
		input.try_into().ok()
	}

	// Conversion of BalanceOf<T> to u128
	pub fn hfund_bal_to_u128(input: HousingFund::BalanceOf<T>) -> Option<u128> {
		input.try_into().ok()
	}

	// Conversion of BalanceOf<T> to f64
	pub fn balance_to_f64_option0(input: HousingFund::BalanceOf<T>) -> Option<f64> {
		let integer: u64 = input.try_into().ok().unwrap();
		let float = integer as f64;
		Some(float)
	}
}