Pass constructor arguments in dfx.json?

Is it possible to pass a default constructor argument for my canister into dfx.json, so that when I simply call dfx deploy, that argument is passed in without specifying it?

Example:
“mycanister”:{
“main”:“src/mycanister.mo”,
“type”:“motoko”,
“argument”:"(12345)"
}

Is this something that could be added in the future?

2 Likes

Do we have a documentation on all the possible entries of dfx.json and what we can achieve with different entries in dfx.json.

Please can someone point us to the documentation/resource if any?

@chiedo was also looking and found this.

1 Like

I agree that document(Customize the front-end :: Internet Computer) helps.

But it describes less number of options out of all the options that can be passed to dfx.json
I need info about the all the options.

Thanks

I don’t think it’s documented but you should be able to figure it out from here:

This helps, thank you.
But please someone document as it helps many.

Did we ever figure out exactly what the argument looks like and if this works? And can it be specified by network?

1 Like

I just had a glance over the definition, and I have to disappoint you. There is an argument field per canister (defined here), but it says This field defines an additional argument to pass to the Motoko compiler when building the canister. and it can’t be specified per network.

While not quite what was asked, the zx “CLI devops scripting” tool might be useful to do this and some other things (like automatically switch which system-wide networks.json configuration is used (@jorgenbuilder)).

Eg:

dfxd.mjs

#!/usr/bin/env z
import { argv, chalk } from "zx";
import { spawnSync } from "child_process"

console.info(chalk.bgBlue("restarting dfx clean"))
// stop currently any currently running 
await $`dfx stop`
// can be done other ways (https://github.com/google/zx/discussions/371)
spawnSync(
    'dfx',
    ['start', '--clean', '--background'],
    { 
        detached: true,
        stdio: 'ignore' // if you want to send output elsewhere
    }
)

if (argv.argDeploy) {
    console.info(chalk.bgGreen("\ndeploying with arg!\n"))
    await $`dfx depoy acanister --argument '( record { invokePerpetualProtocol = true; numOfDerivations = 1_001; etc etc } )'`
} else {
    console.info(chalk.bgCyan("\nnot deploying with arg!\n"))
    await $`dfx deploy acanister`
}

console.info(chalk.bgBlue("\nCanister deployed...\n"))

And in a console:

> $zx ./dfxd.mjs --argDeploy

If you do end up using zx, be aware it does its own string escaping which will definitely interfere if you want to use template literals to build compound expressions involving dfx. A simple way to bypass this is add:

$.quote = (...all) => all

To the top of your script.

Is there by now an option available to specify deploy arguments in the dfx.json file? Something like what was described here.

No, still not available

1 Like

Linking, is this available now or planned? The comment above mine makes it sound like this functionality is available: Deployment Arguments via dfx.json - #6 by lastmjs

As of 0.17.0 it is available as canisters.<canister name>.init_arg

2 Likes

Would be useful to have init_arg_file as well Option to set init arg file in dfx.json · dfinity/sdk · Discussion #3619 · GitHub

3 Likes