Skip to content

Vara-Lab/mini-sails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini-sails

mini-sails is a framework that works on sails-rs, allowing easy construction of smart contracts without having to worry about advanced concepts, state management, etc.

📋 characteristics

  • Multiple Services: build different services in the same smart contract.
  • Cross-service state access: access the service state of other services.
  • Change service name: Change the final name of your service in your smart contract.
  • Events: broadcast events from each of your services.
  • Private methods: You can assign private methods.

Macros

mini-sails has a variety of macros which facilitate the development of contracts for Vara Network:

  • #[sails_contract]: This macro declares a contract, which is responsible for handling all the construction of the contract to sails-rs, service management, status with RefCell, etc.
  • #[sails_state]: This macro indicates that a struct will be used as state, allowing #[sails_contract] to be both the state and the service to be built.
  • #[sails_constructor]: It indicates the service constructor as well as the program constructor how to initialize the service state.
  • #[sails_command]: Indicates if a method is a command.
  • #[sails_query]: Indicates if a method is a command.
  • #[sails_event]: Declares an enum as a contract event.
  • #[sails_io]: Set codec macros for input and output for contract types.

🎯 How to use mini-sails

Simple contract

use sails_rs::prelude::*;
use mini_sails::*;

#[sails_contract]
pub mod MyContract {
    #[sails_state]
    pub struct Counter {
        value: u64,
    }
    
    impl Counter {
        #[sails_constructor]
        pub fn new() -> Self {
            // Use the struct state name instead of Self
            Counter { value: 0 }
        }
        
        #[sails_command]
        pub fn increment(&mut self) -> u64 {
            // change the state of the service
            self.increment_one();
            self.value
        }
        
        #[sails_query]
        pub fn get_value(&self) -> u64 {
            // return the value of the state
            self.value
        }

        // private method
        pub fn increment_one(&mut self) {
            self.value += 1;
        }
    }
}

Payable contract (receive tokens)

use sails_rs::prelude::*;
use mini_sails::*;

#[sails_contract(payable)]
pub mod Vault {
    #[sails_state]
    #[derive(Default)]
    pub struct VaultState {
        total_tokens: u128
    }
    
    impl Counter {
        #[sails_constructor]
        pub fn new() -> Self {
            VaultState::default()
        }
        
        #[sails_command]
        pub fn increment(&mut self) -> u64 {
            // change the state of the service
            let value = Syscall:message_value();
            self.value += value;
            self.value
        }
        
        #[sails_query]
        pub fn get_value(&self) -> u64 {
            // return the value of the state
            self.value
        }
    }
}

Multi-service

#[sails_contract]
pub mod DeFi {
    #[sails_state]
    pub struct Balance {
        total: u128,
    }
    
    #[sails_state]
    pub struct Staking {
        staked: u128,
    }
    
    impl Staking {
        #[sails_command]
        // Access to Balance state
        pub fn stake(&mut self, balance: &mut Balance, amount: u128) {
            // Cross-service state access
            balance.total -= amount;
            self.staked += amount;
        }
    }
}

About

framework for easily working with contracts for Vara Network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages