CLI tool that converts did declaration file to a Motoko Interface file?

Is there a tool that allows me to pop in a did interface file and then generates a Motoko interface file for interacting with that actor?

A basic example would take something like

// .did file
service : {
  ping: () -> (text);

and generate

module {
  public type Interface = actor {
    ping : shared () -> async (Text);

Extra credit: If I could point this tool at any canister on main net and it would both pull down the did file and generate the file for it, ideally in the directory that I execute the command in.

Over the past few weeks several times I’ve gone through the process of:

  • Identify canister I need to interact with
  • Look up a did file for that canister on the candid UI/ICP dashboard
  • Copy over just the APIs I need into a Motoko Interface file

This process could be sped up a ton if I could just run

./generateDidAndInterfaceForCanister <canister_id_on_main_net>

Tagging @chenyan, @dfx-json, and @skilesare in case this tool already exists :slight_smile:


Check out the IC Dashboard yo.

Also, - Canlista has an older version of this.

Maybe I’m missing something that you want…but you should be able to start with this and delete what you don’t need.

1 Like

didc bind a.did -t mo should be what you want. We are going to add some customization configs to the bindgen very soon.

To get the did file from canister id, you can use dfx canister metadata <canister-id> candid:service.

1 Like

Thanks this is great. I just had to scroll all the way to the bottom :sweat_smile:

@skilesare actually, it looks like some canisters have this, but others don’t

For example, some of the SNS canisters have it, others, like the one I was trying to reference don’t have Motoko declarations.

You can see here that even though the Candid, Rust, and other declarations are visible, the Motoko tab is greyed out.

So looking at the didc tool for this canister…

I tried this, and got the following error:

% didc bind snsSwapInterfaceDuplicate.did -t mo                          
thread 'main' panicked at 'float32 not supported in Motoko', rust/candid/src/bindings/
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It looks like there’s an issue with the didc command where it isn’t mapping float32 to Float? Maybe that’s why a Motoko interface file isn’t appearing for this canister?

Right, the underlying code is exactly the same. The problem is that Motoko doesn’t support float32 in Candid, so it cannot generate binding for that canister. It would be good to contact the author of that canister to update the interface. Otherwise, people cannot call that canister from Motoko.

That is the SNS swap canister. Pinging @lara for future reference to avoid using float32 if possible for compatibility