Skip to content

Commit

Permalink
Template for workspace project (#309)
Browse files Browse the repository at this point in the history
* Added template for workspace project.
To avoid name collisions of bins in workspace, bins had to be prepended
with project name.
Rewritten modules to fit changes to core.

* Fixed contract template
  • Loading branch information
kubaplas authored Jan 9, 2024
1 parent 894ceb3 commit 37650a3
Show file tree
Hide file tree
Showing 31 changed files with 325 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
odra = { path = "../odra" }

[[bin]]
name = "build_contract"
name = "examples2_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "build_schema"
name = "examples2_build_schema"
path = "bin/build_schema.rs"
test = false

Expand Down
4 changes: 2 additions & 2 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ casper-event-standard = "0.4.1"
hex = { version = "0.4.3", default-features = false }

[[bin]]
name = "build_contract"
name = "odra_modules_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "build_schema"
name = "odra_modules_build_schema"
path = "bin/build_schema.rs"
test = false

Expand Down
56 changes: 24 additions & 32 deletions modules/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ mod test {
new_owner: Some(deployer)
};

env.emitted_event(&ownable.address, &event);
env.emitted_event(&ownable_2step.address, &event);
env.emitted_event(&ownable.address(), &event);
env.emitted_event(&ownable_2step.address(), &event);
}

#[test]
Expand All @@ -181,14 +181,14 @@ mod test {
let (mut contract, initial_owner) = setup_ownable();

// when the current owner transfers ownership
let new_owner = contract.env.get_account(1);
let new_owner = contract.env().get_account(1);
contract.transfer_ownership(new_owner);

// then the new owner is set
assert_eq!(new_owner, contract.get_owner());
// then a OwnershipTransferred event was emitted
contract.env.emitted_event(
&contract.address,
contract.env().emitted_event(
&contract.address(),
&OwnershipTransferred {
previous_owner: Some(initial_owner),
new_owner: Some(new_owner)
Expand All @@ -202,27 +202,27 @@ mod test {
let (mut contract, initial_owner) = setup_ownable_2_step();

// when the current owner transfers ownership
let new_owner = contract.env.get_account(1);
let new_owner = contract.env().get_account(1);
contract.transfer_ownership(new_owner);

// when the pending owner accepts the transfer
contract.env.set_caller(new_owner);
contract.env().set_caller(new_owner);
contract.accept_ownership();

// then the new owner is set
assert_eq!(new_owner, contract.get_owner());
// then the pending owner is unset
assert_eq!(None, contract.get_pending_owner());
// then OwnershipTransferStarted and OwnershipTransferred events were emitted
contract.env.emitted_event(
&contract.address,
contract.env().emitted_event(
&contract.address(),
&OwnershipTransferStarted {
previous_owner: Some(initial_owner),
new_owner: Some(new_owner)
}
);
contract.env.emitted_event(
&contract.address,
contract.env().emitted_event(
&contract.address(),
&OwnershipTransferred {
previous_owner: Some(initial_owner),
new_owner: Some(new_owner)
Expand All @@ -236,8 +236,8 @@ mod test {
let (mut contract, _) = setup_ownable();

// when a non-owner account is the caller
let (caller, new_owner) = (contract.env.get_account(1), contract.env.get_account(2));
contract.env.set_caller(caller);
let (caller, new_owner) = (contract.env().get_account(1), contract.env().get_account(2));
contract.env().set_caller(caller);

// then ownership transfer fails
let err = contract.try_transfer_ownership(new_owner).unwrap_err();
Expand All @@ -250,15 +250,15 @@ mod test {
let (mut contract, initial_owner) = setup_ownable_2_step();

// when a non-owner account is the caller
let (caller, new_owner) = (contract.env.get_account(1), contract.env.get_account(2));
contract.env.set_caller(caller);
let (caller, new_owner) = (contract.env().get_account(1), contract.env().get_account(2));
contract.env().set_caller(caller);

// then ownership transfer fails
let err = contract.try_transfer_ownership(new_owner).unwrap_err();
assert_eq!(err, CallerNotTheOwner.into());

// when the owner is the caller
contract.env.set_caller(initial_owner);
contract.env().set_caller(initial_owner);
contract.transfer_ownership(new_owner);

// then the pending owner is set
Expand Down Expand Up @@ -287,8 +287,8 @@ mod test {
contract.renounce_ownership();

// then an event is emitted
contract.env.emitted_event(
&contract.address,
contract.env().emitted_event(
&contract.address(),
&OwnershipTransferred {
previous_owner: Some(initial_owner),
new_owner: None
Expand All @@ -310,8 +310,8 @@ mod test {

contracts.iter_mut().for_each(|contract| {
// when a non-owner account is the caller
let caller = contract.env.get_account(1);
contract.env.set_caller(caller);
let caller = contract.env().get_account(1);
contract.env().set_caller(caller);

// then renounce ownership fails
let err = contract.try_renounce_ownership().unwrap_err();
Expand Down Expand Up @@ -344,19 +344,11 @@ mod test {
let env = odra::test_env();
let ownable = OwnableDeployer::init(&env);
let ownable_2_step = Ownable2StepDeployer::init(&env);
let renouncable_ref = RenounceableHostRef::new(*ownable.address(), env.clone());
let renouncable_2_step_ref =
RenounceableHostRef::new(*ownable_2_step.address(), env.clone());
(
vec![
RenounceableHostRef {
address: ownable.address,
env: env.clone(),
attached_value: Default::default()
},
RenounceableHostRef {
address: ownable_2_step.address,
env: env.clone(),
attached_value: Default::default()
},
],
vec![renouncable_ref, renouncable_2_step_ref],
env.get_account(0)
)
}
Expand Down
4 changes: 2 additions & 2 deletions templates/blank/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
#odra_dependency

[[bin]]
name = "build_contract"
name = "{{project-name}}_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "build_schema"
name = "{{project_name}}_build_schema"
path = "bin/build_schema.rs"
test = false

Expand Down
2 changes: 1 addition & 1 deletion templates/blank/Odra.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#[[contracts]]
#name = "mycontract"
#name = "Mycontract"
#fqn = "{{project-name}}::Mycontract"
2 changes: 1 addition & 1 deletion templates/blank/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# {{project-name}}

## Usage
It's recommend to install
It's recommended to install
[cargo-odra](https://github.com/odradev/cargo-odra) first.

### Build
Expand Down
4 changes: 2 additions & 2 deletions templates/full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
#odra_dependency

[[bin]]
name = "build_contract"
name = "{{project-name}}_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "build_schema"
name = "{{project-name}}_build_schema"
path = "bin/build_schema.rs"
test = false

Expand Down
2 changes: 1 addition & 1 deletion templates/full/Odra.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[[contracts]]
name = "flipper"
name = "Flipper"
fqn = "{{project-name}}::Flipper"
2 changes: 1 addition & 1 deletion templates/full/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# {{project-name}}

## Usage
It's recommend to install
It's recommended to install
[cargo-odra](https://github.com/odradev/cargo-odra) first.

### Build
Expand Down
2 changes: 1 addition & 1 deletion templates/full/src/flipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Flipper {
impl Flipper {
/// Odra constructor.
///
/// Initializes the contract with the value of value.
/// Initializes the contract.
pub fn init(&mut self) {
self.value.set(false);
}
Expand Down
15 changes: 10 additions & 5 deletions templates/module.rs.template
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use odra::prelude::*;
use odra::{Module, Variable, ContractEnv};
use odra::Variable;

/// A #module_name module storage definition.
/// A module definition. Each module struct consists Variables and Mappings
/// or/and another modules.
#[odra::module]
pub struct #module_name {
env: Rc<ContractEnv>,
/// The module itself does not store the value,
/// it's a proxy that writes/reads value to/from the host.
value: Variable<bool>,
}

/// Module entrypoints implementation.
/// Module implementation.
///
/// To generate entrypoints,
/// an implementation block must be marked as #[odra::module].
#[odra::module]
impl #module_name {
/// #module_name constructor.
/// Initializes the contract with the value of value.
/// Initializes the contract.
pub fn init(&mut self) {
self.value.set(false);
}
Expand Down
1 change: 1 addition & 0 deletions templates/workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
8 changes: 8 additions & 0 deletions templates/workspace/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

Changelog for `{{project-name}}`.

## [0.1.0] - {{date}}
### Added
- `flipper` module.
- `flapper` module.
16 changes: 16 additions & 0 deletions templates/workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[workspace]
members = [
"flipper",
"flapper",
]
resolver = "2"

[workspace.dependencies]
#odra_dependency

[profile.release]
codegen-units = 1
lto = true

[profile.dev.package."*"]
opt-level = 3
7 changes: 7 additions & 0 deletions templates/workspace/Odra.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[contracts]]
name = "Flipper"
fqn = "flipper::Flipper"

[[contracts]]
name = "Flapper"
fqn = "flapper::Flapper"
31 changes: 31 additions & 0 deletions templates/workspace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# {{project-name}}

## Usage
It's recommended to install
[cargo-odra](https://github.com/odradev/cargo-odra) first.

### Build

```
$ cargo odra build
```
To build a wasm file, you need to pass the -b parameter.
The result files will be placed in `${project-root}/wasm` directory.

```
$ cargo odra build -b casper
```

### Test
To run test on your local machine, you can basically execute the command:

```
$ cargo odra test
```

To test actual wasm files against a backend,
you need to specify the backend passing -b argument to `cargo-odra`.

```
$ cargo odra test -b casper
```
7 changes: 7 additions & 0 deletions templates/workspace/flapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
.vscode
/target
Cargo.lock
.backend*
.builder*
/wasm
17 changes: 17 additions & 0 deletions templates/workspace/flapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "flapper"
version = "0.1.0"
edition = "2021"

[dependencies]
odra = { workspace = true }

[[bin]]
name = "flapper_build_contract"
path = "bin/build_contract.rs"
test = false

[[bin]]
name = "flapper_build_schema"
path = "bin/build_schema.rs"
test = false
4 changes: 4 additions & 0 deletions templates/workspace/flapper/bin/build_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]
#![no_main]
#![allow(unused_imports, clippy::single_component_path_imports)]
use flapper;
10 changes: 10 additions & 0 deletions templates/workspace/flapper/bin/build_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use odra::contract_def::ContractBlueprint;

extern "Rust" {
fn module_schema() -> ContractBlueprint;
}

fn main() {
let schema = unsafe { module_schema() };
println!("{:#?}", schema);
}
8 changes: 8 additions & 0 deletions templates/workspace/flapper/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::env;

pub fn main() {
println!("cargo:rerun-if-env-changed=ODRA_MODULE");
let module = env::var("ODRA_MODULE").unwrap_or_else(|_| "".to_string());
let msg = format!("cargo:rustc-cfg=odra_module=\"{}\"", module);
println!("{}", msg);
}
Loading

0 comments on commit 37650a3

Please sign in to comment.