Coder Social home page Coder Social logo

ink's Introduction

ink! - Parity's ink to write smart contracts

Linux Codecov Coveralls LoC
linux codecov coveralls loc

IMPORTANT NOTE: WORK IN PROGRESS! Do not expect this to be working.

ink! is an eDSL to write WebAssembly based smart contracts using the Rust programming language targeting Substrate blockchains.

For more information please visit the ink! tutorial.

Developer Documentation

ink_metadata ink_env ink_storage ink_lang ink_prelude

Interaction with Substrate

Substrate's Framework for Runtime Aggregation of Modularised Entities (FRAME) contains the contracts pallet, which provides a generic smart contract interface for Wasm blobs. It takes care of e.g. state rent, storage, costs, etc..

ink! is a smart contract language which targets the interface exposed by contracts. As such, ink! smart contracts are compiled to Wasm.

Scripts

Use the scripts provided under scripts directory in order to run checks on either the workspace or all examples. Please do this before pushing work in a PR.

Examples

For building the example smart contracts found under examples you will need to have cargo-contract installed.

cargo install cargo-contract --force

Use the --force to ensure you are updated to the most recent cargo-contract version.

Build example contract and generate the contracts metadata

To build a single example and generate the contracts Wasm file, navigate to the root of the example smart contract and run:

cargo contract build

To generate the contract metadata (a.k.a. the contract ABI), run the following command:

cargo contract generate-metadata

You should now have an optimized <contract-name>.wasm file and an metadata.json file in the target folder of the contract.

For further information, please have a look at our smart contracts workshop.

Hello, World! - The Flipper

The Flipper contract is a simple contract containing only a single bool value that it can flip from true to false and vice versa and return the current state.

To create your own version of the flipper contract, you first need to initialize a new ink! project in your working directory.

cargo contract new flipper

Below you can see the code using the ink_lang version of ink!.

use ink_lang as ink;

#[ink::contract]
mod flipper {
    /// The storage of the flipper contract.
    #[ink(storage)]
    pub struct Flipper {
        /// The single `bool` value.
        value: bool,
    }

    impl Flipper {
        /// Instantiates a new Flipper contract and initializes `value` to `init_value`.
        #[ink(constructor)]
        pub fn new(init_value: bool) -> Self {
            Self {
                value: init_value,
            }
        }

        /// Instantiates a new Flipper contract and initializes `value` to `false` by default.
        #[ink(constructor)]
        pub fn default() -> Self {
            Self::new(false)
        }

        /// Flips `value` from `true` to `false` or vice versa.
        #[ink(message)]
        pub fn flip(&mut self) {
            self.value = !self.value;
        }

        /// Returns the current state of `value`.
        #[ink(message)]
        pub fn get(&self) -> bool {
            self.value
        }
    }

    /// Simply execute `cargo test` in order to test your contract using the below unit tests.
    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn default_works() {
            let flipper = Flipper::default();
            assert_eq!(flipper.get(), false);
        }

        #[test]
        fn it_works() {
            let mut flipper = Flipper::new(false);
            assert_eq!(flipper.get(), false);
            flipper.flip();
            assert_eq!(flipper.get(), true);
        }
    }
}

Place this code in the ./lib.rs file of your flipper contract and run cargo contract build && cargo contract generate-metadata to build your first ink! smart contract example.

Contribution

Visit our contribution guidelines for more information.

License

The entire code within this repository is licensed under the Apache License 2.0. Please contact us if you have questions about the licensing of our products.

ink's People

Contributors

ascjones avatar athei avatar bjornwgnr avatar cmichi avatar demi-marie avatar flyq avatar gnunicorn avatar iorveth avatar jaybutera avatar jckdotim avatar jordy25519 avatar kigawas avatar koushiro avatar liuchengxu avatar lsaether avatar pepyakin avatar riusricardo avatar robbepop avatar satoshi-kusumoto avatar satyamakgec avatar shawntabrizi avatar stefie avatar taskooh avatar tovarishfin avatar tripleight avatar xatmassacre avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.