# Dynamically create a canister from wasm module

**URL:** <https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060>\
**Category:** Developers\
**Created:** [September 7, 2021, 2:15pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060 "2021-09-07T14:15:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayazgv](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayazgv/32/1587_2.png) [@ayazgv](https://forum.dfinity.org/u/ayazgv)\
**Post date:** [September 7, 2021, 2:15pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/1 "2021-09-07T14:15:40Z")

</div>

how to dynamically create a canister from compiled wasm module with another canister using rust?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [September 7, 2021, 3:11pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/2 "2021-09-07T15:11:46Z")

</div>

The cycles wallet is a good reference for that

> **[GitHub - dfinity/cycles-wallet: DFINITY Cycles Wallet](https://github.com/dfinity/cycles-wallet)**
>
> DFINITY Cycles Wallet. Contribute to dfinity/cycles-wallet development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ayazgv](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayazgv/32/1587_2.png) [@ayazgv](https://forum.dfinity.org/u/ayazgv)\
**Post date:** [September 9, 2021, 2:05pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/3 "2021-09-09T14:05:52Z")

</div>

This forum is blocked me, I just don’t know why

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [September 9, 2021, 3:47pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/4 "2021-09-09T15:47:34Z")

</div>

It blocks pasted code pretty often, probably trying to avoid code execution attacks

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 5, 2023, 7:23am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/5 "2023-06-05T07:23:11Z")

</div>

Bro it is 1200 lines of code, people really need minimal examples for each case.  
Can you point out to the place where I can find `dynamically create a canister`

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 5, 2023, 8:11am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/6 "2023-06-05T08:11:23Z")

</div>

Creating a canister is [here](https://github.com/dfinity/cycles-wallet/blob/main/wallet/src/lib.rs#L673-L704), wasm gets installed [here](https://github.com/dfinity/cycles-wallet/blob/main/wallet/src/lib.rs#L750-L781)

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [June 5, 2023, 4:42pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/7 "2023-06-05T16:42:55Z")

</div>

I agree with @AliSci that this warrants docs

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 10, 2023, 1:33pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/8 "2023-06-10T13:33:54Z")

</div>

what is the difference between `wallet_create_canister128` and `wallet_create_canister`?

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 10, 2023, 3:10pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/9 "2023-06-10T15:10:46Z")

</div>

and how should I run the command?  
`dfx canister call wallet create_canister "??";`

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 12, 2023, 6:50am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/10 "2023-06-12T06:50:45Z")

</div>

> [@AliSci](#):
>
> what is the difference between `wallet_create_canister128` and `wallet_create_canister`?

No real difference. The one without `128` is a leftover from when cycles were `u64`. Internally it simply turns the amount of cycles into an `u128` and then calls `wallet_create_canister128`.

> [@AliSci](#):
>
> and how should I run the command?  
> `dfx canister call wallet create_canister "??";`

That depends on the interface of the `wallet` canister. Assuming you’re using the default wallet canister, then the interface is [here](https://github.com/dfinity/cycles-wallet/blob/main/wallet_ui/declarations/wallet/wallet.did#L164-L175). You could use e.g. `‘(record { cycles = 1000000000000; settings = record { controller = ?principal “\<your principal\>”}})’

But it’s probably easier to just use `dfx canister create`

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 12, 2023, 2:26pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/13 "2023-06-12T14:26:02Z")

</div>

```rs
use std::cell::RefCell;

use candid::Principal;
use ic_cdk::api;
use ic_cdk::api::call::CallResult;
use ic_cdk::api::management_canister::main::CanisterSettings;
use ic_cdk::export::candid::{
    candid_method, CandidType, check_prog, Deserialize, export_service, IDLProg, TypeEnv,
};

#[derive(Default)]
struct WalletWASMBytes(Option<serde_bytes::ByteBuf>);

/// The wallet (this canister's) name.
#[derive(Default)]
struct WalletName(pub(crate) Option<String>);

thread_local! {
    static WALLET_NAME: RefCell<WalletName> = Default::default();
    static WALLET_WASM_BYTES: RefCell<WalletWASMBytes> = Default::default();
}

#[candid_method(query)]
#[ic_cdk::query]
fn my_canister_id() -> String {
    format!("{}", ic_cdk::id())
}

#[derive(CandidType, Deserialize)]
struct CreateResult {
    canister_id: Principal,
}

#[derive(CandidType, Deserialize)]
struct CanisterInstall {
    mode: InstallMode,
    canister_id: Principal,
    #[serde(with = "serde_bytes")]
    wasm_module: Vec<u8>,
    arg: Vec<u8>,
}

#[derive(CandidType, Deserialize)]
enum InstallMode {
    #[serde(rename = "install")]
    Install,
    #[serde(rename = "reinstall")]
    Reinstall,
    #[serde(rename = "upgrade")]
    Upgrade,
}

#[candid_method(update)]
#[ic_cdk::update]
async fn create_canister() -> String {
    #[derive(CandidType)]
    struct In {
        settings: Option<CanisterSettings>,
    }
    let settings = CanisterSettings {
        controllers: Some(vec![ic_cdk::api::id()]),
        compute_allocation: None,
        memory_allocation: None,
        freezing_threshold: None,
    };
    let in_arg = In {
        settings: Some(settings),
    };

    let (create_result, ): (CreateResult, ) = match api::call::call_with_payment128(
        Principal::management_canister(),
        "create_canister",
        (in_arg, ),
        400_000_000_000,
    )
        .await
    {
        Ok(x) => x,
        Err((code, msg)) => {
            return format!(
                "An error happened during the call: {}: {}",
                code as u8, msg
            );
        }
    };

    // wasm install

    let wasm_module = WALLET_WASM_BYTES.with(|wallet_bytes| match &wallet_bytes.borrow().0 {
        Some(o) => o.clone().into_vec(),
        None => {
            ic_cdk::trap("No wasm module stored.");
        }
    });

    let install_config = CanisterInstall {
        mode: InstallMode::Install,
        canister_id: ic_cdk::api::id(),
        wasm_module: wasm_module.clone(),
        arg: b" ".to_vec(),
    };

    let (install_res, ): (CreateResult, ) = match api::call::call(
        Principal::management_canister(),
        "install_code",
        (install_config, ),
    )
        .await
    {
        Ok(x) => x,
        Err((code, msg)) => {
            return format!(
                "An error happened during the call: {}: {}",
                code as u8, msg
            );
        }
    };
    format!("{}", create_result.canister_id)
}

```

I build this now, but I don’t know how this wasm thing should work when I call the `create_canister` I got

```rs
Call was rejected:
Request ID: ccacd6fd84d19be86952a40d1d810b5798f3709934db2ecf244cb222ca849695
Reject code: 5
Reject text: Canister bw4dl-smaaa-aaaaa-qaacq-cai trapped explicitly: No wasm module stored.

```

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 12, 2023, 3:59pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/14 "2023-06-12T15:59:26Z")

</div>

You have to have a wasm module that you can install. You can either pass it in the function argument or you can upload it ahead of time (like your example is set up for), but you have to have something available to install

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 14, 2023, 5:57pm UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/15 "2023-06-14T17:57:38Z")

</div>

Now I am getting this error from the candid UI.

```rs
("An error happened during the call: 3: IC0302: Canister br5f7-7uaaa-aaaaa-qaaca-cai has no update method 'install_code'")

```

the should I make and `update` or a `query` named `install_code`?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 15, 2023, 7:25am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/16 "2023-06-15T07:25:17Z")

</div>

It’s on you to decide what you want. If you want an `install_code` function in your canister then go ahead and make it. It probably will have to be an update call since you want to modify state. Queries’ changes get discarded after execution

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [June 15, 2023, 7:55am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/17 "2023-06-15T07:55:41Z")

</div>

Or do you want to call the `install_code` method on the management canister?

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [June 19, 2023, 8:43am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/18 "2023-06-19T08:43:12Z")

</div>

I don’t know why would I want `install_code` or not I don’t fully understand the process here. I am building this minimal project to see how it work, and to see what are my options.

---

<div class="post-metadata">

**Author:** ![benji](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/benji/32/8069_2.png) [@benji](https://forum.dfinity.org/u/benji)\
**Post date:** [June 19, 2023, 9:30am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/19 "2023-06-19T09:30:23Z")

</div>

@Vivienne Is it true that using this method the wasm size can be 10MB? I understand that ingress is no longer the limiting factor here, but where exactly does this limitation come from?

---

<div class="post-metadata">

**Author:** ![benji](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/benji/32/8069_2.png) [@benji](https://forum.dfinity.org/u/benji)\
**Post date:** [June 19, 2023, 9:31am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/20 "2023-06-19T09:31:50Z")

</div>

Also, can the `wasm_module` be gzipped here?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 19, 2023, 9:39am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/21 "2023-06-19T09:39:19Z")

</div>

If the canister you’re installing to via the proxy method AND the two canisters live on the same subnet, then yes, you should be able to go up to 10MB WASM. The 10MB is for _unzipped_ wasm, but you can install it gzipped without any problems

---

<div class="post-metadata">

**Author:** ![abk](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/abk/32/5122_2.png) [@abk](https://forum.dfinity.org/u/abk)\
**Post date:** [June 19, 2023, 10:57am UTC](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060/22 "2023-06-19T10:57:06Z")

</div>

Actually we recently made a change that allows the Wasm module to be 30MB uncompressed as long as the code section is still under 10MB: [Merge branch 'abk/run-647-larger-wasms' into 'master' · dfinity/ic@a044776 · GitHub](https://github.com/dfinity/ic/commit/a044776ba)

[Next page](https://forum.dfinity.org/t/dynamically-create-a-canister-from-wasm-module/7060.md?page=2)
