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 Aug 4, 2024
1 parent 6807cc7 commit c70cec7
Show file tree
Hide file tree
Showing 130 changed files with 4,049 additions and 225 deletions.
9 changes: 0 additions & 9 deletions src/10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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

Expand All @@ -28,14 +27,6 @@

</div>

<div id="template/src/lib.rs" class="subtab tabcontent" data-id="src/lib.rs">

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

</div>



</div>
Expand Down
10 changes: 5 additions & 5 deletions src/10/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
diff --git a/src/impls.rs b/src/impls.rs
index ef77d84..a35e365 100644
index b396f98..9739330 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -3,12 +3,9 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
- /* 🚧 TODO 🚧:
- - `get` the current count of kitties.
- - `get` the `current_count` of kitties.
- - `unwrap_or` set the count to `0`.
- - increment the count by one.
- - `set` the new count of kitties.
- - Create `new_count` by adding one to the `current_count`.
- - `set` the `new_count` of kitties.
- */
+ let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
+ let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
+ let new_count = current_count + 1;
+ CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Expand Down
2 changes: 1 addition & 1 deletion src/10/solution/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
let new_count = current_count + 1;
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Expand Down
2 changes: 1 addition & 1 deletion src/10/solution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pallet {
}

#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
8 changes: 4 additions & 4 deletions src/10/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Check out the [`StorageValue` documentation](https://docs.rs/frame-support/37.0.
To read the current value of a `StorageValue`, you can simply call the `get` API:

```rust
let maybe_count: Option<u64> = CountForKitties::<T>::get();
let maybe_count: Option<u32> = CountForKitties::<T>::get();
```

A few things to note here.
Expand All @@ -27,10 +27,10 @@ In this context, when there is no value in storage for the `CountForKitties`, we
So we can write the following to handle this ergonomically:

```rust
let count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
```

Now, whenever `CountForKitties` returns `Some(count)`, we will simply unwrap that count and directly access the `u64`. If it returns `None`, we will simply return `0u64` instead.
Now, whenever `CountForKitties` returns `Some(count)`, we will simply unwrap that count and directly access the `u32`. If it returns `None`, we will simply return `0u32` instead.

The other thing to note is the generic `<T>` that we need to include. You better get used to this, we will be using `<T>` everywhere! But remember, in our definition of `CountForKitties`, it was a type generic over `<T: Config>`, and thus we need to include `<T>` to access any of the APIs.

Expand All @@ -39,7 +39,7 @@ The other thing to note is the generic `<T>` that we need to include. You better
To set the current value of a `StorageValue`, you can simply call the `set` API:

```rust
CountForKitties::<T>::set(Some(1u64));
CountForKitties::<T>::set(Some(1u32));
```

This storage API cannot fail, so there is no error handling needed. You just set the value directly in storage. Note that `set` will also happily replace any existing value there, so you will need to use other APIs like `exists` or `get` to check if a value is already in storage.
Expand Down
6 changes: 3 additions & 3 deletions src/10/template/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use frame::prelude::*;
impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
/* 🚧 TODO 🚧:
- `get` the current count of kitties.
- `get` the `current_count` of kitties.
- `unwrap_or` set the count to `0`.
- increment the count by one.
- `set` the new count of kitties.
- Create `new_count` by adding one to the `current_count`.
- `set` the `new_count` of kitties.
*/
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/10/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pallet {
}

#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
20 changes: 4 additions & 16 deletions src/10/template/template.diff
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
diff --git a/src/impls.rs b/src/impls.rs
index 03abf99..ef77d84 100644
index 03abf99..b396f98 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -3,6 +3,12 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
+ /* 🚧 TODO 🚧:
+ - `get` the current count of kitties.
+ - `get` the `current_count` of kitties.
+ - `unwrap_or` set the count to `0`.
+ - increment the count by one.
+ - `set` the new count of kitties.
+ - Create `new_count` by adding one to the `current_count`.
+ - `set` the `new_count` of kitties.
+ */
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
diff --git a/src/lib.rs b/src/lib.rs
index 2b03703..b6c66a4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -17,7 +17,6 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

- /* 🚧 TODO 🚧: Learn about storage value. */
#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;

6 changes: 3 additions & 3 deletions src/11/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
diff --git a/src/impls.rs b/src/impls.rs
index 95d4a77..43c7159 100644
index c550bc8..7277e36 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -4,8 +4,7 @@ use frame::prelude::*;
impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = 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(())
diff --git a/src/lib.rs b/src/lib.rs
index e119c87..2c2504c 100644
index e68500d..a913997 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -28,9 +28,7 @@ pub mod pallet {
Expand Down
2 changes: 1 addition & 1 deletion src/11/solution/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
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 });
Expand Down
2 changes: 1 addition & 1 deletion src/11/solution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pallet {
}

#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
18 changes: 9 additions & 9 deletions src/11/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ We will show you the common ergonomic ways to use Pallet Errors going forward.

The basic math operators in Rust are **unsafe**.

Imagine our `CountForKitties` was already at the limit of `u64::MAX`. What would happen if we tried to call `mint` one more time?
Imagine our `CountForKitties` was already at the limit of `u32::MAX`. What would happen if we tried to call `mint` one more time?

We would get an overflow!

In tests `u64::MAX + 1` will actually trigger a panic, but in a `release` build, this overflow will happen silently...
In tests `u32::MAX + 1` will actually trigger a panic, but in a `release` build, this overflow will happen silently...

And this would be really bad. Now our count would be back to 0, and if we had any logic which depended on this count being accurate, that logic would be broken.

Expand All @@ -89,18 +89,18 @@ The checked math APIs will check if there are any underflows or overflows, and r
Here is a verbose way you could handle checked math in a Pallet:

```rust
let final_result: u64 = match value_a.checked_add(value_b) {
let final_result: u32 = match value_a.checked_add(value_b) {
Some(result) => result,
None => return Err(Error::<T>::CustomPalletError.into()),
};
```

You can see how we can directly assign the `u64` value to `final_result`, otherwise it will return an error.
You can see how we can directly assign the `u32` value to `final_result`, otherwise it will return an error.

We can also do this as a one-liner, which is more ergonomic and preferred:

```rust
let final_result: u64 = value_a.checked_add(value_b).ok_or(Error::<T>::CustomPalletError)?;
let final_result: u32 = value_a.checked_add(value_b).ok_or(Error::<T>::CustomPalletError)?;
```

This is exactly how you should be writing all the safe math inside your Pallet.
Expand All @@ -116,10 +116,10 @@ This option is useful because it is safe and does NOT return an `Option`.
Instead, it performs the math operations and keeps the value at the numerical limits, rather than overflowing. For example:

```rust
let value_a: u64 = 1;
let value_b: u64 = u64::MAX;
let result: u64 = value_a.saturating_add(value_b);
assert!(result == u64::MAX);
let value_a: u32 = 1;
let value_b: u32 = u32::MAX;
let result: u32 = value_a.saturating_add(value_b);
assert!(result == u32::MAX);
```

This generally is NOT the preferred API to use because usually you want to handle situations where an overflow would occur. Overflows and underflows usually indicate something "bad" is happening.
Expand Down
2 changes: 1 addition & 1 deletion src/11/template/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
/* 🚧 TODO 🚧: Update this logic to use safe math. */
let new_count = current_count + 1;
CountForKitties::<T>::set(Some(new_count));
Expand Down
2 changes: 1 addition & 1 deletion src/11/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pallet {
}

#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
6 changes: 3 additions & 3 deletions src/11/template/template.diff
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
diff --git a/src/impls.rs b/src/impls.rs
index a35e365..95d4a77 100644
index 9739330..c550bc8 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -4,6 +4,7 @@ use frame::prelude::*;
impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get().unwrap_or(0);
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
+ /* 🚧 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 });
diff --git a/src/lib.rs b/src/lib.rs
index b6c66a4..e119c87 100644
index ebaf109..e68500d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -27,7 +27,11 @@ pub mod pallet {
Expand Down
12 changes: 6 additions & 6 deletions src/12/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
diff --git a/src/impls.rs b/src/impls.rs
index 9cd8a31..8b6d7fe 100644
index b9b5548..d283fd6 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -3,11 +3,9 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub 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 current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
+ let current_count: u32 = 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));
Expand All @@ -17,16 +17,16 @@ index 9cd8a31..8b6d7fe 100644
Ok(())
}
diff --git a/src/lib.rs b/src/lib.rs
index 724375b..c14ae01 100644
index 322eeee..52b2dba 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,8 +18,7 @@ pub mod pallet {
}

#[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>;
- pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;
+ pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32, QueryKind = ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
2 changes: 1 addition & 1 deletion src/12/solution/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u64 = CountForKitties::<T>::get();
let current_count: u32 = 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 });
Expand Down
2 changes: 1 addition & 1 deletion src/12/solution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod pallet {
}

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

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
4 changes: 2 additions & 2 deletions src/12/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ We can do this even more ergonomically by changing the `QueryKind` to `ValueQuer

```rust
#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u64, QueryKind = ValueQuery>;
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32, QueryKind = ValueQuery>;
```

In this case, all of our APIs change.
Expand All @@ -65,7 +65,7 @@ When you call `get`, and the storage is empty, the `OnEmpty` configuration kicks
The default configuration for `OnEmpty` is `GetDefault`. This of course requires that the `Value` type must implement `Default`. But if it does, then you will get the following behavior:

```rust
assert!(CountForKitties::<T>::get() == u64::default());
assert!(CountForKitties::<T>::get() == u32::default());
```

For numbers, this value is normally zero, so simply setting `QueryKind = ValueQuery` gives you exactly the same behavior as what we programmed in our Pallet so far.
Expand Down
2 changes: 1 addition & 1 deletion src/12/template/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use frame::prelude::*;
impl<T: Config> Pallet<T> {
pub 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: u32 = 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));
Expand Down
2 changes: 1 addition & 1 deletion src/12/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod pallet {

#[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 = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down
Loading

0 comments on commit c70cec7

Please sign in to comment.