15 releases (9 breaking)
new 0.9.1 | Apr 15, 2024 |
---|---|
0.8.1 | Mar 1, 2024 |
0.7.1 | Dec 13, 2023 |
0.7.0 | Nov 7, 2023 |
0.2.0 | Nov 23, 2022 |
#403 in Magic Beans
268 downloads per month
Used in odra-modules
200KB
4K
SLoC
Odra - Smart contracts for Casper Network.
Docs | Installation | Tutorials | Cargo Odra | Discord | Blog
Table of Contents
Usage
Use Cargo Odra to generate, build and test you code.
Example
use odra::prelude::*;
use odra::Var;
#[odra::module]
pub struct Flipper {
value: Var<bool>,
}
#[odra::module]
impl Flipper {
pub fn init(&mut self) {
self.value.set(false);
}
pub fn set(&mut self, value: bool) {
self.value.set(value);
}
pub fn flip(&mut self) {
self.value.set(!self.get());
}
pub fn get(&self) -> bool {
self.value.get_or_default()
}
}
#[cfg(test)]
mod tests {
use crate::flipper::FlipperHostRef;
use odra::host::{Deployer, NoArgs};
#[test]
fn flipping() {
let env = odra_test::env();
let mut contract = FlipperHostRef::deploy(&env, NoArgs);
assert!(!contract.get());
contract.flip();
assert!(contract.get());
}
}
Checkout our examples. It shows most of Odra features.
Tests
Before running tests make sure you have following packages installed:
- Rust toolchain (see rustup.rs) with
wasm32-unknown-unknown
target. cargo-odra
(see Cargo Odra)just
(see just)
Run tests:
$ just test
Links
Contact
Need some help? Write to contract@odra.dev.
lib.rs
:
A Rust library for writing smart contracts for the Casper Blockchain.
Example
The following example is a simple counter smart contract. The contract stores a single value, which can be incremented or decremented. The counter value can be initialized at contract creation time.
use odra::prelude::*;
#[odra::module]
struct Counter {
count: odra::Var<u32>,
}
#[odra::module]
impl Counter {
pub fn init(&mut self, count: u32) {
self.count.set(count);
}
pub fn increment(&mut self) {
self.count.set(self.count.get_or_default() + 1);
}
pub fn decrement(&mut self) {
self.count.set(self.count.get_or_default() - 1);
}
pub fn get_count(&self) -> u32 {
self.count.get_or_default()
}
}
Dependencies
~7.5MB
~160K SLoC