Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Ultrachess/app

Application code of Ultrachess.org
https://github.com/Ultrachess/app

blockchain chess-ai chess-engine

Last synced: about 2 months ago
JSON representation

Application code of Ultrachess.org

Awesome Lists containing this project

README

        

[![Build Status](https://github.com/Ultrachess/app/actions/workflows/node.js.yml/badge.svg)](https://github.com/Ultrachess/app/actions/workflows/node.js.yml)

# Ultrachess application code

Ultrachess is a customized DApp written in Python, which originally resembles the one provided by the sample [Echo Python DApp](https://github.com/cartesi/rollups-examples/tree/main/echo-python).
Contrary to that example, this DApp does not use shared resources from the `rollups-examples` main directory, and as such the commands for building, running and deploying it are slightly different.

The documentation below reflects the original application code, and should also be used as a basis for documenting any DApp created with this mechanism.

## Requirements

Please refer to the [rollups-examples requirements](https://github.com/cartesi/rollups-examples/tree/main/README.md#requirements).

## Building

To build the application, run the following command:

```shell
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl --load
```

## Running

To start the application, execute the following command:

```shell
docker compose up
```

The application can afterwards be shut down with the following command:

```shell
docker compose down -v
```

## Interacting with the application

We can use the rollups-examples [frontend-console](https://github.com/cartesi/rollups-examples/tree/main/frontend-console) application to interact with the DApp.
Ensure that the [application has already been built](https://github.com/cartesi/rollups-examples/tree/main/frontend-console/README.md#building) before using it.

From within the `frontend-console` directory, you can send an input as follows:

```shell
yarn start input send --payload "Hello there"
```

In order to verify the notices generated by your inputs, run the command:

```shell
yarn start notice list
```

The response should be something like this:

```shell
[ { epoch: '0', input: '1', notice: '0', payload: 'Hello there' } ]
```

## Developing with the frontend

To start the frontend dev server, run the following commands:

```shell
cd front
yarn install
yarn dev
```

To proxy the CORS headers, you can use nginx. Create the following file at `/etc/nginx/sites-enabled/new-conf.conf`:

```
server {
listen 3001;
add_header 'Access-Control-Allow-Origin' 'http://localhost:3002' always;
location / {
proxy_pass http://localhost:5173;
}
}

server {
listen 3002;
location / {
proxy_pass http://localhost:5005;
}
}

server {
listen 3003;
add_header Access-Control-Allow-Origin *;
location / {
proxy_pass http://localhost:3000;
}
}
```

Then restart nginx:

```shell
sudo systemctl restart nginx
```

## Deploying to a testnet

Deploying the application to a blockchain requires creating a smart contract on that network, as well as running a validator node for the DApp.

The first step is to build the DApp's back-end machine, which will produce a hash that serves as a unique identifier.

```shell
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl machine --load
```

Once the machine docker image is ready, we can use it to deploy a corresponding Rollups smart contract. This requires you to define a few environment variables to specify which network you are deploying to, which account to use, and which RPC gateway to use when submitting the deploy transaction.

```shell
export NETWORK=
export MNEMONIC=
export RPC_URL=
export VALIDATORS=
export GAS_PRICE=
export GAS_LIMIT=
```

Create your .env file with the parameters above and then run the following command:

```shell
chmod +x ./load_env.sh
source ./load_env.sh
```

For example, to deploy to the Goerli testnet using an Alchemy RPC node, you could execute:

```shell
export NETWORK=goerli
export MNEMONIC=
export RPC_URL=https://eth-goerli.alchemyapi.io/v2/
export VALIDATORS=0x433eC3aE31C080da466a8c20710dEC366ceC1f61
export GAS_PRICE=100
export GAS_LIMIT=2800000
```

With that in place, you can submit a deploy transaction to the Cartesi DApp Factory contract on the target network by executing the following command:

```shell
DAPP_NAME=chessAppNew docker compose -f ./deploy-testnet.yml up
```

This will create a file at `./deployments//chessAppNew.json` with the deployed contract's address.
Once the command finishes, it is advisable to stop the docker compose and remove the volumes created when executing it.

```shell
DAPP_NAME=chessAppNew docker compose -f ./deploy-testnet.yml down -v
```

After that, a corresponding Cartesi Validator Node must also be instantiated in order to interact with the deployed smart contract on the target network and handle the back-end logic of the DApp.
Aside from the environment variables defined above, the node will also need a secure websocket endpoint for the RPC gateway (WSS URL) and the chain ID of the target network.

For example, for Goerli and Alchemy, you would set the following additional variables:

```shell
export WSS_URL=wss://eth-goerli.alchemyapi.io/v2/
export CHAIN_ID=5
```

Then, the node itself can be started by running a docker compose as follows:

```shell
DAPP_NAME=chessAppNew docker compose -f ./docker-compose-testnet.yml -f ./docker-compose.override.yml up
```

## Interacting with the deployed application

With the node running, you can interact with the deployed DApp using the [frontend-console](https://github.com/cartesi/rollups-examples/tree/main/frontend-console), as described [previously](#interacting-with-the-application).
This time, however, you need to specify the appropriate connectivity configurations.

First of all, in the separate terminal for the frontend-console, define the `MNEMONIC` and `RPC_URL` variables as before:

```shell
export MNEMONIC=
export RPC_URL=
```

Then, inputs can be sent by specifying the DApp contract's address, as follows:

```shell
yarn start input send --payload "Hello there" --addressFile path/to/chessAppNew/deployments//chessAppNew.json
```

Resulting notices can then be retrieved by querying the local Cartesi Node, as before:

```shell
yarn start notice list
```

## Running the back-end in host mode

When developing an application, it is often important to easily test and debug it. For that matter, it is possible to run the Cartesi Rollups environment in [host mode](https://github.com/cartesi/rollups-examples/tree/main/README.md#host-mode), so that the DApp's back-end can be executed directly on the host machine, allowing it to be debugged using regular development tools such as an IDE.

The host environment can be executed with the following command:

```shell
docker compose -f docker-compose.yml -f docker-compose.override.yml -f docker-compose-host.yml up
```

This DApp's back-end is written in Python, so to run it in your machine you need to have `python3` installed.

In order to start the back-end, run the following commands in a dedicated terminal:

```shell
python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004" python3 main.py
```

The final command will effectively run the back-end and send corresponding outputs to port `5004`.
It can optionally be configured in an IDE to allow interactive debugging using features like breakpoints.

You can also use a tool like [entr](https://eradman.com/entrproject/) to restart the back-end automatically when the code changes. For example:

```shell
ls *.py | ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004" entr -r python3 main.py
```

After the back-end successfully starts, it should print an output like the following:

```log
INFO:__main__:HTTP rollup_server url is http://127.0.0.1:5004
INFO:__main__:Sending finish
```

After that, you can interact with the application normally [as explained above](#interacting-with-the-application).