Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

俊佳-第三次作业 #26

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update kitties.rs
  • Loading branch information
bitboy666 authored Oct 3, 2019
commit c6b908b56615ab6869b6fe1d76b3c111c4499441
23 changes: 22 additions & 1 deletion projects/lesson-5/runtime/src/kitties.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use support::{decl_module, decl_storage, ensure, StorageValue, StorageMap, dispatch::Result, Parameter};
use support::{decl_module, decl_storage, ensure, StorageValue, StorageMap, dispatch::Result, Parameter,traits::Currency};
use sr_primitives::traits::{SimpleArithmetic, Bounded, Member};
use codec::{Encode, Decode};
use runtime_io::blake2_128;
Expand All @@ -7,6 +7,7 @@ use rstd::result;

pub trait Trait: system::Trait {
type KittyIndex: Parameter + Member + SimpleArithmetic + Bounded + Default + Copy;
type Currency: Currency<Self::AccountId>;
}

#[derive(Encode, Decode)]
Expand All @@ -27,6 +28,8 @@ decl_storage! {
pub KittiesCount get(kitties_count): T::KittyIndex;

pub OwnedKitties get(owned_kitties): map (T::AccountId, Option<T::KittyIndex>) => Option<KittyLinkedItem<T>>;

pub KittyPrices get(kitty_price): map T::KittyIndex => Option<u64>;
}
}

Expand Down Expand Up @@ -55,6 +58,23 @@ decl_module! {
// 作业:实现 transfer(origin, to: T::AccountId, kitty_id: T::KittyIndex)
// 使用 ensure! 来保证只有主人才有权限调用 transfer
// 使用 OwnedKitties::append 和 OwnedKitties::remove 来修改小猫的主人
///Transfer a kitty to other owner
pub fn transfer(origin, to: T::AccountId, kitty_id: T::KittyIndex) {
let sender = ensure_signed(origin)?;

ensure!(<OwnedKitties<T>>::exists(&(sender.clone(), Some(kitty_id))), "Only kitty can be transfered by owner");

<OwnedKitties<T>>::remove(&sender, kitty_id);
<OwnedKitties<T>>::append(&to, kitty_id);
}
//
pub fn sell(origin, kitty_id: T::KittyIndex, price: u64) {
let sender = ensure_signed(origin)?;

ensure!(<OwnedKitties<T>>::exists(&(sender.clone(), Some(kitty_id))), "Only kitty can be set price by owner");

<KittyPrices<T>>::insert(kitty_id, price);
}
}
}

Expand Down Expand Up @@ -142,6 +162,7 @@ impl<T: Trait> Module<T> {

fn insert_owned_kitty(owner: &T::AccountId, kitty_id: T::KittyIndex) {
// 作业:调用 OwnedKitties::append 完成实现
<OwnedKitties<T>>::append(owner, kitty_id);
}

fn insert_kitty(owner: &T::AccountId, kitty_id: T::KittyIndex, kitty: Kitty) {
Expand Down