Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ Now, navigate to the project directory and install [`ethers`](https://docs.ether

```bash
yarn add ethers viem
yarn add -D @cartesi/rollups
yarn add -D @cartesi/rollups@1.4.3
```



## Define the ABIs

Let's write a configuration to generate the ABIs of the Cartesi Rollups Contracts.
Expand Down Expand Up @@ -108,14 +106,15 @@ This script will look for all specified `.sol` files and create a TypeScript fil

Now, let's make the script executable:

chmod +x generate_abis.sh

```bash
chmod +x generate_abis.sh
```

And run it:

./generate_abis.sh
```bash
./generate_abis.sh
```

## Building the Ether wallet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,76 @@ def handle_advance(data):
</code></pre>
</TabItem>

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

```rust
fn hex_to_string(hex: &str) -> Result<String, Box<dyn std::error::Error>> {
let hexstr = hex.strip_prefix("0x").unwrap_or(hex);
let bytes = hex::decode(hexstr).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
let s = String::from_utf8(bytes).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
Ok(s)
}

pub async fn handle_advance(
_client: &hyper::Client<hyper::client::HttpConnector>,
_server_addr: &str,
request: JsonValue,
) -> Result<&'static str, Box<dyn std::error::Error>> {
println!("Received advance request data {}", &request);
let payload = request["data"]["payload"]
.as_str()
.ok_or("Missing payload")?;

let payload_string = hex_to_string(payload);

match payload_string {
Ok(payload_extract) => {
// DO SOMETHING HERE!!
}
Err(e) => {
throw_execption(e.to_string()).await;
}
}
Ok("accept")
}

async fn throw_execption( payload: String) -> Option<bool> {
let hex_string = {
let s = payload.strip_prefix("0x").unwrap_or(payload.as_str());
hex::encode(s.as_bytes())
};

let server_addr = env::var("ROLLUP_HTTP_SERVER_URL").expect("ROLLUP_HTTP_SERVER_URL not set");
let client = hyper::Client::new();

let response = object! {
"payload" => format!("0x{}", hex_string),
};
let request = hyper::Request::builder()
.method(hyper::Method::POST)
.header(hyper::header::CONTENT_TYPE, "application/json")
.uri(format!("{}/exception", server_addr))
.body(hyper::Body::from(response.dump()))
.ok()?;

let response = client.request(request).await;
match response {
Ok(_) => {
println!("Exception generation successful");
return Some(true);
}
Err(e) => {
println!("Exception request failed {}", e);
None
}
}
}
```

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

</Tabs>

## Notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,82 @@ while True:
</code></pre>
</TabItem>

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

```rust
use json::{object, JsonValue};
use std::env;

pub async fn handle_advance(
_client: &hyper::Client<hyper::client::HttpConnector>,
_server_addr: &str,
request: JsonValue,
) -> Result<&'static str, Box<dyn std::error::Error>> {
println!("Received advance request data {}", &request);
let _payload = request["data"]["payload"]
.as_str()
.ok_or("Missing payload")?;
// TODO: add application logic here
Ok("accept")
}

pub async fn handle_inspect(
_client: &hyper::Client<hyper::client::HttpConnector>,
_server_addr: &str,
request: JsonValue,
) -> Result<&'static str, Box<dyn std::error::Error>> {
println!("Received inspect request data {}", &request);
let _payload = request["data"]["payload"]
.as_str()
.ok_or("Missing payload")?;
// TODO: add application logic here
Ok("accept")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = hyper::Client::new();
let server_addr = env::var("ROLLUP_HTTP_SERVER_URL")?;

let mut status = "accept";
loop {
println!("Sending finish");
let response = object! {"status" => status};
let request = hyper::Request::builder()
.method(hyper::Method::POST)
.header(hyper::header::CONTENT_TYPE, "application/json")
.uri(format!("{}/finish", &server_addr))
.body(hyper::Body::from(response.dump()))?;
let response = client.request(request).await?;
println!("Received finish status {}", response.status());

if response.status() == hyper::StatusCode::ACCEPTED {
println!("No pending rollup request, trying again");
} else {
let body = hyper::body::to_bytes(response).await?;
let utf = std::str::from_utf8(&body)?;
let req = json::parse(utf)?;

let request_type = req["request_type"]
.as_str()
.ok_or("request_type is not a string")?;
status = match request_type {
"advance_state" => handle_advance(&client, &server_addr[..], req).await?,
"inspect_state" => handle_inspect(&client, &server_addr[..], req).await?,
&_ => {
eprintln!("Unknown request type");
"reject"
}
};
}
}
}
```

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

</Tabs>

## Notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ An **Inspect** request involves making an external HTTP API call to the rollups

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

To perform an Inspect call, send an HTTP POST request to `<address of the node>/inspect/<application name>` with a payload in the request body. For example:
To perform an Inspect call, make a HTTP POST request to `<address of the node>/inspect/<application name>` with a payload in the request body. For example:

```shell
curl -X POST http://localhost:8080/inspect/<application name> \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,57 @@ def handle_advance(data):
</code></pre>
</TabItem>

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

```rust
pub async fn handle_advance(
_client: &hyper::Client<hyper::client::HttpConnector>,
_server_addr: &str,
request: JsonValue,
) -> Result<&'static str, Box<dyn std::error::Error>> {
println!("Received advance request data {}", &request);
let payload = request["data"]["payload"]
.as_str()
.ok_or("Missing payload")?;


emit_notice(payload).await;
Ok("accept")
}

async fn emit_notice(payload: String) -> Option<bool> {
let server_addr = env::var("ROLLUP_HTTP_SERVER_URL").expect("ROLLUP_HTTP_SERVER_URL not set");
let client = hyper::Client::new();

let response = object! {
"payload" => payload,
};
let request = hyper::Request::builder()
.method(hyper::Method::POST)
.header(hyper::header::CONTENT_TYPE, "application/json")
.uri(format!("{}/notice", server_addr))
.body(hyper::Body::from(response.dump()))
.ok()?;

let response = client.request(request).await;
match response {
Ok(_) => {
println!("Notice generation successful");
return Some(true);
}
Err(e) => {
println!("Notice request failed {}", e);
None
}
}
}
```

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

:::note querying notices
Frontend clients can query notices using a GraphQL API exposed by Cartesi Nodes. [Refer to the documentation here](../../development/query-outputs.md/#query-all-reports) to query notices from the rollup server.
Frontend clients can query notices using a JSON RPC API exposed by Cartesi Nodes. [Refer to the documentation here](../../development/query-outputs.md#query-all-notices) to query notices from the rollup server.
:::
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,52 @@ def handle_advance(data):
</code></pre>
</TabItem>

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

```rust
pub async fn handle_advance(
_client: &hyper::Client<hyper::client::HttpConnector>,
_server_addr: &str,
request: JsonValue,
) -> Result<&'static str, Box<dyn std::error::Error>> {
println!("Received advance request data {}", &request);
let payload = request["data"]["payload"]
.as_str()
.ok_or("Missing payload")?;

let server_addr = env::var("ROLLUP_HTTP_SERVER_URL").expect("ROLLUP_HTTP_SERVER_URL not set");
let client = hyper::Client::new();

let response = object! {
"payload" => payload,
};
let request = hyper::Request::builder()
.method(hyper::Method::POST)
.header(hyper::header::CONTENT_TYPE, "application/json")
.uri(format!("{}/report", server_addr))
.body(hyper::Body::from(response.dump()))
.ok()?;

let response = client.request(request).await;
match response {
Ok(_) => {
println!("Report generation successful");
Ok("accept")
}
Err(e) => {
println!("Report request failed {}", e);
Err("Report request failed {}")
}
}
}
```

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

</Tabs>

:::note querying reports
Frontend clients can query reports using a GraphQL API exposed by the Cartesi Nodes. [Refer to the documentation to query reports](../../development/query-outputs.md/#query-all-reports) from your dApp.
Frontend clients can query reports using a GraphQL API exposed by the Cartesi Nodes. [Refer to the documentation to query reports](../../development/query-outputs.md#query-all-reports) from your dApp.
:::
Loading