Getting Started with ResContract CLI πŸš€

 

This guide provides a comprehensive walkthrough of the ResContract CLI for managing smart contracts on ResilientDB. It covers installation, configuration, and usage of various commands to create ac...

This guide provides a comprehensive walkthrough of the ResContract CLI for managing smart contracts on ResilientDB. It covers installation, configuration, and usage of various commands to create accounts, compile contracts, deploy contracts, and execute contract functions.

ResContract CLI GitHub Repository


🌐 Smart Contracts on ResilientDB Today

ResilientDB now supports smart contracts, allowing developers to deploy and interact with decentralized applications seamlessly. Here’s an overview of the current state:

  • Simplified Workflow: The ResContract CLI provides an intuitive interface for managing smart contracts.
  • Comprehensive Commands: It includes commands for account creation, contract compilation, deployment, and execution.
  • Enhanced Logging: Improved logging for better debugging and auditing.

πŸ“ˆ Getting Started with ResContract CLI

Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (version >= 14): Download and install Node.js
  • npm: Comes with Node.js. Ensure it’s up-to-date.
  • Solidity Compiler (solc): Required to compile smart contracts.

Installing solc

Linux (Ubuntu/Debian):

sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install -y solc

macOS:

brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity
  • ResilientDB: A running instance with the smart contracts service enabled.

Installing ResContract CLI

Install the ResContract CLI globally using npm:

npm install -g rescontract-cli

Configuration

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.

Option 1: Set ResDB_Home Environment Variable

Set the ResDB_Home environment variable to point to the directory where ResilientDB is installed.

Linux/macOS:

export ResDB_Home=/path/to/incubator-resilientdb

Add the above line to your .bashrc or .zshrc file to make it persistent.

Option 2: Use a config.yaml File

Create a config.yaml file in the same directory where you run the rescontract command or in your home directory.

Example config.yaml:

ResDB_Home: /path/to/incubator-resilientdb

Ensure the ResDB_Home path is correct.

Note: The CLI checks for config.yaml in the current directory first, then in your home directory.

πŸ‘¨πŸ»β€πŸ’» Using the ResContract CLI

Command Overview

  • create: Create a new account.
  • compile: Compile a Solidity contract.
  • deploy: Deploy a smart contract.
  • execute: Execute a function within a deployed smart contract.
  • add_address: Add external addresses to the system.
  • list-deployments: List all deployed contracts.
  • clear-registry: Clear the deployment registry.

Creating a New Account

Command:

rescontract create --config <path_to_config>

Example:

rescontract create --config ../incubator-resilientdb/service/tools/config/interface/service.config

Sample Output:

0x3b706424119e09dcaad4acf21b10af3b33cde350

Compiling a Smart Contract

Command:

rescontract compile --sol <inputFile.sol> --output <outputFile.json>

Example:

rescontract compile --sol /users/gopuman/ResContract/token.sol --output output.json

Sample Output:

Compiled successfully to output.json

Deploying a Smart Contract

Command:

rescontract deploy --config <configPath> --contract <contract.json> \
--name <contractName> --arguments "<parameters>" --owner <ownerAddress>

Example:

rescontract deploy \
  --config ../incubator-resilientdb/service/tools/config/interface/service.config \
  --contract output.json \
  --name "/users/gopuman/ResContract/token.sol:Token" \
  --arguments "1000" \
  --owner "0x3b706424119e09dcaad4acf21b10af3b33cde350"

Sample Output:

owner_address: "0x3b706424119e09dcaad4acf21b10af3b33cde350"
contract_address: "0xc975ab41e0c2042a0229925a2f4f544747fd66cd"
contract_name: "/users/gopuman/ResContract/token.sol:Token"

Executing a Smart Contract Function

Command:

rescontract execute --config <configPath> --sender <senderAddress> \
--contract <contractAddress> --function-name "<functionSignature>" --arguments "<parameters>"

Example:

rescontract execute \
  --config ../incubator-resilientdb/service/tools/config/interface/service.config \
  --sender "0x3b706424119e09dcaad4acf21b10af3b33cde350" \
  --contract "0xc975ab41e0c2042a0229925a2f4f544747fd66cd" \
  --function-name "transfer(address,uint256)" \
  --arguments "0x4847155cbb6f2219ba9b7df50be11a1c7f23f829,100"

Sample Output:

Function executed successfully.

Adding External Addresses

Command:

rescontract add_address --config <path> --external-address <address>

Example:

rescontract add_address --config ../incubator-resilientdb/service/tools/config/interface/service.config \
--external-address 0xExternalAddress

Listing Deployed Contracts

Command:

rescontract list-deployments

Example:

rescontract list-deployments

This command displays all deployed contracts with their owner addresses, contract names, and contract addresses.

Clearing the Registry

Command:

rescontract clear-registry

Example:

rescontract clear-registry

Warning: This command permanently removes all deployment tracking information.

πŸ“‹ Deployment Registry

The ResContract CLI automatically tracks all deployed contracts in a registry file located at ~/.rescontract_deployed_contracts.json. This registry provides several benefits:

  • Duplicate Prevention: Prevents deploying the same contract with the same owner and name
  • Contract Tracking: Maintains a record of all deployed contracts with their addresses
  • Easy Management: Use list-deployments to view all contracts and clear-registry to reset

The registry stores the following information for each deployment:

  • Owner address
  • Contract name
  • Contract address
  • Deployment timestamp

πŸ“ Advanced Usage and Tips

  • Logging: The ResContract CLI logs important events and errors to ~/.rescontract-logs/cli.log.
  • Error Handling: If you encounter errors, check the logs for detailed information.
  • Permissions: Ensure you have the necessary permissions to execute commands and access files.
  • Registry Management: Use list-deployments to track your deployed contracts and clear-registry to reset when needed.
  • Duplicate Prevention: The registry automatically prevents deploying the same contract with the same owner and name.
  • Updating ResContract CLI: Keep your CLI up-to-date by running:
npm update -g rescontract-cli

🀝 Contributing

We welcome contributions! Please read our Contributing Guidelines to get started.

πŸ“„ License

This project is licensed under the Apache License

πŸ“š Additional Resources