Skip to content

Commit 08aec00

Browse files
committed
feat: add solution to ex01
1 parent b56755d commit 08aec00

File tree

1 file changed

+61
-1
lines changed
  • exercises/ex01-fungible-token/assets/src

1 file changed

+61
-1
lines changed

exercises/ex01-fungible-token/assets/src/lib.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,15 @@ pub mod pallet {
145145

146146
// TODO:
147147
// - Create a new AssetMetadata instance based on the call arguments.
148+
let metadata = AssetMetadata::new(name.clone(), symbol.clone());
148149
// - Insert this metadata in the Metadata storage, under the asset_id key.
150+
Metadata::<T>::insert(asset_id, metadata);
149151
// - Deposit a `MetadataSet` event.
152+
Self::deposit_event(Event::<T>::MetadataSet {
153+
asset_id,
154+
name,
155+
symbol,
156+
});
150157

151158
Ok(())
152159
}
@@ -160,15 +167,19 @@ pub mod pallet {
160167
) -> DispatchResult {
161168
// TODO:
162169
// - Ensure the extrinsic origin is a signed transaction.
170+
let origin = ensure_signed(origin)?;
163171
// - Ensure the caller is the asset owner.
172+
Self::ensure_is_owner(asset_id, origin)?;
164173

165174
let mut minted_amount = 0;
175+
let mut total_supply = 0;
166176

167177
Asset::<T>::try_mutate(asset_id, |maybe_details| -> DispatchResult {
168178
let details = maybe_details.as_mut().ok_or(Error::<T>::UnknownAssetId)?;
169179

170180
let old_supply = details.supply;
171181
details.supply = details.supply.saturating_add(amount);
182+
total_supply = details.supply;
172183
minted_amount = details.supply - old_supply;
173184

174185
Ok(())
@@ -179,6 +190,11 @@ pub mod pallet {
179190
});
180191

181192
// TODO: Deposit a `Minted` event.
193+
Self::deposit_event(Event::<T>::Minted {
194+
asset_id,
195+
owner: to,
196+
total_supply,
197+
});
182198

183199
Ok(())
184200
}
@@ -187,9 +203,34 @@ pub mod pallet {
187203
pub fn burn(origin: OriginFor<T>, asset_id: AssetId, amount: u128) -> DispatchResult {
188204
// TODO:
189205
// - Ensure the extrinsic origin is a signed transaction.
206+
let origin = ensure_signed(origin)?;
190207
// - Mutate the total supply.
191-
// - Mutate the account balance.
208+
let mut total_supply = 0;
209+
210+
Asset::<T>::try_mutate(asset_id, |maybe_details| -> DispatchResult {
211+
let details = maybe_details.as_mut().ok_or(Error::<T>::UnknownAssetId)?;
212+
213+
let mut burned_amount = 0;
214+
215+
// - Mutate the account balance.
216+
Account::<T>::mutate(asset_id, origin.clone(), |balance| {
217+
let old_balance = *balance;
218+
*balance = balance.saturating_sub(amount);
219+
burned_amount = old_balance - *balance;
220+
});
221+
222+
details.supply -= burned_amount;
223+
total_supply = details.supply;
224+
225+
Ok(())
226+
})?;
227+
192228
// - Emit a `Burned` event.
229+
Self::deposit_event(Event::<T>::Burned {
230+
asset_id,
231+
owner: origin,
232+
total_supply,
233+
});
193234

194235
Ok(())
195236
}
@@ -203,8 +244,27 @@ pub mod pallet {
203244
) -> DispatchResult {
204245
// TODO:
205246
// - Ensure the extrinsic origin is a signed transaction.
247+
let origin = ensure_signed(origin)?;
248+
ensure!(Self::asset(asset_id).is_some(), Error::<T>::UnknownAssetId);
206249
// - Mutate both account balances.
250+
let mut transfered_amount = 0;
251+
252+
Account::<T>::mutate(asset_id, origin.clone(), |balance| {
253+
let old_balance = *balance;
254+
*balance = balance.saturating_sub(amount);
255+
transfered_amount = old_balance - *balance;
256+
});
257+
258+
Account::<T>::mutate(asset_id, to.clone(), |balance| {
259+
*balance = balance.saturating_add(transfered_amount);
260+
});
207261
// - Emit a `Transferred` event.
262+
Self::deposit_event(Event::<T>::Transferred {
263+
asset_id,
264+
from: origin,
265+
to,
266+
amount,
267+
});
208268

209269
Ok(())
210270
}

0 commit comments

Comments
 (0)