Intelligent Contracts
Tooling Setup

Tooling Setup

This guide will help you set up the GenLayer environment by installing the GenLayer CLI and launching the GenLayer Studio, write your first Intelligent Contract, deploy it, and interact with it using the GenLayer Studio. You'll also learn how to use the Genlayer Project Boilerplate to write end-to-end tests and build a frontend dApp with GenLayerJS.

Table of Contents

  1. Using the GenLayer Studio
  2. Local Installation of the GenLayer CLI
  3. Launching the GenLayer Studio
  4. Writing Intelligent Contracts
  5. Deploying and Interacting with Intelligent Contracts
  6. Writing End-to-End Tests with genlayer-project-boilerplate
  7. Building a Frontend dApp with GenLayerJS

Using the GenLayer Studio

The GenLayer Studio is a web-based interface for developing, testing, and deploying Intelligent Contracts. It provides a user-friendly environment for interacting with the GenLayer ecosystem. You can find it at studio.genlayer.com (opens in a new tab).

Local Installation of the GenLayer CLI

The GenLayer CLI is used to set up the GenLayer Studio and, in the future, mainnet and testnet environments.

Prerequisites

Ensure you have the following installed and updated:


Installation Steps

  1. Install GenLayer CLI

    Open your terminal and run:

    npm install -g genlayer
  2. Initialize GenLayer Environment

    Run the following command to set up your development environment:

    genlayer init

    During initialization, you'll be prompted to select your preferred LLM provider(s) and enter any required API keys.

    Optional Initialization Parameters

    You can customize the initialization with the following options:

    • --numValidators <number>: Specify the number of validators (default is 5)
    • --headless: Run in headless mode without UI (default is false)
    • --reset-db: Reset the database to a clean state (default is false)
    • --localnet-version <version>: Specify the localnet version to use (default is latest stable version)

    Example:

    genlayer init --numValidators 3 --headless --reset-db --localnet-version v0.32.1

Launching the GenLayer Studio

After initializing the environment, you can start the Studio by running:

genlayer up

This command launches the GenLayer Studio using your existing configuration.

Optional Parameters:

  • --reset-validators: Removes all current validators and creates new ones.
  • --numValidators <number>: Specify the number of validators to start.
  • --headless: Run in headless mode without UI (default is false)
  • --reset-db: Reset the database to a clean state (default is false)

Example:

genlayer up --reset-validators --numValidators 3 --headless --reset-db

Once the Studio is running, access it at http://localhost:8080/ (opens in a new tab).

Writing Intelligent Contracts

Refer to the Your First Contract page for more information on how to write Intelligent Contracts.

Deploying and Interacting with Intelligent Contracts

Deploying the Contract

  1. Load Your Contract
    In the GenLayer Studio, navigate to the Contracts section and upload your contract file.

  2. Deploy the Contract
    Navigate to the Run and Deploy section and click the "Deploy" button.
    The Studio will automatically detect constructor parameters. Provide the required values.
    Upon successful deployment, the contract address will be displayed.

Interacting with the Contract

  1. Read Methods
    View the current state of the contract using Read Methods. These are methods that return data without modifying the state.

  2. Write Methods
    Execute Write Methods to interact with your contract. Provide any required input parameters.

  3. Execution Logs
    Monitor transactions and validator consensus via the Node Logs at the bottom of the Studio.

  4. Transaction Details
    View detailed information about a transaction by clicking on it in the bottom left list of transactions.

Writing End-to-End Tests with genlayer-project-boilerplate

The genlayer-project-boilerplate (opens in a new tab) provides a starting point for writing end-to-end (e2e) tests for your Intelligent Contracts.

Setting Up the Boilerplate

  1. Clone the Repository

On a location different from the GenLayer Studio, clone the repository:

  git clone https://github.com/yeagerai/genlayer-project-boilerplate.git
  1. Install Dependencies
    Navigate to the project directory and install the required packages:
  cd genlayer-project-boilerplate
  npm install
  1. Configure Environment
    Ensure the GenLayer Studio is running. Update the configuration files if necessary.

Writing E2E Tests

  1. Create Test Files
    In the test directory, create test files using your pytest as a testing framework.

  2. Example Test

  import pytest
  from genlayer import GenLayer, Account
 
  def test_simple_contract():
      # Account
      account_1 = create_new_account()
 
      # Deploy contract
      contract_code = open("contracts/my_contract.py", "r").read()
      contract_address, transaction_response_deploy = deploy_intelligent_contract(
        account_1,
        contract_code,
        "{value = 10}",
      )
      
      # Test initial state
      initial_value = call_contract_method(
        contract_address, account_1, "get_value", []
      )
      assert initial_value == 10
      
      # Test state change
      send_transaction_response = send_transaction(
        account_1,
        contract_address,
        "set_value",
        ["20"],
      )
      
      # Test updated state
      updated_value = call_contract_method(
        contract_address, account_1, "get_value", []
      )
      assert updated_value == 20

Note on Deployment: For deployment, developers should currently use the GenLayer Studio UI. Deploying through the CLI is not yet supported.

Building a Frontend dApp with GenLayerJS

You can build a frontend decentralized application (dApp) that interacts with your Intelligent Contract using GenLayerJS, a JavaScript SDK for GenLayer.

You can start by cloning the genlayer-dapp-boilerplate (opens in a new tab) repository or start from scratch with a new frontend project.

After you have your project ready, continue with the following steps:

Installing GenLayerJS

  npm install genlayer-js

Connecting to GenLayer

  import { createClient } from 'genlayer-js';
  const client = createClient(config);

Interacting with Contracts

  1. Read from a Contract
  const value = await client.readContract({
      address: contractAddress,
      functionName: "get_value",
      args: [],
  });
  1. Update Contract State
  const txHash = await client.writeContract({
      address: contractAddress,
      functionName: "set_value",
      args: [newValue],
  });
  const receipt = await client.waitForTransactionReceipt({
    hash: txHash,
    status: "FINALIZED",
  });

Running the Frontend

Ensure your frontend application is configured to connect to the GenLayer Studio's RPC endpoint (http://localhost:4000/api (opens in a new tab)).

Start your frontend application:

  npm run dev