Skip to content

Commit

Permalink
feat: add solution for ex04
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Aug 21, 2022
1 parent b9fbd32 commit b608c21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ pub mod pallet {
let origin = ensure_signed(origin)?;

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

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

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

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

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

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

if amount == sale_data.amount {
ResourcesForSale::<T>::remove(nft_id, seller.clone());
Expand Down
4 changes: 2 additions & 2 deletions exercises/ex04-marketplace/marketplace-nfts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ impl<T: Config> Pallet<T> {

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

fn transfer(nft_id: T::NFTId, from: T::AccountId, to: T::AccountId, amount: u128) -> u128 {
todo!("do the transfer")
Self::unchecked_transfer(nft_id, from, to, amount)
}
}
16 changes: 11 additions & 5 deletions exercises/ex04-marketplace/tightly-coupled-marketplace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ pub mod pallet {
use frame_system::{ensure_signed, pallet_prelude::*};

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

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

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

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

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

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

todo!("call the pallet_marketplace_nft transfer function");
pallet_marketplace_nfts::Pallet::<T>::unchecked_transfer(
nft_id,
seller.clone(),
buyer.clone(),
amount,
);

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

0 comments on commit b608c21

Please sign in to comment.