Getting started

Getting Started with GenLayer

Welcome to the GenLayer getting started guide. This guide will help you set up the GenLayer environment, 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. Installation of the GenLayer CLI
  3. Launching the GenLayer Simulator
  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).
    • --branch <branch>: Specify the branch of the GenLayer Studio to use (default is "main").

    Example:

    genlayer init --numValidators 3 --branch staging

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.
  • --branch <branch>: Specify the branch to start the GenLayer Studio from.

Example:

genlayer up --reset-validators --numValidators 3 --branch staging

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

Writing Intelligent Contracts

Refer to the Intelligent Contracts 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. Set Constructor Parameters
    The Studio will automatically detect constructor parameters. Provide the required values.

  3. Deploy the Contract
    Navigate to the Run and Deploy section and click the "Deploy" button. 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. Transaction Logs
    Monitor transactions and validator consensus via the Node Logs at the bottom of the Studio.

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

Conclusion

You've now set up the GenLayer environment, written and deployed an Intelligent Contract, and interacted with it through both the Studio and a frontend dApp using GenLayerJS. Explore further by customizing your contracts, writing more comprehensive tests, and enhancing your dApp's functionality.