-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
270 additions
and
1,886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
...docs/developers/guides/smart_contracts/writing_contracts/how_to_pop_capsules.md
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...docs/developers/guides/smart_contracts/writing_contracts/how_to_use_capsules.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
title: Using Capsules | ||
sidebar_position: 5 | ||
tags: [functions, oracles] | ||
--- | ||
|
||
Capsules are a per-contract non-volatile database. | ||
It can be used for storing arbitrary data that can be retrieved later. | ||
The data is stored locally in PXE and it is scoped per contract address, so external contracts cannot access it. | ||
The capsule (data stored under a storage slot in the capsules database) persists until explicitly deleted with `delete`. | ||
|
||
The capsules module provides these main functions: | ||
|
||
- `store<T, N>` - Stores arbitrary data at a slot, overwriting any existing data | ||
- `load<T, N>` - Retrieves previously stored data from a slot | ||
- `delete` - Deletes data at a slot | ||
- `copy` - Efficiently copies contiguous entries between slots | ||
|
||
### 1. Import capsules into your smart contract | ||
|
||
Import the capsules module: | ||
|
||
#include_code import_capsules noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr rust | ||
|
||
### 2. Store and load data | ||
|
||
You can store any type that implements `Serialize` and `Deserialize`: | ||
|
||
#include_code load_capsule noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr rust | ||
|
||
The data is stored per contract address and slot. When loading, you'll get back an `Option<T>` - `None` if no data exists at that slot. | ||
|
||
### 3. Copying data | ||
|
||
You can use `copy` to move contiguous entries between slots without repeated loads and stores. | ||
This supports overlapping source and destination regions. | ||
|
||
Note that all values are scoped per contract address, so external contracts cannot access them. | ||
|
||
### 4. Using CapsuleArray | ||
|
||
The `CapsuleArray<T>` type provides a dynamically sized array backed by capsules. | ||
It handles the storage layout and management automatically. | ||
The array stores its length at a base slot, with elements stored in consecutive slots after it. | ||
|
||
Key functions: | ||
|
||
- `at(contract_address, base_slot)` - Creates/connects to an array at the given base slot | ||
- `len()` - Returns the number of elements in the array | ||
- `push(value)` - Appends a value to the end of the array | ||
- `get(index)` - Retrieves the value at the given index | ||
- `remove(index)` - Removes an element, shifting subsequent elements to maintain contiguous storage | ||
|
||
<!-- TODO: Document actual use case of CapsuleArray here once it's actually used. --> |
Oops, something went wrong.