Skip to content

Commit fa8b20b

Browse files
authored
Feature/2.3.1 (#49)
* Add ODRA_CASPER_LIVENET_TTL to the livenet setup * Add version 2.3.1 * Add the `odra_cfg_is_upgrade` to a schema example
1 parent e698600 commit fa8b20b

67 files changed

Lines changed: 10678 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docusaurus/docs/backends/04-livenet.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ ODRA_CASPER_LIVENET_EVENTS_URL=<events url>
7575
7676
# If using CSPR.cloud, you can set the auth token here.
7777
# CSPR_CLOUD_AUTH_TOKEN=
78+
79+
# Optionally, you can set the TTL for the deploys. Default is 5 minutes.
80+
# ODRA_CASPER_LIVENET_TTL=
7881
```
7982

8083
:::note

docusaurus/docs/basics/13-casper-contract-schema.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ The generated schema will be available in the `resources` directory. The schema
229229
"ty": "Bool",
230230
"optional": false
231231
},
232+
{
233+
"name": "odra_cfg_is_upgrade",
234+
"description": "The arg name for the contract upgrade setting.",
235+
"ty": "Bool",
236+
"optional": false
237+
},
232238
{
233239
"name": "name",
234240
"description": null,

docusaurus/docusaurus.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const config = {
5454
sidebarPath: require.resolve('./sidebars.js'),
5555
includeCurrentVersion: true,
5656
showLastUpdateTime: true,
57-
lastVersion: '2.3.0',
57+
lastVersion: '2.3.1',
5858
versions: {
5959
current: {
6060
label: 'next',

docusaurus/versioned_docs/version-2.3.0/basics/13-casper-contract-schema.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ The generated schema will be available in the `resources` directory. The schema
229229
"ty": "Bool",
230230
"optional": false
231231
},
232+
{
233+
"name": "odra_cfg_is_upgrade",
234+
"description": "The arg name for the contract upgrade setting.",
235+
"ty": "Bool",
236+
"optional": false
237+
},
232238
{
233239
"name": "name",
234240
"description": null,
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Delegate
2+
3+
Managing boilerplate code can often lead to code that is cumbersome and challenging to comprehend. The Odra library introduces a solution to this issue with its Delegate feature. As the name implies, the Delegate feature permits the delegation of function calls to child modules, effectively minimizing the redundancy of boilerplate code and maintaining a lean and orderly parent module.
4+
5+
The main advantage of this feature is that it allows you to inherit the default behavior of child modules seamlessly, making your contracts more readable.
6+
7+
## Overview
8+
9+
To utilize the delegate feature in your contract, use the `delegate!` macro provided by Odra. This macro allows you to list the functions you wish to delegate to the child modules. By using the `delegate!` macro, your parent module remains clean and easy to understand.
10+
11+
You can delegate functions to as many child modules as you like. The functions will be available as if they were implemented in the parent module itself.
12+
13+
## Code Examples
14+
15+
Consider the following basic example for better understanding:
16+
17+
```rust
18+
use crate::{erc20::Erc20, ownable::Ownable};
19+
use odra::{casper_types::U256, prelude::*};
20+
21+
#[odra::module]
22+
pub struct OwnedToken {
23+
ownable: SubModule<Ownable>,
24+
erc20: SubModule<Erc20>
25+
}
26+
27+
#[odra::module]
28+
impl OwnedToken {
29+
pub fn init(&mut self, name: String, symbol: String, decimals: u8, initial_supply: U256) {
30+
let deployer = self.env().caller();
31+
self.ownable.init(deployer);
32+
self.erc20.init(name, symbol, decimals, initial_supply);
33+
}
34+
35+
delegate! {
36+
to self.erc20 {
37+
fn transfer(&mut self, recipient: Address, amount: U256);
38+
fn transfer_from(&mut self, owner: Address, recipient: Address, amount: U256);
39+
fn approve(&mut self, spender: Address, amount: U256);
40+
fn name(&self) -> String;
41+
fn symbol(&self) -> String;
42+
fn decimals(&self) -> u8;
43+
fn total_supply(&self) -> U256;
44+
fn balance_of(&self, owner: Address) -> U256;
45+
fn allowance(&self, owner: Address, spender: Address) -> U256;
46+
}
47+
48+
to self.ownable {
49+
fn get_owner(&self) -> Address;
50+
fn change_ownership(&mut self, new_owner: Address);
51+
}
52+
}
53+
54+
pub fn mint(&mut self, address: Address, amount: U256) {
55+
self.ownable.ensure_ownership(self.env().caller());
56+
self.erc20.mint(address, amount);
57+
}
58+
}
59+
```
60+
61+
This `OwnedToken` contract includes two modules: `Erc20` and `Ownable`. We delegate various functions from both modules using the `delegate!` macro. As a result, the contract retains its succinctness without compromising on functionality.
62+
63+
The above example basically merges the functionalities of modules and adds some control over the minting process. But you can use delegation to build more complex contracts, cherry-picking just a few module functionalities.
64+
65+
Let's take a look at another example.
66+
67+
```rust
68+
use crate::{erc20::Erc20, ownable::Ownable, exchange::Exchange};
69+
use odra::{casper_types::U256, prelude::*};
70+
71+
#[odra::module]
72+
pub struct DeFiPlatform {
73+
ownable: SubModule<Ownable>,
74+
erc20: SubModule<Erc20>,
75+
exchange: SubModule<Exchange>
76+
}
77+
78+
#[odra::module]
79+
impl DeFiPlatform {
80+
pub fn init(&mut self, name: String, symbol: String, decimals: u8, initial_supply: U256, exchange_rate: u64) {
81+
let deployer = self.env().caller();
82+
self.ownable.init(deployer);
83+
self.erc20.init(name, symbol, decimals, initial_supply);
84+
self.exchange.init(exchange_rate);
85+
}
86+
87+
delegate! {
88+
to self.erc20 {
89+
fn transfer(&mut self, recipient: Address, amount: U256);
90+
fn balance_of(&self, owner: Address) -> U256;
91+
}
92+
93+
to self.ownable {
94+
fn get_owner(&self) -> Address;
95+
}
96+
97+
to self.exchange {
98+
fn swap(&mut self, sender: Address, recipient: Address);
99+
fn set_exchange_rate(&mut self, new_rate: u64);
100+
}
101+
}
102+
103+
pub fn mint(&mut self, address: Address, amount: U256) {
104+
self.ownable.ensure_ownership(self.env().caller());
105+
self.erc20.mint(address, amount);
106+
}
107+
}
108+
```
109+
110+
In this `DeFiPlatform` contract, we include `Erc20`, `Ownable`, and `Exchange` modules. By delegating functions from these modules, the parent contract becomes a powerhouse of functionality while retaining its readability and structure.
111+
112+
Remember, the possibilities are endless with Odra's. By leveraging this feature, you can write cleaner, more efficient, and modular smart contracts.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Advanced Storage Concepts
2+
3+
The Odra Framework provides advanced storage interaction capabilities that extend beyond the basic storage interaction. This document will focus on the `Mapping` and `Sequence` modules, which are key components of the advanced storage interaction in Odra.
4+
5+
## Recap and Basic Concepts
6+
7+
Before we delve into the advanced features, let's recap some basic storage concepts in Odra. In the realm of basic storage interaction, Odra provides several types for interacting with contract storage, including `Var`, `Mapping`, and `List`. These types enable contracts to store and retrieve data in a structured manner. The Var type is used to store a single value, while the List and Mapping types store collections of values.
8+
9+
**Var**: A Var in Odra is a fundamental building block used for storing single values. Each Var is uniquely identified by its name in the contract.
10+
11+
**Mapping**: Mapping in Odra serves as a key-value storage system. It stores an association of unique keys to values, and the value can be retrieved using the key.
12+
13+
**List**: Built on top of the Var and Mapping building blocks, List in Odra allows storing an ordered collection of values that can be iterated over.
14+
15+
If you need a refresher on these topics, please refer to our [guide](../basics/05-storage-interaction.md) on basic storage in Odra.
16+
17+
## Advanced Storage Concepts
18+
19+
### Sequence
20+
21+
The `Sequence` in Odra is a basic module that stores a single value in the storage that can be read or incremented. Internally, holds a `Var` which keeps track of the current value.
22+
23+
```rust
24+
pub struct Sequence<T>
25+
where
26+
T: Num + One + ToBytes + FromBytes + CLTyped
27+
{
28+
value: Var<T>
29+
}
30+
```
31+
32+
The Sequence module provides functions `get_current_value` and `next_value` to get the current value and increment the value respectively.
33+
34+
### Advanced Mapping
35+
36+
In Odra, a `Mapping` is a key-value storage system where the key is associated with a value.
37+
In previous examples, the value of the `Mapping` typically comprised a standard serializable type (such as number, string, or bool) or a custom type marked with the `#[odra::odra_type]` attribute.
38+
39+
However, there are more advanced scenarios where the value of the Mapping represents a module itself. This approach is beneficial when managing a collection of modules, each maintaining its unique state.
40+
41+
Let's consider the following example:
42+
43+
```rust title="examples/src/features/storage/mapping.rs"
44+
use odra::casper_types::U256;
45+
use odra::prelude::*;
46+
use crate::owned_token::OwnedToken;
47+
48+
#[odra::module]
49+
pub struct Mappings {
50+
strings: Mapping<(String, u32, String), String>,
51+
tokens: Mapping<String, OwnedToken>
52+
}
53+
54+
#[odra::module]
55+
impl Mappings {
56+
57+
...
58+
59+
pub fn total_supply(&mut self, token_name: String) -> U256 {
60+
self.tokens.module(&token_name).total_supply()
61+
}
62+
63+
pub fn get_string_api(
64+
&self,
65+
key1: String,
66+
key2: u32,
67+
key3: String
68+
) -> String {
69+
let opt_string = self.strings.get(&(key1, key2, key3));
70+
opt_string.unwrap_or_revert(&self.env())
71+
}
72+
}
73+
```
74+
75+
As you can see, a `Mapping` key can consist of a tuple of values, not limited to a single value.
76+
77+
:::note
78+
Accessing Odra modules differs from accessing regular values such as strings or numbers.
79+
80+
Firstly, within a `Mapping`, you don't encapsulate the module with `Submodule`.
81+
82+
Secondly, rather than utilizing the `Mapping::get()` function, call `Mapping::module()`, which returns `SubModule<T>` and sets the appropriate namespace for nested modules.
83+
:::
84+
85+
## AdvancedStorage Contract
86+
87+
The given code snippet showcases the `AdvancedStorage` contract that incorporates these storage concepts.
88+
89+
```rust
90+
use odra::casper_types::U512;
91+
use odra::prelude::*;
92+
use crate::modules::Token;
93+
94+
#[odra::module]
95+
pub struct AdvancedStorage {
96+
counter: Sequence<u32>,
97+
tokens: Mapping<(String, String), Token>,
98+
}
99+
100+
impl AdvancedStorage {
101+
pub fn current_value(&self) -> u32 {
102+
self.counter.get_current_value()
103+
}
104+
105+
pub fn increment_and_get(&mut self) -> u32 {
106+
self.counter.next_value()
107+
}
108+
109+
pub fn balance_of(&mut self, token_name: String, creator: String, address: Address) -> U512 {
110+
let token = self.tokens.module(&(token_name, creator));
111+
token.balance_of(&address)
112+
}
113+
114+
pub fn mint(&self, token_name: String, creator: String, amount: U512, to: Address) {
115+
let mut token = self.tokens.module(&(token_name, creator));
116+
token.mint(amount, to);
117+
}
118+
}
119+
```
120+
121+
## Conclusion
122+
123+
Advanced storage features in Odra offer robust options for managing contract state. Two key takeaways from this document are:
124+
1. Odra offers a Sequence module, enabling contracts to store and increment a single value.
125+
2. Mappings support composite keys expressed as tuples and can store modules as values.
126+
127+
Understanding these concepts can help developers design and implement more efficient and flexible smart contracts.

0 commit comments

Comments
 (0)