Skip to content

Commit

Permalink
docs: edit storage README with ByteArray content
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Dec 11, 2023
1 parent f75512c commit ce74a21
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,40 @@ There are two idiosyncacies you should be aware of when using `List`
let mut amounts = self.amounts.read();
amounts.append(42);
```

## ByteArray

`ByteArray` is the new native type of Cairo to support long strings.
Awaiting for the built-in support for `ByteArray` in storage, a first implementation is proposed here.

### Storage value and LegacyMap

The `ByteArray` storage implementation comes with two traits:

1. `StoreByteArray`: which allows you to use `ByteArray` as a value in the storage.
2. `LegacyHashByteArray`: which permits the use of `ByteArray` as keys in `LegacyMap` data structure.

### Usage

`ByteArray` is already a type present in the prelude. To use it in the storage, the trait of alexandria must be in the scope.

```rust
mod my_contract {
use alexandria_storage::byte_array::{LegacyHashByteArray, StoreByteArray};
#[storage]
struct Storage {
my_string: ByteArray,
token_uris: LegacyMap<u256, ByteArray>,
string_keys: LegacyMap<ByteArray, felt252>,
}

#[constructor]
fn constructor(ref self: ContractState) {
self.my_string.write("ABCD");
self.token_uris.write(0x1, "ipfs://....");
self.string_keys.write("user 1", 0x1234);
}

// ... other code
}
```

0 comments on commit ce74a21

Please sign in to comment.