Skip to content

Commit b608c21

Browse files
committed
feat: add solution for ex04
1 parent b9fbd32 commit b608c21

File tree

3 files changed

+17
-15
lines changed
  • exercises/ex04-marketplace
    • loosely-coupled-marketplace/src
    • marketplace-nfts/src
    • tightly-coupled-marketplace/src

3 files changed

+17
-15
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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ pub mod pallet {
2323
use frame_system::{ensure_signed, pallet_prelude::*};
2424

2525
#[pallet::config]
26-
pub trait Config: frame_system::Config + scale_info::TypeInfo {
27-
todo!("add a dependency on pallet_marketplace_nft on the previous line");
26+
pub trait Config:
27+
frame_system::Config + scale_info::TypeInfo + pallet_marketplace_nfts::Config
28+
{
2829
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
2930
type Currency: Currency<Self::AccountId>;
3031
}
@@ -75,7 +76,7 @@ pub mod pallet {
7576
let origin = ensure_signed(origin)?;
7677

7778
ensure!(amount > 0, Error::<T>::ZeroAmount);
78-
let owned = todo!("get the amount owned from the pallet_nft account storage");
79+
let owned = pallet_marketplace_nfts::Pallet::<T>::account(nft_id, origin.clone());
7980
ensure!(owned >= amount, Error::<T>::NotEnoughOwned);
8081

8182
NFTsForSale::<T>::insert(nft_id, origin.clone(), SaleData { price, amount });
@@ -95,7 +96,7 @@ pub mod pallet {
9596
let buyer = ensure_signed(origin)?;
9697

9798
let sale_data = NFTsForSale::<T>::get(nft_id, seller.clone());
98-
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());
99100

100101
ensure!(amount <= sale_data.amount, Error::<T>::NotEnoughInSale);
101102
ensure!(sale_data.amount <= owned, Error::<T>::NotEnoughOwned);
@@ -107,7 +108,12 @@ pub mod pallet {
107108

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

110-
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+
);
111117

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

0 commit comments

Comments
 (0)