import { CommentSection } from '@/components/CommentSection'
import { Divider, Box, Space, Tabs, TabsList, TabsTab, TabsPanel, Code, Alert } from '@mantine/core'
import { IconInfoCircle } from '@tabler/icons-react'
import { ResContractPlayground } from '@/components/ResContractPlayground'

# ResContract

ResContract CLI is a command-line tool for creating, deploying, and managing smart contracts within the ResilientDB ecosystem. It provides a streamlined interface for developers and students to interact with smart contracts efficiently.

## Interactive Tools

Under Development!

## Features

* Create Smart Contracts: Generate new smart contract templates
* Compile Contracts: Compile Solidity contracts to JSON
* Deploy Smart Contracts: Deploy contracts to the blockchain
* Execute Functions: Interact with and manage deployed contracts

## Prerequisites

Before installing and using the ResContract CLI, ensure you have the following prerequisites installed on your system:

* **Node.js (version >= 14)**: [Download and install Node.js](https://nodejs.org/en/download/)
* **npm**: Comes with Node.js. Ensure it's up-to-date.
* **Solidity Compiler (`solc`)**: Required to compile smart contracts.

### Installing `solc`

<Tabs defaultValue="linux">
  <TabsList>
    <TabsTab value="linux">Linux</TabsTab>
    <TabsTab value="macos">macOS</TabsTab>
  </TabsList>

  <TabsPanel value="linux">
    <Code block>
      {`sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install -y solc`}
    </Code>
  </TabsPanel>

  <TabsPanel value="macos">
    <Code block>
      {`brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity`}
    </Code>
  </TabsPanel>
</Tabs>

## Installation

Install the ResContract CLI globally using npm:

<Code block>
  {`npm install -g rescontract-cli`}
</Code>

## Configuration

<Alert icon={<IconInfoCircle size={16} />} title="Important" color="blue">
  Before using the ResContract CLI, you must set the ResDB_Home environment variable or provide the path to your ResilientDB installation in a config.yaml file. The CLI will not prompt you for this path and will exit with an error if it's not set.
</Alert>
<br />
<Tabs defaultValue="env">
  <TabsList>
    <TabsTab value="env">Environment Variable</TabsTab>
    <TabsTab value="config">Config File</TabsTab>
  </TabsList>

  <TabsPanel value="env">
    Set the ResDB_Home environment variable to point to the directory where ResilientDB is installed.

    <Code block>
      {`export ResDB_Home=/path/to/resilientdb`}
    </Code>

    Add the above line to your `.bashrc` or `.zshrc` file to make it persistent.
  </TabsPanel>

  <TabsPanel value="config">
    Update the `config.yaml` file in the same directory where you run the `rescontract` command or in your home directory.

    <Code block>
      {`ResDB_Home: /path/to/resilientdb`}
    </Code>

    Ensure the `ResDB_Home` path is correct.

    <Alert icon={<IconInfoCircle size={16} />} title="Note" color="blue">
      The CLI checks for config.yaml in the current directory first, then in your home directory.
    </Alert>
  </TabsPanel>
</Tabs>

## Usage

After installation, you can use the `rescontract` command in your terminal.

<Code block>
  {`rescontract <command> [options]`}
</Code>

<Tabs defaultValue="create">
  <TabsList>
    <TabsTab value="create">Create</TabsTab>
    <TabsTab value="compile">Compile</TabsTab>
    <TabsTab value="deploy">Deploy</TabsTab>
    <TabsTab value="execute">Execute</TabsTab>
  </TabsList>

  <TabsPanel value="create">
    The `create` command initializes a new account using ResilientDB's smart contract tools.

    <Code block>
      {`rescontract create --config <path_to_config>`}
    </Code>

    Parameters:
    * `--config, -c`: Path to the configuration file.

    Example:
    <Code block>
      {`rescontract create --config ~/resilientdb/config/service.config`}
    </Code>
  </TabsPanel>

  <TabsPanel value="compile">
    The `compile` command compiles a Solidity smart contract into a JSON file using `solc`.

    <Code block>
      {`rescontract compile --sol <inputFile.sol> --output <outputFile.json>`}
    </Code>

    Parameters:
    * `--sol, -s`: Path to the Solidity smart contract file.
    * `--output, -o`: Name of the resulting JSON file.

    Example:
    <Code block>
      {`rescontract compile --sol contracts/MyToken.sol --output build/MyToken.json`}
    </Code>
  </TabsPanel>

  <TabsPanel value="deploy">
    The `deploy` command deploys the smart contract to the blockchain.

    <Code block>
      {`rescontract deploy --config <service.config> --contract <contract.json> \
--name <tokenName> --arguments "<parameters>" --owner <address>`}
    </Code>

    Parameters:
    * `--config, -c`: Client configuration path.
    * `--contract, -p`: Path to the contract JSON file.
    * `--name, -n`: Name of the contract.
    * `--arguments, -a`: Parameters to create the contract object (enclosed in quotes).
    * `--owner, -m`: Contract owner's address.

    Example:
    <Code block>
      {`rescontract deploy --config ~/resilientdb/config/service.config \
--contract build/MyToken.json --name MyToken \
--arguments "1000000" --owner 0xYourAddress`}
    </Code>
  </TabsPanel>

  <TabsPanel value="execute">
    The `execute` command executes a smart contract function.

    <Code block>
      {`rescontract execute --config <service.config> --sender <senderAddress> \
--contract <contractAddress> --function-name <functionName> --arguments "<parameters>"`}
    </Code>

    Parameters:
    * `--config, -c`: Path to the client configuration file.
    * `--sender, -m`: Address of the sender executing the function.
    * `--contract, -s`: Address of the deployed contract.
    * `--function-name, -f`: Name of the function to execute (include parameter types).
    * `--arguments, -a`: Arguments to pass to the function (enclosed in quotes).

    Example:
    <Code block>
      {`rescontract execute --config ~/resilientdb/config/service.config \
--sender 0xYourAddress --contract 0xContractAddress \
--function-name "transfer(address,uint256)" \
--arguments "0xRecipientAddress,100"`}
    </Code>

    <Alert icon={<IconInfoCircle size={16} />} title="Note" color="blue">
      Make sure to include the parameter types in the function name (e.g., "transfer(address,uint256)") and provide the correct number of arguments in the correct order.
    </Alert>
  </TabsPanel>
</Tabs>

## License

This project is licensed under the Apache License 2.0 - see the [LICENSE](https://github.com/apache/incubator-resilientdb-ResContract/blob/main/LICENSE) file for details.

<Space h="xl" />
<Divider my="xl" label="Community Feedback" labelPosition="center" />

<Box mb="xl">
  <CommentSection
    pageTitle="ResContract CLI Documentation"
    pageUrl={typeof window !== 'undefined' ? window.location.href : ''}
    repoOwner="apache"
    repoName="incubator-resilientdb-ResContract"
    labels={['user-feedback', 'documentation', 'rescontract']}
    title="Questions or Feedback about ResContract?"
  />
</Box> 