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
use super::*;
use frame_support::pallet_prelude::*;
//use frame_system::pallet_prelude::*;
use frame_support::sp_io::hashing::blake2_256;
use sp_runtime::sp_std::vec::Vec;

pub type Fields<T> = BoundedVec<(FieldName, Cid), <T as Config>::MaxFiles>;
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type FieldName = BoundedVec<u8, ConstU32<100>>;
pub type Cid = BoundedVec<u8, ConstU32<100>>;
pub type Cids<Len> = BoundedVec<Cid, Len>;
pub type CustodianFields<T> = (AccountIdOf<T>, Cids<<T as Config>::MaxFiles>);

pub type MarketplaceId = [u8; 32];
pub type ApplicationId = [u8; 32];
pub type OfferId = [u8; 32];
pub type RedemptionId = [u8; 32];

use sp_runtime::Permill;

#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct Marketplace<T: Config> {
  pub label: BoundedVec<u8, T::LabelMaxLen>,
  pub buy_fee: Permill,
  pub sell_fee: Permill,
  pub asset_id: T::AssetId,
  pub creator: T::AccountId,
}

#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct RedemptionData<T: Config> {
  pub creator: T::AccountId,
  pub redeemed_by: Option<T::AccountId>,
  pub collection_id: T::CollectionId,
  pub item_id: T::ItemId,
  pub is_redeemed: bool,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]

pub enum RedeemArgs<T: Config> {
  AskForRedemption { collection_id: T::CollectionId, item_id: T::ItemId },
  AcceptRedemption(RedemptionId),
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub enum BlockUserArgs<T: Config> {
  BlockUser(T::AccountId),
  UnblockUser(T::AccountId),
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub enum AccountOrApplication<T: Config> {
  Account(T::AccountId),
  Application([u8; 32]),
}

#[derive(
  Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
)]
pub enum MarketplaceRole {
  Owner,
  Admin,
  Appraiser,
  RedemptionSpecialist,
  Participant,
}

impl Default for MarketplaceRole {
  fn default() -> Self {
    MarketplaceRole::Participant
  }
}

impl MarketplaceRole {
  pub fn to_vec(self) -> Vec<u8> {
    match self {
      Self::Owner => "Owner".as_bytes().to_vec(),
      Self::Admin => "Admin".as_bytes().to_vec(),
      Self::Appraiser => "Appraiser".as_bytes().to_vec(),
      Self::RedemptionSpecialist => "Redemption_specialist".as_bytes().to_vec(),
      Self::Participant => "Participant".as_bytes().to_vec(),
    }
  }

  pub fn id(&self) -> [u8; 32] {
    self.to_vec().using_encoded(blake2_256)
  }

  pub fn enum_to_vec() -> Vec<Vec<u8>> {
    use crate::types::MarketplaceRole::*;
    [
      Owner.to_vec(),
      Admin.to_vec(),
      Appraiser.to_vec(),
      RedemptionSpecialist.to_vec(),
      Participant.to_vec(),
    ]
    .to_vec()
  }
}

/// Extrinsics which require previous authorization to call them
#[derive(
  Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
)]
pub enum Permission {
  Enroll,
  AddAuth,
  RemoveAuth,
  UpdateLabel,
  RemoveMarketplace,
  EnlistSellOffer,
  TakeSellOffer,
  DuplicateOffer,
  RemoveOffer,
  EnlistBuyOffer,
  TakeBuyOffer,
  AskForRedemption,
  AcceptRedemption,
  BlockUser,
}

impl Permission {
  pub fn to_vec(self) -> Vec<u8> {
    match self {
      Self::Enroll => "Enroll".as_bytes().to_vec(),
      Self::AddAuth => "AddAuth".as_bytes().to_vec(),
      Self::RemoveAuth => "RemoveAuth".as_bytes().to_vec(),
      Self::UpdateLabel => "UpdateLabel".as_bytes().to_vec(),
      Self::RemoveMarketplace => "RemoveMarketplace".as_bytes().to_vec(),
      Self::EnlistSellOffer => "EnlistSellOffer".as_bytes().to_vec(),
      Self::TakeSellOffer => "TakeSellOffer".as_bytes().to_vec(),
      Self::DuplicateOffer => "DuplicateOffer".as_bytes().to_vec(),
      Self::RemoveOffer => "RemoveOffer".as_bytes().to_vec(),
      Self::EnlistBuyOffer => "EnlistBuyOffer".as_bytes().to_vec(),
      Self::TakeBuyOffer => "TakeBuyOffer".as_bytes().to_vec(),
      Self::AskForRedemption => "AskForRedemption".as_bytes().to_vec(),
      Self::AcceptRedemption => "AcceptRedemption".as_bytes().to_vec(),
      Self::BlockUser => "BlockUser".as_bytes().to_vec(),
    }
  }

  pub fn id(&self) -> [u8; 32] {
    self.to_vec().using_encoded(blake2_256)
  }

  pub fn admin_permissions() -> Vec<Vec<u8>> {
    use crate::types::Permission::*;
    let mut admin_permissions = [
      Enroll.to_vec(),
      AddAuth.to_vec(),
      RemoveAuth.to_vec(),
      UpdateLabel.to_vec(),
      RemoveMarketplace.to_vec(),
      AcceptRedemption.to_vec(),
      BlockUser.to_vec(),
    ]
    .to_vec();
    admin_permissions.append(&mut Permission::participant_permissions());
    admin_permissions
  }

  pub fn participant_permissions() -> Vec<Vec<u8>> {
    use crate::types::Permission::*;
    [
      EnlistSellOffer.to_vec(),
      TakeSellOffer.to_vec(),
      DuplicateOffer.to_vec(),
      RemoveOffer.to_vec(),
      EnlistBuyOffer.to_vec(),
      TakeBuyOffer.to_vec(),
      AskForRedemption.to_vec(),
    ]
    .to_vec()
  }
}

#[derive(
  CloneNoBound, Encode, Decode, Eq, PartialEq, RuntimeDebugNoBound, Default, TypeInfo, MaxEncodedLen,
)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct Application<T: Config> {
  pub status: ApplicationStatus,
  pub fields: BoundedVec<ApplicationField, T::MaxFiles>,
  pub feedback: BoundedVec<u8, T::MaxFeedbackLen>,
}

#[derive(
  Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
)]
pub enum ApplicationStatus {
  Pending,
  Approved,
  Rejected,
}

impl Default for ApplicationStatus {
  fn default() -> Self {
    ApplicationStatus::Pending
  }
}

#[derive(
  CloneNoBound, Encode, Decode, Eq, RuntimeDebugNoBound, Default, TypeInfo, MaxEncodedLen,
)]
pub struct ApplicationField {
  pub display_name: BoundedVec<u8, ConstU32<100>>,
  pub cid: Cid,
  pub custodian_cid: Option<Cid>,
}
// Eq macro didnt work (binary operation `==` cannot be applied to type...)
impl PartialEq for ApplicationField {
  fn eq(&self, other: &Self) -> bool {
    self.cid == other.cid && self.display_name == other.display_name
  }
}

//offers
#[derive(
  Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
)]
pub enum OfferStatus {
  Open,
  Closed,
}

impl Default for OfferStatus {
  fn default() -> Self {
    OfferStatus::Open
  }
}

#[derive(
  Encode, Decode, Clone, Eq, PartialEq, RuntimeDebugNoBound, MaxEncodedLen, TypeInfo, Copy,
)]
pub enum OfferType {
  SellOrder,
  BuyOrder,
}
#[derive(CloneNoBound, Encode, Decode, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct OfferData<T: Config> {
  pub marketplace_id: [u8; 32],
  pub collection_id: T::CollectionId,
  pub item_id: T::ItemId,
  pub percentage: Permill,
  pub creator: T::AccountId,
  pub price: T::Balance,
  pub fee: T::Balance,
  pub status: OfferStatus,
  pub creation_date: u64,
  pub offer_type: OfferType,
  pub buyer: Option<(T::AccountId, [u8; 32])>,
}