Automatic JavaScript IDL generation from candid

Discussion | Support

It would be very handy for Azle developers to be able to automatically generate TypeScript types and candid IDLs automatically based on a candid file. I have been looking into didc to see if it can accomplish this, and it almost does. The TypeScript types are great and generated exactly how we would need them, but the IDLs that are generated are only used in the generated idlFactory and not directly exposed to the developer.

For example if I take the ic.did from the management canister and run

didc ic.did -t js

I get an idlFactory that has all of the IDLs defined but they are scoped such that I can’t access them.

If we take a look at a simplified version that just has bitcoin_get_balance, this is what I get

export const idlFactory = ({ IDL }) => {
    const bitcoin_network = IDL.Variant({
        mainnet: IDL.Null,
        testnet: IDL.Null
    });
    const bitcoin_address = IDL.Text;
    const bitcoin_get_balance_args = IDL.Record({
        network: bitcoin_network,
        address: bitcoin_address,
        min_confirmations: IDL.Opt(IDL.Nat32)
    });
    const satoshi = IDL.Nat64;
    const bitcoin_get_balance_result = satoshi;
    return IDL.Service({
        bitcoin_get_balance: IDL.Func(
            [bitcoin_get_balance_args],
            [bitcoin_get_balance_result],
            []
        )
    });
};

But what I would like to produce is something like this

export const bitcoin_network = IDL.Variant({
    mainnet: IDL.Null,
    testnet: IDL.Null
});
export const bitcoin_address = IDL.Text;
export const bitcoin_get_balance_args = IDL.Record({
    network: bitcoin_network,
    address: bitcoin_address,
    min_confirmations: IDL.Opt(IDL.Nat32)
});
export const satoshi = IDL.Nat64;
export const bitcoin_get_balance_result = satoshi;

export const idlFactory = ({ IDL }) => {
    return IDL.Service({
        bitcoin_get_balance: IDL.Func(
            [bitcoin_get_balance_args],
            [bitcoin_get_balance_result],
            []
        )
    });
};

Does anyone know if didc or some other tool can do this? Or would it be possible to change didc to output the js bindings like this?

1 Like

It should be easy to do by rearranging the code a bit, particularly this line: candid/rust/candid_parser/src/bindings/javascript.rs at master · dfinity/candid · GitHub.

Just curious, why do you need access to this type objects? The type script binding should give you type information already?

I am hoping to be able to use those types for the first param of IDL.encode and IDL.decode