
# GRPC example

Before running the example make sure you have built the `cardano-wasm` library by following the process described in the readme file of the [`cardano-wasm` folder](../..).

## Running the GRPC example

To run the example in this folder:

1.  Run an instance of the `cardano-node` with the GRPC server enabled and put the socket file for the GRPC server in the root folder of this repo with the name `rpc.socket`. (You can put it somewhere else, but you will have to update the `envoy-conf.yaml` function later.)

>[!NOTE]
>You can run `cardano-testnet cardano --enable-grpc` and then export `CARDANO_NODE_SOCKET_PATH` variable as shown on the screen.
>Then you can create a symlink to the node socket:
>```bash
>ln -sf $(dirname $CARDANO_NODE_SOCKET_PATH)/rpc.sock $(git rev-parse --show-toplevel)/rpc.socket
>```

2.  Generate the JS GRPC client bundle `cardano_node_grpc_web_pb.js` from the GRPC server proto files by running (This will generate it under the `result` folder):
    ```bash
    nix build .#proto-js-bundle
    ```

3.  Copy the generated `cardano_node_grpc_web_pb.js` file to the `grpc-example` subfolder.
    ```bash
    cp result/cardano_node_grpc_web_pb.js cardano-wasm/examples/grpc/
    ```

4.  Copy the generated `cardano-wasm.wasm` file to this subfolder. You can find its location using:
    ```bash
    cp "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)" cardano-wasm/examples/grpc/
    ```

5.  Copy the generated `cardano-wasm.js` file (generated by the `post-link.mjs` command in the section above) to this subfolder.
    ```bash
    cp cardano-wasm.js cardano-wasm/examples/grpc/
    ```

6.  Copy the wrapper files from the `lib-wrapper` folder into this subfolder.
    ```bash
    cp -r cardano-wasm/lib-wrapper/* cardano-wasm/examples/grpc/
    ```

7.  Run `envoy -c envoy-conf.yaml` from this subfolder.
    ```bash
    (cd cardano-wasm/examples/grpc ; envoy -c envoy-conf.yaml)
    ```

8.  Open your web browser and navigate to `http://localhost:8080/`. You should see a page titled `Test Output` with the results of the test.


