11 releases (6 breaking)

Uses old Rust 2015

0.8.1 May 24, 2022
0.8.0 Jan 11, 2022
0.7.0 Dec 14, 2021
0.6.0 Nov 9, 2021
0.1.0-alpha Sep 27, 2019

#30 in #derivation

26 downloads per month

MIT license

33KB
648 lines

mazzaroth-rs-derive

Derivation macros to derive the mazzaroth contract abi from a trait.

Add dependency

[dependencies]
mazzaroth-rs-derive = "0.1.0"

License

MIT


lib.rs:

Mazzaroth Derive Library

The Mazzaroth Derive Library is a rust library that defines the macros used to compile Mazzaroth Smart Contracts and generate the JSON ABI.

How to use

The first step to using this library is to include the necessary dependencies. The following 3 dependencies should be included in your Cargo.toml:

mazzaroth-rs mazzaroth-rs-derive mazzaroth-xdr

Every contract will have a similar base layout for the main function and the contract trait definition. main() is used as the entry point and has several important features. It will instantiate the contract, call a host function to retrieve function input, execute the function, and return a response.

Here is a basic Hello World contract example:

// must include the ContractInterface and mazzaroth_abi for compiling the macro
extern crate mazzaroth_rs;
extern crate mazzaroth_rs_derive;
use mazzaroth_rs::ContractInterface;
use mazzaroth_rs_derive::mazzaroth_abi;

// using specific external host modules
use mazzaroth_rs::external::{transaction, log};

#[no_mangle]
pub fn main() {
    // panic hook is set to call the host error log function when a panic occurs
    std::panic::set_hook(Box::new(mazzaroth_rs::external::errors::hook));

    // Creates a new instance of the ABI generated around the Contract
    let mut contract = HelloWorld::new(Hello {});

    // Use a host function to get arguments
    let args = transaction::arguments();

    // Execute calls one of the functions defined in the contract
    // Input for the function to call and it's params comes from the Runtime
    let response = contract.execute(&args).unwrap();

    // Provide return value through host call
    transaction::ret(response);
}

// mazzaroth_abi used to generate the contract from the trait during compilation
#[mazzaroth_abi(HelloWorld)]
pub trait HelloWorldContract {
    // hello() defined as a readonly function
    #[readonly]
    fn hello(&mut self) -> u32;
}

// Struct used to implement the contract trait
pub struct Hello {}

// Actual contract implementation
impl HelloWorldContract for Hello {
    fn hello(&mut self) -> u32 {
        log("Hello World!".to_string());
        14
    }
}

Dependencies

~2.5MB
~57K SLoC