#internet-computer #canister #cdk #fleek

ic-kit

Testable Canister Developer Kit for the Internet Computer

5 releases

0.4.3 Oct 4, 2021
0.4.2 Sep 13, 2021
0.4.1 Sep 8, 2021
0.4.0 Sep 7, 2021
0.3.0 Sep 4, 2021

#127 in Testing

Download history 75/week @ 2021-09-02 120/week @ 2021-09-09 53/week @ 2021-09-16 20/week @ 2021-09-23 85/week @ 2021-09-30 74/week @ 2021-10-07 22/week @ 2021-10-14

102 downloads per month

Apache-2.0

78KB
2K SLoC

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);
    }
}

Dependencies

~3.5–6.5MB
~143K SLoC