Skip to content

Commit

Permalink
Update 4.storage.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bucanero committed Jan 18, 2024
1 parent c1a5b95 commit af88eac
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions docs/3.tutorials/fts/4.storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,19 @@ For the purpose of simplicity, we'll assume the second method.

This "bytes for longest account ID" should be stored in the contract's state such that we can pull the value during the `storage_deposit` function and ensure the user attaches enough $NEAR. Open the `src/lib.rs` file and add the following code to the `Contract` struct. If you're just joining us now, you can find the skeleton code for this tutorial in the `3.initial-supply` folder.

``` rust reference
https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/lib.rs#L21-L35
```
<Github language="rust" start="21" end="35" url="https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/lib.rs" />

You'll now need a way to calculate this amount which will be done in the initialization function. Move to the `src/internal.rs` file and add the following private function `measure_bytes_for_longest_account_id` which will add the longest possible account ID and remove it while measuring how many bytes the operation took. It will then set the `bytes_for_longest_account_id` field to the result.

``` rust reference
https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs#L36-L45
```
<Github language="rust" start="36" end="45" url="https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs" />

You'll also want to create a function for "registering" an account after they've paid for storage. To do this, you can simply insert them into the `accounts` map with a balance of 0. This way, you know that any account currently in the map is considered "registered" and have paid for storage. Any account that attempts to receive FTs must be in the map with a balance of 0 or greater. If they aren't, the contract should throw.

``` rust reference
https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs#L29-L34
```
<Github language="rust" start="29" end="34" url="https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs" />

Let's also create a function to panic with a custom message if the user doesn't exist yet.

``` rust reference
https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs#L5-L14
```
<Github language="rust" start="5" end="14" url="https://github.com/near-examples/ft-tutorial/blob/main/4.storage/src/internal.rs" />

Now when you call the `internal_deposit` function, rather than defaulting the user's balance to `0` if they don't exist yet via:
```rust
Expand Down

0 comments on commit af88eac

Please sign in to comment.