Skip to content

Commit a5f6ff9

Browse files
committed
feat: add solution for ex04
1 parent 0e1efca commit a5f6ff9

File tree

3 files changed

+22
-24
lines changed
  • exercises/ex04-marketplace
    • loosely-coupled-marketplace/src
    • marketplace-nfts/src
    • tightly-coupled-marketplace/src

3 files changed

+22
-24
lines changed

exercises/ex04-marketplace/loosely-coupled-marketplace/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ pub mod pallet {
8585
let origin = ensure_signed(origin)?;
8686

8787
ensure!(amount > 0, Error::<T>::ZeroAmount);
88-
let owned: u128 = todo!(
89-
"get the amount of this specific NFT owned by the seller, through the Resource type and its Sellable trait"
90-
);
88+
let owned = T::Resource::amount_owned(nft_id, origin.clone());
9189
ensure!(owned >= amount, Error::<T>::NotEnoughOwned);
9290

9391
ResourcesForSale::<T>::insert(nft_id, origin.clone(), SaleData { price, amount });
@@ -107,9 +105,7 @@ pub mod pallet {
107105
let buyer = ensure_signed(origin)?;
108106

109107
let sale_data = ResourcesForSale::<T>::get(nft_id, seller.clone());
110-
let owned = todo!(
111-
"get the amount of this specific NFT owned by the seller, through the Resource type and its Sellable trait"
112-
);
108+
let owned = T::Resource::amount_owned(nft_id, seller.clone());
113109

114110
ensure!(amount <= sale_data.amount, Error::<T>::NotEnoughInSale);
115111
ensure!(sale_data.amount <= owned, Error::<T>::NotEnoughOwned);
@@ -119,9 +115,9 @@ pub mod pallet {
119115
.checked_mul(&amount.checked_into().ok_or(Error::<T>::Overflow)?)
120116
.ok_or(Error::<T>::Overflow)?;
121117

122-
todo!("transfer the amount of currency owed from the buyer to the seller");
118+
T::Currency::transfer(&buyer, &seller, total_to_pay, KeepAlive)?;
123119

124-
todo!("transfer amount of nft_id from the seller to the buyer");
120+
T::Resource::transfer(nft_id, seller.clone(), buyer.clone(), amount);
125121

126122
if amount == sale_data.amount {
127123
ResourcesForSale::<T>::remove(nft_id, seller.clone());

exercises/ex04-marketplace/marketplace-nfts/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ impl<T: Config> Pallet<T> {
208208

209209
impl<T: Config> Sellable<T::AccountId, T::NFTId> for Pallet<T> {
210210
fn amount_owned(nft_id: T::NFTId, account: T::AccountId) -> u128 {
211-
todo!("return the amount of nft_id owned by account")
211+
Self::account(nft_id, account)
212212
}
213213

214214
fn transfer(nft_id: T::NFTId, from: T::AccountId, to: T::AccountId, amount: u128) -> u128 {
215-
todo!("do the transfer")
215+
Self::unchecked_transfer(nft_id, from, to, amount)
216216
}
217217
}

exercises/ex04-marketplace/tightly-coupled-marketplace/src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ use types::*;
1616
pub type BalanceOf<T> =
1717
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
1818

19-
// TODO: Once you added the dependecy to pallet_marketplace_nft use T::NFTId instead
20-
pub type NFTId = u32;
21-
2219
#[frame_support::pallet]
2320
pub mod pallet {
2421
use super::*;
2522
use frame_support::pallet_prelude::*;
2623
use frame_system::{ensure_signed, pallet_prelude::*};
2724

2825
#[pallet::config]
29-
// TODO: add a dependency on pallet_marketplace_nft on the previous line
30-
pub trait Config: frame_system::Config + scale_info::TypeInfo {
26+
pub trait Config:
27+
frame_system::Config + scale_info::TypeInfo + pallet_marketplace_nfts::Config
28+
{
3129
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
3230
type Currency: Currency<Self::AccountId>;
3331
}
@@ -40,9 +38,9 @@ pub mod pallet {
4038
#[pallet::generate_deposit(pub(super) fn deposit_event)]
4139
pub enum Event<T: Config> {
4240
/// NFT has been listed for sale (nft_id, seller, price, amount)
43-
ListedForSale(NFTId, T::AccountId, BalanceOf<T>, u128),
41+
ListedForSale(T::NFTId, T::AccountId, BalanceOf<T>, u128),
4442
// NFT has been sold (nft_id, seller, buyer, amount)
45-
Sold(NFTId, T::AccountId, T::AccountId, u128),
43+
Sold(T::NFTId, T::AccountId, T::AccountId, u128),
4644
}
4745

4846
#[pallet::error]
@@ -59,7 +57,7 @@ pub mod pallet {
5957
pub type NFTsForSale<T: Config> = StorageDoubleMap<
6058
_,
6159
Blake2_128Concat,
62-
NFTId,
60+
T::NFTId,
6361
Blake2_128Concat,
6462
T::AccountId,
6563
SaleData<T>,
@@ -71,15 +69,14 @@ pub mod pallet {
7169
#[pallet::weight(0)]
7270
pub fn set_sale(
7371
origin: OriginFor<T>,
74-
nft_id: NFTId,
72+
nft_id: T::NFTId,
7573
price: BalanceOf<T>,
7674
amount: u128,
7775
) -> DispatchResult {
7876
let origin = ensure_signed(origin)?;
7977

8078
ensure!(amount > 0, Error::<T>::ZeroAmount);
81-
// TODO: get the amount owned from the pallet_nft account storage, instead of 0
82-
let owned = 0;
79+
let owned = pallet_marketplace_nfts::Pallet::<T>::account(nft_id, origin.clone());
8380
ensure!(owned >= amount, Error::<T>::NotEnoughOwned);
8481

8582
NFTsForSale::<T>::insert(nft_id, origin.clone(), SaleData { price, amount });
@@ -92,14 +89,14 @@ pub mod pallet {
9289
#[pallet::weight(0)]
9390
pub fn buy(
9491
origin: OriginFor<T>,
95-
nft_id: NFTId,
92+
nft_id: T::NFTId,
9693
seller: T::AccountId,
9794
amount: u128,
9895
) -> DispatchResult {
9996
let buyer = ensure_signed(origin)?;
10097

10198
let sale_data = NFTsForSale::<T>::get(nft_id, seller.clone());
102-
let owned = todo!("get the amount owned from the pallet_nft account storage");
99+
let owned = pallet_marketplace_nfts::Pallet::<T>::account(nft_id, seller.clone());
103100

104101
ensure!(amount <= sale_data.amount, Error::<T>::NotEnoughInSale);
105102
ensure!(sale_data.amount <= owned, Error::<T>::NotEnoughOwned);
@@ -111,7 +108,12 @@ pub mod pallet {
111108

112109
<T as pallet::Config>::Currency::transfer(&buyer, &seller, total_to_pay, KeepAlive)?;
113110

114-
todo!("call the pallet_marketplace_nft transfer function");
111+
pallet_marketplace_nfts::Pallet::<T>::unchecked_transfer(
112+
nft_id,
113+
seller.clone(),
114+
buyer.clone(),
115+
amount,
116+
);
115117

116118
if amount == sale_data.amount {
117119
NFTsForSale::<T>::remove(nft_id, seller.clone());

0 commit comments

Comments
 (0)