Skip to content

Commit 97d4a8b

Browse files
committed
docs: restructured development section
1 parent f9eedde commit 97d4a8b

File tree

6 files changed

+199
-51
lines changed

6 files changed

+199
-51
lines changed

cartesi-rollups_versioned_docs/version-2.0/api-reference/backend/vouchers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import { stringToHex, encodeFunctionData, erc20Abi } from "viem";
5959
async function handle_advance(data) {
6060
console.log("Received advance request data " + JSON.stringify(data));
6161
const sender = data["metadata"]["msg_sender"];
62-
const erc20Token = "0x784f0c076CC55EAD0a585a9A13e57c467c91Dc3a", // Sample ERC20 token address
62+
const erc20Token = "0x784f0c076CC55EAD0a585a9A13e57c467c91Dc3a"; // Sample ERC20 token address
6363

6464
const call = encodeFunctionData({
6565
abi: erc20Abi,

cartesi-rollups_versioned_docs/version-2.0/development/building-and-deploying-an-application.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,6 @@ resources:
88
title: CartesiScan
99
---
1010

11-
## Creating the application
12-
13-
Cartesi CLI simplifies creating applications on Cartesi. To create a new application, run:
14-
15-
```shell
16-
cartesi create <application-name> --template <language>
17-
```
18-
19-
For example, create a Python project.
20-
21-
```shell
22-
cartesi create new-application --template python
23-
```
24-
25-
This command creates a `new-application` directory with essential files for your application development.
26-
27-
- `Dockerfile`: Contains configurations to build a complete Cartesi machine with your app's dependencies. Your backend code will run in this environment.
28-
29-
- `README.md`: A markdown file with basic information and instructions about your application.
30-
31-
- `dapp.py`: A Python file with template backend code that serves as your application's entry point.
32-
33-
- `requirements.txt`: Lists the Python dependencies required for your application.
34-
35-
Cartesi CLI has templates for the following languages – `cpp`, `cpp-low-level`, `go`, `javascript`, `lua`, `python`, `ruby`, `rust`, and `typescript`.
36-
37-
After creating your application, you can start codding your application by adding your logic to the `dapp.py` file.
38-
39-
:::note Building with Go?
40-
For Go applications on Cartesi, we recommend using [Rollmelette](https://github.com/rollmelette/rollmelette). It’s a high-level Go framework and an alternative template that simplifies development and enhances input management, providing a smoother and more efficient experience.
41-
:::
42-
4311
## Building the application
4412

4513
“Building” in this context compiles your application into RISC-V architecture and consequently builds a Cartesi machine containing your application. This architecture enables computation done by your application to be reproducible and verifiable.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
id: creating-an-application
3+
title: Creating an Application
4+
resources:
5+
- url: https://github.com/Calindra/nonodo
6+
title: NoNodo
7+
- url: https://cartesiscan.io/
8+
title: CartesiScan
9+
---
10+
11+
12+
13+
Cartesi CLI simplifies creating applications on Cartesi. To create a new application, run:
14+
15+
```shell
16+
cartesi create <application-name> --template <language>
17+
```
18+
19+
For example, create a Javascript project.
20+
21+
```shell
22+
cartesi create new-app --template javascript
23+
```
24+
25+
This command creates a `new-app` directory with essential files for your application development.
26+
27+
- `Dockerfile`: Contains configurations to build a complete Cartesi machine with your app's dependencies. Your backend code will run in this environment.
28+
29+
- `README.md`: A markdown file with basic information and instructions about your application.
30+
31+
- `src/index.js`: A javascript file with template backend code that serves as your application's entry point.
32+
33+
- `package.json`: A list of dependencies required for your application along with the name, version and description of your application.
34+
35+
Cartesi CLI has templates for the following languages – `cpp`, `cpp-low-level`, `go`, `javascript`, `lua`, `python`, `ruby`, `rust`, and `typescript`.
36+
37+
:::note Building with Go?
38+
For Go applications on Cartesi, we recommend using [Rollmelette](https://github.com/rollmelette/rollmelette). It’s a high-level Go framework and an alternative template that simplifies development and enhances input management, providing a smoother and more efficient experience.
39+
:::
40+
41+
## Implementing your application Logic
42+
43+
After creating your application, you can begin building your application by adding your logic to the index.js file. This file serves as the entry point of your application. While your project can include multiple files and directories, the default application file should remain the entry point of your application.
44+
45+
It’s important not to modify or remove existing code in index.js unless you fully understand its purpose, as doing so may prevent your application from functioning correctly. Instead, you are encouraged to extend the file by adding your own logic and implementations alongside the default code.
46+
47+
If your application needs to emit notices, vouchers, or reports, make sure to implement the corresponding logic within your codebase to properly handle these outputs. You can check out the respective pages for [Notice](../api-reference/backend/notices.md), [Vouchers](../api-reference/backend/vouchers.md) or [Report](../api-reference/backend/reports.md) for better understanding of what they are and how to implement them.
48+
49+
The default application template includes two primary functions; handle_advance and handle_inspect. These act as entry points for different types of operations within your application. The `handle_advance` function is the entry point for state modifying logic, you can think of this like handling "write" requests in traditional web context. It is intended to carry out computations, updates, and other logic that changes the state of the application. Where appropriate, it can emit outputs such as `notices`, `vouchers`, or `reports`.
50+
51+
On the other hand, the `handle_inspect` function serves as the entry point for read only operations, similar to "read" requests in a typical web context. This function should be implemented to accept user input, perform any necessary lookups or calculations based on the current state, and return the results by emitting a `report`. It's important to understand that handle_inspect is designed strictly for reading the application's state, it should not perform any modifications.
52+
53+
Below is a sample application that has been modified to include the logic to simply receive an input from a user in both inspect and advance route then, emits a notice, voucher and a report. For your application you'll need to include your personal logic and also emit outputs when necessary:
54+
55+
```javascript
56+
57+
import { stringToHex, encodeFunctionData, erc20Abi, hexToString } from "viem";
58+
59+
const rollup_server = process.env.ROLLUP_HTTP_SERVER_URL;
60+
console.log("HTTP rollup_server url is " + rollup_server);
61+
62+
async function handle_advance(data) {
63+
console.log("Received advance request data " + JSON.stringify(data));
64+
65+
const sender = data["metadata"]["msg_sender"];
66+
const payload = hexToString(data.payload);
67+
const erc20Token = "0x784f0c076CC55EAD0a585a9A13e57c467c91Dc3a"; // Sample ERC20 token address
68+
69+
await emitNotice(payload);
70+
await emitReport(payload);
71+
72+
const call = encodeFunctionData({
73+
abi: erc20Abi,
74+
functionName: "transfer",
75+
args: [sender, BigInt(10)],
76+
});
77+
78+
let voucher = {
79+
destination: erc20Token,
80+
payload: call,
81+
value: '0x',
82+
};
83+
84+
await emitVoucher(voucher);
85+
return "accept";
86+
}
87+
88+
async function handle_inspect(data) {
89+
console.log("Received inspect request data " + JSON.stringify(data));
90+
const payload = data.payload;
91+
await emitReport(payload);
92+
return "accept";
93+
}
94+
95+
const emitNotice = async (inputPayload) => {
96+
let hexPayload = stringToHex(inputPayload);
97+
try {
98+
await fetch(rollup_server + "/notice", {
99+
method: "POST",
100+
headers: {
101+
"Content-Type": "application/json",
102+
},
103+
body: JSON.stringify({ payload: hexPayload }),
104+
});
105+
} catch (error) {
106+
//Do something when there is an error
107+
}
108+
}
109+
110+
const emitVoucher = async (voucher) => {
111+
try {
112+
await fetch(rollup_server + "/voucher", {
113+
method: "POST",
114+
headers: {
115+
"Content-Type": "application/json",
116+
},
117+
body: JSON.stringify(voucher),
118+
});
119+
} catch (error) {
120+
//Do something when there is an error
121+
}
122+
};
123+
124+
const emitReport = async (payload) => {
125+
let hexPayload = stringToHex(payload);
126+
try {
127+
await fetch(rollup_server + "/report", {
128+
method: "POST",
129+
headers: {
130+
"Content-Type": "application/json",
131+
},
132+
body: JSON.stringify({ payload: hexPayload }),
133+
});
134+
} catch (error) {
135+
//Do something when there is an error
136+
}
137+
};
138+
139+
var handlers = {
140+
advance_state: handle_advance,
141+
inspect_state: handle_inspect,
142+
};
143+
144+
var finish = { status: "accept" };
145+
146+
(async () => {
147+
while (true) {
148+
const finish_req = await fetch(rollup_server + "/finish", {
149+
method: "POST",
150+
headers: {
151+
"Content-Type": "application/json",
152+
},
153+
body: JSON.stringify({ status: "accept" }),
154+
});
155+
156+
console.log("Received finish status " + finish_req.status);
157+
158+
if (finish_req.status == 202) {
159+
console.log("No pending rollup request, trying again");
160+
} else {
161+
const rollup_req = await finish_req.json();
162+
var handler = handlers[rollup_req["request_type"]];
163+
finish["status"] = await handler(rollup_req["data"]);
164+
}
165+
}
166+
})();
167+
168+
169+
```

cartesi-rollups_versioned_docs/version-2.0/development/query-outputs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ The notice can be validated and queried by any interested party.
147147

148148
Frontend clients can use a GraphQL API exposed by the Cartesi Nodes to query the state of a Cartesi Rollups instance.
149149

150-
You can use the interactive in-browser GraphQL playground hosted on `http://localhost:8080/graphql/{application_address}` for local development. Note that you'll have to replace '{application_address}' with the address of your application.
150+
You can use the interactive in-browser GraphQL playground hosted on `http://localhost:8080/graphql/{application_address}` for local development. Note that you'll have to replace `{application_address}` with the address of your application.
151151

152152
In a GraphQL Playground, you typically have a section where you can input your query and variables separately. Here's how you would do it:
153153

cartesi-rollups_versioned_docs/version-2.0/development/send-inputs.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can send two requests to an application depending on whether you want to cha
1616

1717
## Advance and Inspect Requests
1818

19-
Here is a simple boilerplate application that handles Advance and Inspect requests:
19+
Here is a simple boilerplate application that shows the default implementation of the handle_advance and handle_inspect functions:
2020

2121
import Tabs from '@theme/Tabs';
2222
import TabItem from '@theme/TabItem';
@@ -121,9 +121,9 @@ while True:
121121

122122
</Tabs>
123123

124-
In a typical Cartesi backend application, two functions, `handle_advance` and `handle_inspect,` are defined to handle the two different types of requests.
124+
As stated in the [creating an application section](./creating-an-application.md#implementing-your-application-logic), a typical Cartesi backend application has two primary functions, `handle_advance` and `handle_inspect,` these are defined to handle the two different types of requests.
125125

126-
This script listens to rollup requests, handles them asynchronously using defined handler functions, and communicates the completion status to the rollup server.
126+
Your application listens to rollup requests, handles them asynchronously using defined handler functions, and communicates the completion status to the rollup server.
127127

128128
Every starter project you create has this base code as a template, ready to receive inputs!
129129

@@ -186,12 +186,16 @@ $ cartesi send generic
186186
✔ Wallet Mnemonic
187187
✔ Mnemonic test test test test test test test test test test test junk
188188
✔ Account 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 9993.999194554374748191 ETH
189-
✔ Application address 0xde752d14b5bb9f81d434d0fb6e05b17c5afaa057
189+
✔ Application address 0xb483897a2790a5D1a1C5413690bC5933f269b3A9
190190
✔ Input String encoding
191191
✔ Input (as string) 32.9
192192
✔ Input sent: 0x048a25e6341234b8102f7676b3a8defb947559125197dbd45cfa3afa451a93fd
193193
```
194194

195+
:::note Interacting with Sample application?
196+
Replacing the above dapp address `0xb48..3A9` with the application address from deploying the sample application in the [creating an application section](./creating-an-application.md#implementing-your-application-logic), would emit a notice, voucher and report which can all be quarried using the JSON RPC or GraphQl server.
197+
:::
198+
195199
#### 3. Send inputs via a custom web app
196200

197201
You can create a custom frontend that interacts with your application.
@@ -210,29 +214,36 @@ Inspect requests are best suited for non-production use, such as debugging and t
210214

211215
You can make a simple inspect call from your frontend client to retrieve reports.
212216

213-
To perform an Inspect call, use an HTTP POST request to `<address of the node>/inspect/<application_address>/<request path>`. For example:
217+
To perform an Inspect call, use an HTTP POST request to `<address of the node>/inspect/<application name or address>` with a body containing the request payload. For example:
214218

215219
```shell
216-
curl -X POST http://localhost:8080/inspect/0xeF34611773387750985673f94067EA22dB406F72/mypath
220+
curl -X POST http://localhost:8080/inspect/0xb483897a2790a5D1a1C5413690bC5933f269b3A9 \
221+
-H "Content-Type: application/json" \
222+
-d '"test"'
217223
```
218224

219225
Once the call's response is received, the payload is extracted from the response data, allowing the backend code to examine it and produce outputs as **reports**.
220226

221227
From a frontend client, here is an example of extracting the payload from an inspect request:
222228

223229
```javascript
224-
const response = await fetch(
225-
"http://localhost:8080/inspect/0xeF34611773387750985673f94067EA22dB406F72/mypath",
226-
{
227-
method: "POST",
228-
headers: {
229-
"Content-Type": "application/json",
230-
},
231-
}
232-
);
230+
const response = await fetch("http://localhost:8080/inspect/0xb483897a2790a5D1a1C5413690bC5933f269b3A9", {
231+
method: "POST",
232+
headers: {
233+
"Content-Type": "application/json"
234+
},
235+
body: JSON.stringify("test")
236+
});
233237

234238
const result = await response.json();
239+
235240
for (let i in result.reports) {
236241
let payload = result.reports[i].payload;
242+
console.log("Report Payload:", payload);
237243
}
244+
238245
```
246+
247+
:::note Interacting with Sample application?
248+
Replacing the above address `0xb48..3A9` with the application address from deploying the sample application in the [creating an application](./creating-an-application.md#implementing-your-application-logic), then executing the inspect command would emit a report which would immediately be logged to the CLI and can also be quarried using the JSON RPC or GraphQl server.
249+
:::

cartesi-rollups_versioned_sidebars/version-2.0-sidebars.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@
111111
"collapsed": true,
112112
"items": [
113113
"development/installation",
114+
"development/creating-an-application",
114115
"development/managing-a-devnet-environment",
115116
"development/building-and-deploying-an-application",
116117
"development/send-inputs",
117118
"development/query-outputs",
118-
"development/asset-handling",
119-
"development/cli-commands"
119+
"development/asset-handling"
120120
]
121121
},
122122
{

0 commit comments

Comments
 (0)