|
| 1 | +--- |
| 2 | +title: Migrating |
| 3 | +prev: false |
| 4 | +next: false |
| 5 | +--- |
| 6 | + |
| 7 | +This guide explains how to migrate your bindings generation from the previous tools like [dfx](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/) or [didc](https://internetcomputer.org/docs/building-apps/interact-with-canisters/candid/candid-tools#didc) to `@icp-sdk/bindgen`. |
| 8 | + |
| 9 | +## From dfx (Rust canister) |
| 10 | + |
| 11 | +### Before |
| 12 | + |
| 13 | +If you were using `dfx` to generate bindings, your configuration was likely to be like this: |
| 14 | + |
| 15 | +```json title="dfx.json" |
| 16 | +{ |
| 17 | + "canisters": { |
| 18 | + "hello-world-backend": { |
| 19 | + "candid": "src/hello-world-backend/hello-world-backend.did", |
| 20 | + "package": "hello-world-backend", |
| 21 | + "type": "rust" |
| 22 | + }, |
| 23 | + ... |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +And you were generating bindings with the following command: |
| 29 | + |
| 30 | +```bash |
| 31 | +dfx generate |
| 32 | +``` |
| 33 | + |
| 34 | +This was typically writing the bindings' files in the `src/declarations/hello-world-backend` directory, unless you configured the `canisters.hello-world-backend.declarations` field. |
| 35 | + |
| 36 | +### After |
| 37 | + |
| 38 | +Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following command (typically in a script in the `package.json` file of the frontend): |
| 39 | + |
| 40 | +```bash |
| 41 | +icp-bindgen --did-file ./src/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend |
| 42 | +``` |
| 43 | + |
| 44 | +> In case you configured the `canisters.hello-world-backend.declarations` field, you can pass the `canisters.hello-world-backend.declarations.output` value to the `--out-dir` flag. Note that in `@icp-sdk/bindgen`, there's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts). |
| 45 | +
|
| 46 | +This will generate the bindings in the `src/hello-world-frontend/backend` directory. See the [Bindings Structure](./structure) page for more information on the generated files. |
| 47 | + |
| 48 | +> The same can be applied for a canister configured as `type: custom` in the `dfx.json` file. |
| 49 | +
|
| 50 | +## From dfx (Motoko canister) |
| 51 | + |
| 52 | +### Before |
| 53 | + |
| 54 | +If you were using `dfx` to generate bindings, your configuration was likely to be like this: |
| 55 | + |
| 56 | +```json title="dfx.json" |
| 57 | +{ |
| 58 | + "canisters": { |
| 59 | + "hello-world-backend": { |
| 60 | + "main": "src/hello-world-backend/main.mo", |
| 61 | + "type": "motoko" |
| 62 | + }, |
| 63 | + ... |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +And you were generating bindings with the following command: |
| 69 | + |
| 70 | +```bash |
| 71 | +dfx generate |
| 72 | +``` |
| 73 | + |
| 74 | +This was typically writing the bindings' files in the `src/declarations/hello-world-backend` directory, unless you configured the `canisters.hello-world-backend.declarations` field. |
| 75 | + |
| 76 | +### After |
| 77 | + |
| 78 | +In Motoko, the Candid declaration file (`.did`) is generated by `dfx` at build time. It's typically written in the `.dfx/<network-name>/canisters/hello-world-backend/hello-world-backend.did` file. |
| 79 | + |
| 80 | +Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following commands (typically in a script in the `package.json` file of the frontend): |
| 81 | + |
| 82 | +```bash |
| 83 | +dfx build --check hello-world-backend # can be skipped if you already built the canister |
| 84 | +icp-bindgen --did-file ./.dfx/<network-name>/canisters/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend |
| 85 | +``` |
| 86 | + |
| 87 | +> During the build, `dfx` injects a `$DFX_NETWORK` environment variable that contains the network name, which you can use to construct the path to the `.did` file. |
| 88 | +
|
| 89 | +> In case you configured the `canisters.hello-world-backend.declarations` field, you can pass the `canisters.hello-world-backend.declarations.output` value to the `--out-dir` flag. Note that in `@icp-sdk/bindgen`, there's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts). |
| 90 | +
|
| 91 | +This will generate the bindings in the `src/hello-world-frontend/backend` directory. See the [Bindings Structure](./structure) page for more information on the generated files. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +For a more sophisticated approach, you can configure the `canisters.hello-world-backend.declarations` field to write the `.did` file to a directory of your choice and then use the `--did-file` flag to point to it. For example: |
| 96 | + |
| 97 | +```json title="dfx.json" |
| 98 | +{ |
| 99 | + "canisters": { |
| 100 | + "hello-world-backend": { |
| 101 | + "main": "src/hello-world-backend/main.mo", |
| 102 | + "type": "motoko", |
| 103 | + "declarations": { |
| 104 | + "bindings": ["did"], |
| 105 | + "output": "candid" |
| 106 | + } |
| 107 | + }, |
| 108 | + ... |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +And then you can run the following commands: |
| 114 | + |
| 115 | +```bash |
| 116 | +dfx generate --check hello-world-backend |
| 117 | +icp-bindgen --did-file ./candid/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend |
| 118 | +``` |
| 119 | + |
| 120 | +## From didc |
| 121 | + |
| 122 | +### Before |
| 123 | + |
| 124 | +If you were using `didc` to generate bindings, you were likely running the following commands: |
| 125 | + |
| 126 | +```bash |
| 127 | +didc bind --target js ./service.did > ./service.did.js |
| 128 | +didc bind --target ts ./service.did > ./service.did.d.ts |
| 129 | +``` |
| 130 | + |
| 131 | +### After |
| 132 | + |
| 133 | +Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following command: |
| 134 | + |
| 135 | +```bash |
| 136 | +icp-bindgen --did-file ./service.did --out-dir ./service |
| 137 | +``` |
| 138 | + |
| 139 | +This will generate the bindings in the `service` directory. See the [Bindings Structure](./structure) page for more information on the generated files. |
| 140 | + |
| 141 | +> Note: There's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts). |
0 commit comments