Skip to content

Commit b9fbd32

Browse files
committed
feat: add solution for ex03
1 parent 08aec00 commit b9fbd32

File tree

1 file changed

+83
-0
lines changed
  • exercises/ex03-nft/nft/src

1 file changed

+83
-0
lines changed

exercises/ex03-nft/nft/src/lib.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,58 @@ pub mod pallet {
9494
metadata: BoundedVec<u8, T::MaxLength>,
9595
supply: u128,
9696
) -> DispatchResult {
97+
let origin = ensure_signed(origin)?;
98+
99+
ensure!(supply > 0, Error::<T>::NoSupply);
100+
101+
let id = Self::nonce();
102+
let details = UniqueAssetDetails::new(origin.clone(), metadata, supply);
103+
UniqueAsset::<T>::insert(id, details);
104+
Account::<T>::insert(id, origin.clone(), supply);
105+
Nonce::<T>::set(id.saturating_add(1));
106+
107+
Self::deposit_event(Event::<T>::Created {
108+
creator: origin,
109+
asset_id: id,
110+
});
111+
97112
Ok(())
98113
}
99114

100115
#[pallet::weight(0)]
101116
pub fn burn(origin: OriginFor<T>, asset_id: UniqueAssetId, amount: u128) -> DispatchResult {
117+
let origin = ensure_signed(origin)?;
118+
119+
ensure!(
120+
Self::unique_asset(asset_id).is_some(),
121+
Error::<T>::UnknownAssetId
122+
);
123+
Self::ensure_own_some(asset_id, origin.clone())?;
124+
125+
let mut total_supply = 0;
126+
127+
UniqueAsset::<T>::try_mutate(asset_id, |maybe_details| -> DispatchResult {
128+
let details = maybe_details.as_mut().ok_or(Error::<T>::UnknownAssetId)?;
129+
130+
let mut burned_amount = 0;
131+
Account::<T>::mutate(asset_id, origin.clone(), |balance| {
132+
let old_balance = *balance;
133+
*balance = balance.saturating_sub(amount);
134+
burned_amount = old_balance - *balance;
135+
});
136+
137+
details.supply -= burned_amount;
138+
total_supply = details.supply;
139+
140+
Ok(())
141+
})?;
142+
143+
Self::deposit_event(Event::<T>::Burned {
144+
asset_id,
145+
owner: origin,
146+
total_supply,
147+
});
148+
102149
Ok(())
103150
}
104151

@@ -109,7 +156,43 @@ pub mod pallet {
109156
amount: u128,
110157
to: T::AccountId,
111158
) -> DispatchResult {
159+
let origin = ensure_signed(origin)?;
160+
161+
ensure!(
162+
Self::unique_asset(asset_id).is_some(),
163+
Error::<T>::UnknownAssetId
164+
);
165+
Self::ensure_own_some(asset_id, origin.clone())?;
166+
167+
let mut transfered_amount = 0;
168+
Account::<T>::mutate(asset_id, origin.clone(), |balance| {
169+
let old_balance = *balance;
170+
*balance = balance.saturating_sub(amount);
171+
transfered_amount = old_balance - *balance;
172+
});
173+
174+
Account::<T>::mutate(asset_id, to.clone(), |balance| {
175+
*balance = balance.saturating_add(transfered_amount);
176+
});
177+
178+
Self::deposit_event(Event::<T>::Transferred {
179+
asset_id,
180+
from: origin,
181+
to,
182+
amount: transfered_amount,
183+
});
184+
112185
Ok(())
113186
}
114187
}
115188
}
189+
190+
impl<T: Config> Pallet<T> {
191+
fn ensure_own_some(asset_id: UniqueAssetId, account: T::AccountId) -> Result<(), Error<T>> {
192+
let owned = Self::account(asset_id, account);
193+
194+
ensure!(owned > 0, Error::<T>::NotOwned);
195+
196+
Ok(())
197+
}
198+
}

0 commit comments

Comments
 (0)