Skip to content

Commit

Permalink
make external calls more clear for rust and words about key value sto…
Browse files Browse the repository at this point in the history
…rage
  • Loading branch information
PolyProgrammist committed Nov 21, 2024
1 parent b3d72dc commit 40b7f82
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion docs/2.build/2.smart-contracts/anatomy/crosscontract.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ While making your contract, it is likely that you will want to query information
<Github fname="lib.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs"
start="22" end="51" />
<Github fname="external.rs"
</Language>

</CodeTabs>

Note that in order for that to work in Rust, you have to implement the interface of the contract you call:

<CodeTabs>
<Language value="rust" language="rust">
<Github fname="external.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs"
start="2" end="12" />

Expand All @@ -66,6 +75,13 @@ Calling another contract passing information is also a common scenario. Below yo
<Github fname="lib.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs"
start="53" end="80" />

</Language>

</CodeTabs>

<CodeTabs>
<Language value="rust" language="rust">
<Github fname="external.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs"
start="2" end="12" />
Expand Down
22 changes: 22 additions & 0 deletions docs/2.build/2.smart-contracts/anatomy/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ Smart contracts store data in their account's state, which is public on the chai

It is important to know that the account's **code** and account's **storage** are **independent**. [Updating the code](../release/upgrade.md) does **not erase** the state.

## Key-value storage

Behind the scenes, all data stored on the NEAR blockchain is saved in a key-value database. A storage key is a unique identifier used to access data stored in a smart contract’s persistent storage. The key-value storage model in NEAR allows smart contracts to manage and retrieve data efficiently along with minimizing costs for storage.

Keys can be any binary sequence, but they are typically structured for ease of use (e.g., as human-readable strings).
Data associated with a key persists across contract calls and is stored on-chain until explicitly deleted.

SDK collections are instantiated using a "prefix" so that the elements from different collections access different data.

Here is an example:

```ts
const tokenToOwner = new PersistentMap<TokenId, AccountId>("t2o");
```

That `'t2o'` variable that's passed to `PersistentMap` helps it keep track of all its values. If your account `example.near` owns token with ID `0`, then at the time of writing, here's the data that would get saved to the key-value database:

- key: `t2o::0`
- value: `example.near`

Storage key type should implement the trait `IntoStorageKey`.

<hr class="subsection" />

<ExplainCode languages="js,rust" >
Expand Down

0 comments on commit 40b7f82

Please sign in to comment.