> For the complete documentation index, see [llms.txt](https://docs.cartesi.io/llms.txt)

---
id: calculator
title: Build a Calculator Application
resources:
  - url: https://github.com/Mugen-Builders/calculator
    title: Source code for the Calculator App
---

In this tutorial, we will build a simple Calculator application to illustrate how requests are sent and processed within Cartesi Rollups Infrastructure.

We provide JavaScript, Python, Rust, Go, and C++ implementations so you can use your preferred backend language.

## Set up your environment

Install these to set up your environment for quick building:

- Cartesi CLI: A simple tool for building applications on Cartesi. [Install Cartesi CLI for your OS of choice](../development/installation.md).

- Docker Desktop 4.x: The tool you need to run the Cartesi Machine and its dependencies. [Install Docker for your OS of choice](https://www.docker.com/products/docker-desktop/).

## Create the backend application

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

To create a calculator backend, run the template command for your language:

<Tabs>
  <TabItem value="JavaScript" label="JavaScript" default>
<pre><code>

```shell
cartesi create calculator --template javascript
```

</code></pre>
</TabItem>

<TabItem value="Python" label="Python" default>
<pre><code>

```shell
cartesi create calculator --template python
```

</code></pre>
</TabItem>

<TabItem value="Rust" label="Rust" default>
<pre><code>

```shell
cartesi create calculator --template rust
```

</code></pre>
</TabItem>

<TabItem value="Go" label="Go" default>
<pre><code>

```shell
cartesi create calculator --template go
```

</code></pre>
</TabItem>

<TabItem value="C++" label="C++" default>
<pre><code>

```shell
cartesi create calculator --template cpp
```

</code></pre>
</TabItem>
</Tabs>

This creates a `calculator/` directory with all required files.
Your backend entry point depends on language (`src/index.js`, `dapp.py`, `src/main.rs`, `main.go`, or `src/main.cpp`).

## Review the default backend template

Before implementing the calculator logic, review the default rollup request loop generated by the templates:

import RequestHandlingJS from '../development/snippets/request_handling_js.md';
import RequestHandlingPY from '../development/snippets/request_handling_py.md';
import RequestHandlingRS from '../development/snippets/request_handling_rs.md';
import RequestHandlingGO from '../development/snippets/request_handling_go.md';
import RequestHandlingCPP from '../development/snippets/request_handling_cpp.md';

<Tabs>
  <TabItem value="JavaScript" label="JavaScript" default>
<pre><code>

<RequestHandlingJS />

</code></pre>
</TabItem>

<TabItem value="Python" label="Python" default>
<pre><code>

<RequestHandlingPY />

</code></pre>
</TabItem>

<TabItem value="Rust" label="Rust" default>
<pre><code>

<RequestHandlingRS />

</code></pre>
</TabItem>

<TabItem value="Go" label="Go" default>
<pre><code>

<RequestHandlingGO />

</code></pre>
</TabItem>

<TabItem value="C++" label="C++" default>
<pre><code>

<RequestHandlingCPP />

</code></pre>
</TabItem>
</Tabs>

## Build the backend application

For this tutorial, we parse math expressions from advance payloads and emit calculation results as notices.
Install the minimal dependencies for your language:

<Tabs>
  <TabItem value="JavaScript" label="JavaScript" default>
<pre><code>

```shell
npm add expr-eval
```

</code></pre>
</TabItem>

<TabItem value="Python" label="Python" default>
<pre><code>

```shell
cat > requirements.txt << 'EOF'
requests==2.32.5
py_expression_eval==0.3.14
EOF
```

</code></pre>
</TabItem>

<TabItem value="Rust" label="Rust" default>
<pre><code>

```shell
cargo add meval hex
```

</code></pre>
</TabItem>

<TabItem value="Go" label="Go" default>
<pre><code>

```shell
go get github.com/Knetic/govaluate
```

</code></pre>
</TabItem>

<TabItem value="C++" label="C++" default>
<pre><code>

```shell
# No extra package needed for this tutorial snippet.
# The generated C++ template already includes httplib + picojson.
```

</code></pre>
</TabItem>
</Tabs>

For example, an advance request to the backend with payload `“1+2”` should emit a notice with the response `“3”`.

## Implement the application logic

Copy the snippet for your language and replace the contents of your local backend entry point file:

import CalculatorJS from './snippets/calculator-js.md';
import CalculatorPY from './snippets/calculator-py.md';
import CalculatorRS from './snippets/calculator-rs.md';
import CalculatorGO from './snippets/calculator-go.md';
import CalculatorCPP from './snippets/calculator-cpp.md';

<Tabs>
  <TabItem value="JavaScript" label="JavaScript" default>
<pre><code>

<CalculatorJS />

</code></pre>
</TabItem>

<TabItem value="Python" label="Python" default>
<pre><code>

<CalculatorPY />

</code></pre>
</TabItem>

<TabItem value="Rust" label="Rust" default>
<pre><code>

<CalculatorRS />

</code></pre>
</TabItem>

<TabItem value="Go" label="Go" default>
<pre><code>

<CalculatorGO />

</code></pre>
</TabItem>

<TabItem value="C++" label="C++" default>
<pre><code>

<CalculatorCPP />

</code></pre>
</TabItem>
</Tabs>

With Docker running, “build your backend” application by running:

```shell
cartesi build
```

“Building” in this context installs the libraries in the `requirements.txt`, compiles your application into RISC-V architecture, and consequently builds a Cartesi machine that contains your backend application.

The anvil node can now run your application.

To run your application, enter the command:

```shell
cartesi run
```

### Sending inputs with the CLI

We can send inputs to your application with a custom JavaScript frontend, Cast, or Cartesi CLI.

To send a string encoded input to your application, run the below command:

```shell
cartesi send "1+2"
```

Example: Send `1+2` as an input to the application.

```shell
(base) user@user-MacBook-Pro calculator % cartesi send "1 + 2"
(node:64729) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
✔ Input sent: 0x1866ab903f8fa5bd712fc2d322b8c0ba119bb31d30885a06e0fe6005ff079ff2
```

<!-- <video width="100%" controls poster="/static/img/v1.3/calculatorPoster.png">
    <source src="/videos/Sunodo_Send.mp4" type="video/mp4" />
    Your browser does not support video tags.
</video> -->

## Retrieving outputs from the application

The `cartesi send generic` sends a request to the calculator application to process the computation `(1 + 2)`, this is computed and a payload (notice) containing the result is sent to the Rollup Server's `/notice` endpoint.

:::note querying notices
Notice payloads will be returned in hexadecimal format; developers will need to decode these to convert them into plain text.
:::

We can query these notices using the JSON-RPC server running on `http://127.0.0.1:6751/rpc` or with a custom frontend client.

You can retrieve all notices sent to the rollup server by executing this request on a terminal:

```bash
curl -X POST http://127.0.0.1:6751/rpc \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "cartesi_listOutputs",
    "params": {
      "application": "calculator",
      "limit": 10,
      "offset": 0
    },
    "id": 1
  }'
```

Alternatively you can make a post request to the JsonRPC server like below:

```javascript
const response = await fetch("http://127.0.0.1:6751/rpc", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    jsonrpc: "2.0",
    method: "cartesi_listOutputs",
    params: {
      application: "calculator",
      limit: 10,
      offset: 0
    },
    id: 1
  })
});

const result = await response.json();

const outputs = result?.result?.data ?? [];

for (const output of outputs) {
  const decoded = output.decoded_data;
  console.log("Output payload:", decoded);
}
```

You can also [query a notice based on its input index](../development/query-outputs.md#query-a-single-notice-or-voucher).

Congratulations, you have successfully built a dApp on Cartesi Rollups!

:::info Repo Link
   You can access the complete project implementation [here](https://github.com/Mugen-Builders/docs_examples/tree/main/calculator)!
:::