Skip to content

Latest commit

 

History

History
261 lines (177 loc) · 10.5 KB

README.md

File metadata and controls

261 lines (177 loc) · 10.5 KB

Frontend Console

This project demonstrates how to implement a console application to send inputs to a Cartesi Rollups DApp, both running locally and deployed on remote testnet networks. It's implemented in Typescript and uses the ethers library to communicate with the rollups smart contracts.

Requirements

  • node.js
  • yarn

Building

To build the console application, first clone the repository as follows:

git clone https://github.com/cartesi/rollups-examples.git

Then, build the application by executing the following commands:

cd frontend-console/
yarn
yarn build

Running

The console application can be used to perform several operations. The available commands can be queried as follows:

$ yarn start --help

Commands:
  index.ts erc20 <command>    Operations with ERC-20 tokens
  index.ts input <command>    Operations with inputs
  index.ts inspect            Inspect the state of the DApp
  index.ts notice <command>   Operations with notices
  index.ts voucher <command>  Operations with vouchers

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

In general terms, the application communicates with a Cartesi DApp through two different endpoints, one for layer-1 communication (underlying blockchain) and another for layer-2 communication (Cartesi Node). Layer-1 communication is used for sending data to the DApp, including depositing assets. On the other hand, communication through the layer-2 node is used when querying DApp information such as provable outputs (notices, vouchers) and application logs (reports), or when performing generic application-level queries (inspect).

Sending inputs

The input send command adds inputs to a Cartesi Rollups DApp and has the following format:

yarn start input send --payload [message] <options>

Examples:

  1. Send an input to the current locally deployed DApp using Hardhat's default funded account:

    yarn start input send --payload "my message"
  2. Send an input to an instance of the echo-python DApp already deployed to the Ethereum Goerli testnet, using a user's account and a user's gateway RPC on Alchemy:

    export MNEMONIC=<user sequence of twelve words>
    export RPC_URL=https://eth-goerli.alchemyapi.io/v2/<USER_KEY>
    
    yarn start input send --payload "my message" --dapp echo-python

Options

When sending data to a DApp, the console application needs to communicate with the underlying layer-1 blockchain. In order to do that, it needs the URL of a blockchain RPC gateway, the address of the DApp's Rollups smart contract on that blockchain, and an appropriate account with sufficient funds for submitting transactions to the network.

The following parameters are available to specify this configuration, with some default behaviors and values to make it convenient to the user, especially for a local development environment:

  • --rpc: provides the RPC URL of the remote blockchain gateway; if absent, its default value is "http://localhost:8545", which provides connectivity to the Hardhat node used for local development; it may also be specified by setting a value for the environment variable RPC_URL;
  • --address: represents an explicit definition for the address of the DApp's Rollups smart contract, given as a hex string; if absent, the console application will look for other ways to determine this address, as explained below
  • --addressFile: provides a path to a file containing the address as a hex string, and is only used if address is absent
  • --dapp: specifies the name of the DApp, with a default value of "dapp"; if both address and addressFile are absent, the console application will query the given RPC instance to infer the network being used, and will extract the DApp's address from file ../deployments/<network>/<dapp>.address; as such, if none of the above parameters are specified, the DApp address will be determined by looking for the file ../deployments/localhost/dapp.address;
  • --mnemonic: determines an account for submitting transactions; when using the localhost network, the application will use the default mnemonic for the local Hardhat node, which is already funded; otherwise, you must define a mnemonic for an account that has funds in the specified network; you can also define this parameter by setting the MNEMONIC environment variable;
  • --accountIndex: specifies an account index to use from the provided mnemonic; if absent, index 0 is used;

Listing notices, vouchers and reports

The notice list, voucher list and report list commands display DApp notices, vouchers and reports associated with the given epoch and input.

yarn start notice list <options>
yarn start voucher list <options>
yarn start report list <options>

Examples:

  1. List all notices ever emitted for the current locally deployed DApp:

    yarn start notice list
  2. List all vouchers emitted for epoch 0 of the current locally deployed DApp:

    yarn start voucher list --epoch 0
  3. List all reports emitted for epoch 0 and input 1 of the current locally deployed DApp:

    yarn start report list --epoch 0 --input 1
  4. List notices for epoch 1 and input 3 of the echo-python DApp instance already deployed to the Ethereum Goerli testnet:

    yarn start notice list --url https://echo-python.goerli.rollups.staging.cartesi.io/graphql --epoch 1 --input 3

Options are:

--url      Reader URL
--epoch    Epoch index
--input    Input index

If the url parameter is absent, the console application will use a default value of "http://localhost:4000/graphql", which provides connectivity to the local Cartesi Node.

Validating notices and executing vouchers

The notice validate command can be used to check if a given notice is authentic, meaning that its associated proof can be successfully verified by the Cartesi Rollups framework. In a similar way, the voucher execute command can be used to submit a transaction on layer-1 as an output of a Cartesi DApp (e.g., to withdraw funds back from the Rollups framework).

Both commands can only be executed if a proof is available for the specified notice or voucher. Proofs are only effectively produced when its associated epoch finishes, which happens after the DApp's configured epoch period elapses (e.g., after one week). In a local development environment, it is possible to explicitly advance time in order to finish an epoch and have proofs be generated by the framework.

yarn start notice validate --id [id] <options>
yarn start voucher execute --id [id] <options>

Examples:

  1. Validate notice with id 1 for the current locally deployed DApp, using Hardhat's default funded account:

    yarn start notice validate --id 1
  2. Execute voucher with id 3 for the current locally deployed DApp, using Hardhat's default funded account:

    yarn start voucher execute --id 1
  3. Validate notice with id 5 for the echo-python DApp already deployed to the Ethereum Goerli testnet, using a user's account and a user's gateway RPC on Alchemy:

    export MNEMONIC=<user sequence of twelve words>
    export RPC_URL=https://eth-goerli.alchemyapi.io/v2/<USER_KEY>
    
    yarn start notice validate --id 5 --dapp echo-python --url https://echo-python.goerli.rollups.staging.cartesi.io/graphql

Options are:

--id            Notice or Voucher ID
--rpc           URL of the RPC gateway to use
--address       DApp Rollups contract address
--addressFile   Path to a file containing the DApp Rollups contract address
--dapp          DApp name
--mnemonic      Wallet mnemonic
--accountIndex  Account index from mnemonic
--url           Reader URL

The url parameter has the same behavior as described for the notice/voucher list commands, whereas the other parameters behave as in the input send command.

Inspecting DApp state

The inspect command directly queries the DApp state within the Cartesi Machine. It can be used to send DApp-specific payloads that are interpreted by the application logic to yield a synchronous response. In practice, this command simply sends an HTTP GET request to the specified inspect URL, and does a little bit of parsing on the response.

yarn start inspect --payload [payload] <options>

Examples:

  1. Inspect the state of the locally deployed DApp using a REST-like query:

    yarn start inspect --payload "myresource?attr1=value1&attr2=value2"
  2. Inspect the state of the echo-python DApp instance already deployed to the Ethereum Goerli testnet:

    yarn start inspect --payload "mypayload" --url https://echo-python.goerli.rollups.staging.cartesi.io/inspect

Options are:

--url      Reader inspect URL

If the url parameter is absent, the console application will use a default value of "http://localhost:5005/inspect", which provides connectivity to the local Cartesi Node.

Depositing ERC-20 tokens

The erc20 deposit command deposits ERC-20 tokens in the DApp.

yarn start erc20 deposit --amount [amount] <options>

Examples:

  1. Deposit 10 CTSI in the locally deployed DApp:

    yarn start erc20 deposit --amount 10000000000000000000
  2. Deposit 10 CTSI in the echo-python DApp instance already deployed to the Ethereum Goerli testnet, using a user's account and a user's gateway RPC on Alchemy:

    export MNEMONIC=<user sequence of twelve words>
    export RPC_URL=https://eth-goerli.alchemyapi.io/v2/<USER_KEY>
    
    yarn start erc20 deposit --amount 10000000000000000000 --dapp echo-python

Options are:

--erc20         ERC-20 address
--rpc           URL of the RPC gateway to use
--address       DApp Rollups contract address
--addressFile   Path to a file containing the DApp Rollups contract address
--dapp          DApp name
--mnemonic      Wallet mnemonic
--accountIndex  Account index from mnemonic

If the erc20 address is unspecified, the appropriate address for the CTSI token will be used for the target blockchain being specified.

Aside from that, the other parameters have the same behavior as described for the input send command.