Warning
This repository is deprecated and will remain available only as a historical snapshot. Active development has moved to dusk-network/forge. Please use the Dusk Network repository for current code, issues, and pull requests.
Dusk Forge is a framework designed to simplify the development of smart contracts for the Dusk blockchain. It provides macros to reduce boilerplate, allow developers to focus on implementing core business logic.
The main feature of Dusk Forge is the #[contract] attribute macro, which
automates boilerplate generation. Here's how to get started:
Add dusk_forge as a dependency to your project:
cargo add dusk_forge#![no_std]
use dusk_forge::contract;
#[contract]
pub mod counter {
pub struct Counter {
value: i64,
}
impl Counter {
pub fn new() -> Self {
Self { value: 0 }
}
pub fn read_value(&self) -> i64 {
self.value
}
pub fn increment(&mut self) {
let value = self.value + 1;
self.value = value;
}
}
}The #[contract] macro transforms the module into a contract-ready state by:
- Generating a
static mut STATEfor the contract's state:
pub(crate) static mut STATE: Counter = Counter { value: 0 };- Wrapping public methods with
no_manglefunctions for Dusk VM compatibility:
#[no_mangle]
pub unsafe fn read_value(arg_len: u32) -> u32 {
dusk_core::abi::wrap_call(arg_len, |(): ()| counter::STATE.read_value())
}
#[no_mangle]
pub unsafe fn increment(arg_len: u32) -> u32 {
dusk_core::abi::wrap_call(arg_len, |(): ()| counter::STATE.increment())
}#![no_std]
use dusk_forge::contract;
#[contract]
pub mod complex_contract {
pub struct AccountManager {
accounts: core::collections::BTreeMap<u64, i64>,
}
impl AccountManager {
pub fn new() -> Self {
Self {
accounts: core::collections::BTreeMap::new(),
}
}
pub fn add_account(&mut self, id: u64, balance: i64) {
self.accounts.insert(id, balance);
}
pub fn get_balance(&self, id: u64) -> Option<i64> {
self.accounts.get(&id).cloned()
}
}
}Dusk Force includes tests for macro transformations and tests for individual components. Run tests using:
cargo testTo see the release history for this crate, please see the CHANGELOG file.
This code is licensed under the Mozilla Public License Version 2.0 (MPL-2.0). Please see the LICENSE for further details.
The included illustration is created by Regisha Dauven and is used with exclusive permission. Redistribution, modification, or reuse of the illustration by third parties is prohibited without explicit permission from the creator.
This repository is archived. Contributions should go to dusk-network/forge.
