2024-07-09 00:06:31 +03:00

98 lines
4.0 KiB
Markdown

# ERC-20 DApp
This DApp shows how to parse ERC-20 deposits sent by the ERC-20 Portal, which is where all legitimate ERC-20 deposits come from, and how to issue vouchers so the amount deposited can be withdrawn later on.
A deposit must have a payload, whose format is defined at the [ERC-20 Portal contract](https://github.com/cartesi/rollups/blob/main/onchain/rollups/contracts/portals/IERC20Portal.sol), which is part of the Cartesi Rollups contract.
Any input that either does not come from the Portal or is malformed will be rejected.
After the deposit is properly parsed, the application issues a voucher to return the amount back to the depositor ("I don't want your money!"). This voucher can then be executed to withdraw the amount from the Portal and recover the assets.
## Interacting with the application
We can use the [frontend-console](../frontend-console) application to interact with the DApp.
Ensure that the [application has already been built](../frontend-console/README.md#building) before using it.
First, go to a separate terminal window and switch to the `frontend-console` directory:
```shell
cd frontend-console
```
Then, perform a deposit of some `SimpleERC20` tokens as follows:
```shell
yarn start erc20 deposit --amount 100
```
> Contract `SimpleERC20` is provided by the [`common-contracts`](../common-contracts/README.md#simpleerc20) project and is deployed automatically to the localhost network.
In order to verify the notices generated by your inputs, run the command:
```shell
yarn start notice list
```
The payload of the notice should be something like this:
```json
"Deposit received from: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266; ERC-20: 0xa513e6e4b8f2a923d98304ec87f64353c4d5c853; Amount: 100"
```
In order to check the voucher generated for the deposit, execute the following command:
```shell
yarn start voucher list
```
The voucher may be executed to return the funds to the depositor by following the instructions available at [Validating notices and executing vouchers](../frontend-console/README.md#validating-notices-and-executing-vouchers).
## Running the environment in host mode
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
cd erc20/
python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004" python3 erc20.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 erc20.py
```
After the back-end successfully starts, it should print an output like the following:
```shell
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).
## Running a validator node on testnet
Deploying DApps to a testnet and running corresponding validator nodes are described in the [main README](../README.md#deploying).
However, for this DApp the command to run the validator node needs to be slightly different because of the additional configuration for `common-contracts`, which is used in the local development environment.
As such, for this DApp the final command to run the node should specify the testnet-specific docker compose override, as follows:
```shell
DAPP_NAME=erc20 docker compose --env-file ../env.<network> -f ../docker-compose-testnet.yml -f ./docker-compose-testnet.override.yml up
```
In the case of Sepolia, the command would be:
```shell
DAPP_NAME=erc20 docker compose --env-file ../env.sepolia -f ../docker-compose-testnet.yml -f ./docker-compose-testnet.override.yml up
```