Skip to content
This repository was archived by the owner on Jul 11, 2026. It is now read-only.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dusk Forge

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 Illustration

Repository Documentation

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.

⚠️ This crate requires the nightly Rust compiler.

Usage

The main feature of Dusk Forge is the #[contract] attribute macro, which automates boilerplate generation. Here's how to get started:

Installation

Add dusk_forge as a dependency to your project:

cargo add dusk_forge

Basic Example: Counter Contract

#![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;
        }
    }
}

What Happes Under the Hood?

The #[contract] macro transforms the module into a contract-ready state by:

  1. Generating a static mut STATE for the contract's state:
pub(crate) static mut STATE: Counter = Counter { value: 0 };
  1. Wrapping public methods with no_mangle functions 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())
}

Advanced Example: Struct with Complex State

#![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()
        }
    }
}

Testing

Dusk Force includes tests for macro transformations and tests for individual components. Run tests using:

cargo test

Release History

To see the release history for this crate, please see the CHANGELOG file.

License

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.

Contribute

This repository is archived. Contributions should go to dusk-network/forge.

About

A smart contract development framework for the Dusk

Resources

Stars

12 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages