Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs 1.3.0 #41

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docusaurus/docs/advanced/03-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod test {
#[test]
fn ref_recursion_not_allowed() {
let test_env = odra_test::env();
let mut contract = NonReentrantCounterHostRef::deploy(&test_env, NoArgs);
let mut contract = NonReentrantCounter::deploy(&test_env, NoArgs);

let result = contract.count_ref_recursive(11);
assert_eq!(result, ExecutionError::ReentrantCall.into());
Expand Down
4 changes: 2 additions & 2 deletions docusaurus/docs/advanced/07-signatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn signature_verification_works() {

let public_key = test_env.public_key(&account);

let signature_verifier = SignatureVerifierHostRef::deploy(&test_env, NoArgs);
let signature_verifier = SignatureVerifier::deploy(&test_env, NoArgs);
assert!(signature_verifier.verify_signature(&message_bytes, &signature, &public_key));
}
```
Expand Down Expand Up @@ -84,7 +84,7 @@ fn verify_signature_casper_wallet() {
let public_key_decoded = hex::decode(public_key_hex).unwrap();
let (public_key, _) = PublicKey::from_bytes(public_key_decoded.as_slice()).unwrap();

let signature_verifier = SignatureVerifierHostRef::deploy(&odra_test::env(), NoArgs);
let signature_verifier = SignatureVerifier::deploy(&odra_test::env(), NoArgs);
assert!(signature_verifier.verify_signature(&message_bytes, &signature_bytes, &public_key));
}
```
2 changes: 1 addition & 1 deletion docusaurus/docs/backends/04-livenet.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn main() {
decimals,
initial_supply: Some(initial_supply)
};
let mut token = Erc20HostRef::deploy(env, init_args);
let mut token = Erc20::deploy(env, init_args);

// We can now use the contract as we would in the OdraVM backend.
println!("Token address: {}", token.address().to_string());
Expand Down
9 changes: 4 additions & 5 deletions docusaurus/docs/basics/04-flipper-internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,22 @@ saved there using `set` function will be persisted in the blockchain.
```rust title="flipper.rs"
#[cfg(test)]
mod tests {
use crate::flipper::FlipperHostRef;
use crate::flipper::Flipper;
use odra::host::{Deployer, NoArgs};

#[test]
fn flipping() {
let env = odra_test::env();
// To test a module we need to deploy it. Autogenerated `FlipperHostRef`
// implements `Deployer` trait, so we can use it to deploy the module.
let mut contract = FlipperHostRef::deploy(&env, NoArgs);
// To test a module we need to deploy it.
let mut contract = Flipper::deploy(&env, NoArgs);
assert!(!contract.get());
contract.flip();
assert!(contract.get());
}
...
```
You can write tests in any way you prefer and know in Rust. In the example above we are deploying the
contract using [`Deployer::deploy`] function called on `FlipperHostRef` - a piece of code generated
contract using [`Deployer::deploy`] function called on `Flipper` - a piece of code generated
by the `#[odra::module]`. Because the module implements the constructor but does not accept any arguments,
as the second argument of the deploy function, we pass `NoArgs` - one of the implementations of
the [`InitArgs`] trait provided with the framework.
Expand Down
31 changes: 17 additions & 14 deletions docusaurus/docs/basics/07-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use odra::{List, Var};

#[cfg(test)]
mod tests {
use super::{DogContract3HostRef, DogContract3InitArgs};
use super::{DogContract3, DogContract3InitArgs};
use odra::{host::Deployer, prelude::*};

#[test]
Expand All @@ -22,7 +22,7 @@ mod tests {
let init_args = DogContract3InitArgs {
name: "DogContract".to_string()
};
let mut dog_contract = DogContract3HostRef::deploy(&test_env, init_args);
let mut dog_contract = DogContract3::deploy(&test_env, init_args);
assert_eq!(dog_contract.walks_amount(), 0);
assert_eq!(dog_contract.walks_total_length(), 0);
dog_contract.walk_the_dog(5);
Expand All @@ -36,15 +36,14 @@ mod tests {
The first interesting thing you may notice is placed the import section.

```rust
use super::{DogContract3HostRef, DogContract3InitArgs};
use super::{DogContract3, DogContract3InitArgs};
use odra::{host::Deployer, prelude::*};
```

We are using `super` to import the `DogContract3HostRef` and `DogContract3InitArgs` from the parent module.
`{{ModuleName}}HostRef` and `{{ModuleName}}InitArgs` are types that was generated for us by Odra.
We are using `super` to import the `DogContract3` and `DogContract3InitArgs` from the parent module.
`{{ModuleName}}InitArgs` is a type that was generated for us by Odra, alongside with the code in the
code inside `{{ModuleName}}`.

`DogContract3HostRef` is a reference to the contract that we can use to interact with it (call entrypoints)
and implements [`HostRef`] trait.

`DogContract3InitArgs` is a struct that we use to initialize the contract and implements [`InitArgs`] trait.
Considering the contract initialization, there three possible scenarios:
Expand All @@ -57,14 +56,18 @@ All of those structs implement the `odra::host::InitArgs` trait, required to con
The other import is `odra::host::Deployer`. This is a trait is used to deploy the contract and give us a reference to it.

Let's take a look at the test itself. How to obtain a reference to the contract?
`{{ModuleName}}HostRef` implements the [`Deployer`] trait, which provides the `deploy` method:
`{{ModuleName}}` implements the [`Deployer`] trait, which provides the `deploy` method:

```rust title="examples/src/features/storage/list.rs"
let mut dog_contract = DogContract3HostRef::deploy(&test_env, init_args);
let mut dog_contract = DogContract3::deploy(&test_env, init_args);
```

From now on, we can use `dog_contract` to interact with our deployed contract - in particular, all
`pub` functions from the impl section that was annotated with the `odra::module` attribute are available to us:
From now on, we can use `dog_contract` to interact with our deployed contract.
Its type is `DogContract3HostRef`, which is a reference to the contract that we can use to interact with it (call entrypoints)
and implements [`HostRef`] trait.
In particular, all
`pub` functions from the impl section
that was annotated with the `odra::module` attribute are available to us in this type:

```rust title="examples/src/features/storage/list.rs"
// Impl
Expand All @@ -87,7 +90,7 @@ article about host communication and implement the tests that prove it works:
```rust title="examples/src/features/testing.rs"
#[cfg(test)]
mod tests {
use crate::features::testing::{TestingContractHostRef, TestingContractInitArgs};
use crate::features::testing::{TestingContract, TestingContractInitArgs};
use odra::{host::{Deployer, HostEnv}, prelude::*};

#[test]
Expand All @@ -97,13 +100,13 @@ mod tests {
let init_args = TestingContractInitArgs {
name: "MyContract".to_string()
};
let testing_contract = TestingContractHostRef::deploy(&test_env, init_args);
let testing_contract = TestingContract::deploy(&test_env, init_args);
let creator = testing_contract.created_by();
test_env.set_caller(test_env.get_account(1));
let init_args = TestingContractInitArgs {
name: "MyContract2".to_string()
};
let testing_contract2 = TestingContractHostRef::deploy(&test_env, init_args);
let testing_contract2 = TestingContract::deploy(&test_env, init_args);
let creator2 = testing_contract2.created_by();
assert_ne!(creator, creator2);
}
Expand Down
7 changes: 4 additions & 3 deletions docusaurus/docs/basics/08-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Okay, but how about testing it? Let's write a test that will check if the error
```rust title="examples/src/features/handling_errors.rs"
#[cfg(test)]
mod tests {
use super::{Error, OwnedContractHostRef, OwnedContractInitArgs};
use super::{Error, OwnedContract, OwnedContractInitArgs};
use odra::{host::Deployer, prelude::*};

#[test]
Expand All @@ -96,7 +96,7 @@ mod tests {
let init_args = OwnedContractInitArgs {
name: "OwnedContract".to_string()
};
let mut owned_contract = OwnedContractHostRef::deploy(&test_env, init_args);
let mut owned_contract = OwnedContract::deploy(&test_env, init_args);

test_env.set_caller(not_an_owner);
assert_eq!(
Expand All @@ -106,7 +106,8 @@ mod tests {
}
}
```
Each `{{ModuleName}}HostRef` has `try_{{entry_point_name}}` functions that return an [`OdraResult`].
Each deployed contract is of `{{ModuleName}}HostRef` type and has `try_{{entry_point_name}}` functions
that return an [`OdraResult`].
`OwnedContractHostRef` implements regular entrypoints: `name`, `owner`, `change_name`, and
and safe its safe version: `try_name`, `try_owner`, `try_change_name`.

Expand Down
4 changes: 2 additions & 2 deletions docusaurus/docs/basics/09-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ The event collection process is recursive; if your module consists of other modu
Odra's `HostEnv` comes with a few functions which lets you easily test the events that a given contract has emitted:

```rust title="examples/src/features/events.rs"
use super::{PartyContractHostRef, PartyStarted};
use super::{PartyContract, PartyStarted};
use odra::host::{Deployer, HostEnv, NoArgs};

#[test]
fn test_party() {
let test_env: HostEnv = odra_test::env();
let party_contract = PartyContractHostRef::deploy(&test_env, NoArgs);
let party_contract = PartyContract::deploy(&test_env, NoArgs);
test_env.emitted_event(
&party_contract,
&PartyStarted {
Expand Down
21 changes: 12 additions & 9 deletions docusaurus/docs/basics/10-cross-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ We mentioned `HostRef` already in our [Testing](07-testing.md) article - a host

In the module context we use a `ContractRef` instead, to call other contracts.

Similarly to the `{{ModuleName}}HostRef`, the `{{ModuleName}}ContractRef` is generated automatically,
Similarly to the `HostRef` trait implemetation for the module, the `{{ModuleName}}ContractRef` is generated automatically,
by the `#[odra::module]` attribute.

The reference implements all the public endpoints to the contract (those marked as `pub` in `#[odra::module]`
Expand All @@ -86,7 +86,7 @@ pub trait Adder {
}
```

Analogously to modules, Odra creates the `AdderContractRef` struct (and `AdderHostRef` to be used in tests, but do not implement the `Deployer` trait). Having an address, in the module context we can call:
Odra automatically creates the `AdderContractRef` struct. Having an address, in the module context we can call:

```rust
struct Contract {
Expand All @@ -112,7 +112,7 @@ our contracts in [Livenet](../backends/04-livenet.md) backend. We can load the c
fn _load(env: &HostEnv) -> Erc20HostRef {
let address = "hash-d26fcbd2106e37be975d2045c580334a6d7b9d0a241c2358a4db970dfd516945";
let address = Address::from_str(address).unwrap();
<Erc20HostRef as HostRefLoader>::load(env, address)
Erc20::load(env, address)
}
```

Expand All @@ -122,14 +122,14 @@ Let's see how we can test our cross calls using this knowledge:
```rust title="examples/src/features/cross_calls.rs"
#[cfg(test)]
mod tests {
use super::{CrossContractHostRef, CrossContractInitArgs, MathEngineHostRef};
use super::{CrossContract, CrossContractInitArgs, MathEngineHostRef};
use odra::host::{Deployer, HostRef, NoArgs};

#[test]
fn test_cross_calls() {
let test_env = odra_test::env();
let math_engine_contract = MathEngineHostRef::deploy(&test_env, NoArgs);
let cross_contract = CrossContractHostRef::deploy(
let math_engine_contract = MathEngine::deploy(&test_env, NoArgs);
let cross_contract = CrossContract::deploy(
&test_env,
CrossContractInitArgs {
math_engine_address: *math_engine_contract.address()
Expand All @@ -140,7 +140,10 @@ mod tests {
}
```

Each test begins with a clean instance of the blockchain, with no contracts deployed. To test an external contract, we first deploy a `MathEngine` contract, although we won't directly utilize it. Instead, we only extract its address. Let's continue assuming there is a contract featuring the `add()` function that we intend to utilize.
Each test begins with a clean instance of the blockchain, with no contracts deployed.
To test an external contract, we first deploy a `MathEngine` contract, although we won't directly utilize it.
Instead, we only extract its address.
Let's continue assuming there is a contract featuring the `add()` function that we intend to utilize.

```rust
#[cfg(test)]
Expand All @@ -151,12 +154,12 @@ mod tests {
#[test]
fn test_ext() {
let test_env = odra_test::env();
let adder = AdderHostRef::new(&test_env, get_adder_address(&test_env)).add(3, 5)
let adder = Adder::new(&test_env, get_adder_address(&test_env)).add(3, 5)
assert_eq!(adder.add(1, 2), 3);
}

fn get_adder_address(test_env: &HostEnv) -> Address {
let contract = MathEngineHostRef::deploy(test_env, NoArgs);
let contract = MathEngine::deploy(test_env, NoArgs);
*contract.address()
}
}
Expand Down
4 changes: 2 additions & 2 deletions docusaurus/docs/basics/11-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ As we don't need to hold addresses, the test is really simple:
```rust title="examples/src/features/modules.rs"
#[cfg(test)]
mod tests {
use super::ModulesContractHostRef;
use super::ModulesContract;
use odra::host::{Deployer, NoArgs};

#[test]
fn test_modules() {
let test_env = odra_test::env();
let modules_contract = ModulesContractHostRef::deploy(&test_env, NoArgs);
let modules_contract = ModulesContract::deploy(&test_env, NoArgs);
assert_eq!(modules_contract.add_using_module(), 8);
}
}
Expand Down
4 changes: 2 additions & 2 deletions docusaurus/docs/basics/12-native-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ To be able to test how many tokens a contract (or any address) has, `HostEnv` co
```rust title="examples/src/features/native_token.rs"
#[cfg(test)]
mod tests {
use super::PublicWalletHostRef;
use super::PublicWallet;
use odra::{casper_types::U512, host::{Deployer, HostRef, NoArgs}};

#[test]
fn test_modules() {
let test_env = odra_test::env();
let mut my_contract = PublicWalletHostRef::deploy(&test_env, NoArgs);
let mut my_contract = PublicWallet::deploy(&test_env, NoArgs);
assert_eq!(test_env.balance_of(my_contract.address()), U512::zero());

my_contract.with_tokens(U512::from(100)).deposit();
Expand Down
8 changes: 4 additions & 4 deletions docusaurus/docs/getting-started/flipper.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ mod tests {
#[test]
fn flipping() {
let env = odra_test::env();
// To test a module we need to deploy it. Autogenerated `FlipperHostRef`
// To test a module we need to deploy it. `Flipper` automagically
// implements `Deployer` trait, so we can use it to deploy the module.
let mut contract = FlipperHostRef::deploy(&env, NoArgs);
let mut contract = Flipper::deploy(&env, NoArgs);
assert!(!contract.get());
contract.flip();
assert!(contract.get());
Expand All @@ -72,8 +72,8 @@ mod tests {
#[test]
fn test_two_flippers() {
let env = odra_test::env();
let mut contract1 = FlipperHostRef::deploy(&env, NoArgs);
let contract2 = FlipperHostRef::deploy(&env, NoArgs);
let mut contract1 = Flipper::deploy(&env, NoArgs);
let contract2 = Flipper::deploy(&env, NoArgs);
assert!(!contract1.get());
assert!(!contract2.get());
contract1.flip();
Expand Down
34 changes: 34 additions & 0 deletions docusaurus/docs/migrations/to-1.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
sidebar_position: 1
description: Migration guide to v0.8.0
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Migration guide to v1.3.0

Odra v1.3.0 introduces several breaking changes that require users to update their smart contracts and tests. This migration guide provides a detailed overview of the changes, along with step-by-step instructions for migrating existing code to the new version.

This guide is intended for developers who have built smart contracts using previous versions of Odra and need to update their code to be compatible with v0.8.0. It assumes a basic understanding of smart contract development and the Odra framework. If you're new to Odra, we recommend to start your journey with the [Getting Started](../category/getting-started/).

Odra v1.3.0 introduced a new OdraContract trait, which groups all module-related structs.
From the user perspective, the only change is that the `deploy` and `load` methods
are now implemented in the module itself, not in the autogenerated
`{{ModuleName}}HostRef` struct.

## Migrating to Odra v1.3.0

To migrate your smart contracts to Odra v1.3.0, remove the use the `deploy` method from
the module instead of the `{{ModuleName}}HostRef` struct:

```rust title=before.rs
let token = TokenHostRef::deploy(env, init_args);
let another_token = AnotherTokenHostRef::load(env, address);
```

```rust title=after.rs
let token = Token::deploy(env, init_args);
let another_token = AnotherToken::load(env, address);
```

4 changes: 2 additions & 2 deletions docusaurus/docs/tutorials/cep18.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn it_works() {
initial_supply: U256::from(1_000u64),
};

let mut token = OurTokenHostRef::deploy(&env, init_args);
let mut token = OurToken::deploy(&env, init_args);

// The deployer, as the only token holder,
// starts a new voting to mint 1000 tokens to account 1.
Expand Down Expand Up @@ -548,7 +548,7 @@ mod tests {
initial_supply: U256::from(1_000u64),
};

let mut token = OurTokenHostRef::deploy(&env, init_args);
let mut token = OurToken::deploy(&env, init_args);

// The deployer, as the only token holder,
// starts a new voting to mint 1000 tokens to account 1.
Expand Down
Loading
Loading