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.