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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
use super::*;

use crate::types::*;
use frame_support::{pallet_prelude::*, traits::UnixTime};
use frame_system::pallet_prelude::*;
use pallet_fruniques::types::{Attributes, CollectionDescription, FruniqueRole, ParentInfo};
use pallet_gated_marketplace::types::MarketplaceRole;
// use frame_support::traits::OriginTrait;
use core::convert::TryInto;
use frame_support::sp_io::hashing::blake2_256;
use frame_support::traits::Time;
use pallet_mapped_assets::DebitFlags;
use pallet_rbac::types::IdOrVec;
use pallet_rbac::types::RoleBasedAccessControl;
use pallet_rbac::types::RoleId;
use scale_info::prelude::vec;
use sp_runtime::sp_std::str;
use sp_runtime::sp_std::vec::Vec;
use sp_runtime::traits::Zero;
impl<T: Config> Pallet<T> {
  pub fn do_initial_setup(creator: T::AccountId, admin: T::AccountId) -> DispatchResult {
    Self::initialize_rbac()?;

    let creator_user: User<T> = User {
      cid: ShortString::try_from(b"afloat".to_vec()).unwrap(),
      cid_creator: ShortString::try_from(b"HCD:afloat".to_vec()).unwrap(),
      group: ShortString::try_from(b"afloat".to_vec()).unwrap(),
      created_by: Some(creator.clone()),
      created_date: Some(T::TimeProvider::now().as_secs()),
      last_modified_by: Some(creator.clone()),
      last_modified_date: Some(T::TimeProvider::now().as_secs()),
    };
    <UserInfo<T>>::insert(creator.clone(), creator_user);
    Self::give_role_to_user(creator.clone(), AfloatRole::Owner)?;

    if admin != creator {
      let admin_user: User<T> = User {
        cid: ShortString::try_from(b"afloat".to_vec()).unwrap(),
        cid_creator: ShortString::try_from(b"afloat".to_vec()).unwrap(),
        group: ShortString::try_from(b"afloat".to_vec()).unwrap(),
        created_by: Some(admin.clone()),
        created_date: Some(T::TimeProvider::now().as_secs()),
        last_modified_by: Some(admin.clone()),
        last_modified_date: Some(T::TimeProvider::now().as_secs()),
      };
      <UserInfo<T>>::insert(admin.clone(), admin_user);
      Self::give_role_to_user(admin, AfloatRole::Admin)?;
    }

    Ok(())
  }

  // ! User management

  /// This function creates a new user with the given actor, user address, and sign up arguments.
  ///
  /// # Inputs
  ///
  /// * `actor` - An account ID of the user who initiated this action.
  /// * `user_address` - An account ID of the user to be created.
  /// * `args` - Sign up arguments. It could be either a `BuyerOrSeller` or a `CPA`, and contains
  ///            the first name, last name, email, and state of the user.
  ///
  /// # Errors
  ///
  /// This function may return an error if there is an issue with the `pallet_gated_marketplace`
  /// pallet, which is used to enroll the user in the Afloat marketplace. It may also return an
  /// error if the user already exists.
  ///
  /// # Returns
  ///
  /// Returns `Ok(())` on success.
  ///
  pub fn do_create_user(
    actor: T::AccountId,
    user_address: T::AccountId,
    args: SignUpArgs,
  ) -> DispatchResult {
    ensure!(!<UserInfo<T>>::contains_key(user_address.clone()), Error::<T>::UserAlreadyExists);
    match args {
      SignUpArgs::BuyerOrSeller { cid, cid_creator, group } => {
        let user: User<T> = User {
          cid,
          cid_creator,
          group,
          created_by: Some(actor.clone()),
          created_date: Some(T::TimeProvider::now().as_secs()),
          last_modified_by: Some(actor.clone()),
          last_modified_date: Some(T::TimeProvider::now().as_secs()),
        };
        <UserInfo<T>>::insert(user_address.clone(), user);
        Self::give_role_to_user(user_address.clone(), AfloatRole::BuyerOrSeller)?;
        Self::deposit_event(Event::NewUser(user_address.clone()));
      },
      SignUpArgs::CPA { cid, cid_creator, group } => {
        let user: User<T> = User {
          cid,
          cid_creator,
          group,
          created_by: Some(actor.clone()),
          created_date: Some(T::TimeProvider::now().as_secs()),
          last_modified_by: Some(actor.clone()),
          last_modified_date: Some(T::TimeProvider::now().as_secs()),
        };
        <UserInfo<T>>::insert(user_address.clone(), user);
        Self::give_role_to_user(user_address.clone(), AfloatRole::CPA)?;
        Self::deposit_event(Event::NewUser(user_address.clone()));
      },
    }

    let marketplace_id =
      AfloatMarketPlaceId::<T>::get().ok_or(Error::<T>::MarketPlaceIdNotFound)?;

    Self::add_to_afloat_collection(user_address.clone(), FruniqueRole::Collaborator)?;
    pallet_gated_marketplace::Pallet::<T>::self_enroll(user_address, marketplace_id)?;

    Ok(())
  }
  /// Function for editing user information.
  ///
  /// - `actor`: The `AccountId` of the actor performing the edit.
  /// - `user_address`: The `AccountId` of the user account to edit.
  /// - `first_name`: An optional `ShortString` containing the user's first name.
  /// - `last_name`: An optional `ShortString` containing the user's last name.
  /// - `email`: An optional `LongString` containing the user's email address.
  /// - `lang_key`: An optional `ShortString` containing the language code for the user.
  /// - `phone`: An optional `Option<ShortString>` containing the user's phone number, or None if no phone number is provided.
  /// - `credits_needed`: An optional `u32` containing the number of credits needed for the user's account.
  /// - `cpa_id`: An optional `ShortString` containing the user's CPA ID.
  /// - `state`: An optional `u32` containing the user's state tax authority ID.
  ///
  /// # Errors
  ///
  /// Returns an `Error` if the requested user account is not found or if the edit operation fails.
  ///
  /// # Returns
  ///
  /// Returns `Ok(())` on success.
  ///
  pub fn do_edit_user(
    actor: T::AccountId,
    user_address: T::AccountId,
    cid: ShortString,
    cid_creator: ShortString,
  ) -> DispatchResult {
    <UserInfo<T>>::try_mutate::<_, _, DispatchError, _>(user_address.clone(), |user| {
      let user = user.as_mut().ok_or(Error::<T>::FailedToEditUserAccount)?;

      user.last_modified_date = Some(T::TimeProvider::now().as_secs());
      user.last_modified_by = Some(actor.clone());
      user.cid = cid;
      user.cid_creator = cid_creator;

      Ok(())
    })?;

    Ok(())
  }

  pub fn do_admin_edit_user(
    actor: T::AccountId,
    user_address: T::AccountId,
    cid: ShortString,
    cid_creator: ShortString,
    group: ShortString,
  ) -> DispatchResult {
    <UserInfo<T>>::try_mutate::<_, _, DispatchError, _>(user_address.clone(), |user| {
      let user = user.as_mut().ok_or(Error::<T>::FailedToEditUserAccount)?;

      user.last_modified_date = Some(T::TimeProvider::now().as_secs());
      user.last_modified_by = Some(actor.clone());
      user.cid = cid;
      user.cid_creator = cid_creator;
      user.group = group;

      Ok(())
    })?;

    Ok(())
  }
  /// Function for deleting a user account.
  ///
  /// - _actor: The AccountId of the actor performing the deletion. This parameter is currently unused.
  /// - user_address: The AccountId of the user account to delete.
  ///
  /// # Errors
  ///
  /// Returns an Error if the requested user account is not found.
  ///
  /// # Returns
  ///
  /// Returns Ok(()) on success.
  ///
  pub fn do_delete_user(_actor: T::AccountId, user_address: T::AccountId) -> DispatchResult {
    Self::remove_from_afloat_collection(user_address.clone(), FruniqueRole::Collaborator)?;
    Self::remove_from_afloat_marketplace(user_address.clone())?;

    let user_roles = Self::get_all_roles_for_user(user_address.clone())?;

    if !user_roles.is_empty() {
      for role in user_roles {
        Self::remove_role_from_user(user_address.clone(), role)?;
      }
    }
    <UserInfo<T>>::remove(user_address.clone());
    Self::deposit_event(Event::UserDeleted(user_address.clone()));
    Ok(())
  }

  pub fn do_set_afloat_balance(
    origin: OriginFor<T>,
    user_address: T::AccountId,
    amount: T::Balance,
  ) -> DispatchResult {
    let authority = ensure_signed(origin.clone())?;
    let asset_id = AfloatAssetId::<T>::get().expect("AfloatAssetId should be set");
    let debit_flags = DebitFlags { keep_alive: false, best_effort: true };
    ensure!(UserInfo::<T>::contains_key(user_address.clone()), Error::<T>::UserNotFound);

    let current_balance = Self::do_get_afloat_balance(user_address.clone());
    if current_balance > amount {
      let diff = current_balance - amount;
      pallet_mapped_assets::Pallet::<T>::afloat_do_burn(
        asset_id.into(),
        &user_address.clone(),
        diff,
        Some(authority.clone()),
        debit_flags,
      )?;
    } else if current_balance < amount {
      let diff = amount - current_balance;
      pallet_mapped_assets::Pallet::<T>::afloat_do_mint(
        asset_id.into(),
        &user_address.clone(),
        diff,
        Some(authority.clone()),
      )?;
    }

    Self::deposit_event(Event::AfloatBalanceSet(authority, user_address, amount));
    Ok(())
  }

  pub fn do_get_afloat_balance(user_address: T::AccountId) -> T::Balance {
    let asset_id = AfloatAssetId::<T>::get().expect("AfloatAssetId should be set");
    pallet_mapped_assets::Pallet::<T>::balance(asset_id.into(), user_address)
  }

  pub fn do_create_sell_order(
    authority: T::AccountId,
    item_id: <T as pallet_uniques::Config>::ItemId,
    price: T::Balance,
    tax_credit_amount: u32,
    expiration_date: Date,
  ) -> DispatchResult {
    let maybe_roles = Self::get_all_roles_for_user(authority.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    let transactions = TransactionBoundedVec::default();

    let offer: Offer<T> = Offer {
      tax_credit_amount,
      tax_credit_amount_remaining: tax_credit_amount.into(),
      price_per_credit: price,
      creation_date: T::TimeProvider::now().as_secs(),
      expiration_date,
      tax_credit_id: item_id,
      creator_id: authority.clone(),
      status: OfferStatus::default(),
      offer_type: OfferType::Sell,
      cancellation_date: None,
      transactions,
    };

    let offer_id = offer.using_encoded(blake2_256);
    <AfloatOffers<T>>::insert(offer_id, offer);

    Self::deposit_event(Event::SellOrderCreated(authority));

    Ok(())
  }

  pub fn do_create_buy_order(
    authority: T::AccountId,
    item_id: <T as pallet_uniques::Config>::ItemId,
    price: T::Balance,
    tax_credit_amount: u32,
    expiration_date: Date,
  ) -> DispatchResult {
    let maybe_roles = Self::get_all_roles_for_user(authority.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    let transactions = TransactionBoundedVec::default();

    let offer: Offer<T> = Offer {
      tax_credit_amount,
      tax_credit_amount_remaining: tax_credit_amount.into(),
      price_per_credit: price,
      creation_date: T::TimeProvider::now().as_secs(),
      expiration_date,
      tax_credit_id: item_id,
      creator_id: authority.clone(),
      status: OfferStatus::default(),
      offer_type: OfferType::Buy,
      cancellation_date: None,
      transactions,
    };

    let offer_id = offer.using_encoded(blake2_256);

    <AfloatOffers<T>>::insert(offer_id, offer);

    Self::deposit_event(Event::BuyOrderCreated(authority));

    Ok(())
  }

  /// Starts the process of taking a sell order.
  ///
  /// # Arguments
  ///
  /// * `authority` - The origin of the call, from where the function is triggered.
  /// * `order_id` - The unique identifier of the order.
  /// * `tax_credit_amount` - The amount of tax credit to to take/buy from the original offer.
  ///
  /// # Return
  ///
  /// * Returns a `DispatchResult` to indicate the success or failure of the operation.
  ///
  /// # Errors
  ///
  /// This function will return an error if:
  /// * The caller does not have any roles.
  /// * The specified offer does not exist.
  /// * The specified offer is not a sell offer.
  /// * The specified offer has expired.
  /// * The specified offer has been cancelled.
  /// * The specified offer has already been taken.
  /// * The specified offer does not have enough tax credits available for sale.
  /// * The caller does not have enough afloat balance to take the offer.
  ///
  /// # Side Effects
  ///
  /// * If the function is successful, it will mutate the state of the order and create a transaction.
  ///
  /// # Panics
  ///
  /// * This function does not panic.
  ///
  /// # Safety
  ///
  /// * This function does not use any unsafe blocks.
  ///
  pub fn do_start_take_sell_order(
    authority: OriginFor<T>,
    order_id: [u8; 32],
    tax_credit_amount: T::Balance,
  ) -> DispatchResult
  where
    <T as pallet_uniques::Config>::ItemId: From<u32>,
  {
    let who = ensure_signed(authority.clone())?;

    let maybe_roles = Self::get_all_roles_for_user(who.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);
    // ensure offer exists
    ensure!(<AfloatOffers<T>>::contains_key(order_id), Error::<T>::OfferNotFound);
    //get offer details
    let offer = <AfloatOffers<T>>::get(order_id).unwrap();
    //ensure offer is a sell offer
    ensure!(offer.offer_type == OfferType::Sell, Error::<T>::WrongOfferType);
    //ensure offer is not expired
    ensure!(offer.expiration_date > T::TimeProvider::now().as_secs(), Error::<T>::OfferExpired);
    //ensure offer is not cancelled
    ensure!(offer.cancellation_date.is_none(), Error::<T>::OfferCancelled);
    //ensure offer is not taken
    ensure!(offer.status == OfferStatus::default(), Error::<T>::OfferTaken);
    //ensure offer has enough tax credits for sale
    ensure!(
      offer.tax_credit_amount_remaining >= tax_credit_amount,
      Error::<T>::NotEnoughTaxCreditsAvailable
    );
    //ensure user has enough afloat balance
    ensure!(
      Self::do_get_afloat_balance(who.clone()) >= offer.price_per_credit * tax_credit_amount.into(),
      Error::<T>::NotEnoughAfloatBalanceAvailable
    );
    let zero_balance: T::Balance = Zero::zero();
    //ensure tax credit amount is greater than zero
    ensure!(tax_credit_amount > zero_balance, Error::<T>::Underflow);

    let creation_date: u64 = T::Timestamp::now().into();
    let price_per_credit: T::Balance = offer.price_per_credit.into();
    let total_price: T::Balance = price_per_credit * tax_credit_amount;
    let fee: Option<T::Balance> = None;
    let tax_credit_id: <T as pallet_uniques::Config>::ItemId = offer.tax_credit_id;
    let seller_id: T::AccountId = offer.creator_id;
    let buyer_id: T::AccountId = who.clone();
    let offer_id: StorageId = order_id;
    let seller_confirmation_date: Option<Date> = None;
    let buyer_confirmation_date: Option<Date> = Some(creation_date);
    let confirmed: bool = false;
    let completed: bool = false;
    let child_offer_id: Option<StorageId> = None;

    let transaction = Transaction {
      tax_credit_amount,
      price_per_credit,
      total_price,
      fee,
      creation_date,
      cancellation_date: None,
      tax_credit_id,
      seller_id,
      buyer_id,
      offer_id,
      child_offer_id,
      seller_confirmation_date,
      buyer_confirmation_date,
      confirmed,
      completed,
    };

    let transaction_id = transaction.clone().using_encoded(blake2_256);

    <AfloatOffers<T>>::try_mutate(order_id, |offer| -> DispatchResult {
      let offer = offer.as_mut().ok_or(Error::<T>::OfferNotFound)?;
      offer
        .transactions
        .try_push(transaction_id.clone())
        .map_err(|_| Error::<T>::MaxTransactionsReached)?;
      Ok(())
    })?;

    <AfloatTransactions<T>>::insert(transaction_id, transaction);

    Ok(())
  }

  /// Confirms a sell transaction.
  ///
  /// # Arguments
  ///
  /// * `authority` - The origin of the call, from where the function is triggered.
  /// * `transaction_id` - The unique identifier of the transaction.
  ///
  /// # Return
  ///
  /// * Returns a `DispatchResult` to indicate the success or failure of the operation.
  ///
  /// # Errors
  ///
  /// This function will return an error if:
  /// * The caller does not have any roles.
  /// * The specified transaction does not exist.
  /// * The caller is not the seller in the transaction.
  /// * The specified transaction has been cancelled.
  /// * The specified transaction has already been confirmed by the seller.
  /// * The specified transaction has not been confirmed by the buyer.
  /// * The `AfloatMarketPlaceId` or `AfloatCollectionId` does not exist.
  /// * The tax credit amount overflows when converting from ``T::Balance`` to `u32`.
  ///
  /// # Side Effects
  ///
  /// * If the function is successful, it will mutate the state of the transaction, setting the seller confirmation date,
  /// confirming the transaction, and linking the transaction to a new child offer.
  ///
  /// # Panics
  ///
  /// * This function does not panic.
  ///
  /// # Safety
  ///
  /// * This function does not use any unsafe blocks.
  ///
  /// # Note
  ///
  /// * Before calling this function, make sure that the transaction_id exists and the caller is the seller.
  ///
  pub fn do_confirm_sell_transaction(
    authority: OriginFor<T>,
    transaction_id: [u8; 32],
  ) -> DispatchResult
  where
    <T as pallet_uniques::Config>::ItemId: From<u32>,
  {
    let who = ensure_signed(authority.clone())?;

    let maybe_roles = Self::get_all_roles_for_user(who.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    // Ensure the transaction exists before trying to get it
    ensure!(<AfloatTransactions<T>>::contains_key(transaction_id), Error::<T>::TransactionNotFound);

    // Get transaction details
    let transaction =
      <AfloatTransactions<T>>::get(transaction_id).ok_or(Error::<T>::TransactionNotFound)?;

    // Ensure user is the seller
    ensure!(transaction.seller_id == who.clone(), Error::<T>::Unauthorized);

    // Ensure transaction is not cancelled
    ensure!(transaction.cancellation_date.is_none(), Error::<T>::TransactionCancelled);

    // Ensure transaction is not already confirmed by the seller
    ensure!(
      transaction.seller_confirmation_date.is_none(),
      Error::<T>::TransactionAlreadyConfirmedBySeller
    );

    // Ensure transaction has buyer confirmation
    ensure!(
      transaction.buyer_confirmation_date.is_some(),
      Error::<T>::TransactionNotConfirmedByBuyer
    );

    let confirmation_date: u64 = T::Timestamp::now().into();
    let confirmed: bool = true;

    let marketplace_id =
      <AfloatMarketPlaceId<T>>::get().ok_or(Error::<T>::MarketPlaceIdNotFound)?;
    let collection_id = <AfloatCollectionId<T>>::get().ok_or(Error::<T>::CollectionIdNotFound)?;

    let tax_credit_amount_u32 = if let Ok(amount) = transaction.tax_credit_amount.try_into() {
      amount
    } else {
      return Err(Error::<T>::TaxCreditAmountOverflow.into());
    };

    let child_offer_id = pallet_gated_marketplace::Pallet::<T>::do_enlist_sell_offer(
      who,
      marketplace_id,
      collection_id,
      transaction.tax_credit_id,
      transaction.total_price,
      tax_credit_amount_u32,
    )?;

    <AfloatTransactions<T>>::try_mutate(transaction_id, |transaction| -> DispatchResult {
      let mut transaction = transaction.as_mut().ok_or(Error::<T>::TransactionNotFound)?;
      transaction.seller_confirmation_date = Some(confirmation_date);
      transaction.confirmed = confirmed;
      transaction.child_offer_id = Some(child_offer_id);
      Ok(())
    })?;

    Ok(())
  }

  /// Finishes the process of taking a sell transaction.
  ///
  /// # Arguments
  ///
  /// * `authority` - The origin of the call, from where the function is triggered.
  /// * `transaction_id` - The unique identifier of the transaction.
  ///
  /// # Return
  ///
  /// * Returns a `DispatchResult` to indicate the success or failure of the operation.
  ///
  /// # Errors
  ///
  /// This function will return an error if:
  /// * The caller does not have any roles.
  /// * The specified transaction does not exist.
  /// * The specified transaction has been cancelled.
  /// * The specified transaction has not been confirmed.
  /// * The child offer id in the transaction does not exist.
  /// * The specified offer does not exist.
  /// * The tax credit amount in the offer is less than the tax credit amount in the transaction (underflow).
  ///
  /// # Side Effects
  ///
  /// * If the function is successful, it will trigger the transfer of tax credits and Balance between buyer and seller,
  ///  mutate the state of the offer and transaction and emit a `SellOrderTaken` event.
  ///
  /// # Panics
  ///
  /// * This function does not panic.
  ///
  /// # Safety
  ///
  /// * This function does not use any unsafe blocks.
  ///
  /// # Note
  ///
  /// * Before calling this function, make sure that the transaction id exists, the transaction is confirmed, and the caller is authorized.
  ///
  pub fn do_finish_take_sell_transaction(
    authority: OriginFor<T>,
    transaction_id: [u8; 32],
  ) -> DispatchResult
  where
    <T as pallet_uniques::Config>::ItemId: From<u32>,
  {
    let who = ensure_signed(authority.clone())?;

    let maybe_roles = Self::get_all_roles_for_user(who.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    // Ensure the transaction exists before trying to get it
    ensure!(<AfloatTransactions<T>>::contains_key(transaction_id), Error::<T>::TransactionNotFound);

    // Get transaction details
    let transaction =
      <AfloatTransactions<T>>::get(transaction_id).ok_or(Error::<T>::TransactionNotFound)?;

    // Ensure transaction is not cancelled
    ensure!(transaction.cancellation_date.is_none(), Error::<T>::TransactionCancelled);

    // Ensure transaction is confirmed
    ensure!(transaction.confirmed, Error::<T>::TransactionNotConfirmed);

    // Ensure the child offer id exists
    let child_offer_id = transaction.child_offer_id.ok_or(Error::<T>::ChildOfferIdNotFound)?;
    let offer_id = transaction.offer_id;

    pallet_gated_marketplace::Pallet::<T>::do_take_sell_offer(authority.clone(), child_offer_id)?;

    <AfloatOffers<T>>::try_mutate(offer_id, |offer| -> DispatchResult {
      let offer = offer.as_mut().ok_or(Error::<T>::OfferNotFound)?;
      if transaction.tax_credit_amount > offer.tax_credit_amount_remaining {
        return Err(Error::<T>::Underflow.into());
      }
      offer.tax_credit_amount_remaining =
        offer.tax_credit_amount_remaining - transaction.tax_credit_amount;
      Ok(())
    })?;

    <AfloatTransactions<T>>::try_mutate(transaction_id, |transaction| -> DispatchResult {
      let mut transaction = transaction.as_mut().ok_or(Error::<T>::TransactionNotFound)?;
      transaction.completed = true;
      Ok(())
    })?;

    Self::deposit_event(Event::SellOrderTaken(who));
    Ok(())
  }

  pub fn do_take_buy_order(authority: T::AccountId, order_id: [u8; 32]) -> DispatchResult
  where
    <T as pallet_uniques::Config>::ItemId: From<u32>,
  {
    let maybe_roles = Self::get_all_roles_for_user(authority.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    pallet_gated_marketplace::Pallet::<T>::do_take_buy_offer(authority.clone(), order_id)?;

    Self::deposit_event(Event::BuyOrderTaken(authority));
    Ok(())
  }

  pub fn do_create_tax_credit(
    owner: T::AccountId,
    metadata: CollectionDescription<T>,
    attributes: Option<Attributes<T>>,
    parent_info: Option<ParentInfo<T>>,
  ) -> DispatchResult
  where
    <T as pallet_uniques::Config>::ItemId: From<u32>,
    <T as pallet_uniques::Config>::CollectionId: From<u32>,
  {
    let maybe_roles = Self::get_all_roles_for_user(owner.clone())?;
    ensure!(!maybe_roles.is_empty(), Error::<T>::Unauthorized);

    let collection = AfloatCollectionId::<T>::get().ok_or(Error::<T>::CollectionIdNotFound)?;

    pallet_fruniques::Pallet::<T>::do_spawn(collection, owner, metadata, attributes, parent_info)
  }

  pub fn create_afloat_collection(
    origin: OriginFor<T>,
    metadata: CollectionDescription<T>,
    admin: T::AccountId,
  ) -> DispatchResult
  where
    <T as pallet_uniques::Config>::CollectionId: From<u32>,
  {
    let collection_id =
      pallet_fruniques::Pallet::<T>::do_create_collection(origin.clone(), metadata, admin.clone());
    if let Ok(collection_id) = collection_id {
      AfloatCollectionId::<T>::put(collection_id);
      Ok(())
    } else {
      return Err(Error::<T>::FailedToCreateFruniquesCollection.into());
    }
  }

  pub fn add_to_afloat_collection(invitee: T::AccountId, role: FruniqueRole) -> DispatchResult {
    let collection_id = AfloatCollectionId::<T>::get().ok_or(Error::<T>::CollectionIdNotFound)?;
    pallet_fruniques::Pallet::<T>::insert_auth_in_frunique_collection(invitee, collection_id, role)
  }

  pub fn remove_from_afloat_collection(
    invitee: T::AccountId,
    role: FruniqueRole,
  ) -> DispatchResult {
    let collection_id = AfloatCollectionId::<T>::get().ok_or(Error::<T>::CollectionIdNotFound)?;
    pallet_fruniques::Pallet::<T>::remove_auth_from_frunique_collection(
      invitee,
      collection_id,
      role,
    )
  }

  pub fn remove_from_afloat_marketplace(invitee: T::AccountId) -> DispatchResult {
    let marketplace_id =
      AfloatMarketPlaceId::<T>::get().ok_or(Error::<T>::MarketPlaceIdNotFound)?;
    pallet_gated_marketplace::Pallet::<T>::remove_from_market_lists(
      invitee,
      MarketplaceRole::Participant,
      marketplace_id,
    )
  }

  pub fn is_admin_or_owner(account: T::AccountId) -> Result<bool, DispatchError> {
    let maybe_super_role = <T as pallet::Config>::Rbac::has_role(
      account.clone(),
      Self::pallet_id(),
      &Self::scope_id(),
      [AfloatRole::Admin.id(), AfloatRole::Owner.id()].to_vec(),
    );

    Ok(maybe_super_role.is_ok())
  }

  pub fn is_owner(account: T::AccountId) -> Result<bool, DispatchError> {
    let maybe_owner = <T as pallet::Config>::Rbac::has_role(
      account.clone(),
      Self::pallet_id(),
      &Self::scope_id(),
      [AfloatRole::Owner.id()].to_vec(),
    );

    Ok(maybe_owner.is_ok())
  }

  pub fn is_cpa(account: T::AccountId) -> Result<bool, DispatchError> {
    let maybe_cpa = <T as pallet::Config>::Rbac::has_role(
      account.clone(),
      Self::pallet_id(),
      &Self::scope_id(),
      [AfloatRole::CPA.id()].to_vec(),
    );

    Ok(maybe_cpa.is_ok())
  }

  pub fn give_role_to_user(authority: T::AccountId, role: AfloatRole) -> DispatchResult {
    <T as pallet::Config>::Rbac::assign_role_to_user(
      authority,
      Self::pallet_id(),
      &Self::scope_id(),
      role.id(),
    )?;

    Ok(())
  }

  pub fn do_add_afloat_admin(
    authority: T::AccountId,
    user_address: T::AccountId,
  ) -> DispatchResult {
    //ensure user is registered
    ensure!(UserInfo::<T>::contains_key(user_address.clone()), Error::<T>::UserNotFound);

    Self::give_role_to_user(user_address.clone(), AfloatRole::Admin)?;

    Self::deposit_event(Event::AdminAdded(authority, user_address));
    Ok(())
  }

  pub fn remove_role_from_user(authority: T::AccountId, role: AfloatRole) -> DispatchResult {
    <T as pallet::Config>::Rbac::remove_role_from_user(
      authority,
      Self::pallet_id(),
      &Self::scope_id(),
      role.id(),
    )?;

    Ok(())
  }

  fn scope_id() -> [u8; 32] {
    "AfloatScope".as_bytes().using_encoded(blake2_256)
  }

  fn pallet_id() -> IdOrVec {
    IdOrVec::Vec("AfloatPallet".as_bytes().to_vec())
  }

  pub fn remove_rbac_permissions() -> DispatchResult {
    <T as pallet::Config>::Rbac::remove_pallet_storage(Self::pallet_id())?;
    Ok(())
  }

  pub fn initialize_rbac() -> DispatchResult {
    let pallet_id = Self::pallet_id();
    let scope_id = Self::scope_id();
    <T as pallet::Config>::Rbac::create_scope(Self::pallet_id(), scope_id)?;
    let super_roles = vec![AfloatRole::Owner.to_vec(), AfloatRole::Admin.to_vec()];
    let super_role_ids =
      <T as pallet::Config>::Rbac::create_and_set_roles(pallet_id.clone(), super_roles)?;
    let super_permissions = Permission::admin_permissions();
    for super_role in super_role_ids {
      <T as pallet::Config>::Rbac::create_and_set_permissions(
        pallet_id.clone(),
        super_role,
        super_permissions.clone(),
      )?;
    }
    let participant_roles = vec![AfloatRole::BuyerOrSeller.to_vec(), AfloatRole::CPA.to_vec()];
    <T as pallet::Config>::Rbac::create_and_set_roles(pallet_id.clone(), participant_roles)?;

    Ok(())
  }

  fn role_id_to_afloat_role(role_id: RoleId) -> Option<AfloatRole> {
    AfloatRole::enum_to_vec()
      .iter()
      .find(|role_bytes| role_bytes.using_encoded(blake2_256) == role_id)
      .map(|role_bytes| {
        let role_str = str::from_utf8(role_bytes).expect("Role bytes should be valid UTF-8");

        match role_str {
          "Owner" => AfloatRole::Owner,
          "Admin" => AfloatRole::Admin,
          "BuyerOrSeller" => AfloatRole::BuyerOrSeller,
          "CPA" => AfloatRole::CPA,
          _ => panic!("Unexpected role string"),
        }
      })
  }

  fn get_all_roles_for_user(account_id: T::AccountId) -> Result<Vec<AfloatRole>, DispatchError> {
    let roles_storage = <T as pallet::Config>::Rbac::get_roles_by_user(
      account_id.clone(),
      Self::pallet_id(),
      &Self::scope_id(),
    );
    Ok(roles_storage.into_iter().filter_map(Self::role_id_to_afloat_role).collect())
  }

  pub fn do_delete_all_users() -> DispatchResult {
    UserInfo::<T>::iter_keys().try_for_each(|account_id| {
      let is_admin_or_owner = Self::is_admin_or_owner(account_id.clone())?;

      if !is_admin_or_owner {
        let user_roles = Self::get_all_roles_for_user(account_id.clone())?;

        if !user_roles.is_empty() {
          for role in user_roles {
            Self::remove_role_from_user(account_id.clone(), role)?;
          }
        }

        Self::remove_from_afloat_collection(account_id.clone(), FruniqueRole::Collaborator)?;
        Self::remove_from_afloat_marketplace(account_id.clone())?;
        UserInfo::<T>::remove(account_id);
      }
      Ok::<(), DispatchError>(())
    })?;
    Ok(())
  }
}