Skip to content

Commit

Permalink
mdBook generated from gitorial
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Jul 22, 2024
1 parent 31d96ed commit c0f3e05
Show file tree
Hide file tree
Showing 188 changed files with 14,380 additions and 1,904 deletions.
35 changes: 16 additions & 19 deletions src/10/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
diff --git a/src/lib.rs b/src/lib.rs
index 3b445c2..446ce3c 100644
index 924f26f..2ed0e74 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,8 +22,7 @@ pub mod pallet {
@@ -33,9 +33,7 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
- /* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
- pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
+ pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;
#[pallet::error]
pub enum Error<T> {
- /* TODO:
- - Introduce a new error `TooManyKitties`.
- */
+ TooManyKitties,
}

// Learn about events.
#[pallet::event]
@@ -52,11 +51,9 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// Learn about callable functions and dispatch.
@@ -54,8 +52,7 @@ pub mod 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::<T>::get().unwrap_or(0);
+ let current_count: u64 = CountForKitties::<T>::get();
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
- /* TODO: Remove the `Option` wrapper when setting the `new_count`. */
- CountForKitties::<T>::set(Some(new_count));
+ CountForKitties::<T>::set(new_count);
let current_count: u64 = CountForKitties::<T>::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::<T>::TooManyKitties)?;
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
6 changes: 3 additions & 3 deletions src/10/solution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;

// Learn about events.
#[pallet::event]
Expand Down Expand Up @@ -51,9 +51,9 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// Learn about `AccountId`.
fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get();
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
CountForKitties::<T>::set(new_count);
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
Expand Down
7 changes: 2 additions & 5 deletions src/10/template/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Value Query
# Safe Math

- TODO:
- Explain storage as an option
- Explain `OptionQuery` and `ValueQuery`
- Explain default value
- Explain safe math, overflows, saturating, checked, and APIs
10 changes: 5 additions & 5 deletions src/10/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
/* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;

// Learn about events.
Expand All @@ -34,7 +33,9 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
TooManyKitties,
/* TODO:
- Introduce a new error `TooManyKitties`.
*/
}

// Learn about callable functions and dispatch.
Expand All @@ -52,10 +53,9 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// 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::<T>::get().unwrap_or(0);
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
/* TODO: Remove the `Option` wrapper when setting the `new_count`. */
/* TODO: Update this logic to use safe math. */
let new_count = current_count + 1;
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
Expand Down
28 changes: 15 additions & 13 deletions src/10/template/template.diff
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
diff --git a/src/lib.rs b/src/lib.rs
index 2ed0e74..3b445c2 100644
index 59a65fe..924f26f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,7 @@ pub mod pallet {
@@ -32,7 +32,11 @@ pub mod pallet {
}

/// Learn about storage value.
#[pallet::storage]
+ /* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
#[pallet::error]
- pub enum Error<T> {}
+ pub enum Error<T> {
+ /* TODO:
+ - Introduce a new error `TooManyKitties`.
+ */
+ }

// Learn about events.
@@ -51,8 +52,10 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// Learn about callable functions and dispatch.
#[pallet::call]
@@ -50,6 +54,7 @@ pub mod 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::<T>::get().unwrap_or(0);
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
+ /* TODO: Remove the `Option` wrapper when setting the `new_count`. */
+ /* TODO: Update this logic to use safe math. */
let new_count = current_count + 1;
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
45 changes: 36 additions & 9 deletions src/11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,44 @@
<div class="content-row">
<div class="content-col">

{{#include ./source/README.md}}
{{#include ./template/README.md}}

</div>

<div class="content-col">

<div class="tab">
<button class="maintab tablinks active" onclick="switchMainTab(event, 'Source')">Source</button>
<button class="maintab tablinks active" onclick="switchMainTab(event, 'Template')">Template</button>
<button class="maintab tablinks" onclick="switchMainTab(event, 'Solution')">Solution</button>
<button class="maintab tablinks" onclick="switchMainTab(event, 'Diff')">Diff</button>
</div>

<div id="Source" class="maintab tabcontent active">
<div id="Template" class="maintab tabcontent active">

<div class="tab">
<button class="subtab tablinks file-source file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
<button class="subtab tablinks file-template file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="source/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">
<div id="template/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./source/src/lib.rs}}
{{#include ./template/src/lib.rs}}
```

</div>



</div>

<div id="Solution" class="maintab tabcontent">

<div class="tab">
<button class="subtab tablinks file-solution file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="solution/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./solution/src/lib.rs}}
```

</div>
Expand All @@ -33,12 +52,20 @@


<div class="tab">
<button class="difftab tablinks active" onclick="switchDiff(event, 'changes.diff')" data-id="changes.diff">changes.diff</button>
<button class="difftab tablinks active" onclick="switchDiff(event, 'template.diff')" data-id="template.diff">template.diff</button>
<button class="difftab tablinks" onclick="switchDiff(event, 'solution.diff')" data-id="solution.diff">solution.diff</button>
</div>
<div id="template.diff" class="difftab tabcontent active" data-id="template.diff">

```diff
{{#include ./template/template.diff}}
```

</div>
<div id="changes.diff" class="difftab tabcontent active" data-id="changes.diff">
<div id="solution.diff" class="difftab tabcontent" data-id="solution.diff">

```diff
{{#include ./source/changes.diff}}
{{#include ./solution/solution.diff}}
```

</div>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions src/11/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
diff --git a/src/lib.rs b/src/lib.rs
index 3b445c2..446ce3c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,8 +22,7 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
- /* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
- pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
+ pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;

// Learn about events.
#[pallet::event]
@@ -52,11 +51,9 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// 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::<T>::get().unwrap_or(0);
+ let current_count: u64 = CountForKitties::<T>::get();
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
- /* TODO: Remove the `Option` wrapper when setting the `new_count`. */
- CountForKitties::<T>::set(Some(new_count));
+ CountForKitties::<T>::set(new_count);
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
15 changes: 7 additions & 8 deletions src/8/template/src/lib.rs → src/11/solution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;

// Learn about events.
#[pallet::event]
Expand All @@ -32,7 +32,9 @@ pub mod pallet {
}

#[pallet::error]
pub enum Error<T> {}
pub enum Error<T> {
TooManyKitties,
}

// Learn about callable functions and dispatch.
#[pallet::call]
Expand All @@ -49,12 +51,9 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// 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::<T>::get();
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
CountForKitties::<T>::set(new_count);
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/11/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Value Query

- TODO:
- Explain storage as an option
- Explain `OptionQuery` and `ValueQuery`
- Explain default value
17 changes: 6 additions & 11 deletions src/12/template/src/lib.rs → src/11/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;

/// Learn about storage maps.
#[pallet::storage]
pub(super) type Kitties<T: Config> = StorageMap<Key = [u8; 16], Value = ()>;
/* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;

// Learn about events.
#[pallet::event]
Expand All @@ -46,8 +43,6 @@ pub mod pallet {
pub fn create_kitty(origin: OriginFor<T>) -> DispatchResult {
// Learn about `origin`.
let who = ensure_signed(origin)?;
/* TODO: Create `const default_id`, which type `[u8; 16]` and has value `[0u8; 16]`. */
/* TODO: Pass `default_id` to the `mint` function. */
Self::mint(who)?;
Ok(())
}
Expand All @@ -56,12 +51,12 @@ pub mod pallet {
// Learn about internal functions.
impl<T: Config> Pallet<T> {
// 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::<T>::get();
/* TODO: Remove the `unwrap_or` which is not needed when using `ValueQuery`. */
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
/* TODO: In the `Kitties` map, under the key `id`, insert `()`. */
CountForKitties::<T>::set(new_count);
/* TODO: Remove the `Option` wrapper when setting the `new_count`. */
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
Expand Down
23 changes: 23 additions & 0 deletions src/11/template/template.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/src/lib.rs b/src/lib.rs
index 2ed0e74..3b445c2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,7 @@ pub mod pallet {

/// Learn about storage value.
#[pallet::storage]
+ /* TODO: Update this storage to use a `QueryKind` of `ValueQuery`. */
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;

// Learn about events.
@@ -51,8 +52,10 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
// 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::<T>::get().unwrap_or(0);
let new_count = current_count.checked_add(1).ok_or(Error::<T>::TooManyKitties)?;
+ /* TODO: Remove the `Option` wrapper when setting the `new_count`. */
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
Loading

0 comments on commit c0f3e05

Please sign in to comment.