Skip to content

refactor: Update to latest Node v2 (By using cartesi/cli alpha.9) #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@

A decentralised co-creation text platform powered by Cartesi and Espresso. Comet allows you to jam on poems, stories, lyrics, quips and present your work as ERC1155 token for collectors.

#### Monorepo commands
## Starting the backend `apps/jam-backend`

Turborepo is controlling the monorepo based on `pipelines` described in the `turbo.json`. The commands ran from the root of the project will go through turborepo.
Follow the instructions in it's [README file](./apps/jam-backend/README.md).

## To Start the frontend i.e. `apps/jam-ui` (nextJS)
## Starting the frontend `apps/jam-ui` (nextJS)

1. Check the `.env.template` and copy the values inside to a new `.env` file and replace the values you need (i.e NEXT_PUBLIC_APP_ADDRESS in case your cartesi-app-address is different when deployed using cartesi/cli)
2. Run the following command

```
yarn dev
```

Nothing stops the dev to go inside the individual app and run its own npm-scripts for the sake of simplicity.

## To start the backend i.e. `apps/jam-backend`
Follow the instructions in the README file `./apps/jam-backend/README.md`
Follow the instructions in it's [README file](./apps/jam-ui/)
5 changes: 5 additions & 0 deletions apps/jam-backend/.env.devnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Devnet account 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 to be used as deployer
PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
APP_ADDRESS="0x3b352ef8dbd35fa90c0ef4ff5d7cb41008d49647"
PROVIDER_RPC_URL="http://127.0.0.1:8080/anvil/"
CHAIN_ID=13370
6 changes: 6 additions & 0 deletions apps/jam-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.cartesi
/dist
/node_modules
/cache
/coverage
/dependencies
/out
lcov.info
remappings.txt
45 changes: 35 additions & 10 deletions apps/jam-backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
# syntax=docker.io/docker/dockerfile:1

# This enforces that the packages downloaded from the repositories are the same
# for the defined date, no matter when the image is built.
ARG APT_UPDATE_SNAPSHOT=20250408T030400Z

################################################################################
# riscv64 base stage
FROM --platform=linux/riscv64 cartesi/node:22.14.0-noble-slim AS base

ARG APT_UPDATE_SNAPSHOT
ARG DEBIAN_FRONTEND=noninteractive
RUN <<EOF
set -eu
apt-get update
apt-get install -y --no-install-recommends ca-certificates curl
apt-get update --snapshot=${APT_UPDATE_SNAPSHOT}
EOF

################################################################################
# build stage: includes resources necessary for installing dependencies

# Here the image's platform does not necessarily have to be riscv64.
# If any needed dependencies rely on native binaries, you must use
# a riscv64 image such as cartesi/node:20-jammy for the build stage,
# to ensure that the appropriate binaries will be generated.
FROM --platform=$BUILDPLATFORM node:20.16.0-bookworm AS build-stage
FROM --platform=$BUILDPLATFORM node:22.14.0-bookworm AS build-stage

WORKDIR /opt/cartesi/dapp
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
RUN yarn install && yarn build
RUN yarn build

################################################################################
# runtime stage: produces final image that will be executed

# Here the image's platform MUST be linux/riscv64.
# Give preference to small base images, which lead to better start-up
# performance when loading the Cartesi Machine.
FROM cartesi/node:20.16.0-jammy-slim
FROM base

ARG MACHINE_GUEST_TOOLS_VERSION=0.17.0-test6
ARG DEBIAN_FRONTEND=noninteractive
RUN <<EOF
set -e
apt-get update
apt-get install -y --no-install-recommends \
busybox-static=1:1.30.1-7ubuntu3
busybox-static

cd /tmp
busybox wget https://github.com/cartesi/machine-guest-tools/releases/download/v${MACHINE_GUEST_TOOLS_VERSION}/machine-guest-tools_riscv64.deb
echo "850ddd259832497d122f05da54f91217852d57dd47c24a02efa66a44843b0f9c8c29cd81d3fa6d0f804d4a0b89466af00e619348dde94f23150cfd0f8bc930c8 /tmp/machine-guest-tools_riscv64.deb" \
| sha512sum -c
apt-get install -y --no-install-recommends \
/tmp/machine-guest-tools_riscv64.deb
rm /tmp/machine-guest-tools_riscv64.deb

rm -rf /var/lib/apt/lists/* /var/log/* /var/cache/*
useradd --create-home --user-group dapp
EOF

ARG MACHINE_EMULATOR_TOOLS_VERSION=0.16.1
ADD https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb /
RUN dpkg -i /machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
&& rm /machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb

ENV PATH="/opt/cartesi/bin:${PATH}"

WORKDIR /opt/cartesi/dapp
Expand Down
147 changes: 60 additions & 87 deletions apps/jam-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,125 @@
This is backend for Comet dApp powered by Cartesi written in JavaScript. It uses node to execute the backend application.
The application entrypoint is the `src/index.js` file. It is bundled with [esbuild](https://esbuild.github.io), but any bundler can be used.

## Steps to run with Nonodo + Cartesi Machine
## Steps to run the node locally (Using Cartesi/cli for node-v2)

> [!IMPORTANT]
> Required to succeed
>
> - @cartesi/cli@2.0.0-alpha.9 or above
> - Docker
> - Foundry [reference to install](https://book.getfoundry.sh/getting-started/installation)

1. cd in project backend directory and install node packages

>**NOTE:** This backend works with Rollups v2. It will require an [unreleased Deroll library](https://github.com/tuler/deroll/tree/feature/v2) version. You'll need to clone the repo, build release/v2 branch and `pnpm link path-to-deroll-release-v2` here in this project. Both app and wallet package needs to be linked separately.
1. Install brunodo(nonodo variant) for Espresso ready integration. Note that this was tested with `brunodo 2.10.1-beta` for arm64 darwin
```
npm i -g brunodo
yarn install
```
2. Run brunodo node

2. Start the rollups environment

```
brunodo
cartesi rollups start
```
This will spin up a local instance of Espresso and Cartesi node ready to recieve user inputs.

3. Build your comet jam-backend with cartesi cli
3. Run cartesi build

```
cartesi build
```
4. Download your platform specific Cartesi-Machine from [here](https://github.com/edubart/cartesi-machine-everywhere/releases/tag/v0.18.1-rc7).
5. In another terminal tab, cd into `./bin` of above downloaded folder and run Comet backend inside the Cartesi Machine
```
$ ./cartesi-machine --network \
--env=ROLLUP_HTTP_SERVER_URL=http://10.0.2.2:5004 \ --flash-drive=label:root,filename:./path-to-jam-backend-folder/.cartesi/image.ext2 \ --volume=.:/mnt \
--workdir=mnt/jam-backend \
--ram-length=256Mi \
-- yarn start
```
You should now have your comet backend up and running. Follow [this](https://docs.google.com/document/d/1x8IhhDeZR818uBHXk-NQvZETXhFXbGDj0OLG2h3e9EE/edit?usp=sharing) detailed guide in case of issues.

6. Send inputs to the backend via L1 InputBox contract: You can run the test script `test_comet_jam.sh` file to test basic functions. Alternatively, open another terminal tab and use `cast` commands listed below to test the app:
4. Deploy the DApp (for devnet the default options are fine)

**Create a new Comet Jam**
```
INPUT=7b22616374696f6e223a20226a616d2e637265617465222c226e616d65223a20226d794a616d222c20226465736372697074696f6e22203a20226d79206a616d2064657363222c20226d696e745072696365223a202233222c20226d6178456e7472696573223a20332c202267656e65736973456e747279223a2022526f7365732061726520726564227d; \
INPUT_BOX_ADDRESS=0x593E5BCf894D6829Dd26D0810DA7F064406aebB6; \
APPLICATION_ADDRESS=0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e; \
cast send \
--mnemonic "test test test test test test test test test test test junk" \
--rpc-url "http://localhost:8545/" \
$INPUT_BOX_ADDRESS \
"addInput(address,bytes)(bytes32)" $APPLICATION_ADDRESS $INPUT
cartesi rollups deploy
```

**Append to the Comet Jam**
```
INPUT=7b22616374696f6e223a20226a616d2e617070656e64222c20226a616d4944223a20302c2022656e74727922203a2022536b6965732061726520626c7565227d; \
INPUT_BOX_ADDRESS=0x593E5BCf894D6829Dd26D0810DA7F064406aebB6; \
APPLICATION_ADDRESS=0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e; \
cast send \
--mnemonic "test test test test test test test test test test test junk" \
--rpc-url "http://localhost:8545/" \
$INPUT_BOX_ADDRESS \
"addInput(address,bytes)(bytes32)" $APPLICATION_ADDRESS $INPUT
```
5. Check on the application address provided in the terminal by the previous step and fill the [.env.devnet](./env.devnet) file with the appropriate information.

**Simple Ether deposit to the dApp**
```
cast send \
--mnemonic "test test test test test test test test test test test junk" \
--rpc-url "http://localhost:8545/" \
0xfa2292f6D85ea4e629B156A4f99219e30D12EE17 \
"depositEther(address,bytes)" \
0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e \
0x \
--value 2ether
```
6. Build the contract assets we use (ERC1155 contract).

**Ether Deposit to Mint a Comet**
```
cast send \
--mnemonic "test test test test test test test test test test test junk" \
--rpc-url "http://localhost:8545/" \
0x1733b13aAbcEcf3464157Bd7954Bd7e4Cf91Ce22 \
"depositEther(address,bytes)" \
0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e \
0x7b22616374696f6e223a226a616d2e6d696e74222c20226a616d4944223a317d \
--value 3ether
yarn build:contracts
```

7. deploy the ERC1155 contract we use and notify the deployed DApp about it. (Check the docker logs in rollups-node)

## Steps to run the node locally
1. cd in project backend directory and install node packages
```
yarn && yarn install
```
2. Run cartesi build
```
cartesi build
```
3. Turn up the node
yarn deploy:devnet:contract
```
cartesi run --epoch-duration=10
```
4. Deploy ERC1155 smart contract - Open [Remix IDE](https://remix.ethereum.org/) and copy `contracts/SimpleERC1155.sol` file to Remix project. Compile, select environment as `Injected Provider - Metamask` and hit deploy. Copy the deployed contract address.

5. In a separate terminal tab, use `cartesi send generic` to set the address of the NFT contract deployed in step #4. The input JSON string to use below:
```
{"action":"jam.setNFTAddress", "address":"deployed-nft-contract-address"}
```
6. Use `cartesi send` to set the address of the dApp contract. Select Option available as `Send DApp address input to the application.`
7.1 You can use the address printed for ERC1155 as it is necessary to include the [UI](../jam-ui/) environment variables. In case you clean-up your screen a deployment information is saved under `contracts/deployments`.

7. Environment is now set, proceed for app inputs. Some input actions with json examples are given below.
8. The environment is now set, proceed for app inputs. Some input actions with json examples are given below for illustration purposes as they need to be encoded and sent through contracts.

You can use both the `./test_comet_jam.sh` and `test_inspect.sh` to automatically Create/Contribute/Mint a JAM. Also you can check the data by using making inspect calls to the node. Just update the **application address** inside the scripts in case it does not matches.

### Creating a Jam

```
{"action": "jam.create","name": "myJam", "description" : "my jam desc", "mintPrice": "3", "maxEntries": 3, "genesisEntry": "Roses are red"}
```

### Append to a Jam

```
{"action": "jam.append", "jamID": 0, "entry" : "Skies are blue"}
```

### Create voucher to mint a Jam

Open Explorer to send this input. It's an EtherDeposit function with an arbitrary payload. Select dapp address, ether amount to mint the jam and provide hex encoded extra data. Example below string:

```
{"action":"jam.mint", "jamID":1}
```

Note: You'll have to convert the above JSON to hex format before submitting.

### Withdraw ether from the rollup application
Accepts ether amount in wei format

Accepts ether amount in wei format

```
{"action":"eth.withdraw", "amount":"2000000000000000000"}
```

### Inpect eth balance inside the dapp

```
http://localhost:8080/inspect/balance/<input-address-here>
```

### Other inpect calls for Jams info
```
### Using a Rest Client API (e.g Thunder Client)

The http method should be `POST` and the the way to inspect is the path as body text content. The returns are always hex values, so probably you want to use the [UI](../jam-ui/)

The inspect format is as follow.

`http://127.0.0.1:8080/inspect/<application_address>`

// Get a jam by ID
http://localhost:8080/inspect/jams/<input-jamID-here>

```
jams/<input-jamID-here>
```

// Get all open jams
http://localhost:8080/inspect/openjams

> body -> openjams

// Get all closed jams
http://localhost:8080/inspect/closedjams

> body -> closedjams

// Get all jams
http://localhost:8080/inspect/jams

> body -> jams

// Get jams by user address
http://localhost:8080/inspect/user/<input-address-here>

> body -> user/<input-address-here>

// Get all jam statistics
http://localhost:8080/inspect/jamstats
```

> body -> jamstats
2 changes: 0 additions & 2 deletions apps/jam-backend/cartesi.toml

This file was deleted.

6 changes: 6 additions & 0 deletions apps/jam-backend/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/cache
/deployments
/docs
/out
lcov.info
state.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
pragma solidity ^0.8.23;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin-contracts-5.2.0/token/ERC1155/ERC1155.sol";
import "@openzeppelin-contracts-5.2.0/access/Ownable.sol";

contract JamContract is ERC1155, Ownable {

constructor(address initialOwner) ERC1155("") Ownable(initialOwner){}
constructor() ERC1155("") Ownable(msg.sender){}

function mint(address account, uint256 id)
public
Expand Down
Loading