diff --git a/src/1/README.md b/src/1/README.md index b62a7388..0aa0c410 100644 --- a/src/1/README.md +++ b/src/1/README.md @@ -17,6 +17,7 @@
+
@@ -35,6 +36,14 @@
+
+ +```rust +{{#include ./source/src/impls.rs}} +``` + +
+
```rust diff --git a/src/1/source/changes.diff b/src/1/source/changes.diff index 2fec7958..b8aa363a 100644 --- a/src/1/source/changes.diff +++ b/src/1/source/changes.diff @@ -4069,14 +4069,31 @@ index 0000000..c342153 +# Format comments +comment_width = 100 +wrap_comments = true +diff --git a/src/impls.rs b/src/impls.rs +new file mode 100644 +index 0000000..d49f793 +--- /dev/null ++++ b/src/impls.rs +@@ -0,0 +1,9 @@ ++use super::*; ++use frame_support::pallet_prelude::*; ++ ++impl Pallet { ++ pub fn mint(owner: T::AccountId) -> DispatchResult { ++ Self::deposit_event(Event::::Created { owner }); ++ Ok(()) ++ } ++} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 -index 0000000..8b9507e +index 0000000..f846f0b --- /dev/null +++ b/src/lib.rs -@@ -0,0 +1,43 @@ +@@ -0,0 +1,38 @@ +#![cfg_attr(not(feature = "std"), no_std)] + ++mod impls; ++ +pub use pallet::*; + +#[frame_support::pallet(dev_mode)] @@ -4110,11 +4127,4 @@ index 0000000..8b9507e + Ok(()) + } + } -+ -+ impl Pallet { -+ fn mint(owner: T::AccountId) -> DispatchResult { -+ Self::deposit_event(Event::::Created { owner }); -+ Ok(()) -+ } -+ } +} diff --git a/src/1/source/src/impls.rs b/src/1/source/src/impls.rs new file mode 100644 index 00000000..d49f793b --- /dev/null +++ b/src/1/source/src/impls.rs @@ -0,0 +1,9 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/1/source/src/lib.rs b/src/1/source/src/lib.rs index 8b9507e4..f846f0bb 100644 --- a/src/1/source/src/lib.rs +++ b/src/1/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; #[frame_support::pallet(dev_mode)] @@ -33,11 +35,4 @@ pub mod pallet { Ok(()) } } - - impl Pallet { - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/10/README.md b/src/10/README.md index 061af0d2..b692d103 100644 --- a/src/10/README.md +++ b/src/10/README.md @@ -17,12 +17,12 @@
- +
-
+
```rust -{{#include ./template/src/lib.rs}} +{{#include ./template/src/impls.rs}} ```
@@ -34,12 +34,12 @@
- +
-
+
```rust -{{#include ./solution/src/lib.rs}} +{{#include ./solution/src/impls.rs}} ```
diff --git a/src/10/solution/solution.diff b/src/10/solution/solution.diff index 029788b9..63742707 100644 --- a/src/10/solution/solution.diff +++ b/src/10/solution/solution.diff @@ -1,20 +1,20 @@ -diff --git a/src/lib.rs b/src/lib.rs -index 356e547..59a65fe 100644 ---- a/src/lib.rs -+++ b/src/lib.rs -@@ -49,12 +49,9 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { -- /* TODO: -- - `get` the current count of kitties. -- - `unwrap_or` set the count to `0`. -- - increment the count by one. -- - `set` the new count of kitties. -- */ -+ let current_count: u64 = CountForKitties::::get().unwrap_or(0); -+ let new_count = current_count + 1; -+ CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } +diff --git a/src/impls.rs b/src/impls.rs +index 67fd2a0..ef53a56 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,12 +4,9 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { +- /* TODO: +- - `get` the current count of kitties. +- - `unwrap_or` set the count to `0`. +- - increment the count by one. +- - `set` the new count of kitties. +- */ ++ let current_count: u64 = CountForKitties::::get().unwrap_or(0); ++ let new_count = current_count + 1; ++ CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } diff --git a/src/10/solution/src/impls.rs b/src/10/solution/src/impls.rs new file mode 100644 index 00000000..ef53a56f --- /dev/null +++ b/src/10/solution/src/impls.rs @@ -0,0 +1,13 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get().unwrap_or(0); + let new_count = current_count + 1; + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/10/solution/src/lib.rs b/src/10/solution/src/lib.rs index 59a65fea..acea2e23 100644 --- a/src/10/solution/src/lib.rs +++ b/src/10/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -44,16 +46,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get().unwrap_or(0); - let new_count = current_count + 1; - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/10/template/src/impls.rs b/src/10/template/src/impls.rs new file mode 100644 index 00000000..67fd2a06 --- /dev/null +++ b/src/10/template/src/impls.rs @@ -0,0 +1,16 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + /* TODO: + - `get` the current count of kitties. + - `unwrap_or` set the count to `0`. + - increment the count by one. + - `set` the new count of kitties. + */ + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/10/template/src/lib.rs b/src/10/template/src/lib.rs index 356e5476..acea2e23 100644 --- a/src/10/template/src/lib.rs +++ b/src/10/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -44,19 +46,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - /* TODO: - - `get` the current count of kitties. - - `unwrap_or` set the count to `0`. - - increment the count by one. - - `set` the new count of kitties. - */ - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/10/template/template.diff b/src/10/template/template.diff index 1c6418a2..70be1f0b 100644 --- a/src/10/template/template.diff +++ b/src/10/template/template.diff @@ -1,17 +1,17 @@ -diff --git a/src/lib.rs b/src/lib.rs -index 67dd167..356e547 100644 ---- a/src/lib.rs -+++ b/src/lib.rs -@@ -49,6 +49,12 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { -+ /* TODO: -+ - `get` the current count of kitties. -+ - `unwrap_or` set the count to `0`. -+ - increment the count by one. -+ - `set` the new count of kitties. -+ */ - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } +diff --git a/src/impls.rs b/src/impls.rs +index 869c781..67fd2a0 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,6 +4,12 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { ++ /* TODO: ++ - `get` the current count of kitties. ++ - `unwrap_or` set the count to `0`. ++ - increment the count by one. ++ - `set` the new count of kitties. ++ */ + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } diff --git a/src/11/README.md b/src/11/README.md index 061af0d2..cead1dba 100644 --- a/src/11/README.md +++ b/src/11/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/11/solution/solution.diff b/src/11/solution/solution.diff index a60cd898..c4b93397 100644 --- a/src/11/solution/solution.diff +++ b/src/11/solution/solution.diff @@ -1,8 +1,22 @@ +diff --git a/src/impls.rs b/src/impls.rs +index ba4070f..9481d05 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -5,8 +5,7 @@ impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get().unwrap_or(0); +- /* TODO: Update this logic to use safe math. */ +- let new_count = current_count + 1; ++ let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index 924f26f..2ed0e74 100644 +index 110d15d..9ff62ff 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -33,9 +33,7 @@ pub mod pallet { +@@ -35,9 +35,7 @@ pub mod pallet { #[pallet::error] pub enum Error { @@ -13,13 +27,3 @@ index 924f26f..2ed0e74 100644 } // Learn about callable functions and dispatch. -@@ -54,8 +52,7 @@ pub mod pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get().unwrap_or(0); -- /* TODO: Update this logic to use safe math. */ -- let new_count = current_count + 1; -+ let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/11/solution/src/impls.rs b/src/11/solution/src/impls.rs new file mode 100644 index 00000000..9481d053 --- /dev/null +++ b/src/11/solution/src/impls.rs @@ -0,0 +1,13 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get().unwrap_or(0); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/11/solution/src/lib.rs b/src/11/solution/src/lib.rs index 2ed0e748..9ff62ffe 100644 --- a/src/11/solution/src/lib.rs +++ b/src/11/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -46,16 +48,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get().unwrap_or(0); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/11/template/src/impls.rs b/src/11/template/src/impls.rs new file mode 100644 index 00000000..ba4070f5 --- /dev/null +++ b/src/11/template/src/impls.rs @@ -0,0 +1,14 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get().unwrap_or(0); + /* TODO: Update this logic to use safe math. */ + let new_count = current_count + 1; + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/11/template/src/lib.rs b/src/11/template/src/lib.rs index 924f26fe..110d15d2 100644 --- a/src/11/template/src/lib.rs +++ b/src/11/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -48,17 +50,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get().unwrap_or(0); - /* TODO: Update this logic to use safe math. */ - let new_count = current_count + 1; - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/11/template/template.diff b/src/11/template/template.diff index 065a5e1a..0dc78199 100644 --- a/src/11/template/template.diff +++ b/src/11/template/template.diff @@ -1,8 +1,20 @@ +diff --git a/src/impls.rs b/src/impls.rs +index ef53a56..ba4070f 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -5,6 +5,7 @@ impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get().unwrap_or(0); ++ /* TODO: Update this logic to use safe math. */ + let new_count = current_count + 1; + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); diff --git a/src/lib.rs b/src/lib.rs -index 59a65fe..924f26f 100644 +index acea2e2..110d15d 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -32,7 +32,11 @@ pub mod pallet { +@@ -34,7 +34,11 @@ pub mod pallet { } #[pallet::error] @@ -15,11 +27,3 @@ index 59a65fe..924f26f 100644 // Learn about callable functions and dispatch. #[pallet::call] -@@ -50,6 +54,7 @@ pub mod pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get().unwrap_or(0); -+ /* TODO: Update this logic to use safe math. */ - let new_count = current_count + 1; - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); diff --git a/src/12/README.md b/src/12/README.md index 061af0d2..cead1dba 100644 --- a/src/12/README.md +++ b/src/12/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/12/solution/solution.diff b/src/12/solution/solution.diff index 026c2ac7..caa81773 100644 --- a/src/12/solution/solution.diff +++ b/src/12/solution/solution.diff @@ -1,8 +1,26 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 99141c0..1af97cb 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,11 +4,9 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { +- /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ +- let current_count: u64 = CountForKitties::::get().unwrap_or(0); ++ let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; +- /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ +- CountForKitties::::set(Some(new_count)); ++ CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } diff --git a/src/lib.rs b/src/lib.rs -index 3b445c2..446ce3c 100644 +index 5ef793a..824c5ac 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -22,8 +22,7 @@ pub mod pallet { +@@ -24,8 +24,7 @@ pub mod pallet { /// Learn about storage value. #[pallet::storage] @@ -12,17 +30,3 @@ index 3b445c2..446ce3c 100644 // Learn about events. #[pallet::event] -@@ -52,11 +51,9 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { -- /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ -- let current_count: u64 = CountForKitties::::get().unwrap_or(0); -+ let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -- /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ -- CountForKitties::::set(Some(new_count)); -+ CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } diff --git a/src/12/solution/src/impls.rs b/src/12/solution/src/impls.rs new file mode 100644 index 00000000..1af97cbb --- /dev/null +++ b/src/12/solution/src/impls.rs @@ -0,0 +1,13 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/12/solution/src/lib.rs b/src/12/solution/src/lib.rs index 446ce3ca..824c5aca 100644 --- a/src/12/solution/src/lib.rs +++ b/src/12/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -46,16 +48,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/12/template/src/impls.rs b/src/12/template/src/impls.rs new file mode 100644 index 00000000..99141c05 --- /dev/null +++ b/src/12/template/src/impls.rs @@ -0,0 +1,15 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ + let current_count: u64 = CountForKitties::::get().unwrap_or(0); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/12/template/src/lib.rs b/src/12/template/src/lib.rs index 3b445c24..5ef793a3 100644 --- a/src/12/template/src/lib.rs +++ b/src/12/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -47,18 +49,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ - let current_count: u64 = CountForKitties::::get().unwrap_or(0); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/12/template/template.diff b/src/12/template/template.diff index eff48282..a39feda3 100644 --- a/src/12/template/template.diff +++ b/src/12/template/template.diff @@ -1,8 +1,23 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 9481d05..99141c0 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,8 +4,10 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { ++ /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ + let current_count: u64 = CountForKitties::::get().unwrap_or(0); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; ++ /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ + CountForKitties::::set(Some(new_count)); + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index 2ed0e74..3b445c2 100644 +index 9ff62ff..5ef793a 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -22,6 +22,7 @@ pub mod pallet { +@@ -24,6 +24,7 @@ pub mod pallet { /// Learn about storage value. #[pallet::storage] @@ -10,14 +25,3 @@ index 2ed0e74..3b445c2 100644 pub(super) type CountForKitties = StorageValue; // Learn about events. -@@ -51,8 +52,10 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { -+ /* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */ - let current_count: u64 = CountForKitties::::get().unwrap_or(0); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -+ /* TODO: Remove the `Option` wrapper when setting the `new_count`. */ - CountForKitties::::set(Some(new_count)); - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/13/source/changes.diff b/src/13/source/changes.diff index 809a16c3..7b2265c8 100644 --- a/src/13/source/changes.diff +++ b/src/13/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 446ce3c..c320583 100644 +index 824c5ac..38fafb0 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -24,6 +24,10 @@ pub mod pallet { +@@ -26,6 +26,10 @@ pub mod pallet { #[pallet::storage] pub(super) type CountForKitties = StorageValue; diff --git a/src/13/source/src/impls.rs b/src/13/source/src/impls.rs new file mode 100644 index 00000000..1af97cbb --- /dev/null +++ b/src/13/source/src/impls.rs @@ -0,0 +1,13 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/13/source/src/lib.rs b/src/13/source/src/lib.rs index c3205834..38fafb0c 100644 --- a/src/13/source/src/lib.rs +++ b/src/13/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -50,16 +52,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/14/README.md b/src/14/README.md index 061af0d2..cead1dba 100644 --- a/src/14/README.md +++ b/src/14/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/14/solution/solution.diff b/src/14/solution/solution.diff index d3d5ae5f..891afde3 100644 --- a/src/14/solution/solution.diff +++ b/src/14/solution/solution.diff @@ -1,8 +1,26 @@ +diff --git a/src/impls.rs b/src/impls.rs +index e4449c9..c8320f8 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -3,11 +3,10 @@ use frame_support::pallet_prelude::*; + + impl Pallet { + // Learn about `AccountId`. +- /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ +- pub fn mint(owner: T::AccountId) -> DispatchResult { ++ pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; +- /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ ++ Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index e1d2dd7..6a46bd4 100644 +index 978c774..331ff28 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -46,9 +46,8 @@ pub mod pallet { +@@ -48,9 +48,8 @@ pub mod pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { // Learn about `origin`. let who = ensure_signed(origin)?; @@ -14,17 +32,3 @@ index e1d2dd7..6a46bd4 100644 Ok(()) } } -@@ -56,11 +55,10 @@ pub mod pallet { - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. -- /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ -- fn mint(owner: T::AccountId) -> DispatchResult { -+ fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -- /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ -+ Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/14/solution/src/impls.rs b/src/14/solution/src/impls.rs new file mode 100644 index 00000000..c8320f87 --- /dev/null +++ b/src/14/solution/src/impls.rs @@ -0,0 +1,14 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/14/solution/src/lib.rs b/src/14/solution/src/lib.rs index 6a46bd42..331ff28f 100644 --- a/src/14/solution/src/lib.rs +++ b/src/14/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -51,17 +53,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/14/template/src/impls.rs b/src/14/template/src/impls.rs new file mode 100644 index 00000000..e4449c96 --- /dev/null +++ b/src/14/template/src/impls.rs @@ -0,0 +1,15 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/14/template/src/lib.rs b/src/14/template/src/lib.rs index e1d2dd7f..978c774f 100644 --- a/src/14/template/src/lib.rs +++ b/src/14/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -52,18 +54,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/14/template/template.diff b/src/14/template/template.diff index 3d6247f4..f56e6a5e 100644 --- a/src/14/template/template.diff +++ b/src/14/template/template.diff @@ -1,8 +1,24 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 1af97cb..e4449c9 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -3,9 +3,11 @@ use frame_support::pallet_prelude::*; + + impl Pallet { + // Learn about `AccountId`. ++ /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ + pub fn mint(owner: T::AccountId) -> DispatchResult { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; ++ /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index c320583..e1d2dd7 100644 +index 38fafb0..978c774 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -46,6 +46,8 @@ pub mod pallet { +@@ -48,6 +48,8 @@ pub mod pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { // Learn about `origin`. let who = ensure_signed(origin)?; @@ -11,15 +27,3 @@ index c320583..e1d2dd7 100644 Self::mint(who)?; Ok(()) } -@@ -54,9 +56,11 @@ pub mod pallet { - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. -+ /* TODO: Update this function signature to include `id` which is type `[u8; 16]`. */ - fn mint(owner: T::AccountId) -> DispatchResult { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -+ /* TODO: In the `Kitties` map, under the key `id`, insert `()`. */ - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/15/README.md b/src/15/README.md index 061af0d2..cead1dba 100644 --- a/src/15/README.md +++ b/src/15/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/15/solution/solution.diff b/src/15/solution/solution.diff index af0cde73..ceed39b4 100644 --- a/src/15/solution/solution.diff +++ b/src/15/solution/solution.diff @@ -1,8 +1,26 @@ +diff --git a/src/impls.rs b/src/impls.rs +index c984894..650fdfb 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,10 +4,9 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { +- /* TODO: +- - `ensure!` that `Kitties` map does not `contains_key` for `dna`. +- - If it does, return `Error::::DuplicateKitty`. +- */ ++ // Check if the kitty does not already exist in our storage map ++ ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); ++ + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); diff --git a/src/lib.rs b/src/lib.rs -index 957c715..2b62b30 100644 +index f2120f9..a5bb629 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -38,7 +38,7 @@ pub mod pallet { +@@ -40,7 +40,7 @@ pub mod pallet { #[pallet::error] pub enum Error { TooManyKitties, @@ -11,17 +29,3 @@ index 957c715..2b62b30 100644 } // Learn about callable functions and dispatch. -@@ -57,10 +57,9 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { -- /* TODO: -- - `ensure!` that `Kitties` map does not `contains_key` for `dna`. -- - If it does, return `Error::::DuplicateKitty`. -- */ -+ // Check if the kitty does not already exist in our storage map -+ ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); -+ - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); diff --git a/src/15/solution/src/impls.rs b/src/15/solution/src/impls.rs new file mode 100644 index 00000000..650fdfbe --- /dev/null +++ b/src/15/solution/src/impls.rs @@ -0,0 +1,17 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/15/solution/src/lib.rs b/src/15/solution/src/lib.rs index 2b62b30d..a5bb629c 100644 --- a/src/15/solution/src/lib.rs +++ b/src/15/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -52,20 +54,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/15/template/src/impls.rs b/src/15/template/src/impls.rs new file mode 100644 index 00000000..c9848947 --- /dev/null +++ b/src/15/template/src/impls.rs @@ -0,0 +1,18 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + /* TODO: + - `ensure!` that `Kitties` map does not `contains_key` for `dna`. + - If it does, return `Error::::DuplicateKitty`. + */ + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/15/template/src/lib.rs b/src/15/template/src/lib.rs index 957c7152..f2120f95 100644 --- a/src/15/template/src/lib.rs +++ b/src/15/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -52,21 +54,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - /* TODO: - - `ensure!` that `Kitties` map does not `contains_key` for `dna`. - - If it does, return `Error::::DuplicateKitty`. - */ - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/15/template/template.diff b/src/15/template/template.diff index 014fe750..d8331c11 100644 --- a/src/15/template/template.diff +++ b/src/15/template/template.diff @@ -1,8 +1,23 @@ +diff --git a/src/impls.rs b/src/impls.rs +index c8320f8..c984894 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,6 +4,10 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { ++ /* TODO: ++ - `ensure!` that `Kitties` map does not `contains_key` for `dna`. ++ - If it does, return `Error::::DuplicateKitty`. ++ */ + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); diff --git a/src/lib.rs b/src/lib.rs -index 6a46bd4..957c715 100644 +index 331ff28..f2120f9 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -38,6 +38,7 @@ pub mod pallet { +@@ -40,6 +40,7 @@ pub mod pallet { #[pallet::error] pub enum Error { TooManyKitties, @@ -10,14 +25,3 @@ index 6a46bd4..957c715 100644 } // Learn about callable functions and dispatch. -@@ -56,6 +57,10 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { -+ /* TODO: -+ - `ensure!` that `Kitties` map does not `contains_key` for `dna`. -+ - If it does, return `Error::::DuplicateKitty`. -+ */ - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); diff --git a/src/16/source/src/impls.rs b/src/16/source/src/impls.rs new file mode 100644 index 00000000..650fdfbe --- /dev/null +++ b/src/16/source/src/impls.rs @@ -0,0 +1,17 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/16/source/src/lib.rs b/src/16/source/src/lib.rs index 2b62b30d..a5bb629c 100644 --- a/src/16/source/src/lib.rs +++ b/src/16/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -52,20 +54,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/17/solution/solution.diff b/src/17/solution/solution.diff index d105d90c..c12c6961 100644 --- a/src/17/solution/solution.diff +++ b/src/17/solution/solution.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 5a3aec3..5ffb395 100644 +index a142105..5f5f631 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,13 +20,11 @@ pub mod pallet { +@@ -22,13 +22,11 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } diff --git a/src/17/solution/src/impls.rs b/src/17/solution/src/impls.rs new file mode 100644 index 00000000..650fdfbe --- /dev/null +++ b/src/17/solution/src/impls.rs @@ -0,0 +1,17 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/17/solution/src/lib.rs b/src/17/solution/src/lib.rs index 5ffb3957..5f5f631d 100644 --- a/src/17/solution/src/lib.rs +++ b/src/17/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -58,20 +60,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/17/template/src/impls.rs b/src/17/template/src/impls.rs new file mode 100644 index 00000000..650fdfbe --- /dev/null +++ b/src/17/template/src/impls.rs @@ -0,0 +1,17 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/17/template/src/lib.rs b/src/17/template/src/lib.rs index 5a3aec33..a142105d 100644 --- a/src/17/template/src/lib.rs +++ b/src/17/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -60,20 +62,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/17/template/template.diff b/src/17/template/template.diff index c96e8796..56301566 100644 --- a/src/17/template/template.diff +++ b/src/17/template/template.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 2b62b30..5a3aec3 100644 +index a5bb629..a142105 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,6 +20,14 @@ pub mod pallet { +@@ -22,6 +22,14 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } diff --git a/src/18/README.md b/src/18/README.md index 061af0d2..cead1dba 100644 --- a/src/18/README.md +++ b/src/18/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/18/solution/solution.diff b/src/18/solution/solution.diff index 6b628774..0d6a4d52 100644 --- a/src/18/solution/solution.diff +++ b/src/18/solution/solution.diff @@ -1,8 +1,30 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 4b507f2..5ec6eb6 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,15 +4,13 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { +- /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ +- ++ let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; +- /* TODO: Insert `kitty`into the map instead of `()`. */ +- Kitties::::insert(dna, ()); ++ Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index b7a5372..62bd20e 100644 +index 5e40b25..7fcaaad 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,10 +20,8 @@ pub mod pallet { +@@ -22,10 +22,8 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } @@ -15,7 +37,7 @@ index b7a5372..62bd20e 100644 pub struct Kitty { // Using 16 bytes to represent a kitty DNA pub dna: [u8; 16], -@@ -36,8 +34,7 @@ pub mod pallet { +@@ -38,8 +36,7 @@ pub mod pallet { /// Learn about storage maps. #[pallet::storage] @@ -25,21 +47,3 @@ index b7a5372..62bd20e 100644 // Learn about events. #[pallet::event] -@@ -68,15 +65,13 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { -- /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ -- -+ let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -- /* TODO: Insert `kitty`into the map instead of `()`. */ -- Kitties::::insert(dna, ()); -+ Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/18/solution/src/impls.rs b/src/18/solution/src/impls.rs new file mode 100644 index 00000000..5ec6eb65 --- /dev/null +++ b/src/18/solution/src/impls.rs @@ -0,0 +1,18 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/18/solution/src/lib.rs b/src/18/solution/src/lib.rs index 62bd20ec..7fcaaad6 100644 --- a/src/18/solution/src/lib.rs +++ b/src/18/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -60,21 +62,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/18/template/src/impls.rs b/src/18/template/src/impls.rs new file mode 100644 index 00000000..4b507f20 --- /dev/null +++ b/src/18/template/src/impls.rs @@ -0,0 +1,20 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ + + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + /* TODO: Insert `kitty`into the map instead of `()`. */ + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/18/template/src/lib.rs b/src/18/template/src/lib.rs index b7a5372e..5e40b257 100644 --- a/src/18/template/src/lib.rs +++ b/src/18/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -63,23 +65,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ - - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - /* TODO: Insert `kitty`into the map instead of `()`. */ - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/18/template/template.diff b/src/18/template/template.diff index 2c2fe96a..9212ca34 100644 --- a/src/18/template/template.diff +++ b/src/18/template/template.diff @@ -1,8 +1,27 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 650fdfb..4b507f2 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -4,11 +4,14 @@ use frame_support::pallet_prelude::*; + impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { ++ /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ ++ + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; ++ /* TODO: Insert `kitty`into the map instead of `()`. */ + Kitties::::insert(dna, ()); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); diff --git a/src/lib.rs b/src/lib.rs -index 5ffb395..b7a5372 100644 +index 5f5f631..5e40b25 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,6 +20,10 @@ pub mod pallet { +@@ -22,6 +22,10 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } @@ -13,7 +32,7 @@ index 5ffb395..b7a5372 100644 pub struct Kitty { // Using 16 bytes to represent a kitty DNA pub dna: [u8; 16], -@@ -32,6 +36,7 @@ pub mod pallet { +@@ -34,6 +38,7 @@ pub mod pallet { /// Learn about storage maps. #[pallet::storage] @@ -21,18 +40,3 @@ index 5ffb395..b7a5372 100644 pub(super) type Kitties = StorageMap; // Learn about events. -@@ -63,11 +68,14 @@ pub mod pallet { - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { -+ /* Create a new variable `kitty` which is a `Kitty` struct with `dna` and `owner`. */ -+ - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -+ /* TODO: Insert `kitty`into the map instead of `()`. */ - Kitties::::insert(dna, ()); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); diff --git a/src/19/README.md b/src/19/README.md index 061af0d2..cead1dba 100644 --- a/src/19/README.md +++ b/src/19/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/19/solution/solution.diff b/src/19/solution/solution.diff index 7c7c0675..b68a9f7e 100644 --- a/src/19/solution/solution.diff +++ b/src/19/solution/solution.diff @@ -1,8 +1,43 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 4a482ed..1088c86 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -2,16 +2,20 @@ use super::*; + use frame_support::pallet_prelude::*; + + impl Pallet { +- /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. +- - Create a `unique_payload` which contains data from `frame_system::Pallet::`: +- - `parent_hash` +- - `block_number` +- - `extrinsic_index` +- - `CountForKitties::::get()` +- - `encode()` that payload to a byte array named `encoded_payload`. +- - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. +- - Return the 16 byte hash. +- */ ++ // Generates and returns DNA and Sex ++ pub fn gen_dna() -> [u8; 16] { ++ // Create randomness payload. Multiple kitties can be generated in the same block, ++ // retaining uniqueness. ++ let unique_payload = ( ++ frame_system::Pallet::::parent_hash(), ++ frame_system::Pallet::::block_number(), ++ frame_system::Pallet::::extrinsic_index(), ++ CountForKitties::::get(), ++ ); ++ ++ let encoded_payload: Vec = unique_payload.encode(); ++ frame_support::Hashable::blake2_128(&encoded_payload) ++ } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { diff --git a/src/lib.rs b/src/lib.rs -index c8ef256..cbfa8b5 100644 +index 63c9b02..ed0a13b 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -55,8 +55,7 @@ pub mod pallet { +@@ -57,8 +57,7 @@ pub mod pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { // Learn about `origin`. let who = ensure_signed(origin)?; @@ -12,34 +47,3 @@ index c8ef256..cbfa8b5 100644 Self::mint(who, dna)?; Ok(()) } -@@ -64,16 +63,20 @@ pub mod pallet { - - // Learn about internal functions. - impl Pallet { -- /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. -- - Create a `unique_payload` which contains data from `frame_system::Pallet::`: -- - `parent_hash` -- - `block_number` -- - `extrinsic_index` -- - `CountForKitties::::get()` -- - `encode()` that payload to a byte array named `encoded_payload`. -- - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. -- - Return the 16 byte hash. -- */ -+ // Generates and returns DNA and Sex -+ fn gen_dna() -> [u8; 16] { -+ // Create randomness payload. Multiple kitties can be generated in the same block, -+ // retaining uniqueness. -+ let unique_payload = ( -+ frame_system::Pallet::::parent_hash(), -+ frame_system::Pallet::::block_number(), -+ frame_system::Pallet::::extrinsic_index(), -+ CountForKitties::::get(), -+ ); -+ -+ let encoded_payload: Vec = unique_payload.encode(); -+ frame_support::Hashable::blake2_128(&encoded_payload) -+ } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { diff --git a/src/19/solution/src/impls.rs b/src/19/solution/src/impls.rs new file mode 100644 index 00000000..1088c864 --- /dev/null +++ b/src/19/solution/src/impls.rs @@ -0,0 +1,33 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload: Vec = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/19/solution/src/lib.rs b/src/19/solution/src/lib.rs index cbfa8b5c..ed0a13b5 100644 --- a/src/19/solution/src/lib.rs +++ b/src/19/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -60,36 +62,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload: Vec = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/19/template/src/impls.rs b/src/19/template/src/impls.rs new file mode 100644 index 00000000..4a482eda --- /dev/null +++ b/src/19/template/src/impls.rs @@ -0,0 +1,29 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. + - Create a `unique_payload` which contains data from `frame_system::Pallet::`: + - `parent_hash` + - `block_number` + - `extrinsic_index` + - `CountForKitties::::get()` + - `encode()` that payload to a byte array named `encoded_payload`. + - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. + - Return the 16 byte hash. + */ + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/19/template/src/lib.rs b/src/19/template/src/lib.rs index c8ef2561..63c9b02b 100644 --- a/src/19/template/src/lib.rs +++ b/src/19/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -61,32 +63,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. - - Create a `unique_payload` which contains data from `frame_system::Pallet::`: - - `parent_hash` - - `block_number` - - `extrinsic_index` - - `CountForKitties::::get()` - - `encode()` that payload to a byte array named `encoded_payload`. - - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. - - Return the 16 byte hash. - */ - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/19/template/template.diff b/src/19/template/template.diff index 8cfc3de0..3c8a7cae 100644 --- a/src/19/template/template.diff +++ b/src/19/template/template.diff @@ -1,8 +1,30 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 5ec6eb6..4a482ed 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -2,6 +2,17 @@ use super::*; + use frame_support::pallet_prelude::*; + + impl Pallet { ++ /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. ++ - Create a `unique_payload` which contains data from `frame_system::Pallet::`: ++ - `parent_hash` ++ - `block_number` ++ - `extrinsic_index` ++ - `CountForKitties::::get()` ++ - `encode()` that payload to a byte array named `encoded_payload`. ++ - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. ++ - Return the 16 byte hash. ++ */ ++ + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; diff --git a/src/lib.rs b/src/lib.rs -index 62bd20e..c8ef256 100644 +index 7fcaaad..63c9b02 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -55,6 +55,7 @@ pub mod pallet { +@@ -57,6 +57,7 @@ pub mod pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { // Learn about `origin`. let who = ensure_signed(origin)?; @@ -10,21 +32,3 @@ index 62bd20e..c8ef256 100644 let dna = [0u8; 16]; Self::mint(who, dna)?; Ok(()) -@@ -63,6 +64,17 @@ pub mod pallet { - - // Learn about internal functions. - impl Pallet { -+ /* TODO: Create a function `gen_dna` which returns a `[u8; 16]`. -+ - Create a `unique_payload` which contains data from `frame_system::Pallet::`: -+ - `parent_hash` -+ - `block_number` -+ - `extrinsic_index` -+ - `CountForKitties::::get()` -+ - `encode()` that payload to a byte array named `encoded_payload`. -+ - Use `frame_support::Hashable` to perform a `blake2_128` hash on the encoded payload. -+ - Return the 16 byte hash. -+ */ -+ - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; diff --git a/src/2/source/changes.diff b/src/2/source/changes.diff index e1c7f33e..795dc554 100644 --- a/src/2/source/changes.diff +++ b/src/2/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 8b9507e..44cc2ec 100644 +index f846f0b..4d56aa1 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -2,6 +2,7 @@ +@@ -4,6 +4,7 @@ mod impls; pub use pallet::*; diff --git a/src/2/source/src/impls.rs b/src/2/source/src/impls.rs new file mode 100644 index 00000000..d49f793b --- /dev/null +++ b/src/2/source/src/impls.rs @@ -0,0 +1,9 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/2/source/src/lib.rs b/src/2/source/src/lib.rs index 44cc2ec8..4d56aa19 100644 --- a/src/2/source/src/lib.rs +++ b/src/2/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -34,11 +36,4 @@ pub mod pallet { Ok(()) } } - - impl Pallet { - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/20/README.md b/src/20/README.md index 061af0d2..cead1dba 100644 --- a/src/20/README.md +++ b/src/20/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/20/solution/solution.diff b/src/20/solution/solution.diff index d68cc0ef..b93317af 100644 --- a/src/20/solution/solution.diff +++ b/src/20/solution/solution.diff @@ -1,8 +1,25 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 9678a42..b91e955 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -26,10 +26,10 @@ impl Pallet { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + +- /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ +- ++ KittiesOwned::::append(&owner, dna); + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); ++ + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } diff --git a/src/lib.rs b/src/lib.rs -index fa1418e..7142e28 100644 +index db7d83b..f6d7426 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -36,11 +36,10 @@ pub mod pallet { +@@ -38,11 +38,10 @@ pub mod pallet { #[pallet::storage] pub(super) type Kitties = StorageMap>; @@ -18,16 +35,3 @@ index fa1418e..7142e28 100644 // Learn about events. #[pallet::event] -@@ -93,10 +92,10 @@ pub mod pallet { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - -- /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ -- -+ KittiesOwned::::append(&owner, dna); - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); -+ - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } diff --git a/src/20/solution/src/impls.rs b/src/20/solution/src/impls.rs new file mode 100644 index 00000000..b91e9554 --- /dev/null +++ b/src/20/solution/src/impls.rs @@ -0,0 +1,36 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::append(&owner, dna); + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/20/solution/src/lib.rs b/src/20/solution/src/lib.rs index 7142e28e..f6d7426d 100644 --- a/src/20/solution/src/lib.rs +++ b/src/20/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -65,39 +67,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::append(&owner, dna); - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/20/template/src/impls.rs b/src/20/template/src/impls.rs new file mode 100644 index 00000000..9678a42b --- /dev/null +++ b/src/20/template/src/impls.rs @@ -0,0 +1,36 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ + + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/20/template/src/lib.rs b/src/20/template/src/lib.rs index fa1418e8..db7d83b8 100644 --- a/src/20/template/src/lib.rs +++ b/src/20/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -66,39 +68,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ - - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/20/template/template.diff b/src/20/template/template.diff index dd72bb8f..3843d0cc 100644 --- a/src/20/template/template.diff +++ b/src/20/template/template.diff @@ -1,8 +1,31 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 1088c86..9678a42 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -13,7 +13,7 @@ impl Pallet { + CountForKitties::::get(), + ); + +- let encoded_payload: Vec = unique_payload.encode(); ++ let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + +@@ -25,6 +25,9 @@ impl Pallet { + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; ++ ++ /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ ++ + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + Self::deposit_event(Event::::Created { owner }); diff --git a/src/lib.rs b/src/lib.rs -index cbfa8b5..fa1418e 100644 +index ed0a13b..db7d83b 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -36,6 +36,12 @@ pub mod pallet { +@@ -38,6 +38,12 @@ pub mod pallet { #[pallet::storage] pub(super) type Kitties = StorageMap>; @@ -15,22 +38,3 @@ index cbfa8b5..fa1418e 100644 // Learn about events. #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] -@@ -74,7 +80,7 @@ pub mod pallet { - CountForKitties::::get(), - ); - -- let encoded_payload: Vec = unique_payload.encode(); -+ let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - -@@ -86,6 +92,9 @@ pub mod pallet { - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; -+ -+ /* TODO: `append` the `dna` to the `KittiesOwned` storage for the `owner`. */ -+ - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - Self::deposit_event(Event::::Created { owner }); diff --git a/src/21/README.md b/src/21/README.md index 061af0d2..cead1dba 100644 --- a/src/21/README.md +++ b/src/21/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/21/solution/solution.diff b/src/21/solution/solution.diff index 144827dc..a37d84ef 100644 --- a/src/21/solution/solution.diff +++ b/src/21/solution/solution.diff @@ -1,8 +1,30 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 1d20f32..300924a 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -1,6 +1,7 @@ + use super::*; + use frame_support::pallet_prelude::*; + ++// Learn about internal functions. + impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { +@@ -26,8 +27,7 @@ impl Pallet { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + +- /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ +- KittiesOwned::::append(&owner, dna); ++ KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + diff --git a/src/lib.rs b/src/lib.rs -index 5c8c3d6..6730289 100644 +index ec52a49..8209cc2 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -40,8 +40,7 @@ pub mod pallet { +@@ -42,8 +42,7 @@ pub mod pallet { #[pallet::storage] pub(super) type KittiesOwned = StorageMap< Key = T::AccountId, @@ -12,7 +34,7 @@ index 5c8c3d6..6730289 100644 QueryKind = ValueQuery, >; -@@ -56,7 +55,7 @@ pub mod pallet { +@@ -58,7 +57,7 @@ pub mod pallet { pub enum Error { TooManyKitties, DuplicateKitty, @@ -21,13 +43,3 @@ index 5c8c3d6..6730289 100644 } // Learn about callable functions and dispatch. -@@ -97,8 +96,7 @@ pub mod pallet { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - -- /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ -- KittiesOwned::::append(&owner, dna); -+ KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - diff --git a/src/21/solution/src/impls.rs b/src/21/solution/src/impls.rs new file mode 100644 index 00000000..300924a7 --- /dev/null +++ b/src/21/solution/src/impls.rs @@ -0,0 +1,37 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/21/solution/src/lib.rs b/src/21/solution/src/lib.rs index 6730289c..8209cc22 100644 --- a/src/21/solution/src/lib.rs +++ b/src/21/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -69,39 +71,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/21/template/src/impls.rs b/src/21/template/src/impls.rs new file mode 100644 index 00000000..1d20f328 --- /dev/null +++ b/src/21/template/src/impls.rs @@ -0,0 +1,37 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ + KittiesOwned::::append(&owner, dna); + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/21/template/src/lib.rs b/src/21/template/src/lib.rs index 5c8c3d6d..ec52a492 100644 --- a/src/21/template/src/lib.rs +++ b/src/21/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -70,40 +72,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ - KittiesOwned::::append(&owner, dna); - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/21/template/template.diff b/src/21/template/template.diff index ffafe13d..af80d72f 100644 --- a/src/21/template/template.diff +++ b/src/21/template/template.diff @@ -1,8 +1,20 @@ +diff --git a/src/impls.rs b/src/impls.rs +index b91e955..1d20f32 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -26,6 +26,7 @@ impl Pallet { + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + ++ /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ + KittiesOwned::::append(&owner, dna); + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); diff --git a/src/lib.rs b/src/lib.rs -index 7142e28..5c8c3d6 100644 +index f6d7426..ec52a49 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -38,8 +38,12 @@ pub mod pallet { +@@ -40,8 +40,12 @@ pub mod pallet { /// Track the kitties owned by each account. #[pallet::storage] @@ -17,7 +29,7 @@ index 7142e28..5c8c3d6 100644 // Learn about events. #[pallet::event] -@@ -52,6 +56,7 @@ pub mod pallet { +@@ -54,6 +58,7 @@ pub mod pallet { pub enum Error { TooManyKitties, DuplicateKitty, @@ -25,11 +37,3 @@ index 7142e28..5c8c3d6 100644 } // Learn about callable functions and dispatch. -@@ -92,6 +97,7 @@ pub mod pallet { - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - -+ /* TODO: Update `append` to `try_append` and `map_err` to `Error::::TooManyOwned`. */ - KittiesOwned::::append(&owner, dna); - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); diff --git a/src/22/source/src/impls.rs b/src/22/source/src/impls.rs new file mode 100644 index 00000000..300924a7 --- /dev/null +++ b/src/22/source/src/impls.rs @@ -0,0 +1,37 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/22/source/src/lib.rs b/src/22/source/src/lib.rs index 6730289c..8209cc22 100644 --- a/src/22/source/src/lib.rs +++ b/src/22/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -69,39 +71,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/23/README.md b/src/23/README.md index 061af0d2..cead1dba 100644 --- a/src/23/README.md +++ b/src/23/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/23/solution/solution.diff b/src/23/solution/solution.diff index 078b8fcf..1ba74b65 100644 --- a/src/23/solution/solution.diff +++ b/src/23/solution/solution.diff @@ -1,8 +1,32 @@ +diff --git a/src/impls.rs b/src/impls.rs +index b2b0d67..8210525 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -35,14 +35,9 @@ impl Pallet { + Ok(()) + } + +- /* TODO: Create an internal function called `do_transfer`: +- - It has inputs: +- - `from` which is `T::AccountId`. +- - `to` which is `T::AccountId`. +- - `kitty_id` which is `[u8; 16]`. +- - It returns a `DispatchResult` +- - The inner logic for now is: +- - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. +- - Return `Ok(())`. +- */ ++ // Update storage to transfer kitty ++ pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { ++ Self::deposit_event(Event::::Transferred { from, to, kitty_id }); ++ Ok(()) ++ } + } diff --git a/src/lib.rs b/src/lib.rs -index bfddc9e..8f48917 100644 +index 2c51fa7..2f975da 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -49,12 +49,7 @@ pub mod pallet { +@@ -51,12 +51,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { Created { owner: T::AccountId }, @@ -16,7 +40,7 @@ index bfddc9e..8f48917 100644 } #[pallet::error] -@@ -75,17 +70,15 @@ pub mod pallet { +@@ -77,16 +72,14 @@ pub mod pallet { Ok(()) } @@ -39,32 +63,6 @@ index bfddc9e..8f48917 100644 + let who = ensure_signed(origin)?; + Self::do_transfer(who, to, kitty_id)?; + Ok(()) -+ } - } - - // Learn about internal functions. -@@ -122,15 +115,14 @@ pub mod pallet { - Ok(()) - } - -- /* TODO: Create an internal function called `do_transfer`: -- - It has inputs: -- - `from` which is `T::AccountId`. -- - `to` which is `T::AccountId`. -- - `kitty_id` which is `[u8; 16]`. -- - It returns a `DispatchResult` -- - The inner logic for now is: -- - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. -- - Return `Ok(())`. -- */ -+ // Update storage to transfer kitty -+ pub fn do_transfer( -+ from: T::AccountId, -+ to: T::AccountId, -+ kitty_id: [u8; 16], -+ ) -> DispatchResult { -+ Self::deposit_event(Event::::Transferred { from, to, kitty_id }); -+ Ok(()) + } } } diff --git a/src/23/solution/src/impls.rs b/src/23/solution/src/impls.rs new file mode 100644 index 00000000..8210525e --- /dev/null +++ b/src/23/solution/src/impls.rs @@ -0,0 +1,43 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } +} diff --git a/src/23/solution/src/lib.rs b/src/23/solution/src/lib.rs index 8f489176..2f975da4 100644 --- a/src/23/solution/src/lib.rs +++ b/src/23/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -80,49 +82,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - } } diff --git a/src/23/template/src/impls.rs b/src/23/template/src/impls.rs new file mode 100644 index 00000000..b2b0d678 --- /dev/null +++ b/src/23/template/src/impls.rs @@ -0,0 +1,48 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + /* TODO: Create an internal function called `do_transfer`: + - It has inputs: + - `from` which is `T::AccountId`. + - `to` which is `T::AccountId`. + - `kitty_id` which is `[u8; 16]`. + - It returns a `DispatchResult` + - The inner logic for now is: + - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. + - Return `Ok(())`. + */ +} diff --git a/src/23/template/src/lib.rs b/src/23/template/src/lib.rs index bfddc9e6..2c51fa76 100644 --- a/src/23/template/src/lib.rs +++ b/src/23/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -87,50 +89,4 @@ pub mod pallet { - End with Ok(()). */ } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - /* TODO: Create an internal function called `do_transfer`: - - It has inputs: - - `from` which is `T::AccountId`. - - `to` which is `T::AccountId`. - - `kitty_id` which is `[u8; 16]`. - - It returns a `DispatchResult` - - The inner logic for now is: - - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. - - Return `Ok(())`. - */ - } } diff --git a/src/23/template/template.diff b/src/23/template/template.diff index 516504f5..c937b565 100644 --- a/src/23/template/template.diff +++ b/src/23/template/template.diff @@ -1,8 +1,28 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 300924a..b2b0d67 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -34,4 +34,15 @@ impl Pallet { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } ++ ++ /* TODO: Create an internal function called `do_transfer`: ++ - It has inputs: ++ - `from` which is `T::AccountId`. ++ - `to` which is `T::AccountId`. ++ - `kitty_id` which is `[u8; 16]`. ++ - It returns a `DispatchResult` ++ - The inner logic for now is: ++ - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. ++ - Return `Ok(())`. ++ */ + } diff --git a/src/lib.rs b/src/lib.rs -index 6730289..bfddc9e 100644 +index 8209cc2..2c51fa7 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -49,6 +49,12 @@ pub mod pallet { +@@ -51,6 +51,12 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { Created { owner: T::AccountId }, @@ -15,7 +35,7 @@ index 6730289..bfddc9e 100644 } #[pallet::error] -@@ -68,6 +74,18 @@ pub mod pallet { +@@ -70,5 +76,17 @@ pub mod pallet { Self::mint(who, dna)?; Ok(()) } @@ -30,24 +50,6 @@ index 6730289..bfddc9e 100644 + - Get the caller `who` from `ensure_signed`. + - Call `Self::do_transfer`, and propagate the result. + - End with Ok(()). -+ */ - } - - // Learn about internal functions. -@@ -103,5 +121,16 @@ pub mod pallet { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } -+ -+ /* TODO: Create an internal function called `do_transfer`: -+ - It has inputs: -+ - `from` which is `T::AccountId`. -+ - `to` which is `T::AccountId`. -+ - `kitty_id` which is `[u8; 16]`. -+ - It returns a `DispatchResult` -+ - The inner logic for now is: -+ - Call `Self::dispatch_event` on and emit `Event:::Transferred` with params. -+ - Return `Ok(())`. + */ } } diff --git a/src/24/README.md b/src/24/README.md index 061af0d2..cead1dba 100644 --- a/src/24/README.md +++ b/src/24/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/24/solution/solution.diff b/src/24/solution/solution.diff index 4f83aac0..b128cfff 100644 --- a/src/24/solution/solution.diff +++ b/src/24/solution/solution.diff @@ -1,8 +1,58 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 97b5feb..502cfc1 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -37,28 +37,23 @@ impl Pallet { + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { +- /* TODO: Sanity check the transfer is allowed: +- - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. +- - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. +- - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. +- */ +- +- /* TODO: Update the owner of the kitty: +- - Update `kitty.owner` to `to`. +- - Update the `KittiesOwned` of `from` and `to: +- - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. +- - `try_push` the `kitty_id` to the `to_owned` vector. +- - If the vector is full, `map_err` and return `Error::::TooManyOwned`. +- - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. +- - Write logic to `swap_remove` the item from the `from_owned` vector. +- - If you cannot find the kitty in the vector, return `Error::::NoKitty`. +- */ +- +- /* TODO: Update the final storage. +- - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. +- - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. +- - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. +- */ ++ ensure!(from != to, Error::::TransferToSelf); ++ let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; ++ ensure!(kitty.owner == from, Error::::NotOwner); ++ kitty.owner = to.clone(); ++ ++ let mut to_owned = KittiesOwned::::get(&to); ++ to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; ++ let mut from_owned = KittiesOwned::::get(&from); ++ if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { ++ from_owned.swap_remove(ind); ++ } else { ++ return Err(Error::::NoKitty.into()) ++ } ++ ++ Kitties::::insert(kitty_id, kitty); ++ KittiesOwned::::insert(&to, to_owned); ++ KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index a7e3048..903ab1b 100644 +index a55136f..bdcaa68 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -57,11 +57,9 @@ pub mod pallet { +@@ -59,11 +59,9 @@ pub mod pallet { TooManyKitties, DuplicateKitty, TooManyOwned, @@ -17,49 +67,3 @@ index a7e3048..903ab1b 100644 } // Learn about callable functions and dispatch. -@@ -126,28 +124,23 @@ pub mod pallet { - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { -- /* TODO: Sanity check the transfer is allowed: -- - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. -- - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. -- - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. -- */ -- -- /* TODO: Update the owner of the kitty: -- - Update `kitty.owner` to `to`. -- - Update the `KittiesOwned` of `from` and `to: -- - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. -- - `try_push` the `kitty_id` to the `to_owned` vector. -- - If the vector is full, `map_err` and return `Error::::TooManyOwned`. -- - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. -- - Write logic to `swap_remove` the item from the `from_owned` vector. -- - If you cannot find the kitty in the vector, return `Error::::NoKitty`. -- */ -- -- /* TODO: Update the final storage. -- - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. -- - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. -- - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. -- */ -+ ensure!(from != to, Error::::TransferToSelf); -+ let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; -+ ensure!(kitty.owner == from, Error::::NotOwner); -+ kitty.owner = to.clone(); -+ -+ let mut to_owned = KittiesOwned::::get(&to); -+ to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; -+ let mut from_owned = KittiesOwned::::get(&from); -+ if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { -+ from_owned.swap_remove(ind); -+ } else { -+ return Err(Error::::NoKitty.into()) -+ } -+ -+ Kitties::::insert(kitty_id, kitty); -+ KittiesOwned::::insert(&to, to_owned); -+ KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) diff --git a/src/24/solution/src/impls.rs b/src/24/solution/src/impls.rs new file mode 100644 index 00000000..502cfc12 --- /dev/null +++ b/src/24/solution/src/impls.rs @@ -0,0 +1,61 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } +} diff --git a/src/24/solution/src/lib.rs b/src/24/solution/src/lib.rs index 903ab1b3..bdcaa68a 100644 --- a/src/24/solution/src/lib.rs +++ b/src/24/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -83,67 +85,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - } } diff --git a/src/24/template/src/impls.rs b/src/24/template/src/impls.rs new file mode 100644 index 00000000..97b5febb --- /dev/null +++ b/src/24/template/src/impls.rs @@ -0,0 +1,66 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + /* TODO: Sanity check the transfer is allowed: + - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. + - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. + - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. + */ + + /* TODO: Update the owner of the kitty: + - Update `kitty.owner` to `to`. + - Update the `KittiesOwned` of `from` and `to: + - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. + - `try_push` the `kitty_id` to the `to_owned` vector. + - If the vector is full, `map_err` and return `Error::::TooManyOwned`. + - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. + - Write logic to `swap_remove` the item from the `from_owned` vector. + - If you cannot find the kitty in the vector, return `Error::::NoKitty`. + */ + + /* TODO: Update the final storage. + - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. + - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. + - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. + */ + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } +} diff --git a/src/24/template/src/lib.rs b/src/24/template/src/lib.rs index a7e30481..a55136f9 100644 --- a/src/24/template/src/lib.rs +++ b/src/24/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -85,72 +87,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - /* TODO: Sanity check the transfer is allowed: - - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. - - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. - - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. - */ - - /* TODO: Update the owner of the kitty: - - Update `kitty.owner` to `to`. - - Update the `KittiesOwned` of `from` and `to: - - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. - - `try_push` the `kitty_id` to the `to_owned` vector. - - If the vector is full, `map_err` and return `Error::::TooManyOwned`. - - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. - - Write logic to `swap_remove` the item from the `from_owned` vector. - - If you cannot find the kitty in the vector, return `Error::::NoKitty`. - */ - - /* TODO: Update the final storage. - - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. - - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. - - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. - */ - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - } } diff --git a/src/24/template/template.diff b/src/24/template/template.diff index 30274d06..22c28c89 100644 --- a/src/24/template/template.diff +++ b/src/24/template/template.diff @@ -1,8 +1,42 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 8210525..97b5feb 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -37,6 +37,29 @@ impl Pallet { + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { ++ /* TODO: Sanity check the transfer is allowed: ++ - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. ++ - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. ++ - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. ++ */ ++ ++ /* TODO: Update the owner of the kitty: ++ - Update `kitty.owner` to `to`. ++ - Update the `KittiesOwned` of `from` and `to: ++ - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. ++ - `try_push` the `kitty_id` to the `to_owned` vector. ++ - If the vector is full, `map_err` and return `Error::::TooManyOwned`. ++ - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. ++ - Write logic to `swap_remove` the item from the `from_owned` vector. ++ - If you cannot find the kitty in the vector, return `Error::::NoKitty`. ++ */ ++ ++ /* TODO: Update the final storage. ++ - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. ++ - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. ++ - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. ++ */ ++ + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } diff --git a/src/lib.rs b/src/lib.rs -index 8f48917..a7e3048 100644 +index 2f975da..a55136f 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -57,6 +57,11 @@ pub mod pallet { +@@ -59,6 +59,11 @@ pub mod pallet { TooManyKitties, DuplicateKitty, TooManyOwned, @@ -14,33 +48,3 @@ index 8f48917..a7e3048 100644 } // Learn about callable functions and dispatch. -@@ -121,6 +126,29 @@ pub mod pallet { - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { -+ /* TODO: Sanity check the transfer is allowed: -+ - First `ensure!` that `from` and `to` are not equal, else return `Error::::TransferToSelf`. -+ - Get the `kitty` from `Kitties` using `kitty_id`, else return `Error::::NoKitty`. -+ - Check the `kitty.owner` is equal to `from`, else return `NotOwner`. -+ */ -+ -+ /* TODO: Update the owner of the kitty: -+ - Update `kitty.owner` to `to`. -+ - Update the `KittiesOwned` of `from` and `to: -+ - Create a mutable `to_owned` by querying `KittiesOwned` for `to`. -+ - `try_push` the `kitty_id` to the `to_owned` vector. -+ - If the vector is full, `map_err` and return `Error::::TooManyOwned`. -+ - Create a mutable `from_owned` by querying `KittiesOwned` for `from`. -+ - Write logic to `swap_remove` the item from the `from_owned` vector. -+ - If you cannot find the kitty in the vector, return `Error::::NoKitty`. -+ */ -+ -+ /* TODO: Update the final storage. -+ - Insert into `Kitties` under `kitty_id` the modified `kitty` struct. -+ - Insert into `KittiesOwned` under `to` the modified `to_owned` vector. -+ - Insert into `KittiesOwned` under `from` the modified `from_owned` vector. -+ */ -+ - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } diff --git a/src/25/source/changes.diff b/src/25/source/changes.diff index a23f17eb..0c3fae4d 100644 --- a/src/25/source/changes.diff +++ b/src/25/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 903ab1b..5472d69 100644 +index bdcaa68..6b60305 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -6,7 +6,10 @@ pub use pallet::*; +@@ -8,7 +8,10 @@ pub use pallet::*; #[frame_support::pallet(dev_mode)] pub mod pallet { use super::*; @@ -14,7 +14,7 @@ index 903ab1b..5472d69 100644 use frame_system::pallet_prelude::*; // Learn about the Pallet struct: the structure on which we implement all functions and traits -@@ -18,6 +21,9 @@ pub mod pallet { +@@ -20,6 +23,9 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; diff --git a/src/25/source/src/impls.rs b/src/25/source/src/impls.rs new file mode 100644 index 00000000..502cfc12 --- /dev/null +++ b/src/25/source/src/impls.rs @@ -0,0 +1,61 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone() }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } +} diff --git a/src/25/source/src/lib.rs b/src/25/source/src/lib.rs index 5472d69b..6b603054 100644 --- a/src/25/source/src/lib.rs +++ b/src/25/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -89,67 +91,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone() }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - } } diff --git a/src/26/README.md b/src/26/README.md index 4c64326e..24d28895 100644 --- a/src/26/README.md +++ b/src/26/README.md @@ -15,9 +15,18 @@
- + +
-
+
+ +```rust +{{#include ./source/src/impls.rs}} +``` + +
+ +
```rust {{#include ./source/src/lib.rs}} diff --git a/src/26/source/changes.diff b/src/26/source/changes.diff index cd3bd29e..39a62ef8 100644 --- a/src/26/source/changes.diff +++ b/src/26/source/changes.diff @@ -1,8 +1,21 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 502cfc1..765ad4f 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -20,7 +20,7 @@ impl Pallet { + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { +- let kitty = Kitty { dna, owner: owner.clone() }; ++ let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + diff --git a/src/lib.rs b/src/lib.rs -index 5472d69..c379017 100644 +index 6b60305..65fb75e 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -26,12 +26,17 @@ pub mod pallet { +@@ -28,12 +28,17 @@ pub mod pallet { type NativeBalance: Inspect + Mutate; } @@ -20,12 +33,3 @@ index 5472d69..c379017 100644 } /// Learn about storage value. -@@ -109,7 +114,7 @@ pub mod pallet { - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { -- let kitty = Kitty { dna, owner: owner.clone() }; -+ let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - diff --git a/src/26/source/src/impls.rs b/src/26/source/src/impls.rs new file mode 100644 index 00000000..765ad4f0 --- /dev/null +++ b/src/26/source/src/impls.rs @@ -0,0 +1,61 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } +} diff --git a/src/26/source/src/lib.rs b/src/26/source/src/lib.rs index c3790178..65fb75e7 100644 --- a/src/26/source/src/lib.rs +++ b/src/26/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -94,67 +96,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - } } diff --git a/src/27/README.md b/src/27/README.md index 061af0d2..cead1dba 100644 --- a/src/27/README.md +++ b/src/27/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/27/solution/solution.diff b/src/27/solution/solution.diff index d27d2e2d..ab1bd122 100644 --- a/src/27/solution/solution.diff +++ b/src/27/solution/solution.diff @@ -1,8 +1,35 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 0afb6df..d844db6 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -59,14 +59,12 @@ impl Pallet { + Ok(()) + } + +- /* TODO: Make an internal function called `do_set_price`: +- - Inputs to the function are: +- - `caller` which is `T::AccountId`. +- - `kitty_id` which is `[u8; 16]`. +- - `new_price` which is `Option`. +- - Returns a `DispatchResult`. +- - The internal logic, for now, should be: +- - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. +- - Return `Ok(())`. +- */ ++ pub fn do_set_price( ++ caller: T::AccountId, ++ kitty_id: [u8; 16], ++ new_price: Option>, ++ ) -> DispatchResult { ++ Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); ++ Ok(()) ++ } + } diff --git a/src/lib.rs b/src/lib.rs -index 6567e52..ebaf904 100644 +index 5c9dabe..52c6f53 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -61,11 +61,7 @@ pub mod pallet { +@@ -63,11 +63,7 @@ pub mod pallet { pub enum Event { Created { owner: T::AccountId }, Transferred { from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16] }, @@ -15,7 +42,7 @@ index 6567e52..ebaf904 100644 } #[pallet::error] -@@ -99,17 +95,15 @@ pub mod pallet { +@@ -101,16 +97,14 @@ pub mod pallet { Ok(()) } @@ -38,31 +65,6 @@ index 6567e52..ebaf904 100644 + let who = ensure_signed(origin)?; + Self::do_set_price(who, kitty_id, new_price)?; + Ok(()) -+ } - } - - // Learn about internal functions. -@@ -174,15 +168,13 @@ pub mod pallet { - Ok(()) - } - -- /* TODO: Make an internal function called `do_set_price`: -- - Inputs to the function are: -- - `caller` which is `T::AccountId`. -- - `kitty_id` which is `[u8; 16]`. -- - `new_price` which is `Option`. -- - Returns a `DispatchResult`. -- - The internal logic, for now, should be: -- - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. -- - Return `Ok(())`. -- */ -+ pub fn do_set_price( -+ caller: T::AccountId, -+ kitty_id: [u8; 16], -+ new_price: Option>, -+ ) -> DispatchResult { -+ Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); -+ Ok(()) + } } } diff --git a/src/27/solution/src/impls.rs b/src/27/solution/src/impls.rs new file mode 100644 index 00000000..d844db66 --- /dev/null +++ b/src/27/solution/src/impls.rs @@ -0,0 +1,70 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } +} diff --git a/src/27/solution/src/lib.rs b/src/27/solution/src/lib.rs index ebaf904a..52c6f530 100644 --- a/src/27/solution/src/lib.rs +++ b/src/27/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -105,76 +107,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - } } diff --git a/src/27/template/src/impls.rs b/src/27/template/src/impls.rs new file mode 100644 index 00000000..0afb6dfa --- /dev/null +++ b/src/27/template/src/impls.rs @@ -0,0 +1,72 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + /* TODO: Make an internal function called `do_set_price`: + - Inputs to the function are: + - `caller` which is `T::AccountId`. + - `kitty_id` which is `[u8; 16]`. + - `new_price` which is `Option`. + - Returns a `DispatchResult`. + - The internal logic, for now, should be: + - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. + - Return `Ok(())`. + */ +} diff --git a/src/27/template/src/lib.rs b/src/27/template/src/lib.rs index 6567e52f..5c9dabeb 100644 --- a/src/27/template/src/lib.rs +++ b/src/27/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -111,78 +113,4 @@ pub mod pallet { - Return `Ok(())`. */ } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - /* TODO: Make an internal function called `do_set_price`: - - Inputs to the function are: - - `caller` which is `T::AccountId`. - - `kitty_id` which is `[u8; 16]`. - - `new_price` which is `Option`. - - Returns a `DispatchResult`. - - The internal logic, for now, should be: - - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. - - Return `Ok(())`. - */ - } } diff --git a/src/27/template/template.diff b/src/27/template/template.diff index 5674a256..b83f4d99 100644 --- a/src/27/template/template.diff +++ b/src/27/template/template.diff @@ -1,8 +1,28 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 765ad4f..0afb6df 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -58,4 +58,15 @@ impl Pallet { + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } ++ ++ /* TODO: Make an internal function called `do_set_price`: ++ - Inputs to the function are: ++ - `caller` which is `T::AccountId`. ++ - `kitty_id` which is `[u8; 16]`. ++ - `new_price` which is `Option`. ++ - Returns a `DispatchResult`. ++ - The internal logic, for now, should be: ++ - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. ++ - Return `Ok(())`. ++ */ + } diff --git a/src/lib.rs b/src/lib.rs -index c379017..6567e52 100644 +index 65fb75e..5c9dabe 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -61,6 +61,11 @@ pub mod pallet { +@@ -63,6 +63,11 @@ pub mod pallet { pub enum Event { Created { owner: T::AccountId }, Transferred { from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16] }, @@ -14,7 +34,7 @@ index c379017..6567e52 100644 } #[pallet::error] -@@ -93,6 +98,18 @@ pub mod pallet { +@@ -95,5 +100,17 @@ pub mod pallet { Self::do_transfer(who, to, kitty_id)?; Ok(()) } @@ -29,24 +49,6 @@ index c379017..6567e52 100644 + - Extract the caller `who` with `ensure_signed`. + - Call `Self::do_set_price` with the appropriate parameters, propagating the result. + - Return `Ok(())`. -+ */ - } - - // Learn about internal functions. -@@ -156,5 +173,16 @@ pub mod pallet { - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } -+ -+ /* TODO: Make an internal function called `do_set_price`: -+ - Inputs to the function are: -+ - `caller` which is `T::AccountId`. -+ - `kitty_id` which is `[u8; 16]`. -+ - `new_price` which is `Option`. -+ - Returns a `DispatchResult`. -+ - The internal logic, for now, should be: -+ - `Self::deposit_event` for `Event::::PriceSet` with the appropriate params. -+ - Return `Ok(())`. + */ } } diff --git a/src/28/README.md b/src/28/README.md index 061af0d2..b692d103 100644 --- a/src/28/README.md +++ b/src/28/README.md @@ -17,12 +17,12 @@
- +
-
+
```rust -{{#include ./template/src/lib.rs}} +{{#include ./template/src/impls.rs}} ```
@@ -34,12 +34,12 @@
- +
-
+
```rust -{{#include ./solution/src/lib.rs}} +{{#include ./solution/src/impls.rs}} ```
diff --git a/src/28/solution/solution.diff b/src/28/solution/solution.diff index 27e082b6..4d5a53d1 100644 --- a/src/28/solution/solution.diff +++ b/src/28/solution/solution.diff @@ -1,22 +1,22 @@ -diff --git a/src/lib.rs b/src/lib.rs -index 36836ad..0ac3bab 100644 ---- a/src/lib.rs -+++ b/src/lib.rs -@@ -173,13 +173,10 @@ pub mod pallet { - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { -- /* TODO: Create the logic for setting the Kitty price: -- - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. -- - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. -- - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. -- - Set the `kitty.price` to `new_price`. -- - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. -- */ -+ let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; -+ ensure!(kitty.owner == caller, Error::::NotOwner); -+ kitty.price = new_price; -+ Kitties::::insert(kitty_id, kitty); +diff --git a/src/impls.rs b/src/impls.rs +index d89803c..d53238c 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -64,13 +64,10 @@ impl Pallet { + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { +- /* TODO: Create the logic for setting the Kitty price: +- - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. +- - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. +- - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. +- - Set the `kitty.price` to `new_price`. +- - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. +- */ ++ let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; ++ ensure!(kitty.owner == caller, Error::::NotOwner); ++ kitty.price = new_price; ++ Kitties::::insert(kitty_id, kitty); - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) diff --git a/src/28/solution/src/impls.rs b/src/28/solution/src/impls.rs new file mode 100644 index 00000000..d53238cb --- /dev/null +++ b/src/28/solution/src/impls.rs @@ -0,0 +1,75 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == caller, Error::::NotOwner); + kitty.price = new_price; + Kitties::::insert(kitty_id, kitty); + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } +} diff --git a/src/28/solution/src/lib.rs b/src/28/solution/src/lib.rs index 0ac3bab9..52c6f530 100644 --- a/src/28/solution/src/lib.rs +++ b/src/28/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -105,81 +107,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == caller, Error::::NotOwner); - kitty.price = new_price; - Kitties::::insert(kitty_id, kitty); - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - } } diff --git a/src/28/template/src/impls.rs b/src/28/template/src/impls.rs new file mode 100644 index 00000000..d89803c7 --- /dev/null +++ b/src/28/template/src/impls.rs @@ -0,0 +1,78 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + /* TODO: Create the logic for setting the Kitty price: + - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. + - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. + - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. + - Set the `kitty.price` to `new_price`. + - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. + */ + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } +} diff --git a/src/28/template/src/lib.rs b/src/28/template/src/lib.rs index 36836ad4..52c6f530 100644 --- a/src/28/template/src/lib.rs +++ b/src/28/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -105,84 +107,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - /* TODO: Create the logic for setting the Kitty price: - - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. - - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. - - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. - - Set the `kitty.price` to `new_price`. - - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. - */ - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - } } diff --git a/src/28/template/template.diff b/src/28/template/template.diff index 3d4477b8..950c4b52 100644 --- a/src/28/template/template.diff +++ b/src/28/template/template.diff @@ -1,19 +1,19 @@ -diff --git a/src/lib.rs b/src/lib.rs -index ebaf904..36836ad 100644 ---- a/src/lib.rs -+++ b/src/lib.rs -@@ -173,6 +173,14 @@ pub mod pallet { - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { -+ /* TODO: Create the logic for setting the Kitty price: -+ - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. -+ - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. -+ - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. -+ - Set the `kitty.price` to `new_price`. -+ - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. -+ */ +diff --git a/src/impls.rs b/src/impls.rs +index d844db6..d89803c 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -64,6 +64,14 @@ impl Pallet { + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { ++ /* TODO: Create the logic for setting the Kitty price: ++ - Create a mutable `kitty` by calling `get` on `Kitties` with `kitty_id`. ++ - Return an error if the kitty doesn't exist by returning `Error::::NoKitty`. ++ - `ensure!` that the `kitty.owner` is equal to the `caller` else return `Error::::NotOwner`. ++ - Set the `kitty.price` to `new_price`. ++ - Insert the modified `kitty` back into the `Kitties` map under `kitty_id`. ++ */ + - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } diff --git a/src/29/README.md b/src/29/README.md index 061af0d2..cead1dba 100644 --- a/src/29/README.md +++ b/src/29/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/29/solution/solution.diff b/src/29/solution/solution.diff index 58cdd32d..bb8db322 100644 --- a/src/29/solution/solution.diff +++ b/src/29/solution/solution.diff @@ -1,8 +1,35 @@ +diff --git a/src/impls.rs b/src/impls.rs +index d2b2015..0f425a0 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -73,14 +73,12 @@ impl Pallet { + Ok(()) + } + +- /* Create a new internal function `do_buy_kitty`: +- - Inputs to the function are: +- - `buyer` which is `T::AccountId`. +- - `kitty_id` which is `[u8; 16]`. +- - `price` which is `BalanceOf`. +- - It returns `DispatchResult`. +- - The internal logic, for now, should be: +- - `Self::deposit_event` with `Event::::Sold` and the appropriate params. +- - Return `Ok(())`. +- */ ++ pub fn do_buy_kitty( ++ buyer: T::AccountId, ++ kitty_id: [u8; 16], ++ price: BalanceOf, ++ ) -> DispatchResult { ++ Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); ++ Ok(()) ++ } + } diff --git a/src/lib.rs b/src/lib.rs -index b84ac0d..7c54bc8 100644 +index 5072ec6..d6c59f0 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -62,11 +62,7 @@ pub mod pallet { +@@ -64,11 +64,7 @@ pub mod pallet { Created { owner: T::AccountId }, Transferred { from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16] }, PriceSet { owner: T::AccountId, kitty_id: [u8; 16], new_price: Option> }, @@ -15,7 +42,7 @@ index b84ac0d..7c54bc8 100644 } #[pallet::error] -@@ -110,17 +106,15 @@ pub mod pallet { +@@ -112,16 +108,14 @@ pub mod pallet { Ok(()) } @@ -38,31 +65,6 @@ index b84ac0d..7c54bc8 100644 + let who = ensure_signed(origin)?; + Self::do_buy_kitty(who, kitty_id, max_price)?; + Ok(()) -+ } - } - - // Learn about internal functions. -@@ -199,15 +193,13 @@ pub mod pallet { - Ok(()) - } - -- /* Create a new internal function `do_buy_kitty`: -- - Inputs to the function are: -- - `buyer` which is `T::AccountId`. -- - `kitty_id` which is `[u8; 16]`. -- - `price` which is `BalanceOf`. -- - It returns `DispatchResult`. -- - The internal logic, for now, should be: -- - `Self::deposit_event` with `Event::::Sold` and the appropriate params. -- - Return `Ok(())`. -- */ -+ pub fn do_buy_kitty( -+ buyer: T::AccountId, -+ kitty_id: [u8; 16], -+ price: BalanceOf, -+ ) -> DispatchResult { -+ Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); -+ Ok(()) + } } } diff --git a/src/29/solution/src/impls.rs b/src/29/solution/src/impls.rs new file mode 100644 index 00000000..0f425a08 --- /dev/null +++ b/src/29/solution/src/impls.rs @@ -0,0 +1,84 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == caller, Error::::NotOwner); + kitty.price = new_price; + Kitties::::insert(kitty_id, kitty); + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } + + pub fn do_buy_kitty( + buyer: T::AccountId, + kitty_id: [u8; 16], + price: BalanceOf, + ) -> DispatchResult { + Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); + Ok(()) + } +} diff --git a/src/29/solution/src/lib.rs b/src/29/solution/src/lib.rs index 7c54bc89..d6c59f0e 100644 --- a/src/29/solution/src/lib.rs +++ b/src/29/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -116,90 +118,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == caller, Error::::NotOwner); - kitty.price = new_price; - Kitties::::insert(kitty_id, kitty); - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - - pub fn do_buy_kitty( - buyer: T::AccountId, - kitty_id: [u8; 16], - price: BalanceOf, - ) -> DispatchResult { - Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); - Ok(()) - } - } } diff --git a/src/29/template/src/impls.rs b/src/29/template/src/impls.rs new file mode 100644 index 00000000..d2b20157 --- /dev/null +++ b/src/29/template/src/impls.rs @@ -0,0 +1,86 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == caller, Error::::NotOwner); + kitty.price = new_price; + Kitties::::insert(kitty_id, kitty); + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } + + /* Create a new internal function `do_buy_kitty`: + - Inputs to the function are: + - `buyer` which is `T::AccountId`. + - `kitty_id` which is `[u8; 16]`. + - `price` which is `BalanceOf`. + - It returns `DispatchResult`. + - The internal logic, for now, should be: + - `Self::deposit_event` with `Event::::Sold` and the appropriate params. + - Return `Ok(())`. + */ +} diff --git a/src/29/template/src/lib.rs b/src/29/template/src/lib.rs index b84ac0dd..5072ec6a 100644 --- a/src/29/template/src/lib.rs +++ b/src/29/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -122,92 +124,4 @@ pub mod pallet { - Return `Ok(())`. */ } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == caller, Error::::NotOwner); - kitty.price = new_price; - Kitties::::insert(kitty_id, kitty); - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - - /* Create a new internal function `do_buy_kitty`: - - Inputs to the function are: - - `buyer` which is `T::AccountId`. - - `kitty_id` which is `[u8; 16]`. - - `price` which is `BalanceOf`. - - It returns `DispatchResult`. - - The internal logic, for now, should be: - - `Self::deposit_event` with `Event::::Sold` and the appropriate params. - - Return `Ok(())`. - */ - } } diff --git a/src/29/template/template.diff b/src/29/template/template.diff index 02f7e618..3cb359a7 100644 --- a/src/29/template/template.diff +++ b/src/29/template/template.diff @@ -1,8 +1,28 @@ +diff --git a/src/impls.rs b/src/impls.rs +index d53238c..d2b2015 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -72,4 +72,15 @@ impl Pallet { + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } ++ ++ /* Create a new internal function `do_buy_kitty`: ++ - Inputs to the function are: ++ - `buyer` which is `T::AccountId`. ++ - `kitty_id` which is `[u8; 16]`. ++ - `price` which is `BalanceOf`. ++ - It returns `DispatchResult`. ++ - The internal logic, for now, should be: ++ - `Self::deposit_event` with `Event::::Sold` and the appropriate params. ++ - Return `Ok(())`. ++ */ + } diff --git a/src/lib.rs b/src/lib.rs -index 0ac3bab..b84ac0d 100644 +index 52c6f53..5072ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -62,6 +62,11 @@ pub mod pallet { +@@ -64,6 +64,11 @@ pub mod pallet { Created { owner: T::AccountId }, Transferred { from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16] }, PriceSet { owner: T::AccountId, kitty_id: [u8; 16], new_price: Option> }, @@ -14,7 +34,7 @@ index 0ac3bab..b84ac0d 100644 } #[pallet::error] -@@ -104,6 +109,18 @@ pub mod pallet { +@@ -106,5 +111,17 @@ pub mod pallet { Self::do_set_price(who, kitty_id, new_price)?; Ok(()) } @@ -29,24 +49,6 @@ index 0ac3bab..b84ac0d 100644 + - Extract `who` using `ensure_signed` on `origin`. + - Call `Self::do_buy_kitty` using appropriate params, and propagating the result. + - Return `Ok(())`. -+ */ - } - - // Learn about internal functions. -@@ -181,5 +198,16 @@ pub mod pallet { - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } -+ -+ /* Create a new internal function `do_buy_kitty`: -+ - Inputs to the function are: -+ - `buyer` which is `T::AccountId`. -+ - `kitty_id` which is `[u8; 16]`. -+ - `price` which is `BalanceOf`. -+ - It returns `DispatchResult`. -+ - The internal logic, for now, should be: -+ - `Self::deposit_event` with `Event::::Sold` and the appropriate params. -+ - Return `Ok(())`. + */ } } diff --git a/src/3/source/changes.diff b/src/3/source/changes.diff index 7be53d72..c7705fcc 100644 --- a/src/3/source/changes.diff +++ b/src/3/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 44cc2ec..63d0b59 100644 +index 4d56aa1..a6155ed 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -9,6 +9,8 @@ pub mod pallet { +@@ -11,6 +11,8 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; diff --git a/src/3/source/src/impls.rs b/src/3/source/src/impls.rs new file mode 100644 index 00000000..d49f793b --- /dev/null +++ b/src/3/source/src/impls.rs @@ -0,0 +1,9 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/3/source/src/lib.rs b/src/3/source/src/lib.rs index 63d0b59f..a6155ed8 100644 --- a/src/3/source/src/lib.rs +++ b/src/3/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -36,11 +38,4 @@ pub mod pallet { Ok(()) } } - - impl Pallet { - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/30/README.md b/src/30/README.md index 061af0d2..cead1dba 100644 --- a/src/30/README.md +++ b/src/30/README.md @@ -17,9 +17,18 @@
- + +
-
+
+ +```rust +{{#include ./template/src/impls.rs}} +``` + +
+ +
```rust {{#include ./template/src/lib.rs}} @@ -34,9 +43,18 @@
- + +
-
+
+ +```rust +{{#include ./solution/src/impls.rs}} +``` + +
+ +
```rust {{#include ./solution/src/lib.rs}} diff --git a/src/30/solution/solution.diff b/src/30/solution/solution.diff index 526658d3..83d64138 100644 --- a/src/30/solution/solution.diff +++ b/src/30/solution/solution.diff @@ -1,8 +1,50 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 415b4e7..4770d83 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -1,5 +1,5 @@ + use super::*; +-use frame_support::pallet_prelude::*; ++use frame_support::{pallet_prelude::*, traits::fungible::Mutate}; + + // Learn about internal functions. + impl Pallet { +@@ -78,23 +78,15 @@ impl Pallet { + kitty_id: [u8; 16], + price: BalanceOf, + ) -> DispatchResult { +- /* TODO: Sanity check that the purchase is allowed: +- - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. +- - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. +- - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. +- */ ++ let kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; ++ let real_price = kitty.price.ok_or(Error::::NotForSale)?; ++ ensure!(price >= real_price, Error::::MaxPriceTooLow); + +- /* TODO: Execute the transfers: +- - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. +- - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. +- - The amount transferred should be the `real_price`. +- - Use `Preservation::Preserve` to ensure the buyer account stays alive. +- - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. +- - Remember to propagate up all results from these functions with `?`. +- */ ++ use frame_support::traits::tokens::Preservation; ++ T::NativeBalance::transfer(&buyer, &kitty.owner, real_price, Preservation::Preserve)?; ++ Self::do_transfer(kitty.owner, buyer.clone(), kitty_id)?; + +- /* TODO: Update the event to use the `real_price` in the `Event`. */ +- Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); ++ Self::deposit_event(Event::::Sold { buyer, kitty_id, price: real_price }); + Ok(()) + } + } diff --git a/src/lib.rs b/src/lib.rs -index 035a4de..6ec31f6 100644 +index 32d98e9..4a404e2 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -73,10 +73,8 @@ pub mod pallet { +@@ -75,10 +75,8 @@ pub mod pallet { TransferToSelf, NoKitty, NotOwner, @@ -15,36 +57,3 @@ index 035a4de..6ec31f6 100644 } // Learn about callable functions and dispatch. -@@ -202,23 +200,15 @@ pub mod pallet { - kitty_id: [u8; 16], - price: BalanceOf, - ) -> DispatchResult { -- /* TODO: Sanity check that the purchase is allowed: -- - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. -- - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. -- - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. -- */ -- -- /* TODO: Execute the transfers: -- - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. -- - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. -- - The amount transferred should be the `real_price`. -- - Use `Preservation::Preserve` to ensure the buyer account stays alive. -- - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. -- - Remember to propagate up all results from these functions with `?`. -- */ -- -- /* TODO: Update the event to use the `real_price` in the `Event`. */ -- Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); -+ let kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; -+ let real_price = kitty.price.ok_or(Error::::NotForSale)?; -+ ensure!(price >= real_price, Error::::MaxPriceTooLow); -+ -+ use frame_support::traits::tokens::Preservation; -+ T::NativeBalance::transfer(&buyer, &kitty.owner, real_price, Preservation::Preserve)?; -+ Self::do_transfer(kitty.owner, buyer.clone(), kitty_id)?; -+ -+ Self::deposit_event(Event::::Sold { buyer, kitty_id, price: real_price }); - Ok(()) - } - } diff --git a/src/30/solution/src/impls.rs b/src/30/solution/src/impls.rs new file mode 100644 index 00000000..4770d83b --- /dev/null +++ b/src/30/solution/src/impls.rs @@ -0,0 +1,92 @@ +use super::*; +use frame_support::{pallet_prelude::*, traits::fungible::Mutate}; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == caller, Error::::NotOwner); + kitty.price = new_price; + Kitties::::insert(kitty_id, kitty); + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } + + pub fn do_buy_kitty( + buyer: T::AccountId, + kitty_id: [u8; 16], + price: BalanceOf, + ) -> DispatchResult { + let kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + let real_price = kitty.price.ok_or(Error::::NotForSale)?; + ensure!(price >= real_price, Error::::MaxPriceTooLow); + + use frame_support::traits::tokens::Preservation; + T::NativeBalance::transfer(&buyer, &kitty.owner, real_price, Preservation::Preserve)?; + Self::do_transfer(kitty.owner, buyer.clone(), kitty_id)?; + + Self::deposit_event(Event::::Sold { buyer, kitty_id, price: real_price }); + Ok(()) + } +} diff --git a/src/30/solution/src/lib.rs b/src/30/solution/src/lib.rs index 6ec31f66..4a404e26 100644 --- a/src/30/solution/src/lib.rs +++ b/src/30/solution/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -118,98 +120,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == caller, Error::::NotOwner); - kitty.price = new_price; - Kitties::::insert(kitty_id, kitty); - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - - pub fn do_buy_kitty( - buyer: T::AccountId, - kitty_id: [u8; 16], - price: BalanceOf, - ) -> DispatchResult { - let kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - let real_price = kitty.price.ok_or(Error::::NotForSale)?; - ensure!(price >= real_price, Error::::MaxPriceTooLow); - - use frame_support::traits::tokens::Preservation; - T::NativeBalance::transfer(&buyer, &kitty.owner, real_price, Preservation::Preserve)?; - Self::do_transfer(kitty.owner, buyer.clone(), kitty_id)?; - - Self::deposit_event(Event::::Sold { buyer, kitty_id, price: real_price }); - Ok(()) - } - } } diff --git a/src/30/template/src/impls.rs b/src/30/template/src/impls.rs new file mode 100644 index 00000000..415b4e7b --- /dev/null +++ b/src/30/template/src/impls.rs @@ -0,0 +1,100 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + // Generates and returns DNA and Sex + pub fn gen_dna() -> [u8; 16] { + // Create randomness payload. Multiple kitties can be generated in the same block, + // retaining uniqueness. + let unique_payload = ( + frame_system::Pallet::::parent_hash(), + frame_system::Pallet::::block_number(), + frame_system::Pallet::::extrinsic_index(), + CountForKitties::::get(), + ); + + let encoded_payload = unique_payload.encode(); + frame_support::Hashable::blake2_128(&encoded_payload) + } + + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { + let kitty = Kitty { dna, owner: owner.clone(), price: None }; + // Check if the kitty does not already exist in our storage map + ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); + + let current_count: u64 = CountForKitties::::get(); + let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; + + KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; + Kitties::::insert(dna, kitty); + CountForKitties::::set(new_count); + + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } + + // Update storage to transfer kitty + pub fn do_transfer(from: T::AccountId, to: T::AccountId, kitty_id: [u8; 16]) -> DispatchResult { + ensure!(from != to, Error::::TransferToSelf); + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == from, Error::::NotOwner); + kitty.owner = to.clone(); + + let mut to_owned = KittiesOwned::::get(&to); + to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; + let mut from_owned = KittiesOwned::::get(&from); + if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { + from_owned.swap_remove(ind); + } else { + return Err(Error::::NoKitty.into()) + } + + Kitties::::insert(kitty_id, kitty); + KittiesOwned::::insert(&to, to_owned); + KittiesOwned::::insert(&from, from_owned); + + Self::deposit_event(Event::::Transferred { from, to, kitty_id }); + Ok(()) + } + + pub fn do_set_price( + caller: T::AccountId, + kitty_id: [u8; 16], + new_price: Option>, + ) -> DispatchResult { + let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; + ensure!(kitty.owner == caller, Error::::NotOwner); + kitty.price = new_price; + Kitties::::insert(kitty_id, kitty); + + Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); + Ok(()) + } + + pub fn do_buy_kitty( + buyer: T::AccountId, + kitty_id: [u8; 16], + price: BalanceOf, + ) -> DispatchResult { + /* TODO: Sanity check that the purchase is allowed: + - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. + - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. + - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. + */ + + /* TODO: Execute the transfers: + - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. + - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. + - The amount transferred should be the `real_price`. + - Use `Preservation::Preserve` to ensure the buyer account stays alive. + - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. + - Remember to propagate up all results from these functions with `?`. + */ + + /* TODO: Update the event to use the `real_price` in the `Event`. */ + Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); + Ok(()) + } +} diff --git a/src/30/template/src/lib.rs b/src/30/template/src/lib.rs index 035a4de2..32d98e9c 100644 --- a/src/30/template/src/lib.rs +++ b/src/30/template/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -120,106 +122,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Generates and returns DNA and Sex - fn gen_dna() -> [u8; 16] { - // Create randomness payload. Multiple kitties can be generated in the same block, - // retaining uniqueness. - let unique_payload = ( - frame_system::Pallet::::parent_hash(), - frame_system::Pallet::::block_number(), - frame_system::Pallet::::extrinsic_index(), - CountForKitties::::get(), - ); - - let encoded_payload = unique_payload.encode(); - frame_support::Hashable::blake2_128(&encoded_payload) - } - - // Learn about `AccountId`. - fn mint(owner: T::AccountId, dna: [u8; 16]) -> DispatchResult { - let kitty = Kitty { dna, owner: owner.clone(), price: None }; - // Check if the kitty does not already exist in our storage map - ensure!(!Kitties::::contains_key(dna), Error::::DuplicateKitty); - - let current_count: u64 = CountForKitties::::get(); - let new_count = current_count.checked_add(1).ok_or(Error::::TooManyKitties)?; - - KittiesOwned::::try_append(&owner, dna).map_err(|_| Error::::TooManyOwned)?; - Kitties::::insert(dna, kitty); - CountForKitties::::set(new_count); - - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - - // Update storage to transfer kitty - pub fn do_transfer( - from: T::AccountId, - to: T::AccountId, - kitty_id: [u8; 16], - ) -> DispatchResult { - ensure!(from != to, Error::::TransferToSelf); - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == from, Error::::NotOwner); - kitty.owner = to.clone(); - - let mut to_owned = KittiesOwned::::get(&to); - to_owned.try_push(kitty_id).map_err(|_| Error::::TooManyOwned)?; - let mut from_owned = KittiesOwned::::get(&from); - if let Some(ind) = from_owned.iter().position(|&id| id == kitty_id) { - from_owned.swap_remove(ind); - } else { - return Err(Error::::NoKitty.into()) - } - - Kitties::::insert(kitty_id, kitty); - KittiesOwned::::insert(&to, to_owned); - KittiesOwned::::insert(&from, from_owned); - - Self::deposit_event(Event::::Transferred { from, to, kitty_id }); - Ok(()) - } - - pub fn do_set_price( - caller: T::AccountId, - kitty_id: [u8; 16], - new_price: Option>, - ) -> DispatchResult { - let mut kitty = Kitties::::get(kitty_id).ok_or(Error::::NoKitty)?; - ensure!(kitty.owner == caller, Error::::NotOwner); - kitty.price = new_price; - Kitties::::insert(kitty_id, kitty); - - Self::deposit_event(Event::::PriceSet { owner: caller, kitty_id, new_price }); - Ok(()) - } - - pub fn do_buy_kitty( - buyer: T::AccountId, - kitty_id: [u8; 16], - price: BalanceOf, - ) -> DispatchResult { - /* TODO: Sanity check that the purchase is allowed: - - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. - - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. - - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. - */ - - /* TODO: Execute the transfers: - - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. - - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. - - The amount transferred should be the `real_price`. - - Use `Preservation::Preserve` to ensure the buyer account stays alive. - - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. - - Remember to propagate up all results from these functions with `?`. - */ - - /* TODO: Update the event to use the `real_price` in the `Event`. */ - Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); - Ok(()) - } - } } diff --git a/src/30/template/template.diff b/src/30/template/template.diff index ce03f518..df198d05 100644 --- a/src/30/template/template.diff +++ b/src/30/template/template.diff @@ -1,8 +1,35 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 0f425a0..415b4e7 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -78,6 +78,22 @@ impl Pallet { + kitty_id: [u8; 16], + price: BalanceOf, + ) -> DispatchResult { ++ /* TODO: Sanity check that the purchase is allowed: ++ - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. ++ - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. ++ - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. ++ */ ++ ++ /* TODO: Execute the transfers: ++ - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. ++ - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. ++ - The amount transferred should be the `real_price`. ++ - Use `Preservation::Preserve` to ensure the buyer account stays alive. ++ - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. ++ - Remember to propagate up all results from these functions with `?`. ++ */ ++ ++ /* TODO: Update the event to use the `real_price` in the `Event`. */ + Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); + Ok(()) + } diff --git a/src/lib.rs b/src/lib.rs -index 7c54bc8..035a4de 100644 +index d6c59f0..32d98e9 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -73,6 +73,10 @@ pub mod pallet { +@@ -75,6 +75,10 @@ pub mod pallet { TransferToSelf, NoKitty, NotOwner, @@ -13,26 +40,3 @@ index 7c54bc8..035a4de 100644 } // Learn about callable functions and dispatch. -@@ -198,6 +202,22 @@ pub mod pallet { - kitty_id: [u8; 16], - price: BalanceOf, - ) -> DispatchResult { -+ /* TODO: Sanity check that the purchase is allowed: -+ - Get `kitty` from `Kitties` using `kitty_id`, `ok_or` return `Error::::NoKitty`. -+ - Get the `real_price` from `kitty.price`, `ok_or` return `Error::::NotForSale`. -+ - `ensure!` that `price` is greater or equal to `real_price`, else `Error::::MaxPriceTooLow`. -+ */ -+ -+ /* TODO: Execute the transfers: -+ - Import `use frame_support::traits::tokens::Preservation;`, which is used for balance transfer. -+ - Use `T::NativeBalance` to `transfer` from the `buyer` to the `kitty.owner`. -+ - The amount transferred should be the `real_price`. -+ - Use `Preservation::Preserve` to ensure the buyer account stays alive. -+ - Use `Self::do_transfer` to transfer from the `kitty.owner` to the `buyer` with `kitty_id`. -+ - Remember to propagate up all results from these functions with `?`. -+ */ -+ -+ /* TODO: Update the event to use the `real_price` in the `Event`. */ - Self::deposit_event(Event::::Sold { buyer, kitty_id, price }); - Ok(()) - } diff --git a/src/4/README.md b/src/4/README.md index 4c64326e..24d28895 100644 --- a/src/4/README.md +++ b/src/4/README.md @@ -15,9 +15,18 @@
- + +
-
+
+ +```rust +{{#include ./source/src/impls.rs}} +``` + +
+ +
```rust {{#include ./source/src/lib.rs}} diff --git a/src/4/source/changes.diff b/src/4/source/changes.diff index 6dc4009d..2207df67 100644 --- a/src/4/source/changes.diff +++ b/src/4/source/changes.diff @@ -1,8 +1,20 @@ +diff --git a/src/impls.rs b/src/impls.rs +index d49f793..49906c6 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -1,6 +1,7 @@ + use super::*; + use frame_support::pallet_prelude::*; + ++// Learn about internal functions. + impl Pallet { + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); diff --git a/src/lib.rs b/src/lib.rs -index 63d0b59..d4093e0 100644 +index a6155ed..613e92b 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -28,6 +28,7 @@ pub mod pallet { +@@ -30,6 +30,7 @@ pub mod pallet { #[pallet::error] pub enum Error {} @@ -10,11 +22,3 @@ index 63d0b59..d4093e0 100644 #[pallet::call] impl Pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { -@@ -37,6 +38,7 @@ pub mod pallet { - } - } - -+ // Learn about internal functions. - impl Pallet { - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); diff --git a/src/4/source/src/impls.rs b/src/4/source/src/impls.rs new file mode 100644 index 00000000..49906c6a --- /dev/null +++ b/src/4/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +// Learn about internal functions. +impl Pallet { + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/4/source/src/lib.rs b/src/4/source/src/lib.rs index d4093e02..613e92b2 100644 --- a/src/4/source/src/lib.rs +++ b/src/4/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -37,12 +39,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/5/README.md b/src/5/README.md index 4c64326e..24d28895 100644 --- a/src/5/README.md +++ b/src/5/README.md @@ -15,9 +15,18 @@
- + +
-
+
+ +```rust +{{#include ./source/src/impls.rs}} +``` + +
+ +
```rust {{#include ./source/src/lib.rs}} diff --git a/src/5/source/changes.diff b/src/5/source/changes.diff index db79ae8d..4e28330d 100644 --- a/src/5/source/changes.diff +++ b/src/5/source/changes.diff @@ -1,8 +1,22 @@ +diff --git a/src/impls.rs b/src/impls.rs +index 49906c6..869c781 100644 +--- a/src/impls.rs ++++ b/src/impls.rs +@@ -1,8 +1,8 @@ + use super::*; + use frame_support::pallet_prelude::*; + +-// Learn about internal functions. + impl Pallet { ++ // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) diff --git a/src/lib.rs b/src/lib.rs -index d4093e0..56fe9dc 100644 +index 613e92b..a0865fc 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -32,6 +32,7 @@ pub mod pallet { +@@ -34,6 +34,7 @@ pub mod pallet { #[pallet::call] impl Pallet { pub fn create_kitty(origin: OriginFor) -> DispatchResult { @@ -10,11 +24,3 @@ index d4093e0..56fe9dc 100644 let who = ensure_signed(origin)?; Self::mint(who)?; Ok(()) -@@ -40,6 +41,7 @@ pub mod pallet { - - // Learn about internal functions. - impl Pallet { -+ // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) diff --git a/src/5/source/src/impls.rs b/src/5/source/src/impls.rs new file mode 100644 index 00000000..869c781c --- /dev/null +++ b/src/5/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/5/source/src/lib.rs b/src/5/source/src/lib.rs index 56fe9dce..a0865fc6 100644 --- a/src/5/source/src/lib.rs +++ b/src/5/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -38,13 +40,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/6/source/changes.diff b/src/6/source/changes.diff index 9f6dbcb0..e4ed8d8c 100644 --- a/src/6/source/changes.diff +++ b/src/6/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 56fe9dc..12bb0aa 100644 +index a0865fc..bde6e1c 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -14,6 +14,7 @@ pub mod pallet { +@@ -16,6 +16,7 @@ pub mod pallet { #[pallet::pallet] pub struct Pallet(_); diff --git a/src/6/source/src/impls.rs b/src/6/source/src/impls.rs new file mode 100644 index 00000000..869c781c --- /dev/null +++ b/src/6/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/6/source/src/lib.rs b/src/6/source/src/lib.rs index 12bb0aaf..bde6e1cc 100644 --- a/src/6/source/src/lib.rs +++ b/src/6/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -39,13 +41,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/7/source/changes.diff b/src/7/source/changes.diff index 0aa8f733..154e7505 100644 --- a/src/7/source/changes.diff +++ b/src/7/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 12bb0aa..413d919 100644 +index bde6e1c..330d3e6 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,6 +20,7 @@ pub mod pallet { +@@ -22,6 +22,7 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } diff --git a/src/7/source/src/impls.rs b/src/7/source/src/impls.rs new file mode 100644 index 00000000..869c781c --- /dev/null +++ b/src/7/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/7/source/src/lib.rs b/src/7/source/src/lib.rs index 413d9190..330d3e62 100644 --- a/src/7/source/src/lib.rs +++ b/src/7/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -40,13 +42,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/8/source/src/impls.rs b/src/8/source/src/impls.rs new file mode 100644 index 00000000..869c781c --- /dev/null +++ b/src/8/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/8/source/src/lib.rs b/src/8/source/src/lib.rs index 413d9190..330d3e62 100644 --- a/src/8/source/src/lib.rs +++ b/src/8/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -40,13 +42,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } } diff --git a/src/9/source/changes.diff b/src/9/source/changes.diff index d60f5913..ff4ffe61 100644 --- a/src/9/source/changes.diff +++ b/src/9/source/changes.diff @@ -1,8 +1,8 @@ diff --git a/src/lib.rs b/src/lib.rs -index 413d919..67dd167 100644 +index 330d3e6..acea2e2 100644 --- a/src/lib.rs +++ b/src/lib.rs -@@ -20,6 +20,10 @@ pub mod pallet { +@@ -22,6 +22,10 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; } diff --git a/src/9/source/src/impls.rs b/src/9/source/src/impls.rs new file mode 100644 index 00000000..869c781c --- /dev/null +++ b/src/9/source/src/impls.rs @@ -0,0 +1,10 @@ +use super::*; +use frame_support::pallet_prelude::*; + +impl Pallet { + // Learn about `AccountId`. + pub fn mint(owner: T::AccountId) -> DispatchResult { + Self::deposit_event(Event::::Created { owner }); + Ok(()) + } +} diff --git a/src/9/source/src/lib.rs b/src/9/source/src/lib.rs index 67dd167e..acea2e23 100644 --- a/src/9/source/src/lib.rs +++ b/src/9/source/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +mod impls; + pub use pallet::*; // Learn about Macros used in the `polkadot-sdk`, making pallet development easier. @@ -44,13 +46,4 @@ pub mod pallet { Ok(()) } } - - // Learn about internal functions. - impl Pallet { - // Learn about `AccountId`. - fn mint(owner: T::AccountId) -> DispatchResult { - Self::deposit_event(Event::::Created { owner }); - Ok(()) - } - } }