Giter Club home page Giter Club logo

ic-kit's Introduction

IC Kit

Docs

This library provides an alternative to ic-cdk that can help developers write canisters and unit test them in their Rust code.

Install

Add this to your Cargo.toml

[dependencies]
ic-kit = "0.4.3"
ic-cdk = "0.3.1"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
async-std = { version="1.10.0", features = ["attributes"] }

Example Usage

use ic_kit::macros::*;
use ic_kit::{ic, Principal};

#[update]
fn whoami() -> Principal {
    ic::caller()
}

#[update]
async fn send_cycles(canister_id: Principal, cycles: u64) -> Result<(), String> {
    ic::call_with_payment(canister_id, "wallet_accept", (), cycles)
        .await
        .map_err(|(code, msg)| format!("Call failed with code={}: {}", code as u8, msg))
}

#[cfg(test)]
mod tests {
    use super::*;
    use ic_kit::{mock_principals, MockContext};

    #[test]
    fn test_whoami() {
        MockContext::new()
            .with_caller(mock_principals::alice())
            .inject();

        assert_eq!(whoami(), mock_principals::alice());
    }

    #[async_test]
    async fn test_send_cycles() {
        // Create a context that just consumes 1000 cycles from all the inter-canister calls and
        // returns "()" in response.
        let ctx = MockContext::new()
            .with_consume_cycles_handler(1000)
            .inject();

        // Init a watcher at this point that will track all of the calls made from now on.
        let watcher = ctx.watch();

        send_cycles(mock_principals::xtc(), 5000).await.unwrap();

        assert_eq!(watcher.cycles_consumed(), 1000);
        assert_eq!(watcher.cycles_sent(), 5000);
    }
}

ic-kit's People

Contributors

myse1f avatar qti3e 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.